miasma 0.2.10 → 0.2.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,105 +0,0 @@
1
- require 'miasma'
2
-
3
- module Miasma
4
- module Models
5
- class Compute
6
- class OpenStack < Compute
7
-
8
- include Contrib::OpenStackApiCore::ApiCommon
9
-
10
- # @return [Smash] map state to valid internal values
11
- SERVER_STATE_MAP = Smash.new(
12
- 'ACTIVE' => :running,
13
- 'DELETED' => :terminated,
14
- 'SUSPENDED' => :stopped,
15
- 'PASSWORD' => :running
16
- )
17
-
18
- def server_save(server)
19
- unless(server.persisted?)
20
- server.load_data(server.attributes)
21
- result = request(
22
- :expects => 202,
23
- :method => :post,
24
- :path => '/servers',
25
- :json => {
26
- :server => {
27
- :flavorRef => server.flavor_id,
28
- :name => server.name,
29
- :imageRef => server.image_id,
30
- :metadata => server.metadata,
31
- :personality => server.personality,
32
- :key_pair => server.key_name
33
- }
34
- }
35
- )
36
- server.id = result.get(:body, :server, :id)
37
- else
38
- raise "WAT DO I DO!?"
39
- end
40
- end
41
-
42
- def server_destroy(server)
43
- if(server.persisted?)
44
- result = request(
45
- :expects => 204,
46
- :method => :delete,
47
- :path => "/servers/#{server.id}"
48
- )
49
- true
50
- else
51
- false
52
- end
53
- end
54
-
55
- def server_change_state(server, state)
56
- end
57
-
58
- def server_reload(server)
59
- res = servers.reload.all
60
- node = res.detect do |s|
61
- s.id == server.id
62
- end
63
- if(node)
64
- server.load_data(node.data.dup)
65
- server.valid_state
66
- else
67
- server.data[:state] = :terminated
68
- server.dirty.clear
69
- server
70
- end
71
- end
72
-
73
- def server_all
74
- result = request(
75
- :method => :get,
76
- :path => '/servers/detail'
77
- )
78
- result[:body].fetch(:servers, []).map do |srv|
79
- Server.new(
80
- self,
81
- :id => srv[:id],
82
- :name => srv[:name],
83
- :image_id => srv.get(:image, :id),
84
- :flavor_id => srv.get(:flavor, :id),
85
- :state => SERVER_STATE_MAP.fetch(srv[:status], :pending),
86
- :addresses_private => srv.fetch(:addresses, :private, []).map{|a|
87
- Server::Address.new(
88
- :version => a[:version].to_i, :address => a[:addr]
89
- )
90
- },
91
- :addresses_public => srv.fetch(:addresses, :public, []).map{|a|
92
- Server::Address.new(
93
- :version => a[:version].to_i, :address => a[:addr]
94
- )
95
- },
96
- :status => srv[:status],
97
- :key_name => srv[:key_name]
98
- ).valid_state
99
- end
100
- end
101
-
102
- end
103
- end
104
- end
105
- end
@@ -1,255 +0,0 @@
1
- require 'miasma'
2
-
3
- module Miasma
4
- module Models
5
- class Orchestration
6
- class OpenStack < Orchestration
7
-
8
- include Contrib::OpenStackApiCore::ApiCommon
9
-
10
- # @return [Smash] external to internal resource mapping
11
- RESOURCE_MAPPING = Smash.new(
12
- 'OS::Nova::Server' => Smash.new(
13
- :api => :compute,
14
- :collection => :servers
15
- )
16
- # 'OS::Heat::AutoScalingGroup' => Smash.new(
17
- # :api => :auto_scale,
18
- # :collection => :groups
19
- # )
20
- )
21
-
22
- # Save the stack
23
- #
24
- # @param stack [Models::Orchestration::Stack]
25
- # @return [Models::Orchestration::Stack]
26
- def stack_save(stack)
27
- if(stack.persisted?)
28
- stack.load_data(stack.attributes)
29
- result = request(
30
- :expects => 202,
31
- :method => :put,
32
- :path => "/stacks/#{stack.name}/#{stack.id}",
33
- :json => {
34
- :stack_name => stack.name,
35
- :template => MultiJson.dump(stack.template),
36
- :parameters => stack.parameters || {}
37
- }
38
- )
39
- stack.valid_state
40
- else
41
- stack.load_data(stack.attributes)
42
- result = request(
43
- :expects => 201,
44
- :method => :post,
45
- :path => '/stacks',
46
- :json => {
47
- :stack_name => stack.name,
48
- :template => MultiJson.dump(stack.template),
49
- :parameters => stack.parameters || {},
50
- :disable_rollback => (!!stack.disable_rollback).to_s
51
- }
52
- )
53
- stack.id = result.get(:body, :stack, :id)
54
- stack.valid_state
55
- end
56
- end
57
-
58
- # Reload the stack data from the API
59
- #
60
- # @param stack [Models::Orchestration::Stack]
61
- # @return [Models::Orchestration::Stack]
62
- def stack_reload(stack)
63
- if(stack.persisted?)
64
- result = request(
65
- :method => :get,
66
- :path => "/stacks/#{stack.name}/#{stack.id}",
67
- :expects => 200
68
- )
69
- stk = result.get(:body, :stack)
70
- stack.load_data(
71
- :id => stk[:id],
72
- :capabilities => stk[:capabilities],
73
- :created => Time.parse(stk[:creation_time]),
74
- :description => stk[:description],
75
- :disable_rollback => stk[:disable_rollback].to_s.downcase == 'true',
76
- :notification_topics => stk[:notification_topics],
77
- :name => stk[:stack_name],
78
- :state => stk[:stack_status].downcase.to_sym,
79
- :status => stk[:stack_status],
80
- :status_reason => stk[:stack_status_reason],
81
- :template_description => stk[:template_description],
82
- :timeout_in_minutes => stk[:timeout_mins].to_s.empty? ? nil : stk[:timeout_mins].to_i,
83
- :updated => stk[:updated_time].to_s.empty? ? nil : Time.parse(stk[:updated_time]),
84
- :parameters => stk.fetch(:parameters, Smash.new),
85
- :outputs => stk.fetch(:outputs, []).map{ |output|
86
- Smash.new(
87
- :key => output[:output_key],
88
- :value => output[:output_value],
89
- :description => output[:description]
90
- )
91
- }
92
- ).valid_state
93
- end
94
- stack
95
- end
96
-
97
- # Delete the stack
98
- #
99
- # @param stack [Models::Orchestration::Stack]
100
- # @return [TrueClass, FalseClass]
101
- def stack_destroy(stack)
102
- if(stack.persisted?)
103
- request(
104
- :method => :delete,
105
- :path => "/stacks/#{stack.name}/#{stack.id}",
106
- :expects => 204
107
- )
108
- true
109
- else
110
- false
111
- end
112
- end
113
-
114
- # Fetch stack template
115
- #
116
- # @param stack [Stack]
117
- # @return [Smash] stack template
118
- def stack_template_load(stack)
119
- if(stack.persisted?)
120
- result = request(
121
- :method => :get,
122
- :path => "/stacks/#{stack.name}/#{stack.id}/template"
123
- )
124
- result.fetch(:body, Smash.new)
125
- else
126
- Smash.new
127
- end
128
- end
129
-
130
- # Validate stack template
131
- #
132
- # @param stack [Stack]
133
- # @return [NilClass, String] nil if valid, string error message if invalid
134
- def stack_template_validate(stack)
135
- begin
136
- result = request(
137
- :method => :post,
138
- :path => '/validate',
139
- :json => Smash.new(
140
- :template => stack.template
141
- )
142
- )
143
- nil
144
- rescue Error::ApiError::RequestError => e
145
- MultiJson.load(e.response.body.to_s).to_smash.get(:error, :message)
146
- end
147
- end
148
-
149
- # Return all stacks
150
- #
151
- # @param options [Hash] filter
152
- # @return [Array<Models::Orchestration::Stack>]
153
- # @todo check if we need any mappings on state set
154
- def stack_all(options={})
155
- result = request(
156
- :method => :get,
157
- :path => '/stacks'
158
- )
159
- result.fetch(:body, :stacks, []).map do |s|
160
- Stack.new(
161
- self,
162
- :id => s[:id],
163
- :created => Time.parse(s[:creation_time]),
164
- :description => s[:description],
165
- :name => s[:stack_name],
166
- :state => s[:stack_status].downcase.to_sym,
167
- :status => s[:stack_status],
168
- :status_reason => s[:stack_status_reason],
169
- :updated => s[:updated_time].to_s.empty? ? nil : Time.parse(s[:updated_time])
170
- ).valid_state
171
- end
172
- end
173
-
174
- # Return all resources for stack
175
- #
176
- # @param stack [Models::Orchestration::Stack]
177
- # @return [Array<Models::Orchestration::Stack::Resource>]
178
- def resource_all(stack)
179
- result = request(
180
- :method => :get,
181
- :path => "/stacks/#{stack.name}/#{stack.id}/resources",
182
- :expects => 200
183
- )
184
- result.fetch(:body, :resources, []).map do |resource|
185
- Stack::Resource.new(
186
- stack,
187
- :id => resource[:physical_resource_id],
188
- :name => resource[:resource_name],
189
- :type => resource[:resource_type],
190
- :logical_id => resource[:logical_resource_id],
191
- :state => resource[:resource_status].downcase.to_sym,
192
- :status => resource[:resource_status],
193
- :status_reason => resource[:resource_status_reason],
194
- :updated => Time.parse(resource[:updated_time])
195
- ).valid_state
196
- end
197
- end
198
-
199
- # Reload the stack resource data from the API
200
- #
201
- # @param resource [Models::Orchestration::Stack::Resource]
202
- # @return [Models::Orchestration::Resource]
203
- def resource_reload(resource)
204
- resource.stack.resources.reload
205
- resource.stack.resources.get(resource.id)
206
- end
207
-
208
- # Return all events for stack
209
- #
210
- # @param stack [Models::Orchestration::Stack]
211
- # @return [Array<Models::Orchestration::Stack::Event>]
212
- def event_all(stack, marker = nil)
213
- params = marker ? {:marker => marker} : {}
214
- result = request(
215
- :path => "/stacks/#{stack.name}/#{stack.id}/events",
216
- :method => :get,
217
- :expects => 200,
218
- :params => params
219
- )
220
- result.fetch(:body, :events, []).map do |event|
221
- Stack::Event.new(
222
- stack,
223
- :id => event[:id],
224
- :resource_id => event[:physical_resource_id],
225
- :resource_name => event[:resource_name],
226
- :resource_logical_id => event[:logical_resource_id],
227
- :resource_state => event[:resource_status].downcase.to_sym,
228
- :resource_status => event[:resource_status],
229
- :resource_status_reason => event[:resource_status_reason],
230
- :time => Time.parse(event[:event_time])
231
- ).valid_state
232
- end
233
- end
234
-
235
- # Return all new events for event collection
236
- #
237
- # @param events [Models::Orchestration::Stack::Events]
238
- # @return [Array<Models::Orchestration::Stack::Event>]
239
- def event_all_new(events)
240
- event_all(events.stack, events.all.first.id)
241
- end
242
-
243
- # Reload the stack event data from the API
244
- #
245
- # @param resource [Models::Orchestration::Stack::Event]
246
- # @return [Models::Orchestration::Event]
247
- def event_reload(event)
248
- event.stack.events.reload
249
- event.stack.events.get(event.id)
250
- end
251
-
252
- end
253
- end
254
- end
255
- end
@@ -1,112 +0,0 @@
1
- require 'miasma'
2
- require 'miasma/contrib/open_stack'
3
-
4
- module Miasma
5
- module Contrib
6
-
7
- # Rackspace API core helper
8
- class RackspaceApiCore < OpenStackApiCore
9
-
10
- # Authentication helper class
11
- class Authenticate < OpenStackApiCore::Authenticate
12
- # Authentication implementation compatible for v2
13
- class Version2 < OpenStackApiCore::Authenticate::Version2
14
-
15
- # @return [Smash] authentication request body
16
- def authentication_request
17
- Smash.new(
18
- 'RAX-KSKEY:apiKeyCredentials' => Smash.new(
19
- 'username' => credentials[:open_stack_username],
20
- 'apiKey' => credentials[:open_stack_token]
21
- )
22
- )
23
- end
24
-
25
- end
26
- end
27
-
28
- # Common API methods
29
- module ApiCommon
30
-
31
- # Set attributes into model
32
- #
33
- # @param klass [Class]
34
- def self.included(klass)
35
- klass.attributes.clear
36
-
37
- klass.class_eval do
38
- attribute :rackspace_api_key, String, :required => true
39
- attribute :rackspace_username, String, :required => true
40
- attribute :rackspace_region, String, :required => true
41
-
42
- # @return [Miasma::Contrib::RackspaceApiCore]
43
- def open_stack_api
44
- key = "miasma_rackspace_api_#{attributes.checksum}".to_sym
45
- memoize(key, :direct) do
46
- Miasma::Contrib::RackspaceApiCore.new(attributes)
47
- end
48
- end
49
-
50
- # @return [String]
51
- def open_stack_region
52
- rackspace_region
53
- end
54
-
55
- end
56
- end
57
-
58
-
59
- end
60
-
61
- # @return [Smash] Authentication endpoints
62
- AUTH_ENDPOINT = Smash.new(
63
- :us => 'https://identity.api.rackspacecloud.com/v2.0',
64
- :uk => 'https://lon.identity.api.rackspacecloud.com/v2.0'
65
- )
66
-
67
- # @return [Smash] Mapping to external service name
68
- # @note ["cloudFilesCDN", "cloudFiles", "cloudBlockStorage",
69
- # "cloudImages", "cloudQueues", "cloudBigData",
70
- # "cloudOrchestration", "cloudServersOpenStack", "autoscale",
71
- # "cloudDatabases", "cloudBackup", "cloudMetrics",
72
- # "cloudLoadBalancers", "cloudNetworks", "cloudFeeds",
73
- # "cloudMonitoring", "cloudDNS"]
74
-
75
- API_MAP = Smash.new(
76
- 'compute' => 'cloudServersOpenStack',
77
- 'orchestration' => 'cloudOrchestration',
78
- 'auto_scale' => 'autoscale',
79
- 'load_balancer' => 'cloudLoadBalancers'
80
- )
81
-
82
- # Create a new api instance
83
- #
84
- # @param creds [Smash] credential hash
85
- # @return [self]
86
- def initialize(creds)
87
- if(creds[:rackspace_region].to_s == 'lon')
88
- endpoint = AUTH_ENDPOINT[:uk]
89
- else
90
- endpoint = AUTH_ENDPOINT[:us]
91
- end
92
- super Smash.new(
93
- :open_stack_username => creds[:rackspace_username],
94
- :open_stack_token => creds[:rackspace_api_key],
95
- :open_stack_region => creds[:rackspace_region],
96
- :open_stack_identity_url => endpoint
97
- )
98
- end
99
-
100
- # @return [String] ID of account
101
- def account_id
102
- identity.token[:tenant][:id]
103
- end
104
-
105
- end
106
- end
107
-
108
- Models::Compute.autoload :Rackspace, 'miasma/contrib/rackspace/compute'
109
- Models::Orchestration.autoload :Rackspace, 'miasma/contrib/rackspace/orchestration'
110
- Models::AutoScale.autoload :Rackspace, 'miasma/contrib/rackspace/auto_scale'
111
- Models::LoadBalancer.autoload :Rackspace, 'miasma/contrib/rackspace/load_balancer'
112
- end