megam_api 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -152,9 +152,9 @@ module Megam
152
152
  end
153
153
 
154
154
  # Load a account by email_p
155
- def self.show(email_p)
156
- megam_rest.get_accounts(email)
157
- self
155
+ def self.show(email)
156
+ acct = self.new()
157
+ acct.megam_rest.get_accounts(email)
158
158
  end
159
159
 
160
160
  # Create the node via the REST API
@@ -0,0 +1,197 @@
1
+
2
+ # Copyright:: Copyright (c) 2012, 2013 Megam Systems
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+ module Megam
18
+ class CloudToolSetting
19
+ # Each notify entry is a resource/action pair, modeled as an
20
+ # Struct with a #resource and #action member
21
+ def initialize
22
+ @id = nil
23
+ @accounts_id = nil
24
+ @cloud_type = nil
25
+ @repo = nil
26
+ @vault_location=nil
27
+ @created_at = nil
28
+ @some_msg = {}
29
+ end
30
+
31
+ def cloud_tool_setting
32
+ self
33
+ end
34
+
35
+ def megam_rest
36
+ options = { :email => Megam::Config[:email], :api_key => Megam::Config[:api_key]}
37
+ Megam::API.new(options)
38
+ end
39
+
40
+ def id(arg=nil)
41
+ if arg != nil
42
+ @id = arg
43
+ else
44
+ @id
45
+ end
46
+ end
47
+
48
+ def accounts_id(arg=nil)
49
+ if arg != nil
50
+ @accounts_id= arg
51
+ else
52
+ @accounts_id
53
+ end
54
+ end
55
+
56
+ def cloud_type(arg=nil)
57
+ if arg != nil
58
+ @cloud_type = arg
59
+ else
60
+ @cloud_type
61
+ end
62
+ end
63
+
64
+ def repo(arg=nil)
65
+ if arg != nil
66
+ @repo = arg
67
+ else
68
+ @repo
69
+ end
70
+ end
71
+
72
+ def vault_location(arg=nil)
73
+ if arg != nil
74
+ @vault_location= arg
75
+ else
76
+ @vault_location
77
+ end
78
+ end
79
+
80
+ def created_at(arg=nil)
81
+ if arg != nil
82
+ @created_at = arg
83
+ else
84
+ @created_at
85
+ end
86
+ end
87
+
88
+ def some_msg(arg=nil)
89
+ if arg != nil
90
+ @some_msg = arg
91
+ else
92
+ @some_msg
93
+ end
94
+ end
95
+
96
+ def error?
97
+ crocked = true if (some_msg.has_key?(:msg_type) && some_msg[:msg_type] == "error")
98
+ end
99
+
100
+ # Transform the ruby obj -> to a Hash
101
+ def to_hash
102
+ index_hash = Hash.new
103
+ index_hash["json_claz"] = self.class.name
104
+ index_hash["id"] = id
105
+ index_hash["accounts_id"] = accounts_id
106
+ index_hash["cloud_type"] = cloud_type
107
+ index_hash["repo"] = repo
108
+ index_hash["vault_location"] = vault_location
109
+ index_hash["created_at"] = created_at
110
+ index_hash["some_msg"] = some_msg
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
+ def for_json
121
+ result = {
122
+ "id" => id,
123
+ "accounts_id" => accounts_id,
124
+ "cloud_type" => cloud_type,
125
+ "repo" => repo,
126
+ "vault_location" => vault_location,
127
+ "created_at" => created_at
128
+ }
129
+ result
130
+ end
131
+
132
+ #
133
+ def self.json_create(o)
134
+ cts = new
135
+ cts.id(o["id"]) if o.has_key?("id")
136
+ cts.accounts_id(o["accounts_id"]) if o.has_key?("accounts_id")
137
+ cts.cloud_type(o["cloud_type"]) if o.has_key?("cloud_type")
138
+ cts.repo(o["repo"]) if o.has_key?("repo")
139
+ cts.vault_location(o["vault_location"]) if o.has_key?("vault_location")
140
+
141
+ cts.created_at(o["created_at"]) if o.has_key?("created_at")
142
+ #success or error
143
+ cts.some_msg[:code] = o["code"] if o.has_key?("code")
144
+ cts.some_msg[:msg_type] = o["msg_type"] if o.has_key?("msg_type")
145
+ cts.some_msg[:msg]= o["msg"] if o.has_key?("msg")
146
+ cts.some_msg[:links] = o["links"] if o.has_key?("links")
147
+ cts
148
+ end
149
+
150
+ def self.from_hash(o)
151
+ cts = self.new()
152
+ cts.from_hash(o)
153
+ cts
154
+ end
155
+
156
+ def from_hash(o)
157
+ @id = o[:id] if o.has_key?(:id)
158
+ @accounts_id = o[:accounts_id] if o.has_key?(:accounts_id)
159
+ @cloud_type = o[:cloud_type] if o.has_key?(:cloud_type)
160
+ @repo = o[:repo] if o.has_key?(:repo)
161
+ @vault_location = o[:vault_location] if o.has_key?(:vault_location)
162
+ @created_at = o[:created_at] if o.has_key?(:created_at)
163
+ self
164
+ end
165
+
166
+ def self.create(o)
167
+ acct = from_hash(o)
168
+ acct.create
169
+ end
170
+
171
+ # Create the cloudtoolsetting via the REST API
172
+ def create
173
+ megam_rest.post_cloudtoolsetting(to_hash)
174
+ end
175
+
176
+ # Load all cloudtoolsettings -
177
+ # returns a cloudtoolsettingsCollection
178
+ # don't return self. check if the Megam::cloudtoolsettingCollection is returned.
179
+ def self.list
180
+ cts = self.new()
181
+ cts.megam_rest.get_cloudtoolsettings
182
+ end
183
+
184
+ # Show a particular cloudtoolsetting by name,
185
+ # Megam::cloudtoolsetting
186
+ def self.show(p_name)
187
+ pre = self.new()
188
+ pre.megam_rest.get_cloudtoolsetting(p_name)
189
+ end
190
+
191
+ def to_s
192
+ Megam::Stuff.styled_hash(to_hash)
193
+ #"---> Megam::Account:[error=#{error?}]\n"+
194
+ end
195
+
196
+ end
197
+ end
@@ -0,0 +1,168 @@
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 Megam
17
+ class CloudToolSettingCollection
18
+ include Enumerable
19
+
20
+ attr_reader :iterator
21
+ def initialize
22
+ @cloudtoolsettings = Array.new
23
+ @cloudtoolsettings_by_name = Hash.new
24
+ @insert_after_idx = nil
25
+ end
26
+
27
+ def all_cloudtoolsettings
28
+ @cloudtoolsettings
29
+ end
30
+
31
+ def [](index)
32
+ @cloudtoolsettings[index]
33
+ end
34
+
35
+ def []=(index, arg)
36
+ is_megam_cloudtoolsettings(arg)
37
+ @cloudtoolsettings[index] = arg
38
+ @cloudtoolsettings_by_name[arg.name] = index
39
+ end
40
+
41
+ def <<(*args)
42
+ args.flatten.each do |a|
43
+ is_megam_cloudtoolsettings(a)
44
+ @cloudtoolsettings << a
45
+ @cloudtoolsettings_by_name[a.name] =@cloudtoolsettings.length - 1
46
+ end
47
+ self
48
+ end
49
+
50
+ # 'push' is an alias method to <<
51
+ alias_method :push, :<<
52
+
53
+ def insert(cloudtoolsettings)
54
+ is_megam_cloudtoolsettings(cloudtoolsettings)
55
+ if @insert_after_idx
56
+ # in the middle of executing a run, so any cloudtoolsettings inserted now should
57
+ # be placed after the most recent addition done by the currently executing
58
+ # cloudtoolsettings
59
+ @cloudtoolsettings.insert(@insert_after_idx + 1, cloudtoolsettings)
60
+ # update name -> location mappings and register new cloudtoolsettings
61
+ @cloudtoolsettings_by_name.each_key do |key|
62
+ @cloudtoolsettings_by_name[key] += 1 if@cloudtoolsettings_by_name[key] > @insert_after_idx
63
+ end
64
+ @cloudtoolsettings_by_name[cloudtoolsettings.cloud_type] = @insert_after_idx + 1
65
+ @insert_after_idx += 1
66
+ else
67
+ @cloudtoolsettings << cloudtoolsettings
68
+ @cloudtoolsettings_by_name[cloudtoolsettings.cloud_type] =@cloudtoolsettings.length - 1
69
+ end
70
+ end
71
+
72
+ def each
73
+ @cloudtoolsettings.each do |cloudtoolsettings|
74
+ yield cloudtoolsettings
75
+ end
76
+ end
77
+
78
+ def each_index
79
+ @cloudtoolsettings.each_index do |i|
80
+ yield i
81
+ end
82
+ end
83
+
84
+ def empty?
85
+ @cloudtoolsettings.empty?
86
+ end
87
+
88
+ def lookup(cloudtoolsettings)
89
+ lookup_by = nil
90
+ if cloudtoolsettings.kind_of?(Megam::CloudToolSetting)
91
+ lookup_by = cloudtoolsettings.cloud_type
92
+ elsif cloudtoolsettings.kind_of?(String)
93
+ lookup_by = cloudtoolsettings
94
+ else
95
+ raise ArgumentError, "Must pass a Megam::cloudtoolsettings or String to lookup"
96
+ end
97
+ res =@cloudtoolsettings_by_name[lookup_by]
98
+ unless res
99
+ raise ArgumentError, "Cannot find a cloudtoolsettings matching #{lookup_by} (did you define it first?)"
100
+ end
101
+ @cloudtoolsettings[res]
102
+ end
103
+
104
+ # Transform the ruby obj -> to a Hash
105
+ def to_hash
106
+ index_hash = Hash.new
107
+ self.each do |cloudtoolsettings|
108
+ index_hash[cloudtoolsettings.cloud_type] = cloudtoolsettings.to_s
109
+ end
110
+ index_hash
111
+ end
112
+
113
+ # Serialize this object as a hash: called from JsonCompat.
114
+ # Verify if this called from JsonCompat during testing.
115
+ def to_json(*a)
116
+ for_json.to_json(*a)
117
+ end
118
+
119
+ =begin
120
+ {
121
+ "json_claz":"Megam::PredefCloudCollection",
122
+ "results":[{
123
+ "id":"NOD362554470640386048",
124
+ "name":"ec2_rails",
125
+ "account_id":"ACT362544229488001024",
126
+ "json_claz":"Megam::PredefCloud",
127
+ "spec":{
128
+ "type_name":"sensor-type",
129
+ "groups":"sens-group",
130
+ "image":"sens-image",
131
+ "flavor":"sens-flvr"
132
+ },
133
+ "access":{
134
+ "ssh_key":"sens-ssh",
135
+ "identity_file":"sens-identity-file",
136
+ "ssh_user":"sens-sshuser"
137
+ },
138
+ "ideal":"",
139
+ "performance":""
140
+ }]
141
+ }
142
+ =end
143
+ def self.json_create(o)
144
+ collection = self.new()
145
+ o["results"].each do |cloudtoolsettings_list|
146
+ cloudtoolsettings_array = cloudtoolsettings_list.kind_of?(Array) ? cloudtoolsettings_list : [ cloudtoolsettings_list ]
147
+ cloudtoolsettings_array.each do |cloudtoolsettings|
148
+ collection.insert(cloudtoolsettings)
149
+ end
150
+ end
151
+ collection
152
+ end
153
+
154
+ private
155
+
156
+ def is_megam_cloudtoolsettings(arg)
157
+ unless arg.kind_of?(Megam::CloudToolSetting)
158
+ raise ArgumentError, "Members must be Megam::cloudtoolsetting's"
159
+ end
160
+ true
161
+ end
162
+
163
+ def to_s
164
+ Megam::Stuff.styled_hash(to_hash)
165
+ end
166
+
167
+ end
168
+ end
@@ -44,6 +44,8 @@ module Megam
44
44
  MEGAM_PREDEFCLOUDCOLLECTION = "Megam::PredefCloudCollection".freeze
