Class: VMC::KNIFE::RecipesConfigurationApplier

Inherits:
Object
  • Object
show all
Defined in:
lib/vmc_knife/vmc_knife.rb,
lib/vmc_knife/data_services.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manifest, client, recipe_sel = nil, application_sel = nil, service_sel = nil, opts = nil) ⇒ RecipesConfigurationApplier

Select the applications and data-services to configure according to the values in the SaaS manifest. When the selector is nil all of them are selected.



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/vmc_knife/vmc_knife.rb', line 276

def initialize(manifest, client, recipe_sel=nil, application_sel=nil, service_sel=nil, opts=nil)
  @root = Root.new manifest
  @client = client
  @opts=opts
  @recipes = @root.recipes(recipe_sel)
  @applications = Array.new
  @data_services = Array.new
  @recipes.each do |recipe|
    @applications = @applications + recipe.applications(application_sel)
    @data_services = @data_services + recipe.data_services(service_sel)
  end
  app_names = @applications.collect {|app| app.name } unless @opts && @opts[:data_only]
  service_names = @data_services.collect {|service| service.name } unless @opts && @opts[:apps_only]
  app_names ||= Array.new
  service_names ||= Array.new
  if app_names.empty? && service_names.empty?
    puts "No applications and data-services were selected."
  else
    puts "Applications selected #{app_names.join(', ')}" unless app_names.empty?
    puts "Data-services selected #{service_names.join(', ')}" unless service_names.empty?
  end
end

Instance Attribute Details

#applicationsObject

Returns the value of attribute applications.



273
274
275
# File 'lib/vmc_knife/vmc_knife.rb', line 273

def applications
  @applications
end

#clientObject

Returns the value of attribute client.



273
274
275
# File 'lib/vmc_knife/vmc_knife.rb', line 273

def client
  @client
end

#data_servicesObject

Returns the value of attribute data_services.



273
274
275
# File 'lib/vmc_knife/vmc_knife.rb', line 273

def data_services
  @data_services
end

#optsObject

Returns the value of attribute opts.



273
274
275
# File 'lib/vmc_knife/vmc_knife.rb', line 273

def opts
  @opts
end

#recipesObject

Returns the value of attribute recipes.



273
274
275
# File 'lib/vmc_knife/vmc_knife.rb', line 273

def recipes
  @recipes
end

#rootObject

Returns the value of attribute root.



273
274
275
# File 'lib/vmc_knife/vmc_knife.rb', line 273

def root
  @root
end

Instance Method Details

#__set_current(current_services = nil, current_services_info = nil) ⇒ Object

Only for testing: inject json



299
300
301
302
# File 'lib/vmc_knife/vmc_knife.rb', line 299

def __set_current(current_services=nil,current_services_info=nil)
  @current_services = current_services
  @current_services_info = current_services_info
end

#apply_privilegesObject



200
201
202
203
204
# File 'lib/vmc_knife/data_services.rb', line 200

def apply_privileges()
  @data_services.each do |data_service|
    data_service.apply_privileges
  end
end

#credentialsObject



195
196
197
198
199
# File 'lib/vmc_knife/data_services.rb', line 195

def credentials()
  @data_services.each do |data_service|
    data_service.credentials
  end
end

#deleteObject



394
395
396
397
398
399
400
401
402
403
# File 'lib/vmc_knife/vmc_knife.rb', line 394

def delete()
  @applications.each do |application|
    application_updater = ApplicationManifestApplier.new application, @client
    application_updater.delete
  end
  @data_services.each do |data_service|
    data_service_updater = DataServiceManifestApplier.new data_service, @client, @current_services, @current_services_info
    data_service_updater.delete
  end
end

#dropObject



219
220
221
222
223
224
# File 'lib/vmc_knife/data_services.rb', line 219

def drop()
  collection_or_table_names = @opts[:collection_or_table_names] if @opts
  @data_services.each do |data_service|
    data_service.drop(collection_or_table_names)
  end
end

#executeObject



471
472
473
474
475
476
477
478
479
# File 'lib/vmc_knife/vmc_knife.rb', line 471

