Sha256: 6c137c4db2bc78a7a90677503fbe901710a6837e81dfa7d582c729b9cfb451a8

Contents?: true

Size: 924 Bytes

Versions: 60

Compression:

Stored size: 924 Bytes

Contents

module Opsicle
  class SSHKey

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

    def execute(options={})
      validate!
      update
      Output.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

60 entries across 60 versions & 1 rubygems

Version Path
opsicle-2.15.0 lib/opsicle/commands/ssh_key.rb
opsicle-2.14.2 lib/opsicle/commands/ssh_key.rb
opsicle-2.14.1 lib/opsicle/commands/ssh_key.rb
opsicle-2.14.0 lib/opsicle/commands/ssh_key.rb
opsicle-2.13.1 lib/opsicle/commands/ssh_key.rb
opsicle-2.13.0 lib/opsicle/commands/ssh_key.rb
opsicle-2.12.5 lib/opsicle/commands/ssh_key.rb
opsicle-2.12.3 lib/opsicle/commands/ssh_key.rb
opsicle-2.12.2 lib/opsicle/commands/ssh_key.rb
opsicle-2.12.1 lib/opsicle/commands/ssh_key.rb
opsicle-2.12.0 lib/opsicle/commands/ssh_key.rb
opsicle-2.11.3 lib/opsicle/commands/ssh_key.rb
opsicle-2.11.1 lib/opsicle/commands/ssh_key.rb
opsicle-2.11.0 lib/opsicle/commands/ssh_key.rb
opsicle-2.10.1 lib/opsicle/commands/ssh_key.rb
opsicle-2.10.0 lib/opsicle/commands/ssh_key.rb
opsicle-2.9.6 lib/opsicle/commands/ssh_key.rb
opsicle-2.9.4 lib/opsicle/commands/ssh_key.rb
opsicle-2.9.3 lib/opsicle/commands/ssh_key.rb
opsicle-2.9.2 lib/opsicle/commands/ssh_key.rb