Class: OsCtl::Cli::Container

Inherits:
Command
  • Object
show all
Includes:
Assets, Attributes, CGroupParams, Devices
Defined in:
lib/osctl/cli/container.rb

Constant Summary collapse

FIELDS =
%i(
  pool
  id
  user
  group
  dataset
  rootfs
  lxc_path
  lxc_dir
  group_path
  distribution
  version
  state
  init_pid
  autostart
  autostart_priority
  autostart_delay
  ephemeral
  hostname
  dns_resolvers
  nesting
  seccomp_profile
  loadavg
) + CGroupParams::CGPARAM_STATS
FILTERS =
%i(
  pool
  user
  group
  distribution
  version
  state
)
DEFAULT_FIELDS =
%i(
  pool
  id
  user
  group
  distribution
  version
  state
  init_pid
  memory
  cpu_time
)
PRLIMIT_FIELDS =
%i(
  name
  soft
  hard
)
DATASET_FIELDS =
%i(
  name
  dataset
)
MOUNT_FIELDS =
%i(
  fs
  dataset
  mountpoint
  type
  opts
  automount
  temporary
)

Constants included from Devices

Devices::DEVICES_FIELDS

Constants included from CGroupParams

OsCtl::Cli::CGroupParams::CGPARAM_DEFAULT_FIELDS, OsCtl::Cli::CGroupParams::CGPARAM_FIELDS, OsCtl::Cli::CGroupParams::CGPARAM_FILTERS, OsCtl::Cli::CGroupParams::CGPARAM_STATS

Instance Attribute Summary

Attributes inherited from Command

#args, #gopts, #opts

Instance Method Summary collapse

Methods included from Attributes

#do_set_attr, #do_unset_attr

Methods included from Assets

#print_assets

Methods included from Devices

#do_device_add, #do_device_chmod, #do_device_delete, #do_device_inherit, #do_device_list, #do_device_promote, #do_device_replace, #do_device_set_inherit, #do_device_unset_inherit

Methods included from CGroupParams

#cg_add_stats, #cg_blkio_stats, #cg_read_stats, #do_cgparam_apply, #do_cgparam_list, #do_cgparam_replace, #do_cgparam_set, #do_cgparam_unset, #do_set_cpu_limit, #do_set_memory, #do_unset_cpu_limit, #do_unset_memory, #parse_cgparams, #parse_memory_stat, #parse_subsystem, #read_cgparam, #read_memory_usage

Methods inherited from Command

#cli_opt, #format_output, #initialize, #osctld_call, #osctld_fmt, #osctld_open, #osctld_resp, #require_args!, run

Methods included from Utils::Humanize

#break_interval, #format_long_duration, #format_percent, #format_short_duration, #humanize_data, #humanize_time_ns, #parse_data

Constructor Details

This class inherits a constructor from OsCtl::Cli::Command

Instance Method Details

#add_loadavg(ct) ⇒ Object (protected)



1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
# File 'lib/osctl/cli/container.rb', line 1123

def add_loadavg(ct)
  if ct[:state] != 'running'
    ct[:loadavg] = nil
    return
  end

  lavgs = OsCtl::Lib::LoadAvgReader.read_for(["#{ct[:pool]}:#{ct[:id]}"])

  if lavgs.empty?
    ct[:loadavg] = nil

  else
    ct[:loadavg] = lavgs.first.averages
  end
end

#add_loadavgs(cts) ⇒ Object (protected)



1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
# File 'lib/osctl/cli/container.rb', line 1139

def add_loadavgs(cts)
  lavgs = OsCtl::Lib::LoadAvgReader.read_all_hash

  cts.each do |ct|
    if ct[:state] != 'running'
      ct[:loadavg] = nil
      next
    end

    lavg = lavgs[ "#{ct[:pool]}:#{ct[:id]}" ]

    if lavg
      ct[:loadavg] = lavg.averages

    else
      ct[:loadavg] = nil
    end
  end
end

#assetsObject



680
681
682
683
684
# File 'lib/osctl/cli/container.rb', line 680

def assets
  require_args!('id')

  print_assets(:ct_assets, id: args[0], pool: gopts[:pool])
end

#attachObject



294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/osctl/cli/container.rb', line 294

def attach
  require_args!('id')

  cmd = osctld_call(
    :ct_attach,
    id: args[0],
    pool: gopts[:pool],
    user_shell: opts['user-shell'],
  )

  handle_ct_attach(cmd)
end

#cdObject



614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# File 'lib/osctl/cli/container.rb', line 614

def cd
  require_args!('id')

  ct = osctld_call(:ct_show, id: args[0], pool: gopts[:pool])

  if opts[:runtime]
    raise "container not running" unless ct[:init_pid]
    path = File.join('/proc/', ct[:init_pid].to_s, 'root', '/')

  elsif opts[:lxc]
    path = ct[:lxc_dir]

  else
    path = ct[:rootfs]
  end

  file = Tempfile.new('osctl-rcfile')
  file.write(<<-END
    export PS1="(CT #{ct[:id]}) $PS1"
    cd "#{path}"
    END
  )
  file.close

  puts "Spawning a new shell, exit when done"
  pid = Process.spawn(ENV['SHELL'] || 'bash', '--rcfile', file.path)
  Process.wait(pid)

  file.unlink
