Class: Dcmgr::Cli::KeyPair

Inherits:
Base
  • Object
show all
Includes:
Helpers::CliHelper
Defined in:
lib/dcmgr/cli/keypair.rb

Constant Summary collapse

M =
Dcmgr::Models

Instance Method Summary collapse

Methods included from Helpers::CliHelper

#sh, #tryagain

Instance Method Details

#addObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dcmgr/cli/keypair.rb', line 15

def add
  UnknownUUIDError.raise(options[:account_id]) if M::[options[:account_id]].nil?
  private_key_path = File.expand_path(options[:private_key])
  public_key_path = File.expand_path(options[:public_key])
  Error.raise "Private key file doesn't exist",100 unless File.exists?(private_key_path) || options[:private_key]
  Error.raise "Public key file doesn't exist",100 unless File.exists?(public_key_path)
  
  fields = options.dup
  
  #Get the keys from their respective files.
  fields[:public_key] = File.open(public_key_path) {|f| f.readline}
  fields[:private_key] = File.open(private_key_path) {|f| f.readlines.map.join}
  
  #Generate the fingerprint from the public key file
  res = sh("ssh-keygen -lf #{options[:public_key]}")
  fields[:finger_print] = res[:stdout].split(' ')[1]
  
  puts super(M::SshKeyPair,fields)
end

#del(uuid) ⇒ Object



44
45
46
# File 'lib/dcmgr/cli/keypair.rb', line 44

def del(uuid)
  super(M::SshKeyPair,uuid)
end

#modify(uuid) ⇒ Object



38
39
40
41
# File 'lib/dcmgr/cli/keypair.rb', line 38

def modify(uuid)
  UnknownUUIDError.raise(options[:account_id]) if options[:account_id] && M::[options[:account_id]].nil?
  super(M::SshKeyPair,uuid,options)
end

#show(uuid = nil) ⇒ Object



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
# File 'lib/dcmgr/cli/keypair.rb', line 49

def show(uuid=nil)
  if uuid
    keypair = M::SshKeyPair[uuid] || UnknownUUIDError.raise(uuid)
    puts ERB.new("Keypair UUID:\n  <%= keypair.canonical_uuid %>\nAccount id:\n  <%= keypair.account_id %>\nFinger print:\n  <%= keypair.finger_print %>\nPublic Key:\n  <%= keypair.public_key%>\n<%- if keypair.description -%>\nDescription:\n  <%= keypair.description %>\n<%- end -%>\n", nil, '-').result(binding)
  else
    puts ERB.new("<%- M::SshKeyPair.each { |row| -%>\n<%= row.canonical_uuid %>\\t<%= row.account_id %>\\t<%= row.finger_print %>\n<%- } -%>\n", nil, '-').result(binding)
  end
end