Class: OsCtl::Image::OsCtldClient
- Inherits:
-
Object
- Object
- OsCtl::Image::OsCtldClient
- Defined in:
- lib/osctl/image/osctld_client.rb
Instance Method Summary collapse
- #activate_mount(ctid, dst) ⇒ Object
- #add_netif_bridge(ctid, netif, link) ⇒ Object
-
#batch ⇒ Object
Execute multiple commands within one connection.
- #bind_mount(ctid, src, dst, map_ids: true) ⇒ Object
- #connect {|client| ... } ⇒ Object protected
- #create_container_from_file(ctid, file) ⇒ Object
- #create_container_from_repo(ctid, distribution, version, arch, vendor, variant) ⇒ Object
- #delete_container(ctid, prune: false) ⇒ Object
- #delete_user(user) ⇒ Object
- #exec(ctid, cmd) ⇒ Object
- #find_container(ctid) ⇒ Object
- #ignore_error ⇒ Object
-
#initialize ⇒ OsCtldClient
constructor
A new instance of OsCtldClient.
- #kill_container(ctid) ⇒ Object
- #list_containers ⇒ Array
- #reinstall_container_from_image(ctid, image_path, remove_snapshots: false) ⇒ Object
- #runscript(ctid, script) ⇒ Object
- #set_container_attr(ctid, attr, value) ⇒ Object
- #set_container_dns_resolvers(ctid, dns_resolvers) ⇒ Object
- #set_container_nesting(ctid) ⇒ Object
- #start_container(ctid) ⇒ Object
- #stop_container(ctid) ⇒ Object
- #unmount(ctid, dst) ⇒ Object
- #unset_container_start_menu(ctid) ⇒ Object
- #user_idmap(user) ⇒ Array
Constructor Details
#initialize ⇒ OsCtldClient
Returns a new instance of OsCtldClient.
5 6 7 8 |
# File 'lib/osctl/image/osctld_client.rb', line 5 def initialize @client = OsCtl::Client.new @connected = false end |
Instance Method Details
#activate_mount(ctid, dst) ⇒ Object
258 259 260 261 262 263 264 265 266 |
# File 'lib/osctl/image/osctld_client.rb', line 258 def activate_mount(ctid, dst) connect do |client| client.cmd_data!( :ct_mount_activate, id: ctid, mountpoint: dst ) end end |
#add_netif_bridge(ctid, netif, link) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/osctl/image/osctld_client.rb', line 224 def add_netif_bridge(ctid, netif, link) connect do |client| client.cmd_data!( :netif_create, id: ctid, name: netif, type: 'bridge', link:, dhcp: true ) end end |
#batch ⇒ Object
Execute multiple commands within one connection
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/osctl/image/osctld_client.rb', line 11 def batch opened_before_batch = @connected @batch = true yield(self) ensure @batch = false if !opened_before_batch && @connected @client.close @connected = false end end |
#bind_mount(ctid, src, dst, map_ids: true) ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/osctl/image/osctld_client.rb', line 241 def bind_mount(ctid, src, dst, map_ids: true) connect do |client| client.cmd_data!( :ct_mount_create, id: ctid, fs: src, mountpoint: dst, type: 'bind', opts: 'bind,create=dir', automount: false, map_ids: ) end end |
#connect {|client| ... } ⇒ Object (protected)
306 307 308 309 310 311 312 313 314 315 |
# File 'lib/osctl/image/osctld_client.rb', line 306 def connect @client.open unless @connected @connected = true yield(@client) ensure unless @batch @client.close @connected = false end end |
#create_container_from_file(ctid, file) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/osctl/image/osctld_client.rb', line 71 def create_container_from_file(ctid, file) connect do |client| client.cmd_data!( :ct_import, as_id: ctid, file: File.absolute_path(file), map_mode: 'native' ) end end |
#create_container_from_repo(ctid, distribution, version, arch, vendor, variant) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/osctl/image/osctld_client.rb', line 52 def create_container_from_repo(ctid, distribution, version, arch, vendor, variant) connect do |client| client.cmd_data!( :ct_create, id: ctid, image: { distribution:, version:, arch:, vendor:, variant: }, map_mode: 'native' ) end end |
#delete_container(ctid, prune: false) ⇒ Object
217 218 219 220 221 |
# File 'lib/osctl/image/osctld_client.rb', line 217 def delete_container(ctid, prune: false) connect do |client| client.cmd_data!(:ct_delete, id: ctid, force: true, prune:) end end |
#delete_user(user) ⇒ Object
294 295 296 297 298 299 300 301 |
# File 'lib/osctl/image/osctld_client.rb', line 294 def delete_user(user) connect do |client| client.cmd_data!( :user_delete, name: user ) end end |
#exec(ctid, cmd) ⇒ Object
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 176 177 178 179 180 |
# File 'lib/osctl/image/osctld_client.rb', line 151 def exec(ctid, cmd) connect do |client| cont = client.cmd_data!( :ct_exec, id: ctid, cmd:, run: false ) if cont != 'continue' raise "exec not available: invalid response '#{cont}'" end null = File.open(File::NULL, 'r') client.send_io(null) client.send_io($stdout) client.send_io($stdout) null.close resp = client.receive_resp if resp.error? raise(resp. || 'exec failed') end resp[:exitstatus] end end |
#find_container(ctid) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/osctl/image/osctld_client.rb', line 38 def find_container(ctid) connect do |client| client.cmd_data!(:ct_show, id: ctid) end rescue OsCtl::Client::Error nil end |
#ignore_error ⇒ Object
24 25 26 27 28 |
# File 'lib/osctl/image/osctld_client.rb', line 24 def ignore_error yield(self) rescue OsCtl::Client::Error # ignore end |
#kill_container(ctid) ⇒ Object
143 144 145 146 147 |
# File 'lib/osctl/image/osctld_client.rb', line 143 def kill_container(ctid) connect do |client| client.cmd_data!(:ct_stop, id: ctid, method: 'kill') end end |
#list_containers ⇒ Array
31 32 33 34 35 |
# File 'lib/osctl/image/osctld_client.rb', line 31 def list_containers connect do |client| client.cmd_data!(:ct_list) end end |
#reinstall_container_from_image(ctid, image_path, remove_snapshots: false) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/osctl/image/osctld_client.rb', line 85 def reinstall_container_from_image(ctid, image_path, remove_snapshots: false) connect do |client| client.cmd_data!( :ct_reinstall, id: ctid, remove_snapshots:, type: :image, path: File.absolute_path(image_path) ) end end |
#runscript(ctid, script) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/osctl/image/osctld_client.rb', line 184 def runscript(ctid, script) connect do |client| cont = client.cmd_data!( :ct_runscript, id: ctid, script:, run: false ) if cont != 'continue' raise "runscript not available: invalid response '#{cont}'" end null = File.open(File::NULL, 'r') client.send_io(null) client.send_io($stdout) client.send_io($stdout) null.close resp = client.receive_resp if resp.error? raise(resp. || 'runscript failed') end resp[:exitstatus] end end |
#set_container_attr(ctid, attr, value) ⇒ Object
100 101 102 103 104 |
# File 'lib/osctl/image/osctld_client.rb', line 100 def set_container_attr(ctid, attr, value) connect do |client| client.cmd_data!(:ct_set, id: ctid, attrs: { attr => value }) end end |
#set_container_dns_resolvers(ctid, dns_resolvers) ⇒ Object
108 109 110 111 112 |
# File 'lib/osctl/image/osctld_client.rb', line 108 def set_container_dns_resolvers(ctid, dns_resolvers) connect do |client| client.cmd_data!(:ct_set, id: ctid, dns_resolvers:) end end |
#set_container_nesting(ctid) ⇒ Object
122 123 124 125 126 |
# File 'lib/osctl/image/osctld_client.rb', line 122 def set_container_nesting(ctid) connect do |client| client.cmd_data!(:ct_set, id: ctid, nesting: true) end end |
#start_container(ctid) ⇒ Object
129 130 131 132 133 |
# File 'lib/osctl/image/osctld_client.rb', line 129 def start_container(ctid) connect do |client| client.cmd_data!(:ct_start, id: ctid) end end |
#stop_container(ctid) ⇒ Object
136 137 138 139 140 |
# File 'lib/osctl/image/osctld_client.rb', line 136 def stop_container(ctid) connect do |client| client.cmd_data!(:ct_stop, id: ctid) end end |
#unmount(ctid, dst) ⇒ Object
270 271 272 273 274 275 276 277 278 |
# File 'lib/osctl/image/osctld_client.rb', line 270 def unmount(ctid, dst) connect do |client| client.cmd_data!( :ct_mount_delete, id: ctid, mountpoint: dst ) end end |
#unset_container_start_menu(ctid) ⇒ Object
115 116 117 118 119 |
# File 'lib/osctl/image/osctld_client.rb', line 115 def (ctid) connect do |client| client.cmd_data!(:ct_unset, id: ctid, start_menu: true) end end |
#user_idmap(user) ⇒ Array
282 283 284 285 286 287 288 289 290 291 |
# File 'lib/osctl/image/osctld_client.rb', line 282 def user_idmap(user) connect do |client| client.cmd_data!( :user_idmap_list, name: user, uid: true, gid: true ) end end |