Class: CapistranoMulticonfigParallel::BaseManager

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Celluloid::Logger
Defined in:
lib/capistrano_multiconfig_parallel/managers/base_manager.rb

Overview

finds app dependencies, shows menu and delegates jobs to celluloid manager rubocop:disable ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ BaseManager

Returns a new instance of BaseManager.



13
14
15
16
17
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 13

def initialize
  @stages = fetch_stages
  collect_command_line_tasks(CapistranoMulticonfigParallel.original_args)
  @jobs = []
end

Instance Attribute Details

#application ⇒ Object

Returns the value of attribute application.



11
12
13
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 11

def application
  @application
end

#args ⇒ Object

Returns the value of attribute args.



11
12
13
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 11

def args
  @args
end

#argv ⇒ Object

Returns the value of attribute argv.



11
12
13
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 11

def argv
  @argv
end

#branch_backup ⇒ Object

Returns the value of attribute branch_backup.



11
12
13
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 11

def branch_backup
  @branch_backup
end

#condition ⇒ Object

Returns the value of attribute condition.



11
12
13
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 11

def condition
  @condition
end

#default_stage ⇒ Object

Returns the value of attribute default_stage.



11
12
13
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 11

def default_stage
  @default_stage
end

#dependency_tracker ⇒ Object

Returns the value of attribute dependency_tracker.



11
12
13
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 11

def dependency_tracker
  @dependency_tracker
end

#jobs ⇒ Object

Returns the value of attribute jobs.



11
12
13
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 11

def jobs
  @jobs
end

#manager ⇒ Object

Returns the value of attribute manager.



11
12
13
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 11

def manager
  @manager
end

#name ⇒ Object

Returns the value of attribute name.



11
12
13
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 11

def name
  @name
end

#stage ⇒ Object

Returns the value of attribute stage.



11
12
13
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 11

def stage
  @stage
end

#stages ⇒ Object

Returns the value of attribute stages.



11
12
13
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 11

def stages
  @stages
end

#top_level_tasks ⇒ Object

Returns the value of attribute top_level_tasks.



11
12
13
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 11

def top_level_tasks
  @top_level_tasks
end

Instance Method Details

#backup_the_branch ⇒ Object



58
59
60
61
62
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 58

def backup_the_branch
  return if custom_command? || @argv['BRANCH'].blank?
  @branch_backup = @argv['BRANCH'].to_s
  @argv['BRANCH'] = nil
end

#can_start? ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 64

def can_start?
  @top_level_tasks.size >= 1 && (@stages.include?(@top_level_tasks.first) || custom_command?) && ENV[CapistranoMulticonfigParallel::ENV_KEY_JOB_ID].blank?
end

#can_tag_staging? ⇒ Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 197

def can_tag_staging?
  wants_deploy_production? && tag_staging_exists? && fetch_multi_stages.include?('staging')
end

#check_before_starting ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 143

def check_before_starting
  CapistranoMulticonfigParallel.configuration_valid?(@stages)
  CapistranoMulticonfigParallel.enable_logging
  @dependency_tracker = CapistranoMulticonfigParallel::DependencyTracker.new(Actor.current)
  @default_stage = CapistranoMulticonfigParallel.configuration.development_stages.present? ? CapistranoMulticonfigParallel.configuration.development_stages.first : 'development'
  @condition = Celluloid::Condition.new
  @manager = CapistranoMulticonfigParallel::CelluloidManager.new(Actor.current)
end

#check_giflow_tasks(*tasks) ⇒ Object



182
183
184
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 182

def check_giflow_tasks(*tasks)
  tasks.all? { |t| Rake::Task[t].present? }
end

#check_multi_stages(custom_stages) ⇒ Object



201
202
203
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 201

def check_multi_stages(custom_stages)
  can_tag_staging? ? custom_stages.reject { |u| u == 'production' } : custom_stages
end

#collect_command_line_tasks(args) ⇒ Object

:nodoc:



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 103

def collect_command_line_tasks(args) # :nodoc:
  @argv = {}
  @top_level_tasks = []
  args.each do |arg|
    if arg =~ /^(\w+)=(.*)$/m
      @argv[Regexp.last_match(1)] = Regexp.last_match(2)
    else
      @top_level_tasks << arg unless arg =~ /^-/
    end
  end
  @top_level_tasks.push(Rake.application.default_task_name) if @top_level_tasks.blank?
end

#collect_jobs(options = {}, &_block) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 152

def collect_jobs(options = {}, &_block)
  options = prepare_options(options)
  options = options.stringify_keys
  apps = @dependency_tracker.fetch_apps_needed_for_deployment(options['app'], options['action'])
  backup_the_branch if multi_apps?
  deploy_multiple_apps(apps, options)
  deploy_app(options) if !custom_command? || !multi_apps?
rescue => e
  CapistranoMulticonfigParallel.log_message(e)
end

#configuration ⇒ Object



85
86
87
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 85

def configuration
  CapistranoMulticonfigParallel.configuration
end

#custom_command? ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 68

def custom_command?
  if multi_apps?
    !@stages.include?(@top_level_tasks.first) && custom_commands.values.include?(@top_level_tasks.first)
  else
    !@stages.include?(@top_level_tasks.second) && @stages.include?(@top_level_tasks.first) && custom_commands.values.include?(@top_level_tasks.second)
  end
end

#custom_commands ⇒ Object



76
77
78
79
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 76

def custom_commands
  key = multi_apps? ? CapistranoMulticonfigParallel::MULTI_KEY : CapistranoMulticonfigParallel::SINGLE_KEY
  CapistranoMulticonfigParallel::CUSTOM_COMMANDS[key]
