Class: Shards::Workflow::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/shards/workflow/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Base



14
15
16
17
18
19
# File 'lib/shards/workflow/base.rb', line 14

def initialize config
  @config=config
  config.repo.clean
  @pointer_history = []
  @success=false
end

Instance Attribute Details

#answerObject

Returns the value of attribute answer.



9
10
11
# File 'lib/shards/workflow/base.rb', line 9

def answer
  @answer
end

#blank_dbObject

Returns the value of attribute blank_db.



9
10
11
# File 'lib/shards/workflow/base.rb', line 9

def blank_db
  @blank_db
end

#clientObject

Returns the value of attribute client.



9
10
11
# File 'lib/shards/workflow/base.rb', line 9

def client
  @client
end

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/shards/workflow/base.rb', line 9

def config
  @config
end

#current_loopObject (readonly)

Returns the value of attribute current_loop.



12
13
14
# File 'lib/shards/workflow/base.rb', line 12

def current_loop
  @current_loop
end

#domainObject

Returns the value of attribute domain.



9
10
11
# File 'lib/shards/workflow/base.rb', line 9

def domain
  @domain
end

#flowObject

Returns the value of attribute flow.



9
10
11
# File 'lib/shards/workflow/base.rb', line 9

def flow
  @flow
end

#locationObject

Returns the value of attribute location.



9
10
11
# File 'lib/shards/workflow/base.rb', line 9

def location
  @location
end

#paramsObject

Returns the value of attribute params.



9
10
11
# File 'lib/shards/workflow/base.rb', line 9

def params
  @params
end

#pointerObject

Returns the value of attribute pointer.



9
10
11
# File 'lib/shards/workflow/base.rb', line 9

def pointer
  @pointer
end

#pointer_historyObject

Returns the value of attribute pointer_history.



9
10
11
# File 'lib/shards/workflow/base.rb', line 9

def pointer_history
  @pointer_history
end

#siteObject

Returns the value of attribute site.



9
10
11
# File 'lib/shards/workflow/base.rb', line 9

def site
  @site
end

#stageObject

Returns the value of attribute stage.



9
10
11
# File 'lib/shards/workflow/base.rb', line 9

def stage
  @stage
end

#stepObject

Returns the value of attribute step.



9
10
11
# File 'lib/shards/workflow/base.rb', line 9

def step
  @step
end

#successObject (readonly)

Returns the value of attribute success.



12
13
14
# File 'lib/shards/workflow/base.rb', line 12

def success
  @success
end

#wfObject



55
56
57
# File 'lib/shards/workflow/base.rb', line 55

def wf
  @wf || decamelize( self.class.name.split('::').last )
end

Instance Method Details

#add_ask_blank_db_stepObject



354
355
356
# File 'lib/shards/workflow/base.rb', line 354

def add_ask_blank_db_step
  @flow['write_yaml_files']['correct']='ask_blank_db'
end

#application_reload_shards_taskObject



382
383
384
385
386
387
# File 'lib/shards/workflow/base.rb', line 382

def application_reload_shards_task
  resp=stage.check_remote_process remote_check: :reload_shards!
  errors=filter_status_errors resp
  raise_wrong_text errors if errors.count>0
  output_message step['message']
end

#ask_blank_dbObject



347
348
349
350
351
352
# File 'lib/shards/workflow/base.rb', line 347

def ask_blank_db
  shards=stage.shard.list.keys
  choose_one "Databases:", shards
  @blank_db=shards[answer.to_i]
  stage.chosen_blank_db=blank_db
end

#ask_for_exitObject



219
220
221
# File 'lib/shards/workflow/base.rb', line 219

def ask_for_exit
  exit
end

#ask_for_exit_and_restartObject



325
326
327
328
# File 'lib/shards/workflow/base.rb', line 325

def ask_for_exit_and_restart
  ask_question
  raise step['wrong_message'] if answer.strip.downcase!='r'
end

#ask_for_repeatObject



259
260
261
262
263
264
265
266
267
268
# File 'lib/shards/workflow/base.rb', line 259

def ask_for_repeat
  ask_question
  if answer.strip.downcase!='r'
    config.repo.clean
    raise step['wrong_message']
  end
  @pointer=previous_pointer
  run_step

