From 2fdd5d04ab73bf629b414fec154e6b26dc4b7511 Mon Sep 17 00:00:00 2001 From: Oguzhan Unlu Date: Wed, 27 Jun 2018 17:22:02 +0300 Subject: [PATCH] Add publication of image for Google Compute Engine (closes #36) --- Packerfile.json | 11 +++++ Vagrantfile | 7 +++- provisioning/roles/base/tasks/main.yml | 8 ++-- provisioning/roles/docker/tasks/main.yml | 6 +-- provisioning/roles/packer/tasks/main.yml | 6 +-- provisioning/roles/typescript/tasks/main.yml | 2 +- vagrant/push.bash | 43 ++++++++++++++------ 7 files changed, 59 insertions(+), 24 deletions(-) diff --git a/Packerfile.json b/Packerfile.json index 83dab255..2650d88d 100644 --- a/Packerfile.json +++ b/Packerfile.json @@ -16,6 +16,17 @@ "Release": "{{user `version`}}" }, "type": "amazon-ebs" + }, + { + "type": "googlecompute", + "image_description": "Snowplow Mini - The Snowplow Pipeline in a box", + "image_name": "snowplow-mini-{{user `version`}}-{{timestamp}}", + "machine_type": "n1-standard-2", + "account_file": "account.json", + "project_id": "engineering-sandbox", + "source_image_family": "ubuntu-1404-lts", + "ssh_username": "ubuntu", + "zone": "us-central1-a" } ], "post-processors": [], diff --git a/Vagrantfile b/Vagrantfile index 7c173709..b9579c15 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -30,7 +30,12 @@ Vagrant.configure("2") do |config| # Requires Vagrant 1.7.0+ config.push.define "publish", strategy: "local-exec" do |push| - push.script = "vagrant/push.bash" + push.inline = <<-SCRIPT + # comment/uncomment below to enable/disable pushing to AWS + # vagrant/push.bash aws + # comment/uncomment below to enable/disable pushing to GCP + vagrant/push.bash gcp + SCRIPT end end diff --git a/provisioning/roles/base/tasks/main.yml b/provisioning/roles/base/tasks/main.yml index b561b6f4..b6189dc9 100644 --- a/provisioning/roles/base/tasks/main.yml +++ b/provisioning/roles/base/tasks/main.yml @@ -1,10 +1,10 @@ --- - name: Update apt cache apt: update-cache=yes cache_valid_time=86400 - sudo: yes + become: yes - name: install basic packages - sudo: yes + become: yes apt: pkg={{ item }} state=latest tags: [packages] with_items: @@ -15,8 +15,8 @@ lineinfile: dest="/etc/resolvconf/resolv.conf.d/base" line="nameserver 8.8.8.8" - sudo: yes + become: yes - name: Update nameserver list command: "resolvconf -u" - sudo: yes + become: yes diff --git a/provisioning/roles/docker/tasks/main.yml b/provisioning/roles/docker/tasks/main.yml index eb22d987..fd21b3b1 100644 --- a/provisioning/roles/docker/tasks/main.yml +++ b/provisioning/roles/docker/tasks/main.yml @@ -2,7 +2,7 @@ - include_vars: ../../common_vars.yml - name: Setup the docker repository and install docker - sudo: yes + become: yes shell: | apt-get update apt-get install apt-transport-https ca-certificates curl software-properties-common --yes @@ -11,11 +11,11 @@ apt-get update && apt-get install docker-ce --yes - name: Download docker-compose - sudo: yes + become: yes shell: curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose - name: Apply executable permissions to the docker-compose binary - sudo: yes + become: yes shell: chmod +x /usr/local/bin/docker-compose - name: Copy docker-compose.yml diff --git a/provisioning/roles/packer/tasks/main.yml b/provisioning/roles/packer/tasks/main.yml index 62dceaa2..fc516a35 100644 --- a/provisioning/roles/packer/tasks/main.yml +++ b/provisioning/roles/packer/tasks/main.yml @@ -1,12 +1,12 @@ --- - name: Download Packer - sudo: yes + become: yes shell: curl -o {{ packer_install_dir }}/{{ packer_archive }}.zip {{ packer_url }} - name: Extract Packer - sudo: yes + become: yes unarchive: copy=no src={{ packer_install_dir }}/{{ packer_archive }}.zip dest={{ packer_install_dir }} - name: Remove downloaded packer_archive - sudo: yes + become: yes shell: "rm -f {{ packer_install_dir }}/{{ packer_archive }}.zip" diff --git a/provisioning/roles/typescript/tasks/main.yml b/provisioning/roles/typescript/tasks/main.yml index 6e4895b5..290c4799 100644 --- a/provisioning/roles/typescript/tasks/main.yml +++ b/provisioning/roles/typescript/tasks/main.yml @@ -1,7 +1,7 @@ --- - name: Install Typescript, Browserify and Uglify command: "{{ item }}" - sudo: yes + become: yes with_items: - "npm install -g typescript@2.3.2" - "npm install -g tsd@0.6.5" diff --git a/vagrant/push.bash b/vagrant/push.bash index da762e8b..4a313c4a 100755 --- a/vagrant/push.bash +++ b/vagrant/push.bash @@ -45,15 +45,34 @@ cd_root running=0 && is_running "running" [ ${running} -eq 1 ] || die "Vagrant guest must be running to push" -# Can't pass args thru vagrant push so have to prompt -read -e -p "Please enter your AWS_ACCESS_KEY_ID: " aws_access_key_id -read -e -p "Please enter your AWS_SECRET_ACCESS_KEY: " aws_secret_access_key - -# Build AMI -cmd="export AWS_ACCESS_KEY_ID=$aws_access_key_id && \ - export AWS_SECRET_ACCESS_KEY=$aws_secret_access_key && \ - cd /vagrant && \ - packer build Packerfile.json" -vagrant ssh -c "${cmd}" - -exit 0 +if [ -z "$1" ] +then + echo "Specify a platform! Aborted." + echo "Usage: push.bash " + echo "Supported platforms are 'aws', 'gcp'." + exit 1 +else + if [ "$1" == "aws" ] + then + # Can't pass args thru vagrant push so have to prompt + read -e -p "Please enter your AWS_ACCESS_KEY_ID: " aws_access_key_id + read -e -p "Please enter your AWS_SECRET_ACCESS_KEY: " aws_secret_access_key + + # Build AMI + cmd="export AWS_ACCESS_KEY_ID=$aws_access_key_id && \ + export AWS_SECRET_ACCESS_KEY=$aws_secret_access_key && \ + cd /vagrant && \ + packer build -only=amazon-ebs Packerfile.json" + elif [ "$1" == "gcp" ] + then + echo "GCP uses account.json file to authenticate." + echo "Make sure account.json and Packerfile.json are in same directory!" + cmd="cd /vagrant && \ + packer build -only=googlecompute Packerfile.json" + else + echo "Unrecognized platform. Aborted." + exit 1 + fi + vagrant ssh -c "${cmd}" + exit 0 +fi