Class: Docker::Rails::App
- Inherits:
-
Object
- Object
- Docker::Rails::App
- Includes:
- Singleton
- Defined in:
- lib/docker/rails/app.rb
Instance Attribute Summary collapse
-
#compose_config ⇒ Object
readonly
Returns the value of attribute compose_config.
-
#compose_filename ⇒ Object
readonly
Returns the value of attribute compose_filename.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#exit_code ⇒ Object
readonly
Returns the value of attribute exit_code.
Class Method Summary collapse
Instance Method Summary collapse
- #after_build_command ⇒ Object
- #bash_connect(service_name) ⇒ Object
- #before_command ⇒ Object
- #compose ⇒ Object
- #compose_build ⇒ Object
- #configure(options) ⇒ Object
-
#create_dockito_vault ⇒ Object
Create a dockito vault container for serving private keys github.com/dockito/vault.
- #dockito_vault_enabled? ⇒ Boolean
-
#dockito_vault_name ⇒ Object
FIXME this needs to by dynamic to this run?.
- #extract(container, service_name, extractions) ⇒ Object
- #extract_all ⇒ Object
-
#initialize ⇒ App
constructor
A new instance of App.
- #is_configured? ⇒ Boolean
- #ps ⇒ Object
- #ps_all ⇒ Object
- #rm_compose ⇒ Object
- #rm_dangling ⇒ Object
- #rm_dockito_vault ⇒ Object
- #rm_exited ⇒ Object
- #rm_volumes ⇒ Object
- #run_service_command(service_name, command) ⇒ Object
- #stop_all ⇒ Object
- #up(options = '') ⇒ Object
Constructor Details
#initialize ⇒ App
Returns a new instance of App.
23 24 |
# File 'lib/docker/rails/app.rb', line 23 def initialize end |
Instance Attribute Details
#compose_config ⇒ Object (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_filename ⇒ Object (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 |
#config ⇒ Object (readonly)
Returns the value of attribute config.
6 7 8 |
# File 'lib/docker/rails/app.rb', line 6 def config @config end |
#exit_code ⇒ Object (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, ) app = App.instance if app.is_configured? # puts "Already configured" else app.configure(Thor::CoreExt::HashWithIndifferentAccess.new(target: target).merge()) end app end |
Instance Method Details
#after_build_command ⇒ Object
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_command ⇒ Object
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 |
#compose ⇒ Object
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_build ⇒ Object
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() # 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 = [:build] build = ENV['DOCKER_RAILS_BUILD'] if build.to_i == 1 && !ENV['DOCKER_RAILS_BUILD'].nil? ENV['DOCKER_RAILS_BUILD'] = build target = [:target] # load the docker-rails.yml @config = Docker::Rails::Config.new({build: build, target: target}) @config.load!(target) @is_configured = true end |
#create_dockito_vault ⇒ Object
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
68 69 70 |
# File 'lib/docker/rails/app.rb', line 68 def dockito_vault_enabled? @config[:dockito][:vault] || false end |
#dockito_vault_name ⇒ Object
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. end end end |
#extract_all ⇒ Object
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
92 93 94 |
# File 'lib/docker/rails/app.rb', line 92 def is_configured? @is_configured || false end |
#ps ⇒ Object
172 173 174 175 |
# File 'lib/docker/rails/app.rb', line 172 def ps # Run the compose configuration exec_compose 'ps' end |
#ps_all ⇒ Object
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_compose ⇒ Object
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_dangling ⇒ Object
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_vault ⇒ Object
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_exited ⇒ Object
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_volumes ⇒ Object
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_all ⇒ Object
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( = '') # Run the compose configuration exec_compose 'up', false, #unless skip? :up end |