Class: ContainerManagerAdapter::Lxc
- Inherits:
-
Object
- Object
- ContainerManagerAdapter::Lxc
- Defined in:
- lib/wf_node_api/container_manager_adapter/lxc.rb
Overview
Container adapter for LXC. Encapsulates all LXC specific command line wrapping
Instance Method Summary collapse
-
#container(name) ⇒ Hash
Returns information for a single container.
-
#containers ⇒ Array
Lists all available containers.
-
#create_container(name, ip_address, disk_size_gb, memory_limit_mb, cpu_core_count, template) ⇒ String
Creates a container with the given parameters.
-
#delete(name) ⇒ String
Deletes a container with the given name.
-
#exist?(name) ⇒ Boolean
Checks if a container with the given name exists.
-
#free_cpu_core_count(resman) ⇒ Integer
Returns the amount of free cpu cores.
-
#kill(name) ⇒ String
Kills a container with the given name.
-
#start(name) ⇒ String
Starts a container with the given name.
-
#stop(name) ⇒ String
Stops a container with the given name.
-
#supported_templates ⇒ Hash
Returns a list of supported templates.
Instance Method Details
#container(name) ⇒ Hash
Returns information for a single container
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 128 def container(name) data = {} data[:name] = name data[:state] = translate_state(state(name)) data[:ip_address] = ip_addr(name) data[:cpu_cores] = assigned_cpu_cores(name).count data[:memory_limit_bytes] = memory_limit(name).to_i data[:memory_usage_bytes] = memory_usage(name).to_i data[:disk_space_gb] = 0 data[:disk_usage_gb] = 0 data[:container_type] = 'lxc' data end |
#containers ⇒ Array
Lists all available containers
37 38 39 40 41 42 43 44 45 |
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 37 def containers container_list = [] self.container_names.each do |item| container_list << container(item) end container_list end |
#create_container(name, ip_address, disk_size_gb, memory_limit_mb, cpu_core_count, template) ⇒ String
Creates a container with the given parameters
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 156 def create_container(name, ip_address, disk_size_gb, memory_limit_mb, cpu_core_count, template) output = '' begin if cpu_core_count != 0 cpuset = self.generate_cpu_set(cpu_core_count, ResourceManager.new('linux')) end if false == $lxc_cmd_create.has_key?(template) raise ArgumentError, "template does not exist" end create_result = Open3.capture3($lxc_cmd_create[template].gsub('[name]', name)) output += create_result[0] output += create_result[1] config_path = $lxc_container_config_path.gsub('[name]', name) open(config_path, 'a') do |f| f << "lxc.network.ipv4=#{ip_address}/#{$lxc_ip_netmask}\n" f << "lxc.start.auto=1\n" f << "lxc.start.delay=5\n" if cpu_core_count.to_i != 0 f << "lxc.cgroup.cpuset.cpus=#{cpuset}\n" end if memory_limit_mb.to_i != 0 f << "lxc.cgroup.memory.limit_in_bytes=#{memory_limit_mb.to_s}M\n" end end $logger.info("creation of container " + name + " successful") return output.strip rescue => e $logger.warn("container " + name + " could not be created, rolling back...") # rollback delete(name) if exist?(name) output += e. raise RuntimeError, output.strip end end |
#delete(name) ⇒ String
Deletes a container with the given name
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 111 def delete(name) res = Open3.capture3($lxc_cmd_destroy.gsub('[name]', name)) if res[1].empty? && res[2].exitstatus == 0 $logger.info("container " + name + " successfully deleted") return res[0].strip end $logger.warn("container" + name + " could not be deleted") raise RuntimeError, res[1].strip end |
#exist?(name) ⇒ Boolean
Checks if a container with the given name exists
207 208 209 |
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 207 def exist?(name) self.container_names().include?(name) end |
#free_cpu_core_count(resman) ⇒ Integer
Returns the amount of free cpu cores
216 217 218 |
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 216 def free_cpu_core_count(resman) self.free_cpu_cores(resman).count end |
#kill(name) ⇒ String
Kills a container with the given name
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 92 def kill(name) res = Open3.capture3($lxc_cmd_kill.gsub('[name]', name)) if res[1].empty? && res[2].exitstatus == 0 $logger.info("container " + name + " successfully killed") return res[0].strip end $logger.warn("container " + name + " could not be killed") raise RuntimeError, res[1].strip end |
#start(name) ⇒ String
Starts a container with the given name
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 54 def start(name) res = Open3.capture3($lxc_cmd_start.gsub('[name]', name)) if res[1].empty? && res[2].exitstatus == 0 $logger.info("container " + name + " successfully started") return res[0].strip end $logger.warn("container " + name + " could not be started") raise RuntimeError, res[1].strip end |
#stop(name) ⇒ String
Stops a container with the given name
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 73 def stop(name) res = Open3.capture3($lxc_cmd_stop.gsub('[name]', name)) if res[1].empty? && res[2].exitstatus == 0 $logger.info("container " + name + " successfully stopped") return res[0].strip end $logger.warn("container " + name + " could not be stopped") raise RuntimeError, res[1].strip end |
#supported_templates ⇒ Hash
Returns a list of supported templates
223 224 225 |
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 223 def supported_templates $lxc_cmd_create.keys end |