end

#cgparam_applyObject



752
753
754
755
# File 'lib/osctl/cli/container.rb', line 752

def cgparam_apply
  require_args!('id')
  do_cgparam_apply(:ct_cgparam_apply, id: args[0], pool: gopts[:pool])
end

#cgparam_listObject



736
737
738
739
740
# File 'lib/osctl/cli/container.rb', line 736

def cgparam_list
  require_args!('id')

  do_cgparam_list(:ct_cgparam_list, id: args[0], pool: gopts[:pool])
end

#cgparam_replaceObject



757
758
759
760
# File 'lib/osctl/cli/container.rb', line 757

def cgparam_replace
  require_args!('name')
  do_cgparam_replace(:ct_cgparam_replace, id: args[0], pool: gopts[:pool])
end

#cgparam_setObject



742
743
744
745
# File 'lib/osctl/cli/container.rb', line 742

def cgparam_set
  require_args!('id', 'parameter', 'value')
  do_cgparam_set(:ct_cgparam_set, id: args[0], pool: gopts[:pool])
end

#cgparam_unsetObject



747
748
749
750
# File 'lib/osctl/cli/container.rb', line 747

def cgparam_unset
  require_args!('id', 'parameter')
  do_cgparam_unset(:ct_cgparam_unset, id: args[0], pool: gopts[:pool])
end

#chgrpObject



536
537
538
539
540
541
542
543
544
545
# File 'lib/osctl/cli/container.rb', line 536

def chgrp
  require_args!('id', 'group')
  osctld_fmt(
    :ct_chgrp,
    id: args[0],
    pool: gopts[:pool],
    group: args[1],
    missing_devices: opts['missing-devices']
  )
end

#chownObject



531
532
533
534
# File 'lib/osctl/cli/container.rb', line 531

def chown
  require_args!('id', 'user')
  osctld_fmt(:ct_chown, id: args[0], pool: gopts[:pool], user: args[1])
end

#config_reloadObject



547
548
549
550
551
552
553
554
# File 'lib/osctl/cli/container.rb', line 547

def config_reload
  require_args!('id')
  osctld_fmt(
    :ct_cfg_reload,
    id: args[0],
    pool: gopts[:pool],
  )
end

#config_replaceObject



556
557
558
559
560
561
562
563
564
# File 'lib/osctl/cli/container.rb', line 556

def config_replace
  require_args!('id')
  osctld_fmt(
    :ct_cfg_replace,
    id: args[0],
    pool: gopts[:pool],
    config: STDIN.read,
  )
end

#consoleObject



288
289
290
291
292
# File 'lib/osctl/cli/container.rb', line 288

def console
  require_args!('id')

  open_console(args[0], gopts[:pool], opts[:tty], gopts[:json])
end

#copyObject



482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/osctl/cli/container.rb', line 482

def copy
  require_args!('id', 'new-id')

  if args[1].include?(':')
    target_pool, target_id = args[1].split(':')
  else
    target_pool = opts[:pool]
    target_id = args[1]
  end

  cmd_opts = {
    pool: gopts[:pool],
    id: args[0],
    target_pool: target_pool,
    target_id: target_id,
    consistent: opts[:consistent],
  }

  cmd_opts[:target_user] = opts[:user] if opts[:user]
  cmd_opts[:target_group] = opts[:group] if opts[:group]
  cmd_opts[:target_dataset] = opts[:dataset] if opts[:dataset]

  osctld_fmt(:ct_copy, cmd_opts)
end

#createObject



160
161
162
163
164
165
166
167
168
# File 'lib/osctl/cli/container.rb', line 160

def create
  require_args!('id')

  if opts['skip-image']
    create_empty
  else
    create_with_remote_image
  end
end

#create_emptyObject (protected)



1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
# File 'lib/osctl/cli/container.rb', line 1062

def create_empty
  if !opts[:distribution]
    raise GLI::BadCommandLine, 'provide --distribution'
  elsif !opts[:version]
    raise GLI::BadCommandLine, 'provide --version'
  elsif !opts[:arch]
    raise GLI::BadCommandLine, 'provide --arch'
  end

  cmd_opts = {
    id: args[0],
    pool: opts[:pool] || gopts[:pool],
    user: opts[:user],
    distribution: opts[:distribution],
    version: opts[:version],
    arch: opts[:arch],
  }

  %i(group dataset).each do |v|
    cmd_opts[v] = opts[v] if opts[v]
  end

  osctld_fmt(:ct_create_empty, cmd_opts)
end

#create_with_remote_imageObject (protected)



1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
# File 'lib/osctl/cli/container.rb', line 1041

