Skip to content

Commit

Permalink
Add hot reload dev docker (#78)
Browse files Browse the repository at this point in the history
* Add hot reload dev docker
  • Loading branch information
TheFehr authored May 6, 2019
1 parent 2f413ec commit 69cae94
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 7 deletions.
4 changes: 3 additions & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/vendor/
/vendor/
backend
*.swp
37 changes: 37 additions & 0 deletions backend/.realize.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
settings:
files:
outputs:
status: false
path: ""
name: .r.outputs.log
logs:
status: false
path: ""
name: .r.logs.log
errors:
status: false
path: ""
name: .r.errors.log
legacy:
force: false
interval: 0s
schema:
- name: orsum-inflandi-II/backend
path: /go/src/github.com/orsa-scholis/orsum-inflandi-II/backend
args:
- -verbose
commands:
install:
status: true
run:
status: true
watcher:
extensions:
- go
paths:
- /
ignore:
paths:
- .git
- .realize
- vendor
11 changes: 11 additions & 0 deletions backend/dev.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:1.12

WORKDIR /go/src/github.com/orsa-scholis/orsum-inflandi-II/backend

ENV CGO_ENABLED=0
ENV GO111MODULE=on

RUN go get github.com/oxequa/realize

EXPOSE 4560
ENTRYPOINT ["realize", "start"]
4 changes: 1 addition & 3 deletions backend/go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
github.com/google/logger v1.0.0 h1:SS5v3IgoIXtXYrTqtGYaGaxGeZmeXum9oinQbkQ1Z6s=
github.com/google/logger v1.0.0/go.mod h1:tQN+I/DyBt051hEHNEzPgIeyy/GD1WJaKbqPScoDKdY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190415060736-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422173550-953cdadca894 h1:I1H82UvcKKs7LLgUebogm16Tu+RUN38zBjQUOr7kvuY=
golang.org/x/sys v0.0.0-20190422173550-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
File renamed without changes.
1 change: 1 addition & 0 deletions cli/app
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module OrsumInflandi
option :backend, type: :boolean, default: true
option :frontend, type: :boolean, default: true
option :force_build, type: :boolean
option :dev, type: :boolean, default: false

def start
require_relative 'commands/start'
Expand Down
36 changes: 33 additions & 3 deletions cli/commands/start.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# frozen_string_literal: true

require 'etc'
require_relative '../logger'

module OrsumInflandi
module Commands
class Start
BACKEND_IMAGE_NAME = 'orsa-scholis/orsum-inflandi'
BACKEND_IMAGE_NAME = 'orsa-scholis/orsum-inflandi-ii'
BACKEND_DEV_IMAGE_NAME = 'orsa-scholis/orsum-inflandi-ii-dev'

def initialize(options)
@options = options
end

def run
threads = []
threads << Thread.new(Dir.pwd, &method(:start_backend)) if @options[:backend]
threads << backend_thread if @options[:backend]
threads << Thread.new(&method(:start_frontend)) if @options[:frontend]

%w[INT TERM].each do |signal|
Expand All @@ -25,6 +27,10 @@ def run

private

def backend_thread
Thread.new(Dir.pwd) { |directory| @options[:dev] ? start_dev_backend(directory) : start_backend(directory) }
end

def kill_threads(threads)
puts "\n"
Logger.new('Received KILL SIGNAL, shutting down...').info_log
Expand All @@ -38,14 +44,34 @@ def execute_command(command, &block)

def build_backend(directory)
backend_log('Building image')
execute_command(%W[docker build -t #{BACKEND_IMAGE_NAME} #{directory}/backend], &method(:backend_log))
execute_command(
%W[docker build -f #{directory}/backend/prod.Dockerfile -t #{BACKEND_IMAGE_NAME} #{directory}/backend],
&method(:backend_log)
)
end

def build_dev_backend(directory)
backend_log('Building dev image')
execute_command(
%W[docker build -f #{directory}/backend/dev.Dockerfile -t #{BACKEND_DEV_IMAGE_NAME} #{directory}/backend],
&method(:backend_log)
)
end

def start_backend(directory)
build_backend(directory) unless backend_image_exists?
execute_command(%W[docker run --rm -p 4560:4560 #{BACKEND_IMAGE_NAME} --verbose], &method(:backend_log))
end

def start_dev_backend(directory)
build_dev_backend(directory) unless backend_dev_image_exists?
container_intern_path = '/go/src/github.com/orsa-scholis/orsum-inflandi-II/backend'
execute_command(
%W[docker run --rm -p 4560:4560 -v #{directory}/backend:#{container_intern_path} #{BACKEND_DEV_IMAGE_NAME}],
&method(:backend_log)
)
end

def start_frontend
Dir.chdir('frontend') { execute_command(%w[yarn run start], &method(:frontend_log)) }
end
Expand All @@ -54,6 +80,10 @@ def backend_image_exists?
system "[ \"$(docker images -q #{BACKEND_IMAGE_NAME} 2>/dev/null)\" != \"\" ]"
end

def backend_dev_image_exists?
system "[ \"$(docker images -q #{BACKEND_DEV_IMAGE_NAME} 2>/dev/null)\" != \"\" ]"
end

def frontend_log(log)
Logger.new(log).frontend_log
end
Expand Down

0 comments on commit 69cae94

Please sign in to comment.