Class: NexusSW::LXD::RestAPI
- Inherits:
-
Object
- Object
- NexusSW::LXD::RestAPI
show all
- Includes:
- Connection
- Defined in:
- lib/nexussw/lxd/rest_api.rb,
lib/nexussw/lxd/rest_api/errors.rb,
lib/nexussw/lxd/rest_api/connection.rb
Defined Under Namespace
Modules: Connection
Classes: Error
Instance Method Summary
collapse
-
#container(container_name) ⇒ Object
-
#container_state(container_name) ⇒ Object
-
#containers ⇒ Object
-
#create_container(container_name, options) ⇒ Object
-
#delete_container(container_name, options = {}) ⇒ Object
-
#delete_log(container_name, log_name) ⇒ Object
-
#execute_command(container_name, command, options) ⇒ Object
-
#initialize(api_options) ⇒ RestAPI
constructor
A new instance of RestAPI.
-
#log(container_name, log_name) ⇒ Object
-
#pull_file(container_name, remote_path, local_path) ⇒ Object
def push_file(local_path, container_name, remote_path) write_file container_name, remote_path, content: IO.binread(local_path) end.
-
#read_file(container_name, path) ⇒ Object
-
#start_container(container_name, options) ⇒ Object
-
#stop_container(container_name, options) ⇒ Object
-
#wait_for_operation(operation_id) ⇒ Object
-
#write_file(container_name, path, options) ⇒ Object
Methods included from Connection
#delete, #get, #patch, #post, #put
Constructor Details
#initialize(api_options) ⇒ RestAPI
Returns a new instance of RestAPI.
8
9
10
|
# File 'lib/nexussw/lxd/rest_api.rb', line 8
def initialize(api_options)
@api_options = api_options
end
|
Instance Method Details
#container(container_name) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/nexussw/lxd/rest_api.rb', line 77
def container(container_name)
exceptkeys = %w(config expanded_config)
get "/1.0/containers/#{container_name}" do |response|
retval = JSON.parse(response.body)
lift = retval['metadata'].select { |k, _| exceptkeys.include? k }
retval['metadata'].delete_if { |k, _| exceptkeys.include? k }
retval = LXD.symbolize_keys(retval)
retval[:metadata][:config] = lift['config'] if lift.key? 'config'
retval[:metadata][:expanded_config] = lift['expanded_config'] if lift.key? 'expanded_config'
return retval
end
end
|
#container_state(container_name) ⇒ Object
73
74
75
|
# File 'lib/nexussw/lxd/rest_api.rb', line 73
def container_state(container_name)
get "/1.0/containers/#{container_name}/state"
end
|
#containers ⇒ Object
90
91
92
|
# File 'lib/nexussw/lxd/rest_api.rb', line 90
def containers
get('/1.0/containers')
end
|
#create_container(container_name, options) ⇒ Object
14
15
16
17
18
|
# File 'lib/nexussw/lxd/rest_api.rb', line 14
def create_container(container_name, options)
options, sync = parse_options options
options[:config] = convert_bools(options[:config]) if options.key? :config
handle_async post('/1.0/containers', create_source(options).merge(name: container_name)), sync
end
|
#delete_container(container_name, options = {}) ⇒ Object
46
47
48
|
# File 'lib/nexussw/lxd/rest_api.rb', line 46
def delete_container(container_name, options = {})
handle_async delete("/1.0/containers/#{container_name}"), options[:sync]
end
|
#delete_log(container_name, log_name) ⇒ Object
32
33
34
|
# File 'lib/nexussw/lxd/rest_api.rb', line 32
def delete_log(container_name, log_name)
delete "/1.0/containers/#{container_name}/logs/#{log_name}"
end
|
#execute_command(container_name, command, options) ⇒ Object
20
21
22
23
24
|
# File 'lib/nexussw/lxd/rest_api.rb', line 20
def execute_command(container_name, command, options)
options, sync = parse_options options
command = command.shellsplit if command.is_a? String
handle_async post("/1.0/containers/#{container_name}/exec", options.merge(command: command)), sync
end
|
#log(container_name, log_name) ⇒ Object
26
27
28
29
30
|
# File 'lib/nexussw/lxd/rest_api.rb', line 26
def log(container_name, log_name)
get "/1.0/containers/#{container_name}/logs/#{log_name}" do |response|
return response.body
end
end
|
#pull_file(container_name, remote_path, local_path) ⇒ Object
def push_file(local_path, container_name, remote_path)
write_file container_name, remote_path, content: IO.binread(local_path)
end
69
70
71
|
# File 'lib/nexussw/lxd/rest_api.rb', line 69
def pull_file(container_name, remote_path, local_path)
IO.binwrite(local_path, read_file(container_name, remote_path))
end
|
#read_file(container_name, path) ⇒ Object
50
51
52
53
54
|
# File 'lib/nexussw/lxd/rest_api.rb', line 50
def read_file(container_name, path)
get "/1.0/containers/#{container_name}/files?path=#{path}" do |response|
return response.body
end
end
|
#start_container(container_name, options) ⇒ Object
36
37
38
39
|
# File 'lib/nexussw/lxd/rest_api.rb', line 36
def start_container(container_name, options)
options, sync = parse_options options
handle_async put("/1.0/containers/#{container_name}/state", options.merge(action: 'start')), sync
end
|
#stop_container(container_name, options) ⇒ Object
41
42
43
44
|
# File 'lib/nexussw/lxd/rest_api.rb', line 41
def stop_container(container_name, options)
options, sync = parse_options options
handle_async put("/1.0/containers/#{container_name}/state", options.merge(action: 'stop')), sync
end
|
#wait_for_operation(operation_id) ⇒ Object
94
95
96
|
# File 'lib/nexussw/lxd/rest_api.rb', line 94
def wait_for_operation(operation_id)
get "/1.0/operations/#{operation_id}/wait"
end
|
#write_file(container_name, path, options) ⇒ Object
56
57
58
59
60
61
62
63
|
# File 'lib/nexussw/lxd/rest_api.rb', line 56
def write_file(container_name, path, options)
post "/1.0/containers/#{container_name}/files?path=#{path}", options[:content] do |req|
req.['Content-Type'] = 'application/octet-stream'
req.['X-LXD-uid'] = options[:uid] if options[:uid]
req.['X-LXD-gid'] = options[:gid] if options[:gid]
req.['X-LXD-mode'] = options[:file_mode] if options[:file_mode]
end
end
|