def create_with_remote_image
  cmd_opts = {
    id: args[0],
    pool: opts[:pool] || gopts[:pool],
    user: opts[:user],
    repository: opts[:repository],
  }

  %i(group dataset).each do |v|
    cmd_opts[v] = opts[v] if opts[v]
  end

  if !opts[:distribution]
    raise GLI::BadCommandLine, 'provide --distribution'
  end

  cmd_opts[:image] = repo_image_attrs

  osctld_fmt(:ct_create, cmd_opts)
end

#dataset_createObject



883
884
885
886
887
888
889
890
891
892
893
# File 'lib/osctl/cli/container.rb', line 883

def dataset_create
  require_args!('id', 'name')
  osctld_fmt(
    :ct_dataset_create,
    id: args[0],
    pool: gopts[:pool],
    name: args[1],
    mount: opts[:mount],
    mountpoint: args[2]
  )
end

#dataset_deleteObject



895
896
897
898
899
900
901
902
903
904
905
# File 'lib/osctl/cli/container.rb', line 895

def dataset_delete
  require_args!('id', 'name')
  osctld_fmt(
    :ct_dataset_delete,
    id: args[0],
    pool: gopts[:pool],
    name: args[1],
    recursive: opts[:recursive],
    unmount: opts[:unmount]
  )
end

#dataset_listObject



859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
# File 'lib/osctl/cli/container.rb', line 859

def dataset_list
  if opts[:list]
    puts DATASET_FIELDS.join("\n")
    return
  end

  require_args!('id')
  props = args[1..-1]

  cmd_opts = {id: args[0], pool: gopts[:pool], properties: props}
  fmt_opts = {layout: :columns}

  fmt_opts[:header] = false if opts['hide-header']

  if opts[:output]
    cols = (opts[:output].split(',') + props).map(&:to_sym)

  else
    cols = nil
  end

  osctld_fmt(:ct_dataset_list, cmd_opts, cols, fmt_opts)
end

#deleteObject



170
171
172
173
174
# File 'lib/osctl/cli/container.rb', line 170

def delete
  require_args!('id')

  osctld_fmt(:ct_delete, id: args[0], pool: gopts[:pool], force: opts[:force])
end

#device_addObject



767
768
769
770
# File 'lib/osctl/cli/container.rb', line 767

def device_add
  require_args!('id', 'type', 'major', 'minor', 'mode')
  do_device_add(:ct_device_add, id: args[0], pool: gopts[:pool])
end

#device_chmodObject



777
778
779
780
# File 'lib/osctl/cli/container.rb', line 777

def device_chmod
  require_args!('id', 'type', 'major', 'minor', 'mode')
  do_device_chmod(:ct_device_chmod, id: args[0], pool: gopts[:pool])
end

#device_deleteObject



772
773
774
775
# File 'lib/osctl/cli/container.rb', line 772

def device_delete
  require_args!('id', 'type', 'major', 'minor')
  do_device_delete(:ct_device_delete, id: args[0], pool: gopts[:pool])
end

#device_inheritObject



787
788
789
790
# File 'lib/osctl/cli/container.rb', line 787

def device_inherit
  require_args!('id', 'type', 'major', 'minor')
  do_device_inherit(:ct_device_inherit, id: args[0], pool: gopts[:pool])
end

#device_listObject



762
763
764
765
# File 'lib/osctl/cli/container.rb', line 762

def device_list
  require_args!('id')
  do_device_list(:ct_device_list, id: args[0], pool: gopts[:pool])
end

#device_promoteObject



782
783
784
785
# File 'lib/osctl/cli/container.rb', line 782

def device_promote
  require_args!('id', 'type', 'major', 'minor')
  do_device_chmod(:ct_device_promote, id: args[0], pool: gopts[:pool])
end

#device_replaceObject



802
803
804
805
# File 'lib/osctl/cli/container.rb', line 802

def device_replace
  require_args!('id')
  do_device_replace(:ct_device_replace, id: args[0], pool: gopts[:pool])
end

#device_set_inheritObject



792
793
794
795
# File 'lib/osctl/cli/container.rb', line 792

def device_set_inherit
  require_args!('id', 'type', 'major', 'minor')
  do_device_set_inherit(:ct_device_set_inherit, id: args[0], pool: gopts[:pool])
end

#device_unset_inheritObject



797
798
799
800
# File 'lib/osctl/cli/container.rb', line 797

def device_unset_inherit
  require_args!('id', 'type', 'major', 'minor')
  do_device_unset_inherit(:ct_device_unset_inherit, id: args[0], pool: gopts[:pool])
end

#execObject



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/osctl/cli/container.rb', line 307

def exec
  require_args!('id', 'command')

  c = osctld_open
  cont = c.cmd_data!(
    :ct_exec,
    id: args[0],
    pool: gopts[:pool],
    cmd: args[1..-1],
    run: opts['run-container'],
    network: opts['network'],
  )

  if cont != 'continue'
    warn "exec not available: invalid response '#{cont}'"
    exit(false)
  end

  handle_exec_io(c)
end

#exportObject



586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/osctl/cli/container.rb', line 586

