Class: Chef::Knife::CloudstackKeypairCreate

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

Instance Method Summary collapse

Methods included from CloudstackBase

#connection, included, #locate_config_value, #msg_pair, #validate!

Instance Method Details

#runObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/chef/knife/cloudstack_keypair_create.rb', line 44

def run
  $stdout.sync = true

  validate!

  options = {}
  if locate_config_value(:publickey) != nil
    options['publickey'] = locate_config_value(:publickey)
    mode = 'register'
    if locate_config_value(:name) != nil
      options['name'] = locate_config_value(:name)
    end
  else
    mode = 'create'
    if locate_config_value(:name) != nil
      keypair_name = locate_config_value(:name)
    end
  end

  case mode
  when 'register'
    response = connection.register_ssh_key_pair(options['publickey'], options['name'])
    sshkeypair = response['registersshkeypairresponse']['keypair']

    sshkeypair_list = [
      ui.color('Name', :bold),
      ui.color('Fingerprint', :bold),
      ui.color('Private Key', :bold)
    ]

    sshkeypair_list << sshkeypair['name'].to_s
    sshkeypair_list << sshkeypair['fingerprint'].to_s
    sshkeypair_list << sshkeypair['privatekey'].to_s
    puts ui.list(sshkeypair_list, :columns_across, 3)
  when 'create'
    response = connection.create_ssh_key_pair(keypair_name,options)
    sshkeypair = response['createsshkeypairresponse']['keypair']

    if locate_config_value(:outfile) != nil
      output = locate_config_value(:outfile)
      File.open(output,'w'){|f|
        f.print sshkeypair['privatekey'].to_s
      }
    else
      sshkeypair_list = [
        ui.color('Private Key', :bold)
      ]
      sshkeypair_list << sshkeypair['privatekey'].to_s
      puts ui.list(sshkeypair_list, :columns_across, 3)
    end
  else
    puts 'Error. Missing Keypair Name (-k) option.'
  end

end