Class: Dcmgr::Models::SshKeyPair

Inherits:
AccountResource show all
Defined in:
lib/dcmgr/models/ssh_key_pair.rb

Overview

SSH Key database for account.

Constant Summary

Constants inherited from BaseNew

BaseNew::LOCK_TABLES_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AccountResource

#account

Methods inherited from BaseNew

Proxy, dataset, default_row_lock_mode=, install_data, install_data_hooks, lock!, #to_hash, unlock!, #with_timestamps?

Class Method Details

.entry_new(account, &blk) ⇒ Object

Raises:

  • (ArgurmentError)


50
51
52
53
54
55
56
57
# File 'lib/dcmgr/models/ssh_key_pair.rb', line 50

def self.entry_new(, &blk)
  raise ArgurmentError unless .is_a?(Account)

  ssh = self.new &blk
  ssh. = .canonical_uuid

  ssh
end

.generate_key_pair(name) ⇒ Hash

Returns string’, :public_key=>‘pubkey string’.

Returns:

  • (Hash)

    string’, :public_key=>‘pubkey string’



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dcmgr/models/ssh_key_pair.rb', line 19

def self.generate_key_pair(name)
  pkey = File.expand_path(randstr, Dir.tmpdir)
  pubkey = pkey + '.pub'
  begin
    system("ssh-keygen -q -t rsa -C '%s' -N '' -f %s >/dev/null" % [name, pkey])
    unless $?.exitstatus == 0
      raise "Failed to run ssh-keygen: exitcode=#{$?.exitstatus}"
    end

    # get finger print of pkey file
    fp = `ssh-keygen -l -f #{pkey}`
    unless $?.exitstatus == 0
      raise "Failed to collect finger print value"
    end
    fp = fp.split(/\s+/)[1]

    {:private_key=>IO.read(pkey),
      :public_key=>IO.read(pubkey),
      :finger_print => fp}
  rescue
    # clean up tmp key files
    [pkey, pubkey].each { |i|
      File.unlink(i) if File.exist?(i)
    }
  end
end

Instance Method Details

#before_destroyObject



10
11
12
13
14
# File 'lib/dcmgr/models/ssh_key_pair.rb', line 10

def before_destroy
  # TODO: check running instances which are associated to ssh key
  # pairs. reject deletion if exist.
  super
end

#to_api_documentObject



46
47
48
# File 'lib/dcmgr/models/ssh_key_pair.rb', line 46

def to_api_document
  super
end