def export
  require_args!('id', 'file')

  osctld_fmt(
    :ct_export,
    id: args[0],
    pool: gopts[:pool],
    file: File.expand_path(args[1]),
    consistent: opts[:consistent],
    compression: opts[:compression]
  )
end

#get_ct_wait(v) ⇒ Object (protected)



1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
# File 'lib/osctl/cli/container.rb', line 1159

def get_ct_wait(v)
  if v < 0
    raise GLI::BadCommandLine, 'invalid value for --wait'
  elsif v == 0
    false
  elsif opts[:foreground]
    false
  else
    v
  end
end

#handle_ct_attach(cmd) ⇒ Object (protected)



1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
# File 'lib/osctl/cli/container.rb', line 1235

def handle_ct_attach(cmd)
  f = Tempfile.create(['osctl-ct-attach-settings', '.json'], '/tmp')
  f.puts(cmd[:settings].to_json)
  f.close

  pid = Process.fork do
    cmd[:env].each do |k, v|
      ENV[k.to_s] = v
    end

    Process.exec(cmd[:cmd], f.path, '--', *cmd[:args])
  end

  Process.wait(pid)

ensure
  begin
    f && File.unlink(f.path)
  rescue Errno::ENOENT
    # pass
  end
end

#handle_exec_io(c) ⇒ Object (protected)



1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
# File 'lib/osctl/cli/container.rb', line 1171

def handle_exec_io(c)
  r_in, w_in = IO.pipe
  r_out, w_out = IO.pipe
  r_err, w_err = IO.pipe

  c.send_io(r_in)
  c.send_io(w_out)
  c.send_io(w_err)

  r_in.close
  w_out.close
  w_err.close

  watch_ios = [STDIN, r_out, r_err, c.socket]

  loop do
    rs, ws, = IO.select(watch_ios)

    rs.each do |r|
      case r
      when r_out
        data = r.read_nonblock(4096)
        STDOUT.write(data)
        STDOUT.flush

      when r_err
        data = r.read_nonblock(4096)
        STDERR.write(data)
        STDERR.flush

      when STDIN
        begin
          data = r.read_nonblock(4096)
          w_in.write(data)
        rescue EOFError
          w_in.close
          watch_ios.delete(STDIN)
        end

      when c.socket
        r_out.close
        r_err.close

        handle_exec_response(c)
        return
      end
    end
  end

rescue IOError
  handle_exec_response(c)
end

#handle_exec_response(c) ⇒ Object (protected)



1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
# File 'lib/osctl/cli/container.rb', line 1224

def handle_exec_response(c)
  resp = c.receive_resp

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

  elsif resp[:exitstatus] && resp[:exitstatus] > 0
    raise GLI::CustomExit.new('executed command failed', resp[:exitstatus])
  end
end

#importObject



599
600
601
602
603
604
605
606
607
608
609
610
611
612
# File 'lib/osctl/cli/container.rb', line 599

def import
  require_args!('file')

  file = File.expand_path(args[0])
  fail "#{file}: not found" unless File.exist?(file)

  cmd_opts = {file: file}

  %w(as-id as-user as-group dataset missing-devices).each do |v|
    cmd_opts[v.sub('-', '_').to_sym] = opts[v] if opts[v]
  end

  osctld_fmt(:ct_import, cmd_opts)
end

#listObject



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
118
119
120
121
122
123
124
125
126
127
# File 'lib/osctl/cli/container.rb', line 86

def list
  if opts[:list]
    puts FIELDS.join("\n")
    return
  end

  cmd_opts = {}
  fmt_opts = {
    layout: :columns,
    sort: opts[:sort] && opts[:sort].split(',').map(&:to_sym),
  }

  FILTERS.each do |v|
    [gopts, opts].each do |options|
      next unless options[v]
      cmd_opts[v] = options[v].split(',')
    end
  end

  if opts[:ephemeral]
    cmd_opts[:ephemeral] = true
  elsif opts[:persistent]
    cmd_opts[:ephemeral] = false
  end

  cmd_opts[:ids] = args if args.count > 0
  fmt_opts[:header] = false if opts['hide-header']
  cols = opts[:output] ? opts[:output].split(',').map(&:to_sym) : DEFAULT_FIELDS

  c = osctld_open
  cts = cg_add_stats(
    c,
    c.cmd_data!(:ct_list, cmd_opts),
    lambda { |ct| ct[:group_path] },
    cols,
    gopts[:parsable]
  )

  add_loadavgs(cts)

  format_output(cts, cols, fmt_opts)
end

#log_catObject



645
646
647
648
649
650
651
652
653
# File 'lib/osctl/cli/container.rb', line 645

def log_cat
  require_args!('id')

  ct = osctld_call(:ct_show, id: args[0], pool: gopts[:pool])

  File.open(ct[:log_file]) do |f|
    puts f.readline until f.eof?
  end
end

#log_pathObject



655
656
657
658
659
660
# File 'lib/osctl/cli/container.rb', line 655

def log_path
  require_args!('id')

  ct = osctld_call(:ct_show, id: args[0], pool: gopts[:pool])
  puts ct[:log_file]
