Module: LXC::Shell

Extended by:
Shell
Included in:
LXC, Shell
Defined in:
lib/lxc/shell.rb

Constant Summary collapse

BIN_PREFIX =
'/usr/bin'
BIN_FILES =
[
  'lxc-attach',
  'lxc-cgroup',
  'lxc-checkconfig',
  'lxc-checkpoint',
  'lxc-clone',
  'lxc-console',
  'lxc-create',
  'lxc-destroy',
  'lxc-execute',
  'lxc-freeze',
  'lxc-info',
  'lxc-kill',
  'lxc-ls',
  'lxc-monitor',
  'lxc-netstat',
  'lxc-ps',
  'lxc-restart',
  'lxc-setcap',
  'lxc-setuid',
  'lxc-start',
  'lxc-start-ephemeral',
  'lxc-stop',
  'lxc-unfreeze',
  'lxc-unshare',
  'lxc-version',
  'lxc-wait'
]
CONTAINER_STATES =
[
  'STOPPED',
  'STARTING',
  'RUNNING',
  'STOPPING',
  'ABORTING',
  'FREEZING',
  'FROZEN'
]
REMOVE_COLORS =

Terminal command to strip all special color symbols

'sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"'
@@use_sudo =
false

Instance Method Summary collapse

Instance Method Details

#run(command, *args) ⇒ String

Execute a LXC command If you would like to use pipe command you’ll need to provide a block that returns string

Parameters:

  • name (String)

    command name

  • args (Array)

    command arguments

Returns:

  • (String)

    execution result



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/lxc/shell.rb', line 79

def run(command, *args)
  command_name = "lxc-#{command}"

  unless BIN_FILES.include?(command_name)
    raise ArgumentError, "Invalid command: #{command_name}."
  end

  cmd = ""
  cmd << "sudo " if use_sudo == true
  cmd << "#{command_name} #{args.join(' ')}".strip
  cmd << " | #{yield}" if block_given?

  child = POSIX::Spawn::Child.new(cmd.strip)
  child.out
end

#use_sudoBoolean

Check if LXC is using sudo to run commands

Returns:

  • (Boolean)

    current sudo flag value



53
54
55
# File 'lib/lxc/shell.rb', line 53

def use_sudo
  @@use_sudo
end

#use_sudo=(val) ⇒ Boolean

Set LXC to execute commands with sudo

Parameters:

  • val (Boolean)

    true for sudo usage

Returns:

  • (Boolean)

    new sudo flag value



60
61
62
63
# File 'lib/lxc/shell.rb', line 60

def use_sudo=(val)
  @@use_sudo = val
  val
end

#valid_state?(name) ⇒ Boolean

Check if container state is valid

Parameters:

  • name (String)

    container name

Returns:

  • (Boolean)


68
69
70
# File 'lib/lxc/shell.rb', line 68

def valid_state?(name)
  CONTAINER_STATES.include?(name)
end