chef-metal-fog 0.4 → 0.5.beta

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 60b1659681a87e03c3e75748bf5e0a729d1d42e0
4
- data.tar.gz: 7aea9de39a52c044f12f49b045bb9bad64277851
3
+ metadata.gz: 1dd0fd9efc928a98ed0742916d1bbbed1a2c13b3
4
+ data.tar.gz: a4cb6fb64e7848db44f96ef6e4385bcd1fe1247d
5
5
  SHA512:
6
- metadata.gz: 6528c4091d92999bf66166df4459cd036b273c65e8372cda122a35c5efbaf0a1bc2d12fa624ef2935545b05cf1c86473ca44b535a5c837a17ac1f09f24d8f6d4
7
- data.tar.gz: 64d79d629752c5c3da14a5dfbb9a91a4a96f62bd956ecaacdd8adde30a24e4d040213ae13f06b793065e077c3df7e55aec472d5e0419dd8e4bb7fb39344610bc
6
+ metadata.gz: 872bbb650218e9374713b63575849a25eb5dc296b0633ace402c5c19b791a5e8f68115df0546eb5297e87d586f5d3f24c22fc1c1b592784a95221358a70bf100
7
+ data.tar.gz: 9455469c78c3d131ef9562cea69bb6a529dafa4f98f2f3bd8541012b66bddc2db08cbbbcf83a5266f1e72925c10ddaafde8f43f3a0dab5ada29c39301fb43ee6
data/README.md CHANGED
@@ -1,3 +1,3 @@
1
1
  # chef-metal-fog
2
2
 
3
- This is the Fog provisioner for chef-metal. It provides EC2, DigitalOcean and Openstack functionality.
3
+ This is the Fog driver for chef-metal. It provides EC2, DigitalOcean and Openstack functionality.
@@ -1,11 +1,8 @@
1
1
  require 'chef/provider/lwrp_base'
2
- require 'chef_metal/provider_action_handler'
3
- require 'chef_metal_fog/fog_provisioner'
2
+ require 'chef_metal_fog/fog_driver'
4
3
 
5
4
  class Chef::Provider::FogKeyPair < Chef::Provider::LWRPBase
6
5
 
7
- include ChefMetal::ProviderActionHandler
8
-
9
6
  use_inline_resources
10
7
 
11
8
  def whyrun_supported?
@@ -19,7 +16,7 @@ class Chef::Provider::FogKeyPair < Chef::Provider::LWRPBase
19
16
  action :delete do
20
17
  if current_resource_exists?
21
18
  converge_by "delete #{key_description}" do
22
- case new_resource.provisioner.compute_options[:provider]
19
+ case new_driver.compute_options[:provider]
23
20
  when 'DigitalOcean'
24
21
  compute.destroy_key_pair(@current_id)
25
22
  when 'OpenStack'
@@ -32,23 +29,29 @@ class Chef::Provider::FogKeyPair < Chef::Provider::LWRPBase
32
29
  end
33
30
 
34
31
  def key_description
35
- "#{new_resource.name} on #{new_resource.provisioner.provisioner_url}"
32
+ "#{new_resource.name} on #{new_driver.driver_url}"
36
33
  end
37
34
 
38
35
  def create_key(action)
36
+ if @should_create_directory
37
+ Cheffish.inline_resource(self, action) do
38
+ directory run_context.config[:private_key_write_path]
39
+ end
40
+ end
41
+
39
42
  if current_resource_exists?
40
43
  # If the public keys are different, update the server public key
41
44
  if !current_resource.private_key_path
42
45
  if new_resource.allow_overwrite
43
46
  ensure_keys(action)
44
47
  else
45
- raise "#{key_description} already exists on the server, but the private key #{new_resource.private_key_path} does not exist!"
48
+ raise "#{key_description} already exists on the server, but the private key #{new_private_key_path} does not exist!"
46
49
  end
47
50
  else
48
51
  ensure_keys(action)
49
52
  end
50
53
 
51
- new_fingerprints = case new_resource.provisioner.compute_options[:provider]
54
+ new_fingerprints = case new_driver.compute_options[:provider]
52
55
  when 'DigitalOcean'
