Class: Haconiwa::Runners::Linux

Inherits:
Object
  • Object
show all
Defined in:
lib/haconiwa/runners/linux.rb

Overview

Class Method Summary collapse

Class Method Details

.attach(base, run_command = []) ⇒ Object



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
97
98
99
100
101
102
103
104
# File 'lib/haconiwa/runners/linux.rb', line 70

def self.attach(base, run_command=[])
  if run_command.empty?
    run_command << "/bin/bash"
  end

  wrapper = Tempfile.open("haconiwa-attacher-#{$$}-#{Time.now.to_i}.sh")

  wrapper.puts "#!/bin/bash"
  wrapper.puts "chroot #{base.filesystem.chroot} bash -c '"
  wrapper.puts "cd / ;"
  wrapper.puts Shellwords.shelljoin(["exec", *run_command]).gsub(/'/, %<'"'"'>)
  wrapper.puts "'"
  wrapper.close
  FileUtils.chmod 0700, wrapper.path

  runner = fork {
    base.pid ||= File.read(base.container_pid_file).to_i

    if base.attached_capabilities
      base.attached_capabilities.apply!
    end

    base.cgroup.attach(to: base.name)
    Bundler.with_clean_env { base.namespace.enter(pid: base.pid, wrapper_path: wrapper.path) }
  }

  puts "Attached to contanier: Runner PID = #{runner}"

  res = Process.waitpid2 runner
  if res[1].success?
    puts "Successfully exit."
  else
    puts "Attached process exited with status code <#{res[1].to_i}>."
  end
end

.run(base, init_command = []) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/haconiwa/runners/linux.rb', line 10

def self.run(base, init_command=[])
  container = fork {
    if init_command.empty?
      init_command = Array(base.init_command)
    end

    base.namespace.apply!
    base.cgroup.register_all!(to: base.name)

    base.filesystem.mount_all!

    Dir.chroot base.filesystem.chroot
    Dir.chdir "/"

    wrapper = Tempfile.open("haconiwa-wrapper-#{$$}-#{Time.now.to_i}.sh")

    wrapper.puts "#!/bin/bash"
    wrapper.puts "/bin/bash -c '"
    if base.filesystem.mount_independent_procfs
      wrapper.puts "mount -t proc proc /proc;"
    end
    wrapper.puts "exec $@;"
    wrapper.puts "' -- \"$@\""
    wrapper.close
    FileUtils.chmod 0700, wrapper.path

    Haconiwa::Base.sethostname(base.name)

    base.capabilities.apply!

    if base.namespace.use_pid_ns
      Bundler.with_clean_env {
        exec "unshare", "--pid", "--", wrapper.path, *init_command
      }
    else
      Bundler.with_clean_env { exec wrapper.path, *init_command }
    end
  }

  sleep 0.1 # The magic
  Haconiwa::SmallCgroup.register_at_exit(pid: container, name: base.name, dirs: base.cgroup.to_dirs)
  real_container_pid = if base.namespace.use_pid_ns
                         find_by_ppid(container)
                       else
                         container
                       end
  File.open(base.container_pid_file, "w") {|pid| pid.write real_container_pid }
  Signal.trap(:INT) { Process.kill :TERM, container }
  puts "New container: Name = #{base.name}, PID = #{real_container_pid}"

  res = Process.waitpid2 container
  if res[1].success?
    puts "Successfully exit container."
  else
    puts "Container exited with status code <#{res[1].to_i}>."
  end

  at_exit { FileUtils.rm_f base.container_pid_file }
end