diff --git a/app/models/embedded_ansible_worker/object_management.rb b/app/models/embedded_ansible_worker/object_management.rb index bf7ae7ece32..076dbf208ad 100644 --- a/app/models/embedded_ansible_worker/object_management.rb +++ b/app/models/embedded_ansible_worker/object_management.rb @@ -1,14 +1,12 @@ module EmbeddedAnsibleWorker::ObjectManagement extend ActiveSupport::Concern - CONSOLIDATED_PLUGIN_PLAYBOOKS_TEMPDIR = Pathname.new("/var/lib/awx_consolidated_source").freeze - def ensure_initial_objects(provider, connection) ensure_organization(provider, connection) ensure_credential(provider, connection) ensure_inventory(provider, connection) ensure_host(provider, connection) - ensure_plugin_playbooks_project_seeded(provider, connection) unless MiqEnvironment::Command.is_container? + ensure_plugin_playbooks_project_seeded(provider, connection) end def remove_demo_data(connection) @@ -58,11 +56,10 @@ def ensure_host(provider, connection) end def ensure_plugin_playbooks_project_seeded(provider, connection) - clean_consolidated_plugin_directory - copy_plugin_ansible_content + ea = EmbeddedAnsible.new + return unless ea.respond_to?(:create_local_playbook_repo) - commit_git_plugin_content - chown_playbooks_tempdir + ea.create_local_playbook_repo project = find_default_project(connection, provider.default_project) if project @@ -78,40 +75,6 @@ def ensure_plugin_playbooks_project_seeded(provider, connection) private - def clean_consolidated_plugin_directory - FileUtils.rm_rf(self.class.consolidated_plugin_directory) - end - - def copy_plugin_ansible_content - FileUtils.mkdir_p(self.class.consolidated_plugin_directory) - - Vmdb::Plugins.instance.registered_ansible_content.each do |content| - FileUtils.cp_r(Dir.glob("#{content.path}/*"), self.class.consolidated_plugin_directory) - end - end - - def chown_playbooks_tempdir - FileUtils.chown_R('awx', 'awx', self.class.consolidated_plugin_directory) - end - - def commit_git_plugin_content - Dir.chdir(self.class.consolidated_plugin_directory) do - require 'rugged' - repo = Rugged::Repository.init_at(".") - index = repo.index - index.add_all("*") - index.write - - options = {} - options[:tree] = index.write_tree(repo) - options[:author] = options[:committer] = { :email => "system@localhost", :name => "System", :time => Time.now.utc } - options[:message] = "Initial Commit" - options[:parents] = [] - options[:update_ref] = 'HEAD' - Rugged::Commit.create(repo, options) - end - end - def find_default_project(connection, project_id) return unless project_id connection.api.projects.find(project_id) @@ -129,7 +92,7 @@ def create_playbook_project(connection, organization) class_methods do def consolidated_plugin_directory - CONSOLIDATED_PLUGIN_PLAYBOOKS_TEMPDIR + EmbeddedAnsible.new.playbook_repo_path end def playbook_project_attributes diff --git a/lib/embedded_ansible.rb b/lib/embedded_ansible.rb index e8ad2ae992d..1b3f451657b 100644 --- a/lib/embedded_ansible.rb +++ b/lib/embedded_ansible.rb @@ -29,6 +29,15 @@ def self.<=>(other_embedded_ansible) other_embedded_ansible.priority <=> priority end + def self.consolidate_plugin_playbooks(dir) + FileUtils.rm_rf(dir) + FileUtils.mkdir_p(dir) + + Vmdb::Plugins.instance.registered_ansible_content.each do |content| + FileUtils.cp_r(Dir.glob("#{content.path}/*"), dir) + end + end + def alive? return false unless configured? && running? begin diff --git a/lib/embedded_ansible/appliance_embedded_ansible.rb b/lib/embedded_ansible/appliance_embedded_ansible.rb index 6a1e8f18071..e81b67aa90f 100644 --- a/lib/embedded_ansible/appliance_embedded_ansible.rb +++ b/lib/embedded_ansible/appliance_embedded_ansible.rb @@ -3,13 +3,14 @@ require "securerandom" class ApplianceEmbeddedAnsible < EmbeddedAnsible - TOWER_VERSION_FILE = "/var/lib/awx/.tower_version".freeze - SETUP_SCRIPT = "ansible-tower-setup".freeze - SECRET_KEY_FILE = "/etc/tower/SECRET_KEY".freeze - SETTINGS_FILE = "/etc/tower/settings.py".freeze - EXCLUDE_TAGS = "packages,migrations,firewall".freeze - HTTP_PORT = 54_321 - HTTPS_PORT = 54_322 + TOWER_VERSION_FILE = "/var/lib/awx/.tower_version".freeze + SETUP_SCRIPT = "ansible-tower-setup".freeze + SECRET_KEY_FILE = "/etc/tower/SECRET_KEY".freeze + SETTINGS_FILE = "/etc/tower/settings.py".freeze + EXCLUDE_TAGS = "packages,migrations,firewall".freeze + HTTP_PORT = 54_321 + HTTPS_PORT = 54_322 + CONSOLIDATED_PLUGIN_PLAYBOOKS_TEMPDIR = Pathname.new("/var/lib/awx_consolidated_source").freeze def self.available? require "linux_admin" @@ -70,6 +71,32 @@ def api_connection api_connection_raw("localhost", HTTP_PORT) end + def create_local_playbook_repo + self.class.consolidate_plugin_playbooks(playbook_repo_path) + + Dir.chdir(playbook_repo_path) do + require 'rugged' + repo = Rugged::Repository.init_at(".") + index = repo.index + index.add_all("*") + index.write + + options = {} + options[:tree] = index.write_tree(repo) + options[:author] = options[:committer] = { :email => "system@localhost", :name => "System", :time => Time.now.utc } + options[:message] = "Initial Commit" + options[:parents] = [] + options[:update_ref] = 'HEAD' + Rugged::Commit.create(repo, options) + end + + FileUtils.chown_R('awx', 'awx', playbook_repo_path) + end + + def playbook_repo_path + CONSOLIDATED_PLUGIN_PLAYBOOKS_TEMPDIR + end + private def upgrade? diff --git a/lib/embedded_ansible/null_embedded_ansible.rb b/lib/embedded_ansible/null_embedded_ansible.rb index 85e141329e8..b0a22574876 100644 --- a/lib/embedded_ansible/null_embedded_ansible.rb +++ b/lib/embedded_ansible/null_embedded_ansible.rb @@ -27,6 +27,14 @@ def api_connection raise NotImplementedError, message end + def create_local_playbook_repo + raise NotImplementedError, message + end + + def playbook_repo_path + raise NotImplementedError, message + end + private def message diff --git a/lib/tasks/evm.rake b/lib/tasks/evm.rake index 11615d54320..5fdbc82859d 100644 --- a/lib/tasks/evm.rake +++ b/lib/tasks/evm.rake @@ -130,4 +130,10 @@ namespace :evm do end EvmDatabase.raise_server_event(opts[:event]) end + + desc "Write all plugin ansible content to a directory" + task :write_plugin_ansible_content => :environment do + dest_dir = ENV["ANSIBLE_CONTENT_DIR"] || Rails.root.join("tmp", "ansible_content") + EmbeddedAnsible.consolidate_plugin_playbooks(dest_dir) + end end diff --git a/spec/lib/embedded_ansible/appliance_embedded_ansible_spec.rb b/spec/lib/embedded_ansible/appliance_embedded_ansible_spec.rb index 817a58bb4da..a90d74f994c 100644 --- a/spec/lib/embedded_ansible/appliance_embedded_ansible_spec.rb +++ b/spec/lib/embedded_ansible/appliance_embedded_ansible_spec.rb @@ -304,6 +304,20 @@ end end + describe "#create_local_playbook_repo" do + let!(:tmp_dir) { Pathname.new(Dir.mktmpdir("consolidated_ansible_playbooks")) } + + before do + allow(subject).to receive(:playbook_repo_path).and_return(tmp_dir) + end + + it "creates a git project containing the plugin playbooks" do + expect(FileUtils).to receive(:chown_R).with("awx", "awx", tmp_dir) + subject.create_local_playbook_repo + expect(Dir.exist?(tmp_dir.join(".git"))).to be_truthy + end + end + describe "#update_proxy_settings (private)" do let(:file_content) do <<-EOF diff --git a/spec/models/embedded_ansible_worker_spec.rb b/spec/models/embedded_ansible_worker_spec.rb index c0b4a6a1b08..80ec0bac3d2 100644 --- a/spec/models/embedded_ansible_worker_spec.rb +++ b/spec/models/embedded_ansible_worker_spec.rb @@ -41,17 +41,6 @@ subject.ensure_initial_objects(provider, api_connection) end - - it "skips playbook seeding for containers" do - allow(MiqEnvironment::Command).to receive(:is_container?).and_return(true) - expect(org_collection).to receive(:create!).and_return(org_resource) - expect(cred_collection).to receive(:create!).and_return(cred_resource) - expect(inv_collection).to receive(:create!).and_return(inv_resource) - expect(host_collection).to receive(:create!).and_return(host_resource) - expect(subject).to receive(:ensure_plugin_playbooks_project_seeded).never - - subject.ensure_initial_objects(provider, api_connection) - end end describe "#remove_demo_data" do @@ -148,7 +137,7 @@ end describe "#ensure_plugin_playbooks_project_seeded" do - let!(:tmp_dir) { Pathname.new(Dir.mktmpdir("consolidated_ansible_playbooks")) } + let(:tmp_dir) { "some/temp/directory" } let!(:expected_attributes) do { :name => "ManageIQ Default Project", @@ -161,12 +150,8 @@ before do provider.default_organization = 42 - allow(EmbeddedAnsibleWorker).to receive(:consolidated_plugin_directory).and_return(tmp_dir) - allow(subject).to receive(:chown_playbooks_tempdir) - end - - after do - FileUtils.rm_rf(tmp_dir) + ea = double("EmbeddedAnsible", :create_local_playbook_repo => "", :playbook_repo_path => tmp_dir) + allow(EmbeddedAnsible).to receive(:new).and_return(ea) end it "creates a git project as the provider's default project" do @@ -175,7 +160,6 @@ .and_return(double(:id => 1234)) subject.ensure_plugin_playbooks_project_seeded(provider, api_connection) - expect(Dir.exist?(tmp_dir.join(".git"))).to be_truthy expect(provider.default_project).to eq(1234) end