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

Add awx as an embedded ansible plugin #16205

Merged
merged 12 commits into from
Dec 6, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ gem "color", "~>1.8"
gem "config", "~>1.3.0", :require => false
gem "dalli", "~>2.7.4", :require => false
gem "default_value_for", "~>3.0.3"
gem "docker-api", "~>1.33.6", :require => false
gem "elif", "=0.1.0", :require => false
gem "fast_gettext", "~>1.2.0"
gem "gettext_i18n_rails", "~>1.7.2"
Expand Down
15 changes: 8 additions & 7 deletions app/models/embedded_ansible_worker/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,18 @@ def message_sync_config(*_args); end
private

def provider_url
server = MiqServer.my_server(true)
URI::Generic.build(provider_uri_hash).to_s
end

def provider_uri_hash
if MiqEnvironment::Command.is_container?
host = ENV["ANSIBLE_SERVICE_HOST"]
path = "/api/v1"
{:scheme => "https", :host => ENV["ANSIBLE_SERVICE_HOST"], :path => "/api/v1"}
elsif Rails.env.development?
{:scheme => "http", :host => "localhost", :path => "/api/v1", :port => 54321}
else
host = server.hostname || server.ipaddress
path = "/ansibleapi/v1"
server = MiqServer.my_server(true)
{:scheme => "https", :host => server.hostname || server.ipaddress, :path => "/ansibleapi/v1"}
end

URI::HTTPS.build(:host => host, :path => path).to_s
end

def raise_role_notification(notification_type)
Expand Down
10 changes: 10 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@
:history:
:keep_drift_states: 6.months
:purge_window_size: 10000
:embedded_ansible:
:docker:
:task_image_name: ansible/awx_task
:task_image_tag: latest
:web_image_name: ansible/awx_web
:web_image_tag: latest
:rabbitmq_image_name: rabbitmq
:rabbitmq_image_tag: 3
:memcached_image_name: memcached
:memcached_image_tag: alpine
:ems:
# provider specific settings are nested here, but they are in the provider repos
# e.g.:
Expand Down
46 changes: 45 additions & 1 deletion lib/embedded_ansible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.new
end

def self.detect_available_platform
subclasses.detect(&:available?) || NullEmbeddedAnsible
subclasses.sort.detect(&:available?) || NullEmbeddedAnsible
end

def self.available?
Expand All @@ -21,6 +21,14 @@ def self.enabled?
MiqServer.my_server(true).has_active_role?(ANSIBLE_ROLE)
end

def self.priority
0
end

def self.<=>(other_embedded_ansible)
other_embedded_ansible.priority <=> priority
end

def alive?
return false unless configured? && running?
begin
Expand All @@ -43,9 +51,45 @@ def api_connection_raw(host, port)
)
end

def find_or_create_secret_key
miq_database.ansible_secret_key ||= SecureRandom.hex(16)
end

def find_or_create_admin_authentication
miq_database.ansible_admin_authentication || miq_database.set_ansible_admin_authentication(:password => generate_password)
end

def find_or_create_rabbitmq_authentication
miq_database.ansible_rabbitmq_authentication || miq_database.set_ansible_rabbitmq_authentication(:password => generate_password)
end

def find_or_create_database_authentication
auth = miq_database.ansible_database_authentication
return auth if auth

auth = miq_database.set_ansible_database_authentication(:password => generate_password)

database_connection.select_value("CREATE ROLE #{database_connection.quote_column_name(auth.userid)} WITH LOGIN PASSWORD #{database_connection.quote(auth.password)}")
database_connection.select_value("CREATE DATABASE awx OWNER #{database_connection.quote_column_name(auth.userid)} ENCODING 'utf8'")

auth
end

def generate_password
Copy link
Member

Choose a reason for hiding this comment

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

I can't tell if they aren't already, but these should probably be private methods from here down.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, they're private starting at line 34

SecureRandom.base64(18).tr("+/", "-_")
end

def miq_database
MiqDatabase.first
end

def database_connection
ActiveRecord::Base.connection
end

def database_configuration
@db_config ||= ActiveRecord::Base.configurations[Rails.env]
end
end

Dir.glob(File.join(File.dirname(__FILE__), "embedded_ansible/*.rb")).each { |f| require_dependency f }
47 changes: 11 additions & 36 deletions lib/embedded_ansible/appliance_embedded_ansible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def self.available?
required_rpms.subset?(LinuxAdmin::Rpm.list_installed.keys.to_set)
end

def self.priority
30
end

def initialize
require "linux_admin"
end
Expand Down Expand Up @@ -112,13 +116,8 @@ def with_inventory_file
end

def configure_secret_key
key = miq_database.ansible_secret_key
if key.present?
File.write(SECRET_KEY_FILE, key)
else
AwesomeSpawn.run!("/usr/bin/python -c \"import uuid; file('#{SECRET_KEY_FILE}', 'wb').write(uuid.uuid4().hex)\"")
miq_database.ansible_secret_key = File.read(SECRET_KEY_FILE)
end
key = find_or_create_secret_key
File.write(SECRET_KEY_FILE, key)
end

def update_proxy_settings
Expand All @@ -135,26 +134,10 @@ def update_proxy_settings
File.write(SETTINGS_FILE, new_contents)
end

def generate_admin_authentication
miq_database.set_ansible_admin_authentication(:password => generate_password)
end

def generate_rabbitmq_authentication
miq_database.set_ansible_rabbitmq_authentication(:password => generate_password)
end

def generate_database_authentication
auth = miq_database.set_ansible_database_authentication(:password => generate_password)
database_connection.select_value("CREATE ROLE #{database_connection.quote_column_name(auth.userid)} WITH LOGIN PASSWORD #{database_connection.quote(auth.password)}")
database_connection.select_value("CREATE DATABASE awx OWNER #{database_connection.quote_column_name(auth.userid)} ENCODING 'utf8'")
auth
end

def inventory_file_contents
admin_auth = miq_database.ansible_admin_authentication || generate_admin_authentication
rabbitmq_auth = miq_database.ansible_rabbitmq_authentication || generate_rabbitmq_authentication
database_auth = miq_database.ansible_database_authentication || generate_database_authentication
db_config = Rails.configuration.database_configuration[Rails.env]
admin_auth = find_or_create_admin_authentication
rabbitmq_auth = find_or_create_rabbitmq_authentication
database_auth = find_or_create_database_authentication

<<-EOF.strip_heredoc
[tower]
Expand All @@ -165,8 +148,8 @@ def inventory_file_contents
[all:vars]
admin_password='#{admin_auth.password}'

pg_host='#{db_config["host"] || "localhost"}'
pg_port='#{db_config["port"] || "5432"}'
pg_host='#{database_configuration["host"] || "localhost"}'
pg_port='#{database_configuration["port"] || "5432"}'

pg_database='awx'
pg_username='#{database_auth.userid}'
Expand All @@ -182,14 +165,6 @@ def inventory_file_contents
EOF
end

def generate_password
SecureRandom.base64(18).tr("+/", "-_")
end

def database_connection
ActiveRecord::Base.connection
end

def local_tower_version
File.read(TOWER_VERSION_FILE).strip
end
Expand Down
4 changes: 4 additions & 0 deletions lib/embedded_ansible/container_embedded_ansible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ def self.available?
ContainerOrchestrator.available?
end

def self.priority
20
end

def start
miq_database.set_ansible_admin_authentication(:password => ENV["ANSIBLE_ADMIN_PASSWORD"])
ContainerOrchestrator.new.scale(ANSIBLE_DC_NAME, 1)
Expand Down
Loading