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

#after_build_commandObject



157
158
159
160
# File 'lib/docker/rails/app.rb', line 157

def after_build_command
  after_build_command = @config['after_build_command']
  (exec after_build_command unless after_build_command.nil?)
end

#bash_connect(service_name) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
# File 'lib/docker/rails/app.rb', line 245

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



152
153
154
155
# File 'lib/docker/rails/app.rb', line 152

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

#composeObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/docker/rails/app.rb', line 73

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



167
168
169
170
# File 'lib/docker/rails/app.rb', line 167

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_dockito_vaultObject

Create a dockito vault container for serving private keys

https://github.com/dockito/vault


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/docker/rails/app.rb', line 42

def create_dockito_vault
  begin
    Docker::Container.get(dockito_vault_name)
    puts "Dockito vault container #{dockito_vault_name} already exists."
  rescue Docker::Error::NotFoundError => e

    # docker run --detach --name vault -p 14242:3000 -v ~/.ssh:/vault/.ssh dockito/vault:latest
    exec "docker run --detach --name #{dockito_vault_name} -p 14242:3000 -v ~/.ssh:/vault/.ssh dockito/vault:latest"
    puts "Dockito vault container #{dockito_vault_name} created."
  end
end

#dockito_vault_enabled?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/docker/rails/app.rb', line 68

def dockito_vault_enabled?
  @config[:dockito][:vault] || false
end

#dockito_vault_nameObject

FIXME this needs to by dynamic to this run?



64
65
66
# File 'lib/docker/rails/app.rb', line 64

def dockito_vault_name
  'vault' #@config[:dockito][:vault][:name]
end

#extract(container, service_name, extractions) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/docker/rails/app.rb', line 127

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/docker/rails/app.rb', line 96

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)


92
93
94
# File 'lib/docker/rails/app.rb', line 92

def is_configured?
  @is_configured || false
end

#psObject



172
173
174
175
# File 'lib/docker/rails/app.rb', line 172

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

#ps_allObject



177
178
179
180
181
# File 'lib/docker/rails/app.rb', line 177

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

#rm_composeObject



147
148
149
150
# File 'lib/docker/rails/app.rb', line 147

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

#rm_danglingObject



220
221
222
223
224
225
226
227
228
229
230
# File 'lib/docker/rails/app.rb', line 220

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_dockito_vaultObject



54
55
56
57
58
59
60
61
# File 'lib/docker/rails/app.rb', line 54

def rm_dockito_vault
  begin
    container = Docker::Container.get(dockito_vault_name)
    rm_v(container)
  rescue Docker::Error::NotFoundError => e
    puts "Dockito vault container #{dockito_vault_name} does not exist."
  end
end

#rm_exitedObject



232
233
234
235
236
237
238
# File 'lib/docker/rails/app.rb', line 232

def rm_exited
  puts "\n\nCleaning up exited containers..."
  puts '-----------------------------'

  exec('docker rm -v $(docker ps -a -q -f status=exited)', false, true)
  puts 'Done.'
end

#rm_volumesObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/docker/rails/app.rb', line 205

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



240
241
242
243
# File 'lib/docker/rails/app.rb', line 240

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

#stop_allObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/docker/rails/app.rb', line 183

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



162
163
164
165
# File 'lib/docker/rails/app.rb', line 162

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