Class: Dev::Docker::Compose

Inherits:
Object show all
Defined in:
lib/firespring_dev_commands/docker/compose.rb

Overview

Class containing methods for interfacing with the docker compose cli

Defined Under Namespace

Classes: Config

Constant Summary collapse

EXECUTABLE_NAME =
TODO:

Change this to "docker compose" when everyone is off v1

The name of the docker compose executable

'docker-compose'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(compose_files: self.class.config.compose_files, environment: [], options: [], project_dir: self.class.config.project_dir, project_name: self.class.config.project_name, services: [], user: nil, volumes: [], capture: false) ⇒ Compose

Returns a new instance of Compose.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/firespring_dev_commands/docker/compose.rb', line 46

def initialize(
  compose_files: self.class.config.compose_files,
  environment: [],
  options: [],
  project_dir: self.class.config.project_dir,
  project_name: self.class.config.project_name,
  services: [],
  user: nil,
  volumes: [],
  capture: false
)
  @compose_files = Array(compose_files)
  @environment = environment
  @options = Array(options)
  @project_dir = project_dir
  @project_name = project_name
  @services = Array(services)
  @user = user
  @volumes = Array(volumes)
  @capture = capture
  check_version
end

Instance Attribute Details

#capture ⇒ Object

Returns the value of attribute capture.



44
45
46
# File 'lib/firespring_dev_commands/docker/compose.rb', line 44

def capture
  @capture
end

#compose_files ⇒ Object

Returns the value of attribute compose_files.



44
45
46
# File 'lib/firespring_dev_commands/docker/compose.rb', line 44

def compose_files
  @compose_files
end

#environment ⇒ Object

Returns the value of attribute environment.



44
45
46
# File 'lib/firespring_dev_commands/docker/compose.rb', line 44

def environment
  @environment
end

#options ⇒ Object

Returns the value of attribute options.



44
45
46
# File 'lib/firespring_dev_commands/docker/compose.rb', line 44

def options
  @options
end

#project_dir ⇒ Object

Returns the value of attribute project_dir.



44
45
46
# File 'lib/firespring_dev_commands/docker/compose.rb', line 44

def project_dir
  @project_dir
end

#project_name ⇒ Object

Returns the value of attribute project_name.



44
45
46
# File 'lib/firespring_dev_commands/docker/compose.rb', line 44

def project_name
  @project_name
end

#services ⇒ Object

Returns the value of attribute services.



44
45
46
# File 'lib/firespring_dev_commands/docker/compose.rb', line 44

def services
  @services
end

#user ⇒ Object

Returns the value of attribute user.



44
45
46
# File 'lib/firespring_dev_commands/docker/compose.rb', line 44

def user
  @user
end

#volumes ⇒ Object

Returns the value of attribute volumes.



44
45
46
# File 'lib/firespring_dev_commands/docker/compose.rb', line 44

def volumes
  @volumes
end

Class Method Details

.config {|@config| ... } ⇒ Object Also known as: configure

Instantiates a new top level config object if one hasn't already been created Yields that config object to any given block Returns the resulting config object

Yields:



25
26
27
28
29
# File 'lib/firespring_dev_commands/docker/compose.rb', line 25

def config
  @config ||= Config.new
  yield(@config) if block_given?
  @config
end

.version ⇒ Object

Returns the version of the docker-compose executable on the system



35
36
37
# File 'lib/firespring_dev_commands/docker/compose.rb', line 35

def version
  @version ||= `#{EXECUTABLE_NAME} --version`.match(/version v?([0-9.]+)/)[1]
end

Instance Method Details

#build ⇒ Object

Pull in supported env settings and call build Specify PULL=true to force compose to pull all backing images as part of the build Specify NO_CACHE=true to force compose to build from scratch rather than using build cache



83
84
85
86
87
88
# File 'lib/firespring_dev_commands/docker/compose.rb', line 83

def build
  merge_options('--parallel')
  merge_env_pull_option
  merge_env_cache_option
  execute_command(build_command('build'))
end

#check_version ⇒ Object

Checks the min and max version against the current docker version if they have been configured



70
71
72
73
74
75
76
77
78
# File 'lib/firespring_dev_commands/docker/compose.rb', line 70

