-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
executable file
·558 lines (491 loc) · 13.7 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
<!DOCTYPE html>
<!--
Based on: Google HTML5 slide template
(http://code.google.com/p/html5slides/)
Author: Ricardo Kirkner
-->
<html>
<head>
<title>Using fabric to standardize the development process | PyCon US 2012
- March 06-11| by Ricardo Kirkner</title>
<meta charset='utf-8'>
<meta name="author" content="Ricardo Kirkner" />
<meta name="description" content="Using fabric to standardize the
development process" />
<script src='slides.js'></script>
</head>
<style>
/* Your individual styles here, or just use inline styles if that’s
what you want. */
.current {
opacity: 1;
}
.past, .next {
opacity: 0.2;
}
.far-past, .far-next {
opacity: 0;
}
div#contact {
position: absolute;
top: 350px;
left: 300px;
}
dt {
float: left;
clear: left;
width: 100px;
}
dd {
margin: 0 0 0 110px;
margin-top: 50px;
margin-bottom: 50px;
}
</style>
<body style='display: none'>
<section class='slides layout-regular'>
<!-- Your slides (<article>s) go here. Delete or comment out the
slides below. -->
<article>
<h1>
Using <strong>fabric</strong> to standardize the development
process
</h1>
<p>
Ricardo Kirkner
<br>
PyCon US 2012
</p>
</article>
<article>
<h3>Who is this guy?</h3>
<p>Ricardo Kirkner</p>
<p>Works with Python since 2004, currently employed at Canonical</p>
<p>Background in system administration, machine learning, web
development</p>
</article>
<article>
<h3>What is he babbling about?</h3>
<ul>
<li>What is fabric?</li>
<li>Why fabric?</li>
<li>Fabric for the development cycle</li>
</ul>
</article>
<article class="smaller">
<h3>
What is fabric?
</h3>
<q>
Fabric is a Python [...] library and command-line tool for
streamlining the use of SSH for application deployment or system
administration tasks.
</q>
<div class="black source">
Source: http://docs.fabfile.org/en/1.4.0/index.html#about
</div>
</article>
<article>
<h2>Huh?</h2>
<p>
<img class="centered" style="height: 500px" src="confused2.jpg" />
</p>
</article>
<article>
<h3>naïve approach</h3>
<pre>
version = os.popen("ssh myserver 'cat /proc/version'").read()
</pre>
<div class="build">
<div>
<h3>basic fabric (as a library)</h3>
<pre>
from fabric.api import run
env.hosts = ["myserver"]
version = run("cat /proc/version")
</pre>
</div>
</div>
</article>
<article>
<h3>proper fabric (as a command-line tool)</h3>
<pre>
from fabric.api import run
def show_version():
version = run("cat /proc/version")
print version
</pre>
<pre>
$ fab -H myserver show_version
</pre>
<pre>
$ fab -l
Available commands:
show_version
</pre>
</article>
<article>
<h3>
Why fabric?
</h3>
<div class="build">
<h5>and not ...</h5>
<ul>
<li>
zc.buildout
</li>
<li>
make
</li>
<li>
...
</li>
</ul>
</div>
</article>
<article>
<h3>Why fabric?</h3>
<div class="build">
<h5><strong>Two</strong> reasons</h5>
<ol>
<li class="build">
<p>lazyness</p>
<p>(or, we don't want to learn new stuff)</p>
</li>
<li class="build">
<p>legacy</p>
<p>(or was it lazyness again?)</p>
</li>
</ol>
</div>
</article>
<article>
<h2>
Fabric for the development cycle
</h2>
</article>
<article>
<h3>
What can you do with fabric?
</h3>
<ul>
<li>bootstrap development environment</li>
<li>manage the database</li>
<li>run tests</li>
<li>build binary packages</li>
<li>install packages</li>
<li>configure services</li>
<li>manage services</li>
<li>deploy</li>
<li>rollback</li>
<li>anything you can do manually on a terminal</li>
</ul>
</article>
<article>
<h3>
Bootstrap
</h3>
<div class="build">
<ol>
<li>do sanity checks</li>
<li>setup virtualenv</li>
<li>install dependencies</li>
<li>setup configuration</li>
</ol>
<pre>
def bootstrap():
_check_bootstrap_dependencies()
_check_psycopg2_conflicts()
setup_virtualenv()
install_dependencies()
setup_configuration()
</pre>
</div>
</article>
<article>
<pre>
def setup_virtualenv():
created = False
virtual_env = os.environ.get('VIRTUAL_ENV', None)
if virtual_env is None:
if not os.path.exists(VIRTUALENV):
_create_virtualenv()
created = True
virtual_env = VIRTUALENV
env.virtualenv = os.path.abspath(virtual_env)
_activate_virtualenv()
return created
</pre>
</article>
<article>
<pre>
def _activate_virtualenv():
activate_this = os.path.abspath(
"%s/bin/activate_this.py" % env.virtualenv)
execfile(activate_this, dict(__file__=activate_this))
def _create_virtualenv(clear=False):
if not os.path.exists(VIRTUALENV) or clear:
args = '--no-site-packages --distribute --clear'
local("%s /usr/bin/virtualenv %s %s" % (sys.executable,
args, VIRTUALENV), capture=False)
</pre>
</article>
<article>
<pre>
def install_dependencies():
cmd = ['pip', 'install', '-r', 'requirements.txt']
virtualenv_local(' '.join(cmd), capture=False)
virtualenv_local('python setup.py develop')
</pre>
<pre>
def virtualenv_local(command, capture=True):
prefix = ''
virtual_env = env.get('virtualenv', None)
if virtual_env:
prefix = ". %s/bin/activate && " % virtual_env
command = prefix + command
return local(command, capture=capture)
</pre>
</article>
<article>
<h3>
Database
</h3>
<ul>
<li>setup database</li>
<li>start/stop database</li>
<li>create/drop database</li>
<li>sync database</li>
<li>add/remove users/roles</li>
</ul>
</article>
<article>
<pre>
def setup_postgresql_server():
_set_postgres_environment()
local("mkdir -p %(DATA)s" % env.postgres)
local("%(ENV)s %(BIN)s/initdb -A trust -D %(DATA)s" % env.postgres)
local("cat <<EOF\nfsync = off\nEOF > %(DATA)s/postgresql.conf" %
env.postgres)
start_database()
setup_database()
def shutdown_postgresql_server():
_set_postgres_environment()
dropdb()
stop_database()
local("rm -rf %s" % env.postgres['HOST'])
</pre>
</article>
<article>
<pre>
def start_database():
_set_postgres_environment()
cmd = ['%(ENV)s', '%(BIN)s/pg_ctl', 'start', '-w', '-l',
'%(HOST)s/postgresql.log', '-o', '"-F -k %(HOST)s -h \'\'"']
local(' '.join(cmd) % env.postgres)
def stop_database():
_set_postgres_environment()
cmd = ['%(ENV)s', '%(BIN)s/pg_ctl', 'stop', '-w', '-m', 'fast']
local(' '.join(cmd) % env.postgres)
def setup_database():
success = _check_database()
if not success:
createdb()
_createrole()
syncdb()
</pre>
</article>
<article>
<pre>
def createdb():
_set_postgres_environment()
local("%(ENV)s %(BIN)s/createdb %(DATABASE)s" % env.postgres)
def dropdb(warn_only=False):
_set_postgres_environment()
if isinstance(warn_only, basestring):
warn_only = warn_only.lower() == 'yes'
with settings(warn_only=warn_only):
local("%(ENV)s %(BIN)s/dropdb %(DATABASE)s" % env.postgres)
</pre>
</article>
<article>
<h3>
Development
</h3>
<ul>
<li>run development server</li>
<li>reset database</li>
<li>run tests</li>
</ul>
</article>
<article>
<pre>
def run(*args):
manage('runserver', *args)
def manage(command, *args):
virtualenv_local(
"python django_project/manage.py {0} {1}".format(
command, " ".join(args)), capture=False,)
def resetdb():
with settings(hide='warnings'):
dropdb(warn_only=True)
createdb()
syncdb()
def test(extra=''):
args = ['--noinput', extra]
manage('test', *args)
</pre>
</article>
<article>
<h3>
Continuous integration
</h3>
<div class="build">
<ol>
<li>bootstrap</li>
<li>setup database</li>
<li>run tests</li>
<li>gather metrics</li>
</ol>
<pre>
def jenkins():
"""Run the tests for jenkins."""
bootstrap(jenkins=True)
resetdb()
manage('loaddata test')
manage("jenkins")
</pre>
</div>
</article>
<article>
<h3>
Deployment
</h3>
<ul>
<li>deploy</li>
<li>build binary packages</li>
</ul>
</article>
<article>
<pre>
def deploy(config_branch, build_dir=None, distroseries='lucid'):
delete_dir_after_build = False
if build_dir is None:
build_dir = tempfile.mkdtemp()
delete_dir_after_build = True
dsc_file = build_source_package(build_dir,
distroseries=distroseries)
binary_files = build_binary_packages(build_dir, dsc_file)
install_binary_packages(*binary_files)
migrate_database()
update_configs(config_branch)
preflight_check()
restart_services()
print("Package installed and services restarted.")
if delete_dir_after_build:
shutil.rmtree(build_dir, ignore_errors=True)
</pre>
</article>
<article>
<pre>
def build_source_package(build_dir, recipe_filename='sso.recipe',
distroseries='lucid'):
clean_local_branch()
recipe = dedent("""\
# bzr-builder format 0.2 deb-version {revno}+{time}
.""")
recipe_path = os.path.join(build_dir, recipe_filename)
with open(recipe_path, 'w+') as recipe_file:
recipe_file.write(recipe)
local('bzr dailydeb -&-distribution={0} {1} {2}'.format(
distroseries, recipe_path, build_dir))
dsc_file = glob(os.path.join(build_dir, '*.dsc'))[0]
print("Created source package at {0}.".format(dsc_file))
return dsc_file
</pre>
</article>
<article>
<pre>
def build_binary_packages(build_dir, source_dsc):
print("Building binary package for {0}".format(source_dsc))
local('sudo pbuilder build --buildresult {0} {1}'.format(
build_dir, source_dsc))
deb_files = glob(os.path.join(build_dir, "*.deb"))
print("Created binary at {0}.".format(deb_files))
return deb_files
def install_binary_packages(*deb_files):
files = []
for deb_file in deb_files:
put(deb_file, '.')
deb_filename = os.path.basename(deb_file)
files.append(deb_filename)
sudo('dpkg -i {0}'.format(' '.join(files)), shell=False)
</pre>
</article>
<article>
<pre>
def update_configs(config_branch, config_dir='django_project'):
timestamp = datetime.utcnow().isoformat()
backup_dir = "%s.backup.%s" % (config_dir, timestamp)
if exists(config_dir):
run("mv %s %s" % (config_dir, backup_dir))
put_bzr_export(config_branch, config_dir)
if exists(backup_dir + '/local.cfg'):
run("cp %s/local.cfg %s" % (backup_dir, config_dir))
else:
# Create a local.cfg on the server from our template.
local_fd, local_path = tempfile.mkstemp()
local_file = open(local_path, 'w')
local_file.write(LOCAL_CONFIG % env.host_string)
local_file.close()
put(local_path, "%s/local.cfg" % config_dir, mode=0644)
os.remove(local_path)
</pre>
</article>
<article>
<pre>
def migrate_database(config_dir=None):
if config_dir is None:
# By default, the sso config directory is assumed to be
# /home/username/django_project.
config_dir = 'django_project'
run(config_dir + '/manage.py syncdb')
def preflight_check(config_dir='django_project'):
run('COLUMNS=40 %s/manage.py preflight ' % config_dir)
def restart_services():
sudo('service apache2 graceful', shell=False)
</pre>
</article>
<article id="questions" class="fill">
<h3>Questions?</h3>
<p>
<img src="questions.jpg" />
</p>
<div class="black source">
Source: https://github.com/ricardokirkner/fabric-pycon2012
</div>
</article>
<article class="fill">
<h3>
Contact me
</h3>
<p>
<img src="contact25.jpg" />
</p>
<div id="contact">
<dl>
<dt>email</dt>
<dd>ricardo@kirkner.com.ar</dd>
<dt>twitter</dt>
<dd>@ricardokirkner</dd>
<dt>irc</dt>
<dd>pindonga</dd>
</dl>
</div>
<div class="black source">
Source: https://github.com/ricardokirkner/fabric-pycon2012
</div>
</article>
</section>
</body>
</html>