53
56
  [Cheffish::KeyFormatter.encode(desired_key, :format => :openssh)]
54
57
  when 'OpenStack'
@@ -73,7 +76,7 @@ class Chef::Provider::FogKeyPair < Chef::Provider::LWRPBase
73
76
  if !new_fingerprints.any? { |f| (f.is_a?(Proc) ? f.call : f) == @current_fingerprint }
74
77
  if new_resource.allow_overwrite
75
78
  converge_by "update #{key_description} to match local key at #{new_resource.private_key_path}" do
76
- case new_resource.provisioner.compute_options[:provider]
79
+ case new_driver.compute_options[:provider]
77
80
  when 'DigitalOcean'
78
81
  compute.create_ssh_key(new_resource.name, Cheffish::KeyFormatter.encode(desired_key, :format => :openssh))
79
82
  when 'OpenStack'
@@ -92,7 +95,7 @@ class Chef::Provider::FogKeyPair < Chef::Provider::LWRPBase
92
95
 
93
96
  # Create key
94
97
  converge_by "create #{key_description} from local key at #{new_resource.private_key_path}" do
95
- case new_resource.provisioner.compute_options[:provider]
98
+ case new_driver.compute_options[:provider]
96
99
  when 'DigitalOcean'
97
100
  compute.create_ssh_key(new_resource.name, Cheffish::KeyFormatter.encode(desired_key, :format => :openssh))
98
101
  when 'OpenStack'
@@ -104,10 +107,15 @@ class Chef::Provider::FogKeyPair < Chef::Provider::LWRPBase
104
107
  end
105
108
  end
106
109
 
110
+ def new_driver
111
+ run_context.chef_metal.driver_for(new_resource.driver)
112
+ end
113
+
107
114
  def ensure_keys(action)
108
115
  resource = new_resource
116
+ private_key_path = new_private_key_path
109
117
  Cheffish.inline_resource(self, action) do
110
- private_key resource.private_key_path do
118
+ private_key private_key_path do
111
119
  public_key_path resource.public_key_path
112
120
  if resource.private_key_options
113
121
  resource.private_key_options.each_pair do |key,value|
@@ -131,8 +139,8 @@ class Chef::Provider::FogKeyPair < Chef::Provider::LWRPBase
131
139
 
132
140
  def desired_private_key
133
141
  @desired_private_key ||= begin
134
- private_key, format = Cheffish::KeyFormatter.decode(IO.read(new_resource.private_key_path))
135
- private_key
142
+ private_key, format = Cheffish::KeyFormatter.decode(IO.read(new_private_key_path))
143
+ private_key
136
144
  end
137
145
  end
138
146
 
@@ -141,19 +149,35 @@ class Chef::Provider::FogKeyPair < Chef::Provider::LWRPBase
141
149
  end
142
150
 
143
151
  def compute
144
- new_resource.provisioner.compute
152
+ new_driver.compute
145
153
  end
146
154
 
147
155
  def current_public_key
148
156
  current_resource.source_key
149
157
  end
150
158
 
159
+ def new_private_key_path
160
+ private_key_path = new_resource.private_key_path || new_resource.name
161
+ if private_key_path.is_a?(Symbol)
162
+ private_key_path
163
+ elsif Pathname.new(private_key_path).relative? && new_driver.config[:private_key_write_path]
164
+ @should_create_directory = true
165
+ ::File.join(new_driver.config[:private_key_write_path], private_key_path)
166
+ else
167
+ private_key_path
168
+ end
169
+ end
170
+
171
+ def new_public_key_path
172
+ new_resource.public_key_path
173
+ end
174
+
151
175
  def load_current_resource
152
- if !new_resource.provisioner.kind_of?(ChefMetalFog::FogProvisioner)
153
- raise 'ec2_key_pair only works with fog_provisioner'
176
+ if !new_driver.kind_of?(ChefMetalFog::FogDriver)
177
+ raise 'fog_key_pair only works with fog_driver'
154
178
  end
155
179
  @current_resource = Chef::Resource::FogKeyPair.new(new_resource.name, run_context)
156
- case new_resource.provisioner.compute_options[:provider]
180
+ case new_driver.provider
157
181
  when 'DigitalOcean'
