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
Class Method Summary
collapse
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
-
#server_info ⇒ Object
-
#start_container(container_name, options) ⇒ Object
-
#stop_container(container_name, options) ⇒ Object
-
#update_container(container_name, container_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
|
Class Method Details
.convert_bools(hash) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/nexussw/lxd/rest_api.rb', line 104
def self.convert_bools(hash)
{}.tap do |retval|
hash.each do |k, v|
if [:ephemeral, :stateful].include? k
retval[k] = v
else
retval[k] = case v
when true then "true"
when false then "false"
else v.is_a?(Hash) && ([:config, :devices].include?(k)) ? convert_bools(v) : v
end
end
end
end
end
|
Instance Method Details
#container(container_name) ⇒ Object
92
93
94
|
# File 'lib/nexussw/lxd/rest_api.rb', line 92
def container(container_name)
get "/1.0/containers/#{container_name}"
end
|
#container_state(container_name) ⇒ Object
88
89
90
|
# File 'lib/nexussw/lxd/rest_api.rb', line 88
def container_state(container_name)
get "/1.0/containers/#{container_name}/state"
end
|
#containers ⇒ Object
96
97
98
|
# File 'lib/nexussw/lxd/rest_api.rb', line 96
def containers
get("/1.0/containers")
end
|
#create_container(container_name, options) ⇒ Object
18
19
20
21
|
# File 'lib/nexussw/lxd/rest_api.rb', line 18
def create_container(container_name, options)
options, sync = parse_options options
handle_async post("/1.0/containers", RestAPI.convert_bools(create_source(options).merge(name: container_name))), sync
end
|
#delete_container(container_name, options = {}) ⇒ Object
61
62
63
|
# File 'lib/nexussw/lxd/rest_api.rb', line 61
def delete_container(container_name, options = {})
handle_async delete("/1.0/containers/#{container_name}"), options[:sync]
end
|
#delete_log(container_name, log_name) ⇒ Object
47
48
49
|
# File 'lib/nexussw/lxd/rest_api.rb', line 47
def delete_log(container_name, log_name)
delete "/1.0/containers/#{container_name}/logs/#{log_name}"
end
|
#execute_command(container_name, command, options) ⇒ Object
35
36
37
38
39
|
# File 'lib/nexussw/lxd/rest_api.rb', line 35
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
41
42
43
44
45
|
# File 'lib/nexussw/lxd/rest_api.rb', line 41
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
84
85
86
|
# File 'lib/nexussw/lxd/rest_api.rb', line 84
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
65
66
67
68
69
|
# File 'lib/nexussw/lxd/rest_api.rb', line 65
def read_file(container_name, path)
get "/1.0/containers/#{container_name}/files?path=#{path}" do |response|
return response.body
end
end
|
#server_info ⇒ Object
14
15
16
|
# File 'lib/nexussw/lxd/rest_api.rb', line 14
def server_info
@server_info ||= LXD.symbolize_keys(get("/1.0"))[:metadata]
end
|
#start_container(container_name, options) ⇒ Object
51
52
53
54
|
# File 'lib/nexussw/lxd/rest_api.rb', line 51
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
56
57
58
59
|
# File 'lib/nexussw/lxd/rest_api.rb', line 56
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
|
#update_container(container_name, container_options) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/nexussw/lxd/rest_api.rb', line 23
def update_container(container_name, container_options)
if can_patch?
patch "/1.0/containers/#{container_name}", RestAPI.convert_bools(container_options)
else
data = container(container_name)[:metadata].select { |k, _| [:config, :devices, :profiles].include? k }
data[:config].merge! container_options[:config] if container_options.key? :config
data[:devices].merge! container_options[:devices] if container_options.key? :devices
data[:profiles] = container_options[:profiles] if container_options.key? :profiles
handle_async put("/1.0/containers/#{container_name}", RestAPI.convert_bools(data)), true
end
end
|
#wait_for_operation(operation_id) ⇒ Object
100
101
102
|
# File 'lib/nexussw/lxd/rest_api.rb', line 100
def wait_for_operation(operation_id)
get "/1.0/operations/#{operation_id}/wait"
end
|
#write_file(container_name, path, options) ⇒ Object
71
72
73
74
75
76
77
78
|
# File 'lib/nexussw/lxd/rest_api.rb', line 71
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
|