hyperb 0.4.1 → 0.5.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a274a2e8e2d64607cde734b988c25be8f6c3af0a
4
- data.tar.gz: cb8e39bf87b74eaafe30c575d0e3e720a8ece385
3
+ metadata.gz: 6cfd1df42e8e7a70e5da8139a47a333c132f55ec
4
+ data.tar.gz: 42211756c1e2fac4844f38b02f245f91f2a98ca5
5
5
  SHA512:
6
- metadata.gz: f7d11de4a362dd270fcc75afaf4b5c7bfb140c042b3f767349cb7ddac1bcecf2114315ee9885b7e434d3b85555e059e773c5fe8dfc19d21fbdbed611eb659e79
7
- data.tar.gz: c7f4ba000e628c45acef4ee26df573b51e1fb064f56e06f9de95fe4eee4b0105e1ed660e63b6819c37dfe66fae864e3571a12155ee282209c503f9f5f749ae16
6
+ metadata.gz: 839af760aa8293c023623b92b475e975dbb436a195f90b1afd0ecc5efcdc717cffb25569e46be3d87fa08f842112a52d1fbf2bcb2037f46c609f5a53bbd7e233
7
+ data.tar.gz: 8645b548c23c76ffe182e32ae77746ebfb10de08fa93a2c1319244152702553112518b0c6f372aba388eb3a90231dfc539126ff1deab6b6a8995d416220b18e1
data/.gitignore CHANGED
@@ -10,3 +10,6 @@
10
10
 
11
11
  # rspec failure tracking
12
12
  .rspec_status
13
+
14
+ # annoying mac stuff
15
+ .DS_Store
@@ -39,3 +39,6 @@ Metrics/LineLength:
39
39
 
40
40
  Metrics/ParameterLists:
41
41
  Max: 6
42
+
43
+ Metrics/AbcSize:
44
+ Max: 15.80
data/Makefile CHANGED
@@ -1,7 +1,8 @@
1
1
  .PHONY: test
2
2
 
3
- r:
4
- docker run --rm -it -v $(PWD):/usr/src hyperb bundle exec ruby test.rb
3
+ b:
4
+ docker run --rm -v $(PWD):/usr/src hyperb bundle exec ruby t.rb
5
+
5
6
 
6
7
  build:
7
8
  docker build -t hyperb .
data/README.md CHANGED
@@ -4,6 +4,7 @@
4
4
  <p align="center">
5
5
  <a href="https://circleci.com/gh/drish/hyperb"><img src="https://circleci.com/gh/drish/hyperb.svg?style=svg"></a>
6
6
  <a href="https://github.com/drish/hyperb/blob/master/LICENSE.txt"><img src="https://img.shields.io/badge/license-MIT-blue.svg"></a>
7
+ <a href="https://rubygems.org/gems/hyperb"><img src="https://img.shields.io/gem/dt/hyperb.svg?style=flat-square"></a>
7
8
  <a href="https://coveralls.io/github/drish/hyperb?branch=master"><img src="https://coveralls.io/repos/github/drish/hyperb/badge.svg?branch=master"></a>
8
9
  <a href="https://codeclimate.com/github/drish/hyperb"><img src="https://codeclimate.com/github/drish/hyperb/badges/gpa.svg" /></a>
9
10
  <a href="https://badge.fury.io/rb/hyperb"><img src="https://badge.fury.io/rb/hyperb.svg"</></a>
@@ -71,13 +71,13 @@ response = client.remove_image(name: 'busybox', force: true)
71
71
  returns a Hash containing information about the inspected image
72
72
 
73
73
  ```ruby
74
- info = client.inspect_image(id: 'busybox')
74
+ info = client.inspect_image(name: 'busybox')
75
75
  puts info
76
76
  ```
77
77
 
78
- ## Containers API
78
+ ## [Containers API](https://docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container)
79
79
 
80
- #### create_container
80
+ #### [create_container](https://docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/create.html)
81
81
 
82
82
  Return a hash containing downcased symbolized container info.
83
83
 
