Module: LXC::Shell
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
-
#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.
-
#use_sudo ⇒ Boolean
Check if LXC is using sudo to run commands.
-
#use_sudo=(val) ⇒ Boolean
Set LXC to execute commands with sudo.
-
#valid_state?(name) ⇒ Boolean
Check if container state is valid.
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
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_sudo ⇒ Boolean
Check if LXC is using sudo to run commands
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
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
68 69 70 |
# File 'lib/lxc/shell.rb', line 68 def valid_state?(name) CONTAINER_STATES.include?(name) end |