Skip to content

Commit

Permalink
Merge pull request #57 from agrare/sync_workflows_from_content
Browse files Browse the repository at this point in the history
Seed workflows from plugins' content/workflows
  • Loading branch information
Fryguy authored Mar 29, 2024
2 parents 84629d6 + ea8bd2d commit 73f359f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
class ManageIQ::Providers::Workflows::AutomationManager::ConfigurationScriptSource < ManageIQ::Providers::EmbeddedAutomationManager::ConfigurationScriptSource
FRIENDLY_NAME = "Embedded Workflows Repository".freeze
FRIENDLY_NAME = "Embedded Workflows Repository".freeze
BUILTIN_REPO_NAME = "ManageIQ Built-in Workflows".freeze

supports :update do
_("Cannot update the built-in repository") if name == BUILTIN_REPO_NAME
end
supports :delete do
_("Cannot delete the built-in repository") if name == BUILTIN_REPO_NAME
end

def self.display_name(number = 1)
n_('Repository (Embedded Workflows)', 'Repositories (Embedded Workflows)', number)
end

def self.seed
return unless Settings.prototype.ems_workflows.seed_builtin_workflows

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)
.sync
end

def sync
update!(:status => "running")

transaction do
current = 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)
to_delete = configuration_script_payloads.index_by(&:name)

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 @@ -33,4 +45,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
1 change: 1 addition & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@
:ems_refresh_worker_workflows: {}
:prototype:
:ems_workflows:
:seed_builtin_workflows: false
:enabled: true
5 changes: 4 additions & 1 deletion lib/manageiq/providers/workflows/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ def self.apply_logger_config(config)
end

def self.seedable_classes
%w[ManageIQ::Providers::Workflows]
%w[
ManageIQ::Providers::Workflows
ManageIQ::Providers::Workflows::AutomationManager::ConfigurationScriptSource
]
end

def self.set_floe_runner
Expand Down

0 comments on commit 73f359f

Please sign in to comment.