end

#deploy_app(options = {}) ⇒ Object



205
206
207
208
209
210
211
212
213
214
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 205

def deploy_app(options = {})
  options = options.stringify_keys
  app = options['app'].is_a?(Hash) ? options['app'] : { 'app' => options['app'] }
  branch = @branch_backup.present? ? @branch_backup : @argv['BRANCH'].to_s
  call_task_deploy_app({
    branch: branch,
    app: app,
    action: options['action']
  }.reverse_merge(options))
end

#deploy_multiple_apps(applications, options) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 50

def deploy_multiple_apps(applications, options)
  options = options.stringify_keys
  return unless applications.present?
  applications.each do |app|
    deploy_app(options.merge('app' => app))
  end
end

#fetch_multi_stages ⇒ Object



186
187
188
189
190
191
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 186

def fetch_multi_stages
  custom_stages = @argv['STAGES'].blank? ? '' : @argv['STAGES']
  custom_stages = parse_inputted_value('value' => custom_stages).split(',').compact if custom_stages.present?
  custom_stages = custom_stages.present? ? custom_stages : [@default_stage]
  custom_stages
end

#fetch_stages ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 19

def fetch_stages
  stages_root = 'config/deploy'
  Dir["#{stages_root}/**/*.rb"].map do |file|
    file.slice(stages_root.size + 1..-4).tr('/', ':')
  end.tap do |paths|
    paths.reject! do |path|
      paths.any? { |another| another != path && another.start_with?(path + ':') }
    end
  end.sort
end

#get_app_additional_env_options(app, app_message) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 216

def get_app_additional_env_options(app, app_message)
  app_name = (app.is_a?(Hash) && app[:app].present?) ? app[:app].camelcase : app
  app_name = app_name.present? ? app_name : 'current application'
  message = "Please write additional ENV options for #{app_name} for #{app_message}"
  set :app_additional_env_options, CapistranoMulticonfigParallel.ask_confirm(message, nil)
  fetch_app_additional_env_options
end

#initialize_data ⇒ Object



96
97
98
99
100
101
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 96

def initialize_data
  @application = custom_command? ? nil : @top_level_tasks.first.split(':').reverse[1]
  @stage = custom_command? ? nil : @top_level_tasks.first.split(':').reverse[0]
  @stage = @stage.present? ? @stage : @default_stage
  @name, @args = parse_task_string(@top_level_tasks.second)
end

#multi_apps? ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 81

def multi_apps?
  @stages.find { |stage| stage.include?(':') }.present?
end

#parse_task_string(string) ⇒ Object

:nodoc:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 116

def parse_task_string(string) # :nodoc:
  /^([^\[]+)(?:\[(.*)\])$/ =~ string.to_s

  name           = Regexp.last_match(1)
  remaining_args = Regexp.last_match(2)

  return string, [] unless name
  return name,   [] if     remaining_args.empty?

  args = []

  loop do
    /((?:[^\\,]|\\.)*?)\s*(?:,\s*(.*))?$/ =~ remaining_args

    remaining_args = Regexp.last_match(2)
    args << Regexp.last_match(1).gsub(/\\(.)/, '\1')
    break if   remaining_args.blank?
  end

  [name, args]
end

#process_jobs ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 163

def process_jobs
  return unless @jobs.present?
  if configuration.multi_secvential.to_s.downcase == 'true'
    @jobs.each { |job| CapistranoMulticonfigParallel::StandardDeploy.new(job) }
  else
    run_async_jobs
  end
end

#run ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 30

def run
  options = {}
  if custom_command?
    options = verify_options_custom_command(options)
    run_custom_command(options)
  else
    collect_jobs(options)
  end
  process_jobs
end

#run_custom_command(options) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 41

def run_custom_command(options)
  custom_stages = fetch_multi_stages
  return if custom_stages.blank?
  custom_stages = check_multi_stages(custom_stages)
  custom_stages.each do |stage|
    collect_jobs(options.merge('stage' => stage))
  end
end

#start(&block) ⇒ Object



89
90
91
92
93
94
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 89

def start(&block)
  check_before_starting
  initialize_data
  block.call if block_given?
  run
end

#tag_staging_exists? ⇒ Boolean

check exists task from capistrano-gitflow

Returns:

  • (Boolean)


172
173
174
175
176
177
178
179
180
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 172

def tag_staging_exists? # check exists task from capistrano-gitflow
  check_giflow_tasks(
    CapistranoMulticonfigParallel::GITFLOW_TAG_STAGING_TASK,
    CapistranoMulticonfigParallel::GITFLOW_CALCULATE_TAG_TASK,
    CapistranoMulticonfigParallel::GITFLOW_VERIFY_UPTODATE_TASK
  )
rescue
  return false
end

#verify_options_custom_command(options) ⇒ Object



138
139
140
141
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 138

def verify_options_custom_command(options)
  options[:action] = @argv['ACTION'].present? ? @argv['ACTION'] : 'deploy'
  options
end

#wants_deploy_production? ⇒ Boolean

Returns:

  • (Boolean)


193
194
195
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 193

def wants_deploy_production?
  (!custom_command? && @stage == 'production') || (custom_command? && fetch_multi_stages.include?('production'))
end

#worker_environments ⇒ Object



224
225
226
# File 'lib/capistrano_multiconfig_parallel/managers/base_manager.rb', line 224

def worker_environments
  @jobs.map { |job| job['env'] }
end