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,512 @@
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 RemoteResource < Resource
25
+ attr_accessor :size, :type, :etag, :modified, :synckey, :syncto
26
+ attr_reader :headers
27
+
28
+ DEFAULT_STORAGE_MAX_SIZE = 5368709120
29
+ DEFAULT_STORAGE_SEGMENT_SIZE = 1073741824
30
+ DEFAULT_STORAGE_PAGE_LENGTH = 10000
31
+
32
+ @@storage_max_size = nil
33
+ @@storage_segment_size = nil
34
+ @@storage_chunk_size = nil
35
+
36
+ def parse
37
+ super
38
+
39
+ unless @fname.index('<').nil?
40
+ raise Exception.new("Valid object names do not contain the '<' character: #{@fname}")
41
+ end
42
+ unless @fname.index('>').nil?
43
+ raise Exception.new("Valid object names do not contain the '>' character: #{@fname}")
44
+ end
45
+ unless @fname.index('"').nil?
46
+ raise Exception.new("Valid object names do not contain the '\"' character: #{@fname}")
47
+ end
48
+ @lname = @fname
49
+ @sname = @path
50
+ end
51
+
52
+ def get_size()
53
+ @size = nil if @size == 0 # manifest file
54
+ return 0 unless object_head()
55
+ return @size
56
+ end
57
+
58
+ def head
59
+ return object_head()
60
+ end
61
+
62
+ def parse_container_headers(headers)
63
+ @headers = headers
64
+ @size = headers['X-Container-Bytes-Used']
65
+ @size = 0 if @size.nil?
66
+ @count = headers['X-Container-Object-Count']
67
+ @synckey = headers['X-Container-Sync-Key']
68
+ @syncto = headers['X-Container-Sync-To']
69
+ @writeacl = AclWriter.new(headers)
70
+ @readacl = AclReader.new(headers)
71
+ @public = @readacl.public
72
+ @readers = @readacl.users.join(",") unless @readacl.users.nil?
73
+ @writers = @writeacl.users.join(",") unless @writeacl.users.nil?
74
+ @versions = headers['X-Versions-Location']
75
+ @public_url = ""
76
+ begin
77
+ if @path.nil? || @path.empty?
78
+ @public_url = "#{@storage.url}/#{@container}"
79
+ else
80
+ @public_url = "#{@storage.url}/#{@container}/#{@path}"
81
+ end
82
+ rescue
83
+ end
84
+ @public_url = @public_url.gsub(/%2F/, '/') unless @public_url.nil?
85
+ return true
86
+ end
87
+
88
+ def container_head(force=false)
89
+ begin
90
+ unless force
91
+ return true unless @size.nil?
92
+ end
93
+ @size = 0
94
+ begin
95
+ data = @storage.head_container(@container)
96
+ rescue NoMethodError => e
97
+ data = @storage.directories.get(@container)
98
+ @count = data.files.size.to_s
99
+ return true
100
+ end
101
+ if data.nil? || data.headers.nil?
102
+ @cstatus = CliStatus.new("Cannot find container ':#{@container}'.", :not_found)
103
+ return false
104
+ end
105
+ @tainer_head = data.headers
106
+ return parse_container_headers(@tainer_head)
107
+ rescue Excon::Errors::Forbidden => e
108
+ resp = ErrorResponse.new(e)
109
+ @cstatus = CliStatus.new(resp.error_string, :permission_denied)
110
+ return false
111
+ rescue Fog::Storage::HP::NotFound => error
112
+ @cstatus = CliStatus.new("Cannot find container ':#{@container}'.", :not_found)
113
+ return false
114
+ rescue Fog::HP::Errors::Forbidden => error
115
+ @cstatus = CliStatus.new("Permission denied trying to access '#{@fname}'.", :permission_denied)
116
+ return false
117
+ rescue Exception => error
118
+ @cstatus = CliStatus.new("Error ch reading '#{@fname}': " + error.to_s, :general_error)
119
+ return false
120
+ end
121
+ end
122
+
123
+ def parse_object_headers(headers)
124
+ @headers = headers
125
+ @size = headers['Content-Length'].to_i
126
+ @size = 0 if @size.nil?
127
+ @modified = headers['Last-Modified']
128
+ @etag = headers['Etag']
129
+ @type = headers['Content-Type']
130
+ @public_url = ""
131
+ begin
132
+ @public_url = "#{@storage.url}/#{@container}/#{@path}"
133
+ @public_url = @public_url.gsub(/%2F/, '/') unless @public_url.nil?
134
+ rescue
135
+ end
136
+ return true
137
+ end
138
+
139
+ def object_head()
140
+ begin
141
+ return true unless @size.nil?
142
+ @size = 0
143
+ data = @storage.head_object(@container, @path)
144
+ if data.nil? || data.headers.nil?
145
+ @cstatus = CliStatus.new("Cannot find object ':#{@container}/#{@path}'.", :not_found)
146
+ return false
147
+ end
148
+ return parse_object_headers(data.headers)
149
+ rescue Fog::Storage::HP::NotFound => error
150
+ @cstatus = CliStatus.new("Cannot find object ':#{@container}/#{@path}'.", :not_found)
151
+ return false
152
+ rescue Excon::Errors::Forbidden => e
153
+ resp = ErrorResponse.new(e)
154
+ @cstatus = CliStatus.new(resp.error_string, :permission_denied)
155
+ return false
156
+ rescue Fog::HP::Errors::Forbidden => error
157
+ @cstatus = CliStatus.new("Permission denied trying to access '#{@fname}'.", :permission_denied)
158
+ return false
159
+ rescue Exception => error
160
+ @cstatus = CliStatus.new("Error oh reading '#{@fname}': " + error.to_s, :general_error)
161
+ return false
162
+ end
163
+ end
164
+
165
+ def printable_headers
166
+ printable_object_headers
167
+ end
168
+
169
+ def printable_object_headers
170
+ hsh = @headers.dup
171
+ hsh.delete('Accept-Ranges')
172
+ hsh.delete('Content-Length')
173
+ hsh.delete('Date')
174
+ hsh.delete('Etag')
175
+ hsh.delete('Last-Modified')
176
+ hsh.delete('X-Timestamp')
177
+ hsh.delete('X-Trans-Id')
178
+ hsh
179
+ end
180
+
181
+ def printable_container_headers
182
+ hsh = @headers.dup
183
+ hsh.delete('Accept-Ranges')
184
+ hsh.delete('Content-Length')
185
+ hsh.delete('Content-Type')
186
+ hsh.delete('Date')
187
+ hsh.delete('X-Timestamp')
188
+ hsh.delete('X-Trans-Id')
189
+ hsh
190
+ end
191
+
192
+ CONTAINER_META = [ 'Authorization', 'X-Auth-Token', 'X-Container-Read', 'X-Container-Write', 'X-Container-Sync-To', 'X-Container-Sync-Key', 'X-Container-Meta-Web-Index', 'X-Container-Meta-Web-Error', 'X-Container-Meta-Web-Listings', 'X-Container-Meta-Web-Listings-CSS', 'X-Versions-Location' ]
193
+ OBJECT_META = [ 'Content-Type', 'Content-Disposition', 'X-Delete-At', 'X-Delete-After', 'X-Object-Manifest' ]
194
+
195
+ def valid_metadata_key?(key)
196
+ return true if key.start_with?("X-Object-Meta-")
197
+ return true unless OBJECT_META.index(key).nil?
198
+ return false
199
+ end
200
+
201
+ def set_metadata(key, value)
202
+ hsh = printable_headers()
203
+ hsh[key] = value
204
+ @storage.post_object(@container, @path, hsh)
205
+ end
206
+
207
+ def self.VALID_OBJECT_META
208
+ " * " + OBJECT_META.join("\n * ") + "\n"
209
+ end
210
+
211
+ def self.VALID_CONTAINER_META
212
+ " * " + CONTAINER_META.join("\n * ") + "\n"
213
+ end
214
+
215
+ def self.VALID_OBJECT_META
216
+ " * " + OBJECT_META.join("\n * ") + "\n"
217
+ end
218
+
219
+ def open(output=false, siz=0)
220
+ return object_head()
221
+ end
222
+
223
+ def read
224
+ begin
225
+ yielded_something = false
226
+ @storage.get_object(@container, @path) { |chunk, remain, tot|
227
+ yield chunk
228
+ yielded_something = true
229
+ }
230
+ yield '' unless yielded_something
231
+ rescue Fog::Storage::HP::NotFound => e
232
+ @cstatus = CliStatus.new("The specified object does not exist.", :not_found)
233
+ result = false
234
+ end
235
+ end
236
+
237
+ def write(chunk)
238
+ if @write_io.nil?
239
+ begin
240
+ @read_io, @write_io = IO.pipe
241
+ @write_thread = Thread.new {
242
+ @storage.put_object(@container, @destination, {}, @options) {
243
+ @read_io.read
244
+ }
245
+ }
246
+ rescue Exception => e
247
+ @cstatus = CliStatus.new("Error writing object creating thread.")
248
+ return false
249
+ end
250
+ end
251
+ begin
252
+ @write_io.write(chunk)
253
+ rescue Exception => e
254
+ @cstatus = CliStatus.new("Error writing object.")
255
+ return false
256
+ end
257
+ return true
258
+ end
259
+
260
+ def close
261
+ @write_io.close unless @write_io.nil?
262
+ @write_io = nil
263
+ @write_thread.join unless @write_thread.nil?
264
+ @write_thread = nil
265
+ @read_io.close unless @read_io.nil?
266
+ @read_io = nil
267
+ @pbar.increment(@lastread) unless @pbar.nil?
268
+ @pbar.finish unless @pbar.nil?
269
+ @lastread = 0
270
+ @pbar = nil
271
+
272
+ return true
273
+ end
274
+
275
+ def cdn_public_url
276
+ @cdn_public_url = @storage.directories.get(@container).files.get(@path).cdn_public_url
277
+ @cdn_public_url = @cdn_public_url.gsub(/%2F/, '/') unless @cdn_public_url.nil?
278
+ end
279
+
280
+ def cdn_public_ssl_url
281
+ @cdn_public_ssl_url = @storage.directories.get(@container).files.get(@path).cdn_public_ssl_url
282
+ @cdn_public_ssl_url = @cdn_public_ssl_url.gsub(/%2F/, '/') unless @cdn_public_ssl_url.nil?
283
+ end
284
+
285
+ def valid_source()
286
+ return container_head()
287
+ end
288
+
289
+ def valid_destination(source)
290
+ return false unless container_head()
291
+ if ((source.isMulti() == true) && (isDirectory() == false))
292
+ @cstatus = CliStatus.new("Invalid target for directory/multi-file copy '#{@fname}'.", :incorrect_usage)
293
+ return false
294
+ end
295
+ return true
296
+ end
297
+
298
+ def set_destination(name)
299
+ return false unless container_head()
300
+ if (@path.empty?)
301
+ @destination = name
302
+ else
303
+ if isObject()
304
+ @destination = @path
305
+ else
306
+ @destination = @path
307
+ @destination += '/' unless @destination.end_with?('/')
308
+ @destination += name
309
+ end
310
+ end
311
+ return true
312
+ end
313
+
314
+ def copy_file(from)
315
+ result = true
316
+ return true if from.fname.end_with?("/") # application/directory
317
+ return false if (from.open() == false)
318
+ if from.isLocal()
319
+ if @@storage_segment_size.nil?
320
+ config = Config.new
321
+ @@storage_max_size = config.get_i(:storage_max_size, DEFAULT_STORAGE_MAX_SIZE)
322
+ @@storage_segment_size = config.get_i(:storage_segment_size, DEFAULT_STORAGE_SEGMENT_SIZE)
323
+ @@storage_chunk_size = config.get_i(:storage_chunk_size, Excon::DEFAULT_CHUNK_SIZE)
324
+ @@storage_chunk_size = @@storage_segment_size if @@storage_segment_size < @@storage_chunk_size
325
+ end
326
+ @options = { 'Content-Type' => from.get_mime_type() }
327
+ count = 0
328
+ segment = i=10000000001
329
+ total = from.get_size()
330
+ if total > @@storage_max_size
331
+ prefix = @destination + '.segment.'
332
+ begin
333
+ bytes_read = 0
334
+ bytes_to_read = total - count
335
+ bytes_to_read = @@storage_segment_size if bytes_to_read > @@storage_segment_size
336
+ tmppath = prefix + segment.to_s[1..10]
337
+ already_exists = false
338
+ begin
339
+ if @restart == true
340
+ response = @storage.head_object(@container, tmppath)
341
+ segsiz = response.headers['Content-Length'].to_i
342
+ if segsiz == bytes_to_read
343
+ already_exists = true
344
+ end
345
+ end
346
+ rescue
347
+ end
348
+ if already_exists
349
+ # skip the bytes
350
+ while bytes_to_read > 0 do
351
+ body = from.read(@@storage_chunk_size)
352
+ bytes_read += body.length
353
+ bytes_to_read -= body.length
354
+ end
355
+ else
356
+ chunk_size = @@storage_chunk_size
357
+ @storage.put_object(@container, tmppath, nil, @options) {
358
+ chunk_size = bytes_to_read if bytes_to_read < chunk_size
359
+ body = from.read(chunk_size)
360
+ bytes_read += body.length
361
+ bytes_to_read -= body.length
362
+ body
363
+ }
364
+ end
365
+ count = count + bytes_read
366
+ segment = segment + 1
367
+ end until count >= total
368
+ manifest = @destination
369
+ @options['x-object-manifest'] = @container + '/' + prefix
370
+ @storage.put_object(@container, manifest, nil, @options)
371
+ else
372
+ @storage.put_object(@container, @destination, nil, @options) {
373
+ from.read(@@storage_chunk_size)
374
+ }
375
+ end
376
+ else
377
+ begin
378
+ if from.has_same_account(@storage)
379
+ @storage.put_object(@container, @destination, nil, {'X-Copy-From' => "/#{from.container}/#{from.path}" })
380
+ else
381
+ @lastread = 0
382
+ siz = from.get_size()
383
+ @pbar = Progress.new(@destination, from.get_size())
384
+ @options = { 'Content-Type' => from.get_mime_type() }
385
+ from.read() { |chunk|
386
+ if ! write(chunk)
387
+ result = false
388
+ break
389
+ end
390
+ @pbar.increment(@lastread) unless @pbar.nil?
391
+ @lastread = chunk.length
392
+ }
393
+ end
394
+ rescue Fog::Storage::HP::NotFound => e
395
+ @cstatus = CliStatus.new("The specified object does not exist.", :not_found)
396
+ result = false
397
+ end
398
+ end
399
+ result = false if ! from.close()
400
+ result = false unless close()
401
+ return result
402
+ end
403
+
404
+ def foreach(&block)
405
+ case @ftype
406
+ when :container_directory
407
+ regex = "^" + path + ".*"
408
+ when :container
409
+ regex = ".*"
410
+ else
411
+ regex = "^" + path + '$'
412
+ end
413
+ if @@limit.nil?
414
+ @@limit = Config.new.get_i(:storage_page_length, DEFAULT_STORAGE_PAGE_LENGTH)
415
+ end
416
+ total = 0
417
+ count = 0
418
+ marker = nil
419
+ begin
420
+ options = { :limit => @@limit, :marker => marker }
421
+ begin
422
+ result = @storage.get_container(@container, options)
423
+ rescue NoMethodError => e
424
+ @storage.directories.get(@container).files.each { |file|
425
+ yield ResourceFactory.create(@storage, ':' + @container + '/' + file.key)
426
+ }
427
+ return
428
+ end
429
+ total = result.headers['X-Container-Object-Count'].to_i
430
+ lode = result.body.length
431
+ count += lode
432
+ result.body.each { |x|
433
+ name = x['name']
434
+ if ! name.match(regex).nil?
435
+ res = ResourceFactory.create(@storage, ':' + @container + '/' + name)
436
+ res.path = name
437
+ res.etag = x['hash']
438
+ res.modified = x['last_modified']
439
+ res.size = x['bytes']
440
+ res.type = x['content_type']
441
+ yield res
442
+ marker = name
443
+ end
444
+ }
445
+ break if lode < @@limit
446
+ end until count >= total
447
+ end
448
+
449
+ def get_destination()
450
+ return ':' + @container.to_s + '/' + @destination.to_s
451
+ end
452
+
453
+ def remove(force, at=nil, after=nil)
454
+ begin
455
+ return false unless container_head()
456
+ if at.nil?
457
+ if after.nil?
458
+ @storage.delete_object(@container, @path)
459
+ else
460
+ hsh = { 'X-Delete-After' => after}
461
+ @storage.post_object(@container, @path, hsh)
462
+ end
463
+ else
464
+ hsh = { 'X-Delete-At' => at}
465
+ @storage.post_object(@container, @path, hsh)
466
+ end
467
+ rescue Fog::Storage::HP::NotFound => error
468
+ @cstatus = CliStatus.new("You don't have an object named '#{@fname}'.", :not_found)
469
+ return false
470
+ rescue Excon::Errors::Forbidden => error
471
+ @cstatus = CliStatus.new("Permission denied for '#{@fname}.", :permission_denied)
472
+ return false
473
+ rescue Exception => e
474
+ @cstatus = CliStatus.new("Exception removing '#{@fname}': " + e.to_s, :general_error)
475
+ return false
476
+ end
477
+ return true
478
+ end
479
+
480
+ def tempurl(period, for_update=false)
481
+ begin
482
+ period = 172800 if period.nil?
483
+ return nil unless object_head()
484
+
485
+ return @storage.get_object_temp_url(@container, @path, period, "PUT") if (for_update)
486
+ return @storage.get_object_temp_url(@container, @path, period, "GET")
487
+ rescue Excon::Errors::Forbidden => error
488
+ @cstatus = CliStatus.new("Permission denied for '#{@fname}.", :permission_denied)
489
+ return nil
490
+ rescue Exception => e
491
+ @cstatus = CliStatus.new("Exception getting temporary URL for '#{@fname}': " + e.to_s, :general_error)
492
+ return nil
493
+ end
494
+ return nil
495
+ end
496
+
497
+ def grant(acl)
498
+ @cstatus = CliStatus.new("ACLs are only supported on containers (e.g. :container).", :not_supported)
499
+ return false
500
+ end
501
+
502
+ def revoke(acl)
503
+ @cstatus = CliStatus.new("ACLs are only supported on containers (e.g. :container).", :not_supported)
504
+ return false
505
+ end
506
+
507
+ def has_same_account(storage)
508
+ return storage == @storage
509
+ end
510
+ end
511
+ end
512
+ end