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, #docker_machine, #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.



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

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.


88
89
90
# File 'lib/ruby_yacht/runner/run_containers.rb', line 88

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
# File 'lib/ruby_yacht/runner/run_containers.rb', line 13

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

    project.web_servers.each do |web_server|
      run_container web_server
    end
  end
  
  true
end

#run_container(server) ⇒ Object

This method runs a container.

It will also remove any existing containers with conflicting names.

Parameters

  • server: App/Database/WebServer The server we are running a container for.


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

def run_container(server)
  container_name = server.container_name
  remove_container container_name
  
  flags = ["-d"]
  flags += dns_server_flags
  flags += volume_flags(server)
  flags << "-p #{server.port}:80" if server.is_a?(RubyYacht::WebServer)
  flags << "--net=#{@network}"
  flags << "--net-alias=#{container_name}"

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

#volume_flags(server) ⇒ Object

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

Parameters

  • server: App/Database/WebServer The server we are running a container for.


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

def volume_flags(server)
  if @project.check_out_locally && server.is_a?(RubyYacht::App)
    return ["-v $PWD/../#{server.name}:/var/code"]
  else
    return []
  end
end