@@ -106,7 +106,7 @@ res = client.create_container name: 'nginx-c', image: 'nginx', hostname: 'hostny
106
106
  With custom mounts
107
107
 
108
108
  ```ruby
109
- res = client.create_container name: 'nginx-c', image: 'nginx', mounts: ['./path/to/mount']'
109
+ res = client.create_container name: 'nginx-c', image: 'nginx', mounts: ['./path/to/mount']
110
110
  ```
111
111
 
112
112
  With custom network mode
@@ -121,6 +121,28 @@ Exposing ports
121
121
  res = client.create_container name: 'nginx-c', image: 'nginx', exposedports: { '22/tcp': {} }
122
122
  ```
123
123
 
124
+ Configurations such as:
125
+
126
+ Binds, Links, PublishAllPorts, PortBindings, ReadonlyRootfs, VolumesFrom, RestartPolicy, NetworkMode, LogConfig, VolumeDriver
127
+
128
+ Are setup through Hyperb::HostConfig.
129
+
130
+ see examples below:
131
+
132
+ ```ruby
133
+ # you can either create host configurations through Hyperb::HostConfig
134
+ # or with a Hash
135
+
136
+ client.create_container name: 'mysql-1', image: 'mysql', host_config: Hyperb::HostConfig.new(binds: ['/path'] )}
137
+
138
+ client.create_container name: 'mysql-1', image: 'mysql', host_config: { binds: ['/path/'] }
139
+ client.create_container name: 'mysql-1', image: 'mysql', host_config: { links: ['container1:link_alias'] }
140
+ client.create_container name: 'mysql-1', image: 'mysql', host_config: { publish_all_ports: true }
141
+ client.create_container name: 'mysql-1', image: 'mysql', host_config: { readonly_rootfs: true }
142
+ client.create_container name: 'mysql-1', image: 'mysql', host_config: { restart_policy: { name: 'unless-stopped' }}
143
+ client.create_container name: 'mysql-1', image: 'mysql', host_config: { network_mode: 'host' }
144
+ ```
145
+
124
146
  #### start_container
125
147
 
126
148
  ```ruby
@@ -408,4 +430,4 @@ client.version
408
430
  "Arch"=>"amd64",
409
431
  "KernelVersion"=>"4.0.0"
410
432
  }
411
- ```
433
+ ```
@@ -1,5 +1,6 @@
1
1
  require 'hyperb/request'
2
2
  require 'hyperb/containers/container'
3
+ require 'hyperb/containers/host_config'
3
4
  require 'hyperb/utils'
4
5
  require 'json'
5
6
  require 'uri'
@@ -103,21 +104,28 @@ module Hyperb
103
104
  # @option params [String] :networkmode network mode, ie 'bridge'.
104
105
  # @option params [Hash] :exposedports ports to expose.
105
106
  #
107
+ # @option params [Hash] :exposedports ports to expose.
108
+ #
106
109
  # @option params [Hash] :labels hash containing key: value
107
110
  # @option params labels [String] :sh_hyper_instancetype container size: s1, s2, s3 ...
108
111
  def create_container(params = {})
109
112
  raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'image')
110
113
  path = '/containers/create'
111
114
  query = {}
112
-
113
- # set a default size, otherwise container can't be started
115
+ # set default size, otherwise container can't be started
114
116
  body = { labels: { sh_hyper_instancetype: 's1' } }
117
+
118
+ # parse host_config options
119
+ if params.key?(:host_config)
120
+ body[camelize(:host_config)] = setup_host_config(params)
121
+ params.delete(:host_config)
122
+ end
123
+
115
124
  query[:name] = params[:name] if params.key?(:name)
116
125
  params.delete(:name)
117
126
  body.merge!(params)
118
127
 
119
- response = JSON.parse(Hyperb::Request.new(self, path, query, 'post', body).perform)
120
- downcase_symbolize(response)
128
+ downcase_symbolize(JSON.parse(Hyperb::Request.new(self, path, query, 'post', body).perform))
121
129
  end
122
130
 
123
131
  # inspect a container
