Class: EtnaApp::Magma::Models::ApplyTemplate

Inherits:
Etna::Command show all
Includes:
StrongConfirmation, WithEtnaClients, WithLogger
Defined in:
lib/commands.rb

Instance Attribute Summary

Attributes inherited from Etna::Command

#parent

Instance Method Summary collapse

Methods included from WithLogger

#logger

Methods included from StrongConfirmation

#confirm

Methods included from WithEtnaClients

#environment, #exit, exit, #janus_client, #magma_client, #metis_client, #polyphemus_client, #token

Methods inherited from Etna::Command

#completions, #fill_in_missing_params, #find_command, #initialize, parent_scope, #setup

Methods included from Etna::CommandOrExecutor

#command_name, #completions_for, #desc, #flag_argspec, #flag_as_parameter, included, #parse_flags, #program_name, #usage

Constructor Details

This class inherits a constructor from Etna::Command

Instance Method Details

#execute(project_name, target_model: 'project', file: "#{project_name}_models_#{target_model}_tree.csv") ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/commands.rb', line 366

def execute(project_name, target_model: 'project', file: "#{project_name}_models_#{target_model}_tree.csv")
  reset

  unless File.exists?(file)
    puts "Could not find file #{file}"
    return
  end

  load_models_from_csv(file)

  while true
    if @changeset && @errors.empty?
      puts "File #{file} is well formatted.  Calculating expected changes..."
      sync_workflow = workflow.plan_synchronization(@changeset, project_name, target_model)
      models_and_action_types = sync_workflow.planned_actions.map { |a| Etna::Clients::Magma::ModelSynchronizationWorkflow.models_affected_by(a).map { |m| [m, a.action_name] }.flatten }
      models_and_action_types.sort!
      models_and_action_types = models_and_action_types.group_by(&:first)

      models_and_action_types.each do |model, actions|
        actions = actions.map { |a| a[1] }.sort
        actions = actions.group_by { |v| v }
        puts
        puts "#{model} changes:"
        actions.each do |type, actions|
          puts " * #{type}: #{actions.length}"
        end
      end

      puts
      puts "Would you like to execute?"
      if confirm
        sync_workflow.update_block = Proc.new do |action|
          puts "Executing #{action.action_name} on #{Etna::Clients::Magma::ModelSynchronizationWorkflow.models_affected_by(action)}..."
        end

        sync_workflow.execute_planned!
        File.unlink(file)
      end

      return
    end

    # Poll for updates
    puts "Watching for changes to #{file}..."
    while File.stat(file).mtime == @last_load
      sleep(1)
    end

    load_models_from_csv(file)
  end
end

#load_models_from_csv(file) ⇒ Object



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/commands.rb', line 428

def load_models_from_csv(file)
  reset

  @last_load = File.stat(file).mtime
  @changeset = File.open(file, 'r') do |f|
    workflow.prepare_changeset_from_csv(io: f) do |err|
      @errors << err
    end
  end

  return if @errors.empty?

  puts "Input file #{file} is invalid:"
  @errors.each do |err|
    puts "  * " + err.gsub("\n", "\n\t")
  end
end

#resetObject



422
423
424
425
426
# File 'lib/commands.rb', line 422

def reset
  @errors = []
  @changeset = nil
  @last_load = Time.at(0)
end

#workflowObject



418
419
420
# File 'lib/commands.rb', line 418

def workflow
  @workflow ||= Etna::Clients::Magma::AddProjectModelsWorkflow.new(magma_client: magma_client)
end