Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / services / api / script / migrate-gitolite-to-uuid-storage.rb
1 #!/usr/bin/env ruby
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 #
7 # Prior to April 2015, Arvados Gitolite integration stored repositories by
8 # name.  To improve user repository management, we switched to storing
9 # repositories by UUID, and aliasing them to names.  This makes it easy to
10 # have rich name hierarchies, and allow users to rename repositories.
11 #
12 # This script will migrate a name-based Gitolite configuration to a UUID-based
13 # one.  To use it:
14 #
15 # 1. Change the value of REPOS_DIR below, if needed.
16 # 2. Install this script in the same directory as `update-gitolite.rb`.
17 # 3. Ensure that no *other* users can access Gitolite: edit gitolite's
18 #    authorized_keys file so it only contains the arvados_git_user key,
19 #    and disable the update-gitolite cron job.
20 # 4. Run this script: `ruby migrate-gitolite-to-uuid-storage.rb production`.
21 # 5. Undo step 3.
22
23 require 'rubygems'
24 require 'pp'
25 require 'arvados'
26 require 'tempfile'
27 require 'yaml'
28
29 REPOS_DIR = "/var/lib/gitolite/repositories"
30
31 # Default is development
32 production = ARGV[0] == "production"
33
34 ENV["RAILS_ENV"] = "development"
35 ENV["RAILS_ENV"] = "production" if production
36
37 DEBUG = 1
38
39 # load and merge in the environment-specific application config info
40 # if present, overriding base config parameters as specified
41 path = File.dirname(__FILE__) + '/config/arvados-clients.yml'
42 if File.exist?(path) then
43   cp_config = YAML.load_file(path)[ENV['RAILS_ENV']]
44 else
45   puts "Please create a\n " + File.dirname(__FILE__) + "/config/arvados-clients.yml\n file"
46   exit 1
47 end
48
49 gitolite_url = cp_config['gitolite_url']
50 gitolite_arvados_git_user_key = cp_config['gitolite_arvados_git_user_key']
51
52 gitolite_tmpdir = File.join(File.absolute_path(File.dirname(__FILE__)),
53                             cp_config['gitolite_tmp'])
54 gitolite_admin = File.join(gitolite_tmpdir, 'gitolite-admin')
55 gitolite_keydir = File.join(gitolite_admin, 'keydir', 'arvados')
56
57 ENV['ARVADOS_API_HOST'] = cp_config['arvados_api_host']
58 ENV['ARVADOS_API_TOKEN'] = cp_config['arvados_api_token']
59 if cp_config['arvados_api_host_insecure']
60   ENV['ARVADOS_API_HOST_INSECURE'] = 'true'
61 else
62   ENV.delete('ARVADOS_API_HOST_INSECURE')
63 end
64
65 def ensure_directory(path, mode)
66   begin
67     Dir.mkdir(path, mode)
68   rescue Errno::EEXIST
69   end
70 end
71
72 def replace_file(path, contents)
73   unlink_now = true
74   dirname, basename = File.split(path)
75   new_file = Tempfile.new([basename, ".tmp"], dirname)
76   begin
77     new_file.write(contents)
78     new_file.flush
79     File.rename(new_file, path)
80     unlink_now = false
81   ensure
82     new_file.close(unlink_now)
83   end
84 end
85
86 def file_has_contents?(path, contents)
87   begin
88     IO.read(path) == contents
89   rescue Errno::ENOENT
90     false
91   end
92 end
93
94 module TrackCommitState
95   module ClassMethods
96     # Note that all classes that include TrackCommitState will have
97     # @@need_commit = true if any of them set it.  Since this flag reports
98     # a boolean state of the underlying git repository, that's OK in the
99     # current implementation.
100     @@need_commit = false
101
102     def changed?
103       @@need_commit
104     end
105
106     def ensure_in_git(path, contents)
107       unless file_has_contents?(path, contents)
108         replace_file(path, contents)
109         system("git", "add", path)
110         @@need_commit = true
111       end
112     end
113   end
114
115   def ensure_in_git(path, contents)
116     self.class.ensure_in_git(path, contents)
117   end
118
119   def self.included(base)
120     base.extend(ClassMethods)
121   end
122 end
123
124 class Repository
125   include TrackCommitState
126
127   @@aliases = {}
128
129   def initialize(arv_repo)
130     @arv_repo = arv_repo
131   end
132
133   def self.ensure_system_config(conf_root)
134     ensure_in_git(File.join(conf_root, "arvadosaliases.pl"), alias_config)
135   end
136
137   def self.rename_repos(repos_root)
138     @@aliases.each_pair do |uuid, name|
139       begin
140         File.rename(File.join(repos_root, "#{name}.git/"),
141                     File.join(repos_root, "#{uuid}.git"))
142       rescue Errno::ENOENT
143       end
144       if name == "arvados"
145         Dir.chdir(repos_root) { File.symlink("#{uuid}.git/", "arvados.git") }
146       end
147     end
148   end
149
150   def ensure_config(conf_root)
151     return if name.nil?
152     @@aliases[uuid] = name
153     name_conf_path = auto_conf_path(conf_root, name)
154     return unless File.exist?(name_conf_path)
155     conf_file = IO.read(name_conf_path)
156     conf_file.gsub!(/^repo #{Regexp.escape(name)}$/m, "repo #{uuid}")
157     ensure_in_git(auto_conf_path(conf_root, uuid), conf_file)
158     File.unlink(name_conf_path)
159     system("git", "rm", "--quiet", name_conf_path)
160   end
161
162   private
163
164   def auto_conf_path(conf_root, basename)
165     File.join(conf_root, "conf", "auto", "#{basename}.conf")
166   end
167
168   def uuid
169     @arv_repo[:uuid]
170   end
171
172   def name
173     if @arv_repo[:name].nil?
174       nil
175     else
176       @clean_name ||=
177         @arv_repo[:name].sub(/^[^A-Za-z]+/, "").gsub(/[^\w\.\/]/, "")
178     end
179   end
180
181   def self.alias_config
182     conf_s = "{\n"
183     @@aliases.sort.each do |(repo_name, repo_uuid)|
184       conf_s += "\t'#{repo_name}' \t=> '#{repo_uuid}',\n"
185     end
186     conf_s += "};\n"
187     conf_s
188   end
189 end
190
191 begin
192   # Get our local gitolite-admin repo up to snuff
193   if not File.exist?(gitolite_admin) then
194     ensure_directory(gitolite_tmpdir, 0700)
195     Dir.chdir(gitolite_tmpdir)
196     `git clone #{gitolite_url}`
197     Dir.chdir(gitolite_admin)
198   else
199     Dir.chdir(gitolite_admin)
200     `git pull`
201   end
202
203   arv = Arvados.new
204   permissions = arv.repository.get_all_permissions
205
206   permissions[:repositories].each do |repo_record|
207     repo = Repository.new(repo_record)
208     repo.ensure_config(gitolite_admin)
209   end
210   Repository.ensure_system_config(gitolite_admin)
211
212   message = "#{Time.now().to_s}: migrate to storing repositories by UUID"
213   Dir.chdir(gitolite_admin)
214   `git add --all`
215   `git commit -m '#{message}'`
216   Repository.rename_repos(REPOS_DIR)
217   `git push`
218
219 rescue => bang
220   puts "Error: " + bang.to_s
221   puts bang.backtrace.join("\n")
222   exit 1
223 end
224