Class: Docker::Rails::App

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/docker/rails/app.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



23
24
# File 'lib/docker/rails/app.rb', line 23

def initialize
end

Instance Attribute Details

#compose_configObject (readonly)

Returns the value of attribute compose_config.



6
7
8
# File 'lib/docker/rails/app.rb', line 6

def compose_config
  @compose_config
end

#compose_filenameObject (readonly)

Returns the value of attribute compose_filename.



6
7
8
# File 'lib/docker/rails/app.rb', line 6

def compose_filename
  @compose_filename
end

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/docker/rails/app.rb', line 6

def config
  @config
end

#exit_codeObject (readonly)

Returns the value of attribute exit_code.



6
7
8
# File 'lib/docker/rails/app.rb', line 6

def exit_code
  @exit_code
end

Class Method Details

.configured(target, options) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/docker/rails/app.rb', line 12

def configured(target, options)
  app = App.instance
  if app.is_configured?
    # puts "Already configured"
  else
    app.configure(Thor::CoreExt::HashWithIndifferentAccess.new(target: target).merge(options))
  end
  app
end

Instance Method Details

#bash_connect(service_name) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
# File 'lib/docker/rails/app.rb', line 199

def bash_connect(service_name)
  # docker exec -it 2ed97d0bb938 bash
  container = get_container(service_name)
  if container.nil?
    puts "#{service_name} does not appear to be running for build #{build}"
    return
  end

  exec "docker exec -it #{container.id} bash"
  container
end

#before_commandObject



119
120
121
122
# File 'lib/docker/rails/app.rb', line 119

def before_command
  before_command = @config['before_command']
  (exec before_command unless before_command.nil?) #unless skip? :before_command
end

#composeObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/docker/rails/app.rb', line 40

def compose
  # Write a docker-compose.yml with interpolated variables
  @compose_filename = compose_filename_from project_name

  rm_compose

  @config.write_docker_compose_file(@compose_filename)

  @compose_config = Docker::Rails::ComposeConfig.new
  @compose_config.load!(nil, @compose_filename)

  # check the exit_code
  if @config['exit_code'].nil?
    first_defined_service = @compose_config.keys[0]
    puts "exit_code not set in configuration, using exit code from first defined service: #{first_defined_service}"
    @config['exit_code'] = first_defined_service
  end
end

#compose_buildObject



129
130
131
132
# File 'lib/docker/rails/app.rb', line 129

def compose_build
  # Run the compose configuration
  exec_compose 'build'
end

#configure(options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/docker/rails/app.rb', line 26

def configure(options)
  # Allow CLI option `build` to fallback to an env variable DOCKER_RAILS_BUILD.  Note that CLI provides a default build value of 1, so check against the default and existence of the env var.
  build = options[:build]
  build = ENV['DOCKER_RAILS_BUILD'] if build.to_i == 1 && !ENV['DOCKER_RAILS_BUILD'].nil?
  ENV['DOCKER_RAILS_BUILD'] = build

  target = options[:target]

  # load the docker-rails.yml
  @config = Docker::Rails::Config.new({build: build, target: target})
  @config.load!(target)
  @is_configured = true
end

#create_gemset_volumeObject

Create global gems data volume to cache gems for this version of ruby

https://docs.docker.com/userguide/dockervolumes/


213
214
215
216
217
218
219
220
221
222
# File 'lib/docker/rails/app.rb', line 213

def create_gemset_volume
  begin
    Docker::Container.get(gemset_volume_name)
    puts "Gem data volume container #{gemset_volume_name} already exists."
  rescue Docker::Error::NotFoundError => e

    exec "docker create -v #{gemset_volume_path} --name #{gemset_volume_name} busybox"
    puts "Gem data volume container #{gemset_volume_name} created."
  end
end

#extract(container, service_name, extractions) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/docker/rails/app.rb', line 94

def extract(container, service_name, extractions)
  extractions.each do |extraction|
    if extraction =~ /:/
      tokens = extraction.split(':')
      from = tokens[0]
      to = tokens[1]
    else
      from = extraction
      to = '.'
    end

    puts "\nExtracting #{service_name} #{from} to #{to}"
    begin
      extract_files(container, from, to)
    rescue => e
      puts e.message
    end
  end
