Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 318 lines (261 sloc) 9.087 kB
2d42f13 Initial commit
Ryan Mark authored
1 # -*- coding: utf-8 -*-
2 # Chicago Tribune News Applications fabfile
3 # No copying allowed
4
b24bef7 shiva command works locally, install_plugin gives better feedback
Ryan Mark authored
5 import os
6 import subprocess
7 import urllib
8
2d42f13 Initial commit
Ryan Mark authored
9 from fabric.api import *
10 from fabric.contrib.console import confirm
11 from fabric.context_managers import cd
12
13 from getpass import getpass
14
15 """
16 Base configuration
17 """
18 env.project_name = "my-wp-blog"
19 env.wpdomain = 'my-wp-blog.dev'
20 env.path = os.getcwd()
21
22 # Do you want to use git or svn for deployment?
23 env.strategy = 'git'
24
25 # If you said svn, where should I checkout from?
26 env.svnrepo = ''
27
28 # If you said git, where should I clone from and which branch should I checkout?
29 env.gitrepo = ''
30 env.gitbranch = 'master'
31
32 # These are the credentials for the wordpress. They should match your wp-config.php.
33 env.db_host = 'localhost'
34 env.db_name = env.project_name
35 env.db_wpuser_name = env.project_name
36 env.db_wpuser_pass = 'changeme' #make up something complicated for the password
37
38 # Super user name and pass for adding users and databases to mysql
39 env.db_root_user = "root"
40 env.db_root_pass = "root"
41
42 # This is the config file that will get installed on bootstrap
43 env.config_file = 'wp-config-mamp.php'
44
45 # Fix permissions throughout the deployment process. You may need to use this
46 # if perms are getting messed up.
47 env.fix_perms = False
48
49 # This defaults the run and sudo functions to local, so we don't have to duplicate
50 # code for local development and deployed servers.
51 env.sudo = local
52 env.run = local
53
85662bf added get_wordpress fab command
Ryan Mark authored
54 # Where should I get Wordpress??
55 env.wp_tarball = "http://wordpress.org/latest.tar.gz"
56
2d42f13 Initial commit
Ryan Mark authored
57 """
58 Environments
59 """
60 def production():
61 """
62 Work on production environment
63 """
64 env.settings = 'production'
65 env.hosts = ['example.com']
66 env.user = ''
67 env.path = ''
68 env.wpdomain = 'example.com'
69 env.db_root_user = 'wpcustomuser'
70 env.db_root_pass = ''
71 env.config_file = 'wp-config-production.php'
72 env.db_host = 'db.example.com'
73 env.db_name = 'wp_custom_database'
74 check_env()
75
76 def staging():
77 """
78 Work on staging environment
79 """
80 env.settings = 'staging'
81 env.hosts = ['staging.example.com']
82 env.user = ''
83 env.path = ''
84 env.wpdomain = 'staging.example.com'
85 env.db_root_user = 'wpsuperuser'
86 env.db_root_pass = ''
87 env.config_file = 'wp-config-staging.php'
88 check_env()
89
90 """
91 Commands - setup
92 """
93 def git_clone_repo():
94 """
95 Do initial clone of the git repository.
96 """
97 with settings(warn_only=True):
98 run('git clone %(gitrepo)s %(path)s' % env)
99
100 def git_checkout():
101 """
102 Pull the latest code on the specified branch.
103 """
104 with cd(env.path):
105 if env.branch != 'master':
106 with settings(warn_only=True):
107 run('git checkout -b %(gitbranch)s origin/%(gitbranch)s' % env)
108 run('git checkout %(gitbranch)s' % env)
109 run('git pull origin %(gitbranch)s' % env)
110
111 def svn_checkout():
112 """
113 Checkout the site
114 """
115 env.svn_user = prompt('SVN Username: ')
116 env.svn_pass = getpass('Enter SVN Password: ')
117
118 with cd(env.path):
119 run('svn co %(repo)s . --username %(svn_user)s --password %(svn_pass)s' % env)
120
121 """
122 Commands - deployment
123 """
124 def setup():
125 """
126 Setup the site
127 """
128 if env.strategy == 'git':
129 git_clone()
130 git_checkout()
131 elif env.strategy == 'svn':
132 svn_checkout()
133
134 fix_perms()
135
136 def deploy():
137 """
138 Deploy new code to the site
139 """
140 if env.strategy == 'git':
141 git_clone()
142 git_checkout()
143 elif env.strategy == 'svn':
144 svn_checkout()
145
146 fix_perms()
147
148 """
149 Commands - data
150 """
151 def bootstrap():
152 print("\nStep 1: Database and basic Wordpress setup")
153
154 with cd(env.path):
155 env.run('cp -P %(config_file)s wp-config.php' % env)
156
157 fix_perms()
158
159 create_db()
160 env.run('curl -s http://%(wpdomain)s/scripts/na-install.php' % env)
161
162 print("\nStep 2: Setup plugins")
163
164 env.run('curl -s http://%(wpdomain)s/scripts/na-setup-plugins.php' % env)
165
166 print("\nStep 3: Cleanup, create blogs")
167
168 env.run('curl -s http://%(wpdomain)s/scripts/na-postinstall.php' % env)
169
170 if confirm("Create child blogs?"): create_blogs()
171
172 def create_db():
173 if not env.db_root_pass:
174 env.db_root_pass = getpass("Database password: ")
175
176 env.run('mysqladmin --host=%(db_host)s --user=%(db_root_user)s --password=%(db_root_pass)s create %(db_name)s' % env)
177 env.run('echo "GRANT ALL ON * TO \'%(db_wpuser_name)s\'@\'localhost\' IDENTIFIED BY \'%(db_wpuser_pass)s\';" | mysql --host=%(db_host)s --user=%(db_root_user)s --password=%(db_root_pass)s %(db_name)s' % env)
178
179 def load_db(dump_slug='dump'):
180 env.dump_slug = dump_slug
181 if not env.db_root_pass:
182 env.db_root_pass = getpass("Database password: ")
183 with cd(env.path):
184 env.run("bzcat data/%(dump_slug)s.sql.bz2 |sed s/WPDEPLOYDOMAN/%(wpdomain)s/g |mysql --host=%(db_host)s --user=%(db_root_user)s --password=%(db_root_pass)s %(db_name)s" % env)
185
186 def dump_db(dump_slug='dump'):
187 env.dump_slug = dump_slug
188 if not env.db_root_pass:
189 env.db_root_pass = getpass("Database password: ")
190 with cd(env.path):
e918470 fixed dump_db command
Ryan Mark authored
191 env.run("mysqldump --host=%(db_host)s --user=%(db_root_user)s --password=%(db_root_pass)s --lock-all-tables %(project_name)s |sed s/%(wpdomain)s/WPDEPLOYDOMAN/g |bzip2 > data/%(dump_slug)s.sql.bz2" % env)
2d42f13 Initial commit
Ryan Mark authored
192
193 def destroy_db():
194 if not env.db_root_pass:
195 env.db_root_pass = getpass("Database password: ")
196
197 with settings(warn_only=True):
198 env.run('mysqladmin -f --host=%(db_host)s --user=%(db_root_user)s --password=%(db_root_pass)s drop %(project_name)s' % env)
199 env.run('echo "DROP USER \'%(db_wpuser_name)s\'@\'localhost\';" | mysql --host=%(db_host)s --user=%(db_root_user)s --password=%(db_root_pass)s' % env)
200
201 def destroy_attachments():
202 with cd(env.path):
203 env.run('rm -rf wp-content/blogs.dir')
204
205 def reload_db(dump_slug='dump'):
206 destroy_db()
207 create_db()
208 load_db(dump_slug)
209
210 def create_blogs():
211 response = "Success"
212 base_cmd = 'curl -s http://%(wpdomain)s/scripts/na-createblog.php' % env
213 i=0
214 while "Success" in response:
215 response = env.run(base_cmd + '?new_blog_index=%s' % i)
216 i+=1
217 print(response)
218 print("Created %s blogs" % str(i-1))
219
220 def fix_perms():
221 if env.fix_perms:
222 env.sudo("chown -Rf %(apache_user)s:%(apache_group)s %(path)s; chmod -Rf ug+rw %(path)s;" % env)
223
224 def wrap_media():
225 with cd(env.path):
226 env.run('tar zcf data/media.tgz wp-content/blogs.dir/* wp-content/uploads/*')
227 print('Wrapped up media.\n')
228
229 def unwrap_media():
230 with cd(env.path):
231 env.run('tar zxf data/media.tgz')
232 print('Unwrapped media.\n')
233
234 def put_media():
235 check_env()
236 put('data/media.tgz','%(path)s/data/media.tgz' % env)
237 print('Put media on server.\n')
238
239 def get_media():
240 check_env()
241 get('%(path)s/data/media.tgz' % env, 'data/media.tgz')
242 print('Got media from the server.\n')
243
244 """
245 Deaths, destroyers of worlds
246 """
247 def shiva_the_destroyer():
248 """
249 Remove all directories, databases, etc. associated with the application.
250 """
b24bef7 shiva command works locally, install_plugin gives better feedback
Ryan Mark authored
251 try:
252 check_env()
253 env.run('rm -Rf %(path)s/* %(path)s/.*;' % env)
254 destroy_db()
255 except NameError, e:
256 with settings(warn_only=True):
257 env.run('rm .htaccess')
258 env.run('rm wp-config.php')
259 destroy_db()
2d42f13 Initial commit
Ryan Mark authored
260
261 """
262 Utilities
263 """
264 def check_env():
265 require('settings', provided_by=[production, staging, development, testing])
266 env.sudo = sudo
267 env.run = run
85662bf added get_wordpress fab command
Ryan Mark authored
268
269 def get_wordpress():
6e558ae Added install_plugin fab command which downloads and decompresses plu…
Ryan Mark authored
270 print("Downloading and installing Wordpress...")
85662bf added get_wordpress fab command
Ryan Mark authored
271 with cd(env.path):
6e558ae Added install_plugin fab command which downloads and decompresses plu…
Ryan Mark authored
272 env.run('curl -s %(wp_tarball)s | tar xzf - ' % env)
85662bf added get_wordpress fab command
Ryan Mark authored
273 env.run('mv wordpress/* .')
274 env.run('rmdir wordpress')
6e558ae Added install_plugin fab command which downloads and decompresses plu…
Ryan Mark authored
275 print("Done.")
276
277 def install_plugin(name, version='latest'):
278 try:
279 from lxml.html import parse
280 from lxml.cssselect import CSSSelector
281 except ImportError:
282 print("I need lxml to do this")
283 exit()
284
285 print("Looking for %s..." % name)
286
b24bef7 shiva command works locally, install_plugin gives better feedback
Ryan Mark authored
287 url = "http://wordpress.org/extend/plugins/%s/" % name
288 p = parse("%sdownload/" % url)
6e558ae Added install_plugin fab command which downloads and decompresses plu…
Ryan Mark authored
289 sel = CSSSelector('.block-content .unmarked-list a')
290 dload_elems = sel(p)
291
b24bef7 shiva command works locally, install_plugin gives better feedback
Ryan Mark authored
292 if not dload_elems:
293 print("Can't find plugin %s" % name)
294 exit()
295
6e558ae Added install_plugin fab command which downloads and decompresses plu…
Ryan Mark authored
296 #first is latest
297 if version == 'latest':
298 plugin_zip = dload_elems[0].attrib['href']
299 version = dload_elems[0].text
300 else:
301 plugin_zip = None
302 for e in dload_elems:
303 if e.text == 'version':
304 plugin_zip = e.attrib['href']
305 break
306
307 if not plugin_zip:
308 print("Can't find plugin %s" % name)
309 exit()
310 else:
311 print("Found version %s of %s, installing..." % (version, name) )
b24bef7 shiva command works locally, install_plugin gives better feedback
Ryan Mark authored
312 with cd(env.path + "/wp-content/plugins"):
6e558ae Added install_plugin fab command which downloads and decompresses plu…
Ryan Mark authored
313 env.run('curl -s %s -o %s.%s.zip' % (plugin_zip, name, version) )
314 env.run('unzip -n %s.%s.zip' % (name, version) )
b24bef7 shiva command works locally, install_plugin gives better feedback
Ryan Mark authored
315
316 if raw_input("Read instructions for %s? [Y|n]" % name) in ("","Y"):
317 subprocess.call(['open', url])
Something went wrong with that request. Please try again.