Class: Docker::Compose::Session
- Inherits:
-
Object
- Object
- Docker::Compose::Session
- Defined in:
- lib/docker/compose/session.rb
Overview
A Ruby OOP interface to a docker-compose session. A session is bound to a particular directory and docker-compose file (which are set at initialize time) and invokes whichever docker-compose command is resident in $PATH.
Run docker-compose commands by calling instance methods of this class and passing kwargs that are equivalent to the CLI options you would pass to the command-line tool.
Note that the Ruby command methods usually expose a subset of the options allowed by the docker-compose CLI, and that options are sometimes renamed for clarity, e.g. the “-d” flag always becomes the “detached:” kwarg.
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Instance Method Summary collapse
-
#config(*args) ⇒ Hash
Validate docker-compose file and return it as Hash.
-
#initialize(shell = Backticks::Runner.new(interactive: true), dir: Dir.pwd, file: 'docker-compose.yml') ⇒ Session
constructor
A new instance of Session.
-
#kill(*services, signal: 'KILL') ⇒ Object
Forcibly stop running services.
-
#logs(*services) ⇒ true
Monitor the logs of one or more containers.
-
#pause(*services) ⇒ Object
Pause running services.
-
#port(service, port, protocol: 'tcp', index: 1) ⇒ Object
Figure out which host a port a given service port has been published to.
- #ps ⇒ Object
- #rm(*services, force: false, volumes: false) ⇒ Object
-
#run(service, *cmd, detached: false, no_deps: false, env: [], env_vars: nil, rm: false) ⇒ Object
Idempotently run an arbitrary command with a service container.
-
#run!(*args) ⇒ String
Run a docker-compose command without validating that the CLI parameters make sense.
-
#stop(*services, timeout: 10) ⇒ Object
Stop running services.
-
#unpause(*services) ⇒ Object
Unpause running services.
-
#up(*services, detached: false, timeout: 10, no_build: false, no_deps: false) ⇒ true
Idempotently up the given services in the project.
-
#version(short: false) ⇒ String, Hash
Determine the installed version of docker-compose.
Constructor Details
#initialize(shell = Backticks::Runner.new(interactive: true), dir: Dir.pwd, file: 'docker-compose.yml') ⇒ Session
Returns a new instance of Session.
20 21 22 23 24 25 |
# File 'lib/docker/compose/session.rb', line 20 def initialize(shell = Backticks::Runner.new(interactive: true), dir:Dir.pwd, file:'docker-compose.yml') @shell = shell @dir = dir @file = file end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
18 19 20 |
# File 'lib/docker/compose/session.rb', line 18 def dir @dir end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
18 19 20 |
# File 'lib/docker/compose/session.rb', line 18 def file @file end |
Instance Method Details
#config(*args) ⇒ Hash
Validate docker-compose file and return it as Hash
30 31 32 33 |
# File 'lib/docker/compose/session.rb', line 30 def config(*args) config = run!('config', *args) YAML.load(config) end |
#kill(*services, signal: 'KILL') ⇒ Object
Forcibly stop running services.
129 130 131 |
# File 'lib/docker/compose/session.rb', line 129 def kill(*services, signal:'KILL') run!('kill', { s: signal }, services) end |
#logs(*services) ⇒ true
Monitor the logs of one or more containers.
39 40 41 42 |
# File 'lib/docker/compose/session.rb', line 39 def logs(*services) run!('logs', services) true end |
#pause(*services) ⇒ Object
Pause running services.
107 108 109 |
# File 'lib/docker/compose/session.rb', line 107 def pause(*services) run!('pause', *services) end |
#port(service, port, protocol: 'tcp', index: 1) ⇒ Object
Figure out which host a port a given service port has been published to.
139 140 141 |
# File 'lib/docker/compose/session.rb', line 139 def port(service, port, protocol:'tcp', index:1) run!('port', { protocol: protocol, index: index }, service, port) end |
#ps ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/docker/compose/session.rb', line 44 def ps() inter = @shell.interactive @shell.interactive = false lines = run!('ps', q:true).split(/[\r\n]+/) containers = Collection.new lines.each do |id| containers << docker_ps(id) end containers ensure @shell.interactive = inter end |
#rm(*services, force: false, volumes: false) ⇒ Object
79 80 81 |
# File 'lib/docker/compose/session.rb', line 79 def rm(*services, force:false, volumes:false) run!('rm', { f: force, v: volumes }, services) end |
#run(service, *cmd, detached: false, no_deps: false, env: [], env_vars: nil, rm: false) ⇒ Object
Idempotently run an arbitrary command with a service container.
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/docker/compose/session.rb', line 94 def run(service, *cmd, detached:false, no_deps:false, env:[], env_vars:nil, rm:false) # handle deprecated kwarg if (env.nil? || env.empty?) && !env_vars.nil? env = env_vars end env_params = env.map { |v| { e: v } } run!('run', { d: detached, no_deps: no_deps, rm: rm }, *env_params, service, cmd) end |
#run!(*args) ⇒ String
Run a docker-compose command without validating that the CLI parameters make sense. Prepend project and file options if suitable.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/docker/compose/session.rb', line 172 def run!(*args) project_opts = { file: @file } Dir.chdir(@dir) do cmd = @shell.run('docker-compose', project_opts, *args).join status = cmd.status out = cmd.captured_output err = cmd.captured_error status.success? || fail(Error.new(args.first, status, err)) out end end |
#stop(*services, timeout: 10) ⇒ Object
Stop running services.
121 122 123 |
# File 'lib/docker/compose/session.rb', line 121 def stop(*services, timeout:10) run!('stop', { timeout: timeout }, services) end |
#unpause(*services) ⇒ Object
Unpause running services.
113 114 115 |
# File 'lib/docker/compose/session.rb', line 113 def unpause(*services) run!('unpause', *services) end |
#up(*services, detached: false, timeout: 10, no_build: false, no_deps: false) ⇒ true
Idempotently up the given services in the project.
71 72 73 74 75 76 77 |
# File 'lib/docker/compose/session.rb', line 71 def up(*services, detached:false, timeout:10, no_build:false, no_deps:false) run!('up', { d: detached, timeout: timeout, no_build: no_build, no_deps: no_deps }, services) true end |
#version(short: false) ⇒ String, Hash
Determine the installed version of docker-compose.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/docker/compose/session.rb', line 148 def version(short:false) result = run!('version', short: short, file: false, dir: false) if short result.strip else lines = result.split(/[\r\n]+/) lines.inject({}) do |h, line| kv = line.split(/: +/, 2) h[kv.first] = kv.last h end end end |