6 require 'active_support/all'
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_tmp = cp_config['gitolite_tmp']
34 gitolite_admin = File.join(File.expand_path(File.dirname(__FILE__)) + '/' + gitolite_tmp + '/gitolite-admin')
36 ENV['ARVADOS_API_HOST'] = cp_config['arvados_api_host']
37 ENV['ARVADOS_API_TOKEN'] = cp_config['arvados_api_token']
38 if cp_config['arvados_api_host_insecure']
39 ENV['ARVADOS_API_HOST_INSECURE'] = 'true'
41 ENV.delete('ARVADOS_API_HOST_INSECURE')
48 def ensure_repo(name,permissions,user_keys,gitolite_admin)
51 name.gsub!(/[^a-z0-9]/i,'')
55 user_keys.each do |uuid,p|
57 next if k[:public_key].nil?
58 keys[uuid] = Array.new() if not keys.key?(uuid)
61 # Handle putty-style ssh public keys
62 key.sub!(/^(Comment: "r[^\n]*\n)(.*)$/m,'ssh-rsa \2 \1')
63 key.sub!(/^(Comment: "d[^\n]*\n)(.*)$/m,'ssh-dss \2 \1')
71 cf = gitolite_admin + '/conf/auto/' + name + '.conf'
73 conf = "\nrepo #{name}\n"
78 permissions.sort.each do |uuid,v|
79 conf += "\t#{v[:gitolite_permissions]}\t= #{uuid.to_s}\n"
82 keys.include?(uuid) and keys[uuid].each do |v|
83 kf = gitolite_admin + '/keydir/arvados/' + uuid.to_s + "@#{count}.pub"
85 if !File.exists?(kf) or IO::read(kf) != v then
87 f = File.new(kf + ".tmp",'w')
90 # File.rename will overwrite the destination file if it exists
91 File.rename(kf + ".tmp",kf);
97 if !File.exists?(cf) or IO::read(cf) != conf then
99 f = File.new(cf + ".tmp",'w')
102 # this is about as atomic as we can make the replacement of the file...
103 File.unlink(cf) if File.exists?(cf)
104 File.rename(cf + ".tmp",cf);
113 # Get our local gitolite-admin repo up to snuff
114 if not File.exists?(File.dirname(__FILE__) + '/' + gitolite_tmp) then
115 Dir.mkdir(File.join(File.dirname(__FILE__) + '/' + gitolite_tmp), 0700)
117 if not File.exists?(gitolite_admin) then
118 Dir.chdir(File.join(File.dirname(__FILE__) + '/' + gitolite_tmp))
119 `git clone #{gitolite_url}`
121 Dir.chdir(gitolite_admin)
126 arv = Arvados.new( { :suppress_ssl_warnings => false } )
128 permissions = arv.repository.get_all_permissions
130 repos = permissions[:repositories]
131 user_keys = permissions[:user_keys]
138 next if r[:name].nil?
139 (@c,@s) = ensure_repo(r[:name],r[:user_permissions],user_keys,gitolite_admin)
144 # Clean up public key files that should not be present
145 Dir.glob(gitolite_admin + '/keydir/arvados/*.pub') do |key_file|
146 next if key_file =~ /arvados_git_user.pub$/
147 next if @seen.has_key?(key_file)
148 puts "Extra file #{key_file}"
150 Dir.chdir(gitolite_admin)
151 key_file.gsub!(/^#{gitolite_admin}\//,'')
156 message = "#{Time.now().to_s}: update from API"
157 Dir.chdir(gitolite_admin)
159 `git commit -m '#{message}'`
163 rescue Exception => bang
164 puts "Error: " + bang.to_s
165 puts bang.backtrace.join("\n")