Class: Lxc::Ephemeral

Inherits:
Object
  • Object
show all
Includes:
Helpers, Helpers::Copies, Helpers::Options
Defined in:
lib/elecksee/ephemeral.rb

Overview

Create ephemeral containers

Constant Summary

Constants included from Helpers::Copies

Helpers::Copies::HOSTNAME_FILES, Helpers::Copies::NAME_FILES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Copies

#apply_custom_networking, #el_platform?, #update_naming, #update_net_hwaddr, #update_rootfs, #write_file

Methods included from Helpers::Options

included

Methods included from Helpers

#child_process_command, #detect_home, #log, #mixlib_shellout_command, #run_command, #sudo

Constructor Details

#initialize(args = {}) ⇒ Ephemeral

Create new instance

Parameters:

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :cli (TrueClass, FalseClass)

    enable CLI output



52
53
54
55
56
57
58
59
60
61
# File 'lib/elecksee/ephemeral.rb', line 52

def initialize(args={})
  configure!(args)
  @cli = args[:cli]
  @path = command("mktemp -d -p #{lxc_dir} #{original}-XXXXXXXXXXXX", :sudo => true).stdout.strip
  command("chmod 0755 #{@path}", :sudo => true)
  @name = File.basename(@path)
  @hostname = @name.gsub(%r{[^A-Za-z0-9\-]}, '')
  @ephemeral_binds = []
  @lxc = nil
end

Instance Attribute Details

#cliTrueClass, FalseClass (readonly)

Returns enable CLI output.

Returns:

  • (TrueClass, FalseClass)

    enable CLI output



34
35
36
# File 'lib/elecksee/ephemeral.rb', line 34

def cli
  @cli
end

#ephemeral_bindsArray<Storage::VirtualDevice] (readonly)

Returns Array<Storage::VirtualDevice].

Returns:



46
47
48
# File 'lib/elecksee/ephemeral.rb', line 46

def ephemeral_binds
  @ephemeral_binds
end

#ephemeral_deviceStorage::OverlayDirectory, Storage::VirtualDevice (readonly)



42
43
44
# File 'lib/elecksee/ephemeral.rb', line 42

def ephemeral_device
  @ephemeral_device
end

#ephemeral_overlayStorage::OverlayMount (readonly)



44
45
46
# File 'lib/elecksee/ephemeral.rb', line 44

def ephemeral_overlay
  @ephemeral_overlay
end

#hostnameString (readonly)

Returns hostname of container.

Returns:

  • (String)

    hostname of container



36
37
38
# File 'lib/elecksee/ephemeral.rb', line 36

def hostname
  @hostname
end

#lxcLxc (readonly)

Returns instance of ephemeral.

Returns:

  • (Lxc)

    instance of ephemeral



40
41
42
# File 'lib/elecksee/ephemeral.rb', line 40

def lxc
  @lxc
end

#nameString (readonly)

Returns name of container.

Returns:

  • (String)

    name of container



32
33
34
# File 'lib/elecksee/ephemeral.rb', line 32

def name
  @name
end

#pathString (readonly)

Returns path to container.

Returns:

  • (String)

    path to container



38
39
40
# File 'lib/elecksee/ephemeral.rb', line 38

def path
  @path
end

Instance Method Details

#cleanupTrueClass, FalseClass

Stop container and cleanup ephemeral items

Returns:

  • (TrueClass, FalseClass)


210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/elecksee/ephemeral.rb', line 210

def cleanup
  lxc.stop if lxc.running?
  @ephemeral_overlay.unmount
  @ephemeral_binds.map(&:destroy)
  @ephemeral_device.destroy
  if(lxc.path.to_path.split('/').size > 1)
    command("rm -rf #{lxc.path.to_path}", :sudo => true)
    true
  else
    $stderr.puts "This path seems bad and I won't remove it: #{lxc.path.to_path}"
    false
  end
end

#cli_outputTrueClass, FalseClass

Write output to CLI

Returns:

  • (TrueClass, FalseClass)


76
77
78
79
80
81
82
83
84
# File 'lib/elecksee/ephemeral.rb', line 76