end

#mountObject



201
202
203
204
# File 'lib/osctl/cli/container.rb', line 201

def mount
  require_args!('id')
  osctld_fmt(:ct_mount, id: args[0], pool: gopts[:pool])
end

#mount_activateObject



987
988
989
990
991
992
993
994
995
996
# File 'lib/osctl/cli/container.rb', line 987

def mount_activate
  require_args!('id', 'mountpoint')

  osctld_fmt(
    :ct_mount_activate,
    id: args[0],
    pool: gopts[:pool],
    mountpoint: args[1]
  )
end

#mount_createObject



930
931
932
933
934
935
936
937
938
939
940
941
942
943
# File 'lib/osctl/cli/container.rb', line 930

def mount_create
  require_args!('id')

  osctld_fmt(
    :ct_mount_create,
    id: args[0],
    pool: gopts[:pool],
    fs: opts[:fs],
    mountpoint: opts[:mountpoint],
    type: opts[:type],
    opts: opts[:opts],
    automount: opts[:automount],
  )
end

#mount_datasetObject



945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
# File 'lib/osctl/cli/container.rb', line 945

def mount_dataset
  require_args!('id', 'name', 'mountpoint')

  if opts[:ro] && opts[:rw]
    raise GLI::BadCommandLine, 'use either --ro or --rw, not both'

  elsif opts[:ro]
    mode = 'ro'

  elsif opts[:rw]
    mode = 'rw'

  else
    mode = 'rw'
  end

  osctld_fmt(
    :ct_mount_dataset,
    id: args[0],
    pool: gopts[:pool],
    name: args[1],
    mountpoint: args[2],
    mode: mode,
    automount: opts[:automount],
  )
end

#mount_deactivateObject



998
999
1000
1001
1002
1003
1004
1005
1006
1007
# File 'lib/osctl/cli/container.rb', line 998

def mount_deactivate
  require_args!('id', 'mountpoint')

  osctld_fmt(
    :ct_mount_deactivate,
    id: args[0],
    pool: gopts[:pool],
    mountpoint: args[1]
  )
end

#mount_deleteObject



1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
# File 'lib/osctl/cli/container.rb', line 1009

def mount_delete
  require_args!('id', 'mountpoint')

  osctld_fmt(
    :ct_mount_delete,
    id: args[0],
    pool: gopts[:pool],
    mountpoint: args[1]
  )
end

#mount_listObject



907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
# File 'lib/osctl/cli/container.rb', line 907

def mount_list
  if opts[:list]
    puts MOUNT_FIELDS.join("\n")
    return
  end

  require_args!('id')

  cmd_opts = {id: args[0], pool: gopts[:pool]}
  fmt_opts = {layout: :columns}

  fmt_opts[:header] = false if opts['hide-header']

  if opts[:output]
    cols = opts[:output].split(',').map(&:to_sym)

  else
    cols = MOUNT_FIELDS
  end

  osctld_fmt(:ct_mount_list, cmd_opts, cols, fmt_opts)
end

#mount_registerObject



972
973
974
975
976
977
978
979
980
981
982
983
984
985
# File 'lib/osctl/cli/container.rb', line 972

def mount_register
  require_args!('id', 'mountpoint')

  osctld_fmt(
    :ct_mount_register,
    id: args[0],
    pool: gopts[:pool],
    fs: opts[:fs],
    mountpoint: args[1],
    type: opts[:type],
    opts: opts[:opts],
    lock: !opts['on-ct-start'],
  )
end

#moveObject



507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/osctl/cli/container.rb', line 507

def move
  require_args!('id', 'new-id')

  if args[1].include?(':')
    target_pool, target_id = args[1].split(':')
  else
    target_pool = opts[:pool]
    target_id = args[1]
  end

  cmd_opts = {
    pool: gopts[:pool],
    id: args[0],
    target_pool: target_pool,
    target_id: target_id,
  }

  cmd_opts[:target_user] = opts[:user] if opts[:user]
  cmd_opts[:target_group] = opts[:group] if opts[:group]
  cmd_opts[:target_dataset] = opts[:dataset] if opts[:dataset]

  osctld_fmt(:ct_move, cmd_opts)
end

#open_console(ctid, pool, tty, raw, &block) ⇒ Object



686
687
688
689
690
691
692
693
# File 'lib/osctl/cli/container.rb', line 686

def open_console(ctid, pool, tty, raw, &block)
  if raw
    open_console_raw(ctid, pool, tty)

  else
    open_console_tty(ctid, pool, tty, &block)
  end
end

#open_console_raw(ctid, pool, tty) ⇒ Object



723
724
725
726
727
728
729
730
731
732
733
734
# File 'lib/osctl/cli/container.rb', line 723

def open_console_raw(ctid, pool, tty)
  c = osctld_open
  c.cmd_response!(:ct_console, id: ctid, pool: pool, tty: tty)

  console = OsCtl::Console.new(c.socket, STDIN, STDOUT, raw: true)

  Signal.trap('TERM') do
    console.close
  end

  console.open
