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