Class: Makit::Podman::Podman

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/podman/podman.rb

Instance Method Summary collapse

Constructor Details

#initialize(podman_executable: "podman") ⇒ Podman

Returns a new instance of Podman.



8
9
10
# File 'lib/makit/podman/podman.rb', line 8

def initialize(podman_executable: "podman")
  @service = PodmanServiceImpl.new(podman_executable: podman_executable)
end

Instance Method Details

#create_container(name:, image:, **options) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
# File 'lib/makit/podman/podman.rb', line 77

def create_container(name:, image:, **options)
  request = if grpc_available?
    Makit::V1::Podman::Service::CreateContainerRequest.new(
      name: name,
      image: image,
      command: options[:command] || [],
      args: options[:args] || [],
      environment: options[:environment] || {},
      volume_mounts: options[:volume_mounts] || [],
      port_mappings: options[:port_mappings] || [],
      working_directory: options[:working_directory],
      user: options[:user],
      privileged: options[:privileged] || false,
      capabilities: options[:capabilities] || [],
      labels: options[:labels] || {},
      auto_remove: options[:auto_remove] || false,
      memory_limit: options[:memory_limit] || 0,
      cpu_limit: options[:cpu_limit] || 0
    )
  else
    {
      name: name,
      image: image,
      command: options[:command] || [],
      args: options[:args] || [],
      environment: options[:environment] || {},
      volume_mounts: options[:volume_mounts] || [],
      port_mappings: options[:port_mappings] || [],
      working_directory: options[:working_directory],
      user: options[:user],
      privileged: options[:privileged] || false,
      capabilities: options[:capabilities] || [],
      labels: options[:labels] || {},
      auto_remove: options[:auto_remove] || false,
      memory_limit: options[:memory_limit] || 0,
      cpu_limit: options[:cpu_limit] || 0
    }
  end
  
  @service.create_container(request)
end

#create_port_mapping(host_port:, container_port:, protocol: "tcp") ⇒ Object



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/makit/podman/podman.rb', line 435

def create_port_mapping(host_port:, container_port:, protocol: "tcp")
  if grpc_available?
    Makit::V1::Podman::Service::PortMapping.new(
      host_port: host_port,
      container_port: container_port,
      protocol: protocol
    )
  else
    {
      host_port: host_port,
      container_port: container_port,
      protocol: protocol
    }
  end
end

#create_volume(name:, **options) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/makit/podman/podman.rb', line 356

def create_volume(name:, **options)
  request = if grpc_available?
    Makit::V1::Podman::Service::CreateVolumeRequest.new(
      name: name,
      driver: options[:driver],
      labels: options[:labels] || {},
      options: options[:options] || {}
    )
  else
    {
      name: name,
      driver: options[:driver],
      labels: options[:labels] || {},
      options: options[:options] || {}
    }
  end
  
  @service.create_volume(request)
end

#create_volume_mount(host_path:, container_path:, mode: "rw") ⇒ Object

Helper methods for creating common objects



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/makit/podman/podman.rb', line 419

def create_volume_mount(host_path:, container_path:, mode: "rw")
  if grpc_available?
    Makit::V1::Podman::Service::VolumeMount.new(
      host_path: host_path,
      container_path: container_path,
      mode: mode
    )
  else
    {
      host_path: host_path,
      container_path: container_path,
      mode: mode
    }
  end
end

#execute_command_in_container(container_id, command, **options) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/makit/podman/podman.rb', line 328

def execute_command_in_container(container_id, command, **options)
  request = if grpc_available?
    Makit::V1::Podman::Service::ExecuteCommandRequest.new(
      container_id: container_id,
      command: command,
      environment: options[:environment] || [],
      working_directory: options[:working_directory],
      user: options[:user],
      timeout_seconds: options[:timeout] || 300,
      capture_output: options[:capture_output] != false,
      capture_errors: options[:capture_errors] != false
    )
  else
    {
      container_id: container_id,
      command: command,
      environment: options[:environment] || [],
      working_directory: options[:working_directory],
      user: options[:user],
      timeout_seconds: options[:timeout] || 300,
      capture_output: options[:capture_output] != false,
      capture_errors: options[:capture_errors] != false
    }
  end
  
  @service.execute_command(request)
end

#execute_script_in_container(container_id, script_content, **options) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/makit/podman/podman.rb', line 300

