2 # Copyright (C) The Arvados Authors. All rights reserved.
4 # SPDX-License-Identifier: AGPL-3.0
13 # This script does the actual gitolite config management on disk.
15 # Ward Vandewege <ward@curii.com>
17 # Default is development
18 production = ARGV[0] == "production"
20 ENV["RAILS_ENV"] = "development"
21 ENV["RAILS_ENV"] = "production" if production
25 # load and merge in the environment-specific application config info
26 # if present, overriding base config parameters as specified
27 path = File.absolute_path('../../config/arvados-clients.yml', __FILE__)
28 if File.exist?(path) then
29 cp_config = YAML.load_file(path)[ENV['RAILS_ENV']]
31 puts "Please create a\n #{path}\n file"
35 gitolite_url = cp_config['gitolite_url']
36 gitolite_arvados_git_user_key = cp_config['gitolite_arvados_git_user_key']
38 gitolite_tmpdir = cp_config['gitolite_tmp']
39 gitolite_admin = File.join(gitolite_tmpdir, 'gitolite-admin')
40 gitolite_admin_keydir = File.join(gitolite_admin, 'keydir')
41 gitolite_keydir = File.join(gitolite_admin, 'keydir', 'arvados')
43 ENV['ARVADOS_API_HOST'] = cp_config['arvados_api_host']
44 ENV['ARVADOS_API_TOKEN'] = cp_config['arvados_api_token']
45 if cp_config['arvados_api_host_insecure']
46 ENV['ARVADOS_API_HOST_INSECURE'] = 'true'
48 ENV.delete('ARVADOS_API_HOST_INSECURE')
51 def ensure_directory(path, mode)
58 def replace_file(path, contents)
60 dirname, basename = File.split(path)
61 FileUtils.mkpath(dirname)
62 new_file = Tempfile.new([basename, ".tmp"], dirname)
64 new_file.write(contents)
66 File.rename(new_file, path)
69 new_file.close(unlink_now)
73 def file_has_contents?(path, contents)
75 IO.read(path) == contents
81 module TrackCommitState
83 # Note that all classes that include TrackCommitState will have
84 # @@need_commit = true if any of them set it. Since this flag reports
85 # a boolean state of the underlying git repository, that's OK in the
86 # current implementation.
93 def ensure_in_git(path, contents)
94 unless file_has_contents?(path, contents)
95 replace_file(path, contents)
96 system("git", "add", path)
102 def ensure_in_git(path, contents)
103 self.class.ensure_in_git(path, contents)
106 def self.included(base)
107 base.extend(ClassMethods)
112 include TrackCommitState
114 def initialize(user_keys_map, key_dir)
115 @user_keys_map = user_keys_map
120 def install(filename, pubkey)
122 key_path = File.join(@key_dir, filename)
123 ensure_in_git(key_path, pubkey)
125 @installed[filename] = true
128 def ensure_keys_for_user(user_uuid)
129 return unless key_list = @user_keys_map.delete(user_uuid)
130 key_list.map { |k| k[:public_key] }.compact.each_with_index do |pubkey, ii|
131 # Handle putty-style ssh public keys
132 pubkey.sub!(/^(Comment: "r[^\n]*\n)(.*)$/m,'ssh-rsa \2 \1')
133 pubkey.sub!(/^(Comment: "d[^\n]*\n)(.*)$/m,'ssh-dss \2 \1')
134 pubkey.gsub!(/\n/,'')
136 install("#{user_uuid}@#{ii}.pub", pubkey)
140 def installed?(filename)
146 include TrackCommitState
150 def initialize(arv_repo, user_keys)
152 @user_keys = user_keys
155 def self.ensure_system_config(conf_root)
156 ensure_in_git(File.join(conf_root, "conf", "gitolite.conf"),
157 %Q{include "auto/*.conf"\ninclude "admin/*.conf"\n})
158 ensure_in_git(File.join(conf_root, "arvadosaliases.pl"), alias_config)
160 conf_path = File.join(conf_root, "conf", "admin", "arvados.conf")
162 @arvados_git_user = arvados_git_user
165 RW = @arvados_git_user
168 ensure_directory(File.dirname(conf_path), 0755)
169 ensure_in_git(conf_path, conf_file)
172 def ensure_config(conf_root)
173 if name and (File.exist?(auto_conf_path(conf_root, name)))
174 # This gitolite installation knows the repository by name, rather than
175 # UUID. Leave it configured that way until a separate migration is run.
179 @@aliases[name] = uuid unless name.nil?
181 conf_file = "\nrepo #{basename}\n"
182 @arv_repo[:user_permissions].sort.each do |user_uuid, perm|
183 conf_file += "\t#{perm[:gitolite_permissions]}\t= #{user_uuid}\n"
184 @user_keys.ensure_keys_for_user(user_uuid)
186 ensure_in_git(auto_conf_path(conf_root, basename), conf_file)
191 def auto_conf_path(conf_root, basename)
192 File.join(conf_root, "conf", "auto", "#{basename}.conf")
200 if @arv_repo[:name].nil?
204 @arv_repo[:name].sub(/^[^A-Za-z]+/, "").gsub(/[^\w\.\/]/, "")
208 def self.alias_config
210 @@aliases.sort.each do |(repo_name, repo_uuid)|
211 conf_s += "\t'#{repo_name}' \t=> '#{repo_uuid}',\n"
219 # Get our local gitolite-admin repo up to snuff
220 if not File.exist?(gitolite_admin) then
221 ensure_directory(gitolite_tmpdir, 0700)
222 Dir.chdir(gitolite_tmpdir)
223 `git clone #{gitolite_url}`
224 Dir.chdir(gitolite_admin)
226 Dir.chdir(gitolite_admin)
231 permissions = arv.repository.get_all_permissions
233 ensure_directory(gitolite_keydir, 0700)
234 admin_user_ssh_keys = UserSSHKeys.new(permissions[:user_keys], gitolite_admin_keydir)
235 # Make sure the arvados_git_user key is installed; put it in gitolite_admin_keydir
236 # because that is where gitolite will try to put it if we do not.
237 admin_user_ssh_keys.install('arvados_git_user.pub', gitolite_arvados_git_user_key)
239 user_ssh_keys = UserSSHKeys.new(permissions[:user_keys], gitolite_keydir)
240 permissions[:repositories].each do |repo_record|
241 repo = Repository.new(repo_record, user_ssh_keys)
242 repo.ensure_config(gitolite_admin)
244 Repository.ensure_system_config(gitolite_admin)
246 # Clean up public key files that should not be present
247 Dir.chdir(gitolite_keydir)
248 stale_keys = Dir.glob('*.pub').reject do |key_file|
249 user_ssh_keys.installed?(key_file)
252 stale_keys.each { |key_file| puts "Extra file #{key_file}" }
253 system("git", "rm", "--quiet", *stale_keys)
256 if UserSSHKeys.changed? or Repository.changed? or stale_keys.any?
257 message = "#{Time.now().to_s}: update from API"
258 Dir.chdir(gitolite_admin)
260 `git commit -m '#{message}'`
265 puts "Error: " + bang.to_s
266 puts bang.backtrace.join("\n")