hyperb 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +12 -0
  3. data/.gitignore +12 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +41 -0
  6. data/Dockerfile +7 -0
  7. data/Gemfile +16 -0
  8. data/LICENSE.txt +21 -0
  9. data/Makefile +16 -0
  10. data/README.md +188 -0
  11. data/Rakefile +24 -0
  12. data/circle.yml +13 -0
  13. data/examples/README.md +367 -0
  14. data/examples/auth-gcr-registry.md +19 -0
  15. data/examples/compose.md +75 -0
  16. data/examples/handling-errors.md +54 -0
  17. data/examples/streaming-logs.md +28 -0
  18. data/examples/streaming-stats.md +25 -0
  19. data/hyperb.gemspec +30 -0
  20. data/lib/hyperb.rb +4 -0
  21. data/lib/hyperb/api.rb +22 -0
  22. data/lib/hyperb/auth_object.rb +42 -0
  23. data/lib/hyperb/client.rb +32 -0
  24. data/lib/hyperb/compose/compose.rb +116 -0
  25. data/lib/hyperb/containers/container.rb +27 -0
  26. data/lib/hyperb/containers/containers.rb +251 -0
  27. data/lib/hyperb/error.rb +44 -0
  28. data/lib/hyperb/hyper_version.rb +12 -0
  29. data/lib/hyperb/images/image.rb +13 -0
  30. data/lib/hyperb/images/images.rb +108 -0
  31. data/lib/hyperb/network/fips.rb +102 -0
  32. data/lib/hyperb/request.rb +129 -0
  33. data/lib/hyperb/services/services.rb +59 -0
  34. data/lib/hyperb/snapshots/snapshot.rb +12 -0
  35. data/lib/hyperb/snapshots/snapshots.rb +39 -0
  36. data/lib/hyperb/utils.rb +39 -0
  37. data/lib/hyperb/version.rb +3 -0
  38. data/lib/hyperb/volumes/volume.rb +16 -0
  39. data/lib/hyperb/volumes/volumes.rb +67 -0
  40. data/spec/auth_object_spec.rb +45 -0
  41. data/spec/client_spec.rb +27 -0
  42. data/spec/compose_spec.rb +145 -0
  43. data/spec/container_spec.rb +25 -0
  44. data/spec/containers_spec.rb +442 -0
  45. data/spec/create_snapshot.rb +30 -0
  46. data/spec/error_spec.rb +18 -0
  47. data/spec/fixtures/auth_obj.json +12 -0
  48. data/spec/fixtures/compose_rm.json +1 -0
  49. data/spec/fixtures/compose_up.json +1 -0
  50. data/spec/fixtures/container_stats.json +78 -0
  51. data/spec/fixtures/containers.json +160 -0
  52. data/spec/fixtures/create_container.json +4 -0
  53. data/spec/fixtures/create_image.json +1 -0
  54. data/spec/fixtures/create_service.json +35 -0
  55. data/spec/fixtures/create_snapshot.json +8 -0
  56. data/spec/fixtures/fip_allocate.json +7 -0
  57. data/spec/fixtures/fips_ls.json +14 -0
  58. data/spec/fixtures/images.json +32 -0
  59. data/spec/fixtures/inspect_container.json +159 -0
  60. data/spec/fixtures/inspect_image.json +89 -0
  61. data/spec/fixtures/inspect_volume.json +11 -0
  62. data/spec/fixtures/remove_container.json +1 -0
  63. data/spec/fixtures/remove_image.json +5 -0
  64. data/spec/fixtures/volumes.json +13 -0
  65. data/spec/helper.rb +36 -0
  66. data/spec/image_spec.rb +17 -0
  67. data/spec/images_spec.rb +133 -0
  68. data/spec/network_spec.rb +106 -0
  69. data/spec/request_spec.rb +41 -0
  70. data/spec/services_spec.rb +193 -0
  71. data/spec/version_spec.rb +7 -0
  72. data/spec/volumes_spec.rb +88 -0
  73. metadata +74 -3