158
182
  current_key_pair = compute.ssh_keys.select { |key| key.name == new_resource.name }.first
159
183
  if current_key_pair
@@ -179,11 +203,11 @@ class Chef::Provider::FogKeyPair < Chef::Provider::LWRPBase
179
203
  end
180
204
  end
181
205
 
182
- if new_resource.private_key_path && ::File.exist?(new_resource.private_key_path)
183
- current_resource.private_key_path new_resource.private_key_path
206
+ if new_private_key_path && ::File.exist?(new_private_key_path)
207
+ current_resource.private_key_path new_private_key_path
184
208
  end
185
- if new_resource.public_key_path && ::File.exist?(new_resource.public_key_path)
186
- current_resource.public_key_path new_resource.public_key_path
209
+ if new_public_key_path && ::File.exist?(new_public_key_path)
210
+ current_resource.public_key_path new_public_key_path
187
211
  end
188
212
  end
189
213
  end
@@ -5,18 +5,13 @@ class Chef::Resource::FogKeyPair < Chef::Resource::LWRPBase
5
5
 
6
6
  def initialize(*args)
7
7
  super
8
- @provisioner = run_context.chef_metal.current_provisioner
9
- end
10
-
11
- def after_created
12
- # Make the credentials usable
13
- provisioner.key_pairs[name] = self
8
+ @driver = run_context.chef_metal.current_driver
14
9
  end
15
10
 
16
11
  actions :create, :delete, :nothing
17
12
  default_action :create
18
13
 
19
- attribute :provisioner
14
+ attribute :driver
20
15
  # Private key to use as input (will be generated if it does not exist)
21
16
  attribute :private_key_path, :kind_of => String
22
17
  # Public key to use as input (will be generated if it does not exist)
@@ -0,0 +1,3 @@
1
+ require 'chef_metal_fog/fog_driver'
2
+
3
+ ChefMetal.register_driver_class("fog", ChefMetalFog::FogDriver)
@@ -0,0 +1,65 @@
1
+ require 'inifile'
2
+ require 'csv'
3
+
4
+ module ChefMetalFog
5
+ # Reads in a credentials file in Amazon's download format and presents the credentials to you
6
+ class AWSCredentials
7
+ def initialize
8
+ @credentials = {}
9
+ end
10
+
11
+ def default
12
+ @credentials[ENV['AWS_DEFAULT_PROFILE'] || 'default'] || @credentials.first[1]
13
+ end
14
+
15
+ def keys
16
+ @credentials.keys
17
+ end
18
+
19
+ def [](name)
20
+ @credentials[name]
21
+ end
22
+
23
+ def each(&block)
24
+ @credentials.each(&block)
25
+ end
26
+
27
+ def load_ini(credentials_ini_file)
28
+ inifile = IniFile.load(File.expand_path(credentials_ini_file))
29
+ inifile.each_section do |section|
30
+ if section =~ /^\s*profile\s+(.+)$/ || section =~ /^\s*(default)\s*/
31
+ profile_name = $1.strip
32
+ profile = inifile[section].inject({}) do |result, pair|
33
+ result[pair[0].to_sym] = pair[1]
34
+ result
35
+ end
36
+ profile[:name] = profile_name
37
+ @credentials[profile_name] = profile
38
+ end
39
+ end
40
+ end
41
+
42
+ def load_csv(credentials_csv_file)
43
+ CSV.new(File.open(credentials_csv_file), :headers => :first_row).each do |row|
44
+ @credentials[row['User Name']] = {
45
+ :name => row['User Name'],
46
+ :user_name => row['User Name'],
47
+ :aws_access_key_id => row['Access Key Id'],
48
+ :aws_secret_access_key => row['Secret Access Key']
49
+ }
50
+ end
51
+ end
52
+
53
+ def load_default
54
+ load_ini('~/.aws/config')
55
+ end
56
+
57
+ def self.method_missing(name, *args, &block)
58
+ singleton.send(name, *args, &block)
59
+ end
60
+
61
+ def self.singleton
62
+ @aws_credentials ||= AWSCredentials.new
63
+ end
64
+ end
65
+ end