Skip to content

Instantly share code, notes, and snippets.

@chadgh
Created October 7, 2015 22:01
Show Gist options
  • Save chadgh/1ec2811b831e91f7ea39 to your computer and use it in GitHub Desktop.
Save chadgh/1ec2811b831e91f7ea39 to your computer and use it in GitHub Desktop.
from os import environ
from fabric.api import run, env
import requests
env.hosts.append('mydomain.com')
LINODE_API_KEY = 'mylinodeapikey' # noqa
DOMAIN_ID = 'mylinodedomainid' # for mydomain.com
IPv4 = 'mylinodeipv4address'
IPv6 = 'mylinodeipv6address'
IN_ENV_CMD = 'cd {path} && source env/bin/activate && {cmd}'
MANAGEMENT_COMMAND = './manage.py {cmd}'
SCTL_ALIAS = 'sctl'
SCTL_CMD = '{sctl} {cmd} {project}'
env.project_path = environ.get('FABRIC_PROJECT_PATH')
env.project_name = environ.get('FABRIC_PROJECT_NAME')
env.git_repo = environ.get('FABRIC_GIT_REPO')
def host_type():
run('uname -a')
def do(command):
run(IN_ENV_CMD.format(path=env.project_path, cmd=command))
def manage(command):
management_command = MANAGEMENT_COMMAND.format(cmd=command)
run(IN_ENV_CMD.format(path=env.project_path, cmd=management_command))
def create_new_subdomain(subdomain):
data = {
'domainid': DOMAIN_ID,
'type': 'AAAA',
'name': subdomain,
}
base_url = 'https://api.linode.com/?api_key={}&api_action=domain.resource.create&{}' # noqa
if IPv4:
data['target'] = IPv4
url = base_url.format(LINODE_API_KEY, '&'.join(['{}={}'.format(k, v) for k, v in data.items()])) # noqa
r = requests.get(url).json()
if not r['ERRORARRAY']:
print('Created {} subdomain with IPv4.'.format(subdomain))
if IPv6:
data['target'] = IPv6
url = base_url.format(LINODE_API_KEY, '&'.join(['{}={}'.format(k, v) for k, v in data.items()])) # noqa
r = requests.get(url).json()
if not r['ERRORARRAY']:
print('Created {} subdomain with IPv6.'.format(subdomain))
def deploy(rev=''):
# 1. update code
git_command = 'git pull'
if rev:
git_command += ' && git checkout {} && git pull'.format(rev)
do(git_command)
# 2. update database and static files
manage('migrate --noinput')
manage('collectstatic --noinput')
# 3. restart server
sctl('restart')
def deploy_new(rev=''):
# 1. create domain
pass
def restart():
run(SCTL_CMD.format(sctl=SCTL_ALIAS, cmd='restart', project=env.project_name)) # noqa
def sctl(command, raw=False):
if raw:
run('{sctl} {cmd}'.format(sctl=SCTL_ALIAS, cmd=command))
else:
run(SCTL_CMD.format(sctl=SCTL_ALIAS, cmd=command, project=env.project_name)) # noqa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment