From e84c772506fdfd5ae2f5df9cbfe7d7e86719933d Mon Sep 17 00:00:00 2001 From: Charles Oliveira <18oliveira.charles@gmail.com> Date: Wed, 30 Jul 2014 12:28:28 -0300 Subject: [PATCH] Finished support for fabric on both centos and ubuntu --- fabfile.py | 71 +++++++++++++++++++++++++++++++++++------------------------------------ 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/fabfile.py b/fabfile.py index b9b2581..e33a968 100644 --- a/fabfile.py +++ b/fabfile.py @@ -9,15 +9,18 @@ from fabric.api import env, run, sudo, local from fabric.contrib.files import exists from fabric.context_managers import prefix, cd, settings, shell_env -LINUX_DISTRO = 'Debian' -DISTRO_CMD = {'Debian': { - 'install': 'apt-get install', - 'update': 'apt-get update', - }, - 'CentOS': { - 'install': 'yum install', - 'install': 'yum update -y', - } +LINUX_DISTRO = '' +DEBIAN_FAMILY = ['debian', 'ubuntu'] +REDHAT_FAMILY = ['centos', 'fedora'] + +DISTRO_CMD = {'debian': ('apt-get', { + 'install': '-y', + 'update': '-y', + }), + 'redhat': ('yum', { + 'install': '-y', + 'update': '-y', + }) } APP_USER = APP_NAME = VENV_NAME = 'colab' @@ -44,15 +47,19 @@ 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() + if linux_name in DEBIAN_FAMILY: + return 'debian' + elif linux_name in REDHAT_FAMILY: + return 'redhat' + else : + error(colors.red('Distribuiton `{}` not supported.'.format(linux_name))) + exit(1) -def get_distro(): - global LINUX_DISTRO - linux_name = run('python -c "import platform; print platform.dist()[0]"') - - if 'Ubuntu' in linux_name or 'Debian' in linux_name: - LINUX_DISTRO = 'Debian' - elif 'centos' in linux_name: - LINUX_DISTRO = 'CentOS' +def cmd(family, command, args = ''): + pkgmanager, commands = DISTRO_CMD[family] + return '{} {} {} {}'.format(pkgmanager, command, commands[command], args) @task def environment(name=DEFAULT_ENVIRONMENT): @@ -74,7 +81,8 @@ def environment(name=DEFAULT_ENVIRONMENT): env.environment = name def package_install(pkg): - sudo(DISTRO_CMD[LINUX_DISTRO]['install'] + ' ' + pkg + ' -y -q') + family = get_distro_family() + sudo(cmd(family, 'install', pkg )) def install_requirements(): with cd(REPO_PATH), prefix(WORKON_ENV): @@ -88,14 +96,12 @@ 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 = {} @@ -106,21 +112,17 @@ 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) @@ -129,7 +131,6 @@ 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,25 +143,27 @@ def update_code(): with cd(REPO_PATH): run('git pull') - @task def bootstrap(): """Bootstrap machine to run fabric tasks""" with settings(user=env.superuser): - - sudo(DISTRO_CMD[LINUX_DISTRO]['update']) + 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' + groups = ['sudo'] + + for group in groups: + sudo('groupadd -f {}'.format(group)) + + sudo('useradd {} -G {} -m -s /bin/bash'.format(APP_USER, ','.join(groups))) - sudo('useradd {} -G {} -m -s /bin/bash'.format(APP_USER, groups), - quiet=True) ssh_dir = '/home/{0}/.ssh/'.format(APP_USER) if not exists(ssh_dir): sudo('mkdir -p {0}'.format(ssh_dir)) @@ -179,13 +182,11 @@ 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,7 +205,6 @@ def provision(): sudo('puppet apply --modulepath={} {}'.format(puppet_modules, cmd)) - @task def ssh_keygen(): """Create SSH credentials""" @@ -218,7 +218,6 @@ 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)""" -- libgit2 0.21.2