From bbec7c68550de6a17db7388a2d393d97e777d87d Mon Sep 17 00:00:00 2001 From: Joe Rafaniello Date: Wed, 21 Mar 2018 15:25:43 -0400 Subject: [PATCH] Initial add TODO: Write tests, ruggedize this, make sure role activation run this and make sure it works :sweat_smile: All of this depends on: https://github.com/ManageIQ/ManageIQ/manageiq/pull/17096 https://github.com/ManageIQ/manageiq-content/pull/254 --- .../object_management.rb | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/app/models/embedded_ansible_worker/object_management.rb b/app/models/embedded_ansible_worker/object_management.rb index 800c24f53f5e..82aecd770fe9 100644 --- a/app/models/embedded_ansible_worker/object_management.rb +++ b/app/models/embedded_ansible_worker/object_management.rb @@ -53,4 +53,68 @@ def ensure_host(provider, connection) :variables => {'ansible_connection' => "local"}.to_yaml ).id end + + #TODO: naming is hard + CONSOLIDATED_PLUGIN_PLAYBOOKS_TEMPDIR = Pathname.new("/var/lib/awx_consolidated_source/projects/blah").freeze + def ensure_plugin_playbooks_project_seeded(connection) + @connection = connection + clean_consolidated_plugin_directory + copy_plugin_ansible_content + + commit_git_plugin_content + + if project = existing_plugin_playbook_project + update_playbook_project(project) + else + create_playbook_project + end + ensure + # we already have 2 copies: one in the gem and one imported into ansible in the project, delete the temporary one + clean_consolidated_plugin_directory + @connection = nil + end + + private + + def clean_consolidated_plugin_directory + FileUtils.rm_rf(CONSOLIDATED_PLUGIN_PLAYBOOKS_TEMPDIR) + end + + def copy_plugin_ansible_content + FileUtils.mkdir_p(CONSOLIDATED_PLUGIN_PLAYBOOKS_TEMPDIR) + + #TODO: make this a public api via an attr_reader + Vmdb::Plugins.instance.instance_variable_get(:@registered_ansible_content).each do |content| + FileUtils.cp_r(Dir.glob("#{content.path}/*"), CONSOLIDATED_PLUGIN_PLAYBOOKS_TEMPDIR) + end + end + + def commit_git_plugin_content + Dir.chdir(CONSOLIDATED_PLUGIN_PLAYBOOKS_TEMPDIR) do + # ruggedize this + `git init` + `git add -A` + `git commit -m "YOLO Initial Commit"` + end + end + + PLUGIN_PLAYBOOK_PROJECT_NAME = "Default ManageIQ Playbook Project".freeze + PLAYBOOK_PROJECT_ATTRIBUTES = { + :name => PLUGIN_PLAYBOOK_PROJECT_NAME, + :scm_type => "git", + :scm_url => "file:://#{CONSOLIDATED_PLUGIN_PLAYBOOKS_TEMPDIR}", + :scm_update_on_launch => false + }.freeze + + def existing_plugin_playbook_project + @connection.api.projects.all(:name => PLUGIN_PLAYBOOK_PROJECT_NAME).first + end + + def update_playbook_project(project) + project.update_attributes!(PLAYBOOK_PROJECT_ATTRIBUTES) + end + + def create_playbook_project + @connection.api.projects.create!(PLAYBOOK_PROJECT_ATTRIBUTES.to_json) + end end