-
Notifications
You must be signed in to change notification settings - Fork 26
/
commands.rb
252 lines (218 loc) · 7.96 KB
/
commands.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# typed: strict
require 'kuby/version'
require 'gli'
module Kuby
class Commands
extend T::Sig
extend GLI::App
# GLI doesn't have a wildcard option, so it's impossible to tell it to
# slurp up all args after a certain point. In our case, we want to be
# able to invoke `rails` commands and pass through all the original
# flags, switches, etc. To get around GLI's limitations, we identify
# `rails` commands in this hijacked `run` method and only use GLI to
# parse global options (like -e). The rest of the Rails options are
# captured in an instance variable and thereby made available to the
# Rails command handlers defined below. We use Module#prepend here to
# avoid the usual series of cryptic alias_method calls (note that there
# is no singleton class version of #prepend in the Ruby language).
singleton_class.send(:prepend, Module.new do
sig { params(args: T::Array[String]).void }
def run(args)
if idx = args.index('rails') || idx = args.index('rake')
@rails_options = T.let(@rails_options, T.nilable(T::Array[String]))
@rails_options = args[idx..-1]
super(args[0..idx])
else
super
end
end
end)
sig { returns(Kuby::Tasks) }
def self.tasks
Kuby::Tasks.new(Kuby.environment)
end
sig { void }
def self.must_be_dev_env!
unless Kuby.environment.development?
fail "Command not supported in the '#{Kuby.environment.name}' environment"
end
end
program_desc 'Kuby command-line interface. Kuby is a convention '\
'over configuration approach for running Rails apps in Kubernetes.'
version Kuby::VERSION
subcommand_option_handling :normal
arguments :loose
desc 'The Kuby environment to use. Overrides KUBY_ENV.'
flag [:e, :environment], type: String, required: false
desc 'Path to your Kuby config file. Overrides KUBY_CONFIG.'
default_value './kuby.rb'
flag [:c, :config]
pre do |global_options, options, args|
Kuby.env = global_options[:environment] if global_options[:environment]
Kuby.load!(global_options[:config])
# GLI will abort unless this block returns a truthy value
true
end
# These are only stubs included to fill out the help screens. Rails
# commands are handled by the RailsCommands class.
desc 'Runs a Rails command.'
command :rails do |rc|
rc.action do |global_options, options, args|
must_be_dev_env!
exit 1 unless tasks.dev_deployment_ok
@rails_options = T.let(@rails_options, T.nilable(T::Array[String]))
Kuby::RailsCommands.run(@rails_options)
end
rc.desc 'Runs the rails server (run `rails server --help` for options)'
rc.command [:server, :s] do |c|
c.action do |global_options, options, args|
must_be_dev_env!
exit 1 unless tasks.dev_deployment_ok
Kuby::RailsCommands.run(@rails_options)
end
end
rc.desc 'Runs a script in the Rails environment (run `rails runner --help` for options)'
rc.command [:runner, :r] do |c|
c.action do |global_options, options, args|
must_be_dev_env!
exit 1 unless tasks.dev_deployment_ok
Kuby::RailsCommands.run(@rails_options)
end
end
rc.desc 'Starts an interactive Ruby console with the Rails environment loaded '\
'(run `rails console --help` for options)'
rc.command [:console, :c] do |c|
c.action do |global_options, options, args|
must_be_dev_env!
exit 1 unless tasks.dev_deployment_ok
Kuby::RailsCommands.run(@rails_options)
end
end
end
desc 'Runs a rake task.'
command :rake do |rc|
rc.action do |global_options, options, args|
must_be_dev_env!
exit 1 unless tasks.dev_deployment_ok
Kuby::RailsCommands.run(@rails_options)
end
end
desc 'Builds the Docker image.'
command :build do |c|
c.action do |global_options, options, args|
tasks.build
end
end
desc 'Pushes the Docker image to the configured registry.'
command :push do |c|
c.action do |global_options, options, args|
tasks.push
end
end
desc 'Gets your Kubernetes cluster ready to run your Rails app.'
command :setup do |c|
c.action do |global_options, options, args|
tasks.setup
end
end
desc 'Prints the effective Dockerfile used to build the Docker image.'
command :dockerfile do |c|
c.action do |global_options, options, args|
tasks.print_dockerfile
end
end
desc 'Deploys the application.'
command :deploy do |c|
c.desc 'The Docker tag to deploy. Defaults to the most recent tag.'
c.flag [:t, :tag], required: false
c.action do |global_options, options, args|
tasks.deploy(options[:tag])
end
end
desc 'Rolls back to the previous Docker tag.'
command :rollback do |c|
c.action do |global_options, options, args|
tasks.rollback
end
end
desc 'Prints the effective Kubernetes resources that will be applied on deploy.'
command :resources do |c|
c.action do |global_options, options, args|
tasks.print_resources
end
end
desc 'Prints out the contents of the kubeconfig Kuby is using to communicate with your cluster.'
command :kubeconfig do |c|
c.action do |global_options, options, args|
tasks.print_kubeconfig
end
end
desc 'Runs an arbitrary kubectl command.'
command :kubectl do |c|
c.desc 'Prefixes the kubectl command with the namespace associated with '\
'the current environment. For example, if the Kuby env is "production", '\
'this option will prefix the kubectl command with "-n myapp-production".'
c.switch [:N, :namespaced], default: false
c.action do |global_options, options, args|
if options[:namespaced]
# sorry Demeter
namespace = Kuby.definition.environment.kubernetes.namespace.metadata.name
args = ['-n', namespace, *args]
end
tasks.kubectl(*args)
end
end
desc 'Runs commands, etc against the Kubernetes cluster.'
command :remote do |rc|
rc.desc 'Tails (i.e. continuously streams) the Rails log from your running application.'
rc.command :logs do |c|
c.action do |global_options, options, args|
exit 1 unless tasks.dev_deployment_ok
tasks.remote_logs
end
end
rc.desc 'Lists running Kubernetes pods.'
rc.command :status do |c|
c.action do |global_options, options, args|
exit 1 unless tasks.dev_deployment_ok
tasks.remote_status
end
end
rc.desc 'Runs an arbitrary command inside a running Rails pod.'
rc.command :exec do |c|
c.action do |global_options, options, args|
exit 1 unless tasks.dev_deployment_ok
tasks.remote_exec(args)
end
end
rc.desc 'Establishes a shell inside a running Rails pod.'
rc.command :shell do |c|
c.action do |global_options, options, args|
exit 1 unless tasks.dev_deployment_ok
tasks.remote_shell
end
end
rc.desc 'Establishes a Rails console inside a running Rails pod.'
rc.command :console do |c|
c.action do |global_options, options, args|
exit 1 unless tasks.dev_deployment_ok
tasks.remote_console
end
end
rc.desc 'Establishes a database console inside a running Rails pod.'
rc.command :dbconsole do |c|
c.action do |global_options, options, args|
exit 1 unless tasks.dev_deployment_ok
tasks.remote_dbconsole
end
end
rc.desc "Restarts the Rails app's web pods."
rc.command :restart do |c|
c.action do |global_options, options, args|
exit 1 unless tasks.dev_deployment_ok
tasks.remote_restart
end
end
end
end
end