docker_rails_proxy 0.1.14 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5fad751379dd5d549c8011a29940d27331f621f795e51eabab58e3dcc1255333
4
- data.tar.gz: 39192659415b1591d866f09db17bde167208dfaf9a54e4d980820ee3e855720b
3
+ metadata.gz: 8446a39b234fb3588cdd8c13256daf3fca61fcb8507a16cca046721178c0fea1
4
+ data.tar.gz: ff209e1236606d59fcdb8e00522006911dff44edd1990f9c7e1e272b61dfa4f5
5
5
  SHA512:
6
- metadata.gz: 7138508ef6629f939d0c9639bfd9dfbc6fa57e23e5f3f7745998c856d2da0c258c8b16b58d9eb00f69fbae212f039b6d3341973704d8aebb5daaaf0396448f70
7
- data.tar.gz: 291bc344901a060ed5a7dd7bdaf7f48b75bab002ee982e0f323d459d3b7e7fbb11f8c42a60cad8195cab00b3945849deb657811a1708f72bd8f3fea231f15a9b
6
+ metadata.gz: 5eccccf5651aeab7c860049d66e79f5e5c6acb4e1c08af74e74a764d69d360bd62c1c4603775a6491f6b9f185cfc8ce1baea7491b45e4da0015aeaaca39a3dfd
7
+ data.tar.gz: bceae22e15ab76e82f0408c012edcb699be0ab03e28f45fbea1f3dfcc0b0bcdb928b899d82ea95ab077ef61e9f44b5229a28f94fddfafdd9ec0b44431ae741b0
@@ -1,3 +1,5 @@
1
+ require 'optparse'
2
+
1
3
  Dir[File.expand_path('../docker_rails_proxy/extends/*.rb', __FILE__)].map do |f|
2
4
  require f
3
5
  end
@@ -27,7 +29,14 @@ module DockerRailsProxy
27
29
  include Rsync
28
30
  include Logger
29
31
 
30
- attr_reader :arguments, :additional_arguments
32
+ attr_accessor :additional_arguments_options
33
+ attr_reader :arguments, :additional_arguments
34
+
35
+ after_initialize { self.additional_arguments_options = {} }
36
+ after_initialize { additional_arguments_parser.parse!(additional_arguments) }
37
+ after_initialize do
38
+ DockerRailsProxy.log_level(level: additional_arguments_options[:debug])
39
+ end
31
40
 
32
41
  def initialize(arguments:, additional_arguments:)
33
42
  @arguments = arguments
@@ -52,7 +61,7 @@ module DockerRailsProxy
52
61
 
53
62
  klass.send :_run_before_initialize_callbacks
54
63
 
55
- resource = klass.new(options)
64
+ resource = klass.new(**options)
56
65
 
57
66
  klass.send :_run_after_initialize_callbacks, resource: resource
58
67
 
@@ -71,7 +80,7 @@ module DockerRailsProxy
71
80
  raise NotImplementedError, 'Subclasses must implement a process method'
72
81
  end
73
82
 
74
- private
83
+ private
75
84
 
76
85
  def build_path(*args, &block)
77
86
  self.class.build_path(*args, &block)
@@ -84,16 +93,16 @@ module DockerRailsProxy
84
93
 
85
94
  def get_option(values, default: nil, array: false)
86
95
  flush_stdin
87
- print ": "
96
+ print ': '
88
97
  option = $stdin.gets.chomp
89
98
 
90
99
  return default if option.blank?
91
100
 
92
101
  if array
93
102
  options = option.split(',')
94
- return nil unless options.map{|o| o =~ NUMBER_REGEXP }.all?
103
+ return unless options.all? { |o| o =~ NUMBER_REGEXP }
95
104
 
96
- options.map{|o| values[o.to_i].split(',') }.flatten.compact.uniq.join(',')
105
+ options.map { |o| values[o.to_i].split(',') }.flatten.compact.uniq.join(',')
97
106
  else
98
107
  values[option.to_i] if option =~ NUMBER_REGEXP
