diff --git a/config.yaml b/config.yaml index 54ab94b..4e923c2 100644 --- a/config.yaml +++ b/config.yaml @@ -2,7 +2,7 @@ options: maas-mode: type: string default: region - description: Type of maas node. Valid options are, (region, rack, region+rack, all) + description: Type of maas node. Valid options are, (region, rack) install_sources: type: string default: ppa:maas/next diff --git a/layer.yaml b/layer.yaml index 2295b1b..4c21c52 100644 --- a/layer.yaml +++ b/layer.yaml @@ -4,9 +4,11 @@ includes: - interface:pgsql - interface:http - interface:maas + - layer:leadership -options: - apt: - packages: - - maas-region-api -repo: https://www.github.com/seffyroff/layer-maas/ \ No newline at end of file + +# options: +# apt: +# packages: +# - maas-region-api +repo: https://www.github.com/seffyroff/maas/ \ No newline at end of file diff --git a/metadata.yaml b/metadata.yaml index 64d9719..2741921 100644 --- a/metadata.yaml +++ b/metadata.yaml @@ -1,7 +1,7 @@ -name: maas-region -display-name: MAAS Region Controller -summary: MAAS Region Controller -maintainer: Phil Merricks +name: maas +display-name: MAAS Region and Rack Controller +summary: MAAS Region/Rack Controller +maintainer: Phil Merricks description: | Metal As A Service tags: diff --git a/reactive/maas.py b/reactive/maas.py index f58a1dd..f7bc2a9 100644 --- a/reactive/maas.py +++ b/reactive/maas.py @@ -16,7 +16,7 @@ set_flag('maas.mode.{}'.format(config('maas-mode'))) -status_set('maintenance', 'MAAS {} Configuration'.format(config('maas-mode'))) +# status_set('maintenance', 'MAAS {} Configuration'.format(config('maas-mode'))) kv = unitdata.kv() @@ -27,27 +27,27 @@ def maas_url(): return 'http://{}:5240/MAAS'.format(PRIVATE_IP) @when('maas.mode.region') -@when_not('apt.maas-region-api.installed.') +@when_not('apt.installed.maas-region-api') def install_maas_region_api(): charms.apt.queue_install(['maas-region-api']) -@when('maas.mode.region') -@when('apt.installed.maas-region-api') +@when('maas.mode.region', + 'apt.installed.maas-region-api') @when_not('maas.version.set') def set_message_version(): application_version_set(get_upstream_version('maas-region-api')) # message = sp.check_output('maas', '--version', stderr=sp.STDOUT) - status_set('maintenance', 'MAAS Region API Installed' ) + status_set('maintenance', 'Region installed, waiting for DB rel' ) set_flag('maas.version.set') # @when_not('postgresql.connected') # def set_waiting_for_db_relation(): # status_set('waiting', 'Waiting for PGSQL Relation') -@when('maas.mode.region') -@when('postgresql.connected') +@when('maas.mode.region', + 'postgresql.connected') @when_not('maas.database.requested') def request_postgresql_database_for_maas_region(pgsql): """Request PGSql DB @@ -64,8 +64,8 @@ def request_postgresql_database_for_maas_region(pgsql): status_set('active', 'MAASDB requested') set_flag('maas.database.requested') -@when('maas.mode.region') -@when('postgresql.master.available', +@when('maas.mode.region', + 'postgresql.master.available', 'maas.database.requested') @when_not('maas.juju.database.available') def get_set_postgresql_data_for_maas_db(pgsql): @@ -82,9 +82,10 @@ def get_set_postgresql_data_for_maas_db(pgsql): clear_flag('maas.manual.database.available') set_flag('maas.juju.database.available') -@when('maas.mode.region') -@when('apt.maas-region-api.installed', 'leadership.is_leader') -@when('maas.juju.database.available') +@when('maas.mode.region', + 'apt.installed.maas-region-api', + 'leadership.is_leader', + 'maas.juju.database.available') @when_not('maas.init.complete') def maas_leader_init(): """Init MAAS (region, region+rack) - only leader should run this code @@ -121,8 +122,9 @@ def maas_leader_init(): status_set('active', 'MAAS-{} configured'.format(config('maas-mode'))) set_flag('maas.init.complete') -@when('maas.mode.region') -@when('maas.init.complete', 'leadership.is_leader') +@when('maas.mode.region', + 'maas.init.complete', + 'leadership.is_leader') @when_not('maas.secret.published') def get_set_secret(): status_set('maintenance', 'Retrieving Rack Secret') @@ -161,23 +163,30 @@ def send_relation_data_to_rack(): ### RACK CONTROLLER LOGIC @when('maas.mode.rack') -@when_not('apt.maas-rack-controller.installed') +@when_not('apt.installed.maas-rack-controller') def install_maas_rack_controller(): charms.apt.queue_install(['maas-rack-controller']) -@when('maas.mode.rack') -@when('apt.installed.maas-rack-controller') +@when('maas.mode.rack', + 'apt.installed.maas-rack-controller') @when_not('maas.version.set') def set_message_version(): application_version_set(get_upstream_version('maas-rack-controller')) - status_set('maintenance', 'MAAS Rack Controller Installed' ) + status_set('maintenance', 'Rack Installed, waiting for Region rel' ) set_flag('maas.version.set') -@when('maas.mode.rack') -@when('endpoint.rack.available') -@when('apt.installed.maas-rack-controller') +# @when('maas.mode.rack') +# @when('apt.installed.maas-rack-controller') +# @when_not('endpoint.rack.available') +# def set_rack_waiting_relation(): +# status_set('waiting', 'Waiting for Region Relation' ) + + +@when('maas.mode.rack', + 'endpoint.rack.available', + 'apt.installed.maas-rack-controller') @when_not('rack.relation.data.available') def acquire_config_from_region_controller(): """Acquire maas_url and secret from region @@ -191,8 +200,8 @@ def acquire_config_from_region_controller(): status_set('active', 'Region configuration acquired') set_flag('rack.relation.data.available') -@when('maas.mode.rack') -@when('rack.relation.data.available') +@when('maas.mode.rack', + 'rack.relation.data.available') @when_not('maas.rack.init.complete') def configure_maas_rack(): """Configure rack controller now that we have what we need diff --git a/reactive/maas_region.py b/reactive/maas_region.py deleted file mode 100644 index 5f9fc96..0000000 --- a/reactive/maas_region.py +++ /dev/null @@ -1,113 +0,0 @@ -from charms.reactive import when, when_not, when_any, set_flag, clear_flag -from charmhelpers.core.hookenv import application_version_set, status_set, network_get, config, open_port -from charmhelpers.core import unitdata -from charmhelpers.core.services.base import service_restart -from charmhelpers.fetch import get_upstream_version -import subprocess as sp -from subprocess import call -from charmhelpers.core.templating import render - -PRIVATE_IP = network_get('http')['ingress-addresses'][0] -MAAS_WEB_PORT = 5240 - - -set_flag('maas.mode.{}'.format(config('maas-mode'))) - -kv = unitdata.kv() - -def maas_url(): - if config('maas-url'): - return config('maas-url') - else: - return 'http://{}:5240/MAAS'.format(PRIVATE_IP) - - -@when_not('maas-region-ppa.installed') -def install_maas_region_ppa(): - set_flag('maas-region-ppa.installed') - -@when('apt.installed.maas-region-controller') -def set_message_version(): - application_version_set(get_upstream_version('maas-region-controller')) - - # message = sp.check_output('maas', '--version', stderr=sp.STDOUT) - - # status_set('maintenance', message ) - - set_flag('maas.version.set') -# @when_not('postgresql.connected') -# def set_waiting_for_db_relation(): -# status_set('waiting', 'Waiting for PGSQL Relation') - -@when('postgresql.connected') -@when_not('maas.database.requested') -def request_postgresql_database_for_maas_region(pgsql): - """Request PGSql DB - """ - - conf = config() - status_set('maintenance', 'Requesting MAASDB') - - pgsql.set_database(conf.get('db-name', 'maasdb')) - if conf.get('db-roles'): - pgsql.set_roles(conf.get('db-roles')) - if conf.get('db-extensions'): - pgsql.set_extensions(conf.get('db-extensions')) - status_set('active', 'MAASDB requested') - set_flag('maas.database.requested') - -@when('postgresql.master.available', - 'maas.database.requested') -@when_not('maas.juju.database.available') -def get_set_postgresql_data_for_maas_db(pgsql): - """Get/set postgresql details - """ - - status_set('maintenance', 'Database acquired, saving details') - kv.set('db_host', pgsql.master.host) - kv.set('db_name', pgsql.master.dbname) - kv.set('db_pass', pgsql.master.password) - kv.set('db_user', pgsql.master.user) - status_set('active', 'MAASDB saved to unitdata') - - clear_flag('maas.manual.database.available') - set_flag('maas.juju.database.available') - -@when('maas-region-ppa.installed') -@when('maas.juju.database.available') -@when_not('maas.init.complete') -def maas_leader_init(): - """Init MAAS (region, region+rack) - only leader should run this code - """ - status_set('maintenance', - 'Configuring MAAS-{}'.format(config('maas-mode'))) - - init_ctxt = {'maas_url': maas_url(), - 'maas_mode': config('maas-mode'), - 'db_host': kv.get('db_host'), - 'db_name': kv.get('db_name'), - 'db_pass': kv.get('db_pass'), - 'db_user': kv.get('db_user')} - - cmd_init = ('maas-region local_config_set --maas-url {maas_url} --database-host {db_host} ' - '--database-name {db_name} --database-user {db_user} ' - '--database-pass {db_pass}'.format(**init_ctxt)) - - createadmin_ctxt = {'maas_username': config('admin-username'), - 'maas_password': config('admin-password'), - 'maas_email': config('admin-email'), - 'maas_ssh_import': config('admin-ssh-import')} - - cmd_dbupgrade = ('maas-region dbupgrade'.format()) - - cmd_createadmin = ('maas createadmin --username {maas_username} --password {maas_password} ' - '--email {maas_email} --ssh-import {maas_ssh_import}'.format(**createadmin_ctxt)) - - call(cmd_init.split()) - call(cmd_dbupgrade.split()) - service_restart('maas-regiond') - call(cmd_createadmin.split()) - - status_set('active', 'MAAS-{} configured'.format(config('maas-mode'))) - set_flag('maas.init.complete') - diff --git a/reactive/maas_region_ppa.__.pybak b/reactive/maas_region_ppa.__.pybak deleted file mode 100644 index 5fd63e8..0000000 --- a/reactive/maas_region_ppa.__.pybak +++ /dev/null @@ -1,345 +0,0 @@ -from charms.reactive import when, when_not, set_flag, clear_flag -from charmhelpers.core.hookenv import application_version_set, status_set, network_get, unitdata, config, open_port -from charmhelpers.fetch import get_upstream_version -import subprocess as sp -from charmhelpers.core.templating import render - -PRIVATE_IP = network_get('http')['ingress-addresses'][0] -MAAS_WEB_PORT = 5240 - - -set_flag('maas.mode.{}'.format(config('maas-mode'))) - -kv = unitdata.kv() - -def maas_url(): - if config('maas-url'): - return config('maas-url') - else: - return 'http://{}:5240/MAAS'.format(PRIVATE_IP) - -# # Presistent unit data. -# unit_db = unitdata.kv() - -# # Global config options -# global_configs = [ -# ('maas-name', 'maas_name', 'string'), -# ('main-archive', 'main_archive', 'string'), -# ('ports-archive', 'ports_archive', 'string'), -# ('enable-http-proxy', 'enable_http_proxy', 'boolean'), -# ('http-proxy', 'http_proxy', 'string'), -# ('upstream-dns', 'upstream_dns', 'string'), -# ('dnssec-validation', 'dnssec_validation', 'string'), -# ('ntp-server', 'ntp_server', 'string'), -# ('default-storage-layout', 'default_storage_layout', 'string'), -# ( -# 'enable-disk-erasing-on-release', -# 'enable_disk_erasing_on_release', 'boolean'), -# ('curtin-verbose', 'curtin_verbose', 'boolean'), -] - - -@when_not('maas-region-ppa.installed') -def install_maas_region_ppa(): - set_flag('maas-region-ppa.installed') - -@when('apt.installed.maas-region-controller') -def set_message_version(): - application_version_set(get_upstream_version('maas-region-controller')) - - message = sp.check_output('maas', '--version', stderr=sp.STDOUT) - - status_set('maintenance', message ) - - set_flag('maas.version.set') -@when_not('postgresql.connected') - status_set('Waiting for PGSQL Relation') - -@when('postgresql.connected') -@when_not('maas.database.requested') -def request_postgresql_database_for_maas_region(pgsql): - """Request PGSql DB - """ - - conf = config() - status_set('maintenance', 'Requesting MAASDB') - - pgsql.set_database(conf.get('db-name', 'maasdb')) - if conf.get('db-roles'): - pgsql.set_roles(conf.get('db-roles')) - if conf.get('db-extensions'): - pgsql.set_extensions(conf.get('db-extensions')) - status_set('active', 'MAASDB requested') - set_flag('maas.database.requested') - -@when('postgresql.master.available', - 'maas.database.requested') -@when_not('maas.juju.database.available') -def get_set_postgresql_data_for_maas_db(pgsql): - """Get/set postgresql details - """ - - status_set('maintenance', 'Database acquired, saving details') - kv.set('db_host', pgsql.master.host) - kv.set('db_name', pgsql.master.dbname) - kv.set('db_pass', pgsql.master.password) - kv.set('db_user', pgsql.master.user) - status_set('active', 'MAASDB saved to unitdata') - - clear_flag('maas.manual.database.available') - set_flag('maas.juju.database.available') - -@when('maas-region-ppa.installed') -@when_any('maas.juju.database.available', - 'maas.manual.database.available') -@when_not('maas.init.complete') -def maas_leader_init(): - """Init MAAS (region, region+rack) - only leader should run this code - """ - status_set('maintenance', - 'Configuring MAAS-{}'.format(config('maas-mode'))) - - init_ctxt = {'maas_url': maas_url(), - 'maas_mode': config('maas-mode'), - 'db_host': kv.get('db_host'), - 'db_name': kv.get('db_name'), - 'db_pass': kv.get('db_pass'), - 'db_user': kv.get('db_user')} - - cmd_init = ('maas-region local_config_set --maas-url {maas_url} --database-host {db_host} ' - '--database-name {db_name} --database-user {db_user} ' - '--database-pass {db_pass}' - '--force'.format(**init_ctxt)) - - call(cmd_init.split()) - status_set('active', 'MAAS-{} configured'.format(config('maas-mode'))) - set_flag('maas.init.complete') - - -# def get_maas_secret(): -# """Return the MAAS secret value.""" -# with open('/var/lib/maas/secret', 'r') as fp: -# return fp.read().strip() - - -# def get_apikey(): -# """Return the API key for the admin user.""" -# apikey = leadership.leader_get('apikey') -# if not apikey: -# apikey = check_output([ -# 'maas-region', 'apikey', -# '--username', leadership.leader_get('admin_username')]) -# apikey = apikey.decode('ascii').strip() -# leadership.leader_set(apikey=apikey) -# return apikey - -# def update_config_value(charm_name, maas_name, value_type="string"): -# """Update the global config values.""" -# if not hookenv.is_leader(): -# # Only the leader updates config values. -# return -# admin_username = leadership.leader_get('admin_username') -# if not admin_username: -# # Value can only be set once an administrator has been created. -# return - -# # Ensure logged into API. -# check_call([ -# 'maas', 'login', 'juju-admin', -# 'http://localhost:5240/MAAS', get_apikey()]) - -# # Get the current value. -# result = check_output([ -# 'maas', 'juju-admin', 'maas', 'get-config', 'name=%s' % maas_name]) -# result = result.decode("utf-8") -# if value_type == "boolean": -# if result == "true": -# result = True -# else: -# result = False - -# # Update the value if its different. -# new_value = hookenv.config(charm_name) -# if result != new_value: -# check_call([ -# 'maas', 'juju-admin', 'maas', -# 'set-config', 'name=%s' % maas_name, 'value=%s' % new_value]) - - -# def set_all_global_configs(): -# """Set all global configs. - -# Called after the administrator is first created. -# """ -# hookenv.status_set('maintenance', 'Setting all global config options') -# for charm_name, maas_name, value_type in global_configs: -# update_config_value(charm_name, maas_name, value_type) - - -# def update_admin(old_username, username, email, password): -# env = os.environ.copy() -# env['DJANGO_SETTINGS_MODULE'] = 'maas.settings' -# env['PYTHONPATH'] = '/usr/share/maas' -# check_call([ -# 'python3', 'scripts/update_user.py', -# '--old-username', old_username, -# '--username', username, -# '--email', email, -# '--password', password], env=env) - -# @hook('config-changed') -# def update_global_configs(): -# config = hookenv.config() -# for charm_name, maas_name, value_type in global_configs: -# if config.changed(charm_name): -# update_config_value(charm_name, maas_name, value_type) -# @hook('db-relation-joined') -# def db_joined(pgsql): -# # psql layer does not handle departing correctly. We set -# # db_joined so the write_db_config is not skipped. -# unit_db.set('db_joined', True) -# hookenv.status_set('waiting', 'Requesting database from PostgreSQL') -# pgsql.set_database(hookenv.config('dbname')) - - -# @hook('db-relation-departed') -# def db_departed(pgsql): -# # psql layer does not handle departing correctly. We clear -# # db_joined so the write_db_config is skipped. -# unit_db.unset('db_joined') -# unit_db.unset('dbhost') -# unit_db.unset('dbdb') -# unit_db.unset('dbuser') -# unit_db.unset('dbpass') -# check_call([ -# 'maas-region', 'local_config_set', -# '--database-host', '', -# '--database-name', '', -# '--database-user', '', -# '--database-pass', '', -# ]) -# close_ports() -# remove_state('regiond.running') -# host.service_stop('maas-regiond') -# hookenv.status_set('waiting', 'Waiting on relation to PostgreSQL') - - -# @when_any('config.changed.maas-url', 'config.changed.vip') -# def write_maas_url(): -# hookenv.status_set('maintenance', 'Writing maas_url into regiond.conf') -# vip = hookenv.config('vip') -# maas_url = hookenv.config('maas-url') -# if vip: -# if not is_valid_ip(vip): -# url = '' -# else: -# url = 'http://%s/MAAS' % vip -# elif maas_url: -# url = maas_url -# else: -# url = 'http://localhost/MAAS' -# check_call([ -# 'maas-region', 'local_config_set', -# '--maas-url', url, -# ]) -# start_regiond_when_ready() - - -# @when_not('db.database.available') -# def missing_postgresql(): -# close_ports() -# remove_state('regiond.running') -# host.service_stop('maas-regiond') -# hookenv.status_set('waiting', 'Waiting on relation to PostgreSQL') - - -# @when('db.database.available') -# def write_db_config(pgsql): -# # Do nothing if not db joined. -# if not unit_db.get('db_joined', False): -# return - -# # Update the database configuration only if any of the configuration -# # values have changed. Nothing needs to be done if nothing has changed. -# dbhost = pgsql.host() -# dbdb = pgsql.database() -# dbuser = pgsql.user() -# dbpass = pgsql.password() -# has_changed = ( -# unit_db.get('dbhost') != dbhost or -# unit_db.get('dbdb') != dbdb or -# unit_db.get('dbuser') != dbuser or -# unit_db.get('dbpass') != dbpass) -# if has_changed: -# unit_db.set('dbhost', dbhost) -# unit_db.set('dbdb', dbdb) -# unit_db.set('dbuser', dbuser) -# unit_db.set('dbpass', dbpass) -# hookenv.status_set( -# 'maintenance', 'Writing db configuration into regiond.conf') - -# check_call([ -# 'maas-region', 'local_config_set', -# '--database-host', dbhost if dbhost else '', -# '--database-name', dbdb if dbdb else '', -# '--database-user', dbuser if dbuser else '', -# '--database-pass', dbpass if dbpass else '', -# ]) -# if all([dbhost, dbdb, dbuser, dbpass]): -# start_regiond_when_ready() -# else: -# remove_state('regiond.running') -# host.service_stop('maas-regiond') -# hookenv.status_set('waiting', 'Waiting on relation to PostgreSQL') - - -# @when_any( -# 'config.changed.haproxy-stats-enabled', -# 'config.changed.haproxy-stats-uri', -# 'config.changed.haproxy-stats-auth') -# def configure_haproxy(): -# """Write /etc/haproxy/haproxy.conf and reload haproxy.""" -# ha_units = unit_db.get('ha_units') -# if ha_units is None: -# ha_units = {} -# templating.render('haproxy.cfg', '/etc/haproxy/haproxy.cfg', { -# 'stats_enabled': hookenv.config('haproxy-stats-enabled'), -# 'stats_uri': hookenv.config('haproxy-stats-uri'), -# 'stats_auth': hookenv.config('haproxy-stats-auth'), -# 'unit_name': hookenv.local_unit(), -# 'unit_address': hookenv.unit_private_ip(), -# 'ha_units': ha_units, -# }) - - -# @when('regiond.running') -# @when_any( -# 'config.changed.admin-username', -# 'config.changed.admin-email', -# 'config.changed.admin-password') -# def configure_admin_user(): -# """Configure administrator user.""" -# if hookenv.is_leader(): -# username = hookenv.config('admin-username') -# email = hookenv.config('admin-email') -# password = hookenv.config('admin-password') -# if username and email and password: -# admin_username = leadership.leader_get('admin_username') -# if admin_username: -# # Change the admin username, email, and password. -# hookenv.status_set('maintenance', 'Leader: updating admin') -# update_admin(admin_username, username, email, password) -# leadership.leader_set(admin_username=username) -# else: -# # Create the admin user. -# hookenv.status_set('maintenance', 'Leader: creating admin') -# check_call([ -# 'maas-region', 'createadmin', -# '--username', username, -# '--email', email, -# '--password', password, -# ]) -# leadership.leader_set(admin_username=username) - -# # Update the state. -# update_state() diff --git a/templates/regiond.conf.jinja2 b/templates/regiond.conf.jinja2 deleted file mode 100644 index f5ca8ed..0000000 --- a/templates/regiond.conf.jinja2 +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash -# /etc/maas/regiond.conf -database_host: localhost -database_name: maasdb -database_pass: SJoZjyTSWPFq -database_port: 5432 -database_user: maas -maas_url: http://10.0.10.11:5240/MAAS - -database_host: = ‘{{ pg_database.host() }}’ -database_name: = ‘{{ pg_database.database() }}’ -database_pass: = ‘{{ pg_database.password() }}’ -database_port: = ‘{{ pg_database.port() }}’ -database_user: = ‘{{ pg_database.user() }}’ -maas_url: = ‘{{ maas.url() }}’ \ No newline at end of file