@@ -247,5 +255,15 @@ module Hyperb
247
255
  query[:name] = params[:name] if params.key?(:name)
248
256
  Hyperb::Request.new(self, path, query, 'post').perform
249
257
  end
258
+
259
+ private
260
+
261
+ def setup_host_config(params)
262
+ if params[:host_config].is_a?(Hash)
263
+ HostConfig.new(params[:host_config]).fmt
264
+ elsif params[:host_config].is_a?(HostConfig)
265
+ params[:host_config].fmt
266
+ end
267
+ end
250
268
  end
251
269
  end
@@ -0,0 +1,46 @@
1
+ require 'hyperb/utils'
2
+
3
+ module Hyperb
4
+ # helper for managing creating proper container host configs
5
+ # @see https://docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/Container/create.html
6
+ class HostConfig
7
+ include Hyperb::Utils
8
+
9
+ attr_accessor :binds, :links, :port_bindings, :publish_all_ports,
10
+ :network_mode, :restart_policy, :volume_driver, :log_config,
11
+ :readonly_rootfs, :volumes_from
12
+
13
+ def initialize(params = {})
14
+ params.each do |att, value|
15
+ value = downcase_symbolize(value) if value.is_a?(Hash)
16
+ instance_variable_set("@#{att.downcase.to_sym}", value)
17
+ end
18
+ end
19
+
20
+ # returns a hash containing formated host config data
21
+ #
22
+ # @returns [Hash]
23
+ def fmt
24
+ formated = {}
25
+ attrs.keys.each do |key|
26
+ formated[camelize(key)] = attrs[key]
27
+ end
28
+ formated
29
+ end
30
+
31
+ def attrs
32
+ {
33
+ binds: binds,
34
+ links: links,
35
+ port_bindings: port_bindings,
36
+ publish_all_ports: publish_all_ports,
37
+ network_mode: network_mode,
38
+ restart_policy: restart_policy,
39
+ volume_driver: volume_driver,
40
+ log_config: log_config,
41
+ readonly_rootfs: readonly_rootfs,
42
+ volumes_from: volumes_from
43
+ }
44
+ end
45
+ end
46
+ end
@@ -45,12 +45,10 @@ module Hyperb
45
45
  # @required @option params [String] :from_image image name to be pulled
46
46
  # @option params [String] :tag image tag name
47
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
48
+ # @option params [Hash] :x_registry_auth object containing either login information.
49
+ # @option params x_registry_auth [String] :username
50
+ # @option params x_registry_auth [String] :email
51
+ # @option params x_registry_auth [String] :password
54
52
  def create_image(params = {})
55
53
  raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'from_image')
56
54
  path = '/images/create'
@@ -101,6 +99,7 @@ module Hyperb
101
99
  # @param params [Hash] A customizable set of params.
102
100
  # @option params [String] :name image name to be removed
103
101
  def inspect_image(params = {})
102
+ raise ArgumentError, 'Invalid arguments.' unless check_arguments(params, 'name')
104
103
  path = '/images/' + params[:name] + '/json'
105
104
  res = JSON.parse(Hyperb::Request.new(self, path, {}, 'get').perform)
106
105
  downcase_symbolize(res)
@@ -1,6 +1,12 @@
1
1
  module Hyperb
2
2
  # utils functions
3
3
  module Utils
4
+ # converts from Symbol or String to CamelCase
5
+ # @return String
6
+ def camelize(value)
7
+ value.to_s.split('_').collect(&:capitalize).join
8
+ end
9
+
4
10
  # checks if all args are keys into the hash
5
11
  #
6
12
  # @return [Boolean]
@@ -1,3 +1,3 @@
1
1
  module Hyperb
2
- VERSION = '0.4.1'.freeze
2
+ VERSION = '0.5.0'.freeze
3
3
  end
@@ -263,6 +263,118 @@ RSpec.describe Hyperb::Containers do
263
263
  expect(a_request(:post, path)
264
264
  .with(body: { image: 'image', workingdir: '/path/', labels: { sh_hyper_instancetype: 's1' } })).to have_been_made
