10 # This script does the actual gitolite config management on disk.
12 # Ward Vandewege <ward@curoverse.com>
14 # Default is development
15 production = ARGV[0] == "production"
17 ENV["RAILS_ENV"] = "development"
18 ENV["RAILS_ENV"] = "production" if production
22 # load and merge in the environment-specific application config info
23 # if present, overriding base config parameters as specified
24 path = File.dirname(__FILE__) + '/config/arvados-clients.yml'
25 if File.exists?(path) then
26 cp_config = YAML.load_file(path)[ENV['RAILS_ENV']]
28 puts "Please create a\n " + File.dirname(__FILE__) + "/config/arvados-clients.yml\n file"
32 gitolite_url = cp_config['gitolite_url']
33 gitolite_arvados_git_user_key = cp_config['gitolite_arvados_git_user_key']
35 gitolite_tmpdir = File.join(File.absolute_path(File.dirname(__FILE__)),
36 cp_config['gitolite_tmp'])
37 gitolite_admin = File.join(gitolite_tmpdir, 'gitolite-admin')
38 gitolite_admin_keydir = File.join(gitolite_admin, 'keydir')
39 gitolite_keydir = File.join(gitolite_admin, 'keydir', 'arvados')
41 ENV['ARVADOS_API_HOST'] = cp_config['arvados_api_host']
42 ENV['ARVADOS_API_TOKEN'] = cp_config['arvados_api_token']
43 if cp_config['arvados_api_host_insecure']
44 ENV['ARVADOS_API_HOST_INSECURE'] = 'true'
46 ENV.delete('ARVADOS_API_HOST_INSECURE')
49 def ensure_directory(path, mode)
56 def replace_file(path, contents)
58 dirname, basename = File.split(path)
59 FileUtils.mkpath(dirname)
60 new_file = Tempfile.new([basename, ".tmp"], dirname)
62 new_file.write(contents)
64 File.rename(new_file, path)
67 new_file.close(unlink_now)
71 def file_has_contents?(path, contents)
73 IO.read(path) == contents
79 module TrackCommitState
81 # Note that all classes that include TrackCommitState will have
82 # @@need_commit = true if any of them set it. Since this flag reports
83 # a boolean state of the underlying git repository, that's OK in the
84 # current implementation.
91 def ensure_in_git(path, contents)
92 unless file_has_contents?(path, contents)
93 replace_file(path, contents)
94 system("git", "add", path)
100 def ensure_in_git(path, contents)
101 self.class.ensure_in_git(path, contents)
104 def self.included(base)
105 base.extend(ClassMethods)
110 include TrackCommitState
112 def initialize(user_keys_map, key_dir)
113 @user_keys_map = user_keys_map
118 def install(filename, pubkey)
120 key_path = File.join(@key_dir, filename)
121 ensure_in_git(key_path, pubkey)
123 @installed[filename] = true
126 def ensure_keys_for_user(user_uuid)
127 return unless key_list = @user_keys_map.delete(user_uuid)
128 key_list.map { |k| k[:public_key] }.compact.each_with_index do |pubkey, ii|
129 # Handle putty-style ssh public keys
130 pubkey.sub!(/^(Comment: "r[^\n]*\n)(.*)$/m,'ssh-rsa \2 \1')
131 pubkey.sub!(/^(Comment: "d[^\n]*\n)(.*)$/m,'ssh-dss \2 \1')
132 pubkey.gsub!(/\n/,'')
134 install("#{user_uuid}@#{ii}.pub", pubkey)
138 def installed?(filename)
144 include TrackCommitState
148 def initialize(arv_repo, user_keys)
150 @user_keys = user_keys
153 def self.ensure_system_config(conf_root)
154 ensure_in_git(File.join(conf_root, "conf", "gitolite.conf"),
155 %Q{include "auto/*.conf"\ninclude "admin/*.conf"\n})
156 ensure_in_git(File.join(conf_root, "arvadosaliases.pl"), alias_config)
158 conf_path = File.join(conf_root, "conf", "admin", "arvados.conf")
160 @arvados_git_user = arvados_git_user
163 RW = @arvados_git_user
166 ensure_directory(File.dirname(conf_path), 0755)
167 ensure_in_git(conf_path, conf_file)
170 def ensure_config(conf_root)
171 if name and (File.exist?(auto_conf_path(conf_root, name)))
172 # This gitolite installation knows the repository by name, rather than
173 # UUID. Leave it configured that way until a separate migration is run.
177 @@aliases[name] = uuid unless name.nil?
179 conf_file = "\nrepo #{basename}\n"
180 @arv_repo[:user_permissions].sort.each do |user_uuid, perm|
181 conf_file += "\t#{perm[:gitolite_permissions]}\t= #{user_uuid}\n"
182 @user_keys.ensure_keys_for_user(user_uuid)
184 ensure_in_git(auto_conf_path(conf_root, basename), conf_file)
189 def auto_conf_path(conf_root, basename)
190 File.join(conf_root, "conf", "auto", "#{basename}.conf")
198 if @arv_repo[:name].nil?
202 @arv_repo[:name].sub(/^[^A-Za-z]+/, "").gsub(/[^\w\.\/]/, "")
206 def self.alias_config
208 @@aliases.sort.each do |(repo_name, repo_uuid)|
209 conf_s += "\t'#{repo_name}' \t=> '#{repo_uuid}',\n"
217 # Get our local gitolite-admin repo up to snuff
218 if not File.exists?(gitolite_admin) then
219 ensure_directory(gitolite_tmpdir, 0700)
220 Dir.chdir(gitolite_tmpdir)
221 `git clone #{gitolite_url}`
222 Dir.chdir(gitolite_admin)
224 Dir.chdir(gitolite_admin)
229 permissions = arv.repository.get_all_permissions
231 ensure_directory(gitolite_keydir, 0700)
232 admin_user_ssh_keys = UserSSHKeys.new(permissions[:user_keys], gitolite_admin_keydir)
233 # Make sure the arvados_git_user key is installed; put it in gitolite_admin_keydir
234 # because that is where gitolite will try to put it if we do not.
235 admin_user_ssh_keys.install('arvados_git_user.pub', gitolite_arvados_git_user_key)
237 user_ssh_keys = UserSSHKeys.new(permissions[:user_keys], gitolite_keydir)
238 permissions[:repositories].each do |repo_record|
239 repo = Repository.new(repo_record, user_ssh_keys)
240 repo.ensure_config(gitolite_admin)
242 Repository.ensure_system_config(gitolite_admin)
244 # Clean up public key files that should not be present
245 Dir.chdir(gitolite_keydir)
246 stale_keys = Dir.glob('*.pub').reject do |key_file|
247 user_ssh_keys.installed?(key_file)
250 stale_keys.each { |key_file| puts "Extra file #{key_file}" }
251 system("git", "rm", "--quiet", *stale_keys)
254 if UserSSHKeys.changed? or Repository.changed? or stale_keys.any?
255 message = "#{Time.now().to_s}: update from API"
256 Dir.chdir(gitolite_admin)
258 `git commit -m '#{message}'`
263 puts "Error: " + bang.to_s
264 puts bang.backtrace.join("\n")