Class: RubyYacht::Runner::RunContainers

Inherits:
Command
  • Object
show all
Defined in:
lib/ruby_yacht/runner/run_containers.rb

Overview

This class provides a command for running all the images in new containers.

It will also remove any existing containers with conflicting names.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

#backtick, #default_project, #docker, #get_machine_info, #log, #option_parser, #parse_positional_arguments, #project_named, #projects, short_script_name, #system

Class Method Details

.commandObject

THe name of the command.


7
# File 'lib/ruby_yacht/runner/run_containers.rb', line 7

def self.command; 'run_containers'; end

.descriptionObject

The short description of the command.


10
# File 'lib/ruby_yacht/runner/run_containers.rb', line 10

def self.description; 'Runs containers for all the apps'; end

Instance Method Details

#dns_server_flagsObject

This method gets the flags for defining DNS server config when running a container.


37
38
39
40
41
42
43
44
# File 'lib/ruby_yacht/runner/run_containers.rb', line 37

def dns_server_flags
  flags = []
  if @project.dns_server
    flags += @project.dns_server.servers.map { |server| "--dns=#{server}" }
    flags += @project.dns_server.search_domains.map { |domain| "--dns-search=#{domain}" }
  end
  flags
end

#remove_container(container_name) ⇒ Object

This method removes a container.

Parameters

  • container_name: String The full name of the container.

93
94
95
# File 'lib/ruby_yacht/runner/run_containers.rb', line 93

def remove_container(container_name)  
  IO.popen("docker rm -f #{container_name}", err: :close) {}
end

#runObject

This method runs the logic of the command.


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby_yacht/runner/run_containers.rb', line 13

def run
  @network = projects.first.system_prefix
  
  projects.each do |project|
    @project = project
    
    project.databases.select(&:local?).each do |database|
      run_container database.container_label
    end
    
    project.apps.each do |app|
      run_container app.name
    end
  end
  
  @project = projects.first
  
  run_container :web
  
  true
end

#run_container(name) ⇒ Object

This method runs a container.

It will also remove any existing containers with conflicting names.

Parameters

  • name: Symbol The name of the container, not including the system prefix.

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ruby_yacht/runner/run_containers.rb', line 69

def run_container(name)
  if name == @project.system_prefix
    container_name = name
  else
    container_name = "#{@project.system_prefix}-#{name}"
  end
  remove_container container_name
  
  flags = ["-d"]
  flags += dns_server_flags
  flags += volume_flags(name)
  flags << "-p 80:80" if name == :web
  flags << "--net=#{@network}"
  flags << "--net-alias=#{container_name}"

  flag_text = flags.join(' ')
  docker "run #{flag_text} --name=#{container_name} #{container_name}"
end

#volume_flags(container_name) ⇒ Object

This method gets the flags for mapping the code volume to a local path for a container.

Parameters

  • container_name: Symbol The name of the container, not including the system prefix.

53
54
55
56
57
58
59
# File 'lib/ruby_yacht/runner/run_containers.rb', line 53

def volume_flags(container_name)
  if @project.check_out_locally && ![:database, :web].include?(container_name)
    return ["-v $PWD/../#{container_name}:/var/code"]
  else
    return []
  end
end