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
# File 'lib/shards/workflow/base.rb', line 14

def initialize config
  @config=config
  config.repo.clean
  @pointer_history = []
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

#wfObject



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

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

Instance Method Details

#add_ask_blank_db_stepObject



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

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

#application_reload_shards_taskObject



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

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
  puts step['message']
end

#ask_blank_dbObject



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

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

#ask_for_exitObject



118
119
120
# File 'lib/shards/workflow/base.rb', line 118

def ask_for_exit
  exit
end

#ask_for_exit_and_restartObject



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

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

#ask_for_repeatObject



158
159
160
161
162
163
164
165
166
167
# File 'lib/shards/workflow/base.rb', line 158

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



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

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

#check_database_presence_in_serverObject



174
175
176
# File 'lib/shards/workflow/base.rb', line 174

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



178
179
180
# File 'lib/shards/workflow/base.rb', line 178

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

#check_domain_presence_in_sitesObject



182
183
184
185
186
# File 'lib/shards/workflow/base.rb', line 182

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



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

def check_fingerprints
  resp=stage.check_remote_process remote_check: :fingerprints
  errors=filter_errors resp
  raise_wrong_text errors if errors.count>0
  puts step['message']
end

#check_shard_presence_in_shardsObject



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

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



122
123
124
125
126
# File 'lib/shards/workflow/base.rb', line 122

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

#clean_repo_and_exitObject



211
212
213
# File 'lib/shards/workflow/base.rb', line 211

def clean_repo_and_exit
  puts config.repo.clean
end

#commit_changesObject



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

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

#create_databaseObject



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

def create_database
  stage.shard.db.blank_db = blank_db
  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?
  puts step['message'] % message_params
end

#decamelize(camelcase) ⇒ Object



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

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



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

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

#dns_upsertObject



215
216
217
218
219
# File 'lib/shards/workflow/base.rb', line 215

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

#fileObject



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

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

#format_options(title, array) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/shards/workflow/base.rb', line 110

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



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

def get_file fname

  f=File.join path, fname

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

  f

end

#hostObject



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

def host
  stage.shard.dns.host
end

#loop_stepObject



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

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
  puts step['message'] % [step['correct'], step['seconds_between_loop']]
  sleep step['seconds_between_loop']
end

#output_message(message) ⇒ Object



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

def output_message message
  puts message
end

#pathObject



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

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

#previous_pointerObject



154
155
156
# File 'lib/shards/workflow/base.rb', line 154

def previous_pointer
  @pointer_history.at(-2)
end

#puppet_agent_updateObject



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

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
  puts step['message']
end

#raise_wrong_text(wrong_text) ⇒ Object



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

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

#run_stepObject



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

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



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

def shared_file
  get_file 'base.yaml'
end

#shared_stepsObject



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

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

#startObject



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

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

#start_at(pointer_step) ⇒ Object



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

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

#validate_in_index(text, array) ⇒ Object



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

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

#validate_locationObject



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

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



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

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



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

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

#validate_stageObject



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

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



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

def validation condition, wrong_text
  raise_wrong_text wrong_text if condition
end

#write_yaml_filesObject



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

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

#yamlObject



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

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