Class: Dcmgr::Cli::KeyPair

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

Constant Summary collapse

M =
Dcmgr::Models

Instance Method Summary collapse

Instance Method Details

#addObject



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

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)
  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
  fields[:finger_print] = %x{ssh-keygen -lf #{options[:public_key]} | cut -d ' ' -f2}.chomp
  
  puts super(M::SshKeyPair,fields)
end

#del(uuid) ⇒ Object



42
43
44
# File 'lib/dcmgr/cli/keypair.rb', line 42

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

#modify(uuid) ⇒ Object



36
37
38
39
# File 'lib/dcmgr/cli/keypair.rb', line 36

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dcmgr/cli/keypair.rb', line 47

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 %>\nName:\n  <%= keypair.name%>\nFinger print:\n  <%= keypair.finger_print %>\nPublic Key:\n  <%= keypair.public_key%>\n", nil, '-').result(binding)
  else
    puts ERB.new("<%- M::SshKeyPair.each { |row| -%>\n<%= row.canonical_uuid %>\\t<%= row.account_id %>\\t<%= row.name %>\\t<%= row.finger_print %>\n<%- } -%>\n", nil, '-').result(binding)
  end
end