def execute()
  return if updates_pending.empty?
  @data_service_updaters.each do |name,data_service_updater|
    data_service_updater.execute
  end
  @application_updaters.each do |name,application_updater|
    application_updater.execute
  end
end

#exportObject



212
213
214
215
216
217
218
# File 'lib/vmc_knife/data_services.rb', line 212

def export()
  file_names = @opts[:file_names] if @opts
  app_name = @opts[:app_name] if @opts
  @data_services.each do |data_service|
    data_service.export(app_name,file_names)
  end
end

#extract_deployedObject



368
369
370
371
372
373
# File 'lib/vmc_knife/vmc_knife.rb', line 368

def extract_deployed()
  @applications.each do |application|
    application_updater = ApplicationManifestApplier.new application, @client
    application_updater.extract_deployed(@opts[:force])
  end
end

#importObject



205
206
207
208
209
210
211
# File 'lib/vmc_knife/data_services.rb', line 205

def import()
  file_names = @opts[:file_names] if @opts
  app_name = @opts[:app_name] if @opts
  @data_services.each do |data_service|
    data_service.import(app_name,file_names)
  end
end

#infoObject



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/vmc_knife/vmc_knife.rb', line 374

def info()
	configure_only=@opts[:configure_only] || false
  if configure_only
    if updates_pending().empty?
      puts "All configuration up to date: true"
      return true
    else
      puts "All configuration up to data: false"
      return false
    end
  end
  up_to_date=true
  @applications.each do |application|
    application_updater = ApplicationManifestApplier.new application, @client
    app_up_to_date = application_updater.info()
    up_to_date = false unless app_up_to_date
  end
  puts "All up to date: #{up_to_date}"
  return up_to_date
end

#logsObject



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/vmc_knife/vmc_knife.rb', line 490

def logs()
  output_file=@opts[:output_file] if @opts
  if @root.wrapped['logs']
    man_logs=@root.wrapped['logs']
    name_prefix=man_logs['prefix']
    parent_output_folder=man_logs['destination']
    if parent_output_folder
      FileUtils.mkdir_p parent_output_folder
      logs_base_url=man_logs['logs_base_url']
      # TODO: clean the old files and folders if there are too many of them.
    end
    other_logs = man_logs['other_logs']
  end
  name_prefix||="cflogs-"
  parent_output_folder||="/tmp"
  name="#{name_prefix}#{Time.now.strftime("%Y%m%d-%H%M%S")}"
  output_file||="#{name}.zip"
  output_folder="#{parent_output_folder}/#{name}"
  FileUtils.rm_rf(output_folder) if File.exist? output_folder
  FileUtils.mkdir(output_folder)
  log_apps=@opts[:log_apps] if @opts
  log_vcap=@opts[:log_vcap] if @opts
  log_apps||=false
  log_vcap||=false
  if log_apps
    FileUtils.mkdir(File.join(output_folder, 'apps'))
    @applications.each do |application|
      application.log(File.join(output_folder, 'apps'))
    end

