class Golem::Command::UpdateKeysFile

Command for updating the .ssh/authorized_keys file.

Constants

CONTENT_MARK

Content mark to identify automatically updated part of file.

SSH_OPTS_COMMAND_DEFAULT

Default SSH(D) options to set if using command=“” style keys file.

USAGE

@private

Public Instance Methods

run() click to toggle source

Run the command. Old content is preserved, searched for {CONTENT_MARK}, and only content after the mark gets replaced.

# File lib/golem/command/update_keys_file.rb, line 11
def run
    orig_content = File.exists?(Golem::Config.keys_file_path) ? File.read(Golem::Config.keys_file_path) : ""
    new_content = if orig_content.match(Regexp.new('^' + Regexp.escape(CONTENT_MARK) + '$'))
        orig_content.sub(Regexp.new('^' + Regexp.escape(CONTENT_MARK) + '$.*\z', Regexp::MULTILINE), CONTENT_MARK + "\n" + keys_str)
    else
        orig_content + "\n" + CONTENT_MARK + "\n" + keys_str
    end
    File.open(Golem::Config.keys_file_path, "w") {|f| f.write(new_content)}
end

Private Instance Methods

keys_file_line(user, key) click to toggle source
# File lib/golem/command/update_keys_file.rb, line 26
def keys_file_line(user, key)
    first_part = if Golem::Config.keys_file_use_command
        "command=\"#{Golem::Config.bin_dir + '/golem'} auth '#{user}'\""
    else
        "environment=\"GOLEM_USER=#{user}\""
    end
    ssh_opts = if Golem::Config.keys_file_ssh_opts.nil?
        Golem::Config.keys_file_use_command ? ",#{SSH_OPTS_COMMAND_DEFAULT}" : ""
    else
        "," + Golem::Config.keys_file_ssh_opts.to_s
    end
    "#{first_part}#{ssh_opts} #{key}"
end
keys_str() click to toggle source
# File lib/golem/command/update_keys_file.rb, line 22
def keys_str
    Golem::Access.ssh_keys.collect {|user, keys| keys.collect {|key| keys_file_line(user, key)}.join("\n")}.join("\n") + "\n"
end