-
-
Save jamesbeedy/4ce0224642ae11df473771b83c5e3506 to your computer and use it in GitHub Desktop.
feed.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from charms.reactive import when, when_not, set_state | |
from charmhelpers.core import host | |
from charmhelpers.core import unitdata | |
from charmhelpers.core.hookenv import (unit_get, | |
open_port, | |
log, | |
config, | |
status_set, | |
resource_get) | |
from charmhelpers.core.templating import render | |
from charms.layer.nginx_passenger import configure_site | |
from rubylib import bundle | |
from tlslib import client_key, client_cert | |
config = config() | |
kv = unitdata.kv() | |
RAILS_ENV = config['env'] | |
FEED_DIR = config['app-path'] | |
FEED_SHARED = os.path.join(FEED_DIR, 'shared') | |
FEED_SHARED_CONF = os.path.join(FEED_SHARED, 'config') | |
def _render_conf(cfg_path, cfg_tmpl, ctxt={}, perms=0o644): | |
''' Render configs | |
''' | |
if os.path.exists(cfg_path): | |
os.remove(cfg_path) | |
render(source=cfg_tmpl, | |
target=cfg_path, | |
owner='deploy', | |
perms=perms, | |
context=ctxt) | |
@when_not('feed.deploy.user.available') | |
def create_deploy_user(): | |
"""Create feed deploy user and dirs | |
""" | |
# Set status | |
status_set('maintenance', 'Creating deploy user for FEED') | |
# Create user | |
host.adduser('deploy', password=config['deploy-pass'], shell='/bin/bash', | |
system_user=False) | |
if not os.path.exists('/home/deploy/.ssh'): | |
os.makedirs('/home/deploy/.ssh', mode=0o755) | |
# Provision deploy key | |
_render_conf('/home/deploy/.ssh/kt-readonly', 'kt-readonly.tmpl', perms=0o600) | |
# Provision prm deploy key | |
_render_conf('/home/deploy/.ssh/prm_deploy_key', 'prm_deploy_key.tmpl', perms=0o600) | |
# Provision git ssh config | |
_render_conf('/home/deploy/.ssh/config', 'config.tmpl', perms=0o600) | |
# Authorized_keys | |
_render_conf('/home/deploy/.ssh/authorized_keys', 'authorized_keys.tmpl', perms=0o755) | |
# Known_hosts | |
_render_conf('/home/deploy/.ssh/known_hosts', 'known_hosts.tmpl', perms=0o644) | |
# Chownr deploy dirs | |
host.chownr('/home/deploy', | |
owner='deploy', | |
group='deploy') | |
# Set active status | |
status_set('active', 'Deploy user created for FEED') | |
# Set state | |
set_state('feed.deploy.user.available') | |
@when('ruby.available', 'feed.deploy.user.available') | |
@when_not('feed.installed') | |
def install_feed(): | |
'''Install FEED | |
''' | |
# Set status | |
status_set('maintenance', 'Installing FEED') | |
# Create dirs | |
if not os.path.exists(FEED_SHARED): | |
os.makedirs(FEED_SHARED) | |
for folder in ['config', 'public', 'tmp']: | |
sub_folder = os.path.exists(os.path.join(FEED_SHARED, folder)) | |
if not os.path.exists(sub_folder): | |
os.makedirs(sub_folder) | |
if not os.path.exists(os.path.join(FEED_SHARED, 'public', 'storage')): | |
os.makedirs(os.path.join(FEED_SHARED, 'public', 'storage')) | |
if not os.path.exists(os.path.join(FEED_SHARED, 'tmp', 'pids')): | |
os.makedirs(os.path.join(FEED_SHARED, 'tmp', 'pids')) | |
if not os.path.exists(os.path.join(FEED_SHARED, 'tmp', 'imports')): | |
os.makedirs(os.path.join(FEED_SHARED, 'tmp', 'imports')) | |
# Chownr app-dir | |
host.chownr(path=config['app-path'], | |
owner='deploy', | |
group='deploy') | |
# Set status | |
status_set('active', | |
'FEED installed') | |
# Set state | |
set_state('feed.installed') | |
@when('redis.available', 'feed.installed') | |
@when_not('feed.redis.available') | |
def get_set_redis_conn(redis): | |
"""Get set redis connection details | |
""" | |
status_set('maintenance', | |
'Setting Redis connection details.') | |
# Get redis connection details | |
db = redis.redis_data()[0] | |
REDIS_CONN = 'redis://{private_address}:{port}/12'.format(**db) | |
# Set REDIS_CONN in kv | |
kv.set('REDIS_CONN', REDIS_CONN) | |
status_set('active', | |
'Redis connection details saved.') | |
set_state('feed.redis.available') | |
@when('feed.installed', 'postgresql.connected') | |
@when_not('feed.postgresql.requested') | |
def request_postgresql_database(pgsql): | |
"""Request dbs for feed | |
""" | |
status_set('maintenance', | |
'Requesting PostgreSQL database for FEED.') | |
# Request database for FEED | |
pgsql.set_database("feed_%s" % RAILS_ENV) | |
pgsql.set_database("feed_report_%s" % RAILS_ENV) | |
# Set active status | |
status_set('active', | |
'PostgreSQL databases requested') | |
# Set state | |
set_state('feed.postgresql.requested') | |
@when('postgresql.master.available', | |
'feed.postgresql.requested') | |
@when_not('feed.postgresql.available') | |
def get_set_postgres_data(pgsql): | |
"""Get/set postgresql details | |
""" | |
status_set('maintenance', | |
'Getting/Setting PostgreSQL details for FEED.') | |
PG_DATA = {'user': pgsql.master.user, | |
'pass': pgsql.master.password, | |
'host': pgsql.master.host} | |
kv.set('PG_DATA', PG_DATA) | |
# Set active status | |
log(pgsql.master) | |
status_set('active', ('%s connected.' % pgsql.master)) | |
# Set state | |
set_state('feed.postgresql.available') | |
@when('feed.redis.available', 'feed.postgresql.available') | |
@when_not('feed.env.vars.available') | |
def write_app_yml(): | |
"""Write out application.yml | |
""" | |
status_set('maintenance', | |
'Writing application.yml') | |
ctxt = {'redis_conn': kv.get('REDIS_CONN'), | |
'pg_user': kv.get('PG_DATA')['user'], | |
'pg_pass': kv.get('PG_DATA')['pass'], | |
'pg_host': kv.get('PG_DATA')['host'], | |
'aws_access_key': config['aws-access-key'], | |
'aws_secret_key': config['aws-secret-key'], | |
'aws_bucket': config['aws-bucket'], | |
'aws_host_alias': config['aws-host-alias'], | |
'sendgrid_user': config['sendgrid-user'], | |
'sendgrid_password': config['sendgrid-password'], | |
'view_api_auth_email': config['view-api-auth-email'], | |
'view_api_auth_token': config['view-api-auth-token'], | |
'view_database_url': config['view-database-url'], | |
'rails_env': RAILS_ENV} | |
# Render application.yml | |
_render_conf(os.path.join(FEED_SHARED_CONF, 'application.yml'), | |
'application.yml.tmpl', ctxt=ctxt) | |
# Render feed_import_config.yml | |
_render_conf(os.path.join(FEED_SHARED_CONF, 'feed_import_config.yml'), | |
'feed_import_config.yml.tmpl') | |
set_state('feed.env.vars.available') | |
@when('feed.redis.available', 'feed.postgresql.available', | |
'feed.installed', 'nginx.passenger.available', | |
'feed.env.vars.available', 'tls.server.certificate available') | |
@when_not('feed.web.configured') | |
def configure_webserver(): | |
'''Configure nginx | |
''' | |
status_set('maintenance', 'Configuring website') | |
# Write out cert + key | |
server_key(None, '/etc/ssl/star.creativedrive.com.key', user='root', group='root') | |
server_cert(None, '/etc/ssl/star.creativedrive.com.crt', user='root', group='root') | |
# Write nginx conf | |
configure_site('feed', 'feed.conf.tmpl', rails_env=RAILS_ENV) | |
status_set('active', 'Website configured') | |
set_state('feed.web.configured') | |
@when('nginx.passenger.available', 'website.available') | |
@when_not('feed.web.available') | |
def configure_website(website): | |
'''Configure webserver - opens port 443 | |
''' | |
website.configure(port=443) | |
open_port(80) | |
set_state('feed.web.available') | |
@when('feed.web.available') | |
@when_not('feed.available') | |
def final_configure(): | |
'''Render config and start feed | |
''' | |
# Set status | |
status_set('maintenance', | |
'Final configuration of FEED.') | |
status_set('active', 'FEED webapp available!') | |
# Set state | |
set_state('feed.available') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment