Class: CIDE::Runner

Inherits:
Object
  • Object
show all
Includes:
Docker
Defined in:
lib/cide/runner.rb

Instance Method Summary collapse

Methods included from Docker

#docker, id

Constructor Details

#initialize(command: [], env: {}, links: [], tag: nil, user: nil) ⇒ Runner

Returns a new instance of Runner.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cide/runner.rb', line 9

def initialize(
  command: [],
  env: {},
  links: [],
  tag: nil,
  user: nil
)
  fail ArgumentError, 'tag missing' unless tag

  @containers = []
  @id = SecureRandom.hex

  @tag = tag
  @env = env
  @links = links
  @command = command
  @user = user
end

Instance Method Details

#cleanup!(output: $stderr) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/cide/runner.rb', line 69

def cleanup!(output: $stderr)
  return if @containers.empty?
  begin
    report_containers!(output)
  ensure
    # Shutdown old containers
    docker :rm, '--force', '--volumes', *@containers.reverse, capture: true
  end
end

#export!(guest_dir: nil, host_dir: nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cide/runner.rb', line 56

def export!(guest_dir: nil, host_dir: nil)
  fail ArgumentError, 'guest export_dir missing' unless guest_dir
  fail ArgumentError, 'host export_dir missing' unless host_dir

  # FIXME: strip trailing slashes ?
  guest_dir = File.expand_path(guest_dir, CIDE_SRC_DIR)
  host_dir = File.expand_path(host_dir, Dir.pwd)

  FileUtils.mkdir_p(File.dirname(host_dir))

  docker :cp, [@id, guest_dir].join(':'), host_dir
end

#run!(interactive: false) ⇒ Object

!!!! Don’t call run twice !!!!



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
# File 'lib/cide/runner.rb', line 29

def run!(interactive: false)
  start_links!

  run_options = ['--detach']

  @env.each_pair do |key, value|
    run_options.push '--env', [key, value].join('=')
  end

  @links.each do |link|
    run_options.push '--link', [link.id, link.name].join(':')
  end

  run_options.push '--name', @id

  run_options.push '--user', @user if @user

  run_options.push('--interactive', '--tty') if interactive

  run_options.push @tag
  run_options.push(*@command)

  id = docker(:run, *run_options, capture: true).strip
  @containers << id
  docker(:attach, id)
end