Class: OrenoLxdapi::Client
- Inherits:
-
Object
- Object
- OrenoLxdapi::Client
- Defined in:
- lib/oreno_lxdapi.rb
Overview
Helper Class to talk to LXD daemon
Instance Method Summary collapse
- #check_container_status ⇒ Object
- #client ⇒ Object
- #config_container(opts = {}) ⇒ Object
- #create_container(opts = {}) ⇒ Object
- #create_exec(command) ⇒ Object
- #delete_container ⇒ Object
- #describe_container ⇒ Object
- #file_upload(src, dst) ⇒ Object
-
#initialize(uri, image_name, container_name) ⇒ Client
constructor
A new instance of Client.
- #list_containers ⇒ Object
- #run_exec(operation_id, secret) ⇒ Object
- #run_lxc_exec(command) ⇒ Object
- #state_container(action, opts = {}) ⇒ Object
Constructor Details
#initialize(uri, image_name, container_name) ⇒ Client
Returns a new instance of Client.
13 14 15 16 17 18 |
# File 'lib/oreno_lxdapi.rb', line 13 def initialize(uri, image_name, container_name) @log = Logger.new(STDOUT) @uri = uri @image_name = image_name @container_name = container_name end |
Instance Method Details
#check_container_status ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/oreno_lxdapi.rb', line 79 def check_container_status req = Net::HTTP::Get.new("/1.0/containers/#{@container_name}/state") resp = client.request(req) json = JSON.parse(resp.body) status = '' ipv4 = '' status = json['metadata']['status'] if json['metadata'] return status if json['metadata']['ips'].nil json['metadata']['ips'].each\ { |ip| ipv4 = ip['address'] if ip['interface'] == 'eth0' } end |
#client ⇒ Object
20 21 22 |
# File 'lib/oreno_lxdapi.rb', line 20 def client NetX::HTTPUnix.new(@uri) end |
#config_container(opts = {}) ⇒ Object
30 |
# File 'lib/oreno_lxdapi.rb', line 30 def config_container(opts = {}) end |
#create_container(opts = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/oreno_lxdapi.rb', line 32 def create_container(opts = {}) = { architecture: 2, profiles: ['default'], ephemeral: true, limits_cpu: '1' } .merge!(opts) req = Net::HTTP::Post.new('/1.0/containers') req['Content-Type'] = 'application/json' payload = { 'name' => @container_name, 'architecture' => [:architecture].to_i, 'profiles' => [:profiles], 'ephemeral' => [:ephemeral], 'config' => { 'limits.cpu' => [:limits_cpu] }, 'source' => { 'type' => 'image', 'alias' => @image_name } } req.body = payload.to_json resp = client.request(req) resp.body end |
#create_exec(command) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/oreno_lxdapi.rb', line 144 def create_exec(command) # commands = command.split(" ") # req = Net::HTTP::Post.new("/1.0/containers/#{@container_name}/exec") # req["Content-Type"] = "application/json" # payload = { # "command" => commands, # "environment" => { # "HOME" => "/root", # "TERM" => "screen", # "USER" => "root", # }, # "wait-for-websocket" => true, # "interactive" => true, # } # req.body = payload.to_json # # resp = client.request(req) # json = JSON.parse(resp.body) # operation_id = "" # secret = "" # if json['metadata'] # operation_id = json['metadata']['id'] # unless json['metadata']['metadata'] == nil # secret = json['metadata']['metadata']['fds']['0'] # return operation_id, secret # else # return operation_id # end # end end |
#delete_container ⇒ Object
63 64 65 66 67 68 |
# File 'lib/oreno_lxdapi.rb', line 63 def delete_container @log.info('Deleting Container...') req = Net::HTTP::Delete.new("/1.0/containers/#{@container_name}") resp = client.request(req) resp.body end |
#describe_container ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/oreno_lxdapi.rb', line 70 def describe_container req = Net::HTTP::Get.new("/1.0/containers/#{@container_name}") resp = client.request(req) json = JSON.parse(resp.body) return json['metadata'] if json['metadata'] @log.warn('Failed to get metadata.') end |
#file_upload(src, dst) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/oreno_lxdapi.rb', line 126 def file_upload(src, dst) req = Net::HTTP::Post.new("/1.0/containers/#{@container_name}"\ "/files?path=#{dst}") req['X-LXD-uid'] = '0' req['X-LXD-gid'] = '0' req['X-LXD-mode'] = '700' req['Content-Type'] = 'multipart/form-data' resp = '' File.open(src, 'rb') do |f| req.body_stream = f req['Content-Length'] = f.size resp = client.request(req) end resp.body end |
#list_containers ⇒ Object
24 25 26 27 28 |
# File 'lib/oreno_lxdapi.rb', line 24 def list_containers req = Net::HTTP::Get.new('/1.0/containers') resp = client.request(req) resp.body end |
#run_exec(operation_id, secret) ⇒ Object
177 178 179 |
# File 'lib/oreno_lxdapi.rb', line 177 def run_exec(operation_id, secret) # run_lxc_exec end |
#run_lxc_exec(command) ⇒ Object
181 182 183 184 185 186 |
# File 'lib/oreno_lxdapi.rb', line 181 def run_lxc_exec(command) lxc_exec = "lxc exec #{@container_name} -- " run_command = lxc_exec + command status = system(run_command) status end |
#state_container(action, opts = {}) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/oreno_lxdapi.rb', line 92 def state_container(action, opts = {}) = { timeout: 30, force: true } .merge!(opts) req = Net::HTTP::Put.new("/1.0/containers/#{@container_name}/state") req['Content-Type'] = 'application/json' payload = { 'action' => action, 'timeout' => [:timeout], 'force' => [:force] } req.body = payload.to_json resp = client.request(req) if action == 'start' loop do @log.info('Starting Container...') status = check_container_status break if status.length == 2 && status[1] != '' sleep 3 end return resp.body elsif action == 'stop' @log.info('Stopping Container...') return resp.body else @log.warn('Invalid argument.') end end |