Module: TestLab::Container::Actions

Included in:
TestLab::Container
Defined in:
lib/testlab/container/actions.rb

Instance Method Summary collapse

Instance Method Details

#createBoolean

Create the container

Builds the configuration for the container and sends a request to the LXC sub-system to create the container.

Returns:

  • (Boolean)

    True if successful.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/testlab/container/actions.rb', line 12

def create
  @ui.logger.debug { "Container Create: #{self.id}" }

  self.node.alive? or return false

  persistent_operation_check(:create)

  please_wait(:ui => @ui, :message => format_object_action(self, 'Create', :green)) do
    configure

    self.lxc.create(*create_args)

    do_provisioner_callbacks(self, :create, @ui)
  end

  true
end

#destroyBoolean

Destroy the container

Sends a request to the LXC sub-system to destroy the container.

Returns:

  • (Boolean)

    True if successful.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/testlab/container/actions.rb', line 35

def destroy
  @ui.logger.debug { "Container Destroy: #{self.id}" }

  self.node.alive? or return false

  please_wait(:ui => @ui, :message => format_object_action(self, 'Destroy', :red)) do
    self.lxc.destroy(%(-f))
    self.lxc_clone.destroy(%(-f))

    do_provisioner_callbacks(self, :destroy, @ui)
  end

  true
end

#downBoolean

Stop the container

Sends a request to the LXC sub-system to take the container offline.

Returns:

  • (Boolean)

    True if successful.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/testlab/container/actions.rb', line 103

def down
  @ui.logger.debug { "Container Down: #{self.id}" }

  self.node.alive? or return false

  please_wait(:ui => @ui, :message => format_object_action(self, 'Down', :red)) do

    self.lxc.stop

    # If we are in ephemeral mode...
    if self.is_ephemeral?

      # IMPORTANT NOTE:
      #
      # If we are using a non-memory backed COW filesystem for the
      # ephemeral clones we should destroy the container.
      #
      # If we are using a memory backed COW filesystem for the ephemeral
      # clones then it will be released when the container is stopped.
      self.persist and self.lxc.destroy(%(-f))
    end

    (self.state == :running) and raise ContainerError, "The container #{self.id.inspect} failed to offline!"

    do_provisioner_callbacks(self, :down, @ui)
  end

  true
end

#upBoolean

Start the container

Sends a request to the LXC sub-system to bring the container online.

Returns:

  • (Boolean)

    True if successful.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/testlab/container/actions.rb', line 55

def up
  @ui.logger.debug { "Container Up: #{self.id}" }

  self.node.alive? or return false

  please_wait(:ui => @ui, :message => format_object_action(self, 'Up', :green)) do
    configure

    # Remove any existing ARP entries for our container from the node.
    self.interfaces.each do |interface|
      self.node.exec(%(sudo arp --verbose --delete #{interface.ip}), :ignore_exit_status => true)
    end

    if self.is_ephemeral?
      self.lxc_clone.start_ephemeral(clone_args)
    else
      self.lxc.start(start_args)
    end

    (self.state != :running) and raise ContainerError, "The container #{self.id.inspect} failed to online! (did you create it? Check status with 'tl status')"

    ZTK::TCPSocketCheck.new(:ui => @ui, :host => self.primary_interface.ip, :port => 22).wait

    # If we are not in ephemeral mode we should attempt to provision our
    # defined users.
    if self.is_persistent?
      self.users.each do |user|
        user.provision
      end
    end

    # Ensure the hostname is set
    self.lxc.attach(%(-- /bin/bash -c 'hostname #{self.id}'))
    self.lxc.attach(%(-- /bin/bash -c 'echo "#{self.id}" | tee /etc/hostname'))
    self.lxc.attach(%(-- /bin/bash -c 'sed -i "s/\\(127\\.0\\.1\\.1\\).*/\\1\t#{self.id} #{self.fqdn}/g" /etc/hosts'))
    self.lxc.attach(%(-- /bin/bash -c '(grep "#{self.id}" /etc/hosts) || (echo "127.0.1.1\t#{self.id} #{self.fqdn}" | tee -a /etc/hosts)'))

    do_provisioner_callbacks(self, :up, @ui)
  end

  true
end