Class: OsCtl::Image::OsCtldClient

Inherits:
Object
  • Object
show all
Defined in:
lib/osctl/image/osctld_client.rb

Instance Method Summary collapse

Constructor Details

#initializeOsCtldClient

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

Parameters:

  • ctid (String)
  • dst (String)


217
218
219
220
221
222
223
224
225
# File 'lib/osctl/image/osctld_client.rb', line 217

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

Parameters:

  • ctid (String)


185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/osctl/image/osctld_client.rb', line 185

def add_netif_bridge(ctid, netif, link)
  connect do |client|
    client.cmd_data!(
      :netif_create,
      id: ctid,
      name: netif,
      type: 'bridge',
      link: link,
      dhcp: true,
    )
  end
end

#batchObject

Execute multiple commands within one connection



11
12
13
14
15
16
# File 'lib/osctl/image/osctld_client.rb', line 11

def batch
  @batch = true
  yield(self)
ensure
  @batch = false
end

#bind_mount(ctid, src, dst) ⇒ Object

Parameters:

  • ctid (String)
  • src (String)
  • dst (String)


201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/osctl/image/osctld_client.rb', line 201

def bind_mount(ctid, src, dst)
  connect do |client|
    client.cmd_data!(
      :ct_mount_create,
      id: ctid,
      fs: src,
      mountpoint: dst,
      type: 'bind',
      opts: 'bind,create=dir',
      automount: false,
    )
  end
end

#connect {|client| ... } ⇒ Object (protected)

Yield Parameters:

  • client (OsCtl::Client)


264
265
266
267
268
269
270
271
272
273
# File 'lib/osctl/image/osctld_client.rb', line 264

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

Parameters:

  • ctid (String)
  • file (String)


63
64
65
66
67
68
69
70
71
# File 'lib/osctl/image/osctld_client.rb', line 63

def create_container_from_file(ctid, file)
  connect do |client|
    client.cmd_data!(
      :ct_import,
      as_id: ctid,
      file: File.absolute_path(file),
    )
  end
end

#create_container_from_repo(ctid, distribution, version, arch, vendor, variant) ⇒ Object

Parameters:

  • ctid (String)
  • distribution (String)
  • version (String)
  • arch (String)
  • vendor (String)
  • variant (String)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/osctl/image/osctld_client.rb', line 45

def create_container_from_repo(ctid, distribution, version, arch, vendor, variant)
  connect do |client|
    client.cmd_data!(
      :ct_create,
      id: ctid,
      image: {
        distribution: distribution,
        version: version,
        arch: arch,
        vendor: vendor,
        variant: variant,
      },
    )
  end
end

#delete_container(ctid) ⇒ Object

Parameters:

  • ctid (String)


178
179
180
181
182
# File 'lib/osctl/image/osctld_client.rb', line 178

def delete_container(ctid)
  connect do |client|
    client.cmd_data!(:ct_delete, id: ctid, force: true)
  end
end

#delete_user(user) ⇒ Object

Parameters:

  • user (String)


253
254
255
256
257
258
259
260
# File 'lib/osctl/image/osctld_client.rb', line 253

def delete_user(user)
  connect do |client|
    client.cmd_data!(
      :user_delete,
      name: user,
    )
  end
end

#exec(ctid, cmd) ⇒ Object

Parameters:

  • ctid (String)
  • cmd (Array<String>)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/osctl/image/osctld_client.rb', line 113

def exec(ctid, cmd)
  connect do |client|
    cont = client.cmd_data!(
      :ct_exec,
      id: ctid,
      cmd: cmd,
      run: false,
    )

    if cont != 'continue'
      fail "exec not available: invalid response '#{cont}'"
    end

    null = File.open('/dev/null', 'r')

    client.send_io(null)
    client.send_io(STDOUT)
    client.send_io(STDOUT)

    null.close

    resp = client.receive_resp

    if resp.error?
      fail (resp['message'] || 'exec failed')
    end

    resp[:exitstatus]
  end
