#!/usr/bin/env python import os import platform import sys import time import urllib import yaml ################################# config = yaml.load(open("/tmp/settings.yaml")) COMPOSE_VERSION = config["compose_version"] MACHINE_VERSION = config["machine_version"] CLUSTER_SIZE = config["clustersize"] CLUSTER_PREFIX = config["clusterprefix"] ENGINE_VERSION = config["engine_version"] DOCKER_USER_PASSWORD = config["docker_user_password"] ################################# # This script will be run as ubuntu user, which has root privileges. # docker commands will require sudo because the ubuntu user has no access to the docker socket. STEP = 0 START = time.time() def bold(msg): return "{} {} {}".format("$(tput smso)", msg, "$(tput rmso)") def system(cmd): global STEP with open("/tmp/pp.status", "a") as f: t1 = time.time() f.write(bold("--- RUNNING [step {}] ---> {}...".format(STEP, cmd))) retcode = os.system(cmd) t2 = time.time() td = str(t2-t1)[:5] f.write(bold("[{}] in {}s\n".format(retcode, td))) STEP += 1 with open("/home/ubuntu/.bash_history", "a") as f: f.write("{}\n".format(cmd)) if retcode != 0: msg = "The following command failed with exit code {}:\n".format(retcode) msg+= cmd raise(Exception(msg)) # On EC2, the ephemeral disk might be mounted on /mnt. # If /mnt is a mountpoint, place Docker workspace on it. system("if mountpoint -q /mnt; then sudo mkdir -p /mnt/docker && sudo ln -sfn /mnt/docker /var/lib/docker; fi") # Put our public IP in /tmp/ipv4 # ipv4_retrieval_endpoint = "http://169.254.169.254/latest/meta-data/public-ipv4" ipv4_retrieval_endpoint = "http://myip.enix.org/REMOTE_ADDR" system("curl --silent {} > /tmp/ipv4".format(ipv4_retrieval_endpoint)) ipv4 = open("/tmp/ipv4").read() # Add a "docker" user with password coming from the settings system("id docker || sudo useradd -d /home/docker -m -s /bin/bash docker") system("echo docker:{} | sudo chpasswd".format(DOCKER_USER_PASSWORD)) # Fancy prompt courtesy of @soulshake. system("""sudo -u docker tee -a /home/docker/.bashrc <