Class: Mumukit::IsolatedEnvironment

Inherits:
Object
  • Object
show all
Defined in:
lib/mumukit/isolated_environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#containerObject

Returns the value of attribute container.



7
8
9
# File 'lib/mumukit/isolated_environment.rb', line 7

def container
  @container
end

Instance Method Details

#configure!(*files) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mumukit/isolated_environment.rb', line 9

def configure!(*files)
  filenames = files.map { |it| File.absolute_path(it.path) }
  dirnames = filenames.map { |it| Pathname.new(it).dirname }.uniq

  binds = dirnames.map { |it| "#{it}:#{it}" }
  volumes = Hash[dirnames.map { |it| [[it, {}]] }]

  command = yield(*filenames).split

  configure_container! command, binds, volumes
end

#configure_container!(command, binds, volumes) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/mumukit/isolated_environment.rb', line 21

def configure_container!(command, binds, volumes)
  self.container = Docker::Container.create(
    'Image' => Mumukit.config.docker_image,
    'Cmd' => command,
    'NetworkDisabled' => true,
    'HostConfig' => {
        'Binds' => binds},
    'Volumes' => volumes)
end

#destroy!Object



55
56
57
58
59
60
# File 'lib/mumukit/isolated_environment.rb', line 55

def destroy!
  if container
    container.stop
    container.delete
  end
end

#fetch_container_state!Object



49
50
51
52
53
# File 'lib/mumukit/isolated_environment.rb', line 49

def fetch_container_state!
  exit = container.json['State']['ExitCode']
  out = container.streaming_logs(stdout: true, stderr: true)
  [exit, out]
end

#run!Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mumukit/isolated_environment.rb', line 31

def run!
  run_container!
  exit, out = fetch_container_state!

  if exit == 0
    [out, :passed]
  else
    [out, :failed]
  end
rescue Docker::Error::TimeoutError => e
  [I18n.t('mumukit.time_exceeded', limit: Mumukit.config.command_time_limit), :aborted]
end

#run_container!Object



44
45
46
47
# File 'lib/mumukit/isolated_environment.rb', line 44

def run_container!
  container.start
  container.wait(Mumukit.config.command_time_limit)
end