#          svc_dir = File.join(output_folder, 'services')
#          FileUtils.mkdir(svc_dir)
#          @data_services.each do | ds |
#            ds.log(svc_dir)
#          end

  end
  if log_vcap
    if ENV['CLOUD_FOUNDRY_CONFIG_PATH'] && File.exist?(ENV['CLOUD_FOUNDRY_CONFIG_PATH'])
      vcap_log_folder = File.join(File.dirname(ENV['CLOUD_FOUNDRY_CONFIG_PATH']), "log")
    else
      puts "Can't find the log folder for vcap, the environment variable CLOUD_FOUNDRY_CONFIG_PATH is not defined."
    end
    if File.exist? vcap_log_folder
      FileUtils.mkdir(File.join(output_folder, 'vcap'))
      FileUtils.cp_r Dir.glob(File.join(vcap_log_folder,"*")), File.join(output_folder, 'vcap')
    end
  end
  if other_logs
    other_logs_dir=File.join(output_folder, 'other_logs')
    FileUtils.mkdir other_logs_dir
    other_logs.each do |name,glob|
      other_dir=File.join(other_logs_dir, name)
      FileUtils.mkdir other_dir
      FileUtils.cp_r Dir.glob(glob), other_dir
    end
  end

  curr_dir=Dir.pwd
  destination=parent_output_folder||curr_dir
  copy_or_move=("/tmp" == parent_output_folder)?"mv":"cp"
  if "/tmp" == parent_output_folder
    puts "Generating a zip of the logs as #{curr_dir}/#{output_file}"
  else
    if logs_base_url
      hostname=`hostname`.strip
      complete_base_url= logs_base_url =~ /:\/\// ? logs_base_url : "http://#{hostname}.local#{logs_base_url}"
      puts "Logs available at #{complete_base_url}/#{name} and in #{parent_output_folder}/#{name}"
    end
  end
  `cd #{File.dirname(output_folder)}; zip -r #{output_file} #{File.basename(output_folder)}; #{copy_or_move} #{output_file} #{curr_dir}`
  if "/tmp" == parent_output_folder
    `rm -rf #{output_folder}`
  else
    `mv #{parent_output_folder}/#{output_file} #{parent_output_folder}/#{name}`
  end
  if logs_base_url
    # not the most elegant code to cp then delete:
    `rm #{output_file}` unless @opts[:output_file]
  end
end

#logs_shellObject



480
481
482
483
484
485
486
487
488
489
# File 'lib/vmc_knife/vmc_knife.rb', line 480

def logs_shell()
  log_apps=@opts[:log_apps] if @opts
  logs_shell=@opts[:logs_shell] if @opts
  log_files_glob=@opts[:log_files_glob] if @opts
  raise "The name of the application to read its log is required." unless log_apps
  @applications.each do |application|
     application.log_shell(logs_shell,log_files_glob)
  end
  raise "No application with this name was found."
end

#patchObject



362
363
364
365
366
367
# File 'lib/vmc_knife/vmc_knife.rb', line 362

def patch()
  @applications.each do |application|
    application_updater = ApplicationManifestApplier.new application, @client
    application_updater.patch(@opts[:force])
  end
end

#restartObject



404
405
406
407
# File 'lib/vmc_knife/vmc_knife.rb', line 404

def restart()
  stop()
  start()
end

#running_applications?Boolean

Returns:

  • (Boolean)


414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/vmc_knife/vmc_knife.rb', line 414

def running_applications?()
  if @opts && @opts[:wait]
    tries = @opts[:wait][:retries]
    tries = tries.to_i if tries
    interval = @opts[:wait][:interval]
    interval = interval.to_i if interval
  end
  tries ||= 1
  interval ||= 30
  curr_iteration = 0
  already_running = {}
  @applications.each do |application|
    already_running[application.name] = "false"
  end
  vmc_apps = Array.new
  while (curr_iteration <= tries)
    # brute approach: cmd-line.
    vmc_apps = ENV['TEST_VMC_APPS_STRING'] # this is for testing.
    unless vmc_apps
      vmc_apps = `vmc apps`.strip.split("\n")
      raise "Unable to run vmc" unless $?.exitstatus == 0
    end
    current_healths = Hash.new
    vmc_apps.each do |line|
      if line =~ /^\| (\S*)\s*\|\s*\d*\s*\|\s*(\S*)\s*\|/
         app = $1
         health = $2
         current_healths[app] = health
      end
    end
    not_running = Array.new
    @applications.each do |application|
      #health = `echo "#{vmc_apps}" | grep \|\ #{application.name}\ | cut -d '|' -f4`
      health = current_healths[application.name]
      already_running[application.name] = health
      not_running << application.name unless health == 'RUNNING'
    end
    all_running = not_running.empty?
    curr_iteration = curr_iteration + 1
    break if not_running.empty? || curr_iteration == tries
    p "Sleeping #{interval}s as #{not_running.join(' ')} #{not_running.size == 1 ? 'is' : 'are' } not running yet."
    sleep(interval)
  end
  p "Success: all apps are running" if all_running
  return true if all_running
  p "At least one app is not running"
  vmc_apps.each do |line|
    p line
  end
  return false
