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 +4 -4
- data/.travis.yml +4 -3
- data/Rakefile +16 -4
- data/lib/oreno_lxdapi.rb +101 -113
- data/lib/oreno_lxdapi/version.rb +1 -1
- data/oreno_lxdapi.gemspec +16 -15
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ce6447395deac24ee036dc733dee62e6aa28304
|
4
|
+
data.tar.gz: 4ae833b4a656e398894b8b10cd1c49c686b50622
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 001420b2c9294efcd1b8abd13d298a4847be4d9b544c1fbafcbc67c9e376ecb71d693616191c1253efcd82c5257409f93da100147a64d39f55030d992093f80f
|
7
|
+
data.tar.gz: 94c2266f7023ec81aaf3b0814182d2e0b3f60de957f30ce48f2c2768b225b23f924b50f1caa1107dcd278213636052a665ad006b49728eba64d5cdfae0ebc235
|
data/.travis.yml
CHANGED
data/Rakefile
CHANGED
@@ -1,6 +1,18 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
require 'rubocop/rake_task'
|
3
4
|
|
4
|
-
RSpec
|
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
|
-
|
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
|
data/lib/oreno_lxdapi.rb
CHANGED
@@ -1,11 +1,15 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
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(
|
25
|
+
req = Net::HTTP::Get.new('/1.0/containers')
|
22
26
|
resp = client.request(req)
|
23
|
-
|
27
|
+
resp.body
|
24
28
|
end
|
25
29
|
|
26
|
-
def config_container(opts={})
|
27
|
-
|
28
|
-
|
29
|
-
def create_container(opts={})
|
30
|
+
def config_container(opts = {}) end
|
31
|
+
|
32
|
+
def create_container(opts = {})
|
30
33
|
options = {
|
31
|
-
:
|
32
|
-
:
|
33
|
-
:
|
34
|
-
:
|
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(
|
40
|
-
req[
|
42
|
+
req = Net::HTTP::Post.new('/1.0/containers')
|
43
|
+
req['Content-Type'] = 'application/json'
|
41
44
|
payload = {
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
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
|
-
|
50
|
-
|
51
|
-
|
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
|
-
|
60
|
+
resp.body
|
58
61
|
end
|
59
62
|
|
60
63
|
def delete_container
|
61
|
-
@log.info(
|
64
|
+
@log.info('Deleting Container...')
|
62
65
|
req = Net::HTTP::Delete.new("/1.0/containers/#{@container_name}")
|
63
66
|
resp = client.request(req)
|
64
|
-
|
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
|
-
|
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
|
-
|
89
|
-
|
90
|
-
|
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
|
-
:
|
102
|
-
:
|
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[
|
101
|
+
req['Content-Type'] = 'application/json'
|
109
102
|
payload = {
|
110
|
-
|
111
|
-
|
112
|
-
|
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 ==
|
110
|
+
if action == 'start'
|
118
111
|
loop do
|
119
|
-
@log.info(
|
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 ==
|
129
|
-
@log.info(
|
118
|
+
elsif action == 'stop'
|
119
|
+
@log.info('Stopping Container...')
|
130
120
|
return resp.body
|
131
121
|
else
|
132
|
-
@log.warn(
|
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}
|
139
|
-
|
140
|
-
req[
|
141
|
-
req[
|
142
|
-
req[
|
143
|
-
|
144
|
-
|
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[
|
137
|
+
req['Content-Length'] = f.size
|
148
138
|
resp = client.request(req)
|
149
139
|
end
|
150
|
-
|
151
|
-
|
140
|
+
|
141
|
+
resp.body
|
152
142
|
end
|
153
143
|
|
154
144
|
def create_exec(command)
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
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
|
-
|
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
|
data/lib/oreno_lxdapi/version.rb
CHANGED
data/oreno_lxdapi.gemspec
CHANGED
@@ -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 =
|
7
|
+
spec.name = 'oreno_lxdapi'
|
8
8
|
spec.version = OrenoLxdapi::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
9
|
+
spec.authors = ['inokappa']
|
10
|
+
spec.email = ['inokara at gmail.com']
|
11
11
|
|
12
|
-
spec.summary =
|
13
|
-
spec.description =
|
14
|
-
spec.homepage =
|
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
|
-
|
18
|
-
spec.bindir =
|
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 = [
|
20
|
+
spec.require_paths = ['lib']
|
21
21
|
|
22
|
-
spec.add_dependency
|
22
|
+
spec.add_dependency 'net_http_unix'
|
23
23
|
|
24
|
-
spec.add_development_dependency
|
25
|
-
spec.add_development_dependency
|
26
|
-
spec.add_development_dependency
|
27
|
-
spec.add_development_dependency
|
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.
|
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:
|
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.
|
132
|
+
rubygems_version: 2.5.1
|
119
133
|
signing_key:
|
120
134
|
specification_version: 4
|
121
135
|
summary: LXD API Client for Ruby.
|