end

#extract_allObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/docker/rails/app.rb', line 63

def extract_all

  # For each container, process extractions
  #  Containers are defined in compose, extractions are defined at root under container name e.g.:
  #     web:
  #         extract:
  #         - '<from_container>:<to_host>'
  #         - '/project/target:.'
  #         - '/project/vcr'      # same as extract to '.'
  #         - '/project/tmp'
  #         - '/project/spec/dummy/log:spec/dummy'
  #         - '/project/tmp/parallel_runtime_cucumber.log:./tmp'
  #         - '/project/tmp/parallel_runtime_rspec.log:./tmp'

  @compose_config.each_key do |service_name|
    service_config = @config[service_name]
    extractions = service_config[:extract] unless service_config.nil?
    next if extractions.nil?

    puts "\n\nProcessing extract for #{service_name}:"
    puts '---------------------------------'
    container = get_container(service_name) rescue nil
    if container.nil?
      puts 'none.'
      next
    end

    extract(container, service_name, extractions)
  end
end

#is_configured?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/docker/rails/app.rb', line 59

def is_configured?
  @is_configured || false
end

#psObject



134
135
136
137
# File 'lib/docker/rails/app.rb', line 134

def ps
  # Run the compose configuration
  exec_compose 'ps'
end

#ps_allObject



139
140
141
142
143
# File 'lib/docker/rails/app.rb', line 139

def ps_all
  puts "\n\nAll remaining containers..."
  puts '-----------------------------'
  exec 'docker ps -a'
end

#rm_composeObject



114
115
116
117
# File 'lib/docker/rails/app.rb', line 114

def rm_compose
  # Delete old docker compose files
  exec "rm #{compose_filename_from '*'}" rescue ''
end

#rm_danglingObject



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/docker/rails/app.rb', line 182

def rm_dangling
  puts "\n\nCleaning up dangling images..."
  puts '-----------------------------'

  list_images_cmd = 'docker images --filter dangling=true -q'
  output = exec(list_images_cmd, true)

  # if there are any dangling, let's clean them up.
  exec("#{list_images_cmd} | xargs docker rmi", false, true) if !output.nil? && output.length > 0
  puts 'Done.'
end

#rm_gemset_volumeObject



224
225
226
227
228
229
230
231
# File 'lib/docker/rails/app.rb', line 224

def rm_gemset_volume
  begin
    container = Docker::Container.get(gemset_volume_name)
    rm_v(container)
  rescue Docker::Error::NotFoundError => e
    puts "Gem data volume container #{gemset_volume_name} does not exist."
  end
end

#rm_volumesObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/docker/rails/app.rb', line 167

def rm_volumes
  puts "\n\nRemoving container volumes..."
  puts '-----------------------------'

  # http://docs.docker.com/v1.7/reference/api/docker_remote_api_v1.19/#remove-a-container
  containers = Docker::Container.all(all: true)
  containers.each do |container|
    if is_project_container?(container)
      puts container.name
      rm_v(container)
    end
  end
  puts 'Done.'
end

#run_service_command(service_name, command) ⇒ Object



194
195
196
197
# File 'lib/docker/rails/app.rb', line 194

def run_service_command(service_name, command)
  # Run the compose configuration
  exec_compose("run #{service_name} #{command}", false, '', true)
end

#stop_allObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/docker/rails/app.rb', line 145

def stop_all
  puts "\n\n\n\nStopping containers..."
  puts '-----------------------------'
  containers = Docker::Container.all(all: true)
  containers.each do |container|
    if is_project_container?(container)
      stop(container)

      service_name = container.compose.service
      if @config['exit_code'].eql?(service_name)
        if container.up?
          puts "Unable to determine exit code, the #{service_name} is still up, current status: #{container.status}"
          @exit_code = -999
        else
          @exit_code = container.exit_code
        end
      end
    end
  end
  puts 'Done.'
end

#up(options = '') ⇒ Object



124
125
126
127
# File 'lib/docker/rails/app.rb', line 124

def up(options = '')
  # Run the compose configuration
  exec_compose 'up', false, options #unless skip? :up
end