oreno_lxdapi 0.0.2 → 0.0.3

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: a3710f9cd3880c44e63eef272dcb7358f6d812ee
4
- data.tar.gz: d323a3419d7e6622778d8fd61683bd99ba4f298b
3
+ metadata.gz: 4ce6447395deac24ee036dc733dee62e6aa28304
4
+ data.tar.gz: 4ae833b4a656e398894b8b10cd1c49c686b50622
5
5
  SHA512:
6
- metadata.gz: c3b4aaad5370be402f4396baad0c4086bc7387279dd2278f4b132fe62a2062725f72a77415710b4ccf9110de2723074d1713353d8575db556d50c4aa37a04300
7
- data.tar.gz: a1d5d4a3710f555bc0f468f045a2865d8b8ade8f18c7db8530ea77e7040128333280ceddbc14505ac3306aa42bf5d75ad45c5ba02bf70a033f5067fa84b31a6b
6
+ metadata.gz: 001420b2c9294efcd1b8abd13d298a4847be4d9b544c1fbafcbc67c9e376ecb71d693616191c1253efcd82c5257409f93da100147a64d39f55030d992093f80f
7
+ data.tar.gz: 94c2266f7023ec81aaf3b0814182d2e0b3f60de957f30ce48f2c2768b225b23f924b50f1caa1107dcd278213636052a665ad006b49728eba64d5cdfae0ebc235
@@ -1,6 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.3.0
4
- - 2.2.4
5
- - 2.1.8
3
+ - 2.4.0
4
+ - 2.3.3
5
+ - 2.2.6
6
+ - 2.1.10
6
7
  before_install: gem install bundler -v 1.10.6
data/Rakefile CHANGED
@@ -1,6 +1,18 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
3
4
 
4
- RSpec::Core::RakeTask.new(:spec)
5
+ if defined?(RSpec)
6
+ task default: :spec
7
+ task spec: 'spec:all'
8
+ namespace :spec do
9
+ task all: ['spec:core',
10
+ 'spec:rubocop']
5
11
 
6
- task :default => :spec
12
+ RSpec::Core::RakeTask.new(:core) do |t|
13
+ t.pattern = 'spec/*_spec.rb'
14
+ end
15
+
16
+ RuboCop::RakeTask.new
17
+ end
18
+ end
@@ -1,11 +1,15 @@
1
- require "oreno_lxdapi/version"
2
- require "net_http_unix"
3
- require "json"
4
- require "logger"
1
+ require 'oreno_lxdapi/version'
2
+ require 'net_http_unix'
3
+ require 'json'
4
+ require 'logger'
5
+
6
+ # rubocop:disable Metrics/MethodLength
7
+ # rubocop:disable Metrics/ClassLength
8
+ # rubocop:disable Metrics/AbcSize
5
9
 
6
10
  module OrenoLxdapi
11
+ # Helper Class to talk to LXD daemon
7
12
  class Client
8
-
9
13
  def initialize(uri, image_name, container_name)
10
14
  @log = Logger.new(STDOUT)
11
15
  @uri = uri
@@ -16,173 +20,158 @@ module OrenoLxdapi
16
20
  def client
17
21
  NetX::HTTPUnix.new(@uri)
18
22
  end
19
-
23
+
20
24
  def list_containers
21
- req = Net::HTTP::Get.new("/1.0/containers")
25
+ req = Net::HTTP::Get.new('/1.0/containers')
22
26
  resp = client.request(req)
23
- return resp.body
27
+ resp.body
24
28
  end
25
29
 
26
- def config_container(opts={})
27
- end
28
-
29
- def create_container(opts={})
30
+ def config_container(opts = {}) end
31
+
32
+ def create_container(opts = {})
30
33
  options = {
31
- :architecture => 2,
32
- :profiles => ["default"],
33
- :ephemeral => true,
34
- :limits_cpu => "1",
34
+ architecture: 2,
35
+ profiles: ['default'],
36
+ ephemeral: true,
37
+ limits_cpu: '1'
35
38
  }
36
39
 
37
40
  options.merge!(opts)
38
41
 
