From 589b1c3cdd306b6f3513adb7eea25fdd5dbb7715 Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Fri, 29 Mar 2024 12:20:26 -0400 Subject: [PATCH] Use ConfigurationScriptSource#sync for wf seeding --- .../configuration_script_source.rb | 58 ++++++++++++++----- .../workflows/automation_manager/workflow.rb | 24 -------- lib/manageiq/providers/workflows/engine.rb | 1 - 3 files changed, 44 insertions(+), 39 deletions(-) diff --git a/app/models/manageiq/providers/workflows/automation_manager/configuration_script_source.rb b/app/models/manageiq/providers/workflows/automation_manager/configuration_script_source.rb index 9d43267..1b71da8 100644 --- a/app/models/manageiq/providers/workflows/automation_manager/configuration_script_source.rb +++ b/app/models/manageiq/providers/workflows/automation_manager/configuration_script_source.rb @@ -17,29 +17,24 @@ def self.seed manager = ManageIQ::Providers::Workflows::AutomationManager.in_my_region.first return if manager.nil? - manager.configuration_script_sources.find_or_create_by!(:type => name, :name => BUILTIN_REPO_NAME) + manager.configuration_script_sources + .find_or_create_by!(:type => name, :name => BUILTIN_REPO_NAME) + .sync end def sync update!(:status => "running") transaction do - current = configuration_script_payloads.index_by(&:name) + to_delete = configuration_script_payloads.index_by(&:name) - git_repository.update_repo - git_repository.with_worktree do |worktree| - worktree.ref = scm_branch - worktree.blob_list.each do |filename| - next if filename.start_with?(".") || !filename.end_with?(".asl") - - payload = worktree.read_file(filename) - found = current.delete(filename) || self.class.module_parent::Workflow.new(:configuration_script_source_id => id) - - found.update!(:name => filename, :manager_id => manager_id, :payload => payload, :payload_type => "json") - end + if git_repository.present? + sync_from_git_repository(to_delete) + else + sync_from_content(to_delete) end - current.values.each(&:destroy) + to_delete.each_value(&:destroy) configuration_script_payloads.reload end @@ -48,4 +43,39 @@ def sync update!(:status => "error", :last_updated_on => Time.zone.now, :last_update_error => error) raise error end + + private + + def sync_from_git_repository(to_delete) + git_repository.update_repo + git_repository.with_worktree do |worktree| + worktree.ref = scm_branch + worktree.blob_list.each do |filename| + next if filename.start_with?(".") || !filename.end_with?(".asl") + + payload = worktree.read_file(filename) + workflow = create_workflow_from_payload(filename, payload) + to_delete.delete(workflow.name) + end + end + end + + def sync_from_content(to_delete) + Vmdb::Plugins.embedded_workflows_content.each do |engine, workflow_paths| + base_dir = engine.root.join("content", "workflows") + workflow_paths.each do |filename| + workflow_name = filename.relative_path_from(base_dir).to_s + payload = File.read(filename) + workflow = create_workflow_from_payload(workflow_name, payload) + + to_delete.delete(workflow.name) + end + end + end + + def create_workflow_from_payload(name, payload) + configuration_script_payloads.find_or_initialize_by(:name => name).tap do |wf| + wf.update!(:name => name, :manager_id => manager_id, :type => self.class.module_parent::Workflow.name, :payload => payload, :payload_type => "json") + end + end end diff --git a/app/models/manageiq/providers/workflows/automation_manager/workflow.rb b/app/models/manageiq/providers/workflows/automation_manager/workflow.rb index 24b1f39..92a4264 100644 --- a/app/models/manageiq/providers/workflows/automation_manager/workflow.rb +++ b/app/models/manageiq/providers/workflows/automation_manager/workflow.rb @@ -1,28 +1,4 @@ class ManageIQ::Providers::Workflows::AutomationManager::Workflow < ManageIQ::Providers::EmbeddedAutomationManager::ConfigurationScriptPayload - def self.seed - manager = ManageIQ::Providers::Workflows::AutomationManager.in_my_region.first - return if manager.nil? - - repo = manager.configuration_script_sources.find_by(:name => ManageIQ::Providers::Workflows::AutomationManager::ConfigurationScriptSource::BUILTIN_REPO_NAME) - return if repo.nil? - - to_delete = repo.configuration_script_payloads.pluck(:id) - Vmdb::Plugins.embedded_workflows_content.each do |filename| - plugin, path = filename.to_s - .match(/.+\/(?.+)\/content\/workflows\/(?.+)/) - .values_at("plugin", "path") - - payload = File.read(filename) - workflow = repo.configuration_script_payloads.find_or_initialize_by(:name => "#{plugin}/#{path}") - - workflow.update!(:type => name, :manager => manager, :payload => payload, :payload_type => "json") - - to_delete.delete(workflow.id) - end - - destroy(to_delete) - end - def self.create_from_json!(json, **kwargs) json = JSON.parse(json) if json.kind_of?(String) name = json["Comment"] diff --git a/lib/manageiq/providers/workflows/engine.rb b/lib/manageiq/providers/workflows/engine.rb index b3654a0..ac4797f 100644 --- a/lib/manageiq/providers/workflows/engine.rb +++ b/lib/manageiq/providers/workflows/engine.rb @@ -34,7 +34,6 @@ def self.seedable_classes %w[ ManageIQ::Providers::Workflows ManageIQ::Providers::Workflows::AutomationManager::ConfigurationScriptSource - ManageIQ::Providers::Workflows::AutomationManager::Workflow ] end