Skip to content

Commit

Permalink
Use ConfigurationScriptSource#sync for wf seeding
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Mar 29, 2024
1 parent 5d42841 commit 589b1c3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -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(/.+\/(?<plugin>.+)\/content\/workflows\/(?<path>.+)/)
.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"]
Expand Down
1 change: 0 additions & 1 deletion lib/manageiq/providers/workflows/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def self.seedable_classes
%w[
ManageIQ::Providers::Workflows
ManageIQ::Providers::Workflows::AutomationManager::ConfigurationScriptSource
ManageIQ::Providers::Workflows::AutomationManager::Workflow
]
end

Expand Down

0 comments on commit 589b1c3

Please sign in to comment.