end

#open_console_tty(ctid, pool, tty) {|c| ... } ⇒ Object

Yields:

  • (c)


695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'lib/osctl/cli/container.rb', line 695

def open_console_tty(ctid, pool, tty)
  c = osctld_open
  c.cmd_response!(:ct_console, id: ctid, pool: pool, tty: tty)

  puts "Press Ctrl+a q to detach the console"
  puts

  state = `stty -g`
  `stty raw -echo -icanon -isig`

  pid = Process.fork do
    console = OsCtl::Console.new(c.socket, STDIN, STDOUT)

    Signal.trap('WINCH') do
      console.resize(*STDIN.winsize)
    end

    console.open
  end

  yield(c) if block_given?

  Process.wait(pid)

  `stty #{state}`
  puts
end

#passwdObject



566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/osctl/cli/container.rb', line 566

def passwd
  require_args!('id', 'user')

  if args[2]
    password = args[2]

  else
    cli = HighLine.new
    password = cli.ask('Password: ') { |q| q.echo = false }.strip
  end

  osctld_fmt(
    :ct_passwd,
    id: args[0],
    pool: gopts[:pool],
    user: args[1],
    password: password
  )
end

#pidObject



667
668
669
670
671
672
673
674
675
676
677
678
# File 'lib/osctl/cli/container.rb', line 667

def pid
  require_args!('pid|-')

  finder = PidFinder.new(header: !opts['hide-header'])

  if args[0] == '-'
    finder.find(STDIN.readline.strip.to_i) until STDIN.eof?

  else
    args.each { |pid| finder.find(pid.to_i) }
  end
end

#prlimit_listObject



807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
# File 'lib/osctl/cli/container.rb', line 807

def prlimit_list
  if opts[:list]
    puts PRLIMIT_FIELDS.join("\n")
    return
  end

  require_args!('id')

  cmd_opts = {id: args[0], pool: gopts[:pool]}
  fmt_opts = {layout: :columns}

  cmd_opts[:limits] = args[1..-1] if args.count > 1
  fmt_opts[:header] = false if opts['hide-header']

  if opts[:output]
    cols = opts[:output].split(',').map(&:to_sym)

  else
    cols = PRLIMIT_FIELDS
  end

  data = osctld_call(:ct_prlimit_list, cmd_opts)
  format_output(data.map { |k, v| v.merge(name: k)}, cols, fmt_opts)
end

#prlimit_setObject



832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
# File 'lib/osctl/cli/container.rb', line 832

def prlimit_set
  require_args!('id', 'limit', 'value')

  soft, hard = args[2..3].map { |v| /^\d+$/ =~ v ? v.to_i : v }
  hard = soft if hard.nil?

  osctld_fmt(
    :ct_prlimit_set,
    id: args[0],
    pool: gopts[:pool],
    name: args[1],
    soft: soft,
    hard: hard
  )
end

#prlimit_unsetObject



848
849
850
851
852
853
854
855
856
857
# File 'lib/osctl/cli/container.rb', line 848

def prlimit_unset
  require_args!('id', 'limit')

  do_cgparam_unset(
    :ct_prlimit_unset,
    id: args[0],
    pool: gopts[:pool],
    name: args[1]
  )
end

#reconfigureObject



662
663
664
665
# File 'lib/osctl/cli/container.rb', line 662

def reconfigure
  require_args!('id')
  osctld_fmt(:ct_reconfigure, id: args[0], pool: gopts[:pool])
end

#recover_cleanupObject



1030
1031
1032
1033
1034
1035
1036
1037
1038
# File 'lib/osctl/cli/container.rb', line 1030

def recover_cleanup
  require_args!('id')

  osctld_fmt(
    :ct_recover_cleanup,
    id: args[0],
    pool: gopts[:pool],
  )
end

#recover_stateObject



1020
1021
1022
1023
1024
1025
1026
1027
1028
# File 'lib/osctl/cli/container.rb', line 1020

def recover_state
  require_args!('id')

  osctld_fmt(
    :ct_recover_state,
    id: args[0],
    pool: gopts[:pool],
  )
end

#reinstallObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/osctl/cli/container.rb', line 176

def reinstall
  require_args!('id')

  cmd_opts = {
    id: args[0],
    pool: opts[:pool] || gopts[:pool],
    repository: opts[:repository],
    remove_snapshots: opts['remove-snapshots'],
  }

  if opts['from-file']
    cmd_opts.update(
      type: :image,
      path: File.absolute_path(opts['from-file']),
    )
  else
    cmd_opts.update(
      type: :remote,
      image: repo_image_attrs(defaults: false),
    )
  end

  osctld_fmt(:ct_reinstall, cmd_opts)
end

#repo_image_attrs(defaults: true) ⇒ Object (protected)



1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
# File 'lib/osctl/cli/container.rb', line 1103

