3 # Prior to April 2015, Arvados Gitolite integration stored repositories by
4 # name. To improve user repository management, we switched to storing
5 # repositories by UUID, and aliasing them to names. This makes it easy to
6 # have rich name hierarchies, and allow users to rename repositories.
8 # This script will migrate a name-based Gitolite configuration to a UUID-based
11 # 1. Change the value of REPOS_DIR below, if needed.
12 # 2. Install this script in the same directory as `update-gitolite.rb`.
13 # 3. Ensure that no *other* users can access Gitolite: edit gitolite's
14 # authorized_keys file so it only contains the arvados_git_user key,
15 # and disable the update-gitolite cron job.
16 # 4. Run this script: `ruby migrate-gitolite-to-uuid-storage.rb production`.
25 REPOS_DIR = "/var/lib/gitolite/repositories"
27 # Default is development
28 production = ARGV[0] == "production"
30 ENV["RAILS_ENV"] = "development"
31 ENV["RAILS_ENV"] = "production" if production
35 # load and merge in the environment-specific application config info
36 # if present, overriding base config parameters as specified
37 path = File.dirname(__FILE__) + '/config/arvados-clients.yml'
38 if File.exists?(path) then
39 cp_config = YAML.load_file(path)[ENV['RAILS_ENV']]
41 puts "Please create a\n " + File.dirname(__FILE__) + "/config/arvados-clients.yml\n file"
45 gitolite_url = cp_config['gitolite_url']
46 gitolite_arvados_git_user_key = cp_config['gitolite_arvados_git_user_key']
48 gitolite_tmpdir = File.join(File.absolute_path(File.dirname(__FILE__)),
49 cp_config['gitolite_tmp'])
50 gitolite_admin = File.join(gitolite_tmpdir, 'gitolite-admin')
51 gitolite_keydir = File.join(gitolite_admin, 'keydir', 'arvados')
53 ENV['ARVADOS_API_HOST'] = cp_config['arvados_api_host']
54 ENV['ARVADOS_API_TOKEN'] = cp_config['arvados_api_token']
55 if cp_config['arvados_api_host_insecure']
56 ENV['ARVADOS_API_HOST_INSECURE'] = 'true'
58 ENV.delete('ARVADOS_API_HOST_INSECURE')
61 def ensure_directory(path, mode)
68 def replace_file(path, contents)
70 dirname, basename = File.split(path)
71 new_file = Tempfile.new([basename, ".tmp"], dirname)
73 new_file.write(contents)
75 File.rename(new_file, path)
78 new_file.close(unlink_now)
82 def file_has_contents?(path, contents)
84 IO.read(path) == contents
90 module TrackCommitState
92 # Note that all classes that include TrackCommitState will have
93 # @@need_commit = true if any of them set it. Since this flag reports
94 # a boolean state of the underlying git repository, that's OK in the
95 # current implementation.
102 def ensure_in_git(path, contents)
103 unless file_has_contents?(path, contents)
104 replace_file(path, contents)
105 system("git", "add", path)
111 def ensure_in_git(path, contents)
112 self.class.ensure_in_git(path, contents)
115 def self.included(base)
116 base.extend(ClassMethods)
121 include TrackCommitState
125 def initialize(arv_repo)
129 def self.ensure_system_config(conf_root)
130 ensure_in_git(File.join(conf_root, "arvadosaliases.pl"), alias_config)
133 def self.rename_repos(repos_root)
134 @@aliases.each_pair do |uuid, name|
136 File.rename(File.join(repos_root, "#{name}.git/"),
137 File.join(repos_root, "#{uuid}.git"))
141 Dir.chdir(repos_root) { File.symlink("#{uuid}.git/", "arvados.git") }
146 def ensure_config(conf_root)
148 @@aliases[uuid] = name
149 name_conf_path = auto_conf_path(conf_root, name)
150 return unless File.exist?(name_conf_path)
151 conf_file = IO.read(name_conf_path)
152 conf_file.gsub!(/^repo #{Regexp.escape(name)}$/m, "repo #{uuid}")
153 ensure_in_git(auto_conf_path(conf_root, uuid), conf_file)
154 File.unlink(name_conf_path)
155 system("git", "rm", "--quiet", name_conf_path)
160 def auto_conf_path(conf_root, basename)
161 File.join(conf_root, "conf", "auto", "#{basename}.conf")
169 if @arv_repo[:name].nil?
173 @arv_repo[:name].sub(/^[^A-Za-z]+/, "").gsub(/[^\w\.\/]/, "")
177 def self.alias_config
179 @@aliases.sort.each do |(repo_name, repo_uuid)|
180 conf_s += "\t'#{repo_name}' \t=> '#{repo_uuid}',\n"
188 # Get our local gitolite-admin repo up to snuff
189 if not File.exists?(gitolite_admin) then
190 ensure_directory(gitolite_tmpdir, 0700)
191 Dir.chdir(gitolite_tmpdir)
192 `git clone #{gitolite_url}`
193 Dir.chdir(gitolite_admin)
195 Dir.chdir(gitolite_admin)
200 permissions = arv.repository.get_all_permissions
202 permissions[:repositories].each do |repo_record|
203 repo = Repository.new(repo_record)
204 repo.ensure_config(gitolite_admin)
206 Repository.ensure_system_config(gitolite_admin)
208 message = "#{Time.now().to_s}: migrate to storing repositories by UUID"
209 Dir.chdir(gitolite_admin)
211 `git commit -m '#{message}'`
212 Repository.rename_repos(REPOS_DIR)
216 puts "Error: " + bang.to_s
217 puts bang.backtrace.join("\n")