6 require 'active_support/all'
8 # This script does the actual gitolite config management on disk.
10 # Ward Vandewege <ward@curoverse.com>
12 # Default is development
13 production = ARGV[0] == "production"
15 ENV["RAILS_ENV"] = "development"
16 ENV["RAILS_ENV"] = "production" if production
20 # load and merge in the environment-specific application config info
21 # if present, overriding base config parameters as specified
22 path = File.dirname(__FILE__) + '/config/arvados-clients.yml'
23 if File.exists?(path) then
24 cp_config = YAML.load_file(path)[ENV['RAILS_ENV']]
26 puts "Please create a\n " + File.dirname(__FILE__) + "/config/arvados-clients.yml\n file"
30 gitolite_url = cp_config['gitolite_url']
31 gitolite_tmp = cp_config['gitolite_tmp']
33 gitolite_admin = File.join(File.expand_path(File.dirname(__FILE__)) + '/' + gitolite_tmp + '/gitolite-admin')
35 ENV['ARVADOS_API_HOST'] = cp_config['arvados_api_host']
36 ENV['ARVADOS_API_TOKEN'] = cp_config['arvados_api_token']
42 def ensure_repo(name,permissions,user_keys,gitolite_admin)
45 name.gsub!(/[^a-z0-9]/i,'')
49 user_keys.each do |uuid,p|
51 next if k[:public_key].nil?
52 keys[uuid] = Array.new() if not keys.key?(uuid)
55 # Handle putty-style ssh public keys
56 key.sub!(/^(Comment: "r[^\n]*\n)(.*)$/m,'ssh-rsa \2 \1')
57 key.sub!(/^(Comment: "d[^\n]*\n)(.*)$/m,'ssh-dss \2 \1')
65 cf = gitolite_admin + '/conf/auto/' + name + '.conf'
67 conf = "\nrepo #{name}\n"
72 permissions.sort.each do |uuid,v|
73 conf += "\t#{v[:gitolite_permissions]}\t= #{uuid.to_s}\n"
76 keys.include?(uuid) and keys[uuid].each do |v|
77 kf = gitolite_admin + '/keydir/arvados/' + uuid.to_s + "@#{count}.pub"
79 if !File.exists?(kf) or IO::read(kf) != v then
81 f = File.new(kf + ".tmp",'w')
84 # File.rename will overwrite the destination file if it exists
85 File.rename(kf + ".tmp",kf);
91 if !File.exists?(cf) or IO::read(cf) != conf then
93 f = File.new(cf + ".tmp",'w')
96 # this is about as atomic as we can make the replacement of the file...
97 File.unlink(cf) if File.exists?(cf)
98 File.rename(cf + ".tmp",cf);
107 # Get our local gitolite-admin repo up to snuff
108 if not File.exists?(File.dirname(__FILE__) + '/' + gitolite_tmp) then
109 Dir.mkdir(File.join(File.dirname(__FILE__) + '/' + gitolite_tmp), 0700)
111 if not File.exists?(gitolite_admin) then
112 Dir.chdir(File.join(File.dirname(__FILE__) + '/' + gitolite_tmp))
113 `git clone #{gitolite_url}`
115 Dir.chdir(gitolite_admin)
120 arv = Arvados.new( { :suppress_ssl_warnings => false } )
122 permissions = arv.repository.get_all_permissions
124 repos = permissions[:repositories]
125 user_keys = permissions[:user_keys]
132 next if r[:name].nil?
133 (@c,@s) = ensure_repo(r[:name],r[:user_permissions],user_keys,gitolite_admin)
138 # Clean up public key files that should not be present
139 Dir.glob(gitolite_admin + '/keydir/arvados/*.pub') do |key_file|
140 next if key_file =~ /arvados_git_user.pub$/
141 next if @seen.has_key?(key_file)
142 puts "Extra file #{key_file}"
144 Dir.chdir(gitolite_admin)
145 key_file.gsub!(/^#{gitolite_admin}\//,'')
150 message = "#{Time.now().to_s}: update from API"
151 Dir.chdir(gitolite_admin)
153 `git commit -m '#{message}'`
157 rescue Exception => bang
158 puts "Error: " + bang.to_s
159 puts bang.backtrace.join("\n")