Class: RubyYacht::Runner::BuildImages

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

Overview

This command builds the images for the system.

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 identifier for the command.



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

def self.command; 'build_images'; end

.descriptionObject

The description for the command.



10
11
12
# File 'lib/ruby_yacht/runner/build_images.rb', line 10

def self.description
  "Build images for your environment"
end

Instance Method Details

#build_image(folder_name, image_name = nil) ⇒ Object

This method builds an image.

This will copy a Dockerfile template from a folder underneath the images folder, as well as any other files in that folder, into the tmp folder. It will then evaluate the Dockerfile.erb file, using the binding from the current context, and write it to the Dockerfile in the tmp folder. It will then build the image, and clear out the tmp folder.

### Parameters

  • **folder_name: String** The name of the folder containing the

    Dockerfile and related files.
    
  • **image_name: String** The name for the new image. If this is not

    provided, the image name will be the
    current project's prefix, followed by a
    dash, followed by the folder name.
    


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

def build_image(folder_name, image_name = nil)
  Dir[File.join(File.dirname(File.dirname(__FILE__)), 'images', folder_name, '*')].each do |path|
    FileUtils.cp(path, 'tmp')
  end
  
  File.open(File.join('tmp', 'Dockerfile'), 'w') do |file|
    contents = ERB.new(File.read(File.join('tmp', 'Dockerfile.erb'))).result(binding)
    file.write(contents)
  end
  
  image_name ||= "#{@project.system_prefix}-#{folder_name}"
  docker "build -t #{image_name} tmp"
  
  FileUtils.rm(Dir[File.join("tmp", "*")])
end

#runObject

This method runs the logic for the command.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_yacht/runner/build_images.rb', line 15

def run
  FileUtils.mkdir_p 'tmp'
  
  default_system_prefix = projects.first.system_prefix
  docker "network create #{default_system_prefix} 2> /dev/null"
  
  projects.each do |project|
    log "Building images for #{project.name}"
    @project = project
    
    id_path = File.join(ENV['HOME'], '.ssh', 'id_rsa')
    tmp_id_path = File.join('tmp', 'id_rsa')
    if File.exist?(id_path)
      FileUtils.cp(id_path, tmp_id_path)
    end
  
    build_image 'app-dependencies'
    build_image 'database' if project.database.local?
    
    @project.apps.each do |app|
      @app = app
      build_image 'app', "#{@project.system_prefix}-#{app.name}"
    end
    
    if @project.create_deploy_container
      build_image 'deploy'
    end
  end
  
  @projects = projects
  build_image 'web', "#{default_system_prefix}-web"
  
  true
end