end

#find_container(ctid) ⇒ Object

Parameters:

  • ctid (String)


31
32
33
34
35
36
37
# File 'lib/osctl/image/osctld_client.rb', line 31

def find_container(ctid)
  connect do |client|
    client.cmd_data!(:ct_show, id: ctid)
  end
rescue OsCtl::Client::Error
  nil
end

#ignore_errorObject



18
19
20
21
# File 'lib/osctl/image/osctld_client.rb', line 18

def ignore_error
  yield(self)
rescue OsCtl::Client::Error
end

#list_containersArray

Returns:

  • (Array)


24
25
26
27
28
# File 'lib/osctl/image/osctld_client.rb', line 24

def list_containers
  connect do |client|
    client.cmd_data!(:ct_list)
  end
end

#reinstall_container_from_image(ctid, image_path, remove_snapshots: false) ⇒ Object

Parameters:

  • ctid (String)
  • image_path (String)
  • remove_snapshots (Boolean) (defaults to: false)


76
77
78
79
80
81
82
83
84
85
86
# File 'lib/osctl/image/osctld_client.rb', line 76

def reinstall_container_from_image(ctid, image_path, remove_snapshots: false)
  connect do |client|
    client.cmd_data!(
      :ct_reinstall,
      id: ctid,
      remove_snapshots: remove_snapshots,
      type: :image,
      path: File.absolute_path(image_path),
    )
  end
end

#runscript(ctid, script) ⇒ Object

Parameters:

  • ctid (String)
  • script (String)


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/osctl/image/osctld_client.rb', line 146

def runscript(ctid, script)
  connect do |client|
    cont = client.cmd_data!(
      :ct_runscript,
      id: ctid,
      script: script,
      run: false,
    )

    if cont != 'continue'
      fail "runscript not available: invalid response '#{cont}'"
    end

    null = File.open('/dev/null', 'r')

    client.send_io(null)
    client.send_io(STDOUT)
    client.send_io(STDOUT)

    null.close

    resp = client.receive_resp

    if resp.error?
      fail (resp['message'] || 'runscript failed')
    end

    resp[:exitstatus]
  end
end

#set_container_attr(ctid, attr, value) ⇒ Object

Parameters:

  • ctid (String)
  • attr (String)
  • value (String)


91
92
93
94
95
# File 'lib/osctl/image/osctld_client.rb', line 91

def set_container_attr(ctid, attr, value)
  connect do |client|
    client.cmd_data!(:ct_set, id: ctid, attrs: {attr => value})
  end
end

#start_container(ctid) ⇒ Object

Parameters:

  • ctid (String)


98
99
100
101
102
# File 'lib/osctl/image/osctld_client.rb', line 98

def start_container(ctid)
  connect do |client|
    client.cmd_data!(:ct_start, id: ctid)
  end
end

#stop_container(ctid) ⇒ Object

Parameters:

  • ctid (String)


105
106
107
108
109
# File 'lib/osctl/image/osctld_client.rb', line 105

def stop_container(ctid)
  connect do |client|
    client.cmd_data!(:ct_stop, id: ctid)
  end
end

#unmount(ctid, dst) ⇒ Object

Parameters:

  • ctid (String)
  • dst (String)


229
230
231
232
233
234
235
236
237
# File 'lib/osctl/image/osctld_client.rb', line 229

def unmount(ctid, dst)
  connect do |client|
    client.cmd_data!(
      :ct_mount_delete,
      id: ctid,
      mountpoint: dst,
    )
  end
end

#user_idmap(user) ⇒ Array

Parameters:

  • user (String)

Returns:

  • (Array)


241
242
243
244
245
246
247
248
249
250
# File 'lib/osctl/image/osctld_client.rb', line 241

def user_idmap(user)
  connect do |client|
    client.cmd_data!(
      :user_idmap_list,
      name: user,
      uid: true,
      gid: true,
    )
  end
end