def execute_script_in_container(container_id, script_content, **options)
  request = if grpc_available?
    Makit::V1::Podman::Service::ExecuteScriptRequest.new(
      container_id: container_id,
      script_content: script_content,
      environment: options[:environment] || [],
      working_directory: options[:working_directory],
      user: options[:user],
      timeout_seconds: options[:timeout] || 300,
      capture_output: options[:capture_output] != false,
      capture_errors: options[:capture_errors] != false
    )
  else
    {
      container_id: container_id,
      script_content: script_content,
      environment: options[:environment] || [],
      working_directory: options[:working_directory],
      user: options[:user],
      timeout_seconds: options[:timeout] || 300,
      capture_output: options[:capture_output] != false,
      capture_errors: options[:capture_errors] != false
    }
  end
  
  @service.execute_script(request)
end

#health_checkObject



406
407
408
# File 'lib/makit/podman/podman.rb', line 406

def health_check
  @service.health_check(nil)
end

#inspect_container(container_id) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/makit/podman/podman.rb', line 181

def inspect_container(container_id)
  request = if grpc_available?
    Makit::V1::Podman::Service::InspectContainerRequest.new(
      container_id: container_id
    )
  else
    {
      container_id: container_id
    }
  end
  
  @service.inspect_container(request)
end

#inspect_image(image_id) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/makit/podman/podman.rb', line 63

def inspect_image(image_id)
  request = if grpc_available?
    Makit::V1::Podman::Service::InspectImageRequest.new(
      image_id: image_id
    )
  else
    {
      image_id: image_id
    }
  end
  
  @service.inspect_image(request)
end

#list_containers(all: false, filter: nil) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/makit/podman/podman.rb', line 165

def list_containers(all: false, filter: nil)
  request = if grpc_available?
    Makit::V1::Podman::Service::ListContainersRequest.new(
      all: all,
      filter: filter
    )
  else
    {
      all: all,
      filter: filter
    }
  end
  
  @service.list_containers(request)
end

#list_images(all: false, filter: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/makit/podman/podman.rb', line 31

def list_images(all: false, filter: nil)
  request = if grpc_available?
    Makit::V1::Podman::Service::ListImagesRequest.new(
      all: all,
      filter: filter
    )
  else
    {
      all: all,
      filter: filter
    }
  end
  
  @service.list_images(request)
end

#list_volumes(filter: nil) ⇒ Object



376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/makit/podman/podman.rb', line 376

def list_volumes(filter: nil)
  request = if grpc_available?
    Makit::V1::Podman::Service::ListVolumesRequest.new(
      filter: filter
    )
  else
    {
      filter: filter
    }
  end
  
  @service.list_volumes(request)
end

#pull_image(image_name, tag: "latest", force: false) ⇒ Object

High-level convenience methods



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/makit/podman/podman.rb', line 13

def pull_image(image_name, tag: "latest", force: false)
  request = if grpc_available?
    Makit::V1::Podman::Service::PullImageRequest.new(
      image_name: image_name,
      tag: tag,
      force: force
    )
  else
    {
      image_name: image_name,
      tag: tag,
      force: force
    }
  end
  
  @service.pull_image(request)
end

#remove_container(container_id, force: false) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/makit/podman/podman.rb', line 149

def remove_container(container_id, force: false)
  request = if grpc_available?
    Makit::V1::Podman::Service::RemoveContainerRequest.new(
      container_id: container_id,
      force: force
    )
  else
    {
      container_id: container_id,
      force: force
    }
  end
  
  @service.remove_container(request)
end

#remove_image(image_id, force: false) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/makit/podman/podman.rb', line 47

def remove_image(image_id, force: false)
  request = if grpc_available?
    Makit::V1::Podman::Service::RemoveImageRequest.new(
      image_id: image_id,
      force: force
    )
  else
    {
      image_id: image_id,
      force: force
    }
  end
  
  @service.remove_image(request)
end

#remove_volume(volume_name, force: false) ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/makit/podman/podman.rb', line 390

def remove_volume(volume_name, force: false)
  request = if grpc_available?
    Makit::V1::Podman::Service::RemoveVolumeRequest.new(
      volume_name: volume_name,
      force: force
    )
  else
    {
      volume_name: volume_name,
      force: force
    }
  end
  
  @service.remove_volume(request)
end

#run_command(image, command, **options) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/makit/podman/podman.rb', line 258

