Commit 7c6bb0278c37b97fe3d682d0a087877b2ede17d4

Authored by Charles Oliveira
1 parent dd2338ce

Fixed tab/spaces

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