Class: LxcSsh::SpecificManager10
- Inherits:
-
Object
- Object
- LxcSsh::SpecificManager10
- Defined in:
- lib/lxc_ssh/manager/specific_manager10.rb
Instance Method Summary collapse
-
#container_config(name) ⇒ LxcSsh::ContainerConfig
Returns a LxcSsh::ContainerConfig object for the container name.
-
#container_names ⇒ Array
Returns an array of lxc containers.
-
#containers ⇒ Array
Returns an array containing objects with container metadata.
-
#create_container(name, template, template_options = nil) ⇒ String
Creates a container with the default configuration and additional template options and returns the output as a string.
-
#destroy_container(name) ⇒ Boolean
Destroys a container with the given name.
-
#get_container_obj(name) ⇒ LxcSsh::Container
Returns an LxcSsh::Container object for the given container name.
-
#initialize(session, lxc_path) ⇒ SpecificManager10
constructor
Initializes the manager.
-
#start_container(name) ⇒ Boolean
Starts a container with the given name.
-
#stop_container(name) ⇒ Boolean
Stops a container.
-
#template_help(template_name) ⇒ String
Returns the template help text for a given template name.
-
#template_names ⇒ Array
Returns an array containing all templates.
-
#write_config(container_name, config) ⇒ Object
Writes the config file contents back to the container config.
Constructor Details
#initialize(session, lxc_path) ⇒ SpecificManager10
Initializes the manager
8 9 10 11 |
# File 'lib/lxc_ssh/manager/specific_manager10.rb', line 8 def initialize(session, lxc_path) @session = session @lxc_path = lxc_path end |
Instance Method Details
#container_config(name) ⇒ LxcSsh::ContainerConfig
Returns a LxcSsh::ContainerConfig object for the container name
58 59 60 61 62 |
# File 'lib/lxc_ssh/manager/specific_manager10.rb', line 58 def container_config(name) contents = config_contents name ContainerConfig.new contents end |
#container_names ⇒ Array
Returns an array of lxc containers
16 17 18 19 20 |
# File 'lib/lxc_ssh/manager/specific_manager10.rb', line 16 def container_names result = @session.exec! @lxc_path + '/bin/lxc-ls' result.lines.map(&:strip).uniq end |
#containers ⇒ Array
Returns an array containing objects with container metadata
25 26 27 28 29 30 31 32 33 |
# File 'lib/lxc_ssh/manager/specific_manager10.rb', line 25 def containers containers = [] container_names.each do |name| containers << get_container_obj(name) end containers end |
#create_container(name, template, template_options = nil) ⇒ String
Creates a container with the default configuration and additional template options and returns the output as a string
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/lxc_ssh/manager/specific_manager10.rb', line 113 def create_container(name, template, = nil) args = "-t #{template}" if .nil? == false args += " -- #{}" end output = run_cmd("lxc-create", name, args) output end |
#destroy_container(name) ⇒ Boolean
Destroys a container with the given name
149 150 151 152 153 |
# File 'lib/lxc_ssh/manager/specific_manager10.rb', line 149 def destroy_container(name) output = run_cmd("lxc-destroy", name) output.nil? end |
#get_container_obj(name) ⇒ LxcSsh::Container
Returns an LxcSsh::Container object for the given container name
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/lxc_ssh/manager/specific_manager10.rb', line 38 def get_container_obj(name) container = Container.new container.name = name info = info(name) container.pid = info.pid container.state = info.state container.memory_usage = run_cmd("lxc-cgroup", name, "memory.usage_in_bytes").strip.to_i container.memory_limit = run_cmd("lxc-cgroup", name, "memory.limit_in_bytes").strip.to_i container.cpu_shares = run_cmd("lxc-cgroup", name, "cpu.shares").strip.to_i container.cpu_usage = cpu_usage name container.ip = ip_addr name container end |
#start_container(name) ⇒ Boolean
Starts a container with the given name
129 130 131 132 133 |
# File 'lib/lxc_ssh/manager/specific_manager10.rb', line 129 def start_container(name) output = run_cmd("lxc-start", name, '-d') output.nil? end |
#stop_container(name) ⇒ Boolean
Stops a container
139 140 141 142 143 |
# File 'lib/lxc_ssh/manager/specific_manager10.rb', line 139 def stop_container(name) output = run_cmd("lxc-stop", name) output.nil? end |
#template_help(template_name) ⇒ String
Returns the template help text for a given template name
99 100 101 102 103 |
# File 'lib/lxc_ssh/manager/specific_manager10.rb', line 99 def template_help(template_name) output = run_cmd("lxc-create", nil, "-t lxc-#{template_name} -h") output end |
#template_names ⇒ Array
check path
Returns an array containing all templates
86 87 88 89 90 91 92 93 |
# File 'lib/lxc_ssh/manager/specific_manager10.rb', line 86 def template_names output = @session.exec! "ls -1 " + @lxc_path + "/share/lxc/templates/" output.gsub! 'lxc-', '' result = output.lines.map(&:strip).uniq result end |
#write_config(container_name, config) ⇒ Object
verify function
escape contents variable
Writes the config file contents back to the container config
71 72 73 74 75 76 77 78 79 |
# File 'lib/lxc_ssh/manager/specific_manager10.rb', line 71 def write_config(container_name, config) if !config.kind_of?(ContainerConfig) raise ArgumentError, "config must be a instance of ContainerConfig" end contents = config.config_contents @session.exec! "echo -e '" + contents + "' > " + config_path(container_name) end |