Class: HP::Cloud::KeypairHelper

Inherits:
BaseHelper show all
Defined in:
lib/hpcloud/keypair_helper.rb

Instance Attribute Summary collapse

Attributes inherited from BaseHelper

#connection, #cstatus, #fog

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseHelper

#destroy, #is_valid?, #set_error, #to_hash

Constructor Details

#initialize(connection, foggy = nil) ⇒ KeypairHelper

Returns a new instance of KeypairHelper.



31
32
33
34
35
36
37
38
39
# File 'lib/hpcloud/keypair_helper.rb', line 31

def initialize(connection, foggy = nil)
  super(connection, foggy)
  return if foggy.nil?
  @id = foggy.name
  @name = foggy.name
  @fingerprint = foggy.fingerprint
  @public_key = foggy.public_key
  @private_key = foggy.private_key
end

Instance Attribute Details

#fingerprintObject

Returns the value of attribute fingerprint.



25
26
27
# File 'lib/hpcloud/keypair_helper.rb', line 25

def fingerprint
  @fingerprint
end

#idObject

Returns the value of attribute id.



25
26
27
# File 'lib/hpcloud/keypair_helper.rb', line 25

def id
  @id
end

#nameObject

Returns the value of attribute name.



25
26
27
# File 'lib/hpcloud/keypair_helper.rb', line 25

def name
  @name
end

#private_keyObject

Returns the value of attribute private_key.



25
26
27
# File 'lib/hpcloud/keypair_helper.rb', line 25

def private_key
  @private_key
end

#public_keyObject

Returns the value of attribute public_key.



25
26
27
# File 'lib/hpcloud/keypair_helper.rb', line 25

def public_key
  @public_key
end

Class Method Details

.get_keysObject



27
28
29
# File 'lib/hpcloud/keypair_helper.rb', line 27

def self.get_keys()
  return [ "name", "fingerprint" ]
end

.private_directoryObject



61
62
63
# File 'lib/hpcloud/keypair_helper.rb', line 61

def self.private_directory
  "#{ENV['HOME']}/.hpcloud/keypairs/"
end

.private_filename(name) ⇒ Object



65
66
67
# File 'lib/hpcloud/keypair_helper.rb', line 65

def self.private_filename(name)
  "#{KeypairHelper.private_directory}#{name}.pem"
end

.private_listObject



93
94
95
96
97
98
99
100
# File 'lib/hpcloud/keypair_helper.rb', line 93

def self.private_list
  ray = Dir.entries(KeypairHelper.private_directory)
  ray = ray.delete_if{|x| x == "."}
  ray = ray.delete_if{|x| x == ".."}
  ray = ray.delete_if{|x| x.match(/\.pem$/) == nil }
  ray.collect!{|x| x.gsub(/\.pem$/,'') }
  ray.sort
end

Instance Method Details

#private_addObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hpcloud/keypair_helper.rb', line 76

def private_add
  directory = KeypairHelper.private_directory
  FileUtils.mkdir_p(directory)
  FileUtils.chmod(0700, directory)
  filename = KeypairHelper.private_filename(name)
  FileUtils.rm_f(filename)
  if @fog.nil?
    file = File.new(filename, "w")
    file.write(@private_key)
    file.close
  else
    @fog.write(filename)
  end
  FileUtils.chmod(0400, filename)
  return filename
end

#private_readObject



69
70
71
72
73
74
# File 'lib/hpcloud/keypair_helper.rb', line 69

def private_read
  filename = KeypairHelper.private_filename(name)
  @private_key = File.read(filename)
  @fog.private_key = @private_key unless @fog.nil?
  @private_key
end

#private_removeObject



102
103
104
105
106
# File 'lib/hpcloud/keypair_helper.rb', line 102

def private_remove
  filename = KeypairHelper.private_filename(name)
  FileUtils.rm(filename)
  filename
end

#saveObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/hpcloud/keypair_helper.rb', line 41

def save
  return false if is_valid? == false
  if @fog.nil?
    if @public_key.nil?
      hsh = {:name => @name, :fingerprint => @fingerprint, :private_key => @private_key}
      keypair = @connection.compute.key_pairs.create(hsh)
    else
      keypair = @connection.compute.create_key_pair(@name, @public_key)
    end
    if keypair.nil?
      set_error("Error creating keypair")
      return false
    end
    @fog = keypair
    return true
  else
    raise "Update not implemented"
  end
end