end

#ask_questionObject



206
207
208
209
# File 'lib/shards/workflow/base.rb', line 206

def ask_question
  puts step['question']
  @answer=$stdin.gets.chomp
end

#check_database_presence_in_serverObject



275
276
277
# File 'lib/shards/workflow/base.rb', line 275

def check_database_presence_in_server
  raise_wrong_text [stage.shard.db.name,stage.shard.db.server] if stage.shard.db.exist?
end

#check_domain_dns_presenceObject



279
280
281
# File 'lib/shards/workflow/base.rb', line 279

def check_domain_dns_presence
  raise_wrong_text stage.shard.dns.host if stage.shard.dns.exist?
end

#check_domain_presence_in_sitesObject



283
284
285
286
287
# File 'lib/shards/workflow/base.rb', line 283

def check_domain_presence_in_sites
  @site = Shards::Site.new stage
  site.client = client
  raise_wrong_text site.domain if site.exist?
end

#check_fingerprintsObject



358
359
360
361
362
363
364
365
# File 'lib/shards/workflow/base.rb', line 358

def check_fingerprints
  resp=stage.check_remote_process remote_check: :fingerprints

  errors=filter_errors multitenant(resp)
  stage.host_list = multitenant(resp).keys
  raise_wrong_text errors if errors.count>0
  output_message step['message']
end

#check_fingerprints2Object



367
368
369
370
# File 'lib/shards/workflow/base.rb', line 367

def check_fingerprints2
  # this method if for the second time after puppet
  check_fingerprints
end

#check_shard_presence_in_shardsObject



270
271
272
273
# File 'lib/shards/workflow/base.rb', line 270

def check_shard_presence_in_shards
  stage.add_domain domain, dryrun: true
  raise_wrong_text stage.shard.name if stage.shard.exist?
end

#choose_one(title, array) ⇒ Object



223
224
225
226
227
# File 'lib/shards/workflow/base.rb', line 223

def choose_one title, array
  format_options(title, array)
  ask_question
  validate_in_index answer, array
end

#clean_repo_and_exitObject



311
312
313
# File 'lib/shards/workflow/base.rb', line 311

def clean_repo_and_exit
  output_message config.repo.clean
end

#commit_changesObject



297
298
299
300
301
302
# File 'lib/shards/workflow/base.rb', line 297

def commit_changes
  config.repo.message = "Added shard in #{location.name} for #{client}"
  config.repo.autocommit
  output_message step['message']
  output_message config.repo.message
end

#correct_stepsObject



105
106
107
# File 'lib/shards/workflow/base.rb', line 105

def correct_steps
  @correct_steps||={}
end

#create_databaseObject



304
305
306
307
308
309
# File 'lib/shards/workflow/base.rb', line 304

def create_database
  stage.shard.db.create dryrun: false
  message_params= [stage.shard.db.name, stage.shard.db.server]
  raise_wrong_text message_params unless stage.shard.db.exist?
  output_message step['message'] % message_params
end

#decamelize(camelcase) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/shards/workflow/base.rb', line 59

def decamelize camelcase
  camelcase.
    gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2').
    gsub(/([a-z]+)([A-Z\d])/, '\1_\2').
    gsub(/([A-Z]{2,})(\d+)/i, '\1_\2').
    gsub(/(\d+)([a-z])/i, '\1_\2').
    gsub(/(.+?)\&(.+?)/, '\1_&_\2').
    gsub(/\s/, '_').downcase
end

#default_pathObject



51
52
53
# File 'lib/shards/workflow/base.rb', line 51

def default_path
  File.join Shards.root_path, %w(lib settings workflows)
end

#dns_upsertObject



315
316
317
318
319
# File 'lib/shards/workflow/base.rb', line 315

def dns_upsert
  stage.shard.dns.set dryrun: false
  raise_wrong_text host unless stage.shard.dns.exist?
  output_message step['message'] % host
end

#fileObject



25
26
27
# File 'lib/shards/workflow/base.rb', line 25

def file
  get_file "#{wf}.yaml"
end

#finish_successfullyObject



396
397
398
399
# File 'lib/shards/workflow/base.rb', line 396

