Commit 7c6bb0278c37b97fe3d682d0a087877b2ede17d4
1 parent
dd2338ce
Exists in
master
and in
39 other branches
Fixed tab/spaces
Showing
1 changed file
with
50 additions
and
27 deletions
Show diff stats
fabfile.py
| ... | ... | @@ -12,19 +12,21 @@ from fabric.context_managers import prefix, cd, settings, shell_env |
| 12 | 12 | DEBIAN_FAMILY = ['debian', 'ubuntu'] |
| 13 | 13 | REDHAT_FAMILY = ['centos', 'fedora'] |
| 14 | 14 | |
| 15 | -DISTRO_CMD = {'debian': ('apt-get', { | |
| 16 | - 'install': '-y', | |
| 17 | - 'update': '-y', | |
| 18 | - }), | |
| 19 | - 'redhat': ('yum', { | |
| 20 | - 'install': '-y', | |
| 21 | - 'update': '-y', | |
| 22 | - }) | |
| 23 | - } | |
| 15 | +DISTRO_CMD = { | |
| 16 | + 'debian': ('apt-get', { | |
| 17 | + 'install': '-y', | |
| 18 | + 'update': '-y', | |
| 19 | + }), | |
| 20 | + 'redhat': ('yum', { | |
| 21 | + 'install': '-y', | |
| 22 | + 'update': '-y', | |
| 23 | + }) | |
| 24 | +} | |
| 24 | 25 | |
| 25 | 26 | APP_USER = APP_NAME = VENV_NAME = 'colab' |
| 26 | 27 | REPO_URL = 'git@github.com:colab-community/colab.git' |
| 27 | 28 | |
| 29 | + | |
| 28 | 30 | environments = { |
| 29 | 31 | 'dev': { |
| 30 | 32 | 'hosts': ['127.0.0.1'], |
| ... | ... | @@ -46,19 +48,23 @@ WORKON_ENV = '{} && workon {}'.format(SOURCE_VENV, VENV_NAME) |
| 46 | 48 | MANAGE_PATH = os.path.join(REPO_PATH, 'src') |
| 47 | 49 | SETTINGS_PATH = os.path.join(MANAGE_PATH, APP_NAME) |
| 48 | 50 | |
| 51 | + | |
| 49 | 52 | def get_distro_family(): |
| 50 | - linux_name = run('python -c "import platform; print platform.dist()[0]"').lower() | |
| 53 | + cmd = 'python -c "import platform; print platform.dist()[0]"' | |
| 54 | + linux_name = run(cmd).lower() | |
| 51 | 55 | if linux_name in DEBIAN_FAMILY: |
| 52 | - return 'debian' | |
| 56 | + return 'debian' | |
| 53 | 57 | elif linux_name in REDHAT_FAMILY: |
| 54 | - return 'redhat' | |
| 55 | - else : | |
| 56 | - error(colors.red('Distribuiton `{}` not supported.'.format(linux_name))) | |
| 57 | - exit(1) | |
| 58 | + return 'redhat' | |
| 59 | + else: | |
| 60 | + error(colors.red('Distribuiton `{}` not supported'.format(linux_name))) | |
| 61 | + exit(1) | |
| 62 | + | |
| 58 | 63 | |
| 59 | -def cmd(family, command, args = ''): | |
| 64 | +def cmd(family, command, args=''): | |
| 60 | 65 | pkgmanager, commands = DISTRO_CMD[family] |
| 61 | - return '{} {} {} {}'.format(pkgmanager, command, commands[command], args) | |
| 66 | + return ' '.join([pkgmanager, command, commands[command], args]) | |
| 67 | + | |
| 62 | 68 | |
| 63 | 69 | @task |
| 64 | 70 | def environment(name=DEFAULT_ENVIRONMENT): |
| ... | ... | @@ -79,9 +85,11 @@ def environment(name=DEFAULT_ENVIRONMENT): |
| 79 | 85 | env.update(environments[name]) |
| 80 | 86 | env.environment = name |
| 81 | 87 | |
| 82 | -def package_install(pkg): | |
| 88 | + | |
| 89 | +def package_install(pkg): | |
| 83 | 90 | family = get_distro_family() |
| 84 | - sudo(cmd(family, 'install', pkg )) | |
| 91 | + sudo(cmd(family, 'install', pkg)) | |
| 92 | + | |
| 85 | 93 | |
| 86 | 94 | def install_requirements(): |
| 87 | 95 | with cd(REPO_PATH), prefix(WORKON_ENV): |
| ... | ... | @@ -95,12 +103,14 @@ def install_requirements(): |
| 95 | 103 | else: |
| 96 | 104 | run('pip install -r requirements.txt') |
| 97 | 105 | |
| 106 | + | |
| 98 | 107 | def mkvirtualenv(): |
| 99 | 108 | if not exists('~/.virtualenvs/' + VENV_NAME): |
| 100 | 109 | with prefix(SOURCE_VENV): |
| 101 | 110 | run('mkvirtualenv ' + VENV_NAME) |
| 102 | 111 | return True |
| 103 | 112 | |
| 113 | + | |
| 104 | 114 | def manage(command): |
| 105 | 115 | django_settings = env.get('django_settings') |
| 106 | 116 | env_vars = {} |
| ... | ... | @@ -111,17 +121,21 @@ def manage(command): |
| 111 | 121 | with cd(MANAGE_PATH), prefix(WORKON_ENV): |
| 112 | 122 | run('python manage.py {}'.format(command)) |
| 113 | 123 | |
| 124 | + | |
| 114 | 125 | def syncdb(): |
| 115 | 126 | manage('syncdb') |
| 116 | 127 | |
| 128 | + | |
| 117 | 129 | def migrate(): |
| 118 | 130 | manage('migrate') |
| 119 | 131 | |
| 132 | + | |
| 120 | 133 | def collectstatic(): |
| 121 | 134 | sudo('mkdir -p /usr/share/nginx/{}'.format(APP_NAME)) |
| 122 | 135 | sudo('chown {} /usr/share/nginx/{}'.format(env.user, APP_NAME)) |
| 123 | 136 | manage('collectstatic --noinput') |
| 124 | 137 | |
| 138 | + | |
| 125 | 139 | def create_local_settings(): |
| 126 | 140 | with cd(SETTINGS_PATH), settings(user=env.superuser): |
| 127 | 141 | env_local_settings = 'local_settings-{}.py'.format(env.environment) |
| ... | ... | @@ -130,6 +144,7 @@ def create_local_settings(): |
| 130 | 144 | run('ln -s {} {}'.format(env_local_settings, 'local_settings.py')) |
| 131 | 145 | run('chown {} local_settings.py'.format(env.user)) |
| 132 | 146 | |
| 147 | + | |
| 133 | 148 | def update_code(): |
| 134 | 149 | if env.is_vagrant: |
| 135 | 150 | if not exists(REPO_PATH): |
| ... | ... | @@ -142,26 +157,29 @@ def update_code(): |
| 142 | 157 | with cd(REPO_PATH): |
| 143 | 158 | run('git pull') |
| 144 | 159 | |
| 160 | + | |
| 145 | 161 | @task |
| 146 | 162 | def bootstrap(): |
| 147 | 163 | """Bootstrap machine to run fabric tasks""" |
| 164 | + | |
| 148 | 165 | with settings(user=env.superuser): |
| 149 | - family = get_distro_family() | |
| 150 | - sudo(cmd(family, 'update')) | |
| 151 | - | |
| 152 | - if not exists('/usr/bin/git'): | |
| 166 | + family = get_distro_family() | |
| 167 | + sudo(cmd(family, 'update')) | |
| 168 | + | |
| 169 | + if not exists('/usr/bin/git'): | |
| 153 | 170 | package_install('git-core') |
| 154 | 171 | |
| 155 | 172 | if env.is_vagrant: |
| 156 | - groups = ['sudo' ,'vagrant'] | |
| 173 | + groups = ['sudo', 'vagrant'] | |
| 157 | 174 | local('chmod -fR g+w {}'.format(PROJECT_PATH)) |
| 158 | 175 | else: |
| 159 | 176 | groups = ['sudo'] |
| 160 | 177 | |
| 161 | - for group in groups: | |
| 162 | - sudo('groupadd -f {}'.format(group)) | |
| 178 | + for group in groups: | |
| 179 | + sudo('groupadd -f {}'.format(group)) | |
| 163 | 180 | |
| 164 | - sudo('useradd {} -G {} -m -s /bin/bash'.format(APP_USER, ','.join(groups))) | |
| 181 | + command = 'useradd {} -G {} -m -s /bin/bash' | |
| 182 | + sudo(command.format(APP_USER, ','.join(groups))) | |
| 165 | 183 | |
| 166 | 184 | ssh_dir = '/home/{0}/.ssh/'.format(APP_USER) |
| 167 | 185 | if not exists(ssh_dir): |
| ... | ... | @@ -181,11 +199,13 @@ def bootstrap(): |
| 181 | 199 | sudo('chmod 440 {}'.format(tmp_file)) |
| 182 | 200 | sudo('mv {} {}'.format(tmp_file, sudoers_file)) |
| 183 | 201 | |
| 202 | + | |
| 184 | 203 | @task |
| 185 | 204 | def provision(): |
| 186 | 205 | """Run puppet""" |
| 187 | 206 | |
| 188 | 207 | update_code() |
| 208 | + | |
| 189 | 209 | puppet_path = os.path.join(REPO_PATH, 'puppet/') |
| 190 | 210 | modules_path = os.path.join(puppet_path, 'modules') |
| 191 | 211 | puppet_modules = '{}:/etc/puppet/modules'.format(modules_path) |
| ... | ... | @@ -204,6 +224,7 @@ def provision(): |
| 204 | 224 | |
| 205 | 225 | sudo('puppet apply --modulepath={} {}'.format(puppet_modules, cmd)) |
| 206 | 226 | |
| 227 | + | |
| 207 | 228 | @task |
| 208 | 229 | def ssh_keygen(): |
| 209 | 230 | """Create SSH credentials""" |
| ... | ... | @@ -217,6 +238,7 @@ def ssh_keygen(): |
| 217 | 238 | print('') |
| 218 | 239 | print('Add the key above to your github repository deploy keys') |
| 219 | 240 | |
| 241 | + | |
| 220 | 242 | @task |
| 221 | 243 | def deploy(noprovision=False): |
| 222 | 244 | """Deploy and run the new code (master branch)""" |
| ... | ... | @@ -238,5 +260,6 @@ def deploy(noprovision=False): |
| 238 | 260 | |
| 239 | 261 | sudo('supervisorctl start all') |
| 240 | 262 | |
| 263 | + | |
| 241 | 264 | # Main |
| 242 | 265 | environment() | ... | ... |