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 =

Returns:

  • (Array<String>)
%w[BRANCH NO_MIGRATIONS].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeArgs

Returns a new instance of Args.



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

def initialize
  @rake = 'deploy'
end

Instance Attribute Details

#branchString?

Returns Git branch that the code will be deployed to.

Returns:

  • (String, nil)

    Git branch that the code will be deployed to



157
158
159
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 157

def branch
  @branch
end

#force_haproxyBoolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 167

def force_haproxy
  @force_haproxy
end

#groupString?

Returns Name of the HAProxy server group/backend.

Returns:

  • (String, nil)

    Name of the HAProxy server group/backend



161
162
163
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 161

def group
  @group
end

#no_haproxyBoolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 163

def no_haproxy
  @no_haproxy
end

#no_migrationsBoolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 165

def no_migrations
  @no_migrations
end

#onlyArray<String>?

Returns Ordered list of servers to which the app will be deployed.

Returns:

  • (Array<String>, nil)

    Ordered list of servers to which the app will be deployed



169
170
171
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 169

def only
  @only
end

#rakeString?

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

Returns:

  • (String, nil)

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



171
172
173
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 171

def rake
  @rake
end

#stageString?

Returns Name of the deployment stage/server.

Returns:

  • (String, nil)

    Name of the deployment stage/server



173
174
175
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 173

def stage
  @stage
end

#testBoolean Also known as: test?

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

Returns:

  • (Boolean)

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



159
160
161
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 159

def test
  @test
end

Class Method Details

.parse(options = nil) ⇒ self

Parameters:

  • options (Array, nil) (defaults to: nil)

Returns:

  • (self)


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
# 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(
      '-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
  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

Parameters:

  • key (Symbol, String)

Returns:

  • (Object)


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

def [](key)
  public_send(key)
end

#[]=(key, val) ⇒ Object

Parameters:

  • key (Symbol, String)
  • val (Object)

Returns:

  • (Object)


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

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

#deploy_command(stage = nil) ⇒ String

Parameters:

  • stage (String, Symbol, nil) (defaults to: nil)

Returns:

  • (String)


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

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

#humanized_deploy_command(stage = nil) ⇒ String

Parameters:

  • stage (String, Symbol, nil) (defaults to: nil)

Returns:

  • (String)


206
207
208
209
210
211
212
213
214
215
216
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 206

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

Returns:

  • (Boolean)


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

def only?
  return false if @only.nil?

  @only.any?
end

#prepare_if_one_servervoid

This method returns an undefined value.



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

def prepare_if_one_server
  return unless one_server?

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