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

: -> void



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

def initialize
  @rake = 'deploy'
end

Instance Attribute Details

#branchObject

Git branch that the code will be deployed to

: String?



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

def branch
  @branch
end

#checkObject

Checks deployment dependencies : bool



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

def check
  @check
end

#force_haproxyObject

: bool



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

def force_haproxy
  @force_haproxy
end

#groupObject

Name of the HAProxy server group/backend

: String?



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

def group
  @group
end

#no_haproxyObject

: bool



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

def no_haproxy
  @no_haproxy
end

#no_migrationsObject

: bool



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

def no_migrations
  @no_migrations
end

#onlyObject

Ordered list of servers to which the app will be deployed

: Array?



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

def only
  @only
end

#rakeObject

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

: String?



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

def rake
  @rake
end

#stageObject

Name of the deployment stage/server

: String?



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

def stage
  @stage
end

#testObject Also known as: test?

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

: bool



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

def test
  @test
end

Class Method Details

.parse(options = nil) ⇒ Object

: (Array?) -> 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
# 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(
      '--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

: (Symbol | String) -> Object



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

def [](key)
  public_send(key)
end

#[]=(key, val) ⇒ Object

: (Symbol | String, Object) -> Object



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

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

#deploy_command(stage = nil) ⇒ Object

: (String | Symbol | nil) -> String



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

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

#humanized_deploy_command(stage = nil) ⇒ Object

: (String | Symbol | nil) -> String



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

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

#only?Boolean

: -> bool

Returns:

  • (Boolean)


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

def only?
  return false if @only.nil?

  @only.any?
end

#prepare_if_one_serverObject

: -> void



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

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