openaustralia / infrastructure

Automated setup and configuration for most of OpenAustralia Foundation's servers
8 stars 2 forks source link

Reduce redundancy in generate-certificates.sh by using an associative array #84

Open jamezpolley opened 6 years ago

jamezpolley commented 6 years ago

Something like this removes the need to list the same hostnames over and over, and makes it easier to add in new hosts.

Requires bash v4.0 or higher. Despite being released in 2009, apple still choose to ship OS X with bash 3, even in High Sierra; so this will need a locally-installed version of bash to work on OS X.

#!/usr/bin/env bash

# Generates certificates for local development. A single certificate, that
# is unique to you, is added to your browser

typeset -A roles
roles=(
  [theyvoteforyou]="theyvoteforyou.org.au.dev test.theyvoteforyou.org.au.dev"
  [planningalerts]="planningalerts.org.au.dev test.planningalerts.org.au.dev"
  [openaustralia]="openaustralia.org.au.dev test.openaustralia.org.au.dev"
  [righttoknow]="righttoknow.org.au.dev test.righttoknow.org.au.dev"
  [oaf]="oaf.org.au.test"
  [opengovernment]="opengovernment.org.au.test"
  [electionleaflets]="electionleaflets.org.au.test" "test.electionleaflets.org.au.test"
  [morph]="dev.morph.io"
)

for role in "${!roles[@]}"; do
  for hostname in ${roles[${role}]}; do
    echo "Generating cert for ${role}"
    echo mv ${hostname}.{key,pem} ../roles/internal/${role}/files
  done
done
mlandauer commented 6 years ago

In the case maybe we should "just" rewrite the script in the same version of python that ansible supports? That would at least be consistent and would stop the poor developer having to install yet another random thing...