@@ -0,0 +1,27 @@
1
+ require 'hyperb/utils'
2
+
3
+ module Hyperb
4
+ # container object
5
+ class Container
6
+ include Hyperb::Utils
7
+ attr_accessor :id, :names, :image, :imageid,
8
+ :created, :command, :ports, :labels,
9
+ :sizerw, :sizerootfs, :state, :hostconfig,
10
+ :networksettings
11
+
12
+ def initialize(attrs = {})
13
+ attrs.each do |att, value|
14
+ value = downcase_symbolize(value) if value.is_a?(Hash)
15
+ instance_variable_set("@#{att.downcase.to_sym}", value)
16
+ end
17
+ end
18
+
19
+ def name
20
+ names.first
21
+ end
22
+
23
+ def running?
24
+ state == 'running'
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,251 @@
1
+ require 'hyperb/request'
2
+ require 'hyperb/containers/container'
3
+ require 'hyperb/utils'
4
+ require 'json'
5
+ require 'uri'
6
+ require 'base64'
7
+
8
+ module Hyperb
9
+ # wrapper for containers api
10
+ module Containers
11
+ include Hyperb::Utils
12
+
13
+ # list existing containers
14
+ #
15
+ # @see https://docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/list.html
16
+ #
17
+ # @raise [Hyperb::Error::Unauthorized] raised when credentials are not valid.
18
+ # @raise [Hyperb::Error::InternalServerError] raised when 5xx is returned from hyper.
19
+ #
20
+ # @return [Hyperb::Container] Array of Hyperb::Container.
21
+ #
22
+ # @param params [Hash] A customizable set of params.
23
+ #
24
+ # @option params [Boolean] :all show all containers, false by default
25
+ # @option params [Boolean] :size show containers size
26
+ # @option params [String] :limit show `limit` last created containers.
27
+ # @option params [String] :since show only containers created since Id.
28
+ # @option params [String] :before only containers created before Id.
29
+ # TODO: @option params [Hash] :filters JSON encoded value of the filters.
30
+ def containers(params = {})
31
+ path = '/containers/json'
32
+ query = {}
33
+ query.merge!(params)
34
+ response = JSON.parse(Hyperb::Request.new(self, path, query, 'get').perform)
35
+ response.map { |container| Hyperb::Container.new(container) }
36
+ end
37
+
38
+ # stop the container id
39
+ #
40
+ # @see https://docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/stop.html
41
+ #
42
+ # @raise [Hyperb::Error::Unauthorized] raised when credentials are not valid.
43
+ # @raise [Hyperb::Error::NotFound] raised when container can't be found.
44
+ # @raise [Hyperb::Error::Conflict] raised when container is running and can't be removed.
45
+ # @raise [Hyperb::Error::InternalServerError] raised when a 5xx is returned.
46
+ #
47
+ # @param params [Hash] A customizable set of params.
48
+ #
49
+ # @option params [Boolean] :t number of seconds to wait before killing the container.
50
+ def stop_container(params = {})
51
+ raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'id')
52
+ path = '/containers/' + params[:id] + '/stop'
53
+ query = {}
54
+ query[:t] = params[:t] if params.key?(:t)
55
+ Hyperb::Request.new(self, path, query, 'post').perform
56
+ end
57
+
58
+ # remove the container id
59
+ #
60
+ # @see https://docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/delete.html
61
+ #
62
+ # @raise [Hyperb::Error::Unauthorized] raised when credentials are not valid.
63
+ # @raise [Hyperb::Error::NotFound] raised when container can't be found.
64
+ # @raise [Hyperb::Error::Conflict] raised when container is running and can't be removed.
65
+ # @raise [Hyperb::Error::InternalServerError] raised when a 5xx is returned.
66
+ #
67
+ # @return [Hash] downcased symbolized json response.
68
+ #
69
+ # @param params [Hash] A customizable set of params.
70
+ #
71
+ # @option params [Boolean] :v remove volumes attached. default false
72
+ # @option params [Boolean] :force force remove. default false
73
+ def remove_container(params = {})
74
+ raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'id')
75
+ path = '/containers/' + params[:id]
76
+ query = {}
77
+ query[:v] = params[:v] if params.key?(:v)
78
+ query[:force] = params[:force] if params.key?(:force)
79
+ response = JSON.parse(Hyperb::Request.new(self, path, query, 'delete').perform)
80
+ downcase_symbolize(response)
81
+ end
82
+
83
+ # create a container
84
+ #
85
+ # @see https://docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/create.html
86
+ #
87
+ # @raise [Hyperb::Error::Unauthorized] raised when credentials are not valid.
88
+ # @raise [Hyperb::Error::Conflict] raised container with the same name is already created.
89
+ # @raise [Hyperb::Error::InternalServerError] raised when a 5xx is returned
90
+ #
91
+ # @return [Hash] hash containing downcased symbolized json response.
92
+ #
93
+ # @param params [Hash] A customizable set of params.
94
+ #
95
+ # @option params [String] :name container name
96
+ # @option params [String] :image image to be used
97
+ # @option params [String] :hostname container hostname
98
+ # @option params [String] :entrypoint container entrypoint
99
+ # @option params [String] :cmd container command
100
+ # @option params [String] :user add user to container
101
+ # @option params [String] :workingdir working directory for commands to run in.
102
+ # @option params [Array] :mounts array of strings with mount directories.
103
+ # @option params [String] :networkmode network mode, ie 'bridge'.
104
+ # @option params [Hash] :exposedports ports to expose.
105
+ #
106
+ # @option params [Hash] :labels hash containing key: value
107
+ # @option params labels [String] :sh_hyper_instancetype container size: s1, s2, s3 ...
108
+ def create_container(params = {})
109
+ raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'image')
110
+ path = '/containers/create'
111
+ query = {}
112
+
113
+ # set a default size, otherwise container can't be started
114
+ body = { labels: { sh_hyper_instancetype: 's1' } }
115
+ query[:name] = params[:name] if params.key?(:name)
116
+ params.delete(:name)
117
+ body.merge!(params)
118
+
119
+ response = JSON.parse(Hyperb::Request.new(self, path, query, 'post', body).perform)
120
+ downcase_symbolize(response)
121
+ end
122
+
123
+ # inspect a container
124
+ #
125
+ # @see https://docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/inspect.html
126
+ #
127
+ # @raise [Hyperb::Error::Unauthorized] raised when credentials are not valid.
128
+ # @raise [Hyperb::Error::NotFound] raised when the container can't be found.
129
+ # @raise [Hyperb::Error::InternalServerError] raised when 5xx is returned.
130
+ #
131
+ # @return [Hash] Array of downcased symbolized json response.
132
+ #
133
+ # @param params [Hash] A customizable set of params.
134
+ #
135
+ # @option params [String] :id container's name or id
136
+ # @option params [String] :size include container's size on response
137
+ def inspect_container(params = {})
138
+ raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'id')
139
+ path = '/containers/' + params[:id] + '/json'
140
+ query = {}
141
+ query[:size] = params[:size] if params.key?(:size)
142
+ response = JSON.parse(Hyperb::Request.new(self, path, query, 'get').perform)
143
+ downcase_symbolize(response)
144
+ end
145
+
146
+ # start a container
147
+ #
148
+ # @see https://docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/start.html
149
+ #
150
+ # @raise [Hyperb::Error::Unauthorized] raised when credentials are not valid.
151
+ # @raise [Hyperb::Error::NotFound] raised when the container can't be found.
152
+ # @raise [Hyperb::Error::BadRequest] raised when request is invalid.
153
+ # @raise [Hyperb::Error::InternalServerError] raised when a 5xx is returned.
154
+ #
155
+ # @param params [Hash] A customizable set of params.
156
+ # @option params [String] :id container's name or id
157
+ def start_container(params = {})
158
+ raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'id')
159
+ path = '/containers/' + params[:id] + '/start'
160
+ Hyperb::Request.new(self, path, {}, 'post').perform
161
+ end
162
+
163
+ # container logs
164
+ #
165
+ # @see https://docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/logs.html
166
+ #
167
+ # @raise [Hyperb::Error::Unauthorized] raised when credentials are not valid.
168
+ # @raise [Hyperb::Error::NotFound] raised when the container can't be found.
169
+ # @raise [Hyperb::Error::BadRequest] raised when request is invalid.
170
+ # @raise [Hyperb::Error::InternalServerError] raised when a 5xx is returned.
171
+ #
172
+ # @return [HTTP::Response::Body] a streamable http response body object
173
+ #
174
+ # @param params [Hash] A customizable set of params.
175
+ # @option params [String] :id container's name or id
176
+ # @option params [String] :follow stream output
177
+ # @option params [String] :stderr stream stderr
178
+ # @option params [String] :stdout stream stdout
179
+ # @option params [String] :since stream outputs since id
180
+ # @option params [String] :timestamps include timestamps on stdouts, default false
181
+ # @option params [String] :tail tail number
182
+ def container_logs(params = {})
183
+ raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'id')
184
+ path = '/containers/' + params[:id] + '/logs'
185
+ query = {}
186
+ params.delete(:id)
187
+ query.merge!(params)
188
+ Hyperb::Request.new(self, path, query, 'get').perform
189
+ end
190
+
191
+ # container stats
192
+ #
193
+ # @see https://docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/logs.html
194
+ #
195
+ # @raise [Hyperb::Error::Unauthorized] raised when credentials are not valid.
196
+ # @raise [Hyperb::Error::NotFound] raised when the container can't be found.
197
+ # @raise [Hyperb::Error::BadRequest] raised when request is invalid.
198
+ # @raise [Hyperb::Error::InternalServerError] raised when 5xx is returned.
199
+ #
200
+ # @return [HTTP::Response::Body] a streamable http response body object
201
+ #
202
+ # @param params [Hash] A customizable set of params.
203
+ # @option params [String] :id container's name or id
204
+ # @option params [String] :stream stream output
205
+ def container_stats(params = {})
206
+ raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'id')
207
+ path = '/containers/' + params[:id] + '/stats'
208
+ query = {}
209
+ query[:stream] = params[:stream] if params.key?(:stream)
210
+ Hyperb::Request.new(self, path, query, 'get').perform
211
+ end
212
+
213
+ # kill a container
214
+ #
215
+ # @see https://docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/kill.html
216
+ #
217
+ # @raise [Hyperb::Error::Unauthorized] raised when credentials are not valid.
218
+ # @raise [Hyperb::Error::NotFound] raised when the container can't be found.
219
+ # @raise [Hyperb::Error::InternalServerError] raised when 5xx is returned.
220
+ #
221
+ # @param params [Hash] A customizable set of params.
222
+ # @option params [String] :id container's name or id
223
+ # @option params [String] :signal stream output
224
+ def kill_container(params = {})
225
+ raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'id')
226
+ path = '/containers/' + params[:id] + '/kill'
227
+ query = {}
228
+ query[:signal] = params[:signal] if params.key?(:signal)
229
+ Hyperb::Request.new(self, path, query, 'post').perform
230
+ end
231
+
232
+ # rename a container
233
+ #
234
+ # @see https://docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/rename.html
235
+ #
236
+ # @raise [Hyperb::Error::Unauthorized] raised when credentials are not valid.
237
+ # @raise [Hyperb::Error::NotFound] raised when the container can't be found.
238
+ # @raise [Hyperb::Error::InternalServerError] raised when 5xx is returned.
239
+ #
240
+ # @param params [Hash] A customizable set of params.
241
+ # @option params [String] :id new name
242
+ # @option params [String] :name new name
243
+ def rename_container(params = {})
244
+ raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'name', 'id')
245
+ path = '/containers/' + params[:id] + '/rename'
246
+ query = {}
247
+ query[:name] = params[:name] if params.key?(:name)
248
+ Hyperb::Request.new(self, path, query, 'post').perform
249
+ end
250
+ end
251
+ end
@@ -0,0 +1,44 @@
1
+ module Hyperb
2
+ # representation of Hyper errors
3
+ class Error < StandardError
4
+ attr_reader :code, :msg
5
+
6
+ # 4xx HTTP status code
7
+ ClientError = Class.new(self)
8
+
9
+ # 5xx HTTP status code
10
+ ServerError = Class.new(self)
11
+
12
+ # hyper uses code 304 as a server representation for some operations
13
+ NotModified = Class.new(ClientError)
14
+
15
+ # code 400
16
+ BadRequest = Class.new(ClientError)
17
+
18
+ # code 401
19
+ Unauthorized = Class.new(ClientError)
20
+
21
+ # code 404
22
+ NotFound = Class.new(ClientError)
23
+
24
+ # code 409
25
+ Conflict = Class.new(ClientError)
26
+
27
+ # code 500
28
+ InternalServerError = Class.new(ServerError)
29
+
30
+ ERRORS = {
31
+ 304 => Hyperb::Error::NotModified,
32
+ 400 => Hyperb::Error::BadRequest,
33
+ 401 => Hyperb::Error::Unauthorized,
34
+ 404 => Hyperb::Error::NotFound,
35
+ 409 => Hyperb::Error::Conflict,
36
+ 500 => Hyperb::Error::InternalServerError
37
+ }.freeze
38
+
39
+ def initialize(msg, code)
40
+ super(msg)
41
+ @code = code
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,12 @@
1
+ require 'hyperb/request'
2
+ require 'json'
3
+
4
+ module Hyperb
5
+ # simple wrapper for the version endpoint
6
+ module HyperVersion
7
+ # returns current version of hyper.sh api
8
+ def version
9
+ JSON.parse(Hyperb::Request.new(self, '/version', {}, 'get').perform)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ module Hyperb
2
+ # image object
3
+ class Image
4
+ attr_accessor :id, :parent_id, :repo_tags, :repo_digests,
5
+ :created, :size, :labels, :virtual_size
6
+
7
+ def initialize(attrs = {})
8
+ attrs.each do |k, v|
9
+ instance_variable_set("@#{k.downcase.to_sym}", v)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,108 @@
1
+ require 'hyperb/images/image'
2
+ require 'hyperb/request'
3
+ require 'hyperb/utils'
4
+ require 'hyperb/auth_object'
5
+ require 'json'
6
+ require 'uri'
7
+ require 'base64'
8
+
9
+ module Hyperb
10
+ # images api wrapper
11
+ module Images
12
+ include Hyperb::Utils
13
+ # list images
14
+ #
15
+ # @see https://docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Image/list.html
16
+ #
17
+ # @raise [Hyperb::Error::Unauthorized] raised when credentials are not valid.
18
+ #
19
+ # @return [Hyperb::Image] Array of Images.
20
+ #
21
+ # @param params [Hash] A customizable set of params.
22
+ # @option params [String] :all default is true
23
+ # @option params [String] :filter only return image with the specified name
24
+ def images(params = {})
25
+ path = '/images/json'
26
+ query = {}
27
+ query[:all] = params[:all] || true
28
+ query[:filter] = params[:filter] if params.key?(:filter)
29
+ response = JSON.parse(Hyperb::Request.new(self, path, query, 'get').perform)
30
+ response.map { |image| Hyperb::Image.new(image) }
31
+ end
32
+
33
+ # create (pull) an image
34
+ #
35
+ # @see https://docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Image/create.html
36
+ #
37
+ # @raise [Hyperb::Error::Unauthorized] raised when credentials are not valid.
38
+ # @raise [Hyperb::Error::InternalServerError] server error on hyper side.
39
+ # @raise [ArgumentError] when required arguments are not provided.
40
+ #
41
+ # @return [HTTP::Response::Body] a streamable response object.
42
+ #
43
+ # @param params [Hash] A customizable set of params.
44
+ #
45
+ # @required @option params [String] :from_image image name to be pulled
46
+ # @option params [String] :tag image tag name
47
+ #
48
+ # @option params [Hash] :x-registry-auth object containing either login information, or a token
49
+ # @option params x-registry-auth [String] :username
50
+ # @option params x-registry-auth [String] :email
51
+ # @option params x-registry-auth [String] :password
52
+ #
53
+ # TODO: @option params [Boolean] :stdout print stream to stdout
54
+ def create_image(params = {})
55
+ raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'from_image')
56
+ path = '/images/create'
57
+ query = { fromImage: params[:from_image] }
58
+ query[:tag] = params[:tag] if params.key?(:tag)
59
+ additional_headers = {}
60
+ if params.key?(:x_registry_auth)
61
+ auth = params[:x_registry_auth]
62
+ additional_headers[:x_registry_auth] = Hyperb::AuthObject.new(auth).encode
63
+ end
64
+ res = Hyperb::Request.new(self, path, query, 'post', '', additional_headers).perform
65
+ res
66
+ end
67
+
68
+ # remove an image
69
+ #
70
+ # @see https://docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Image/remove.html
71
+ #
72
+ # @raise [Hyperb::Error::Unauthorized] raised when credentials are not valid.
73
+ # @raise [Hyperb::Error::NotFound] raised when tag is not found.
74
+ # @raise [Hyperb::Error::Conflict] raised when the image will only be removed with force.
75
+ # @raise [Hyperb::Error::InternalServerError] server error.
76
+ #
77
+ # @return [Array] array of downcase symbolized json response.
78
+ #
79
+ # @param params [Hash] A customizable set of params.
80
+ # @option params [String] :name image name to be removed
81
+ # @option params [Boolean] :force force image to be removed
82
+ def remove_image(params = {})
83
+ path = '/images/' + params[:name]
84
+ query = {}
85
+ query[:force] = true if params.key?(:force)
86
+ res = JSON.parse(Hyperb::Request.new(self, path, query, 'delete').perform)
87
+ downcase_symbolize(res)
88
+ end
89
+
90
+ # inspect an image
91
+ #
92
+ # @see https://docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Image/inspect.html
93
+ #
94
+ # @raise [Hyperb::Error::Unauthorized] raised when credentials are not valid.
95
+ # @raise [Hyperb::Error::NotFound] raised when tag is not found.
96
+ # @raise [Hyperb::Error::InternalServerError] server error on hyper side.
97
+ #
98
+ # @return [Hash] downcased symbolized `inspect` json response.
99
+ #
100
+ # @param params [Hash] A customizable set of params.
101
+ # @option params [String] :name image name to be removed
102
+ def inspect_image(params = {})
103
+ path = '/images/' + params[:name] + '/json'
104
+ res = JSON.parse(Hyperb::Request.new(self, path, {}, 'get').perform)
105
+ downcase_symbolize(res)
106
+ end
107
+ end
108
+ end