45
45
  MEGAM_CLOUDTOOL = "Megam::CloudTool".freeze
46
46
  MEGAM_CLOUDTOOLCOLLECTION = "Megam::CloudToolCollection".freeze
47
+ MEGAM_CLOUDTOOLSETTING = "Megam::CloudToolSetting".freeze
48
+ MEGAM_CLOUDTOOLSETTINGCOLLECTION = "Megam::CloudToolSettingCollection".freeze
47
49
  MEGAM_CLOUDTEMPLATE = "Megam::CloudTemplate".freeze
48
50
  MEGAM_CLOUDTEMPLATECOLLECTION = "Megam::CloudTemplateCollection".freeze
49
51
  MEGAM_CLOUDINSTRUCTION = "Megam::CloudInstruction".freeze
@@ -163,6 +165,10 @@ module Megam
163
165
  Megam::CloudTool
164
166
  when MEGAM_CLOUDTOOLCOLLECTION
165
167
  Megam::CloudToolCollection
168
+ when MEGAM_CLOUDTOOLSETTING
169
+ Megam::CloudToolSetting
170
+ when MEGAM_CLOUDTOOLSETTINGCOLLECTION
171
+ Megam::CloudToolSettingCollection
166
172
  when MEGAM_CLOUDTEMPLATE
