| import os | |
| from subprocess import call | |
| from charms.reactive import ( | |
| when, | |
| when_any, | |
| when_not, | |
| set_state, | |
| remove_state | |
| ) | |
| from charmhelpers.core.host import chownr, chdir | |
| from charmhelpers.core import unitdata | |
| from charmhelpers.core.hookenv import ( | |
| status_set, | |
| config, | |
| log | |
| ) | |
| from charms.layer.prm_base import ( | |
| PRM_DIR, | |
| PRM_CURRENT, | |
| RAILS_ENV, | |
| SU_CONF_DIR, | |
| PRM_STATIC_ASSETS, | |
| SHARED_STATIC_ASSETS, | |
| start_restart, | |
| render_application_yml | |
| ) | |
| kv = unitdata.kv() | |
| @when_not('manual.db.check') | |
| def check_user_provided_database(): | |
| if not config('db-uri'): | |
| remove_state('prm.manual.database.available') | |
| log("Manual database not configured") | |
| else: | |
| db_configs = {'uri': config('db-uri'), | |
| 'vars': "?encoding=unicode&pool=5"} | |
| pg_conn = ("{uri}{vars}").format(**db_configs) | |
| kv.set('prmdb', pg_conn) | |
| set_state('prm.manual.database.available') | |
| remove_state('prm.env.vars.available') | |
| set_state('manual.db.check') | |
| @when_not('prm.postgresql.available', 'prm.manual.database.available') | |
| def set_blocked_on_db(): | |
| status_set('blocked', "Need database info by config or relation") | |
| @when_not('prm.conf.dir.created') | |
| def create_prm_conf_dir(): | |
| """Ensure PRM config dir exists | |
| """ | |
| if not os.path.isdir(SHARED_STATIC_ASSETS): | |
| os.makedirs(SHARED_STATIC_ASSETS, mode=0o644, exist_ok=True) | |
| chownr(SU_CONF_DIR, owner='www-data', group='www-data') | |
| set_state('prm.conf.dir.created') | |
| @when('pgsql.connected') | |
| @when_not('prmdb.requested') | |
| def request_prm_database(pgsql): | |
| """Request prm db | |
| """ | |
| status_set('maintenance', 'Requesting PostgreSQL database for PRM.') | |
| # Request database for PRM | |
| pgsql.set_database("prm_%s" % RAILS_ENV) | |
| pgsql.set_roles('postgres') | |
| # Set active status | |
| status_set('active', 'PostgreSQL databases requested') | |
| # Set state | |
| set_state('prmdb.requested') | |
| @when('pgsql.master.available', 'prmdb.requested') | |
| @when_not('prm.postgresql.available') | |
| def get_set_prmdb_data(pgsql): | |
| """Get/set prmdb details | |
| """ | |
| # Set maintenance status | |
| status_set('maintenance', | |
| 'Getting/Setting details for PRM db.') | |
| # Get/set postgres master uri | |
| uri_vars = {'uri': pgsql.master.uri, | |
| 'vars': "?encoding=unicode&pool=5"} | |
| pg_conn = "{uri}{vars}".format(**uri_vars) | |
| # Set pg conn info to unitdata | |
| kv.set('prmdb', pg_conn) | |
| # Set status | |
| status_set('active', 'Database: prm_%s available' % RAILS_ENV) | |
| # Set state | |
| set_state('prm.postgresql.available') | |
| @when('redis.available') | |
| @when_not('prm.redis.available') | |
| def get_set_redis_conn(redis): | |
| """Get set redis connection details | |
| """ | |
| status_set('maintenance', 'Setting Redis connection details.') | |
| db = redis.redis_data() | |
| db['redis_db'] = config('redis-db') | |
| kv.set('redis_conn', '{uri}/{redis_db}'.format(**db)) | |
| status_set('active', 'Redis connection details saved.') | |
| set_state('prm.redis.available') | |
| @when('codebase.available', 'prm.conf.dir.created') | |
| @when_not('prm.assets.dir.available') | |
| def symlink_static_assets(): | |
| """Symlink static assets | |
| """ | |
| if os.path.exists(PRM_STATIC_ASSETS): | |
| os.remove(PRM_STATIC_ASSETS) | |
| os.symlink(SHARED_STATIC_ASSETS, PRM_STATIC_ASSETS) | |
| chownr(path=os.path.dirname(os.path.normpath(PRM_DIR)), | |
| owner='www-data', group='www-data') | |
| set_state('prm.assets.dir.available') | |
| @when('prm.redis.available', 'prm.assets.dir.available') | |
| @when_any('prm.postgresql.available', 'prm.manual.database.available') | |
| @when_not('prm.env.vars.available') | |
| def write_app_yml(): | |
| """Write out application.yml | |
| """ | |
| status_set('maintenance', 'Writing application.yml') | |
| render_application_yml() | |
| status_set('active', 'Application.yml written') | |
| set_state('prm.env.vars.available') | |
| @when_not('prm.ready') | |
| @when('prm.env.vars.available', 'codebase.available') | |
| def install_prm_deps(): | |
| status_set('maintenance', 'Installing PRM deps') | |
| with chdir(PRM_CURRENT): | |
| call("bundle install".split()) | |
| status_set('active', 'PRM deps installed') | |
| set_state('prm.ready') | |
| @when('prm.ready') | |
| @when_not('prm.base.available') | |
| def set_base_avail(): | |
| """Set prm.base.available | |
| """ | |
| status_set('active', 'PRM base-layer available') | |
| set_state('prm.base.available') | |
| @when('config.changed.secrets') | |
| def rerender_application_yml(): | |
| remove_state('prm.env.vars.available') | |
| @when('config.changed.db-uri') | |
| def db_config_changed(): | |
| remove_state('manual.db.check') | |
| @when_not('pgsql.connected') | |
| def remove_database_available(): | |
| remove_state('prm.postgresql.available') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment