Class: LxcSsh::SpecificManager10

Inherits:
Object
  • Object
show all
Defined in:
lib/lxc_ssh/manager/specific_manager10.rb

Instance Method Summary collapse

Constructor Details

#initialize(session, lxc_path) ⇒ SpecificManager10

Initializes the manager

Parameters:

  • session (SSH::Connection::Session)

    ssh session

  • lxc_path (String)

    lxc installation path



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_namesArray

Returns an array of lxc containers

Returns:

  • (Array)


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

#containersArray

Returns an array containing objects with container metadata

Returns:

  • (Array)


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

Parameters:

  • name (String)

    container name

  • template (String)

    template name (without ‘lxc-’-prefix)

  • template_options (String) (defaults to: nil)

    additional template options

Returns:

  • (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, template_options = nil)
  args = "-t #{template}"

  if template_options.nil? == false
    args += " -- #{template_options}"
  end

  output = run_cmd("lxc-create", name, args)

  output
end

#destroy_container(name) ⇒ Boolean

Destroys a container with the given name

Parameters:

  • name (String)

    container name

Returns:

  • (Boolean)


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

Returns:



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

Parameters:

  • name (String)

    container name

Returns:

  • (Boolean)


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

Parameters:

  • name (String)

    container name

Returns:

  • (Boolean)


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

Parameters:

  • template_name (String)

    template to use (without ‘lxc-’-prefix)

Returns:

  • (String)


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_namesArray

TODO:

check path

Returns an array containing all templates

Returns:

  • (Array)


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

TODO:

verify function

TODO:

escape contents variable

Writes the config file contents back to the container config

Parameters:

  • container_name (String)

    the container to use

  • config (String)

    configuration string to save



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