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

Returns a new instance of 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



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

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

#application_reload_shards_taskObject



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

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



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

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



140
141
142
# File 'lib/shards/workflow/base.rb', line 140

def ask_for_exit
  exit
end

#ask_for_exit_and_restartObject



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

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

#ask_for_repeatObject



180
181
182
183
184
185
186
187
188
189
# File 'lib/shards/workflow/base.rb', line 180

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



127
128
129
130
# File 'lib/shards/workflow/base.rb', line 127

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

#check_database_presence_in_serverObject



196
197
198
# File 'lib/shards/workflow/base.rb', line 196

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



200
201
202
# File 'lib/shards/workflow/base.rb', line 200

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

#check_domain_presence_in_sitesObject



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

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



279
280
281
282
283
284
285
286
# File 'lib/shards/workflow/base.rb', line 279

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



288
289
290
291
# File 'lib/shards/workflow/base.rb', line 288

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

#check_shard_presence_in_shardsObject



191
192
193
194
# File 'lib/shards/workflow/base.rb', line 191

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



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

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

#clean_repo_and_exitObject



232
233
234
# File 'lib/shards/workflow/base.rb', line 232

def clean_repo_and_exit
  output_message config.repo.clean
end

#commit_changesObject



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

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

#create_databaseObject



225
226
227
228
229
230
# File 'lib/shards/workflow/base.rb', line 225

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



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

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



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

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

#format_options(title, array) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/shards/workflow/base.rb', line 132

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



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

def host
  stage.shard.dns.host
end

#loop_stepObject



293
294
295
296
297
298
299
300
# File 'lib/shards/workflow/base.rb', line 293

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



101
102
103
# File 'lib/shards/workflow/base.rb', line 101

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



176
177
178
# File 'lib/shards/workflow/base.rb', line 176

def previous_pointer
  @pointer_history.at(-2)
end

#puppet_agent_updateObject



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

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

#raise_wrong_text(wrong_text) ⇒ Object



172
173
174
# File 'lib/shards/workflow/base.rb', line 172

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

#run_stepObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/shards/workflow/base.rb', line 81

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



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

def start
  @flow=shared_steps.merge yaml[wf]
  @pointer=yaml['start']
  run_step
end

#start_at(pointer_step) ⇒ Object



75
76
77
78
79
# File 'lib/shards/workflow/base.rb', line 75

def start_at pointer_step
  @flow=shared_steps.merge yaml[wf]
  @pointer=pointer_step
  run_step
end

#validate_blank_db_shardObject



263
264
265
266
# File 'lib/shards/workflow/base.rb', line 263

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



105
106
107
108
109
110
111
112
113
# File 'lib/shards/workflow/base.rb', line 105

def validate_client

  cli=params[:client]

  validate_min_size cli, step['min_size']

  @client=cli

end

#validate_domainObject



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/shards/workflow/base.rb', line 115

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



162
163
164
165
166
# File 'lib/shards/workflow/base.rb', line 162

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

#validate_locationObject



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

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



150
151
152
153
154
# File 'lib/shards/workflow/base.rb', line 150

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



156
157
158
159
160
# File 'lib/shards/workflow/base.rb', line 156

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

#validate_stageObject



257
258
259
260
261
# File 'lib/shards/workflow/base.rb', line 257

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



168
169
170
# File 'lib/shards/workflow/base.rb', line 168

def validation condition, wrong_text
  raise_wrong_text wrong_text if condition
end

#write_yaml_filesObject



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

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

#yamlObject



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

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