Class: Maze::Docker
- Inherits:
-
Object
- Object
- Maze::Docker
- Defined in:
- lib/maze/docker.rb
Overview
Responsible for running docker containers in the local environment
Constant Summary collapse
- COMPOSE_FILENAME =
The default place to look for the docker-compose file
'features/fixtures/docker-compose.yml'
Class Attribute Summary collapse
- .compose_project_name ⇒ Object
-
.last_command_logs ⇒ Object
Returns the value of attribute last_command_logs.
-
.last_exit_code ⇒ Object
Returns the value of attribute last_exit_code.
Class Method Summary collapse
-
.copy_from_container(service, from:, to:) ⇒ Object
Copy a file or directory from a container to the local filesystem.
-
.copy_to_container(service, from:, to:) ⇒ Object
Copy a file or directory from the local filesystem to a container.
-
.down_all_services ⇒ Object
Kills all running services.
-
.down_service(service) ⇒ Object
Kills a running service.
-
.exec(service, command, detach: false) ⇒ Object
Execute a command in an already running docker container.
- .last_command_logs Provides access to the output from the last run docker command=(Providesaccesstotheoutputfromthelastrundockercommand = (value)) ⇒ Object
- .last_exit_code Provides access to the exit code of the last run docker command=(Providesaccesstotheexitcodeofthelastrundockercommand = (value)) ⇒ Object
-
.reset ⇒ Object
Resets any state ready for the next scenario.
-
.start_service(service, command: nil, interactive: false) ⇒ Object
Builds and starts a service, using a command if given.
Class Attribute Details
.compose_project_name ⇒ Object
122 123 124 |
# File 'lib/maze/docker.rb', line 122 def compose_project_name @compose_project_name ||= nil end |
.last_command_logs ⇒ Object
Returns the value of attribute last_command_logs.
16 17 18 |
# File 'lib/maze/docker.rb', line 16 def last_command_logs @last_command_logs end |
.last_exit_code ⇒ Object
Returns the value of attribute last_exit_code.
13 14 15 |
# File 'lib/maze/docker.rb', line 13 def last_exit_code @last_exit_code end |
Class Method Details
.copy_from_container(service, from:, to:) ⇒ Object
Copy a file or directory from a container to the local filesystem
This is equivalent to the Docker Compose ‘cp’ command: docs.docker.com/engine/reference/commandline/compose_cp/
80 81 82 |
# File 'lib/maze/docker.rb', line 80 def copy_from_container(service, from:, to:) run_docker_compose_command("cp #{service}:#{from} #{to}", success_codes: [0]) end |
.copy_to_container(service, from:, to:) ⇒ Object
Copy a file or directory from the local filesystem to a container
This is equivalent to the Docker Compose ‘cp’ command: docs.docker.com/engine/reference/commandline/compose_cp/
92 93 94 |
# File 'lib/maze/docker.rb', line 92 def copy_to_container(service, from:, to:) run_docker_compose_command("cp #{from} #{service}:#{to}", success_codes: [0]) end |
.down_all_services ⇒ Object
Kills all running services
113 114 115 116 117 118 119 120 |
# File 'lib/maze/docker.rb', line 113 def down_all_services # This will fail to remove the network that maze is connected to # as it is still in use, that is ok to ignore so we pass success codes! # We set timeout to 0 so this kills the services rather than stopping them # as its quicker and they are stateless anyway. run_docker_compose_command('down -t 0', success_codes: [0, 256]) if compose_stack_exists? && @services_started @services_started = false end |
.down_service(service) ⇒ Object
Kills a running service
99 100 101 102 103 |
# File 'lib/maze/docker.rb', line 99 def down_service(service) # We set timeout to 0 so this kills the services rather than stopping them # as its quicker and they are stateless anyway. run_docker_compose_command("down -t 0 #{service}") end |
.exec(service, command, detach: false) ⇒ Object
Execute a command in an already running docker container. Use start_service to build and run the service before calling this method
This is equivalent to the Docker Compose ‘exec’ command: docs.docker.com/engine/reference/commandline/compose_exec/
66 67 68 69 70 |
# File 'lib/maze/docker.rb', line 66 def exec(service, command, detach: false) flags = detach ? " --detach" : "" run_docker_compose_command("exec #{flags} #{service} /bin/sh -c '#{command}'") end |
.last_command_logs Provides access to the output from the last run docker command=(Providesaccesstotheoutputfromthelastrundockercommand = (value)) ⇒ Object
16 |
# File 'lib/maze/docker.rb', line 16 attr_accessor :last_command_logs |
.last_exit_code Provides access to the exit code of the last run docker command=(Providesaccesstotheexitcodeofthelastrundockercommand = (value)) ⇒ Object
13 |
# File 'lib/maze/docker.rb', line 13 attr_accessor :last_exit_code |
.reset ⇒ Object
Resets any state ready for the next scenario
106 107 108 109 110 |
# File 'lib/maze/docker.rb', line 106 def reset down_all_services @last_exit_code = nil @last_command_logs = nil end |
.start_service(service, command: nil, interactive: false) ⇒ Object
Builds and starts a service, using a command if given. If running a command, it will be executed as an attached process, otherwise it will run detached.
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 |
# File 'lib/maze/docker.rb', line 25 def start_service(service, command: nil, interactive: false) if interactive run_docker_compose_command("build #{service}") # Run the built service in an interactive session. The service _must_ # have an appropriate entrypoint, e.g. '/bin/sh'. We also disable ANSI # escape sequences from docker-compose as they can cause issues with # stderr expectations by 'leaking' into the next line command = get_docker_compose_command("--no-ansi run #{service} #{command}") cli = Runner.start_interactive_session(command) cli.on_exit do |status| @last_exit_code = status @last_command_logs = cli.stdout_lines + cli.stderr_lines end # The logs and exit code aren't available from the interactive session # at this point (we've just started it!) so we can't provide them here @last_command_logs = [] @last_exit_code = nil elsif command # We build the service before running it as there is no --build # option for run. run_docker_compose_command("build #{service}") run_docker_compose_command("run --use-aliases #{service} #{command}") else # TODO: Consider adding a logs command here run_docker_compose_command("up -d --build #{service}") end @services_started = true end |