class NexusSW::LXD::RestAPI
Attributes
api_options[R]
Public Class Methods
convert_bools(hash)
click to toggle source
# 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
new(api_options)
click to toggle source
# File lib/nexussw/lxd/rest_api.rb, line 8 def initialize(api_options) @api_options = api_options end
Public Instance Methods
container(container_name)
click to toggle source
# File lib/nexussw/lxd/rest_api.rb, line 92 def container(container_name) get "/1.0/containers/#{container_name}" end
container_state(container_name)
click to toggle source
# File lib/nexussw/lxd/rest_api.rb, line 88 def container_state(container_name) get "/1.0/containers/#{container_name}/state" end
containers()
click to toggle source
# File lib/nexussw/lxd/rest_api.rb, line 96 def containers get("/1.0/containers") end
create_container(container_name, options)
click to toggle source
# 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 = {})
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
def push_file(local_path, container_name, remote_path)
write_file container_name, remote_path, content: IO.binread(local_path)
end
# 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)
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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.headers["Content-Type"] = "application/octet-stream" req.headers["X-LXD-uid"] = options[:uid] if options[:uid] req.headers["X-LXD-gid"] = options[:gid] if options[:gid] req.headers["X-LXD-mode"] = options[:file_mode] if options[:file_mode] end end
Private Instance Methods
can_patch?()
click to toggle source
# File lib/nexussw/lxd/rest_api.rb, line 124 def can_patch? server_info[:api_extensions].include? "patch" end
create_source(options)
click to toggle source
# File lib/nexussw/lxd/rest_api.rb, line 138 def create_source(options) moveprops = [:type, :alias, :fingerprint, :properties, :protocol, :server] options.dup.tap do |retval| retval[:source] = { type: "image", mode: "pull" }.merge(retval.select { |k, _| moveprops.include? k }) unless retval.key? :source retval.delete_if { |k, _| moveprops.include? k } end end
handle_async(data, sync)
click to toggle source
# File lib/nexussw/lxd/rest_api.rb, line 128 def handle_async(data, sync) return data if sync == false wait_for_operation data[:metadata][:id] end
parse_options(options)
click to toggle source
# File lib/nexussw/lxd/rest_api.rb, line 133 def parse_options(options) sync = options[:sync] [options.delete_if { |k, _| k == :sync }, sync] end