Commit e84c772506fdfd5ae2f5df9cbfe7d7e86719933d

Authored by Charles Oliveira
1 parent 2470bf96

Finished support for fabric on both centos and ubuntu

Showing 1 changed file with 35 additions and 36 deletions   Show diff stats
fabfile.py
... ... @@ -9,15 +9,18 @@ from fabric.api import env, run, sudo, local
9 9 from fabric.contrib.files import exists
10 10 from fabric.context_managers import prefix, cd, settings, shell_env
11 11  
12   -LINUX_DISTRO = 'Debian'
13   -DISTRO_CMD = {'Debian': {
14   - 'install': 'apt-get install',
15   - 'update': 'apt-get update',
16   - },
17   - 'CentOS': {
18   - 'install': 'yum install',
19   - 'install': 'yum update -y',
20   - }
  12 +LINUX_DISTRO = ''
  13 +DEBIAN_FAMILY = ['debian', 'ubuntu']
  14 +REDHAT_FAMILY = ['centos', 'fedora']
  15 +
  16 +DISTRO_CMD = {'debian': ('apt-get', {
  17 + 'install': '-y',
  18 + 'update': '-y',
  19 + }),
  20 + 'redhat': ('yum', {
  21 + 'install': '-y',
  22 + 'update': '-y',
  23 + })
21 24 }
22 25  
23 26 APP_USER = APP_NAME = VENV_NAME = 'colab'
... ... @@ -44,15 +47,19 @@ WORKON_ENV = '{} && workon {}'.format(SOURCE_VENV, VENV_NAME)
44 47 MANAGE_PATH = os.path.join(REPO_PATH, 'src')
45 48 SETTINGS_PATH = os.path.join(MANAGE_PATH, APP_NAME)
46 49  
  50 +def get_distro_family():
  51 + linux_name = run('python -c "import platform; print platform.dist()[0]"').lower()
  52 + if linux_name in DEBIAN_FAMILY:
  53 + return 'debian'
  54 + elif linux_name in REDHAT_FAMILY:
  55 + return 'redhat'
  56 + else :
  57 + error(colors.red('Distribuiton `{}` not supported.'.format(linux_name)))
  58 + exit(1)
47 59  
48   -def get_distro():
49   - global LINUX_DISTRO
50   - linux_name = run('python -c "import platform; print platform.dist()[0]"')
51   -
52   - if 'Ubuntu' in linux_name or 'Debian' in linux_name:
53   - LINUX_DISTRO = 'Debian'
54   - elif 'centos' in linux_name:
55   - LINUX_DISTRO = 'CentOS'
  60 +def cmd(family, command, args = ''):
  61 + pkgmanager, commands = DISTRO_CMD[family]
  62 + return '{} {} {} {}'.format(pkgmanager, command, commands[command], args)
56 63  
57 64 @task
58 65 def environment(name=DEFAULT_ENVIRONMENT):
... ... @@ -74,7 +81,8 @@ def environment(name=DEFAULT_ENVIRONMENT):
74 81 env.environment = name
75 82  
76 83 def package_install(pkg):
77   - sudo(DISTRO_CMD[LINUX_DISTRO]['install'] + ' ' + pkg + ' -y -q')
  84 + family = get_distro_family()
  85 + sudo(cmd(family, 'install', pkg ))
78 86  
79 87 def install_requirements():
80 88 with cd(REPO_PATH), prefix(WORKON_ENV):
... ... @@ -88,14 +96,12 @@ def install_requirements():
88 96 else:
89 97 run('pip install -r requirements.txt')
90 98  
91   -
92 99 def mkvirtualenv():
93 100 if not exists('~/.virtualenvs/' + VENV_NAME):
94 101 with prefix(SOURCE_VENV):
95 102 run('mkvirtualenv ' + VENV_NAME)
96 103 return True
97 104  
98   -
99 105 def manage(command):
100 106 django_settings = env.get('django_settings')
101 107 env_vars = {}
... ... @@ -106,21 +112,17 @@ def manage(command):
106 112 with cd(MANAGE_PATH), prefix(WORKON_ENV):
107 113 run('python manage.py {}'.format(command))
108 114  
109   -
110 115 def syncdb():
111 116 manage('syncdb')
112 117  
113   -
114 118 def migrate():
115 119 manage('migrate')
116 120  
117   -
118 121 def collectstatic():
119 122 sudo('mkdir -p /usr/share/nginx/{}'.format(APP_NAME))
120 123 sudo('chown {} /usr/share/nginx/{}'.format(env.user, APP_NAME))
121 124 manage('collectstatic --noinput')
122 125  
123   -
124 126 def create_local_settings():
125 127 with cd(SETTINGS_PATH), settings(user=env.superuser):
126 128 env_local_settings = 'local_settings-{}.py'.format(env.environment)
... ... @@ -129,7 +131,6 @@ def create_local_settings():
129 131 run('ln -s {} {}'.format(env_local_settings, 'local_settings.py'))
130 132 run('chown {} local_settings.py'.format(env.user))
131 133  
132   -
133 134 def update_code():
134 135 if env.is_vagrant:
135 136 if not exists(REPO_PATH):
... ... @@ -142,25 +143,27 @@ def update_code():
142 143 with cd(REPO_PATH):
143 144 run('git pull')
144 145  
145   -
146 146 @task
147 147 def bootstrap():
148 148 """Bootstrap machine to run fabric tasks"""
149 149 with settings(user=env.superuser):
150   -
151   - sudo(DISTRO_CMD[LINUX_DISTRO]['update'])
  150 + family = get_distro_family()
  151 + sudo(cmd(family, 'update'))
152 152  
153 153 if not exists('/usr/bin/git'):
154 154 package_install('git-core')
155 155  
156 156 if env.is_vagrant:
157   - groups = 'sudo,vagrant'
  157 + groups = ['sudo' ,'vagrant']
158 158 local('chmod -fR g+w {}'.format(PROJECT_PATH))
159 159 else:
160   - groups = 'sudo'
  160 + groups = ['sudo']
  161 +
  162 + for group in groups:
  163 + sudo('groupadd -f {}'.format(group))
  164 +
  165 + sudo('useradd {} -G {} -m -s /bin/bash'.format(APP_USER, ','.join(groups)))
161 166  
162   - sudo('useradd {} -G {} -m -s /bin/bash'.format(APP_USER, groups),
163   - quiet=True)
164 167 ssh_dir = '/home/{0}/.ssh/'.format(APP_USER)
165 168 if not exists(ssh_dir):
166 169 sudo('mkdir -p {0}'.format(ssh_dir))
... ... @@ -179,13 +182,11 @@ def bootstrap():
179 182 sudo('chmod 440 {}'.format(tmp_file))
180 183 sudo('mv {} {}'.format(tmp_file, sudoers_file))
181 184  
182   -
183 185 @task
184 186 def provision():
185 187 """Run puppet"""
186 188  
187 189 update_code()
188   -
189 190 puppet_path = os.path.join(REPO_PATH, 'puppet/')
190 191 modules_path = os.path.join(puppet_path, 'modules')
191 192 puppet_modules = '{}:/etc/puppet/modules'.format(modules_path)
... ... @@ -204,7 +205,6 @@ def provision():
204 205  
205 206 sudo('puppet apply --modulepath={} {}'.format(puppet_modules, cmd))
206 207  
207   -
208 208 @task
209 209 def ssh_keygen():
210 210 """Create SSH credentials"""
... ... @@ -218,7 +218,6 @@ def ssh_keygen():
218 218 print('')
219 219 print('Add the key above to your github repository deploy keys')
220 220  
221   -
222 221 @task
223 222 def deploy(noprovision=False):
224 223 """Deploy and run the new code (master branch)"""
... ...