Skip to content

Commit

Permalink
Merge pull request #17407 from carbonin/container_playbook_seeding
Browse files Browse the repository at this point in the history
Expose plugin ansible content consolidation as a rake task
  • Loading branch information
jrafanie authored May 21, 2018
2 parents 9720be7 + f8568b9 commit 428d092
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 68 deletions.
47 changes: 5 additions & 42 deletions app/models/embedded_ansible_worker/object_management.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions lib/embedded_ansible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 34 additions & 7 deletions lib/embedded_ansible/appliance_embedded_ansible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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?
Expand Down
8 changes: 8 additions & 0 deletions lib/embedded_ansible/null_embedded_ansible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/tasks/evm.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions spec/lib/embedded_ansible/appliance_embedded_ansible_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 3 additions & 19 deletions spec/models/embedded_ansible_worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 428d092

Please sign in to comment.