Class: Rascal::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/rascal/environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(full_name, name:, image:, env_variables: {}, services: [], volumes: [], before_shell: [], after_shell: [], working_dir: nil) ⇒ Environment

Returns a new instance of Environment.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rascal/environment.rb', line 7

def initialize(full_name, name:, image:, env_variables: {}, services: [], volumes: [], before_shell: [], after_shell: [], working_dir: nil)
  @name = name
  @network = Docker::Network.new(full_name)
  @container = Docker::Container.new(full_name, image)
  @env_variables = env_variables
  @services = services
  @volumes = volumes
  @working_dir = working_dir
  @before_shell = before_shell
  @after_shell = after_shell
end

Instance Attribute Details

#before_shellObject (readonly)

Returns the value of attribute before_shell.



5
6
7
# File 'lib/rascal/environment.rb', line 5

def before_shell
  @before_shell
end

#containerObject (readonly)

Returns the value of attribute container.



5
6
7
# File 'lib/rascal/environment.rb', line 5

def container
  @container
end

#env_variablesObject (readonly)

Returns the value of attribute env_variables.



5
6
7
# File 'lib/rascal/environment.rb', line 5

def env_variables
  @env_variables
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/rascal/environment.rb', line 5

def name
  @name
end

#networkObject (readonly)

Returns the value of attribute network.



5
6
7
# File 'lib/rascal/environment.rb', line 5

def network
  @network
end

#servicesObject (readonly)

Returns the value of attribute services.



5
6
7
# File 'lib/rascal/environment.rb', line 5

def services
  @services
end

#volumesObject (readonly)

Returns the value of attribute volumes.



5
6
7
# File 'lib/rascal/environment.rb', line 5

def volumes
  @volumes
end

#working_dirObject (readonly)

Returns the value of attribute working_dir.



5
6
7
# File 'lib/rascal/environment.rb', line 5

def working_dir
  @working_dir
end

Instance Method Details

#clean(clean_volumes: false) ⇒ Object



53
54
55
56
57
# File 'lib/rascal/environment.rb', line 53

def clean(clean_volumes: false)
  @services.each(&:clean)
  @network.clean
  @volumes.each(&:clean) if clean_volumes
end

#run_command(*command) ⇒ Object

Runs a single command in the environment, without a TTY, and returns its Process::Status. Unlike #run_shell this is suitable for non-interactive use (scripts, CI-like runs, AI agents), which need the command's real exit status.

The command is a list of arguments, which is escaped before being handed to bash, so arguments may contain spaces and shell metacharacters. Use e.g. bash -c "foo && bar" to run something the shell must interpret.



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rascal/environment.rb', line 40

def run_command(*command)
  download_missing
  start_services
  @container.run_and_attach('bash', '-c', command_script(Shellwords.join(command)),
    env: @env_variables,
    network: @network,
    volumes: @volumes,
    working_dir: @working_dir,
    allow_failure: true,
    tty: false
  )
end

#run_shellObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rascal/environment.rb', line 19

def run_shell
  download_missing
  start_services
  command = [*@before_shell, 'bash', *@after_shell].join(';')
  @container.run_and_attach('bash', '-c', command,
    env: @env_variables,
    network: @network,
    volumes: @volumes,
    working_dir: @working_dir,
    allow_failure: true
  )
end

#update(skip: []) ⇒ Object



59
60
61
62
63
64
# File 'lib/rascal/environment.rb', line 59

def update(skip: [])
  [
    *@services.collect { |s| s.update(skip: skip) },
    *@container.update(skip: skip),
  ]
end