end

#shellObject



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/vmc_knife/data_services.rb', line 184

def shell()
  app_name = @opts[:app_name] if @opts
  file_name = @opts[:file_name] if @opts
  data_cmd = @opts[:data_cmd] if @opts
  if data_cmd
    file_name = data_cmd
  end
  @data_services.each do |data_service|
    data_service.shell(file_name)
  end
end

#shrinkObject



225
226
227
228
229
230
# File 'lib/vmc_knife/data_services.rb', line 225

def shrink()
  collection_or_table_names = @opts[:collection_or_table_names] if @opts
  @data_services.each do |data_service|
    data_service.shrink(collection_or_table_names)
  end
end

#startObject



465
466
467
468
469
470
# File 'lib/vmc_knife/vmc_knife.rb', line 465

def start()
  @applications.each do |application|
    application_updater = ApplicationManifestApplier.new application, @client
    application_updater.start
  end
end

#stopObject



408
409
410
411
412
413
# File 'lib/vmc_knife/vmc_knife.rb', line 408

def stop()
  @applications.each do |application|
    application_updater = ApplicationManifestApplier.new application, @client
    application_updater.stop
  end
end

#updateObject



356
357
358
359
360
361
# File 'lib/vmc_knife/vmc_knife.rb', line 356

def update()
  @applications.each do |application|
    application_updater = ApplicationManifestApplier.new application, @client
    application_updater.update(@opts[:force])
  end
end

#updates_pendingObject



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/vmc_knife/vmc_knife.rb', line 303

def updates_pending()
  return @updates_report if @updates_report
  @current_services ||= @client.services
  @current_services_info ||= @client.services_info
  res = Hash.new
  data_services_updates = Hash.new
  applications_updates = Hash.new
  @data_service_updaters = Hash.new
  @application_updaters = Hash.new
  @data_services.each do |data_service|
     unless @data_service_updaters[data_service.name]
       data_service_updater = DataServiceManifestApplier.new data_service, @client, @current_services, @current_services_info
       @data_service_updaters[data_service.name] = data_service_updater
       updates = data_service_updater.updates_pending
       data_services_updates[data_service.name] = updates if updates
     end
  end
  @applications.each do |application|
     unless @application_updaters[application.name]
       application_updater = ApplicationManifestApplier.new application, @client
       @application_updaters[application.name] = application_updater
       updates = application_updater.updates_pending
       applications_updates[application.name] = updates if updates
       # if we did bind to a database; let's re-assign the ownership of the sql functions
       # to the favorite app if there is such a thing.
       if updates && updates['services'] && updates['services']['add']
         list_services_name=updates['services']['add']
         list_services_name.each do |cf_service_name|
           @data_services.each do |data_service|
             if data_service.name == cf_service_name
               # is this the privileged app?
               priv_app_name = data_service.wrapped['director']['bound_app'] if data_service.wrapped['director']
               if priv_app_name == application.name
                 data_service.apply_privileges(priv_app_name)
               end
             end
           end
         end
       end
     end
  end
  res['services'] = data_services_updates unless data_services_updates.empty?
  res['applications'] = applications_updates unless applications_updates.empty?
  @updates_report = res
  puts JSON.pretty_generate @updates_report
  @updates_report
end

#uploadObject



350
351
352
353
354
355
# File 'lib/vmc_knife/vmc_knife.rb', line 350

def upload()
  @applications.each do |application|
    application_updater = ApplicationManifestApplier.new application, @client
    application_updater.upload(@opts[:force])
  end
end

#versionObject



571
572
573
574
575
576
577
578
579
580
581
# File 'lib/vmc_knife/vmc_knife.rb', line 571

def version()
  versions = {}
  @applications.each do |application|
    application_updater = ApplicationManifestApplier.new(application, @client)
    ver = application_updater.version_installed()
    puts "#{application.name()} => #{ver}" if VMC::Cli::Config.trace
    versions[application.name()] = ver
  end
  puts versions
  versions
end