hpcloud 2.0.0

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.
Files changed (238) hide show
  1. data/CHANGELOG +127 -0
  2. data/LICENSE +1 -0
  3. data/README.rdoc +59 -0
  4. data/bin/hpcloud +11 -0
  5. data/completion/hpcloud +372 -0
  6. data/lib/hpcloud/accounts.rb +288 -0
  7. data/lib/hpcloud/acl.rb +112 -0
  8. data/lib/hpcloud/acl_cmd.rb +81 -0
  9. data/lib/hpcloud/address_helper.rb +63 -0
  10. data/lib/hpcloud/addresses.rb +42 -0
  11. data/lib/hpcloud/auth_cache.rb +128 -0
  12. data/lib/hpcloud/base_helper.rb +56 -0
  13. data/lib/hpcloud/checker.rb +104 -0
  14. data/lib/hpcloud/cli.rb +170 -0
  15. data/lib/hpcloud/cli_status.rb +80 -0
  16. data/lib/hpcloud/columns.rb +46 -0
  17. data/lib/hpcloud/commands/account/catalog.rb +57 -0
  18. data/lib/hpcloud/commands/account/copy.rb +44 -0
  19. data/lib/hpcloud/commands/account/edit.rb +204 -0
  20. data/lib/hpcloud/commands/account/remove.rb +53 -0
  21. data/lib/hpcloud/commands/account/setup.rb +40 -0
  22. data/lib/hpcloud/commands/account/tenants.rb +43 -0
  23. data/lib/hpcloud/commands/account/use.rb +46 -0
  24. data/lib/hpcloud/commands/account/verify.rb +55 -0
  25. data/lib/hpcloud/commands/account.rb +67 -0
  26. data/lib/hpcloud/commands/acl/grant.rb +59 -0
  27. data/lib/hpcloud/commands/acl/revoke.rb +52 -0
  28. data/lib/hpcloud/commands/acl.rb +63 -0
  29. data/lib/hpcloud/commands/addresses/add.rb +67 -0
  30. data/lib/hpcloud/commands/addresses/associate.rb +63 -0
  31. data/lib/hpcloud/commands/addresses/disassociate.rb +58 -0
  32. data/lib/hpcloud/commands/addresses/remove.rb +57 -0
  33. data/lib/hpcloud/commands/addresses.rb +63 -0
  34. data/lib/hpcloud/commands/cdn_containers/add.rb +56 -0
  35. data/lib/hpcloud/commands/cdn_containers/get.rb +70 -0
  36. data/lib/hpcloud/commands/cdn_containers/location.rb +61 -0
  37. data/lib/hpcloud/commands/cdn_containers/remove.rb +55 -0
  38. data/lib/hpcloud/commands/cdn_containers/set.rb +63 -0
  39. data/lib/hpcloud/commands/cdn_containers.rb +72 -0
  40. data/lib/hpcloud/commands/complete.rb +60 -0
  41. data/lib/hpcloud/commands/config/set.rb +63 -0
  42. data/lib/hpcloud/commands/config.rb +46 -0
  43. data/lib/hpcloud/commands/containers/add.rb +67 -0
  44. data/lib/hpcloud/commands/containers/remove.rb +63 -0
  45. data/lib/hpcloud/commands/containers/sync.rb +59 -0
  46. data/lib/hpcloud/commands/containers.rb +24 -0
  47. data/lib/hpcloud/commands/copy.rb +78 -0
  48. data/lib/hpcloud/commands/dns/add.rb +56 -0
  49. data/lib/hpcloud/commands/dns/records/add.rb +50 -0
  50. data/lib/hpcloud/commands/dns/records/remove.rb +53 -0
  51. data/lib/hpcloud/commands/dns/records/update.rb +50 -0
  52. data/lib/hpcloud/commands/dns/records.rb +57 -0
  53. data/lib/hpcloud/commands/dns/remove.rb +57 -0
  54. data/lib/hpcloud/commands/dns/servers.rb +57 -0
  55. data/lib/hpcloud/commands/dns/update.rb +58 -0
  56. data/lib/hpcloud/commands/dns.rb +65 -0
  57. data/lib/hpcloud/commands/flavors.rb +59 -0
  58. data/lib/hpcloud/commands/get.rb +58 -0
  59. data/lib/hpcloud/commands/images/add.rb +54 -0
  60. data/lib/hpcloud/commands/images/metadata/add.rb +54 -0
  61. data/lib/hpcloud/commands/images/metadata/remove.rb +56 -0
  62. data/lib/hpcloud/commands/images/metadata.rb +54 -0
  63. data/lib/hpcloud/commands/images/remove.rb +58 -0
  64. data/lib/hpcloud/commands/images.rb +67 -0
  65. data/lib/hpcloud/commands/info.rb +41 -0
  66. data/lib/hpcloud/commands/keypairs/add.rb +73 -0
  67. data/lib/hpcloud/commands/keypairs/import.rb +62 -0
  68. data/lib/hpcloud/commands/keypairs/private/add.rb +44 -0
  69. data/lib/hpcloud/commands/keypairs/private/location.rb +50 -0
  70. data/lib/hpcloud/commands/keypairs/private/remove.rb +52 -0
  71. data/lib/hpcloud/commands/keypairs/private.rb +47 -0
  72. data/lib/hpcloud/commands/keypairs/public_key.rb +51 -0
  73. data/lib/hpcloud/commands/keypairs/remove.rb +58 -0
  74. data/lib/hpcloud/commands/keypairs.rb +64 -0
  75. data/lib/hpcloud/commands/lb/add.rb +92 -0
  76. data/lib/hpcloud/commands/lb/algorithms.rb +55 -0
  77. data/lib/hpcloud/commands/lb/limits.rb +55 -0
  78. data/lib/hpcloud/commands/lb/nodes/add.rb +47 -0
  79. data/lib/hpcloud/commands/lb/nodes/remove.rb +54 -0
  80. data/lib/hpcloud/commands/lb/nodes/update.rb +47 -0
  81. data/lib/hpcloud/commands/lb/nodes.rb +55 -0
  82. data/lib/hpcloud/commands/lb/protocols.rb +55 -0
  83. data/lib/hpcloud/commands/lb/remove.rb +54 -0
  84. data/lib/hpcloud/commands/lb/update.rb +46 -0
  85. data/lib/hpcloud/commands/lb/versions.rb +55 -0
  86. data/lib/hpcloud/commands/lb/virtualips.rb +51 -0
  87. data/lib/hpcloud/commands/lb.rb +73 -0
  88. data/lib/hpcloud/commands/list.rb +114 -0
  89. data/lib/hpcloud/commands/location.rb +56 -0
  90. data/lib/hpcloud/commands/metadata/set.rb +61 -0
  91. data/lib/hpcloud/commands/metadata.rb +60 -0
  92. data/lib/hpcloud/commands/migrate.rb +61 -0
  93. data/lib/hpcloud/commands/move.rb +73 -0
  94. data/lib/hpcloud/commands/networks/add.rb +55 -0
  95. data/lib/hpcloud/commands/networks/remove.rb +57 -0
  96. data/lib/hpcloud/commands/networks/update.rb +54 -0
  97. data/lib/hpcloud/commands/networks.rb +63 -0
  98. data/lib/hpcloud/commands/ports/add.rb +81 -0
  99. data/lib/hpcloud/commands/ports/remove.rb +57 -0
  100. data/lib/hpcloud/commands/ports/update.rb +67 -0
  101. data/lib/hpcloud/commands/ports.rb +63 -0
  102. data/lib/hpcloud/commands/remove.rb +75 -0
  103. data/lib/hpcloud/commands/routers/add.rb +67 -0
  104. data/lib/hpcloud/commands/routers/interface/add.rb +59 -0
  105. data/lib/hpcloud/commands/routers/interface/remove.rb +66 -0
  106. data/lib/hpcloud/commands/routers/remove.rb +54 -0
  107. data/lib/hpcloud/commands/routers/update.rb +66 -0
  108. data/lib/hpcloud/commands/routers.rb +66 -0
  109. data/lib/hpcloud/commands/securitygroups/add.rb +52 -0
  110. data/lib/hpcloud/commands/securitygroups/remove.rb +57 -0
  111. data/lib/hpcloud/commands/securitygroups/rules/add.rb +126 -0
  112. data/lib/hpcloud/commands/securitygroups/rules/remove.rb +51 -0
  113. data/lib/hpcloud/commands/securitygroups/rules.rb +56 -0
  114. data/lib/hpcloud/commands/securitygroups.rb +62 -0
  115. data/lib/hpcloud/commands/servers/add.rb +100 -0
  116. data/lib/hpcloud/commands/servers/console.rb +81 -0
  117. data/lib/hpcloud/commands/servers/limits.rb +46 -0
  118. data/lib/hpcloud/commands/servers/metadata/add.rb +56 -0
  119. data/lib/hpcloud/commands/servers/metadata/remove.rb +57 -0
  120. data/lib/hpcloud/commands/servers/metadata.rb +56 -0
  121. data/lib/hpcloud/commands/servers/password.rb +47 -0
  122. data/lib/hpcloud/commands/servers/ratelimits.rb +46 -0
  123. data/lib/hpcloud/commands/servers/reboot.rb +61 -0
  124. data/lib/hpcloud/commands/servers/rebuild.rb +59 -0
  125. data/lib/hpcloud/commands/servers/remove.rb +58 -0
  126. data/lib/hpcloud/commands/servers/securitygroups/add.rb +47 -0
  127. data/lib/hpcloud/commands/servers/securitygroups/remove.rb +47 -0
  128. data/lib/hpcloud/commands/servers/ssh.rb +92 -0
  129. data/lib/hpcloud/commands/servers.rb +92 -0
  130. data/lib/hpcloud/commands/snapshots/add.rb +62 -0
  131. data/lib/hpcloud/commands/snapshots/remove.rb +58 -0
  132. data/lib/hpcloud/commands/snapshots.rb +62 -0
  133. data/lib/hpcloud/commands/subnets/add.rb +78 -0
  134. data/lib/hpcloud/commands/subnets/remove.rb +57 -0
  135. data/lib/hpcloud/commands/subnets/update.rb +70 -0
  136. data/lib/hpcloud/commands/subnets.rb +63 -0
  137. data/lib/hpcloud/commands/tempurl.rb +62 -0
  138. data/lib/hpcloud/commands/volumes/add.rb +88 -0
  139. data/lib/hpcloud/commands/volumes/attach.rb +59 -0
  140. data/lib/hpcloud/commands/volumes/detach.rb +57 -0
  141. data/lib/hpcloud/commands/volumes/metadata/add.rb +54 -0
  142. data/lib/hpcloud/commands/volumes/metadata.rb +55 -0
  143. data/lib/hpcloud/commands/volumes/remove.rb +57 -0
  144. data/lib/hpcloud/commands/volumes/server.rb +64 -0
  145. data/lib/hpcloud/commands/volumes.rb +65 -0
  146. data/lib/hpcloud/config.rb +205 -0
  147. data/lib/hpcloud/connection.rb +290 -0
  148. data/lib/hpcloud/container_resource.rb +175 -0
  149. data/lib/hpcloud/database_helper.rb +70 -0
  150. data/lib/hpcloud/databases.rb +38 -0
  151. data/lib/hpcloud/dns_helper.rb +125 -0
  152. data/lib/hpcloud/dnss.rb +38 -0
  153. data/lib/hpcloud/error_response.rb +89 -0
  154. data/lib/hpcloud/exceptions/base.rb +35 -0
  155. data/lib/hpcloud/exceptions/general.rb +34 -0
  156. data/lib/hpcloud/exceptions/not_found.rb +34 -0
  157. data/lib/hpcloud/flavors.rb +40 -0
  158. data/lib/hpcloud/floating_ip_helper.rb +114 -0
  159. data/lib/hpcloud/floating_ips.rb +42 -0
  160. data/lib/hpcloud/fog_collection.rb +124 -0
  161. data/lib/hpcloud/image_helper.rb +111 -0
  162. data/lib/hpcloud/images.rb +43 -0
  163. data/lib/hpcloud/keypair_helper.rb +109 -0
  164. data/lib/hpcloud/keypairs.rb +41 -0
  165. data/lib/hpcloud/lb_algorithms.rb +37 -0
  166. data/lib/hpcloud/lb_limits.rb +37 -0
  167. data/lib/hpcloud/lb_nodes.rb +42 -0
  168. data/lib/hpcloud/lb_protocols.rb +37 -0
  169. data/lib/hpcloud/lb_versions.rb +37 -0
  170. data/lib/hpcloud/lb_virtualips.rb +42 -0
  171. data/lib/hpcloud/lbs.rb +38 -0
  172. data/lib/hpcloud/local_resource.rb +183 -0
  173. data/lib/hpcloud/log.rb +55 -0
  174. data/lib/hpcloud/metadata.rb +118 -0
  175. data/lib/hpcloud/monkey.rb +22 -0
  176. data/lib/hpcloud/network_helper.rb +88 -0
  177. data/lib/hpcloud/networks.rb +47 -0
  178. data/lib/hpcloud/object_store.rb +66 -0
  179. data/lib/hpcloud/port_helper.rb +150 -0
  180. data/lib/hpcloud/ports.rb +38 -0
  181. data/lib/hpcloud/progress.rb +53 -0
  182. data/lib/hpcloud/remote_resource.rb +512 -0
  183. data/lib/hpcloud/resource.rb +264 -0
  184. data/lib/hpcloud/resource_factory.rb +110 -0
  185. data/lib/hpcloud/routers.rb +56 -0
  186. data/lib/hpcloud/rule_helper.rb +70 -0
  187. data/lib/hpcloud/rules.rb +41 -0
  188. data/lib/hpcloud/security_group_helper.rb +59 -0
  189. data/lib/hpcloud/security_groups.rb +37 -0
  190. data/lib/hpcloud/server_helper.rb +318 -0
  191. data/lib/hpcloud/servers.rb +47 -0
  192. data/lib/hpcloud/shared_resource.rb +172 -0
  193. data/lib/hpcloud/snapshot_helper.rb +78 -0
  194. data/lib/hpcloud/snapshots.rb +38 -0
  195. data/lib/hpcloud/subnet_helper.rb +170 -0
  196. data/lib/hpcloud/subnets.rb +38 -0
  197. data/lib/hpcloud/tableizer.rb +77 -0
  198. data/lib/hpcloud/thor_ext/thor.rb +54 -0
  199. data/lib/hpcloud/time_parser.rb +47 -0
  200. data/lib/hpcloud/version.rb +27 -0
  201. data/lib/hpcloud/volume_attachment.rb +58 -0
  202. data/lib/hpcloud/volume_attachments.rb +58 -0
  203. data/lib/hpcloud/volume_helper.rb +119 -0
  204. data/lib/hpcloud/volumes.rb +38 -0
  205. data/lib/hpcloud.rb +112 -0
  206. data/lib/monkey/hp/lb.rb +188 -0
  207. data/lib/monkey/hp/models/lb/algorithm.rb +32 -0
  208. data/lib/monkey/hp/models/lb/algorithms.rb +46 -0
  209. data/lib/monkey/hp/models/lb/limit.rb +38 -0
  210. data/lib/monkey/hp/models/lb/limits.rb +50 -0
  211. data/lib/monkey/hp/models/lb/load_balancer.rb +70 -0
  212. data/lib/monkey/hp/models/lb/load_balancers.rb +46 -0
  213. data/lib/monkey/hp/models/lb/node.rb +69 -0
  214. data/lib/monkey/hp/models/lb/nodes.rb +47 -0
  215. data/lib/monkey/hp/models/lb/protocol.rb +33 -0
  216. data/lib/monkey/hp/models/lb/protocols.rb +46 -0
  217. data/lib/monkey/hp/models/lb/version.rb +34 -0
  218. data/lib/monkey/hp/models/lb/versions.rb +47 -0
  219. data/lib/monkey/hp/models/lb/virtual_ip.rb +37 -0
  220. data/lib/monkey/hp/models/lb/virtual_ips.rb +47 -0
  221. data/lib/monkey/hp/requests/lb/create_load_balancer.rb +64 -0
  222. data/lib/monkey/hp/requests/lb/create_load_balancer_node.rb +58 -0
  223. data/lib/monkey/hp/requests/lb/delete_load_balancer.rb +54 -0
  224. data/lib/monkey/hp/requests/lb/delete_load_balancer_node.rb +60 -0
  225. data/lib/monkey/hp/requests/lb/get_load_balancer.rb +88 -0
  226. data/lib/monkey/hp/requests/lb/get_load_balancer_node.rb +60 -0
  227. data/lib/monkey/hp/requests/lb/get_version.rb +68 -0
  228. data/lib/monkey/hp/requests/lb/get_virtual_ips.rb +48 -0
  229. data/lib/monkey/hp/requests/lb/list_algorithms.rb +51 -0
  230. data/lib/monkey/hp/requests/lb/list_limits.rb +60 -0
  231. data/lib/monkey/hp/requests/lb/list_load_balancer_nodes.rb +79 -0
  232. data/lib/monkey/hp/requests/lb/list_load_balancer_virtual_ips.rb +76 -0
  233. data/lib/monkey/hp/requests/lb/list_load_balancers.rb +69 -0
  234. data/lib/monkey/hp/requests/lb/list_protocols.rb +51 -0
  235. data/lib/monkey/hp/requests/lb/list_versions.rb +51 -0
  236. data/lib/monkey/hp/requests/lb/update_load_balancer.rb +46 -0
  237. data/lib/monkey/hp/requests/lb/update_load_balancer_node.rb +50 -0
  238. metadata +368 -0