def check_version
  min_version = self.class.config.min_version
  raise "requires #{EXECUTABLE_NAME} version >= #{min_version} (found #{self.class.version})" if min_version &&
                                                                                                 !Dev::Common.new.version_greater_than(min_version, self.class.version)

  max_version = self.class.config.max_version
  raise "requires #{EXECUTABLE_NAME} version < #{max_version} (found #{self.class.version})" if max_version &&
                                                                                                Dev::Common.new.version_greater_than(max_version, self.class.version)
end

#container_by_name(service_name, prefix = nil, status: [Docker::Status::RUNNING]) ⇒ Object

Get the first container matching the given name If prefix is specified then this method will filter for compose services in the given project only If status is specified then this method will filter containers in the given status only



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/firespring_dev_commands/docker/compose.rb', line 151

def container_by_name(service_name, prefix = nil, status: [Docker::Status::RUNNING])
  prefix ||= project_name
  containers = ::Docker::Container.all(filters: {status: Array(status), label: ["com.docker.compose.service=#{service_name}"]}.to_json)
  containers.each do |container|
    container&.info&.dig('Names')&.each do |name|
      return container if name.start_with?("/#{prefix}")
    end
  end

  raise "Container not found for #{service_name} with prefix #{prefix}"
end

#down ⇒ Object

Pull in supported env settings and call down Specify REMOVE_VOLUMES=true to also remove any unused volumes when the containers are stopped



118
119
120
121
# File 'lib/firespring_dev_commands/docker/compose.rb', line 118

def down
  merge_env_volumes_option
  execute_command(build_command('down'))
end

#exec(*args) ⇒ Object

Call the compose exec method passing the given args after it



129
130
131
# File 'lib/firespring_dev_commands/docker/compose.rb', line 129

def exec(*args)
  execute_command(build_command('exec', *args))
end

#logs ⇒ Object

Pull in supported env settings and call logs Specify NO_FOLLOW=true if you want to print current logs and exist Specify TAIL to pass tail options to the logs command



110
111
112
113
114
# File 'lib/firespring_dev_commands/docker/compose.rb', line 110

def logs
  merge_env_follow_option
  merge_env_tail_option
  execute_command(build_command('logs'))
end

#mapped_public_port(service_name, private_port) ⇒ Object

Gets the dynamic port which was assigned to the compose service on the original private port



164
165
166
167
168
# File 'lib/firespring_dev_commands/docker/compose.rb', line 164

def mapped_public_port(service_name, private_port)
  container = container_by_name(service_name)
  port_mapping = container.info['Ports'].find { |it| it['PrivatePort'] == private_port }
  port_mapping['PublicPort']
end

#pull ⇒ Object

Call the compose pull method



144
145
146
# File 'lib/firespring_dev_commands/docker/compose.rb', line 144

def pull
  execute_command(build_command('pull'))
end

#push ⇒ Object

Call the compose push method



139
140
141
# File 'lib/firespring_dev_commands/docker/compose.rb', line 139

def push
  execute_command(build_command('push'))
end

#run(*args) ⇒ Object

Call the compose run method passing the given args after it



134
135
136
# File 'lib/firespring_dev_commands/docker/compose.rb', line 134

def run(*args)
  execute_command(build_command('run', *args))
end

#sh(shell_commands = ['bash']) ⇒ Object

Exec into a running container and run the given shell commands Default to running 'bash' which will start a terminal in the running container



103
104
105
# File 'lib/firespring_dev_commands/docker/compose.rb', line 103

def sh(shell_commands = ['bash'])
  execute_command(build_command('exec', *shell_commands))
end

#stop ⇒ Object

Pull in supported env settings and call stop



124
125
126
# File 'lib/firespring_dev_commands/docker/compose.rb', line 124

def stop
  execute_command(build_command('stop'))
end

#up ⇒ Object

Pull in supported env settings and call up Specify BUILD=true to force/allow service builds before startup Specify NO_DEPS=true to only start the given service and ignore starting it's dependencies Specify DETACHED=false to not detach from the started processes



94
95
96
97
98
99
# File 'lib/firespring_dev_commands/docker/compose.rb', line 94

def up
  merge_env_build_option
  merge_env_deps_option
  merge_env_detach_option
  execute_command(build_command('up'))
end