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
20
# 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)
  command = command.split if command.is_a? String

  configure_container! command, binds, volumes
end

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



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

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



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

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

#fetch_container_state!Object



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

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

#run!Object



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

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



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

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