Sha256: 97062fd003319187daa01e4807dad290c792077d388c63016837aea75158cacd

Contents?: true

Size: 917 Bytes

Versions: 2

Compression:

Stored size: 917 Bytes

Contents

module Opsicle
  class SSHKey

    def initialize(environment, keyfile)
      @client = Client.new(environment)
      @keyfile = keyfile
    end

    def execute(options={})
      validate!
      update
      say "ssh-key updated successfully"
    end

    def validate!
      raise KeyFileNotFound, "No key file could be found" unless File.exists?(@keyfile)
      raise InvalidKeyFile, "Key file is invalid" unless valid_key_file?
      raise InvalidKeyFile, "Key file is a private key" unless public_key?
    end

    def valid_key_file?
      system("ssh-keygen -l -f #{@keyfile} > /dev/null")
    end

    def public_key?
      !key.match(/PRIVATE KEY/)
    end

    def key
      @key ||= File.read(@keyfile)
    end

    def update
      @client.api_call(:update_my_user_profile, {ssh_public_key: key})
    end

  end
  KeyFileNotFound = Class.new(StandardError)
  InvalidKeyFile = Class.new(StandardError)
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
opsicle-0.2.1 lib/opsicle/commands/ssh_key.rb
opsicle-0.2.0 lib/opsicle/commands/ssh_key.rb