docker_rails_proxy 0.0.12 → 0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a3a12d0b5a75064d9d2536c11ccd90898c8d88f6
4
- data.tar.gz: 279c5c537357f88c68cad0dd49ba48d7b2b224d8
3
+ metadata.gz: e054684446e381e85f5a034bdf97b68f34cecbc8
4
+ data.tar.gz: '08563be16ae0fae359a12fe46c799cee34fd4785'
5
5
  SHA512:
6
- metadata.gz: 8d9e1bbfb5714a48d34b7b42846677db5b5278876b066a4289fa465b1d2a3baa465e6743700078f43a39641fea4943aca27bb6c8ccc6d9fc0b55a004929c04be
7
- data.tar.gz: d59f6de86cc746002b3f59a9bfcf3d4d4ddaf0aebae75f8e8bbc4018f81c0c6de70b2da1a864853f5de1702c33092a4cdcaf25e4e4f8e0009eec161e769e386c
6
+ metadata.gz: a4868a69273f6b96a5a78a14d81072616399bf091cc91f5a19dd7ef9504911fb33df03b9aa04523b9f3ac41290de24a200d516141615b5c78029bba1d34bb736
7
+ data.tar.gz: 8997b53995275162011777f74e5b7c5a03c0db763c5231b5c1764a8b9a02fd8baab3196fe492b54bc30cb562a2aeacf60f62ac088eb67b9004961a592bd1c367
@@ -2,7 +2,7 @@ module DockerRailsProxy
2
2
  class Bundle < SyncBack
3
3
  def process
4
4
  command, *args = arguments
5
- system "docker exec -it #{APP_NAME} bundle #{command} #{args.join(' ')}"
5
+ execute "bundle #{command} #{args.join(' ')}"
6
6
  end
7
7
  end
8
8
  end
@@ -2,6 +2,8 @@ module DockerRailsProxy
2
2
  class Compose < Docker
3
3
  class Down < self
4
4
  def process
5
+ return unless docker_machine?
6
+
5
7
  containers = %x(
6
8
  docker-compose ps | grep '#{APP_NAME}' | awk '{print $1}' | xargs
7
9
  ).strip.split(' ')
@@ -13,7 +13,7 @@ EOS
13
13
 
14
14
  def process
15
15
  File.write(OVERRIDE_PATH, OVERRIDE_FILE)
16
- puts "#{OVERRIDE_PATH} was created"
16
+ logger.info "#{OVERRIDE_PATH} was created"
17
17
  end
18
18
  end
19
19
  end
@@ -3,7 +3,7 @@ module DockerRailsProxy
3
3
  class Proxy < self
4
4
  def process
5
5
  command, *args = arguments
6
- system "docker-compose #{command} #{args.join(' ')}"
6
+ system "docker-compose #{command} #{args.join(' ')}" if docker_machine?
7
7
  end
8
8
  end
9
9
  end
@@ -9,7 +9,7 @@ module DockerRailsProxy
9
9
  --latency=0.1 \
10
10
  --exclude '.git' \
11
11
  --exclude '4913' \
12
- "#{APP_PATH}" "#{GEMS_PATH}" | xargs -0 -n1 -I{} echo {}
12
+ "#{[APP_PATH, GEMS_PATH].compact.join('" "')}" | xargs -0 -n1 -I{} echo {}
13
13
  EOS
14
14
 