def finish_successfully
  output_message step['message']
  @success=true
end

#flow_initObject



69
70
71
# File 'lib/shards/workflow/base.rb', line 69

def flow_init
  @flow=shared_steps.merge yaml[wf]
end

#format_options(title, array) ⇒ Object



211
212
213
214
215
216
217
# File 'lib/shards/workflow/base.rb', line 211

def format_options title, array
  puts title
  puts
  array.each_index.map do |k|
    puts "\t#{k}) #{array[k]}"
  end
end

#get_file(fname) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/shards/workflow/base.rb', line 37

def get_file fname

  f=File.join path, fname

  f=File.join default_path, fname unless File.exist? f

  f

end

#hostObject



321
322
323
# File 'lib/shards/workflow/base.rb', line 321

def host
  stage.shard.dns.host
end

#loop_stepObject



372
373
374
375
376
377
378
379
# File 'lib/shards/workflow/base.rb', line 372

def loop_step
  @current_loop||=0
  @total_loops||=step['total_loops']
  @current_loop+=1
  raise_wrong_text [current_loop,@total_loops] if current_loop>@total_loops
  output_message step['message'] % [step['correct'], step['seconds_between_loop']]
  sleep step['seconds_between_loop'] if @current_loop>1
end

#output_message(message) ⇒ Object



180
181
182
# File 'lib/shards/workflow/base.rb', line 180

def output_message message
  puts message
end

#pathObject



47
48
49
# File 'lib/shards/workflow/base.rb', line 47

def path
  ENV['WORKFLOW_YAML_PATH'] || default_path
end

#previous_pointerObject



255
256
257
# File 'lib/shards/workflow/base.rb', line 255

def previous_pointer
  @pointer_history.at(-2)
end


137
138
139
140
# File 'lib/shards/workflow/base.rb', line 137

def print_description
  puts "h1. #{wf_title}"
  puts yaml['description'] if yaml.has_key?('description')
end


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/shards/workflow/base.rb', line 152

def print_extra_steps
  (wrong_steps.keys & correct_steps.keys).each { |k| wrong_steps.delete k }

  loop do
    initial_keys=wrong_steps.keys + correct_steps.keys
    new_keys=[]

    wrong_steps.each_pair do |k,v|
      new_keys << v['correct'] if v.has_key?('correct') and !initial_keys.include?v['correct']
      new_keys << v['wrong'] if v.has_key?('wrong') and !initial_keys.include?v['wrong']
    end
    if new_keys.empty?
      break
    else
      new_keys.each do |k|
        wrong_steps[k]=flow[k]
      end
    end
  end

  puts "h2. Additional Steps:" unless wrong_steps.empty?

  wrong_steps.each_pair do |k,v|
    puts_step k, v
  end

end


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/shards/workflow/base.rb', line 113

def print_step

  if pointer.nil?
    flow_init
    @pointer=yaml['start']
    puts "h2. Step List"
  end

  @step=flow[pointer]
  @pointer_history << pointer

  puts_step pointer, step

  correct_steps[step['correct']]=flow[step['correct']]
  wrong_steps[step['wrong']]=flow[step['wrong']] unless step['wrong'].nil?
  @pointer=step['correct']
  print_step if @step['correct']!='final_step'

end

#puppet_agent_updateObject



389
390
391
392
393
394
# File 'lib/shards/workflow/base.rb', line 389

def puppet_agent_update
  resp=stage.check_remote_process remote_check: :puppet_agent!
  errors=filter_status_errors resp
  raise_wrong_text errors if errors.count>0
  output_message step['message']
end

#puts_step(pointer, step) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/shards/workflow/base.rb', line 142

def puts_step pointer, step

  puts "|| #{pointer} "
  puts " - Correct => #{step['correct']}" if step.has_key?('correct')
  puts " - Wrong => #{step['wrong']}" if step.has_key?('wrong')
  puts "|"
  puts

end

#raise_wrong_text(wrong_text) ⇒ Object



251
252
253
# File 'lib/shards/workflow/base.rb', line 251

def raise_wrong_text wrong_text
  raise @step['wrong_message'] % wrong_text
end

#run_stepObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/shards/workflow/base.rb', line 85