def repo_image_attrs(defaults: true)
  ret = {}

  if defaults
    ret[:vendor] ||= opts[:vendor] || 'default'
    ret[:variant] ||= opts[:variant] || 'default'
    ret[:arch] ||= opts[:arch] || `uname -m`.strip
    ret[:distribution] ||= opts[:distribution]
    ret[:version] ||= opts[:version] || 'stable'
  else
    ret[:vendor] ||= opts[:vendor]
    ret[:variant] ||= opts[:variant]
    ret[:arch] ||= opts[:arch]
    ret[:distribution] ||= opts[:distribution]
    ret[:version] ||= opts[:version]
  end

  ret
end

#restartObject



253
254
255
256
257
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
# File 'lib/osctl/cli/container.rb', line 253

def restart
  require_args!('id')

  if opts[:kill] && opts['dont-kill']
    raise GLI::BadCommandLine, '--kill and --dont-kill cannot be used together'

  elsif (opts[:kill] || opts['dont-kill']) && opts[:reboot]
    raise GLI::BadCommandLine, '--kill and --dont-kill cannot be used with --reboot'

  elsif opts[:kill]
    m = :kill

  elsif opts['dont-kill']
    m = :shutdown_or_fail

  else
    m = :shutdown_or_kill
  end

  cmd_opts = {
    id: args[0],
    pool: gopts[:pool],
    wait: get_ct_wait(opts[:wait]),
    reboot: opts[:reboot],
    stop_timeout: opts[:timeout],
    stop_method: m,
  }

  return osctld_fmt(:ct_restart, cmd_opts) unless opts[:foreground]

  open_console(args[0], gopts[:pool], 0, gopts[:json]) do |sock|
    sock.close if osctld_resp(:ct_restart, cmd_opts).error?
  end
end

#runscriptObject



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/osctl/cli/container.rb', line 328

def runscript
  require_args!('id', 'script')

  c = osctld_open
  cont = c.cmd_data!(
    :ct_runscript,
    id: args[0],
    pool: gopts[:pool],
    script: File.realpath(args[1]),
    arguments: args[2..-1],
    run: opts['run-container'],
    network: opts['network'],
  )

  if cont != 'continue'
    warn "runscript not available: invalid response '#{cont}'"
    exit(false)
  end

  handle_exec_io(c)
end

#set(option) ⇒ Object (protected)



1087
1088
1089
1090
1091
1092
1093
# File 'lib/osctl/cli/container.rb', line 1087

def set(option)
  require_args!('id')
  cmd_opts = {id: args[0], pool: gopts[:pool]}
  cmd_opts[option] = yield(args[1..-1])

  osctld_fmt(:ct_set, cmd_opts)
end

#set_attrObject



463
464
465
466
467
468
469
470
471
# File 'lib/osctl/cli/container.rb', line 463

def set_attr
  require_args!('id', 'attribute', 'value')
  do_set_attr(
    :ct_set,
    {id: args[0], pool: gopts[:pool]},
    args[1],
    args[2],
  )
end

#set_autostartObject



357
358
359
360
361
362
363
364
# File 'lib/osctl/cli/container.rb', line 357

def set_autostart
  set(:autostart) do
    {
      priority: opts[:priority],
      delay: opts[:delay],
    }
  end
end

#set_cpu_limitObject



438
439
440
441
# File 'lib/osctl/cli/container.rb', line 438

def set_cpu_limit
  require_args!('id', 'limit')
  do_set_cpu_limit(:ct_cgparam_set, id: args[0], pool: gopts[:pool])
end

#set_distributionObject



409
410
411
412
413
414
415
416
417
418
# File 'lib/osctl/cli/container.rb', line 409

def set_distribution
  set(:distribution) do |args|
    raise GLI::BadCommandLine, 'expected <distribution> <version>' if args.count != 2

    {
      name: args[0],
      version: args[1],
    }
  end
end

#set_dns_resolverObject



388
389
390
391
392
393
# File 'lib/osctl/cli/container.rb', line 388

def set_dns_resolver
  set(:dns_resolvers) do |args|
    raise GLI::BadCommandLine, 'expected at least one address' if args.empty?
    args
  end
end

#set_ephemeralObject



370
371
372
# File 'lib/osctl/cli/container.rb', line 370

def set_ephemeral
  set(:ephemeral) { true }
end

#set_hostnameObject



378
379
380
381
382
# File 'lib/osctl/cli/container.rb', line 378

def set_hostname
  set(:hostname) do |args|
    args[0] || (fail 'expected hostname')
  end
end

#set_memoryObject



448
449
450
451
452
453
454
455
456
# File 'lib/osctl/cli/container.rb', line 448

def set_memory
  require_args!('id', 'memory')
  do_set_memory(
    :ct_cgparam_set,
    :ct_cgparam_unset,
    id: args[0],
    pool: gopts[:pool]
  )
end

#set_nestingObject



399
400
401
402
403
# File 'lib/osctl/cli/container.rb', line 399

def set_nesting
  set(:nesting) do |args|
    true
  end
end

#set_seccomp_profileObject



420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/osctl/cli/container.rb', line 420

