Skip to content

Instantly share code, notes, and snippets.

@silenius
Created August 3, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silenius/169321897f64363fd27a to your computer and use it in GitHub Desktop.
Save silenius/169321897f64363fd27a to your computer and use it in GitHub Desktop.
import binascii
import os
import os.path
import salt
from bbpf_common import _dict
def venv(name):
cfg = _dict(__salt__['pillar.get']('venvs:webapps:{}'.format(name)))
lookup = _dict(__salt__['pillar.get']('venvs:lookup'))
cfg.update({
'name' : name,
'full_path' : os.path.join(lookup.base, name),
})
cfg['base_path'] = os.path.dirname(cfg.full_path)
_python = cfg.get('python_version')
_ruby = cfg.get('ruby_version')
if _python:
lang = 'python'
cfg['py3k'] = str(_python).startswith('3')
cfg['activate'] = os.path.join(cfg.full_path, 'bin', 'activate')
elif _ruby:
lang = 'ruby'
cfg['is18'] = str(_ruby).startswith('1.8')
else:
lang = None
cfg['lang'] = lang
return cfg
def repository(name):
cfg = _dict(__salt__['pillar.get']('repos:webapps:items:{}'.format(name)))
lookup = _dict(__salt__['pillar.get']('repos:webapps:lookup'))
cfg['name'] = name
clone_dir = cfg.get('dst', name)
if '..' in clone_dir or clone_dir.startswith('/'):
raise ValueError('Invalid clone directory')
cfg['clone_dir'] = os.path.join(lookup.dst, clone_dir.strip('/'))
use_repo = cfg.get('src', lookup.src)
repo_lookup = __salt__['pillar.get']('repos:lookup:{}'.format(use_repo))
#cfg['repo_cfg'] = repo_lookup
cfg['repo_user'] = repo_lookup['user']
cfg['repo_password'] = repo_lookup['password']
cfg['repo_url'] = '{}/{}'.format(repo_lookup['url'], cfg.get('repo', name))
return cfg
def pgsql(db):
role = _dict(__salt__['pillar.get']('postgresql:roles:{}'.format(db.role)))
use = _dict(__salt__['pillar.get']('postgresql:databases:{}'.format(db.use)))
use['name'] = db.use
return _dict({
'username': db.role,
'password': role.password,
# FIXME
'host': 'db',
'port': 5432,
'db' : use
})
def merge_unicorn_config(webapp_cfg):
return _dict({
'pid' : os.path.join(webapp_cfg.repo.clone_dir, 'unicorn.pid'),
'worker_processes' : webapp_cfg.deploy.get('proc', 2),
'stdout_path' : os.path.join(webapp_cfg.repo.clone_dir, 'log',
'stdout.log'),
'stderr_path' : os.path.join(webapp_cfg.repo.clone_dir, 'log',
'stderr.log'),
'_fullpath' : os.path.join(webapp_cfg.repo.clone_dir, 'config',
'unicorn.rb'),
'listen' : '127.0.0.1:{}'.format(webapp_cfg.deploy['port'])
})
def merge_paster_config(webapp_cfg):
cfg = _dict({
'_fullpath' : os.path.join(webapp_cfg.repo.clone_dir, 'config.ini'),
'random_secret' : binascii.hexlify(os.urandom(32)),
})
if 'log_level' not in webapp_cfg.paster_config:
cfg.paster_config['log_level'] = {
'development' : 'DEBUG',
'acceptance' : 'INFO',
'production' : 'WARN',
}.get(cfg.deploy.env, 'INFO')
if 'pgsql' in webapp_cfg:
dsn = 'postgresql+psycopg2://{username}:{password}@{host}:{port}/{database}'.format(**webapp_cfg['pgsql'])
cfg['dsn'] = dsn
return cfg
def merge_supervise_config(webapp_cfg):
return _dict({
'_fullpath' : os.path.join('/var/service', webapp_cfg.name, 'run'),
})
def merge_rails_config(webapp_cfg):
return _dict({
'_db_fullpath' : os.path.join(webapp_cfg.repo.clone_dir, 'config',
'database.yml'),
'_cfgenv_fullpath' : os.path.join(webapp_cfg.repo.clone_dir, 'config',
'environments',
'.'.join((webapp_cfg.deploy['env'],
'rb'))),
})
def config(name):
cfg = _dict(__salt__['pillar.get']('webapps:items:{}'.format(name)))
lookup = _dict(__salt__['pillar.get']('webapps:lookup'))
cfg.deploy.update(lookup.deploy)
cfg.update({
'name' : name,
'venv' : venv(cfg.venv),
'repo' : repository(cfg.repo),
})
for k in cfg.use:
# Default values
use_cfg = _dict(__salt__['pillar.get']('webapps:lookup:{}'.format(k)))
# App specific values for k
use_cfg.update(cfg.get(k, {}))
cfg[k] = use_cfg
if 'pgsql' in cfg:
cfg['pgsql'].update(pgsql(_dict(cfg.pgsql)))
if 'unicorn_config' in cfg.use:
cfg['unicorn_config'].update(merge_unicorn_config(cfg))
elif 'paster_config' in cfg.use:
cfg['paster_config'].update(merge_paster_config(cfg))
if 'supervise' in cfg.use:
cfg['supervise'].update(merge_supervise_config(cfg))
if 'rails' in cfg.use:
cfg['rails'].update(merge_rails_config(cfg))
return cfg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment