Skip to content

Instantly share code, notes, and snippets.

@marksteve
Last active January 25, 2019 23:51

Revisions

  1. marksteve revised this gist Jan 6, 2014. 1 changed file with 19 additions and 9 deletions.
    28 changes: 19 additions & 9 deletions pelican_deployer.py
    Original file line number Diff line number Diff line change
    @@ -2,10 +2,10 @@
    Simple web server that listens for Github webhooks to implement push-to-deploy
    with Pelican static sites
    Settings are loaded from a json file except for SECRET which should be an
    Settings are loaded from a json file except for SECRET which should be an
    environment variable
    Example deployer.json
    Example `deployer.json`
    {
    "repos": {
    @@ -24,19 +24,26 @@
    Add http://<deployer_host>/mysite/thisisasecret as a webhook url and you're done
    """
    import logging
    import os
    import subprocess
    import sys
    from subprocess import check_output

    from flask import Flask, json, jsonify, request


    app = Flask(__name__)


    def sh(cmd, **kwargs):
    app.logger.info(check_output(cmd.format(**kwargs), shell=True))

    cmd = cmd.format(**kwargs)
    output = subprocess.check_output(
    cmd,
    stderr=subprocess.STDOUT,
    shell=True,
    )
    app.logger.info('\n' + cmd)
    app.logger.info('\n'.join([' > ' + l for l in output.split('\n')]))


    @app.route('/<repo_id>/{}'.format(os.environ['SECRET']), methods=['POST'])
    def deploy(repo_id):
    @@ -45,12 +52,12 @@ def deploy(repo_id):
    repo = app.config['repos'][repo_id]
    os.chdir(repo['root'])
    sh(
    'git pull {remote} {branch}',
    remote=repo.get('remote', 'origin'),
    'git checkout {branch}',
    branch=branch,
    )
    sh(
    'git checkout -f {branch}',
    'git pull --ff-only {remote} {branch}',
    remote=repo.get('remote', 'origin'),
    branch=branch,
    )
    sh(
    @@ -63,4 +70,7 @@ def deploy(repo_id):
    if __name__ == '__main__':
    with open(sys.argv[1]) as f:
    app.config.update(**json.load(f))
    app.logger.addHandler(logging.StreamHandler())
    app.logger.setLevel(logging.INFO)
    app.run(port=int(os.environ.get('PORT', 5000)))

  2. marksteve revised this gist Jan 6, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pelican_deployer.py
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    Settings are loaded from a json file except for SECRET which should be an
    environment variable
    Example `deployer.json`
    Example deployer.json
    {
    "repos": {
  3. marksteve revised this gist Jan 6, 2014. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions pelican_deployer.py
    Original file line number Diff line number Diff line change
    @@ -55,9 +55,7 @@ def deploy(repo_id):
    )
    sh(
    'pelican -d -o {output} content',
    output=repo['output'],
    branch=branch,
    **payload
    output=repo['output'].format(branch=branch, **payload),
    )
    return jsonify(dict(ok=True))

  4. marksteve revised this gist Jan 6, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion pelican_deployer.py
    Original file line number Diff line number Diff line change
    @@ -25,6 +25,7 @@
    Add http://<deployer_host>/mysite/thisisasecret as a webhook url and you're done
    """
    import os
    import sys
    from subprocess import check_output

    from flask import Flask, json, jsonify, request
    @@ -62,6 +63,6 @@ def deploy(repo_id):


    if __name__ == '__main__':
    with open('deployer.json') as f:
    with open(sys.argv[1]) as f:
    app.config.update(**json.load(f))
    app.run(port=int(os.environ.get('PORT', 5000)))
  5. marksteve revised this gist Jan 6, 2014. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions pelican_deployer.py
    Original file line number Diff line number Diff line change
    @@ -48,10 +48,15 @@ def deploy(repo_id):
    remote=repo.get('remote', 'origin'),
    branch=branch,
    )
    sh(
    'git checkout -f {branch}',
    branch=branch,
    )
    sh(
    'pelican -d -o {output} content',
    output=repo['output'],
    branch=branch,
    **payload
    )
    return jsonify(dict(ok=True))

  6. marksteve revised this gist Jan 6, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions pelican_deployer.py
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@
    "mysite": {
    "root": "/path/to/repo",
    "remote": "origin",
    "output": "/srv/www"
    "output": "/srv/www/{branch}"
    }
    },
    "port": 5000
    @@ -49,7 +49,7 @@ def deploy(repo_id):
    branch=branch,
    )
    sh(
    'pelican -d -o {output}/{branch} content',
    'pelican -d -o {output} content',
    output=repo['output'],
    branch=branch,
    )
  7. marksteve revised this gist Jan 6, 2014. 1 changed file with 10 additions and 7 deletions.
    17 changes: 10 additions & 7 deletions pelican_deployer.py
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,12 @@
    """
    Simple web server that listens for Github
    webhooks to implement push-to-deploy
    Simple web server that listens for Github webhooks to implement push-to-deploy
    with Pelican static sites
    Usage:
    $ python ./pelican_deployer.py deployer.json
    Settings are loaded from a json file except for SECRET which should be an
    environment variable
    Settings are loaded from a json file except
    for SECRET which should be an env variable
    Example `deployer.json`
    Example deployer.json:
    {
    "repos": {
    "mysite": {
    @@ -20,6 +17,12 @@
    },
    "port": 5000
    }
    Run it
    $ SECRET=thisisasecret python ./pelican_deployer.py deployer.json
    Add http://<deployer_host>/mysite/thisisasecret as a webhook url and you're done
    """
    import os
    from subprocess import check_output
  8. marksteve revised this gist Jan 6, 2014. 1 changed file with 27 additions and 18 deletions.
    45 changes: 27 additions & 18 deletions pelican_deployer.py
    Original file line number Diff line number Diff line change
    @@ -2,49 +2,58 @@
    Simple web server that listens for Github
    webhooks to implement push-to-deploy
    with Pelican static sites
    s
    Usage:
    $ python ./pelican_deployer.py
    Settings are loaded from environment variables
    SECRET=<secret key>
    REMOTE=<git remote> (defaults to origin)
    ROOT=<output root> (defaults to /srv/pelican)
    PORT=<port> (defaults to 5000)
    Usage:
    $ python ./pelican_deployer.py deployer.json
    Settings are loaded from a json file except
    for SECRET which should be an env variable
    Example deployer.json:
    {
    "repos": {
    "mysite": {
    "root": "/path/to/repo",
    "remote": "origin",
    "output": "/srv/www"
    }
    },
    "port": 5000
    }
    """

    import os
    from subprocess import check_output

    from flask import Flask, json, jsonify, request


    app = Flask(__name__)


    def sh(cmd, **kwargs):
    app.logger.info(check_output(cmd.format(**kwargs), shell=True))


    @app.route('/{}'.format(os.environ['SECRET']), methods=['POST'])
    def deploy():
    @app.route('/<repo_id>/{}'.format(os.environ['SECRET']), methods=['POST'])
    def deploy(repo_id):
    payload = json.loads(request.form['payload'])
    branch = payload.get('ref').split('/')[2]

    repo = app.config['repos'][repo_id]
    os.chdir(repo['root'])
    sh(
    'git pull {remote} {branch}',
    remote=os.environ.get('REMOTE', 'origin'),
    remote=repo.get('remote', 'origin'),
    branch=branch,
    )
    sh(
    'pelican -d -o {root}/{branch} content',
    root=os.environ.get('ROOT', '/srv/pelican'),
    'pelican -d -o {output}/{branch} content',
    output=repo['output'],
    branch=branch,
    )

    return jsonify(dict(ok=True))


    if __name__ == '__main__':
    with open('deployer.json') as f:
    app.config.update(**json.load(f))
    app.run(port=int(os.environ.get('PORT', 5000)))

  9. marksteve revised this gist Jan 5, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions pelican_deployer.py
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    """
    Simple web server that listens for Github
    service hooks to implement push-to-deploy
    webhooks to implement push-to-deploy
    with Pelican static sites
    s
    Usage:
    $ python ./pelican_deployer.py
  10. marksteve revised this gist Jan 5, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion pelican_deployer.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    """
    Simple web server that listens for Github
    service hooks to deploy Pelican static sites
    service hooks to implement push-to-deploy
    with Pelican static sites
    Usage:
    $ python ./pelican_deployer.py
  11. marksteve revised this gist Jan 5, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions pelican_deployer.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    """
    Simple web server that accepts Github service hooks
    to deploy Pelican static sites
    Simple web server that listens for Github
    service hooks to deploy Pelican static sites
    Usage:
    $ python ./pelican_deployer.py
  12. marksteve revised this gist Jan 5, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pelican_deployer.py
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    Usage:
    $ python ./pelican_deployer.py
    Settings are loaded from environt
    Settings are loaded from environment variables
    SECRET=<secret key>
    REMOTE=<git remote> (defaults to origin)
  13. marksteve revised this gist Jan 5, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pelican_deployer.py
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    Usage:
    $ python ./pelican_deployer.py
    Settings are loaded from env:
    Settings are loaded from environt
    SECRET=<secret key>
    REMOTE=<git remote> (defaults to origin)
  14. marksteve revised this gist Jan 5, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pelican_deployer.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    """
    Simple web server that accepts Github serice hooks
    Simple web server that accepts Github service hooks
    to deploy Pelican static sites
    Usage:
  15. marksteve revised this gist Jan 5, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion pelican_deployer.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    """
    Deploy Pelican with Flask and Github Service Hooks
    Simple web server that accepts Github serice hooks
    to deploy Pelican static sites
    Usage:
    $ python ./pelican_deployer.py
  16. marksteve revised this gist Jan 5, 2014. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions pelican_deployer.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,17 @@
    """
    Deploy Pelican with Flask and Github Service Hooks
    Usage:
    $ python ./pelican_deployer.py
    Settings are loaded from env:
    SECRET=<secret key>
    REMOTE=<git remote> (defaults to origin)
    ROOT=<output root> (defaults to /srv/pelican)
    PORT=<port> (defaults to 5000)
    """

    import os
    from subprocess import check_output

  17. marksteve renamed this gist Jan 5, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions deployer.py → pelican_deployer.py
    Original file line number Diff line number Diff line change
    @@ -10,8 +10,8 @@ def sh(cmd, **kwargs):
    app.logger.info(check_output(cmd.format(**kwargs), shell=True))


    @app.route('/<{}>'.format(os.environ['SECRET']), methods=['POST'])
    def deploy(secret):
    @app.route('/{}'.format(os.environ['SECRET']), methods=['POST'])
    def deploy():
    payload = json.loads(request.form['payload'])
    branch = payload.get('ref').split('/')[2]

  18. marksteve revised this gist Jan 4, 2014. 1 changed file with 2 additions and 5 deletions.
    7 changes: 2 additions & 5 deletions deployer.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    import os
    from subprocess import check_output

    from flask import abort, Flask, json, jsonify, request
    from flask import Flask, json, jsonify, request

    app = Flask(__name__)

    @@ -10,11 +10,8 @@ def sh(cmd, **kwargs):
    app.logger.info(check_output(cmd.format(**kwargs), shell=True))


    @app.route('/<secret>', methods=['POST'])
    @app.route('/<{}>'.format(os.environ['SECRET']), methods=['POST'])
    def deploy(secret):
    if secret != os.environ['SECRET']:
    abort(403)

    payload = json.loads(request.form['payload'])
    branch = payload.get('ref').split('/')[2]

  19. marksteve revised this gist Jan 4, 2014. 1 changed file with 4 additions and 6 deletions.
    10 changes: 4 additions & 6 deletions deployer.py
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,23 @@
    from __future__ import print_function

    import os
    from subprocess import check_output

    from flask import abort, Flask, json, jsonify, request


    app = Flask(__name__)


    def sh(cmd, **kwargs):
    print(check_output(cmd.format(**kwargs), shell=True))
    app.logger.info(check_output(cmd.format(**kwargs), shell=True))


    @app.route('/<secret>', methods=['POST'])
    def deploy(secret):
    if secret != os.environ['SECRET']:
    abort(403)

    payload = json.loads(request.form['payload'])
    branch = payload.get('ref').split('/')[2]

    sh(
    'git pull {remote} {branch}',
    remote=os.environ.get('REMOTE', 'origin'),
    @@ -37,3 +34,4 @@ def deploy(secret):

    if __name__ == '__main__':
    app.run(port=int(os.environ.get('PORT', 5000)))

  20. marksteve revised this gist Jan 4, 2014. 1 changed file with 17 additions and 4 deletions.
    21 changes: 17 additions & 4 deletions deployer.py
    Original file line number Diff line number Diff line change
    @@ -5,20 +5,33 @@

    from flask import abort, Flask, json, jsonify, request


    app = Flask(__name__)


    def sh(cmd, **kwargs):
    print(check_output(cmd.format(**kwargs), shell=True))


    @app.route('/<secret>', methods=['POST'])
    def post(secret):
    def deploy(secret):
    if secret != os.environ['SECRET']:
    abort(403)

    payload = json.loads(request.form['payload'])
    branch = payload.get('ref').split('/')[2]
    cmd = 'pelican -d -o {root}/{branch} content'
    print(check_output(cmd.format(

    sh(
    'git pull {remote} {branch}',
    remote=os.environ.get('REMOTE', 'origin'),
    branch=branch,
    )
    sh(
    'pelican -d -o {root}/{branch} content',
    root=os.environ.get('ROOT', '/srv/pelican'),
    branch=branch,
    ), shell=True))
    )

    return jsonify(dict(ok=True))


  21. marksteve revised this gist Jan 4, 2014. 1 changed file with 7 additions and 13 deletions.
    20 changes: 7 additions & 13 deletions deployer.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    from __future__ import print_function

    import os
    import traceback
    from subprocess import check_output

    from flask import abort, Flask, json, jsonify, request
    @@ -13,18 +12,13 @@
    def post(secret):
    if secret != os.environ['SECRET']:
    abort(403)
    try:
    print('Deploying...')
    payload = json.loads(request.form['payload'])
    branch = payload.get('ref').split('/')[2]
    cmd = 'pelican -d -o {root}/{branch} content'
    print(check_output(cmd.format(
    root=os.environ.get('ROOT', '/srv/pelican'),
    branch=branch,
    ), shell=True))
    except Exception:
    traceback.print_exc()
    raise
    payload = json.loads(request.form['payload'])
    branch = payload.get('ref').split('/')[2]
    cmd = 'pelican -d -o {root}/{branch} content'
    print(check_output(cmd.format(
    root=os.environ.get('ROOT', '/srv/pelican'),
    branch=branch,
    ), shell=True))
    return jsonify(dict(ok=True))


  22. marksteve revised this gist Jan 3, 2014. 1 changed file with 2 additions and 6 deletions.
    8 changes: 2 additions & 6 deletions deployer.py
    Original file line number Diff line number Diff line change
    @@ -17,12 +17,8 @@ def post(secret):
    print('Deploying...')
    payload = json.loads(request.form['payload'])
    branch = payload.get('ref').split('/')[2]
    cmds = [
    'rm -rf output/',
    'pelican content',
    'rsync -r -m -h --delete --progress output/ {root}/{branch}',
    ]
    print(check_output(' && '.join(cmds).format(
    cmd = 'pelican -d -o {root}/{branch} content'
    print(check_output(cmd.format(
    root=os.environ.get('ROOT', '/srv/pelican'),
    branch=branch,
    ), shell=True))
  23. marksteve renamed this gist Jan 3, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  24. marksteve created this gist Jan 3, 2014.
    36 changes: 36 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    from __future__ import print_function

    import os
    import traceback
    from subprocess import check_output

    from flask import abort, Flask, json, jsonify, request

    app = Flask(__name__)


    @app.route('/<secret>', methods=['POST'])
    def post(secret):
    if secret != os.environ['SECRET']:
    abort(403)
    try:
    print('Deploying...')
    payload = json.loads(request.form['payload'])
    branch = payload.get('ref').split('/')[2]
    cmds = [
    'rm -rf output/',
    'pelican content',
    'rsync -r -m -h --delete --progress output/ {root}/{branch}',
    ]
    print(check_output(' && '.join(cmds).format(
    root=os.environ.get('ROOT', '/srv/pelican'),
    branch=branch,
    ), shell=True))
    except Exception:
    traceback.print_exc()
    raise
    return jsonify(dict(ok=True))


    if __name__ == '__main__':
    app.run(port=int(os.environ.get('PORT', 5000)))