-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdefault.rb
More file actions
84 lines (73 loc) · 2.25 KB
/
default.rb
File metadata and controls
84 lines (73 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
45
46
47
48
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
74
75
76
77
78
79
80
81
82
83
84
require File.join(File.dirname(__FILE__), '..', 'cloudstack.rb')
Puppet::Type.type(:cloudstack_keypair).provide(
'default',
:parent => Puppet::Provider::CloudStack
) do
mk_resource_methods
def self.instances
# I may need to fail if the server does not have a name?
(connection.list_ssh_key_pairs['listsshkeypairsresponse']['sshkeypair'] || []).collect do |keypair|
new(
:name => keypair['name'],
:fingerprint => keypair['fingerprint'],
:ensure => :present
)
end
end
def exists?
@property_hash[:ensure] == :present
end
def create
#domain_id = get_domain_id(resource[:domain])
#project_id = get_project_id(resource[:project])
Puppet.debug("Bootstrapping instance with:
#{resource[:name]}
")
response = connection.create_ssh_key_pair(
resource[:name]
)['createsshkeypairresponse']['keypair']
# if the resource is locally caching created keys
if resource['cache_key']
if resource['key_file']
key_file = resource[:key_file]
else
key_file = key_file_path(response['fingerprint'])
end
FileUtils.mkdir_p(File.dirname(key_file))
if File.exists?(key_file)
fail("keyfile: #{key_file} already exists, not going to override")
end
Puppet.info("Writing your private key to #{key_file}")
File.new(key_file, 'w').write(response['privatekey'])
File.chmod(0600, key_file)
end
@property_hash[:fingerprint] = response['fingerprint']
@property_hash[:privatekey] = response['privatekey']
@property_hash[:ensure] = :present
end
def destroy
connection.delete_ssh_key_pair(resource[:name])
end
def privatekey
if fingerprint
key_file = key_file_path(fingerprint)
if File.exists?(key_file)
File.read(key_file)
else
Puppet.notice("Could not find locally cached private key for #{resource[:name]}")
nil
end
else
fail('Expected fingerprint to be set')
end
end
def fingerprint
@property_hash[:fingerprint]
end
def key_file_path(fingerprint_arg=fingerprint)
File.join(Puppet[:confdir], 'cloudstack', 'keypair', fingerprint_arg, 'id_rsa')
end
# def flush
# @property_hash = resource.to_hash
# end
end