39
- req = Net::HTTP::Post.new("/1.0/containers")
40
- req["Content-Type"] = "application/json"
42
+ req = Net::HTTP::Post.new('/1.0/containers')
43
+ req['Content-Type'] = 'application/json'
41
44
  payload = {
42
- "name" => @container_name,
43
- "architecture" => options[:architecture].to_i,
44
- "profiles" => options[:profiles],
45
- "ephemeral" => options[:ephemeral],
46
- "config" => {
47
- "limits.cpu" => options[:limits_cpu]
45
+ 'name' => @container_name,
46
+ 'architecture' => options[:architecture].to_i,
47
+ 'profiles' => options[:profiles],
48
+ 'ephemeral' => options[:ephemeral],
49
+ 'config' => {
50
+ 'limits.cpu' => options[:limits_cpu]
48
51
  },
49
- "source" => {
50
- "type" => "image",
51
- "alias" => @image_name
52
+ 'source' => {
53
+ 'type' => 'image',
54
+ 'alias' => @image_name
52
55
  }
53
56
  }
54
57
  req.body = payload.to_json
55
58
 
56
59
  resp = client.request(req)
57
- return resp.body
60
+ resp.body
58
61
  end
59
62
 
60
63
  def delete_container
61
- @log.info("Deleting Container...")
64
+ @log.info('Deleting Container...')
62
65
  req = Net::HTTP::Delete.new("/1.0/containers/#{@container_name}")
63
66
  resp = client.request(req)
64
- return resp.body
67
+ resp.body
65
68
  end
66
-
69
+
67
70
  def describe_container
68
71
  req = Net::HTTP::Get.new("/1.0/containers/#{@container_name}")
69
72
  resp = client.request(req)
70
73
  json = JSON.parse(resp.body)
71
74
 
72
- if json['metadata']
73
- return json['metadata']
74
- else
75
- @log.warn("Failed to get metadata.")
76
- end
77
-
75
+ return json['metadata'] if json['metadata']
76
+ @log.warn('Failed to get metadata.')
78
77
  end
79
-
78
+
80
79
  def check_container_status
81
80
  req = Net::HTTP::Get.new("/1.0/containers/#{@container_name}/state")
82
81
  resp = client.request(req)
83
82
  json = JSON.parse(resp.body)
84
83
 
85
- status = ""
86
- ipv4 = ""
87
- if json['metadata']
88
- status = json['metadata']['status']
89
- unless json['metadata']['ips'] == nil
90
- json['metadata']['ips'].each { |ip| ipv4 = ip['address'] if ip['interface'] == "eth0"}
91
- return status, ipv4
92
- else
93
- return status
94
- end
95
- end
96
-
84
+ status = ''
85
+ ipv4 = ''
86
+ status = json['metadata']['status'] if json['metadata']
87
+ return status if json['metadata']['ips'].nil
88
+ json['metadata']['ips'].each\
89
+ { |ip| ipv4 = ip['address'] if ip['interface'] == 'eth0' }
97
90
  end
98
91
 
99
- def state_container(action, opts={})
92
+ def state_container(action, opts = {})
100
93
  options = {
101
- :timeout => 30,
102
- :force => true
94
+ timeout: 30,
95
+ force: true
103
96
  }
104
97
 
105
98
  options.merge!(opts)
106
99
 
107
100
  req = Net::HTTP::Put.new("/1.0/containers/#{@container_name}/state")
108
- req["Content-Type"] = "application/json"
101
+ req['Content-Type'] = 'application/json'
109
102
  payload = {
110
- "action" => action,
111
- "timeout" => options[:timeout],
112
- "force" => options[:force]
103
+ 'action' => action,
104
+ 'timeout' => options[:timeout],
105
+ 'force' => options[:force]
113
106
  }
114
107
  req.body = payload.to_json
115
108
  resp = client.request(req)
116
109
 
117
- if action == "start"
110
+ if action == 'start'
118
111
  loop do
119
- @log.info("Starting Container...")
112
+ @log.info('Starting Container...')
120
113
  status = check_container_status
121
- if status.length == 2 && status[1] != ""
122
- break
123
- end
124
-
114
+ break if status.length == 2 && status[1] != ''
125
115
  sleep 3
126
116
  end
127
117
  return resp.body
128
- elsif action == "stop"
129
- @log.info("Stopping Container...")
118
+ elsif action == 'stop'
119
+ @log.info('Stopping Container...')
130
120
  return resp.body
131
121
  else
132
- @log.warn("Invalid argument.")
133
- end
134
-
122
+ @log.warn('Invalid argument.')
123
+ end
135
124
  end
136
125
 
137
126
  def file_upload(src, dst)
138
- req = Net::HTTP::Post.new("/1.0/containers/#{@container_name}/files?path=#{dst}")
139
- req["X-LXD-uid"] = "0"
140
- req["X-LXD-gid"] = "0"
141
- req["X-LXD-mode"] = "700"
142
- req["Content-Type"] = "multipart/form-data"
143
-
144
- resp = ""
127
+ req = Net::HTTP::Post.new("/1.0/containers/#{@container_name}"\
128
+ "/files?path=#{dst}")
129
+ req['X-LXD-uid'] = '0'
130
+ req['X-LXD-gid'] = '0'
131
+ req['X-LXD-mode'] = '700'
132
+ req['Content-Type'] = 'multipart/form-data'
133
+
134
+ resp = ''
145
135
  File.open(src, 'rb') do |f|
146
136
  req.body_stream = f
147
- req["Content-Length"] = f.size
137
+ req['Content-Length'] = f.size
148
138
  resp = client.request(req)
149
139
  end
150
-
151
- return resp.body
140
+
141
+ resp.body
152
142
  end
153
143
 
154
144
  def create_exec(command)
155
- # commands = command.split(" ")
156
- # req = Net::HTTP::Post.new("/1.0/containers/#{@container_name}/exec")
157
- # req["Content-Type"] = "application/json"
158
- # payload = {
159
- # "command" => commands,
160
- # "environment" => {
161
- # "HOME" => "/root",
162
- # "TERM" => "screen",
163
- # "USER" => "root",
164
- # },
165
- # "wait-for-websocket" => true,
166
- # "interactive" => true,
167
- # }
168
- # req.body = payload.to_json
169
- #
170
- # resp = client.request(req)
171
- # json = JSON.parse(resp.body)
172
-
173
- # operation_id = ""
174
- # secret = ""
175
-
176
- # if json['metadata']
177
- # operation_id = json['metadata']['id']
178
- # unless json['metadata']['metadata'] == nil
179
- # secret = json['metadata']['metadata']['fds']['0']
180
- # return operation_id, secret
181
- # else
182
- # return operation_id
183
- # end
184
- # end
185
-
145
+ # commands = command.split(" ")
146
+ # req = Net::HTTP::Post.new("/1.0/containers/#{@container_name}/exec")
147
+ # req["Content-Type"] = "application/json"
148
+ # payload = {
149
+ # "command" => commands,
150
+ # "environment" => {
151
+ # "HOME" => "/root",
152
+ # "TERM" => "screen",
153
+ # "USER" => "root",
154
+ # },
155
+ # "wait-for-websocket" => true,
156
+ # "interactive" => true,
157
+ # }
158
+ # req.body = payload.to_json
159
+ #
160
+ # resp = client.request(req)
161
+ # json = JSON.parse(resp.body)
162
+
163
+ # operation_id = ""
164
+ # secret = ""
165
+
166
+ # if json['metadata']
167
+ # operation_id = json['metadata']['id']
168
+ # unless json['metadata']['metadata'] == nil
169
+ # secret = json['metadata']['metadata']['fds']['0']
170
+ # return operation_id, secret
171
+ # else
172
+ # return operation_id
173
+ # end
174
+ # end
186
175
  end
187
176
 
188
177
  def run_exec(operation_id, secret)
@@ -193,14 +182,13 @@ module OrenoLxdapi
193
182
  lxc_exec = "lxc exec #{@container_name} -- "
194
183
  run_command = lxc_exec + command
195
184
  status = system(run_command)
196
- return status
185
+ status
197
186
  end
198
187
 
199
- #def wait_operation(operation_id)
188
+ # def wait_operation(operation_id)
200
189
  # req = Net::HTTP::Get.new("/1.0/operations/#{operation_id}/wait")
201
190
  # resp = client.request(req)
202
191
  # return resp.body
203
- #end
204
-
192
+ # end
205
193
  end
206
194
  end
@@ -1,3 +1,3 @@
1
1
  module OrenoLxdapi
2
- VERSION = "0.0.2"
2
+ VERSION = '0.0.3'.freeze
3
3
  end
@@ -4,25 +4,26 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'oreno_lxdapi/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "oreno_lxdapi"
7
+ spec.name = 'oreno_lxdapi'
8
8
  spec.version = OrenoLxdapi::VERSION
9
- spec.authors = ["inokappa"]
10
- spec.email = ["inokara at gmail.com"]
9
+ spec.authors = ['inokappa']
10
+ spec.email = ['inokara at gmail.com']
11
11
 
12
- spec.summary = %q{LXD API Client for Ruby.}
13
- spec.description = %q{LXD API Client for Ruby.}
14
- spec.homepage = "https://github.com/inokappa/oreno_lxdapi"
12
+ spec.summary = 'LXD API Client for Ruby.'
13
+ spec.description = 'LXD API Client for Ruby.'
14
+ spec.homepage = 'https://github.com/inokappa/oreno_lxdapi'
15
15
 
16
-
17
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
- spec.bindir = "exe"
16
+ spec.files = `git ls-files -z`.split("\x0").reject\
17
+ { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
- spec.require_paths = ["lib"]
20
+ spec.require_paths = ['lib']
21
21
 
22
- spec.add_dependency "net_http_unix"
22
+ spec.add_dependency 'net_http_unix'
23
23
 
24
- spec.add_development_dependency "bundler", "~> 1.10"
25
- spec.add_development_dependency "rake", "~> 10.0"
26
- spec.add_development_dependency "rspec"
27
- spec.add_development_dependency "pry"
24
+ spec.add_development_dependency 'bundler', '~> 1.10'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec'
27
+ spec.add_development_dependency 'pry'
28
+ spec.add_development_dependency 'rubocop', '~> 0.47.0'
28
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oreno_lxdapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - inokappa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-03 00:00:00.000000000 Z
11
+ date: 2017-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net_http_unix
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.47.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.47.0
83
97
  description: LXD API Client for Ruby.
84
98
  email:
85
99
  - inokara at gmail.com
@@ -115,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
129
  version: '0'
116
130
  requirements: []
117
131
  rubyforge_project:
118
- rubygems_version: 2.2.2
132
+ rubygems_version: 2.5.1
119
133
  signing_key:
120
134
  specification_version: 4
121
135
  summary: LXD API Client for Ruby.