9 # This script does the actual gitolite config management on disk.
11 # Ward Vandewege <ward@curoverse.com>
13 # Default is development
14 production = ARGV[0] == "production"
16 ENV["RAILS_ENV"] = "development"
17 ENV["RAILS_ENV"] = "production" if production
21 # load and merge in the environment-specific application config info
22 # if present, overriding base config parameters as specified
23 path = File.dirname(__FILE__) + '/config/arvados-clients.yml'
24 if File.exists?(path) then
25 cp_config = YAML.load_file(path)[ENV['RAILS_ENV']]
27 puts "Please create a\n " + File.dirname(__FILE__) + "/config/arvados-clients.yml\n file"
31 gitolite_url = cp_config['gitolite_url']
32 gitolite_arvados_git_user_key = cp_config['gitolite_arvados_git_user_key']
34 gitolite_tmpdir = File.join(File.absolute_path(File.dirname(__FILE__)),
35 cp_config['gitolite_tmp'])
36 gitolite_admin = File.join(gitolite_tmpdir, 'gitolite-admin')
37 gitolite_keydir = File.join(gitolite_admin, 'keydir', 'arvados')
39 ENV['ARVADOS_API_HOST'] = cp_config['arvados_api_host']
40 ENV['ARVADOS_API_TOKEN'] = cp_config['arvados_api_token']
41 if cp_config['arvados_api_host_insecure']
42 ENV['ARVADOS_API_HOST_INSECURE'] = 'true'
44 ENV.delete('ARVADOS_API_HOST_INSECURE')
47 def ensure_directory(path, mode)
54 def replace_file(path, contents)
56 dirname, basename = File.split(path)
57 new_file = Tempfile.new([basename, ".tmp"], dirname)
59 new_file.write(contents)
61 File.rename(new_file, path)
64 new_file.close(unlink_now)
68 def file_has_contents?(path, contents)
70 IO.read(path) == contents
76 module TrackCommitState
78 # Note that all classes that include TrackCommitState will have
79 # @@need_commit = true if any of them set it. Since this flag reports
80 # a boolean state of the underlying git repository, that's OK in the
81 # current implementation.
88 def ensure_in_git(path, contents)
89 unless file_has_contents?(path, contents)
90 replace_file(path, contents)
91 system("git", "add", path)
97 def ensure_in_git(path, contents)
98 self.class.ensure_in_git(path, contents)
101 def self.included(base)
102 base.extend(ClassMethods)
107 include TrackCommitState
109 def initialize(user_keys_map, key_dir)
110 @user_keys_map = user_keys_map
115 def install(filename, pubkey)
117 key_path = File.join(@key_dir, filename)
118 ensure_in_git(key_path, pubkey)
120 @installed[filename] = true
123 def ensure_keys_for_user(user_uuid)
124 return unless key_list = @user_keys_map.delete(user_uuid)
125 key_list.map { |k| k[:public_key] }.compact.each_with_index do |pubkey, ii|
126 # Handle putty-style ssh public keys
127 pubkey.sub!(/^(Comment: "r[^\n]*\n)(.*)$/m,'ssh-rsa \2 \1')
128 pubkey.sub!(/^(Comment: "d[^\n]*\n)(.*)$/m,'ssh-dss \2 \1')
129 pubkey.gsub!(/\n/,'')
131 install("#{user_uuid}@#{ii}.pub", pubkey)
135 def installed?(filename)
141 include TrackCommitState
145 def initialize(arv_repo, user_keys)
147 @user_keys = user_keys
150 def self.ensure_system_config(conf_root)
151 ensure_in_git(File.join(conf_root, "conf", "gitolite.conf"),
152 %Q{include "auto/*.conf"\ninclude "admin/*.conf"\n})
153 ensure_in_git(File.join(conf_root, "arvadosaliases.pl"), alias_config)
155 conf_path = File.join(conf_root, "conf", "admin", "arvados.conf")
157 @arvados_git_user = arvados_git_user
160 RW = @arvados_git_user
163 ensure_directory(File.dirname(conf_path), 0755)
164 ensure_in_git(conf_path, conf_file)
167 def ensure_config(conf_root)
168 if name and (File.exist?(auto_conf_path(conf_root, name)))
169 # This gitolite installation knows the repository by name, rather than
170 # UUID. Leave it configured that way until a separate migration is run.
174 @@aliases[name] = uuid unless name.nil?
176 conf_file = "\nrepo #{basename}\n"
177 @arv_repo[:user_permissions].sort.each do |user_uuid, perm|
178 conf_file += "\t#{perm[:gitolite_permissions]}\t= #{user_uuid}\n"
179 @user_keys.ensure_keys_for_user(user_uuid)
181 ensure_in_git(auto_conf_path(conf_root, basename), conf_file)
186 def auto_conf_path(conf_root, basename)
187 File.join(conf_root, "conf", "auto", "#{basename}.conf")
195 if @arv_repo[:name].nil?
199 @arv_repo[:name].sub(/^[^A-Za-z]+/, "").gsub(/[^\w\.\/]/, "")
203 def self.alias_config
205 @@aliases.sort.each do |(repo_name, repo_uuid)|
206 conf_s += "\t'#{repo_name}' \t=> '#{repo_uuid}',\n"
214 # Get our local gitolite-admin repo up to snuff
215 if not File.exists?(gitolite_admin) then
216 ensure_directory(gitolite_tmpdir, 0700)
217 Dir.chdir(gitolite_tmpdir)
218 `git clone #{gitolite_url}`
219 Dir.chdir(gitolite_admin)
221 Dir.chdir(gitolite_admin)
226 permissions = arv.repository.get_all_permissions
228 ensure_directory(gitolite_keydir, 0700)
229 user_ssh_keys = UserSSHKeys.new(permissions[:user_keys], gitolite_keydir)
230 # Make sure the arvados_git_user key is installed
231 user_ssh_keys.install('arvados_git_user.pub', gitolite_arvados_git_user_key)
233 permissions[:repositories].each do |repo_record|
234 repo = Repository.new(repo_record, user_ssh_keys)
235 repo.ensure_config(gitolite_admin)
237 Repository.ensure_system_config(gitolite_admin)
239 # Clean up public key files that should not be present
240 Dir.chdir(gitolite_keydir)
241 stale_keys = Dir.glob('*.pub').reject do |key_file|
242 user_ssh_keys.installed?(key_file)
245 stale_keys.each { |key_file| puts "Extra file #{key_file}" }
246 system("git", "rm", "--quiet", *stale_keys)
249 if UserSSHKeys.changed? or Repository.changed? or stale_keys.any?
250 message = "#{Time.now().to_s}: update from API"
251 Dir.chdir(gitolite_admin)
253 `git commit -m '#{message}'`
258 puts "Error: " + bang.to_s
259 puts bang.backtrace.join("\n")