def run_step

  @step=flow[pointer]
  @pointer_history << pointer
  @answer=nil

  begin
    send pointer
    @pointer=step['correct']
  rescue => e
    output_message e.message
    @pointer=step['wrong']
  end

  @step=flow[pointer]

  run_step if @step['correct']!='final_step'

end

#shared_fileObject



29
30
31
# File 'lib/shards/workflow/base.rb', line 29

def shared_file
  get_file 'base.yaml'
end

#shared_stepsObject



33
34
35
# File 'lib/shards/workflow/base.rb', line 33

def shared_steps
  YAML.load_file(shared_file)['shared_steps']
end

#startObject



73
74
75
76
77
# File 'lib/shards/workflow/base.rb', line 73

def start
  flow_init
  @pointer=yaml['start']
  run_step
end

#start_at(pointer_step) ⇒ Object



79
80
81
82
83
# File 'lib/shards/workflow/base.rb', line 79

def start_at pointer_step
  flow_init
  @pointer=pointer_step
  run_step
end

#validate_blank_db_shardObject



342
343
344
345
# File 'lib/shards/workflow/base.rb', line 342

def validate_blank_db_shard
  raise_wrong_text stage.blank_db_shard_key unless stage.blank_db_shard_exist?
  output_message step['message'] % stage.blank_db_shard_key
end

#validate_clientObject



184
185
186
187
188
189
190
191
192
# File 'lib/shards/workflow/base.rb', line 184

def validate_client

  cli=params[:client]

  validate_min_size cli, step['min_size']

  @client=cli

end

#validate_domainObject



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/shards/workflow/base.rb', line 194

def validate_domain

  domain_name=params[:domain]

  validate_min_size domain_name, step['min_size']

  validate_regex domain_name, step['regex']

  @domain=domain_name

end

#validate_in_index(text, array) ⇒ Object



241
242
243
244
245
# File 'lib/shards/workflow/base.rb', line 241

def validate_in_index text, array
  condition= array.at(text.to_i).nil?
  wrong_text = text
  validation condition, wrong_text
end

#validate_locationObject



330
331
332
333
334
# File 'lib/shards/workflow/base.rb', line 330

def validate_location
  loc=params[:location]
  raise_wrong_text loc unless config.locations.keys.include? loc
  @location=config.locations[loc]
end

#validate_min_size(text, min) ⇒ Object



229
230
231
232
233
# File 'lib/shards/workflow/base.rb', line 229

def validate_min_size text, min
  condition= text.size < min
  wrong_text= "'%s' is shorter than '%d' characters" % [text, min]
  validation condition, wrong_text
end

#validate_regex(text, regex) ⇒ Object



235
236
237
238
239
# File 'lib/shards/workflow/base.rb', line 235

def validate_regex text, regex
  condition= text !~ /#{regex}/
  wrong_text = "'%s' has a wrong format" % text
  validation condition, wrong_text
end

#validate_stageObject



336
337
338
339
340
# File 'lib/shards/workflow/base.rb', line 336

def validate_stage
  st=params[:stage]
  raise_wrong_text st unless location.stages.keys.include? st
  @stage=location.stages[st]
end

#validation(condition, wrong_text) ⇒ Object



247
248
249
# File 'lib/shards/workflow/base.rb', line 247

def validation condition, wrong_text
  raise_wrong_text wrong_text if condition
end

#wf_titleObject



133
134
135
# File 'lib/shards/workflow/base.rb', line 133

def wf_title
  wf.split('_').map { |x| x.capitalize }.join(' ')
end

#write_yaml_filesObject



289
290
291
292
293
294
295
# File 'lib/shards/workflow/base.rb', line 289

def write_yaml_files
  stage.add_domain domain
  site.add_site
  output_message config.repo.diff
  ask_question
  raise step['wrong_message'] if answer.strip.downcase!='y'
end

#wrong_stepsObject



109
110
111
# File 'lib/shards/workflow/base.rb', line 109

def wrong_steps
  @wrong_steps||={}
end

#yamlObject



21
22
23
# File 'lib/shards/workflow/base.rb', line 21

def yaml
  @yaml||= YAML.load_file file
end