fabfile.py

Tue, 03 Jan 2017 18:21:20 +0100

author
David Douard <david.douard@logilab.fr>
date
Tue, 03 Jan 2017 18:21:20 +0100
changeset 71
76d5c4108e51
parent 0
e7f601f9db69
permissions
-rw-r--r--

[blog] new series on the HP 3562A

0
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
1 from fabric.api import *
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
2 import fabric.contrib.project as project
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
3 import os
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
4 import shutil
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
5 import sys
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
6 import SocketServer
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
7
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
8 from pelican.server import ComplexHTTPRequestHandler
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
9
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
10 # Local path configuration (can be absolute or relative to fabfile)
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
11 env.deploy_path = 'output'
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
12 DEPLOY_PATH = env.deploy_path
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
13
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
14 # Remote server configuration
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
15 production = 'root@localhost:22'
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
16 dest_path = '/var/www'
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
17
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
18 # Rackspace Cloud Files configuration settings
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
19 env.cloudfiles_username = 'my_rackspace_username'
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
20 env.cloudfiles_api_key = 'my_rackspace_api_key'
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
21 env.cloudfiles_container = 'my_cloudfiles_container'
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
22
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
23 # Github Pages configuration
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
24 env.github_pages_branch = "gh-pages"
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
25
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
26 # Port for `serve`
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
27 PORT = 8000
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
28
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
29 def clean():
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
30 """Remove generated files"""
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
31 if os.path.isdir(DEPLOY_PATH):
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
32 shutil.rmtree(DEPLOY_PATH)
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
33 os.makedirs(DEPLOY_PATH)
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
34
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
35 def build():
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
36 """Build local version of site"""
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
37 local('pelican -s pelicanconf.py')
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
38
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
39 def rebuild():
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
40 """`clean` then `build`"""
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
41 clean()
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
42 build()
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
43
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
44 def regenerate():
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
45 """Automatically regenerate site upon file modification"""
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
46 local('pelican -r -s pelicanconf.py')
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
47
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
48 def serve():
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
49 """Serve site at http://localhost:8000/"""
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
50 os.chdir(env.deploy_path)
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
51
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
52 class AddressReuseTCPServer(SocketServer.TCPServer):
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
53 allow_reuse_address = True
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
54
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
55 server = AddressReuseTCPServer(('', PORT), ComplexHTTPRequestHandler)
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
56
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
57 sys.stderr.write('Serving on port {0} ...\n'.format(PORT))
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
58 server.serve_forever()
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
59
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
60 def reserve():
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
61 """`build`, then `serve`"""
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
62 build()
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
63 serve()
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
64
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
65 def preview():
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
66 """Build production version of site"""
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
67 local('pelican -s publishconf.py')
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
68
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
69 def cf_upload():
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
70 """Publish to Rackspace Cloud Files"""
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
71 rebuild()
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
72 with lcd(DEPLOY_PATH):
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
73 local('swift -v -A https://auth.api.rackspacecloud.com/v1.0 '
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
74 '-U {cloudfiles_username} '
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
75 '-K {cloudfiles_api_key} '
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
76 'upload -c {cloudfiles_container} .'.format(**env))
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
77
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
78 @hosts(production)
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
79 def publish():
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
80 """Publish to production via rsync"""
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
81 local('pelican -s publishconf.py')
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
82 project.rsync_project(
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
83 remote_dir=dest_path,
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
84 exclude=".DS_Store",
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
85 local_dir=DEPLOY_PATH.rstrip('/') + '/',
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
86 delete=True,
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
87 extra_opts='-c',
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
88 )
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
89
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
90 def gh_pages():
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
91 """Publish to GitHub Pages"""
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
92 rebuild()
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
93 local("ghp-import -b {github_pages_branch} {deploy_path}".format(**env))
e7f601f9db69 initial setup
David Douard <david.douard@logilab.fr>
parents:
diff changeset
94 local("git push origin {github_pages_branch}".format(**env))

mercurial