167
173
  Megam::CloudTemplate
168
174
  when MEGAM_CLOUDTEMPLATECOLLECTION
@@ -15,13 +15,14 @@
15
15
 
16
16
  require 'logger'
17
17
  require 'mixlib/log'
18
+ require 'megam/core/monologger'
18
19
 
19
20
  module Megam
20
21
  class Log
21
22
  extend Mixlib::Log
22
23
 
23
24
  # Force initialization of the primary log device (@logger)
24
- init
25
+ init(Megam::MonoLogger.new(STDOUT))
25
26
 
26
27
  class Formatter
27
28
  def self.show_time=(*args)
@@ -0,0 +1,89 @@
1
+ require 'logger'
2
+ require 'pp'
3
+
4
+ module Megam
5
+ #== MonoLogger
6
+ # A subclass of Ruby's stdlib Logger with all the mutex and logrotation stuff
7
+ # ripped out.
8
+ class MonoLogger < Logger
9
+ #
10
+ # === Synopsis
11
+ #
12
+ # Logger.new(name, shift_age = 7, shift_size = 1048576)
13
+ # Logger.new(name, shift_age = 'weekly')
14
+ #
15
+ # === Args
16
+ #
17
+ # +logdev+::
18
+ # The log device. This is a filename (String) or IO object (typically
19
+ # +STDOUT+, +STDERR+, or an open file).
20
+ # +shift_age+::
21
+ # Number of old log files to keep, *or* frequency of rotation (+daily+,
22
+ # +weekly+ or +monthly+).
23
+ # +shift_size+::
24
+ # Maximum logfile size (only applies when +shift_age+ is a number).
25
+ #
26
+ # === Description
27
+ #
28
+ # Create an instance.
29
+ #
30
+ def initialize(logdev)
31
+ @progname = nil
32
+ @level = DEBUG
33
+ @default_formatter = Formatter.new
34
+ @formatter = nil
35
+ @logdev = nil
36
+ if logdev
37
+ @logdev = LocklessLogDevice.new(logdev)
38
+ end
39
+ end
40
+
41
+ class LocklessLogDevice < LogDevice
42
+ def initialize(log = nil)
43
+ @dev = @filename = @shift_age = @shift_size = nil
44
+ if log.respond_to?(:write) and log.respond_to?(:close)
45
+ @dev = log
46
+ else
47
+ @dev = open_logfile(log)
48
+ @dev.sync = true
49
+ @filename = log
50
+ end
51
+ end
52
+
53
+ def write(message)
54
+ @dev.write(message)
55
+ rescue Exception => ignored
56
+ warn("log writing failed. #{ignored}")
57
+ end
58
+
59
+ def close
60
+ @dev.close rescue nil
61
+ end
62
+
63
+ private
64
+
65
+ def open_logfile(filename)
66
+ if (FileTest.exist?(filename))
67
+ open(filename, (File::WRONLY | File::APPEND))
68
+ else
69
+ create_logfile(filename)
70
+ end
71
+ end
72
+
73
+ def create_logfile(filename)
74
+ logdev = open(filename, (File::WRONLY | File::APPEND | File::CREAT))
75
+ logdev.sync = true
76
+ add_log_header(logdev)
77
+ logdev
78
+ end
79
+
80
+ def add_log_header(file)
81
+ file.write(
82
+ "# Logfile created on %s by %s\n" % [Time.now.to_s, Logger::ProgName]
83
+ )
84
+ end
85
+
86
+ end
87
+
88
+ end
89
+ end