Class: Capistrano::DataPlaneApi::Deploy::Args

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/data_plane_api/deploy/args.rb

Overview

Class which parses all provided command-line arguments passed to the deployment script and saves them in an object.

Constant Summary collapse

PRINTABLE_ENV_VARS =
%w[BRANCH NO_MIGRATIONS NO_ASSET_PRECOMPILATION].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeArgs

Returns a new instance of Args.

Signature:

  • -> void



221
222
223
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 221

def initialize
  @rake = 'deploy'
end

Instance Attribute Details

#branchObject

Git branch that the code will be deployed to

Signature:

  • String?



178
179
180
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 178

def branch
  @branch
end

#checkObject

Checks deployment dependencies

Signature:

  • bool



216
217
218
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 216

def check
  @check
end

#force_haproxyObject

Signature:

  • bool



197
198
199
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 197

def force_haproxy
  @force_haproxy
end

#groupObject

Name of the HAProxy server group/backend

Signature:

  • String?



188
189
190
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 188

def group
  @group
end

#no_haproxyObject

Signature:

  • bool



191
192
193
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 191

def no_haproxy
  @no_haproxy
end

#no_migrationsObject

Signature:

  • bool



194
195
196
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 194

def no_migrations
  @no_migrations
end

#onlyObject

Ordered list of servers to which the app will be deployed

Signature:

  • Array[String]?



202
203
204
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 202

def only
  @only
end

#rakeObject

Rake command that will be called remotely (deploy by default)

Signature:

  • String?



207
208
209
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 207

def rake
  @rake
end

#stageObject

Name of the deployment stage/server

Signature:

  • String?



212
213
214
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 212

def stage
  @stage
end

#testObject Also known as: test?

Runs in test mode if true, only prints commands without executing them

Signature:

  • bool



183
184
185
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 183

def test
  @test
end

Class Method Details

.parse(options = nil) ⇒ Object

Signature:

  • (?Array[untyped]?) -> instance



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
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 17

def self.parse(options = nil) # rubocop:disable Metrics/MethodLength, Style/ClassMethodsDefinitions
  args = new

  opt_parser = ::OptionParser.new do |parser| # rubocop:disable Metrics/BlockLength
    parser.banner = <<~BANNER
      Usage: bin/deploy [options]

      This script can be used to deploy this app to remote servers.

    BANNER

    parser.on(
      '-c',
      '--current',
      'Deploy from the currently checked out branch',
    ) do |_val|
      args.branch = `git branch --show-current`.strip
      ::ENV['BRANCH'] = args.branch
    end

    parser.on(
      '-t',
      '--test',
      'Show the commands that would be executed but do not carry out the deployment',
    ) do |val|
      args.test = val
    end

    parser.on(
      '-C',
      '--check',
      'Test deployment dependencies. ' \
      'Checks things like directory permissions, necessary utilities, ' \
      'HaProxy backends and servers',
    ) do |val|
      args.check = val
      args.rake = 'deploy:check' if val
    end

    parser.on(
      '-g GROUP',
      '--group=GROUP',
      'Deploy the code to every server in the passed HAProxy backend/group',
    ) do |val|
      args.group = val
    end

    parser.on(
      '--no-haproxy',
      'Do not modify the state of any server in HAProxy',
    ) do |val|
      args.no_haproxy = val
      ::ENV['NO_HAPROXY'] = 'true'
    end

    parser.on(
      '--force-haproxy',
      'Ignore the current state of servers in HAProxy',
    ) do |val|
      args.force_haproxy = val
      ::ENV['FORCE_HAPROXY'] = 'true'
    end

    parser.on(
      '-o ONLY',
      '--only=ONLY',
      'Deploy the code only to the passed servers in the same order',
    ) do |val|
      next unless val

      args.only = val.split(',').map(&:strip).uniq
    end

    parser.on(
      '-H',
      '--haproxy-config',
      'Show the current HAProxy configuration',
    ) do |val|
      next unless val

      ::Signal.trap('INT') { exit }
      ::Capistrano::DataPlaneApi.show_config
      exit
    end

    parser.on(
      '-S',
      '--haproxy-state',
      'Show the current HAProxy state',
    ) do |val|
      next unless val

      ::Signal.trap('INT') { exit }
      ::Capistrano::DataPlaneApi.show_state
      exit
    end

    parser.on(
      '-T',
      '--tasks',
      'Print a list of all available deployment Rake tasks',
    ) do |val|
      next unless val

      puts COLORS.bold.blue('Available Rake Tasks')
      `cap -T`.each_line do |line|
        puts line.delete_prefix('cap ')
      end
      exit
    end

    parser.on(
      '-r RAKE',
      '--rake=RAKE',
      'Carry out a particular Rake task on the server',
    ) do |val|
      next unless val

      args.rake = val
    end

    parser.on('-h', '--help', 'Prints this help') do
      puts parser
      exit
    end

    parser.on(
      '-b BRANCH',
      '--branch=BRANCH',
      'Deploy the code from the passed Git branch',
    ) do |val|
      args.branch = val
      ::ENV['BRANCH'] = val
    end

    parser.on(
      '--no-migrations',
      'Do not carry out migrations',
    ) do |val|
      args.no_migrations = val
      ::ENV['NO_MIGRATIONS'] = 'true'
    end

    parser.on(
      '-A',
      '--no-asset-precompilation',
      'Skip asset precompilation during deployment',
    ) do
      ::ENV['NO_ASSET_PRECOMPILATION'] = 'true'
    end
  end

  opt_parser.parse!(options || ::ARGV)
  args.stage = ::ARGV.first&.start_with?('-') ? nil : ::ARGV.first
  args.prepare_if_one_server
  args
end

Instance Method Details

#[](key) ⇒ Object

Signature:

  • (Symbol | String) -> Object



261
262
263
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 261

def [](key)
  public_send(key)
end

#[]=(key, val) ⇒ Object

Signature:

  • (Symbol | String, Object) -> Object



266
267
268
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 266

def []=(key, val)
  public_send("#{key}=", val)
end

#deploy_command(stage = nil) ⇒ Object

Signature:

  • (?String | Symbol | nil) -> String



242
243
244
245
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 242

def deploy_command(stage = nil)
  used_stage = stage || self.stage
  "cap #{used_stage} #{rake}"
end

#humanized_deploy_command(stage = nil) ⇒ Object

Signature:

  • (?String | Symbol | nil) -> String



248
249
250
251
252
253
254
255
256
257
258
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 248

def humanized_deploy_command(stage = nil)
  result = ::String.new
  PRINTABLE_ENV_VARS.each do |env_var_name|
    next unless (value = ::ENV[env_var_name])

    result << "#{env_var_name}=#{value} "
  end

  result << deploy_command(stage)
  result
end

#one_server?Boolean

Signature:

  • -> bool

Returns:

  • (Boolean)


271
272
273
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 271

def one_server?
  Boolean(@stage && @group.nil?)
end

#only?Boolean

Signature:

  • -> bool

Returns:

  • (Boolean)


226
227
228
229
230
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 226

def only?
  return false if @only.nil?

  @only.any?
end

#prepare_if_one_serverObject

Signature:

  • -> void



233
234
235
236
237
238
239
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 233

def prepare_if_one_server
  return unless one_server?

  server, backend = ::Capistrano::DataPlaneApi.find_server_and_backend(T.must(@stage))
  @only = [T.must(server.name)]
  @group = backend.name
end