podnix 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,148 +0,0 @@
1
- # Copyright:: Copyright (c) 2012, 2013 Megam Systems
2
- # License:: Apache License, Version 2.0
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
- #
16
- module Podnix
17
- class ImagesCollection
18
- include Enumerable
19
-
20
-
21
- attr_reader :iterator
22
- def initialize
23
- @appreqs = Array.new
24
- @appreqs_by_name = Hash.new
25
- @insert_after_idx = nil
26
- end
27
-
28
- def all_appreqs
29
- @appreqs
30
- end
31
-
32
- def [](index)
33
- @appreqs[index]
34
- end
35
-
36
- def []=(index, arg)
37
- is_megam_appreq(arg)
38
- @appreqs[index] = arg
39
- @appreqs_by_name[arg.id] = index
40
- end
41
-
42
- def <<(*args)
43
- args.flatten.each do |a|
44
- is_megam_appreq(a)
45
- @appreqs << a
46
- @appreqs_by_name[a.id] = @appreqs.length - 1
47
- end
48
- self
49
- end
50
-
51
- # 'push' is an alias method to <<
52
- alias_method :push, :<<
53
-
54
- def insert(appreq)
55
- is_megam_appreq(appreq)
56
- if @insert_after_idx
57
- # in the middle of executing a run, so any Appreqss inserted now should
58
- # be placed after the most recent addition done by the currently executing
59
- # appreqs
60
- @appreqs.insert(@insert_after_idx + 1, appreq)
61
- # update name -> location mappings and register new appreqs
62
- @appreqs_by_name.each_key do |key|
63
- @appreqs_by_name[key] += 1 if @appreqs_by_name[key] > @insert_after_idx
64
- end
65
- @appreqs_by_name[appreq.id] = @insert_after_idx + 1
66
- @insert_after_idx += 1
67
- else
68
- @appreqs << appreq
69
- @appreqs_by_name[appreq.id] = @appreqs.length - 1
70
- end
71
- end
72
-
73
- def each
74
- @appreqs.each do |appreq|
75
- yield appreq
76
- end
77
- end
78
-
79
- def each_index
80
- @appreqs.each_index do |i|
81
- yield i
82
- end
83
- end
84
-
85
- def empty?
86
- @appreqs.empty?
87
- end
88
-
89
- def lookup(appreq)
90
- lookup_by = nil
91
- if appreq.kind_of?(Megam::AppRequest)
92
- lookup_by = appreq.id
93
- elsif appreq.kind_of?(String)
94
- lookup_by = appreq
95
- else
96
- raise ArgumentError, "Must pass a Megam::Appreqs or String to lookup"
97
- end
98
- res = @appreqs_by_name[lookup_by]
99
- unless res
100
- raise ArgumentError, "Cannot find a appreq matching #{lookup_by} (did you define it first?)"
101
- end
102
- @appreqs[res]
103
- end
104
-
105
- # Transform the ruby obj -> to a Hash
106
- def to_hash
107
- index_hash = Hash.new
108
- self.each do |appreq|
109
- index_hash[appreq.id] = appreq.to_s
110
- end
111
- index_hash
112
- end
113
-
114
- # Serialize this object as a hash: called from JsonCompat.
115
- # Verify if this called from JsonCompat during testing.
116
- def to_json(*a)
117
- for_json.to_json(*a)
118
- end
119
-
120
-
121
- def self.json_create(o)
122
- collection = self.new()
123
- o["results"].each do |appreqs_list|
124
- appreqs_array = appreqs_list.kind_of?(Array) ? appreqs_list : [ appreqs_list ]
125
- appreqs_array.each do |appreq|
126
- collection.insert(appreq)
127
- end
128
- end
129
- collection
130
- end
131
-
132
- private
133
-
134
-
135
-
136
- def is_megam_appreq(arg)
137
- unless arg.kind_of?(Megam::AppRequest)
138
- raise ArgumentError, "Members must be Megam::Appreq's"
139
- end
140
- true
141
- end
142
-
143
- def to_s
144
- Podnix::Stuff.styled_hash(to_hash)
145
- end
146
-
147
- end
148
- end
@@ -1,162 +0,0 @@
1
- #
2
- # Licensed under the Apache License, Version 2.0 (the "License");
3
- # you may not use this file except in compliance with the License.
4
- # You may obtain a copy of the License at
5
- #
6
- # http://www.apache.org/licenses/LICENSE-2.0
7
- #
8
- # Unless required by applicable law or agreed to in writing, software
9
- # distributed under the License is distributed on an "AS IS" BASIS,
10
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
- # See the License for the specific language governing permissions and
12
- # limitations under the License.
13
-
14
- # Wrapper class for interacting with JSON.
15
-
16
- require 'yajl/json_gem'
17
- require 'yajl'
18
-
19
- module Podnix
20
- class JSONCompat
21
- JSON_MAX_NESTING = 1000
22
-
23
- JSON_CLAZ = "json_claz".freeze
24
-
25
- PODNIX_ERROR = "Podnix::Error".freeze
26
- PODNIX_IMAGES = "Podnix::Image".freeze
27
- PODNIX_IMAGESCOLLECTION = "Podnix::NodeCollection".freeze
28
- PODNIX_SERVERS = "Megam::Predef".freeze
29
- PODNIX_SERVERSCOLLECTION = "Megam::PredefCollection".freeze
30
-
31
- class <<self
32
- # Increase the max nesting for JSON, which defaults
33
- # to 19, and isn't enough for some (for example, a Node within a Node)
34
- # structures.
35
- def opts_add_max_nesting(opts)
36
- if opts.nil? || !opts.has_key?(:max_nesting)
37
- opts = opts.nil? ? Hash.new : opts.clone
38
- opts[:max_nesting] = JSON_MAX_NESTING
39
- end
40
- opts
41
- end
42
-
43
- # Just call the JSON gem's parse method with a modified :max_nesting field
44
- def from_json(source, opts = {})
45
- obj = ::Yajl::Parser.parse(source)
46
- # JSON gem requires top level object to be a Hash or Array (otherwise
47
- # you get the "must contain two octets" error). Yajl doesn't impose the
48
- # same limitation. For compatibility, we re-impose this condition.
49
- unless obj.kind_of?(Hash) or obj.kind_of?(Array)
50
- raise JSON::ParserError, "Top level JSON object must be a Hash or Array. (actual: #{obj.class})"
51
- end
52
-
53
- # The old default in the json gem (which we are mimicing because we
54
- # sadly rely on this misfeature) is to "create additions" i.e., convert
55
- # JSON objects into ruby objects. Explicit :create_additions => false
56
- # is required to turn it off.
57
- if opts[:create_additions].nil? || opts[:create_additions]
58
- map_to_rb_obj(obj)
59
- else
60
- obj
61
- end
62
- end
63
-
64
- # Look at an object that's a basic type (from json parse) and convert it
65
- # to an instance of Megam classes if desired.
66
- def map_to_rb_obj(json_obj)
67
- case json_obj
68
- when Hash
69
- mapped_hash = map_hash_to_rb_obj(json_obj)
70
- if json_obj.has_key?(JSON_CLAZ) && (class_to_inflate = class_for_json_class(json_obj[JSON_CLAZ]))
71
- class_to_inflate.json_create(mapped_hash)
72
- else
73
- mapped_hash
74
- end
75
- when Array
76
- json_obj.map {|e| map_to_rb_obj(e) }
77
- else
78
- json_obj
79
- end
80
- end
81
-
82
- def map_hash_to_rb_obj(json_hash)
83
- json_hash.each do |key, value|
84
- json_hash[key] = map_to_rb_obj(value)
85
- end
86
- json_hash
87
- end
88
-
89
- def to_json(obj, opts = nil)
90
- obj.to_json(opts_add_max_nesting(opts))
91
- end
92
-
93
- def to_json_pretty(obj, opts = nil)
94
- ::JSON.pretty_generate(obj, opts_add_max_nesting(opts))
95
- end
96
-
97
- # Map +JSON_CLAZ+ to a Class object. We use a +case+ instead of a Hash
98
- # assigned to a constant because otherwise this file could not be loaded
99
- # until all the constants were defined, which means you'd have to load
100
- # the world to get json.
101
- def class_for_json_class(json_class)
102
- case json_class
103
- when MEGAM_ERROR
104
- Megam::Error
105
- when MEGAM_AUTH
106
- Megam::Auth
107
- when MEGAM_ACCOUNT
108
- Megam::Account
109
- when MEGAM_NODE
110
- Megam::Node
111
- when MEGAM_APPDEFNS
112
- Megam::Appdefns
113
- when MEGAM_APPREQUEST
114
- Megam::AppRequest
115
- when MEGAM_BOLTREQUEST
116
- Megam::BoltRequest
117
- when MEGAM_BOLTDEFNS
118
- Megam::Boltdefns
119
- when MEGAM_NODECOLLECTION
120
- Megam::NodeCollection
121
- when MEGAM_APPDEFNSCOLLECTION
122
- Megam::AppdefnsCollection
123
- when MEGAM_APPREQUESTCOLLECTION
124
- Megam::AppRequestCollection
125
- when MEGAM_BOLTREQUESTCOLLECTION
126
- Megam::BoltRequestCollection
127
- when MEGAM_BOLTDEFNSCOLLECTION
128
- Megam::BoltdefnsCollection
129
- when MEGAM_REQUEST
130
- Megam::Request
131
- when MEGAM_REQUESTCOLLECTION
132
- Megam::RequestCollection
133
- when MEGAM_PREDEF
134
- Megam::Predef
135
- when MEGAM_PREDEFCOLLECTION
136
- Megam::PredefCollection
137
- when MEGAM_PREDEFCLOUD
138
- Megam::PredefCloud
139
- when MEGAM_PREDEFCLOUDCOLLECTION
140
- Megam::PredefCloudCollection
141
- when MEGAM_CLOUDTOOL
142
- Megam::CloudTool
143
- when MEGAM_CLOUDTOOLCOLLECTION
144
- Megam::CloudToolCollection
145
- when MEGAM_CLOUDTEMPLATE
146
- Megam::CloudTemplate
147
- when MEGAM_CLOUDTEMPLATECOLLECTION
148
- Megam::CloudTemplateCollection
149
- when MEGAM_CLOUDINSTRUCTION
150
- Megam::CloudInstruction
151
- when MEGAM_CLOUDINSTRUCTIONGROUP
152
- Megam::CloudInstructionGroup
153
- when MEGAM_CLOUDINSTRUCTIONCOLLECTION
154
- Megam::CloudInstructionCollection
155
- else
156
- raise JSON::ParserError, "Unsupported `json_class` type '#{json_class}'"
157
- end
158
- end
159
-
160
- end
161
- end
162
- end
@@ -1,375 +0,0 @@
1
- # Copyright:: Copyright (c) 2012, 2013 Megam Systems
2
- # License:: Apache License, Version 2.0
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
- #
16
- require 'hashie'
17
- module Podnix
18
- class Node
19
- # Each notify entry is a resource/action pair, modeled as an
20
- # Struct with a #resource and #action member
21
- =begin
22
- def self.hash_tree
23
- Hash.new do |hash, key|
24
- hash[key] = hash_tree
25
- end
26
- end
27
- =end
28
-
29
- def initialize
30
- @id = nil
31
- @node_name = nil
32
- @accounts_id = nil
33
- @node_type = nil
34
- @req_type = nil
35
- @status=nil
36
- @noofinstances=0
37
- @request ={}
38
- @predefs={}
39
- @some_msg = {}
40
- #@command = self.class.hash_tree
41
- @command = Hashie::Mash.new
42
- @appdefnsid = nil
43
- @boltdefnsid = nil
44
- @appdefns = {}
45
- @boltdefns = {}
46
- @created_at = nil
47
- end
48
- def node
49
- self
50
- end
51
-
52
- def podnix_rest
53
- options = { :email => Megam::Config[:email], :api_key => Megam::Config[:api_key]}
54
- Megam::API.new(options)
55
- end
56
-
57
- def node_name(arg=nil)
58
- if arg != nil
59
- @node_name = arg
60
- else
61
- @node_name
62
- end
63
- end
64
-
65
- def noofinstances(arg=nil)
66
- if arg != nil
67
- @noofinstances = arg
68
- else
69
- @noofinstances
70
- end
71
- end
72
-
73
- def command(arg=nil)
74
- if arg != nil
75
- @command = arg
76
- else
77
- @command
78
- end
79
- end
80
-
81
- def id(arg=nil)
82
- if arg != nil
83
- @id = arg
84
- else
85
- @id
86
- end
87
- end
88
-
89
- def accounts_id(arg=nil)
90
- if arg != nil
91
- @accounts_id = arg
92
- else
93
- @accounts_id
94
- end
95
- end
96
-
97
- def node_type(arg=nil)
98
- if arg != nil
99
- @node_type = arg
100
- else
101
- @node_type
102
- end
103
- end
104
-
105
- def req_type(arg=nil)
106
- if arg != nil
107
- @req_type = arg
108
- else
109
- @req_type
110
- end
111
- end
112
-
113
- def status(arg=nil)
114
- if arg != nil
115
- @status = arg
116
- else
117
- @status
118
- end
119
- end
120
-
121
- def request(arg=nil)
122
- if arg != nil
123
- @request = arg
124
- else
125
- @request
126
- end
127
- end
128
-
129
- def predefs(arg=nil)
130
- if arg != nil
131
- @predefs = arg
132
- else
133
- @predefs
134
- end
135
- end
136
-
137
- def appdefns(arg=nil)
138
- if arg != nil
139
- @appdefns = arg
140
- else
141
- @appdefns
142
- end
143
- end
144
-
145
- def boltdefns(arg=nil)
146
- if arg != nil
147
- @boltdefns = arg
148
- else
149
- @boltdefns
150
- end
151
- end
152
- def appdefnsid(arg=nil)
153
- if arg != nil
154
- @appdefnsid = arg
155
- else
156
- @appdefnsid
157
- end
158
- end
159
-
160
- def boltdefnsid(arg=nil)
161
- if arg != nil
162
- @boltdefnsid = arg
163
- else
164
- @boltdefnsid
165
- end
166
- end
167
-
168
- def created_at(arg=nil)
169
- if arg != nil
170
- @created_at = arg
171
- else
172
- @created_at
173
- end
174
- end
175
-
176
- def some_msg(arg=nil)
177
- if arg != nil
178
- @some_msg = arg
179
- else
180
- @some_msg
181
- end
182
- end
183
-
184
- def error?
185
- crocked = true if (some_msg.has_key?(:msg_type) && some_msg[:msg_type] == "error")
186
- end
187
-
188
- # Transform the ruby obj -> to a Hash
189
- def to_hash
190
- index_hash = Hash.new
191
- index_hash["json_claz"] = self.class.name
192
- index_hash["id"] = id
193
- index_hash["node_name"] = node_name
194
- index_hash["accounts_id"] = accounts_id
195
- index_hash["node_type"] = node_type
196
- index_hash["req_type"] = req_type
197
- index_hash["status"] = status
198
- index_hash["command"] = command
199
- index_hash["request"] = request
200
- index_hash["predefs"] = predefs
201
- index_hash["appdefns"] = appdefns
202
- index_hash["boltdefns"] = boltdefns
203
- index_hash["some_msg"] = some_msg
204
- index_hash["noofinstances"] = noofinstances.to_i
205
- index_hash["appdefnsid"] = appdefnsid
206
- index_hash["boltdefnsid"] = boltdefnsid
207
- index_hash["created_at"] = created_at
208
- index_hash
209
- end
210
-
211
- # Serialize this object as a hash: called from JsonCompat.
212
- # Verify if this called from JsonCompat during testing.
213
- def to_json(*a)
214
- for_json.to_json(*a)
215
- end
216
-
217
- def for_json
218
- result = {
219
- "id" => id,
220
- "node_name" => node_name,
221
- "accounts_id" => accounts_id,
222
- "node_type" => node_type,
223
- "req_type" => req_type,
224
- "status" => status,
225
- "request" => request,
226
- "predefs" => predefs,
227
- "appdefns" => appdefns,
228
- "boltdefns" => boltdefns,
229
- "appdefnsid" => appdefnsid,
230
- "boltdefnsid" => boltdefnsid,
231
- "noofinstances" => noofinstances,
232
- "created_at" => created_at
233
- }
234
- result
235
- end
236
-
237
- # Create a Megam::Node from NodeResult-JSON
238
- #
239
- #[{
240
- #"id":"NOD362212018897289216",
241
- #"accounts_id":"ACT362211962353876992",
242
- #"json_claz":"Megam::Node",
243
- #"request":{
244
- #"req_id":"NOD362212018897289216",
245
- #"command":"commands"
246
- #},
247
- #"predefs":{
248
- #"name":"",
249
- #"scm":"",
250
- #"war":"",
251
- #"db":"",
252
- #"queue":""
253
- #}
254
- #}]
255
- #
256
- def self.json_create(o)
257
- node = new
258
- node.id(o["id"]) if o.has_key?("id")
259
- node.node_name(o["node_name"]) if o.has_key?("node_name")
260
- node.accounts_id(o["accounts_id"]) if o.has_key?("accounts_id")
261
- node.node_type(o["node_type"]) if o.has_key?("node_type")
262
- node.req_type(o["req_type"]) if o.has_key?("req_type")
263
- node.status(o["status"]) if o.has_key?("status")
264
- node.appdefnsid(o["appdefnsid"]) if o.has_key?("appdefnsid")
265
- node.boltdefnsid(o["boltdefnsid"]) if o.has_key?("boltdefnsid")
266
- node.noofinstances(o["noofinstances"]) if o.has_key?("noofinstances")
267
- node.created_at(o["created_at"]) if o.has_key?("created_at")
268
- #requests
269
- oq = o["request"]
270
- node.request[:req_id] = oq["req_id"] if oq && oq.has_key?("req_id")
271
- node.request[:req_type] = oq["req_type"] if oq && oq.has_key?("req_type")
272
- node.request[:command] = oq["command"] if oq && oq.has_key?("command")
273
-
274
- #Command
275
- =begin
276
- node.command[:systemprovider][:provider][:prov] = oc["systemprovider"]["provider"]["prov"]
277
- node.command[:compute][:cctype] = oc["compute"]["cctype"]
278
- node.command[:compute][:cc][:groups] = oc["compute"]["cc"]["groups"]
279
- node.command[:compute][:cc][:image] = oc["compute"]["cc"]["image"]
280
- node.command[:compute][:cc][:flavor] = oc["compute"]["cc"]["flavor"]
281
- node.command[:compute][:access][:ssh_key] = oc["compute"]["access"]["ssh_key"]
282
- node.command[:compute][:access][:identity_file] = oc["compute"]["access"]["identity_file"]
283
- node.command[:compute][:access][:ssh_user] = oc["compute"]["access"]["ssh_user"]
284
- node.command[:cloudtool][:chef][:command] = oc["cloudtool"]["chef"]["command"]
285
- node.command[:cloudtool][:chef][:plugin] = oc["cloudtool"]["chef"]["plugin"]
286
- node.command[:cloudtool][:chef][:run_list] = oc["cloudtool"]["chef"]["run_list"]
287
- node.command[:cloudtool][:chef][:name] = oc["cloudtool"]["chef"]["name"]
288
- =end
289
- #predef
290
- op = o["predefs"]
291
- node.predefs[:name] = op["name"] if op && op.has_key?("name")
292
- node.predefs[:scm] = op["scm"] if op && op.has_key?("scm")
293
- node.predefs[:war]= op["war"] if op && op.has_key?("war")
294
- node.predefs[:db] = op["db"] if op && op.has_key?("db")
295
- node.predefs[:queue] = op["queue"] if op && op.has_key?("queue")
296
-
297
- #APP DEFINITIONS
298
- op = o["appdefns"]
299
- node.appdefns[:timetokill] = op["timetokill"] if op && op.has_key?("timetokill")
300
- node.appdefns[:metered] = op["metered"] if op && op.has_key?("metered")
301
- node.appdefns[:logging]= op["logging"] if op && op.has_key?("logging")
302
- node.appdefns[:runtime_exec] = op["runtime_exec"] if op && op.has_key?("runtime_exec")
303
-
304
- #BOLT DEFINITIONS
305
- op = o["boltdefns"]
306
- node.boltdefns[:username] = op["username"] if op && op.has_key?("username")
307
- node.boltdefns[:apikey] = op["apikey"] if op && op.has_key?("apikey")
308
- node.boltdefns[:store_name]= op["store_name"] if op && op.has_key?("store_name")
309
- node.boltdefns[:url] = op["url"] if op && op.has_key?("url")
310
- node.boltdefns[:timetokill] = op["timetokill"] if op && op.has_key?("timetokill")
311
- node.boltdefns[:metered] = op["metered"] if op && op.has_key?("metered")
312
- node.boltdefns[:logging]= op["logging"] if op && op.has_key?("logging")
313
- node.boltdefns[:runtime_exec] = op["runtime_exec"] if op && op.has_key?("runtime_exec")
314
-
315
- #success or error
316
- node.some_msg[:code] = o["code"] if o.has_key?("code")
317
- node.some_msg[:msg_type] = o["msg_type"] if o.has_key?("msg_type")
318
- node.some_msg[:msg]= o["msg"] if o.has_key?("msg")
319
- node.some_msg[:links] = o["links"] if o.has_key?("links")
320
-
321
- node
322
- end
323
-
324
- def self.from_hash(o)
325
- node = self.new()
326
- node.from_hash(o)
327
- node
328
- end
329
-
330
- def from_hash(o)
331
- @node_name = o["node_name"] if o.has_key?("node_name")
332
- @command = o["command"] if o.has_key?("command")
333
- @id = o["id"] if o.has_key?("id")
334
- @accounts_id = o["accounts_id"] if o.has_key?("accounts_id")
335
- @node_type = o["node_type"] if o.has_key?("node_type")
336
- @req_type = o["req_type"] if o.has_key?("req_type")
337
- @request = o["request"] if o.has_key?("request")
338
- @predefs = o["predefs"] if o.has_key?("predefs")
339
- @appdefns = o["appdefns"] if o.has_key?("appdefns")
340
- @boltdefns = o["boltdefns"] if o.has_key?("boltdefns")
341
- @appdefnsid = o["appdefnsid"] if o.has_key?("appdefnsid")
342
- @boltdefnsid = o["boltdefnsid"] if o.has_key?("boltdefnsid")
343
- @noofinstances = o["noofinstances"] if o.has_key?("noofinstances")
344
- @created_at = o["created_at"] if o.has_key?("created_at")
345
- self
346
- end
347
-
348
- def self.create(o)
349
- acct = from_hash(o)
350
- acct.create
351
- end
352
-
353
- # Create the node via the REST API
354
- def create
355
- podnix_rest.post_node(to_hash)
356
- end
357
-
358
- # Load a account by email_p
359
- def self.show(node_name)
360
- node = self.new()
361
- node.podnix_rest.get_node(node_name)
362
- end
363
-
364
- def self.list
365
- node = self.new()
366
- node.podnix_rest.get_nodes
367
- end
368
-
369
- def to_s
370
- Podnix::Stuff.styled_hash(to_hash)
371
- #"---> Megam::Account:[error=#{error?}]\n"+
372
- end
373
-
374
- end
375
- end