Class: Opsicle::SSHKey

Inherits:
Object
  • Object
show all
Defined in:
lib/opsicle/commands/ssh_key.rb

Instance Method Summary collapse

Constructor Details

#initialize(environment, keyfile) ⇒ SSHKey

Returns a new instance of SSHKey.



4
5
6
7
# File 'lib/opsicle/commands/ssh_key.rb', line 4

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

Instance Method Details

#execute(options = {}) ⇒ Object



9
10
11
12
13
# File 'lib/opsicle/commands/ssh_key.rb', line 9

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

#keyObject



29
30
31
# File 'lib/opsicle/commands/ssh_key.rb', line 29

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

#public_key?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/opsicle/commands/ssh_key.rb', line 25

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

#updateObject



33
34
35
# File 'lib/opsicle/commands/ssh_key.rb', line 33

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

#valid_key_file?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/opsicle/commands/ssh_key.rb', line 21

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

#validate!Object

Raises:



15
16
17
18
19
# File 'lib/opsicle/commands/ssh_key.rb', line 15

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