def run_command(image, command, **options)
  request = if grpc_available?
    Makit::V1::Podman::Service::RunContainerRequest.new(
      image: image,
      command: command,
      args: options[:args] || [],
      environment: options[:environment] || {},
      volume_mounts: options[:volume_mounts] || [],
      port_mappings: options[:port_mappings] || [],
      working_directory: options[:working_directory] || "/tmp",
      user: options[:user],
      auto_remove: options[:auto_remove] != false,
      interactive: options[:interactive] || false,
      tty: options[:tty] || false,
      timeout_seconds: options[:timeout] || 300,
      capture_output: options[:capture_output] != false,
      capture_errors: options[:capture_errors] != false,
      labels: options[:labels] || {}
    )
  else
    {
      image: image,
      command: command,
      args: options[:args] || [],
      environment: options[:environment] || {},
      volume_mounts: options[:volume_mounts] || [],
      port_mappings: options[:port_mappings] || [],
      working_directory: options[:working_directory] || "/tmp",
      user: options[:user],
      auto_remove: options[:auto_remove] != false,
      interactive: options[:interactive] || false,
      tty: options[:tty] || false,
      timeout_seconds: options[:timeout] || 300,
      capture_output: options[:capture_output] != false,
      capture_errors: options[:capture_errors] != false,
      labels: options[:labels] || {}
    }
  end
  
  @service.run_container(request)
end

#run_script(image, script_content, **options) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/makit/podman/podman.rb', line 195

def run_script(image, script_content, **options)
  # Create container

  container_request = if grpc_available?
    Makit::V1::Podman::Service::CreateContainerRequest.new(
      name: options[:name] || "makit-#{SecureRandom.uuid}",
      image: image,
      command: ["sleep", "infinity"],
      environment: options[:environment] || {},
      volume_mounts: options[:volume_mounts] || [],
      working_directory: options[:working_directory] || "/tmp",
      auto_remove: false
    )
  else
    {
      name: options[:name] || "makit-#{SecureRandom.uuid}",
      image: image,
      command: ["sleep", "infinity"],
      environment: options[:environment] || {},
      volume_mounts: options[:volume_mounts] || [],
      working_directory: options[:working_directory] || "/tmp",
      auto_remove: false
    }
  end
  
  container_response = @service.create_container(container_request)
  return container_response unless container_response.respond_to?(:success) ? container_response.success : container_response[:success]
  
  # Start container

  container_id = container_response.respond_to?(:container_id) ? container_response.container_id : container_response[:container_id]
  start_response = start_container(container_id)
  return start_response unless start_response.respond_to?(:success) ? start_response.success : start_response[:success]
  
  # Execute script

  script_request = if grpc_available?
    Makit::V1::Podman::Service::ExecuteScriptRequest.new(
      container_id: container_response.respond_to?(:container_id) ? container_response.container_id : container_response[:container_id],
      script_content: script_content,
      environment: options[:environment] || [],
      working_directory: options[:working_directory] || "/tmp",
      timeout_seconds: options[:timeout] || 300,
      capture_output: options[:capture_output] != false,
      capture_errors: options[:capture_errors] != false
    )
  else
    {
      container_id: container_response.respond_to?(:container_id) ? container_response.container_id : container_response[:container_id],
      script_content: script_content,
      environment: options[:environment] || [],
      working_directory: options[:working_directory] || "/tmp",
      timeout_seconds: options[:timeout] || 300,
      capture_output: options[:capture_output] != false,
      capture_errors: options[:capture_errors] != false
    }
  end
  
  result = @service.execute_script(script_request)
  
  # Cleanup container

  remove_container(container_response.respond_to?(:container_id) ? container_response.container_id : container_response[:container_id], force: true)
  
  result
end

#start_container(container_id) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/makit/podman/podman.rb', line 119

def start_container(container_id)
  request = if grpc_available?
    Makit::V1::Podman::Service::StartContainerRequest.new(
      container_id: container_id
    )
  else
    {
      container_id: container_id
    }
  end
  
  @service.start_container(request)
end

#stop_container(container_id, timeout_seconds: 10) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/makit/podman/podman.rb', line 133

def stop_container(container_id, timeout_seconds: 10)
  request = if grpc_available?
    Makit::V1::Podman::Service::StopContainerRequest.new(
      container_id: container_id,
      timeout_seconds: timeout_seconds
    )
  else
    {
      container_id: container_id,
      timeout_seconds: timeout_seconds
    }
  end
  
  @service.stop_container(request)
end

#system_infoObject



414
415
416
# File 'lib/makit/podman/podman.rb', line 414

def system_info
  @service.get_system_info(nil)
end

#versionObject



410
411
412
# File 'lib/makit/podman/podman.rb', line 410

def version
  @service.get_version(nil)
end