265
265
  end
266
+
267
+ it 'correct request should be made with Hyperb::HostConfig class' do
268
+ path = @create_container_path
269
+
270
+ hc = Hyperb::HostConfig.new(binds: ['/path/to/mount'])
271
+
272
+ stub_request(:post, path)
273
+ .with(body: { image: 'image', labels: { sh_hyper_instancetype:'s1' }, 'HostConfig': hc.fmt })
274
+ .to_return(body: fixture('create_container.json'))
275
+
276
+ @client.create_container(image: 'image', host_config: hc)
277
+ expect(a_request(:post, path)
278
+ .with(body: { image: 'image',
279
+ labels: { sh_hyper_instancetype:'s1' },
280
+ 'HostConfig': hc.fmt })).to have_been_made
281
+ end
282
+
283
+ it 'correct request should be made with host_configs:binds' do
284
+ path = @create_container_path
285
+
286
+ hc = Hyperb::HostConfig.new(binds: ['/path/to/mount'])
287
+
288
+ stub_request(:post, path)
289
+ .with(body: { image: 'image', labels: { sh_hyper_instancetype:'s1' }, 'HostConfig': hc.fmt })
290
+ .to_return(body: fixture('create_container.json'))
291
+
292
+ @client.create_container(image: 'image', host_config: { binds: ['/path/to/mount'] })
293
+ expect(a_request(:post, path)
294
+ .with(body: { image: 'image',
295
+ labels: { sh_hyper_instancetype:'s1' },
296
+ 'HostConfig': hc.fmt })).to have_been_made
297
+ end
298
+
299
+ it 'correct request should be made with host_configs:links' do
300
+ path = @create_container_path
301
+
302
+ hc = Hyperb::HostConfig.new(links: ['container2:alias'])
303
+
304
+ stub_request(:post, path)
305
+ .with(body: { image: 'image', labels: { sh_hyper_instancetype:'s1' }, 'HostConfig': hc.fmt })
306
+ .to_return(body: fixture('create_container.json'))
307
+
308
+ @client.create_container(image: 'image', host_config: { links: ['container2:alias'] })
309
+ expect(a_request(:post, path)
310
+ .with(body: { image: 'image',
311
+ labels: { sh_hyper_instancetype:'s1' },
312
+ 'HostConfig': hc.fmt })).to have_been_made
313
+ end
314
+
315
+ it 'correct request should be made with host_configs:publish_all_ports' do
316
+ path = @create_container_path
317
+
318
+ hc = Hyperb::HostConfig.new(publish_all_ports: false)
319
+
320
+ stub_request(:post, path)
321
+ .with(body: { image: 'image', labels: { sh_hyper_instancetype:'s1' }, 'HostConfig': hc.fmt })
322
+ .to_return(body: fixture('create_container.json'))
323
+
324
+ @client.create_container(image: 'image', host_config: { publish_all_ports: false })
325
+ expect(a_request(:post, path)
326
+ .with(body: { image: 'image',
327
+ labels: { sh_hyper_instancetype:'s1' },
328
+ 'HostConfig': hc.fmt })).to have_been_made
329
+ end
330
+
331
+ it 'correct request should be made with host_configs:readonly_rootfs' do
332
+ path = @create_container_path
333
+
334
+ hc = Hyperb::HostConfig.new(readonly_rootfs: false)
335
+
336
+ stub_request(:post, path)
337
+ .with(body: { image: 'image', labels: { sh_hyper_instancetype:'s1' }, 'HostConfig': hc.fmt })
338
+ .to_return(body: fixture('create_container.json'))
339
+
340
+ @client.create_container(image: 'image', host_config: { readonly_rootfs: false })
341
+ expect(a_request(:post, path)
342
+ .with(body: { image: 'image',
343
+ labels: { sh_hyper_instancetype:'s1' },
344
+ 'HostConfig': hc.fmt })).to have_been_made
345
+ end
346
+
347
+ it 'correct request should be made with host_configs:restart_policy' do
348
+ path = @create_container_path
349
+
350
+ hc = Hyperb::HostConfig.new(restart_policy: { name: 'unless-stopped' })
351
+
352
+ stub_request(:post, path)
353
+ .with(body: { image: 'image', labels: { sh_hyper_instancetype:'s1' }, 'HostConfig': hc.fmt })
354
+ .to_return(body: fixture('create_container.json'))
355
+
356
+ @client.create_container(image: 'image', host_config: { restart_policy: { name: 'unless-stopped' }})
357
+ expect(a_request(:post, path)
358
+ .with(body: { image: 'image',
359
+ labels: { sh_hyper_instancetype:'s1' },
360
+ 'HostConfig': hc.fmt })).to have_been_made
361
+ end
362
+
363
+ it 'correct request should be made with host_configs:network_mode' do
364
+ path = @create_container_path
365
+
366
+ hc = Hyperb::HostConfig.new(network_mode: 'host')
367
+
368
+ stub_request(:post, path)
369
+ .with(body: { image: 'image', labels: { sh_hyper_instancetype:'s1' }, 'HostConfig': hc.fmt })
370
+ .to_return(body: fixture('create_container.json'))
371
+
372
+ @client.create_container(image: 'image', host_config: { network_mode: 'host' })
373
+ expect(a_request(:post, path)
374
+ .with(body: { image: 'image',
375
+ labels: { sh_hyper_instancetype:'s1' },
376
+ 'HostConfig': hc.fmt })).to have_been_made
377
+ end
266
378
  end
