Class: Mccloud::Keypair

Inherits:
Object
  • Object
show all
Defined in:
lib/mccloud/keypair.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, env) ⇒ Keypair

Returns a new instance of Keypair.



10
11
12
13
14
15
16
# File 'lib/mccloud/keypair.rb', line 10

def initialize(name,env)
  @name=name
  @env=env
  @private_key_path=File.join(env.ssh_key_path,"#{name}_rsa")
  @public_key_path=File.join(env.ssh_key_path,"#{name}_rsa.pub")
  return self
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



8
9
10
# File 'lib/mccloud/keypair.rb', line 8

def env
  @env
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/mccloud/keypair.rb', line 7

def name
  @name
end

#private_key_pathObject

Returns the value of attribute private_key_path.



6
7
8
# File 'lib/mccloud/keypair.rb', line 6

def private_key_path
  @private_key_path
end

#public_key_pathObject

Returns the value of attribute public_key_path.



5
6
7
# File 'lib/mccloud/keypair.rb', line 5

def public_key_path
  @public_key_path
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/mccloud/keypair.rb', line 18

def exists?
  return false unless File.exists?(@public_key_path)
  return false unless File.exists?(@private_key_path)
  return true
end

#generate(options = {:force => false}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mccloud/keypair.rb', line 24

def generate(options={:force => false})
  force=options[:force]
  if exists? && force==false
    env.ui.error "Keypair: #{@name} already exists"
    env.ui.error "- #{@public_key_path}"
    env.ui.error "- #{@private_key_path}"
    raise ::Mccloud::Error, "Keypair #{@name} already exists"
  else
    env.ui.info "Generating Keypair: #{@name}"
    env.ui.info "- #{@public_key_path}"
    env.ui.info "- #{@private_key_path}"
    env.ui.info ""
    env.ui.warn "Make sure you make a backup!!"
    rsa_key=::Mccloud::Util::SSHKey.generate({ :comment => "Key generated by mccloud #{@name}"})
    begin
      File.open(@public_key_path,'w'){|f| f.write(rsa_key.ssh_public_key)}
      File.open(@private_key_path,'w'){|f| f.write(rsa_key.rsa_private_key)}
    rescue Exception => ex
      env.ui.error "Error generating keypair : #{ex}"
    end
  end
end