Class: EtnaApp::Administrate::Models::ApplyTemplate

Inherits:
Etna::Command
  • Object
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



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
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/commands.rb', line 310

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



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/commands.rb', line 372

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



366
367
368
369
370
# File 'lib/commands.rb', line 366

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

#workflowObject



362
363
364
# File 'lib/commands.rb', line 362

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