def set_seccomp_profile
  set(:seccomp_profile) do |args|
    if args.count != 1
      raise GLI::BadCommandLine, 'expected <profile>'

    elsif !File.exist?(args[0])
      raise GLI::BadCommandLine, "file '#{args[0]}' does not exist"

    else
      File.absolute_path(args[0])
    end
  end
end

#showObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/osctl/cli/container.rb', line 139

def show
  if opts[:list]
    puts FIELDS.join("\n")
    return
  end

  require_args!('id')

  cols = opts[:output] ? opts[:output].split(',').map(&:to_sym) : FIELDS

  c = osctld_open
  ct = c.cmd_data!(:ct_show, id: args[0], pool: gopts[:pool])

  cg_add_stats(c, ct, ct[:group_path], cols, gopts[:parsable])
  c.close

  add_loadavg(ct)

  format_output(ct, cols, header: !opts['hide-header'])
end

#startObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/osctl/cli/container.rb', line 206

def start
  require_args!('id')
  cmd_opts = {
    id: args[0],
    pool: gopts[:pool],
    wait: get_ct_wait(opts[:wait]),
    queue: opts[:queue],
    priority: opts[:priority],
    debug: opts[:debug],
  }
  return osctld_fmt(:ct_start, cmd_opts) unless opts[:foreground]

  open_console(args[0], gopts[:pool], 0, gopts[:json]) do |sock|
    sock.close if osctld_resp(:ct_start, cmd_opts).error?
  end
end

#stopObject



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
# File 'lib/osctl/cli/container.rb', line 223

def stop
  require_args!('id')

  if opts[:kill] && opts['dont-kill']
    raise GLI::BadCommandLine, '--kill and --dont-kill cannot be used together'

  elsif opts[:kill]
    m = :kill

  elsif opts['dont-kill']
    m = :shutdown_or_fail

  else
    m = :shutdown_or_kill
  end

  cmd_opts = {
    id: args[0],
    pool: gopts[:pool],
    timeout: opts[:timeout],
    method: m,
  }

  return osctld_fmt(:ct_stop, cmd_opts) unless opts[:foreground]

  open_console(args[0], gopts[:pool], 0, gopts[:json]) do |sock|
    sock.close if osctld_resp(:ct_stop, cmd_opts).error?
  end
end

#suObject



350
351
352
353
354
355
# File 'lib/osctl/cli/container.rb', line 350

def su
  require_args!('id')

  cmd = osctld_call(:ct_su, id: args[0], pool: gopts[:pool])
  handle_ct_attach(cmd)
end

#treeObject



129
130
131
132
133
134
135
136
137
# File 'lib/osctl/cli/container.rb', line 129

def tree
  require_args!('pool')
  Tree.print(
    args[0],
    parsable: gopts[:parsable],
    color: gopts[:color],
    containers: true
  )
end

#unset(option) ⇒ Object (protected)



1095
1096
1097
1098
1099
1100
1101
# File 'lib/osctl/cli/container.rb', line 1095

def unset(option)
  require_args!('id')
  cmd_opts = {id: args[0], pool: gopts[:pool]}
  cmd_opts[option] = block_given? ? yield(args[1..-1]) : true

  osctld_fmt(:ct_unset, cmd_opts)
end

#unset_attrObject



473
474
475
476
477
478
479
480
# File 'lib/osctl/cli/container.rb', line 473

def unset_attr
  require_args!('id', 'attribute')
  do_unset_attr(
    :ct_unset,
    {id: args[0], pool: gopts[:pool]},
    args[1],
  )
end

#unset_autostartObject



366
367
368
# File 'lib/osctl/cli/container.rb', line 366

def unset_autostart
  unset(:autostart)
end

#unset_cpu_limitObject



443
444
445
446
# File 'lib/osctl/cli/container.rb', line 443

def unset_cpu_limit
  require_args!('id')
  do_unset_cpu_limit(:ct_cgparam_unset, id: args[0], pool: gopts[:pool])
end

#unset_dns_resolverObject



395
396
397
# File 'lib/osctl/cli/container.rb', line 395

def unset_dns_resolver
  unset(:dns_resolvers)
end

#unset_ephemeralObject



374
375
376
# File 'lib/osctl/cli/container.rb', line 374

def unset_ephemeral
  unset(:ephemeral)
end

#unset_hostnameObject



384
385
386
# File 'lib/osctl/cli/container.rb', line 384

def unset_hostname
  unset(:hostname)
end

#unset_memoryObject



458
459
460
461
# File 'lib/osctl/cli/container.rb', line 458

def unset_memory
  require_args!('id')
  do_unset_memory(:ct_cgparam_unset, id: args[0], pool: gopts[:pool])
end

#unset_nestingObject



405
406
407
# File 'lib/osctl/cli/container.rb', line 405

def unset_nesting
  unset(:nesting)
end

#unset_seccomp_profileObject



434
435
436
# File 'lib/osctl/cli/container.rb', line 434

def unset_seccomp_profile
  unset(:seccomp_profile)
end