99
108
  end
@@ -104,7 +113,7 @@ module DockerRailsProxy
104
113
 
105
114
  while value.nil? do
106
115
  print_options(values, message)
107
- value = get_option(values, options)
116
+ value = get_option(values, **options)
108
117
  end
109
118
 
110
119
  value
@@ -115,6 +124,23 @@ module DockerRailsProxy
115
124
  Timeout::timeout(0.1) { $stdin.gets.chomp } rescue break
116
125
  end
117
126
  end
127
+
128
+ def additional_arguments_parser
129
+ @additional_arguments_parser ||= OptionParser.new do |opts|
130
+ opts.banner = "Usage: bin/#{APP_NAME} #{self.class.name.demodulize.parameterize} [<arguments>] -- [<additional_arguments>]".bold
131
+
132
+ opts.on('--debug', 'Debug mode') do |debug|
133
+ additional_arguments_options[:debug] = ::Logger::DEBUG
134
+ end
135
+
136
+ yield opts if block_given?
137
+
138
+ opts.on('-h', '--help', 'Display this screen') do
139
+ puts opts
140
+ exit
141
+ end
142
+ end
143
+ end
118
144
  end
119
145
 
120
146
  class AwsCli < Base
@@ -169,10 +195,9 @@ module DockerRailsProxy
169
195
  end
170
196
 
171
197
  after_initialize { self.docker_options = {} }
172
- after_initialize { docker_arguments_parser.parse!(additional_arguments) }
173
198
  after_initialize :set_app_container_id
174
199
 
175
- private
200
+ private
176
201
 
177
202
  def docker_compose?
178
203
  !!Docker.kubernetes_running == false
@@ -216,32 +241,23 @@ module DockerRailsProxy
216
241
  replace_process ? exec(command) : system(command)
217
242
  end
218
243
 
219
- def docker_arguments_parser
220
- @docker_arguments_parser ||= OptionParser.new do |opts|
221
- opts.banner = "Usage: bin/#{APP_NAME} #{self.class.name.demodulize.parameterize} -- [<docker exec arguments>]".bold
222
-
223
- opts.on('-w', '--workdir WORKDIR', 'Working directory inside the container') do |workdir|
244
+ def additional_arguments_parser
245
+ super do |opts|
246
+ opts.on('--docker-workdir WORKDIR', 'Working directory inside the container') do |workdir|
224
247
  docker_options['-w'] = workdir
225
248
  end
226
249
 
227
- opts.on('-u', '--user USER', 'Username or UID (format: <name|uid>[:<group|gid>])') do |user|
250
+ opts.on('--docker-user USER', 'Username or UID (format: <name|uid>[:<group|gid>])') do |user|
228
251
  docker_options['-u'] = user
229
252
  end
230
253
 
231
- opts.on('-t', '--tty', 'Allocate a pseudo-TTY') do |tty|
254
+ opts.on('--docker-tty', 'Allocate a pseudo-TTY') do |tty|
232
255
  docker_options['-t'] = nil
233
256
  end
234
257
 
235
- opts.on('-i', '--interactive', 'Keep STDIN open even if not attached') do |interactive|
258
+ opts.on('--docker-interactive', 'Keep STDIN open even if not attached') do |interactive|
236
259
  docker_options['-i'] = nil
237
260
  end
238
-
239
- yield opts if block_given?
240
-
241
- opts.on('-h', '--help', 'Display this screen') do
242
- puts opts
243
- exit
244
- end
245
261
  end
246
262
  end
247
263
  end
@@ -2,8 +2,8 @@ module DockerRailsProxy
2
2
  class Cli
3
3
  class << self
4
4
  def invoke(arguments)
5
- command, *all_arguments = arguments
6
- arguments, additional_arguments = split_arguments(all_arguments)
5
+ command, *all_arguments = arguments
6
+ arguments, additional_arguments = split_arguments(all_arguments)
7
7
 
8
8
  if command.nil?
