Class: Capistrano::DataPlaneApi::Deploy::Args
- Inherits:
-
Object
- Object
- Capistrano::DataPlaneApi::Deploy::Args
- 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].freeze
Instance Attribute Summary collapse
-
#branch ⇒ String?
Git branch that the code will be deployed to.
- #force_haproxy ⇒ Boolean
-
#group ⇒ String?
Name of the HAProxy server group/backend.
- #no_haproxy ⇒ Boolean
- #no_migrations ⇒ Boolean
-
#only ⇒ Array<String>?
Ordered list of servers to which the app will be deployed.
-
#rake ⇒ String?
Rake command that will be called remotely (‘deploy` by default).
-
#stage ⇒ String?
Name of the deployment stage/server.
-
#test ⇒ Boolean
(also: #test?)
Runs in test mode if true, only prints commands without executing them.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #deploy_command(stage = nil) ⇒ String
- #humanized_deploy_command(stage = nil) ⇒ String
-
#initialize ⇒ Args
constructor
A new instance of Args.
- #only? ⇒ Boolean
- #prepare_if_one_server ⇒ void
Constructor Details
#initialize ⇒ Args
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
#branch ⇒ String?
Returns 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_haproxy ⇒ Boolean
167 168 169 |
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 167 def force_haproxy @force_haproxy end |
#group ⇒ String?
Returns 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_haproxy ⇒ Boolean
163 164 165 |
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 163 def no_haproxy @no_haproxy end |
#no_migrations ⇒ Boolean
165 166 167 |
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 165 def no_migrations @no_migrations end |
#only ⇒ Array<String>?
Returns 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 |
#rake ⇒ String?
Returns 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 |
#stage ⇒ String?
Returns Name of the deployment stage/server.
173 174 175 |
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 173 def stage @stage end |
#test ⇒ Boolean Also known as: test?
Returns 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
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( = nil) # rubocop:disable Metrics/MethodLength, Style/ClassMethodsDefinitions args = new opt_parser = ::OptionParser.new do |parser| # rubocop:disable Metrics/BlockLength parser. = <<~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!( || ::ARGV) args.stage = ::ARGV.first&.start_with?('-') ? nil : ::ARGV.first args.prepare_if_one_server args end |
Instance Method Details
#[](key) ⇒ Object
220 221 222 |
# File 'lib/capistrano/data_plane_api/deploy/args.rb', line 220 def [](key) public_send(key) end |
#[]=(key, val) ⇒ 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
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
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
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_server ⇒ void
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 |