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
@@ -9,15 +9,18 @@ from fabric.api import env, run, sudo, local @@ -9,15 +9,18 @@ from fabric.api import env, run, sudo, local
9 from fabric.contrib.files import exists 9 from fabric.contrib.files import exists
10 from fabric.context_managers import prefix, cd, settings, shell_env 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 APP_USER = APP_NAME = VENV_NAME = 'colab' 26 APP_USER = APP_NAME = VENV_NAME = 'colab'
@@ -44,15 +47,19 @@ WORKON_ENV = '{} && workon {}'.format(SOURCE_VENV, VENV_NAME) @@ -44,15 +47,19 @@ WORKON_ENV = '{} && workon {}'.format(SOURCE_VENV, VENV_NAME)
44 MANAGE_PATH = os.path.join(REPO_PATH, 'src') 47 MANAGE_PATH = os.path.join(REPO_PATH, 'src')
45 SETTINGS_PATH = os.path.join(MANAGE_PATH, APP_NAME) 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 @task 64 @task
58 def environment(name=DEFAULT_ENVIRONMENT): 65 def environment(name=DEFAULT_ENVIRONMENT):
@@ -74,7 +81,8 @@ def environment(name=DEFAULT_ENVIRONMENT): @@ -74,7 +81,8 @@ def environment(name=DEFAULT_ENVIRONMENT):
74 env.environment = name 81 env.environment = name
75 82
76 def package_install(pkg): 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 def install_requirements(): 87 def install_requirements():
80 with cd(REPO_PATH), prefix(WORKON_ENV): 88 with cd(REPO_PATH), prefix(WORKON_ENV):
@@ -88,14 +96,12 @@ def install_requirements(): @@ -88,14 +96,12 @@ def install_requirements():
88 else: 96 else:
89 run('pip install -r requirements.txt') 97 run('pip install -r requirements.txt')
90 98
91 -  
92 def mkvirtualenv(): 99 def mkvirtualenv():
93 if not exists('~/.virtualenvs/' + VENV_NAME): 100 if not exists('~/.virtualenvs/' + VENV_NAME):
94 with prefix(SOURCE_VENV): 101 with prefix(SOURCE_VENV):
95 run('mkvirtualenv ' + VENV_NAME) 102 run('mkvirtualenv ' + VENV_NAME)
96 return True 103 return True
97 104
98 -  
99 def manage(command): 105 def manage(command):
100 django_settings = env.get('django_settings') 106 django_settings = env.get('django_settings')
101 env_vars = {} 107 env_vars = {}
@@ -106,21 +112,17 @@ def manage(command): @@ -106,21 +112,17 @@ def manage(command):
106 with cd(MANAGE_PATH), prefix(WORKON_ENV): 112 with cd(MANAGE_PATH), prefix(WORKON_ENV):
107 run('python manage.py {}'.format(command)) 113 run('python manage.py {}'.format(command))
108 114
109 -  
110 def syncdb(): 115 def syncdb():
111 manage('syncdb') 116 manage('syncdb')
112 117
113 -  
114 def migrate(): 118 def migrate():
115 manage('migrate') 119 manage('migrate')
116 120
117 -  
118 def collectstatic(): 121 def collectstatic():
119 sudo('mkdir -p /usr/share/nginx/{}'.format(APP_NAME)) 122 sudo('mkdir -p /usr/share/nginx/{}'.format(APP_NAME))
120 sudo('chown {} /usr/share/nginx/{}'.format(env.user, APP_NAME)) 123 sudo('chown {} /usr/share/nginx/{}'.format(env.user, APP_NAME))
121 manage('collectstatic --noinput') 124 manage('collectstatic --noinput')
122 125
123 -  
124 def create_local_settings(): 126 def create_local_settings():
125 with cd(SETTINGS_PATH), settings(user=env.superuser): 127 with cd(SETTINGS_PATH), settings(user=env.superuser):
126 env_local_settings = 'local_settings-{}.py'.format(env.environment) 128 env_local_settings = 'local_settings-{}.py'.format(env.environment)
@@ -129,7 +131,6 @@ def create_local_settings(): @@ -129,7 +131,6 @@ def create_local_settings():
129 run('ln -s {} {}'.format(env_local_settings, 'local_settings.py')) 131 run('ln -s {} {}'.format(env_local_settings, 'local_settings.py'))
130 run('chown {} local_settings.py'.format(env.user)) 132 run('chown {} local_settings.py'.format(env.user))
131 133
132 -  
133 def update_code(): 134 def update_code():
134 if env.is_vagrant: 135 if env.is_vagrant:
135 if not exists(REPO_PATH): 136 if not exists(REPO_PATH):
@@ -142,25 +143,27 @@ def update_code(): @@ -142,25 +143,27 @@ def update_code():
142 with cd(REPO_PATH): 143 with cd(REPO_PATH):
143 run('git pull') 144 run('git pull')
144 145
145 -  
146 @task 146 @task
147 def bootstrap(): 147 def bootstrap():
148 """Bootstrap machine to run fabric tasks""" 148 """Bootstrap machine to run fabric tasks"""
149 with settings(user=env.superuser): 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 if not exists('/usr/bin/git'): 153 if not exists('/usr/bin/git'):
154 package_install('git-core') 154 package_install('git-core')
155 155
156 if env.is_vagrant: 156 if env.is_vagrant:
157 - groups = 'sudo,vagrant' 157 + groups = ['sudo' ,'vagrant']
158 local('chmod -fR g+w {}'.format(PROJECT_PATH)) 158 local('chmod -fR g+w {}'.format(PROJECT_PATH))
159 else: 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 ssh_dir = '/home/{0}/.ssh/'.format(APP_USER) 167 ssh_dir = '/home/{0}/.ssh/'.format(APP_USER)
165 if not exists(ssh_dir): 168 if not exists(ssh_dir):
166 sudo('mkdir -p {0}'.format(ssh_dir)) 169 sudo('mkdir -p {0}'.format(ssh_dir))
@@ -179,13 +182,11 @@ def bootstrap(): @@ -179,13 +182,11 @@ def bootstrap():
179 sudo('chmod 440 {}'.format(tmp_file)) 182 sudo('chmod 440 {}'.format(tmp_file))
180 sudo('mv {} {}'.format(tmp_file, sudoers_file)) 183 sudo('mv {} {}'.format(tmp_file, sudoers_file))
181 184
182 -  
183 @task 185 @task
184 def provision(): 186 def provision():
185 """Run puppet""" 187 """Run puppet"""
186 188
187 update_code() 189 update_code()
188 -  
189 puppet_path = os.path.join(REPO_PATH, 'puppet/') 190 puppet_path = os.path.join(REPO_PATH, 'puppet/')
190 modules_path = os.path.join(puppet_path, 'modules') 191 modules_path = os.path.join(puppet_path, 'modules')
191 puppet_modules = '{}:/etc/puppet/modules'.format(modules_path) 192 puppet_modules = '{}:/etc/puppet/modules'.format(modules_path)
@@ -204,7 +205,6 @@ def provision(): @@ -204,7 +205,6 @@ def provision():
204 205
205 sudo('puppet apply --modulepath={} {}'.format(puppet_modules, cmd)) 206 sudo('puppet apply --modulepath={} {}'.format(puppet_modules, cmd))
206 207
207 -  
208 @task 208 @task
209 def ssh_keygen(): 209 def ssh_keygen():
210 """Create SSH credentials""" 210 """Create SSH credentials"""
@@ -218,7 +218,6 @@ def ssh_keygen(): @@ -218,7 +218,6 @@ def ssh_keygen():
218 print('') 218 print('')
219 print('Add the key above to your github repository deploy keys') 219 print('Add the key above to your github repository deploy keys')
220 220
221 -  
222 @task 221 @task
223 def deploy(noprovision=False): 222 def deploy(noprovision=False):
224 """Deploy and run the new code (master branch)""" 223 """Deploy and run the new code (master branch)"""