From 2f6689d63932d51f11dfaac9551fa3ddd14a11c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Petazzoni?= Date: Fri, 29 Sep 2017 22:29:08 +0200 Subject: [PATCH] Refactor card generation to use Jinja templates This makes the card generation process a bit easier to customize. A few issues with Chrome page breaks were also fixed. --- prepare-vms/Dockerfile | 1 + prepare-vms/cards.html | 80 +++++++++++++++++ prepare-vms/scripts/ips-txt-to-html.py | 113 +++++------------------- prepare-vms/settings/example.yaml | 36 -------- prepare-vms/settings/fundamentals.yaml | 41 ++++----- prepare-vms/settings/orchestration.yaml | 37 +++----- prepare-vms/settings/sec.yml | 32 ------- 7 files changed, 133 insertions(+), 207 deletions(-) create mode 100644 prepare-vms/cards.html delete mode 100644 prepare-vms/settings/example.yaml delete mode 100644 prepare-vms/settings/sec.yml diff --git a/prepare-vms/Dockerfile b/prepare-vms/Dockerfile index 9fe3c076..1ce09a95 100644 --- a/prepare-vms/Dockerfile +++ b/prepare-vms/Dockerfile @@ -20,6 +20,7 @@ RUN apt-get update && apt-get install -y \ RUN pip install \ awscli \ + jinja2 \ pdfkit \ PyYAML \ termcolor diff --git a/prepare-vms/cards.html b/prepare-vms/cards.html new file mode 100644 index 00000000..c9501da6 --- /dev/null +++ b/prepare-vms/cards.html @@ -0,0 +1,80 @@ +{# Feel free to customize or override anything in there! #} +{%- set url = "http://container.training/" -%} +{%- if clustersize == 1 -%} + {%- set workshop_name = "Docker" -%} + {%- set pagesize = 15 -%} + {%- set cluster_or_machine = "machine" -%} + {%- set this_or_each = "this" -%} + {%- set machine_is_or_machines_are = "machine is" -%} + {%- set background_image = "https://s3-us-west-2.amazonaws.com/www.breadware.com/integrations/docker.png" -%} +{%- else -%} + {%- set workshop_name = "orchestration" -%} + {%- set pagesize = 12 -%} + {%- set cluster_or_machine = "cluster" -%} + {%- set this_or_each = "each" -%} + {%- set machine_is_or_machines_are = "machines are" -%} + {%- set background_image = "https://cdn.wp.nginx.com/wp-content/uploads/2016/07/docker-swarm-hero2.png" -%} +{%- endif -%} + + + + +{% for cluster in clusters %} + {% if loop.index0>0 and loop.index0%pagesize==0 %} + + {% endif %} +
+

+ Here is the connection information to your very own + {{ cluster_or_machine }} for this {{ workshop_name }} workshop. + You can connect to {{ this_or_each }} VM with any SSH client. +

+

+ Your {{ machine_is_or_machines_are }}: +

+

+

+ login: docker +
+ password: training +

+

For slides, chat and other useful links, see: +

{{ url }}
+

+
+{% endfor %} + + diff --git a/prepare-vms/scripts/ips-txt-to-html.py b/prepare-vms/scripts/ips-txt-to-html.py index d498afee..f95fe3d9 100755 --- a/prepare-vms/scripts/ips-txt-to-html.py +++ b/prepare-vms/scripts/ips-txt-to-html.py @@ -2,52 +2,27 @@ import os import sys import yaml -try: - import pdfkit -except ImportError: - print("WARNING: could not import pdfkit; PDF generation will fali.") +import jinja2 def prettify(l): l = [ip.strip() for ip in l] ret = [ "node{}: {}".format(i+1, s) for (i, s) in zip(range(len(l)), l) ] return ret - # Read settings from user-provided settings file -with open(sys.argv[1]) as f: - data = f.read() +SETTINGS = yaml.load(open(sys.argv[1])) -SETTINGS = yaml.load(data) -SETTINGS['footer'] = SETTINGS['footer'].format(url=SETTINGS['url']) -globals().update(SETTINGS) - -############################################################################### +clustersize = SETTINGS["clustersize"] ips = list(open("ips.txt")) -print("Current settings (as defined in settings.yaml):") +print("---------------------------------------------") print(" Number of IPs: {}".format(len(ips))) print(" VMs per cluster: {}".format(clustersize)) -print("Background image: {}".format(background_image)) print("---------------------------------------------") assert len(ips)%clustersize == 0 -if clustersize == 1: - blurb = blurb.format( - cluster_or_machine="machine", - this_or_each="this", - machine_is_or_machines_are="machine is", - workshop_name=workshop_short_name, - ) -else: - blurb = blurb.format( - cluster_or_machine="cluster", - this_or_each="each", - machine_is_or_machines_are="machines are", - workshop_name=workshop_short_name, - ) - clusters = [] while ips: @@ -55,66 +30,22 @@ while ips: ips = ips[clustersize:] clusters.append(cluster) -html = open("ips.html", "w") -html.write("") -for i, cluster in enumerate(clusters): - if i>0 and i%pagesize==0: - html.write('\n') - - html.write("
") - html.write(blurb) - for s in prettify(cluster): - html.write("
  • %s
  • \n"%s) - html.write("

    ") - html.write("

    login: {}
    password: {}

    \n".format(instance_login, instance_password)) - html.write(footer) - html.write("
    ") -html.close() - -""" - html.write("
    ") - html.write("

    {}

    ".format(blurb)) - for s in prettify(cluster): - html.write("
  • {}
  • ".format(s)) - html.write("

    ") - html.write("
    ") - html.write("

    login: {}    password: {}

    \n".format(instance_login, instance_password)) - html.write("
    ") - html.write(footer) - html.write("
    ") -html.close() -""" - -with open('ips.html') as f: - pdfkit.from_file(f, 'ips.pdf') +try: + import pdfkit + with open("ips.html") as f: + pdfkit.from_file(f, "ips.pdf", options={ + "page-size": SETTINGS["paper_size"], + "margin-top": SETTINGS["paper_margin"], + "margin-bottom": SETTINGS["paper_margin"], + "margin-left": SETTINGS["paper_margin"], + "margin-right": SETTINGS["paper_margin"], + }) + print("Generated ips.pdf") +except ImportError: + print("WARNING: could not import pdfkit; did not generate ips.pdf") diff --git a/prepare-vms/settings/example.yaml b/prepare-vms/settings/example.yaml deleted file mode 100644 index 494d7a65..00000000 --- a/prepare-vms/settings/example.yaml +++ /dev/null @@ -1,36 +0,0 @@ -# This file is passed by trainer-cli to scripts/ips-txt-to-html.py - -workshop_name: Docker Orchestration -workshop_short_name: orchestration -repo: https://github.com/jpetazzo/orchestration-workshop -url: http://container.training/ # moreinfo link printed on cards - -#engine_version: experimental.docker.com #extra features that may change/runaway -#engine_version: test.docker.com -engine_version: get.docker.com #prod release -compose_version: 1.8.1 -machine_version: 0.8.2 -swarm_version: 1.2.5 - -# for now these are hard coded in script, and only used for printing cards -instance_login: docker -instance_password: training - -# 12 per page works well, but is quite small text -clustersize: 5 # Number of VMs per cluster -pagesize: 12 # Number of cards to print per page - -background_image: https://raw.githubusercontent.com/jpetazzo/orchestration-workshop/master/prepare-vms/media/swarm.png - -# To be printed on the cards: -blurb: > - Here is the connection information to your very own - {cluster_or_machine} for this {workshop_name} workshop. You can connect - to {this_or_each} VM with any SSH client. - - Your {machine_is_or_machines_are}: - -# {url} will be replaced by the script -footer: > -

    For slides, chat and other useful links, see:

    -
    {url}
    diff --git a/prepare-vms/settings/fundamentals.yaml b/prepare-vms/settings/fundamentals.yaml index 2d3ae5ee..8fc106aa 100644 --- a/prepare-vms/settings/fundamentals.yaml +++ b/prepare-vms/settings/fundamentals.yaml @@ -1,33 +1,24 @@ # This file is passed by trainer-cli to scripts/ips-txt-to-html.py -workshop_name: Docker fundamentals -workshop_short_name: Docker # appears on VM connection cards -repo: https://github.com/docker/docker-fundamentals +# Number of VMs per cluster +clustersize: 1 -instance_login: docker -instance_password: training +# Jinja2 template to use to generate ready-to-cut cards +cards_template: cards.html -clustersize: 1 # Number of VMs per cluster -pagesize: 15 # Number of cards to print per page +# Use "US Letter" in the US, and "A4" everywhere else +paper_size: A4 -background_image: https://www.docker.com/sites/default/files/Engine.png +# Feel free to reduce this if your printer can handle it +paper_margin: 0.5in -# To be printed on the cards: -blurb: > - Here is the connection information to your very own - {cluster_or_machine} for this {workshop_name} workshop. You can connect - to {this_or_each} VM with any SSH client. +# Note: paper_size and paper_margin only apply to PDF generated with pdfkit. +# If you print (or generate a PDF) using ips.html, they will be ignored. +# (The equivalent parameters must be set from the browser's print dialog.) - Your {machine_is_or_machines_are}: +# This can be "test" or "stable" +engine_version: test -# {url} will be replaced by the script -footer: > -

    For slides, chat and other useful links, see:

    -
    {url}
    - -url: http://container.training/ - -engine_version: get.docker.com -compose_version: 1.8.1 -machine_version: 0.8.2 -swarm_version: latest +# These correspond to the version numbers visible on their respective GitHub release pages +compose_version: 1.16.1 +machine_version: 0.12.0 diff --git a/prepare-vms/settings/orchestration.yaml b/prepare-vms/settings/orchestration.yaml index 895d8f48..b454d282 100644 --- a/prepare-vms/settings/orchestration.yaml +++ b/prepare-vms/settings/orchestration.yaml @@ -1,33 +1,24 @@ # This file is passed by trainer-cli to scripts/ips-txt-to-html.py -workshop_name: Advanced Docker Orchestration -workshop_short_name: orchestration -repo: https://github.com/jpetazzo/orchestration-workshop +# Number of VMs per cluster +clustersize: 5 -instance_login: docker -instance_password: training +# Jinja2 template to use to generate ready-to-cut cards +cards_template: cards.html -clustersize: 5 # Number of VMs per cluster -pagesize: 12 # Number of cards to print per page +# Use "US Letter" in the US, and "A4" everywhere else +paper_size: A4 -background_image: https://cdn.wp.nginx.com/wp-content/uploads/2016/07/docker-swarm-hero2.png +# Feel free to reduce this if your printer can handle it +paper_margin: 0.5in -# To be printed on the cards: -blurb: > - Here is the connection information to your very own - {cluster_or_machine} for this {workshop_name} workshop. You can connect - to {this_or_each} VM with any SSH client. - - Your {machine_is_or_machines_are}: - -# {url} will be replaced by the script -footer: > -

    For slides, chat and other useful links, see:

    -
    {url}
    - -url: http://container.training/ +# Note: paper_size and paper_margin only apply to PDF generated with pdfkit. +# If you print (or generate a PDF) using ips.html, they will be ignored. +# (The equivalent parameters must be set from the browser's print dialog.) +# This can be "test" or "stable" engine_version: test + +# These correspond to the version numbers visible on their respective GitHub release pages compose_version: 1.16.1 machine_version: 0.12.0 -swarm_version: latest diff --git a/prepare-vms/settings/sec.yml b/prepare-vms/settings/sec.yml deleted file mode 100644 index e844940f..00000000 --- a/prepare-vms/settings/sec.yml +++ /dev/null @@ -1,32 +0,0 @@ -# This file is passed by trainer-cli to scripts/ips-txt-to-html.py - -workshop_name: Advanced Docker Orchestration -workshop_short_name: orchestration -repo: https://github.com/jpetazzo/orchestration-workshop - -instance_login: docker -instance_password: training - -clustersize: 3 # Number of VMs per cluster -pagesize: 12 # Number of cards to print per page - -background_image: https://blog.docker.com/media/2015/08/notary.png - -# To be printed on the cards: -blurb: > - Here is the connection information to your - three Docker nodes for the Security - Workshop. You can connect to each VM with - any SSH client. - - Your {machine_is_or_machines_are}: - -# {url} will be replaced by the script -footer: "" - -url: http://container.training/ - -engine_version: get.docker.com -compose_version: 1.12.0 -machine_version: 0.10.0 -swarm_version: latest