diff --git a/fabfile.py b/fabfile.py index b0204df..aa95583 100644 --- a/fabfile.py +++ b/fabfile.py @@ -12,19 +12,21 @@ from fabric.context_managers import prefix, cd, settings, shell_env DEBIAN_FAMILY = ['debian', 'ubuntu'] REDHAT_FAMILY = ['centos', 'fedora'] -DISTRO_CMD = {'debian': ('apt-get', { - 'install': '-y', - 'update': '-y', - }), - 'redhat': ('yum', { - 'install': '-y', - 'update': '-y', - }) - } +DISTRO_CMD = { + 'debian': ('apt-get', { + 'install': '-y', + 'update': '-y', + }), + 'redhat': ('yum', { + 'install': '-y', + 'update': '-y', + }) +} APP_USER = APP_NAME = VENV_NAME = 'colab' REPO_URL = 'git@github.com:colab-community/colab.git' + environments = { 'dev': { 'hosts': ['127.0.0.1'], @@ -46,19 +48,23 @@ WORKON_ENV = '{} && workon {}'.format(SOURCE_VENV, VENV_NAME) MANAGE_PATH = os.path.join(REPO_PATH, 'src') SETTINGS_PATH = os.path.join(MANAGE_PATH, APP_NAME) + def get_distro_family(): - linux_name = run('python -c "import platform; print platform.dist()[0]"').lower() + cmd = 'python -c "import platform; print platform.dist()[0]"' + linux_name = run(cmd).lower() if linux_name in DEBIAN_FAMILY: - return 'debian' + return 'debian' elif linux_name in REDHAT_FAMILY: - return 'redhat' - else : - error(colors.red('Distribuiton `{}` not supported.'.format(linux_name))) - exit(1) + return 'redhat' + else: + error(colors.red('Distribuiton `{}` not supported'.format(linux_name))) + exit(1) + -def cmd(family, command, args = ''): +def cmd(family, command, args=''): pkgmanager, commands = DISTRO_CMD[family] - return '{} {} {} {}'.format(pkgmanager, command, commands[command], args) + return ' '.join([pkgmanager, command, commands[command], args]) + @task def environment(name=DEFAULT_ENVIRONMENT): @@ -79,9 +85,11 @@ def environment(name=DEFAULT_ENVIRONMENT): env.update(environments[name]) env.environment = name -def package_install(pkg): + +def package_install(pkg): family = get_distro_family() - sudo(cmd(family, 'install', pkg )) + sudo(cmd(family, 'install', pkg)) + def install_requirements(): with cd(REPO_PATH), prefix(WORKON_ENV): @@ -95,12 +103,14 @@ def install_requirements(): else: run('pip install -r requirements.txt') + def mkvirtualenv(): if not exists('~/.virtualenvs/' + VENV_NAME): with prefix(SOURCE_VENV): run('mkvirtualenv ' + VENV_NAME) return True + def manage(command): django_settings = env.get('django_settings') env_vars = {} @@ -111,17 +121,21 @@ def manage(command): with cd(MANAGE_PATH), prefix(WORKON_ENV): run('python manage.py {}'.format(command)) + def syncdb(): manage('syncdb') + def migrate(): manage('migrate') + def collectstatic(): sudo('mkdir -p /usr/share/nginx/{}'.format(APP_NAME)) sudo('chown {} /usr/share/nginx/{}'.format(env.user, APP_NAME)) manage('collectstatic --noinput') + def create_local_settings(): with cd(SETTINGS_PATH), settings(user=env.superuser): env_local_settings = 'local_settings-{}.py'.format(env.environment) @@ -130,6 +144,7 @@ def create_local_settings(): run('ln -s {} {}'.format(env_local_settings, 'local_settings.py')) run('chown {} local_settings.py'.format(env.user)) + def update_code(): if env.is_vagrant: if not exists(REPO_PATH): @@ -142,26 +157,29 @@ def update_code(): with cd(REPO_PATH): run('git pull') + @task def bootstrap(): """Bootstrap machine to run fabric tasks""" + with settings(user=env.superuser): - family = get_distro_family() - sudo(cmd(family, 'update')) - - if not exists('/usr/bin/git'): + family = get_distro_family() + sudo(cmd(family, 'update')) + + if not exists('/usr/bin/git'): package_install('git-core') if env.is_vagrant: - groups = ['sudo' ,'vagrant'] + groups = ['sudo', 'vagrant'] local('chmod -fR g+w {}'.format(PROJECT_PATH)) else: groups = ['sudo'] - for group in groups: - sudo('groupadd -f {}'.format(group)) + for group in groups: + sudo('groupadd -f {}'.format(group)) - sudo('useradd {} -G {} -m -s /bin/bash'.format(APP_USER, ','.join(groups))) + command = 'useradd {} -G {} -m -s /bin/bash' + sudo(command.format(APP_USER, ','.join(groups))) ssh_dir = '/home/{0}/.ssh/'.format(APP_USER) if not exists(ssh_dir): @@ -181,11 +199,13 @@ def bootstrap(): sudo('chmod 440 {}'.format(tmp_file)) sudo('mv {} {}'.format(tmp_file, sudoers_file)) + @task def provision(): """Run puppet""" update_code() + puppet_path = os.path.join(REPO_PATH, 'puppet/') modules_path = os.path.join(puppet_path, 'modules') puppet_modules = '{}:/etc/puppet/modules'.format(modules_path) @@ -204,6 +224,7 @@ def provision(): sudo('puppet apply --modulepath={} {}'.format(puppet_modules, cmd)) + @task def ssh_keygen(): """Create SSH credentials""" @@ -217,6 +238,7 @@ def ssh_keygen(): print('') print('Add the key above to your github repository deploy keys') + @task def deploy(noprovision=False): """Deploy and run the new code (master branch)""" @@ -238,5 +260,6 @@ def deploy(noprovision=False): sudo('supervisorctl start all') + # Main environment() -- libgit2 0.21.2