module Gitolite class GitoliteAdmin attr_accessor :gl_admin, :ssh_keys, :config CONF = "/conf/gitolite.conf" KEYDIR = "/keydir" #Intialize with the path to #the gitolite-admin repository def initialize(path, options = {}) @gl_admin = Grit::Repo.new(path) @path = path @conf = options[:conf] || CONF @keydir = options[:keydir] || KEYDIR @ssh_keys = load_keys(File.join(@path, @keydir)) @config = Config.new(File.join(@path, @conf)) end #Writes all aspects out to the file system #will also stage all changes def save #Process config file #Process ssh keys files = list_keys(File.join(@path, @keydir)).map{|f| File.basename f} keys = @ssh_keys.values.map{|f| f.map {|t| t.filename}}.flatten #Remove all keys we don't have a record for to_remove = (files - keys).map { |f| File.join(@keydir, f)} @gl_admin.remove(to_remove) unless to_remove.empty? #Write all keys to files, overwriting existing keys keys.each do |key| File.open(key, "w") do |f| f.write key.to_s end end end #commits all staged changes and pushes back #to origin def apply status = @gl_admin.status end #Calls save and apply in order def save_and_apply end def add_key(key) raise "Key must be of type Gitolite::SSHKey!" unless key.instance_of? Gitolite::SSHKey @ssh_keys[key.owner] << key end def rm_key(key) @ssh_keys[key.owner].delete key end private #Loads all .pub files in the gitolite-admin #keydir directory def load_keys(path) keys = Hash.new {|k,v| k[v] = []} list_keys(path).each do |key| new_key = SSHKey.from_file(File.join(path, key)) owner = new_key.owner keys[owner] << new_key end keys end def list_keys(path) Dir.chdir(path) Dir.glob("**/*.pub") end end end