15
15
  EIO_ERROR = %(
@@ -34,55 +34,64 @@ module DockerRailsProxy
34
34
  after_initialize :parse_options!, :set_defaults
35
35
 
36
36
  before_process do
37
- self.from_scratch = %x(docker-compose ps | grep -c #{APP_NAME}).to_i.zero?
37
+ self.from_scratch = if docker_machine?
38
+ %x(docker-compose ps | grep -c #{APP_NAME}).to_i.zero?
39
+ elsif minikube?
40
+ %x(kubectl get deploy -l 'app=#{APP_NAME}' -o name).empty?
41
+ end
38
42
  end
39
43
 
40
44
  after_process { fswatch_start }
41
45
 
42
46
  def process
43
- system 'docker-compose up -d'
47
+ system 'docker-compose up -d' if docker_machine?
44
48
  sync_app_and_gems_folders
45
- run_bundle_install if options[:bundle]
46
- seed if from_scratch?
49
+
50
+ set_app_container_id if app_container_id.empty?
51
+ run_bundle_install if options[:bundle]
52
+ seed if from_scratch?
47
53
  end
48
54
 
49
55
  private
50
56
 
51
57
  def seed
52
- system <<-EOS.strip.freeze
53
- docker exec #{APP_NAME} bin/rake db:drop db:create db:migrate db:seed
54
- EOS
58
+ logger.info "Seeding #{APP_NAME} data"
59
+
60
+ loop do
61
+ break if execute "bin/rake db:reset"
62
+ sleep 5
63
+ end
55
64
  end
56
65
 
57
66
  def sync_app_and_gems_folders
58
67
  rsync_app
59
- sync source: GEMS_PATH
68
+ sync source: GEMS_PATH if GEMS_PATH.present?
60
69
  end
61
70
 
62
71
  def rsync_app
63
72
  loop do
64
73
  break if sync(source: APP_PATH)
65
- puts "waiting for rsync-volume service on #{_rsync_host}"
66
- sleep 2
74
+ logger.info "waiting for rsync-volume service on #{_rsync_host}"
75
+ sleep 5
67
76
  end
68
77
  end
69
78
 
70
79
  def fswatch_start
71
- puts 'fswatch has been started'
80
+ logger.info 'fswatch has been started'
72
81
  PTY.spawn(FSWATCH_CMD) do |stdout, stdin, pid|
73
82
  begin
74
83
  stdout.each { |path| sync_or_kill(path: path, pid: pid) }
75
84
  rescue Errno::EIO
76
- $stderr.puts EIO_ERROR
85
+ logger.error EIO_ERROR
77
86
  end
78
87
  end
79
88
 
80
89
  rescue PTY::ChildExited
81
- $stderr.puts '"The fswatch process exited!'
90
+ logger.error '"The fswatch process exited!'
82
91
 
83
92
  # Captures Ctrl-C
84
93
  rescue Exception
85
- puts 'fswatch has been stopped'
94
+ logger.info 'fswatch has been stopped'
86
95
  end
87
96
 
88
97
  def sync_or_kill(path:, pid:)
@@ -106,7 +115,7 @@ module DockerRailsProxy
106
115
 
107
116
  # since new gems may have been added, we need bundle install
108
117
  def run_bundle_install
109
- system "docker exec #{APP_NAME} bundle install"
118
+ execute "bundle install"
110
119
  end
111
120
 
112
121
  def parse_options!
@@ -10,7 +10,7 @@ module DockerRailsProxy
10
10
  --profile '#{options[:profile]}'
11
11
  EOS
12
12
 
13
- puts "Data bags pulled from #{options[:bucket_path]} to #{options[:folder]}"
13
+ logger.info "Data bags pulled from #{options[:bucket_path]} to #{options[:folder]}"
14
14
  end
15
15
  end
16
16
  end
@@ -29,7 +29,7 @@ module DockerRailsProxy
29
29
  --sse aws:kms
30
30
  EOS
31
31
 
32
- puts "Data bags pushed from #{options[:folder]} to #{options[:bucket_path]}"
32
+ logger.info "Data bags pushed from #{options[:folder]} to #{options[:bucket_path]}"
33
33
  end
34
34
  end
35
35
  end
@@ -9,31 +9,38 @@ module DockerRailsProxy
9
9
  when 'logs' then logs args
10
10
  when 'restart', 'touch' then restart
11
11
  else
12
- system "docker exec -it #{APP_NAME} bin/rails #{command} #{args.join(' ')}"
12
+ execute "bin/rails #{command} #{args.join(' ')}", tty: true
13
13
  end
14
14
  end
15
15
 
16
16
  private
17
17
 
18
18
  def console(args)
19
- exec "docker exec -it #{APP_NAME} bin/rails c #{args.join(' ')}"
19
+ execute "bin/rails c #{args.join(' ')}", tty: true, replace_process: true
20
20
  end
21
21
 
22
22
  def db(args)
23
- exec <<-EOS
24
- docker exec -it #{APP_NAME}_db \
25
- mysql #{args.first || "#{APP_NAME}_development"}
26
- EOS
23
+ container_id = get_docker_container_id(:mysql)
24
+
25
+ if container_id.empty?
26
+ logger.error "Couldn't connect to mysql container, make sure it's running"
27
+ exit 1
28
+ end
29
+
30
+ execute(
31
+ "mysql #{args.first || "#{APP_NAME}_development"}",
32
+ container_id: container_id,
33
+ tty: true,
34
+ replace_process: true
35
+ )
27
36
  end
28
37
 
29
38
  def logs(args)
30
- exec <<-EOS
31
- docker exec #{APP_NAME} tail -f log/#{args.first || 'development'}.log
32
- EOS
39
+ execute "tail -f log/#{args.first || 'development'}.log", replace_process: true
33
40
  end
34
41
 
35
42
  def restart
36
- exec "docker exec #{APP_NAME} touch tmp/restart.txt"
43
+ execute "touch tmp/restart.txt", replace_process: true
37
44
  end
38
45
  end
39
46
  end
@@ -2,7 +2,7 @@ module DockerRailsProxy
2
2
  class Rake < SyncBack
3
3
  def process
4
4
  command, *args = arguments
5
- system "docker exec -it #{APP_NAME} bin/rake #{command} #{args.join(' ')}"
5
+ execute "bin/rake #{command} #{args.join(' ')}"
6
6
  end
7
7
  end
8
8
  end
@@ -2,7 +2,7 @@ module DockerRailsProxy
2
2
  class Rspec < SyncBack
3
3
  def process
4
4
  command, *args = arguments
5
- system "docker exec #{APP_NAME} bin/rspec #{command} #{args.join(' ')}"
5
+ execute "bin/rspec #{command} #{args.join(' ')}"
6
6
  end
7
7
  end
8
8
  end
@@ -2,7 +2,7 @@ module DockerRailsProxy
2
2
  class Spring < DockerMainApp
3
3
  def process
4
4
  command, *args = arguments
5
- system "docker exec #{APP_NAME} bin/spring #{command} #{args.join(' ')}"
5
+ execute "bin/spring #{command} #{args.join(' ')}"
6
6
  end
7
7
  end
8
8
  end
@@ -9,7 +9,7 @@ module DockerRailsProxy
9
9
  when '-h', '--help'
10
10
  opt_parser.parse %w(-h)
11
11
  else
12
- system "docker exec -it #{APP_NAME} #{command} #{args.join(' ')}"
12
+ execute "#{command} #{args.join(' ')}", tty: true
13
13
  end
14
14
  end
15
15
 
@@ -2,7 +2,7 @@ module DockerRailsProxy
2
2
  class Webpack < SyncBack
3
3
  def process
4
4
  command, *args = arguments
5
- system "docker exec #{APP_NAME} bin/webpack #{command} #{args.join(' ')}"
5
+ execute "bin/webpack #{command} #{args.join(' ')}"
6
6
  end
7
7
  end
8
8
  end
@@ -2,7 +2,7 @@ module DockerRailsProxy
2
2
  class WebpackDevServer < DockerMainApp
3
3
  def process
4
4
  command, *args = arguments
5
- system "docker exec #{APP_NAME} bin/webpack-dev-server #{command} #{args.join(' ')}"
5
+ execute "bin/webpack-dev-server #{command} #{args.join(' ')}"
6
6
  end
7
7
  end
8
8
  end
@@ -2,7 +2,7 @@ module DockerRailsProxy
2
2
  class Yarn < SyncBack
3
3
  def process
4
4
  command, *args = arguments
5
- system "docker exec #{APP_NAME} bin/yarn #{command} #{args.join(' ')}"
5
+ execute "bin/yarn #{command} #{args.join(' ')}"
6
6
  end
7
7
  end
8
8
  end
@@ -0,0 +1,25 @@
1
+ require 'logger'
2
+
3
+ module DockerRailsProxy
4
+ module Logger
5
+ class << self
6
+ def included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+ end
10
+
11
+ module ClassMethods
12
+ def logger
13
+ @logger ||= ::Logger.new(STDOUT).tap do |log|
14
+ log.formatter = proc do |severity, datetime, progname, msg|
15
+ "> #{datetime.strftime '%Y-%m-%d %T'} -- #{msg}\n"
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ def logger
22
+ self.class.logger
23
+ end
24
+ end
25
+ end
@@ -31,7 +31,16 @@ module DockerRailsProxy
31
31
 
32
32
  def _rsync_host
33
33
  @_rsync_host ||= begin
34
- "rsync://#{ENV['DOCKER_HOST'].to_s.sub('tcp://', '').sub(/:\d+$/, '')}:10873"
34
+ ip = ENV['DOCKER_HOST'].to_s.sub('tcp://', '').sub(/:\d+$/, '')
35
+ port = '10873'
36
+
37
+ loop do
38
+ break if (port = %x(kubectl get svc "#{APP_NAME}" -o jsonpath='{.spec.ports[?(@.name=="rsync")].nodePort}' 2> /dev/null).strip).present?
39
+ sleep 5
40
+ logger.info 'Waiting for rsync port ...'
41
+ end if Docker.vm_provisioner == :minikube
42
+
43
+ "rsync://#{ip}:#{port}"
35
44
  end
36
45
  end
37
46
 
@@ -67,7 +76,7 @@ module DockerRailsProxy
67
76
  def normalize_options(source:, reverse: false, **)
68
77
  values = if source.include? APP_PATH
69
78
  ["#{APP_PATH}/", 'app']
70
- elsif source.include? GEMS_PATH
79
+ elsif GEMS_PATH.present? and source.include?(GEMS_PATH)
71
80
  ["#{GEMS_PATH}/", 'gems']
72
81
  else
73
82
  $stderr.puts "There is no rsync volume related with this path: #{source}"
@@ -1,3 +1,3 @@
1
1
  module DockerRailsProxy
2
- VERSION = '0.0.12'
2
+ VERSION = '0.1'
3
3
  end
@@ -16,6 +16,7 @@ module DockerRailsProxy
16
16
  autoload :InheritableAttributes, 'docker_rails_proxy/concerns/inheritable_attributes'
17
17
  autoload :Callbacks, 'docker_rails_proxy/concerns/callbacks'
18
18
  autoload :Rsync, 'docker_rails_proxy/concerns/rsync'
19
+ autoload :Logger, 'docker_rails_proxy/concerns/logger'
19
20
 
20
21
  autoload :Cli, 'docker_rails_proxy/cli'
21
22
  autoload :VERSION, 'docker_rails_proxy/version'
@@ -24,6 +25,7 @@ module DockerRailsProxy
24
25
  include InheritableAttributes
25
26
  include Callbacks
26
27
  include Rsync
28
+ include Logger
27
29
 
28
30
  attr_reader :arguments
29
31
 
@@ -41,9 +43,7 @@ module DockerRailsProxy
41
43
  end
42
44
 
43
45
  def execute(options)
44
- system <<-EOS
45
- #{build_path("bin/#{APP_NAME}")} #{command} #{options}
46
- EOS
46
+ system "#{build_path("bin/#{APP_NAME}")} #{command} #{options}"
47
47
  end
48
48
 
49
49
  def call(options)
@@ -125,22 +125,71 @@ module DockerRailsProxy
125
125
  end
126
126
 
127
127
  class Docker < Base
128
+ class << self
129
+ attr_accessor :vm_provisioner
130
+ end
131
+
132
+ attr_accessor :app_container_id
133
+
128
134
  before_initialize do
129
135
  if ENV['DOCKER_HOST'].nil?
130
136
  %(
131
137
  Couldn't connect to Docker daemon you might need to run:
132
138
  docker-machine start default
133
- eval $(docker-machine env default)
139
+ eval $(docker-machine env default) or eval $(minikube docker-env)
134
140
  )
135
141
  end
136
142
  end
143
+
144
+ before_initialize do
145
+ Docker.vm_provisioner = ENV['DOCKER_CERT_PATH'].include?('.minikube') ? :minikube : :docker_machine
146
+ end
147
+
148
+ before_initialize do
149
+ unless system 'type kubectl &> /dev/null'
150
+ 'kubectl is required, `brew install kubectl`'
151
+ end if Docker.vm_provisioner == :minikube
152
+ end
153
+
154
+ after_initialize :set_app_container_id
155
+
156
+ private
157
+
158
+ def docker_machine?
159
+ Docker.vm_provisioner == :docker_machine
160
+ end
161
+
162
+ def minikube?
163
+ Docker.vm_provisioner == :minikube
164
+ end
165
+
166
+ def set_app_container_id
167
+ self.app_container_id = get_docker_container_id(APP_NAME)
168
+ end
169
+
170
+ def get_docker_container_id(app, container: nil)
171
+ if docker_machine?
172
+ %x(docker ps -q --filter "name=^/#{[app, container].compact.join('_')}$").strip
173
+ elsif minikube?
174
+ pod_name = %x(kubectl get pod -l "app=#{app}" 2> /dev/null | grep Running | awk '{print $1}').strip
175
+ return '' if pod_name.empty?
176
+
177
+ full_id = %x(kubectl get pod #{pod_name} -o jsonpath='{ .status.containerStatuses[?(@.name=="#{container || app}")].containerID }').strip
178
+ full_id.split('//').last
179
+ end
180
+ end
181
+
182
+ def execute(command, tty: false, container_id: app_container_id, replace_process: false, **)
183
+ command = "docker exec #{tty ? '-ti' : nil} #{container_id} #{command}"
184
+ replace_process ? exec(command) : system(command)
185
+ end
137
186
  end
138
187
 
139
188
  class DockerMainApp < Docker
140
- before_initialize do
141
- unless system "docker ps | grep '#{APP_NAME}$' &> /dev/null"
189
+ after_initialize do
190
+ if app_container_id.empty?
142
191
  %(
143
- Couldn't connect to #{APP_NAME} container you might need to run:
192
+ Couldn't connect to #{APP_NAME}'s container you might need to run:
144
193
  bin/#{APP_NAME} compose up
145
194
  )
146
195
  end
@@ -151,7 +200,7 @@ module DockerRailsProxy
151
200
  end
152
201
 
153
202
  class SyncBack < DockerMainApp
154
- after_process { system "docker exec #{APP_NAME} chown -R nobody:nogroup ." }
203
+ after_process { execute "chown -R nobody:nogroup ." }
155
204
  after_process { sync source: APP_PATH, reverse: true }
156
205
  end
157
206
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docker_rails_proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: '0.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jairo
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-12-02 00:00:00.000000000 Z
12
+ date: 2017-12-22 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Configures docker-compose and provides rails command helpers
15
15
  email:
@@ -49,6 +49,7 @@ files:
49
49
  - lib/docker_rails_proxy/commands/yarn.rb
50
50
  - lib/docker_rails_proxy/concerns/callbacks.rb
51
51
  - lib/docker_rails_proxy/concerns/inheritable_attributes.rb
52
+ - lib/docker_rails_proxy/concerns/logger.rb
52
53
  - lib/docker_rails_proxy/concerns/rsync.rb
53
54
  - lib/docker_rails_proxy/extends/colorization.rb
54
55
  - lib/docker_rails_proxy/extends/fixnum_support.rb