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



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/commands.rb', line 192

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



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/commands.rb', line 254

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



248
249
250
251
252
# File 'lib/commands.rb', line 248

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

#workflowObject



244
245
246
# File 'lib/commands.rb', line 244

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