def cli_output
  if(cli)
    puts "New ephemeral container started. (#{name})"
    puts "    - Connect using: sudo ssh -i #{ssh_key} root@#{lxc.container_ip(10)}"
    true
  else
    false
  end
end

#create!TrueClass

Create the ephemeral container

Returns:

  • (TrueClass)


110
111
112
113
# File 'lib/elecksee/ephemeral.rb', line 110

def create!
  setup
  true
end

#register_trapsTrueClass

Trap signals to force cleanup

Returns:

  • (TrueClass)


66
67
68
69
70
71
# File 'lib/elecksee/ephemeral.rb', line 66

def register_traps
  %w(TERM INT QUIT).each do |sig|
    Signal.trap(sig){ cleanup && raise }
  end
  true
end

#shell_wrapperTempfile

Bash based wrapper script to start ephemeral and clean up ephemeral resources on exit

Returns:

  • (Tempfile)

    wrapper script



149
150
151
152
153
154
155
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
201
202
203
204
205
# File 'lib/elecksee/ephemeral.rb', line 149

def shell_wrapper
  content = ['#!/bin/bash']
  content << 'scrub()' << '{'
  content << "umount #{ephemeral_overlay.target}"
  ephemeral_binds.map do |bind|
    unless(bind.device_path == :none)
      if(File.file?(bind.device_path))
        content << "rm #{bind.device_path}"
      elsif(File.directory?(bind.device_path))
        content << "rm -rf #{bind.device_path}"
      end
    end
    unless(bind.mount_path == :none)
      if(File.directory?(bind.mount_path))
        content << "rmdir #{bind.mount_path}"
      end
    end
  end
  case ephemeral_device
  when Storage::OverlayDirectory
    if(File.directory?(ephemeral_device.overlay_path))
      content << "rm -rf #{ephemeral_device.overlay_path}"
    end
  when Storage::VirtualDevice
    if(ephemeral_device.mounted?)
      content << "umount #{ephemeral_device.mount_path}"
    end
    unless(ephemeral_device.device_path == :none)
      if(File.file?(ephemeral_device.device_path))
        content << "rm #{ephemeral_device.device_path}"
      elsif(File.directory?(ephemeral_device.device_path))
        content << "rm -rf #{ephemeral_device.device_path}"
      end
    end
    unless(ephemeral_device.mount_path == :none)
      if(File.directory?(ephemeral_device.mount_path))
        content << "rmdir #{ephemeral_device.mount_path}"
      end
    end
  end
  if(lxc.path.to_path.split('/').size > 1)
    content << "rm -rf #{lxc.path.to_path}"
  end
  content << '}'
  content << 'trap scrub SIGTERM SIGINT SIGQUIT'
  content << "lxc-start -n #{lxc.name} -d"
  content << 'sleep 1'
  content << 'until [ `lxc-wait -n #{lxc.name} -s STOPPED -t 2` ]'
  content << 'do'
  content << 'done'
  content << 'scrub'
  tmp = Tempfile.new('elecksee')
  tmp.chmod(0700)
  tmp.puts content.join("\n")
  tmp.close
  tmp
end

#start!(*args) ⇒ TrueClass

Note:

use :fork to fork startup

Start the ephemeral container

Parameters:

  • args (Symbol)

    argument list

Returns:

  • (TrueClass)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/elecksee/ephemeral.rb', line 120

def start!(*args)
  setup
  if(daemon)
    if(args.include?(:fork))
      register_traps
      fork do
        start_action
      end
    elsif(args.include?(:detach))
      cmd = [sudo, shell_wrapper.path].compact.map(&:strip)
      process = ChildProcess.build(*cmd)
      process.detach = true
      process.start
      shell_wrapper.delete
    else
      register_traps
      Process.daemon
      start_action
    end
  else
    start_action
  end
  true
end

#start_actionTrueClass

Note:

generally should not be called directly

Start the ephemeral container

Returns:

  • (TrueClass)

See Also:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/elecksee/ephemeral.rb', line 91

def start_action
  begin
    lxc.start
    if(ephemeral_command)
      lxc.wait_for_state(:running)
      lxc.container_command(ephemeral_command, 0) # no retries on ephemeral commands
    else
      cli_output
      lxc.wait_for_state(:stopped)
    end
  ensure
    cleanup
  end
  true
end