@@ -0,0 +1,318 @@
1
+ # encoding: utf-8
2
+ #
3
+ # © Copyright 2013 Hewlett-Packard Development Company, L.P.
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in all
12
+ # copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ # SOFTWARE.
21
+
22
+ require 'hpcloud/metadata'
23
+ require "base64"
24
+
25
+ module HP
26
+ module Cloud
27
+ class ServerHelper < BaseHelper
28
+ attr_reader :private_key, :windows, :personality
29
+ attr_accessor :id, :name, :flavor, :image, :ips, :public_ip, :private_ip, :keyname, :security_groups, :security, :created, :state, :volume, :meta
30
+ attr_accessor :network_name, :networks, :tenantid, :region
31
+
32
+ def self.get_keys()
33
+ return [ "id", "name", "flavor", "image", "ips", "keyname", "security_groups", "created", "state" ]
34
+ end
35
+
36
+ def initialize(connection, foggy = nil)
37
+ super(connection, foggy)
38
+ @windows = false
39
+ @private_image = false
40
+ if foggy.nil?
41
+ @meta = HP::Cloud::Metadata.new(nil)
42
+ return
43
+ end
44
+ @id = foggy.id
45
+ @name = foggy.name
46
+ @flavor = foggy.flavor_id
47
+ @image = foggy.image_id
48
+ @public_ip = foggy.public_ip_address
49
+ @private_ip = foggy.private_ip_address
50
+ @keyname = foggy.key_name
51
+ unless foggy.security_groups.nil?
52
+ @security_groups = foggy.security_groups.map {|sg| sg["name"]}.join(', ')
53
+ end
54
+ @created = foggy.created_at
55
+ @state = foggy.state
56
+ @tenantid = foggy.tenant_id
57
+ @network_name = foggy.network_name
58
+ @networks = ""
59
+ @ips = ""
60
+ unless foggy.addresses.nil?
61
+ begin
62
+ foggy.addresses.keys.each { |x|
63
+ @networks += "," unless @networks.empty?
64
+ @networks += x.to_s
65
+ foggy.addresses[x].each { |y|
66
+ unless y["addr"].nil?
67
+ @ips += "," unless @ips.empty?
68
+ @ips += y["addr"].to_s
69
+ end
70
+ }
71
+ }
72
+ rescue Exception => e
73
+ warn "Error parsing addresses: " + e.to_s
74
+ end
75
+ end
76
+ @meta = HP::Cloud::Metadata.new(foggy.metadata)
77
+ end
78
+
79
+ def set_flavor(value)
80
+ flav = Flavors.new.get(value, false)
81
+ @flavor = flav.id
82
+ return true
83
+ end
84
+
85
+ def set_image(value)
86
+ return true if value.nil?
87
+ @windows = false
88
+ @private_image = false
89
+ image = Images.new().get(value, false)
90
+ unless image.is_valid?
91
+ set_error(image.cstatus)
92
+ return false
93
+ end
94
+ @windows = image.is_windows?
95
+ @private_image = image.is_private?
96
+ @image = image.id
97
+ return true
98
+ end
99
+
100
+ def set_volume(value)
101
+ return true if value.nil?
102
+ @windows = false # windows not supported right now
103
+ volume = Volumes.new().get(value, false)
104
+ unless volume.is_valid?
105
+ set_error(volume.cstatus)
106
+ return false
107
+ end
108
+ @volume = volume.id
109
+ return true
110
+ end
111
+
112
+ def set_keypair(value)
113
+ if value.nil?
114
+ return true
115
+ end
116
+ @keypair = Keypairs.new.get(value, false)
117
+ unless @keypair.is_valid?
118
+ set_error(@keypair.cstatus)
119
+ return false
120
+ end
121
+ @keyname = @keypair.name
122
+ if @windows
123
+ begin
124
+ @private_key = @keypair.private_read
125
+ rescue
126
+ end
127
+ end
128
+ return true
129
+ end
130
+
131
+ def set_security_groups(value)
132
+ if value.nil?
133
+ return true
134
+ end
135
+ if value.empty?
136
+ @security = []
137
+ @security_groups = ''
138
+ return true
139
+ end
140
+ begin
141
+ ray = eval("[\"" + value.gsub(',', '","') + "\"]")
142
+ if (ray.kind_of? Array)
143
+ @security = ray
144
+ @security_groups = value
145
+ return true
146
+ end
147
+ rescue SyntaxError => se
148
+ rescue NameError => ne
149
+ end
150
+ set_error("Invalid security group '#{value}' should be comma separated list", :incorrect_usage)
151
+ return false
152
+ end
153
+
154
+ def add_security_groups(value)
155
+ @connection.request(
156
+ :body => Fog::JSON.encode({ 'addSecurityGroup' => { 'name' => value}}),
157
+ :expects => 202,
158
+ :method => 'POST',
159
+ :path => "servers/#{@id}/action"
160
+ )
161
+ return true
162
+ end
163
+
164
+ def remove_security_groups(value)
165
+ @connection.request(
166
+ :body => Fog::JSON.encode({ 'removeSecurityGroup' => { 'name' => value}}),
167
+ :expects => 202,
168
+ :method => 'POST',
169
+ :path => "servers/#{@id}/action"
170
+ )
171
+ return true
172
+ end
173
+
174
+ def set_private_key(value)
175
+ if value.nil?
176
+ return true unless @windows
177
+ return true unless @private_key.nil?
178
+ set_error("You must specify the private key file if you want to create a windows instance.", :incorrect_usage)
179
+ return false
180
+ end
181
+ begin
182
+ @private_key = File.read(File.expand_path(value))
183
+ rescue Exception => e
184
+ set_error("Error reading private key file '#{value}': " + e.to_s, :incorrect_usage)
185
+ return false
186
+ end
187
+ return true
188
+ end
189
+
190
+ def set_network(value)
191
+ return true if value.nil?
192
+ network = Networks.new.get(value, false)
193
+ unless network.is_valid?
194
+ set_error(network.cstatus)
195
+ return false
196
+ end
197
+ @networks = [ network.id.to_s ]
198
+ return true
199
+ end
200
+
201
+ def set_user_data(filename)
202
+ return if filename.nil?
203
+ @user_data = File.new(filename).read
204
+ end
205
+
206
+ def set_personality(directory)
207
+ return if directory.nil?
208
+ @personality = []
209
+ Dir.glob("#{directory}/**/*").each { |file|
210
+ unless File.directory?(file)
211
+ rfile = file.gsub(directory, '')
212
+ hsh = { "path" => rfile,
213
+ "contents" => Base64.encode64(File.read(file)) }
214
+ @personality << hsh
215
+ end
216
+ }
217
+ end
218
+
219
+ def windows_password(retries=10)
220
+ begin
221
+ (0..retries).each { |x|
222
+ @epass = @fog.windows_password()
223
+ break unless @epass.empty?
224
+ sleep (x*10) unless retries == 1
225
+ }
226
+ return "Failed to get password" if @epass.empty?
227
+ begin
228
+ private_key = OpenSSL::PKey::RSA.new(@private_key)
229
+ from_base64 = Base64.decode64(@epass)
230
+ return private_key.private_decrypt(from_base64).strip
231
+ rescue Exception => error
232
+ end
233
+ return "Failed to decrypt: " + @epass
234
+ rescue Exception => e
235
+ return "Error getting windows password: " + e.to_s
236
+ end
237
+ end
238
+
239
+ def create_image(name, hash)
240
+ resp = @fog.create_image(name , hash)
241
+ if resp.nil?
242
+ set_error("Error creating image '#{name}'")
243
+ return nil
244
+ end
245
+ return resp
246
+ end
247
+
248
+ def save
249
+ if is_valid?
250
+ set_error(@meta.cstatus)
251
+ end
252
+ return false if is_valid? == false
253
+ acct = Accounts.new.get(Connection.instance.get_account)
254
+ @flavor = acct[:options][:preferred_flavor] if @flavor.nil?
255
+ @image = acct[:options][:preferred_image] if @image.nil? && @volume.nil?
256
+ if @image.nil? && @volume.nil?
257
+ set_error("You must specify either an image or a volume to create a server.", :incorrect_usage)
258
+ return false
259
+ end
260
+ if @fog.nil?
261
+ hsh = {:flavor_id => @flavor,
262
+ :name => @name,
263
+ :key_name => @keyname,
264
+ :security_groups => @security,
265
+ :metadata => @meta.hsh}
266
+ hsh[:image_id] = @image unless @image.nil?
267
+ hsh['networks'] = @networks unless @networks.nil?
268
+ hsh['user_data'] = @user_data unless @user_data.nil?
269
+ hsh['personality'] = @personality unless @personality.nil?
270
+ unless @volume.nil?
271
+ hsh[:block_device_mapping] = [{
272
+ 'volume_size' => '',
273
+ 'volume_id' => "#{@volume}",
274
+ 'delete_on_termination' => '0',
275
+ 'device_name' => 'vda'
276
+ }]
277
+ end
278
+ server = @connection.servers.create(hsh)
279
+ if server.nil?
280
+ set_error("Error creating server '#{@name}'")
281
+ return false
282
+ end
283
+ @id = server.id
284
+ @fog = server
285
+ begin
286
+ unless @keypair.nil?
287
+ @keypair.private_read if @keypair.private_key.nil?
288
+ @keypair.name = "#{@id}"
289
+ @keypair.private_add
290
+ end
291
+ rescue
292
+ end
293
+ return true
294
+ else
295
+ raise "Update not implemented"
296
+ end
297
+ end
298
+
299
+ def is_windows?
300
+ return @windows
301
+ end
302
+
303
+ def is_private_image?
304
+ return @private_image
305
+ end
306
+
307
+ def destroy
308
+ @fog.destroy unless @fog.nil?
309
+ begin
310
+ keypair = KeypairHelper.new(nil)
311
+ keypair.name = "#{@id}"
312
+ keypair.private_remove
313
+ rescue Exception => e
314
+ end
315
+ end
316
+ end
317
+ end
318
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+ #
3
+ # © Copyright 2013 Hewlett-Packard Development Company, L.P.
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in all
12
+ # copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ # SOFTWARE.
21
+
22
+ require 'hpcloud/fog_collection'
23
+ require 'hpcloud/server_helper'
24
+
25
+ module HP
26
+ module Cloud
27
+ class Servers < FogCollection
28
+ def initialize
29
+ super("server")
30
+ @items = @connection.compute.servers.all(:details=>true)
31
+ end
32
+
33
+ def create(item = nil)
34
+ return ServerHelper.new(@connection.compute, item)
35
+ end
36
+
37
+ def find_by_ip(ipaddy)
38
+ @items.each { |x|
39
+ if ipaddy == x.public_ip_address
40
+ return create(x)
41
+ end
42
+ }
43
+ return nil
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,172 @@
1
+ # encoding: utf-8
2
+ #
3
+ # © Copyright 2013 Hewlett-Packard Development Company, L.P.
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in all
12
+ # copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ # SOFTWARE.
21
+
22
+ module HP
23
+ module Cloud
24
+ class SharedResource < RemoteResource
25
+ attr_accessor :count
26
+
27
+ def parse()
28
+ @container = nil
29
+ @path = nil
30
+ if @fname.empty?
31
+ return
32
+ end
33
+ #
34
+ # Extract container given the expected input in the form:
35
+ # https://domain_and_port/version/tenant_id/container/object/name/with/slashes.txt
36
+ # where the container for that shared object is:
37
+ # https://domain_and_port/version/tenant_id/container
38
+ # and the path for the object is:
39
+ # object/name/with/slashes.txt
40
+ @container = @fname.match(/http[s]*:\/\/[^\/]*\/[^\/]*\/[^\/]*\/[^\/]*/).to_s
41
+ @path = @fname.gsub(@container, '')
42
+ @path = @path.gsub(/^\/*/, '')
43
+ @lname = @fname
44
+ @sname = @path
45
+ end
46
+
47
+ def head
48
+ return container_head()
49
+ end
50
+
51
+ def container_head
52
+ begin
53
+ return true unless @size.nil?
54
+ @size = 0
55
+ @directory = @storage.shared_directories.get(@container)
56
+ if @directory.nil?
57
+ @cstatus = CliStatus.new("Cannot find container '#{@container}'.", :not_found)
58
+ return false
59
+ end
60
+ @count = @directory.count.to_i
61
+ @size = @directory.bytes.to_i
62
+ rescue Fog::Storage::HP::NotFound => error
63
+ p self
64
+ @cstatus = CliStatus.new("Cannot find container '#{@container}'.", :not_found)
65
+ return false
66
+ rescue Excon::Errors::Forbidden => e
67
+ resp = ErrorResponse.new(e)
68
+ @cstatus = CliStatus.new(resp.error_string, :permission_denied)
69
+ return false
70
+ rescue Fog::HP::Errors::Forbidden => error
71
+ @cstatus = CliStatus.new("Permission denied trying to access '#{@fname}'.", :permission_denied)
72
+ return false
73
+ rescue Exception => error
74
+ @cstatus = CliStatus.new("Error reading '#{@fname}': " + error.to_s, :general_error)
75
+ return false
76
+ end
77
+ return true
78
+ end
79
+
80
+ def object_head
81
+ return container_head()
82
+ end
83
+
84
+ def get_size()
85
+ return 0 unless container_head()
86
+ return @size
87
+ end
88
+
89
+ #
90
+ # Add the capability to iterate through all the matching files
91
+ # for copy. Use different regular expressions for a directory
92
+ # where we want to recursively copy things vs a regular file
93
+ #
94
+ def foreach(&block)
95
+ return false unless container_head()
96
+ case @ftype
97
+ when :shared_directory
98
+ regex = "^" + path + ".*"
99
+ else
100
+ regex = "^" + path + '$'
101
+ end
102
+ @directory.files.each { |x|
103
+ name = x.key.to_s
104
+ if ! name.match(regex).nil?
105
+ yield ResourceFactory.create(@storage, @container + '/' + name)
106
+ end
107
+ }
108
+ end
109
+
110
+ def read
111
+ begin
112
+ @storage.get_shared_object(@fname) { |chunk, one, two|
113
+ yield chunk
114
+ }
115
+ rescue Fog::Storage::HP::NotFound => e
116
+ @cstatus = CliStatus.new("The specified object does not exist.", :not_found)
117
+ result = false
118
+ end
119
+ end
120
+
121
+ def copy_file(from)
122
+ result = true
123
+ if from.isLocal()
124
+ return false unless from.open
125
+ options = { 'Content-Type' => from.get_mime_type() }
126
+ @storage.put_shared_object(@container, @destination, {}, options) {
127
+ from.read().to_s
128
+ }
129
+ result = false unless from.close()
130
+ else
131
+ begin
132
+ @storage.put_shared_object(@container, @destination, nil, {'X-Copy-From' => "/#{from.container}/#{from.path}" })
133
+ rescue Fog::Storage::HP::NotFound => e
134
+ @cstatus = CliStatus.new("The specified object does not exist.", :not_found)
135
+ result = false
136
+ end
137
+ end
138
+ return result
139
+ end
140
+
141
+ def remove(force, at=nil, after=nil)
142
+ unless at.nil?
143
+ @cstatus = CliStatus.new("The at option is only supported for objects.", :incorrect_usage)
144
+ return false
145
+ end
146
+ unless after.nil?
147
+ @cstatus = CliStatus.new("The after option is only supported for objects.", :incorrect_usage)
148
+ return false
149
+ end
150
+
151
+ if @path.empty?
152
+ @cstatus = CliStatus.new("Removal of shared containers is not supported.", :not_supported)
153
+ return false
154
+ end
155
+ begin
156
+ return false unless container_head()
157
+ @storage.delete_shared_object(@container + '/' + @path)
158
+ rescue Fog::Storage::HP::NotFound => error
159
+ @cstatus = CliStatus.new("You don't have an object named '#{@fname}'.", :not_found)
160
+ return false
161
+ rescue Excon::Errors::Forbidden => error
162
+ @cstatus = CliStatus.new("Permission denied for '#{@fname}.", :permission_denied)
163
+ return false
164
+ rescue Exception => e
165
+ @cstatus = CliStatus.new("Exception removing '#{@fname}': " + e.to_s, :general_error)
166
+ return false
167
+ end
168
+ return true
169
+ end
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,78 @@
1
+ # encoding: utf-8
2
+ #
3
+ # © Copyright 2013 Hewlett-Packard Development Company, L.P.
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in all
12
+ # copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ # SOFTWARE.
21
+
22
+ module HP
23
+ module Cloud
24
+ class SnapshotHelper < BaseHelper
25
+ @@volumous = nil
26
+ attr_accessor :id, :name, :volume, :size, :created, :status, :description
27
+
28
+ def self.get_keys()
29
+ return [ "id", "name", "volume", "size", "created", "status", "description" ]
30
+ end
31
+
32
+ def initialize(connection, foggy = nil)
33
+ super(connection, foggy)
34
+ return if foggy.nil?
35
+ @id = foggy.id
36
+ @name = foggy.name
37
+ set_volume(foggy.volume_id)
38
+ @size = foggy.size
39
+ @created = foggy.created_at
40
+ @status = foggy.status
41
+ @description = foggy.description
42
+ end
43
+
44
+ def set_volume(volume_name_id)
45
+ @@volumous = Volumes.new if @@volumous.nil?
46
+ @volume_obj = @@volumous.get(volume_name_id.to_s)
47
+ if @volume_obj.is_valid? == false
48
+ return false
49
+ end
50
+ @volume = @volume_obj.name
51
+ return true
52
+ end
53
+
54
+ def save
55
+ return false if is_valid? == false
56
+ if @fog.nil?
57
+ hsh = {:name => @name,
58
+ :volume_id => @volume_obj.id,
59
+ :description => @description}
60
+ snapshot = @connection.block.snapshots.create(hsh)
61
+ if snapshot.nil?
62
+ set_error("Error creating snapshot '#{@name}'")
63
+ return false
64
+ end
65
+ @id = snapshot.id
66
+ @fog = snapshot
67
+ return true
68
+ else
69
+ raise "Update not implemented"
70
+ end
71
+ end
72
+
73
+ def self.clear_cache
74
+ @@volumous = nil
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+ #
3
+ # © Copyright 2013 Hewlett-Packard Development Company, L.P.
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in all
12
+ # copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ # SOFTWARE.
21
+
22
+ require 'hpcloud/fog_collection'
23
+ require 'hpcloud/snapshot_helper'
24
+
25
+ module HP
26
+ module Cloud
27
+ class Snapshots < FogCollection
28
+ def initialize()
29
+ super("snapshot")
30
+ @items = @connection.block.snapshots
31
+ end
32
+
33
+ def create(item = nil)
34
+ return SnapshotHelper.new(@connection, item)
35
+ end
36
+ end
37
+ end
38
+ end