267
379
 
268
380
  describe '#start_container' do
@@ -0,0 +1,18 @@
1
+ require 'helper'
2
+
3
+ RSpec.describe Hyperb::HostConfig do
4
+
5
+ it '#fmt' do
6
+ host_config = Hyperb::HostConfig.new(binds: ['path/to/binds'], publish_all_ports: false)
7
+ formated = host_config.fmt
8
+ expect(formated).to be_a Hash
9
+ expect(formated.key?('Binds')).to be true
10
+ expect(formated.key?('PublishAllPorts')).to be true
11
+ end
12
+
13
+ it '#attrs' do
14
+ host_config = Hyperb::HostConfig.new(binds: 'path/to/binds')
15
+ expect(host_config.attrs).to be_a Hash
16
+ expect(host_config.attrs[:binds]).to be_a String
17
+ end
18
+ end
@@ -120,6 +120,10 @@ RSpec.describe Hyperb::Images do
120
120
  .to_return(body: fixture('inspect_image.json'))
121
121
  end
122
122
 
123
+ it 'should raise ArgumentError when name is not provided' do
124
+ expect { @client.inspect_image }.to raise_error(ArgumentError)
125
+ end
126
+
123
127
  it 'request to the correct path should be made' do
124
128
  @client.inspect_image name: 'busybox'
125
129
  expect(a_request(:get, @inspect_image_path)).to have_been_made
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyperb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - drish
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-22 00:00:00.000000000 Z
11
+ date: 2017-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -146,6 +146,7 @@ files:
146
146
  - lib/hyperb/compose/compose.rb
147
147
  - lib/hyperb/containers/container.rb
148
148
  - lib/hyperb/containers/containers.rb
149
+ - lib/hyperb/containers/host_config.rb
149
150
  - lib/hyperb/error.rb
150
151
  - lib/hyperb/hyper_version.rb
151
152
  - lib/hyperb/images/image.rb
@@ -187,6 +188,7 @@ files:
187
188
  - spec/fixtures/services.json
188
189
  - spec/fixtures/volumes.json
189
190
  - spec/helper.rb
191
+ - spec/host_config_spec.rb
190
192
  - spec/image_spec.rb
191
193
  - spec/images_spec.rb
192
194
  - spec/network_spec.rb
@@ -215,7 +217,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
215
217
  version: '0'
216
218
  requirements: []
217
219
  rubyforge_project:
218
- rubygems_version: 2.6.11
220
+ rubygems_version: 2.6.12
219
221
  signing_key:
220
222
  specification_version: 4
221
223
  summary: The Hyper.sh Ruby Gem