9
9
  $stderr.puts <<-EOF
@@ -16,7 +16,7 @@ module DockerRailsProxy
16
16
 
17
17
  if COMMANDS.include? command
18
18
  arguments << '-h' if arguments.empty?
19
- "DockerRailsProxy::#{command}".constantize.(
19
+ "DockerRailsProxy::#{command}".constantize.call(
20
20
  arguments: arguments, additional_arguments: additional_arguments
21
21
  )
22
22
  else
@@ -39,9 +39,10 @@ module DockerRailsProxy
39
39
  private
40
40
 
41
41
  def split_arguments(arguments)
42
- arguments = arguments.join(' ') if arguments.is_a?(Array)
43
- arguments, additional_arguments = arguments.split('-- ')
44
- [arguments.split(' '), additional_arguments.to_s.split(' ')]
42
+ return [arguments, []] unless (separator_index = arguments.index('--'))
43
+
44
+ arguments.delete_at(separator_index)
45
+ arguments.partition.with_index { |_, index| index < separator_index }
45
46
  end
46
47
  end
47
48
  end
@@ -1,5 +1,3 @@
1
- require 'optparse'
2
-
3
1
  module DockerRailsProxy
4
2
  class Build < Docker
5
3
  attr_accessor :options
@@ -1,5 +1,4 @@
1
1
  require 'pty'
2
- require 'optparse'
3
2
 
4
3
  module DockerRailsProxy
5
4
  class Compose < Docker
@@ -52,7 +51,7 @@ module DockerRailsProxy
52
51
  seed if from_scratch?
53
52
  end
54
53
 
55
- private
54
+ private
56
55
 
57
56
  def seed
58
57
  logger.info "Seeding #{APP_NAME} data"
@@ -77,10 +76,12 @@ module DockerRailsProxy
77
76
  end
78
77
 
79
78
  def fswatch_start
79
+ logger.debug "fswatch command: #{FSWATCH_CMD}"
80
80
  logger.info 'fswatch has been started'
81
+
81
82
  PTY.spawn(FSWATCH_CMD) do |stdout, stdin, pid|
82
83
  begin
83
- stdout.each { |path| sync_or_kill(path: path, pid: pid) }
84
+ stdout.each { |path| sync_or_kill(path: path.strip, pid: pid) }
84
85
  rescue Errno::EIO
85
86
  logger.error EIO_ERROR
86
87
  end
@@ -98,7 +99,9 @@ module DockerRailsProxy
98
99
  return if stopping?
99
100
 
100
101
  if branch_name == (branch = current_branch)
102
+ logger.debug("syncing #{path}")
101
103
  sync source: path
104
+ logger.debug("#{path} synced")
102
105
  else
103
106
  $stderr.puts %(
104
107
  `git checkout #{branch}` was detected, stopping fswatch
@@ -1,5 +1,3 @@
1
- require 'optparse'
2
-
3
1
  module DockerRailsProxy
4
2
  class DataBags < AwsCli
5
3
  autoload :Pull, 'docker_rails_proxy/commands/data_bags/pull'
@@ -1,5 +1,3 @@
1
- require 'optparse'
2
-
3
1
  module DockerRailsProxy
4
2
  class Kubectl < Base
5
3
  autoload :Bash, 'docker_rails_proxy/commands/kubectl/bash'
@@ -12,7 +10,7 @@ module DockerRailsProxy
12
10
  end
13
11
 
14
12
  validates do
15
- unless File.exist? KUBECONFIG_PATH
13
+ unless File.exist?(KUBECONFIG_PATH)
16
14
  <<-EOS
17
15
 
18
16
  kubeconfig is required, run this command to set it
@@ -21,7 +19,7 @@ module DockerRailsProxy
21
19
  end unless arguments.first == 'set-kubeconfig'
22
20
  end
23
21
 
24
- builds -> (params:) do
22
+ builds ->(params:) do
25
23
  case params[:arguments].first
26
24
  when 'bash' then Bash
27
25
  when 'set-kubeconfig' then SetKubeconfig
@@ -32,10 +30,12 @@ module DockerRailsProxy
32
30
  kubectl arguments.join(' ')
33
31
  end
34
32
 
35
- private
33
+ private
36
34
 
37
35
  def kubectl(command)
38
- exec "kubectl --kubeconfig='#{KUBECONFIG_PATH}' #{command}"
36
+ cmd = "kubectl --kubeconfig='#{KUBECONFIG_PATH}' #{command}"
37
+ cmd << " -- #{additional_arguments.join(', ')}" unless additional_arguments.empty?
38
+ exec cmd
39
39
  end
40
40
 
41
41
  def kubectl_output(command)
@@ -1,20 +1,23 @@
1
- require 'optparse'
2
1
  require 'json'
3
2
 
4
3
  module DockerRailsProxy
5
4
  class Kubectl < Base
6
5
  class Bash < self
6
+ UNNEEDED_ATTRIBUTES = %w[livenessProbe readinessProbe command lifecycle].freeze
7
+
7
8
  attr_accessor :data
8
9
 
9
10
  before_process do
10
- self.data = JSON.parse kubectl_output("get pod #{pod} -o json")
11
+ self.data = JSON.parse(
12
+ kubectl_output("get pod #{cloned_pod_name} -o json")
13
+ )
11
14
  end
12
15
 
13
- def process
14
- %w[livenessProbe readinessProbe command lifecycle].each do |attr|
15
- container.delete attr
16
- end
16
+ before_process do
17
+ UNNEEDED_ATTRIBUTES.each { |attribute| container.delete(attribute) }
18
+ end
17
19
 
20
+ def process
18
21
  overrides = {
19
22
  metadata: {
20
23
  annotations: {
@@ -50,28 +53,27 @@ module DockerRailsProxy
50
53
  pod_name = "#{container['name']}-bash-#{Time.now.strftime '%Y%m%d%H%M%S'}"
51
54
  puts "Starting #{pod_name} pod ..."
52
55
 
53
- kubectl <<-EOS
56
+ kubectl <<-RUN_COMMAND
54
57
  run #{pod_name} --rm -i --tty \
55
58
  --image='#{container['image']}' \
56
- --generator='run-pod/v1' \
57
59
  --overrides='#{overrides.to_json}'
58
- EOS
60
+ RUN_COMMAND
59
61
  end
60
62
 
61
- private
63
+ private
62
64
 
63
- def pod
64
- @pod_name ||= begin
65
- pods = kubectl_output(<<-EOS).split(" ")
65
+ def cloned_pod_name
66
+ @cloned_pod_name ||= begin
67
+ pods = kubectl_output(<<-GET_PODS).split(" ")
66
68
  get pods -o jsonpath='{range .items[*]}{.metadata.name},{.status.phase}{" "}{end}'}
67
- EOS
69
+ GET_PODS
68
70
 
69
71
  pods = pods.map do |values|
70
72
  name, phase = values.split(',')
71
73
  phase == 'Running' ? name : nil
72
74
  end.compact
73
75
 
74
- until_get_option pods, "Choose a pod and press [ENTER]"
76
+ until_get_option(pods, "Choose a pod and press [ENTER]")
75
77
  end
76
78
  end
77
79
 
@@ -80,9 +82,9 @@ module DockerRailsProxy
80
82
  containers = data.dig('spec', 'containers')
81
83
 
82
84
  if containers.size > 1
83
- names = containers.map{|c| c['name'] }
84
- name = until_get_option names, "Choose a container and press [ENTER]"
85
- containers.detect{|c| c['name'] == name }
85
+ names = containers.map { |container| container['name'] }
86
+ name = until_get_option(names, "Choose a container and press [ENTER]")
87
+ containers.find { |container| container['name'] == name }
86
88
  else
87
89
  containers.first
88
90
  end
@@ -1,5 +1,4 @@
1
1
  require 'fileutils'
2
- require 'optparse'
3
2
  require 'yaml'
4
3
 
5
4
  module DockerRailsProxy
@@ -2,6 +2,16 @@ module DockerRailsProxy
2
2
  class Rails < SyncBack
3
3
  attr_reader :args
4
4
 
5
+ autoload :Credentials, 'docker_rails_proxy/commands/rails/credentials'
6
+
7
+ builds ->(params:) do
8
+ case params[:arguments].first
9
+ when 'credentials' then Credentials
10
+ else
11
+ DockerRailsProxy::Rails
12
+ end
13
+ end
14
+
5
15
  def process
6
16
  command, *@args = arguments
7
17
 
@@ -11,13 +21,12 @@ module DockerRailsProxy
11
21
  when 'logs' then logs
12
22
  when 'restart', 'touch' then restart
13
23
  when 'secrets' then secrets
14
- when 'credentials' then credentials
15
24
  else
16
25
  execute "bin/rails #{command} #{args.join(' ')}", tty: true
17
26
  end
18
27
  end
19
28
 
20
- private
29
+ private
21
30
 
22
31
  def console
23
32
  execute "bin/rails c #{args.join(' ')}", tty: true, replace_process: true
@@ -52,8 +61,8 @@ module DockerRailsProxy
52
61
  end
53
62
 
54
63
  def credentials
55
- #environment = args.first
56
- #raise 'Invalid environment' if environment.present? && !%w[development stage].include?(environment)
64
+ # Temporary solution to use credentials in Rails 5.2
65
+ # Future version will use the --environment argument instead
57
66
  command = "RAILS_ENV=#{args.first} EDITOR=vim bin/rails credentials:edit"
58
67
  execute "bash -c '#{command}'", tty: true
59
68
  end
@@ -0,0 +1,34 @@
1
+ module DockerRailsProxy
2
+ class Rails < SyncBack
3
+ class Credentials < self
4
+ attr_accessor :options
5
+
6
+ after_initialize { self.options = {} }
7
+ after_initialize { opt_parser.parse!(arguments) }
8
+
9
+ validates { '--environment is required' if options[:environment].nil? }
10
+
11
+ def process
12
+ command = "EDITOR=vim bin/rails credentials:edit --environment #{options[:environment]}"
13
+ execute "bash -c '#{command}'", tty: true
14
+ end
15
+
16
+ private
17
+
18
+ def opt_parser
19
+ @opt_parser ||= OptionParser.new do |opts|
20
+ opts.banner = "Usage: bin/#{APP_NAME} rails credentials [options]"
21
+
22
+ opts.on(
23
+ '--environment ENVIRONMENT', 'App environment'
24
+ ) { |environment| options[:environment] = environment }
25
+
26
+ opts.on('-h', '--help', 'Display this screen') do
27
+ puts opts
28
+ exit
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,5 +1,3 @@
1
- require 'optparse'
2
-
3
1
  module DockerRailsProxy
4
2
  class Ssh < DockerMainApp
5
3
  def process
@@ -1,5 +1,3 @@
1
- require 'optparse'
2
-
3
1
  module DockerRailsProxy
4
2
  class Stack < AwsCli
5
3
  autoload :Create, 'docker_rails_proxy/commands/stack/create'
@@ -50,18 +50,10 @@ module DockerRailsProxy
50
50
  def _make_lambda(callback:)
51
51
  case callback
52
52
  when Symbol
53
- ->(resource, *rest) { resource.send(callback, *rest) }
53
+ ->(resource, *args, **kw_args) { resource.send(callback, *args, **kw_args) }
54
54
  when ::Proc
55
- if callback.arity <= 0
56
- ->(resource) { resource.instance_exec(&callback) }
57
- else
58
- ->(resource, *rest) do
59
- if rest.empty?
60
- resource.instance_exec(resource, &callback)
61
- else
62
- resource.instance_exec(*rest, &callback)
63
- end
64
- end
55
+ lambda do |resource, *args, **kw_args|
56
+ resource.instance_exec(*args, **kw_args, &callback)
65
57
  end
66
58
  else
67
59
  ->(*) {}
@@ -1,6 +1,18 @@
1
1
  require 'logger'
2
2
 
3
3
  module DockerRailsProxy
4
+ def self.logger
5
+ @logger ||= ::Logger.new(STDOUT).tap do |log|
6
+ log.formatter = proc do |severity, datetime, progname, msg|
7
+ "#{severity[0]}, [#{datetime.strftime('%Y-%m-%d %T')}] -- : #{msg}\n"
8
+ end
9
+ end
10
+ end
11
+
12
+ def self.log_level(level: nil)
13
+ logger.level = level || ::Logger::INFO
14
+ end
15
+
4
16
  module Logger
5
17
  class << self
6
18
  def included(base)
@@ -10,16 +22,12 @@ module DockerRailsProxy
10
22
 
11
23
  module ClassMethods
12
24
  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
25
+ ::DockerRailsProxy.logger
18
26
  end
19
27
  end
20
28
 
21
29
  def logger
22
- self.class.logger
30
+ ::DockerRailsProxy.logger
23
31
  end
24
32
  end
25
33
  end
@@ -31,7 +31,7 @@ module DockerRailsProxy
31
31
  _rsync.sync source: source, reverse: reverse, silent: silent
32
32
  end
33
33
 
34
- private
34
+ private
35
35
 
36
36
  def _rsync_host
37
37
  @_rsync_host ||= begin
@@ -64,15 +64,16 @@ module DockerRailsProxy
64
64
  end
65
65
 
66
66
  def sync(options)
67
- return if source_in_exclusions?(options)
68
- source, target, volume = normalize_options(options)
67
+ return if source_in_exclusions?(**options)
68
+
69
+ source, target, volume = normalize_options(**options)
69
70
 
70
- result = send "sync_#{volume}", {
71
- source: source, target: target, reverse: options[:reverse]
72
- }
71
+ result = send(
72
+ "sync_#{volume}", source: source, target: target, reverse: options[:reverse]
73
+ )
73
74
 
74
75
  if result && options[:silent].eql?(false)
75
- puts "#{source} =======> #{target}"
76
+ DockerRailsProxy.logger.info "#{source} =======> #{target}"
76
77
  end
77
78
 
78
79
  result
@@ -1,3 +1,3 @@
1
1
  module DockerRailsProxy
2
- VERSION = '0.1.14'
2
+ VERSION = '0.2.1'
3
3
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docker_rails_proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.14
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jairo
8
8
  - Vázquez
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-03-14 00:00:00.000000000 Z
12
+ date: 2021-07-28 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Configures docker-compose and provides rails command helpers
15
15
  email:
@@ -37,6 +37,7 @@ files:
37
37
  - lib/docker_rails_proxy/commands/kubectl/bash.rb
38
38
  - lib/docker_rails_proxy/commands/kubectl/set_kubeconfig.rb
39
39
  - lib/docker_rails_proxy/commands/rails.rb
40
+ - lib/docker_rails_proxy/commands/rails/credentials.rb
40
41
  - lib/docker_rails_proxy/commands/rake.rb
41
42
  - lib/docker_rails_proxy/commands/rspec.rb
42
43
  - lib/docker_rails_proxy/commands/spring.rb
@@ -60,7 +61,7 @@ homepage: https://github.com/jairovm/docker_rails_proxy
60
61
  licenses:
61
62
  - MIT
62
63
  metadata: {}
63
- post_install_message:
64
+ post_install_message:
64
65
  rdoc_options: []
65
66
  require_paths:
66
67
  - lib
@@ -75,8 +76,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
76
  - !ruby/object:Gem::Version
76
77
  version: '0'
77
78
  requirements: []
78
- rubygems_version: 3.0.6
79
- signing_key:
79
+ rubygems_version: 3.1.4
80
+ signing_key:
80
81
  specification_version: 4
81
82
  summary: docker, docker-compose and rails wrapper
82
83
  test_files: []