Class: Chef::Knife::DigitalOceanSshkeyCreate

Inherits:
Chef::Knife
  • Object
show all
Includes:
DigitalOceanBase
Defined in:
lib/chef/knife/digital_ocean_sshkey_create.rb

Instance Method Summary collapse

Methods included from DigitalOceanBase

#client, included, load_deps, #locate_config_value, #validate!, #wait_for_status

Instance Method Details

#runObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chef/knife/digital_ocean_sshkey_create.rb', line 33

def run
  $stdout.sync = true

  validate!

  unless locate_config_value(:name)
    ui.error('SSH Key name cannot be empty. => -n <sshkey-name>')
    exit 1
  end

  unless locate_config_value(:public_key)
    ui.error('SSH key file needs to be specified. => -i <public_key>')
    exit 1
  end

  ssh_key = DropletKit::SSHKey.new(
    name: locate_config_value(:name),
    public_key: File.read(File.expand_path(locate_config_value(:public_key)))
  )
  result = client.ssh_keys.create(ssh_key)
  ui.info 'OK' if result.class == DropletKit::SSHKey || ui.error(JSON.parse(result)['message'])
end