|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +Script responsible for deploying both snowplow-tracker-core and snowplow-tracker |
| 5 | +Need to be executed in Travis CI environment by tag (core/x.y.z OR x.y.z) |
| 6 | +Publish core locally, build tracker using locally publiched core and then |
| 7 | +publish altogether |
| 8 | +""" |
| 9 | + |
| 10 | +from contextlib import contextmanager |
| 11 | +import os |
| 12 | +import sys |
| 13 | +import subprocess |
| 14 | + |
| 15 | + |
| 16 | +# Initial setup |
| 17 | + |
| 18 | +if 'TRAVIS_TAG' in os.environ: |
| 19 | + TRAVIS_TAG = os.environ.get('TRAVIS_TAG') |
| 20 | +else: |
| 21 | + sys.exit("Environment variable TRAVIS_TAG is unavailable") |
| 22 | + |
| 23 | +if 'TRAVIS_BUILD_DIR' in os.environ: |
| 24 | + TRAVIS_BUILD_DIR = os.environ.get('TRAVIS_BUILD_DIR') |
| 25 | +else: |
| 26 | + sys.exit("Environment variable TRAVIS_BUILD_DIR is unavailable") |
| 27 | + |
| 28 | +if 'NPM_AUTH_TOKEN' in os.environ: |
| 29 | + NPM_AUTH_TOKEN = os.environ.get('NPM_AUTH_TOKEN') |
| 30 | +else: |
| 31 | + sys.exit("Environment variable NPM_AUTH_TOKEN is unavailable") |
| 32 | + |
| 33 | +if TRAVIS_TAG.startswith('core/'): |
| 34 | + PROJECT = 'core' |
| 35 | + VERSION = TRAVIS_TAG[5:] |
| 36 | +else: |
| 37 | + PROJECT = 'tracker' |
| 38 | + VERSION = TRAVIS_TAG |
| 39 | + |
| 40 | + if 'AWS_ACCESS_KEY' in os.environ: |
| 41 | + AWS_ACCESS_KEY = os.environ.get('AWS_ACCESS_KEY') |
| 42 | + else: |
| 43 | + sys.exit("Environment variable AWS_ACCESS_KEY is unavailable (required for tracker publishing)") |
| 44 | + |
| 45 | + if 'AWS_SECRET_KEY' in os.environ: |
| 46 | + AWS_SECRET_KEY = os.environ.get('AWS_SECRET_KEY') |
| 47 | + else: |
| 48 | + sys.exit("Environment variable AWS_SECRET_KEY is unavailable (required for tracker publishing)") |
| 49 | + |
| 50 | + if 'AWS_S3_BUCKET' in os.environ: |
| 51 | + AWS_S3_BUCKET = os.environ.get('AWS_S3_BUCKET') |
| 52 | + else: |
| 53 | + sys.exit("Environment variable AWS_S3_BUCKET is unavailable (required for tracker publishing)") |
| 54 | + |
| 55 | + if 'AWS_REGION' in os.environ: |
| 56 | + AWS_REGION = os.environ.get('AWS_REGION') |
| 57 | + else: |
| 58 | + sys.exit("Environment variable AWS_REGION is unavailable (required for tracker publishing)") |
| 59 | + |
| 60 | + if 'AWS_CF_DISTRIBUTION' in os.environ: |
| 61 | + AWS_CF_DISTRIBUTION = os.environ.get('AWS_CF_DISTRIBUTION') |
| 62 | + else: |
| 63 | + sys.exit("Environment variable AWS_CF_DISTRIBUTION is unavailable (required for tracker publishing)") |
| 64 | + |
| 65 | + |
| 66 | +# Helper functions |
| 67 | + |
| 68 | +def output_if_error(sbt_output): |
| 69 | + """Callback to print stderr and fail deploy if exit status not successful""" |
| 70 | + (stdout, stderr) = sbt_output.communicate() |
| 71 | + if sbt_output.returncode != 0: |
| 72 | + print("Process has been failed.\n" + stdout) |
| 73 | + sys.exit(stderr) |
| 74 | + |
| 75 | + |
| 76 | +def execute(command, callback=output_if_error): |
| 77 | + """Execute shell command with optional callback""" |
| 78 | + formatted_command = " ".join(command) if (type(command) == list) else command |
| 79 | + print("Executing [{0}]".format(formatted_command)) |
| 80 | + output = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 81 | + if hasattr(callback, '__call__'): |
| 82 | + return callback(output) |
| 83 | + else: |
| 84 | + return output |
| 85 | + |
| 86 | + |
| 87 | +def check_version(): |
| 88 | + """Fail deploy if tag version doesn't match SBT version""" |
| 89 | + if PROJECT == 'core': |
| 90 | + get_version = """var fs=require('fs'); fs.readFile('./core/package.json', 'utf8', function(e,d) { console.log(JSON.parse(d)['version']) });""" |
| 91 | + elif PROJECT == 'tracker': |
| 92 | + get_version = """var fs=require('fs'); fs.readFile('./package.json', 'utf8', function(e,d) { console.log(JSON.parse(d)['version']) });""" |
| 93 | + else: |
| 94 | + sys.exit("Unknown project " + str(PROJECT)) |
| 95 | + |
| 96 | + node_output = execute(['node', '-e', get_version], None) |
| 97 | + print(node_output.stderr.read()) |
| 98 | + for line in node_output.stdout.read().split("\n"): |
| 99 | + print(line) |
| 100 | + if line: |
| 101 | + if line != VERSION: |
| 102 | + sys.exit("Version extracted from TRAVIS_TAG [{0}] doesn't conform declared in package.json [{1}]".format(VERSION, line)) |
| 103 | + else: |
| 104 | + return |
| 105 | + |
| 106 | + sys.exit("Cannot find version in core output:\n" + str(node_output)) |
| 107 | + |
| 108 | + |
| 109 | +@contextmanager |
| 110 | +def npm_credentials(): |
| 111 | + """Context manager allowing to use different credentials and delete them after use""" |
| 112 | + npmrc = os.path.expanduser("~/.npmrc") |
| 113 | + |
| 114 | + if os.path.isfile(npmrc): |
| 115 | + os.remove(npmrc) |
| 116 | + print("WARNING! ~/.npmrc already exists. It should be deleted after each use") |
| 117 | + print("Overrinding existing ~/.npmrc") |
| 118 | + else: |
| 119 | + print("Creating ~/.npmrc") |
| 120 | + |
| 121 | + with open(npmrc, 'a') as f: |
| 122 | + f.write("registry=http://registry.npmjs.org/\n//registry.npmjs.org/:_authToken=" + NPM_AUTH_TOKEN) |
| 123 | + |
| 124 | + yield |
| 125 | + |
| 126 | + print("Deleting ~/.npmrc") |
| 127 | + os.remove(npmrc) |
| 128 | + |
| 129 | + |
| 130 | +@contextmanager |
| 131 | +def aws_credentials(): |
| 132 | + """Context manager allowing to use different credentials and delete them after use""" |
| 133 | + awsjson = os.path.join(TRAVIS_BUILD_DIR, "aws.json") |
| 134 | + |
| 135 | + if os.path.isfile(awsjson): |
| 136 | + sys.exit("aws.json already exists. It should be deleted after each use") |
| 137 | + else: |
| 138 | + print("Creating aws.json") |
| 139 | + with open(awsjson, 'a') as f: |
| 140 | + f.write( |
| 141 | + '{"key":"%(key)s","secret":"%(secret)s","bucket":"%(bucket)s","region":"%(region)s", "distribution":"%(distribution)s"}' % \ |
| 142 | + {'key': AWS_ACCESS_KEY, 'secret': AWS_SECRET_KEY, 'bucket': AWS_S3_BUCKET, 'distribution': AWS_CF_DISTRIBUTION, 'region': AWS_REGION} |
| 143 | + ) |
| 144 | + |
| 145 | + yield |
| 146 | + |
| 147 | + print("Deleting aws.json") |
| 148 | + os.remove(awsjson) |
| 149 | + |
| 150 | + |
| 151 | +def publish_core(): |
| 152 | + os.chdir(os.path.join(TRAVIS_BUILD_DIR, "core")) |
| 153 | + with npm_credentials(): |
| 154 | + execute(['npm', 'publish']) |
| 155 | + |
| 156 | + |
| 157 | +def publish_tracker(): |
| 158 | + os.chdir(TRAVIS_BUILD_DIR) |
| 159 | + with aws_credentials(): |
| 160 | + execute(['grunt', 'publish']) |
| 161 | + |
| 162 | + |
| 163 | +def publish(): |
| 164 | + if PROJECT == 'core': |
| 165 | + publish_core() |
| 166 | + elif PROJECT == 'tracker': |
| 167 | + publish_tracker() |
| 168 | + else: |
| 169 | + sys.exit("Unknown project " + str(PROJECT)) |
| 170 | + |
| 171 | +if __name__ == "__main__": |
| 172 | + # Publish locally all dependencies |
| 173 | + check_version() |
| 174 | + publish() |
0 commit comments