Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose plugin ansible content consolidation as a rake task #17407

Merged
merged 4 commits into from
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 2 additions & 42 deletions app/models/embedded_ansible_worker/object_management.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
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)
Expand Down Expand Up @@ -58,11 +56,7 @@ def ensure_host(provider, connection)
end

def ensure_plugin_playbooks_project_seeded(provider, connection)
clean_consolidated_plugin_directory
copy_plugin_ansible_content

commit_git_plugin_content
chown_playbooks_tempdir
EmbeddedAnsible.new.create_local_playbook_repo

project = find_default_project(connection, provider.default_project)
if project
Expand All @@ -78,40 +72,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 +89,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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised there isn't a master branch. Does awx/tower only reference the HEAD commit anyway?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a master branch after everything is set up. Is that just the default behavior?

[root@localhost ~]# cd /var/lib/awx_consolidated_source/
[root@localhost awx_consolidated_source]# git status
# On branch master
nothing to commit, working directory clean
[root@localhost awx_consolidated_source]# git log
commit 882ea0dbf707c366de82655bd1b74376ccf1d712
Author: System <system@localhost>
Date:   Mon May 14 19:06:53 2018 +0000

    Initial Commit
[root@localhost awx_consolidated_source]#

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is probably using the default behavior.

Rugged::Commit.create(repo, options)
end

FileUtils.chown_R('awx', 'awx', playbook_repo_path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does awx own the repo? I know that was there before, but that doesn't make any sense to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrafanie ? I know the tower processes run as the awx user, but if it's just copying off the files it seems like it wouldn't need to own the directory.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assumption was the awx user would be reading this git repo's path when we posted this local path for the new project. If it has permission to read in all situations, then this can be removed.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, this just copies the content but doesn't set up git?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct.

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
11 changes: 3 additions & 8 deletions spec/models/embedded_ansible_worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,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 +161,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 +171,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