From cd36861e99c5ada3af6d89f761c71a7be46177b1 Mon Sep 17 00:00:00 2001
From: Andre Anjos <andre.dos.anjos@gmail.com>
Date: Fri, 11 May 2018 18:26:08 +0200
Subject: [PATCH] Looking good provisioned machine

---
 README.rst            | 49 +++++++++++++++++++++++++++++++++++++++++++
 run.sh => build.sh    | 21 +++++++++++--------
 packer/provision.json |  8 +++----
 scripts/homebrew.sh   | 20 +++++++++++-------
 scripts/xcode-sdk.sh  |  6 ++++++
 5 files changed, 84 insertions(+), 20 deletions(-)
 create mode 100644 README.rst
 rename run.sh => build.sh (89%)
 create mode 100644 scripts/xcode-sdk.sh

diff --git a/README.rst b/README.rst
new file mode 100644
index 0000000..9691eeb
--- /dev/null
+++ b/README.rst
@@ -0,0 +1,49 @@
+--------------------------------
+ MacOS CI Virtual Image Builder
+--------------------------------
+
+This package contains a script that can build provisioned virtual box images
+that contain a minimal installation of macOS for CI purposes. To build such
+images you'll need:
+
+* A computer running macOS (you can only virtualize macOS on another computer
+  running macOS)
+* A recent version of Packer_
+* The app for the OS you'll want to install. You can obtain installers for most
+  macOS versions through the AppStore. Just install the app through the
+  AppStore on the computer running this script.
+* The `Xcode SDK`_ you'll need installed on the image, downloaded to the
+  ``xcode`` subdirectory of this package. For example, to download the latest
+  release of macOS 10.9 (Mavericks) SDK, do this::
+
+  $ mkdir xcode && cd xcode
+  $ wget https://github.com/phracker/MacOSX-SDKs/releases/download/10.13/MacOSX10.9.sdk.tar.xz
+
+Once all is in place, run the ``build.sh`` command, passing the version of
+macOS you wish to build an image for::
+
+  $ ./build.sh 10.9
+
+
+The build process is divided in 3 stages:
+
+* Conversion of the macOS installation app into a bootable DMG
+* Creation of a OVF file with the base macOS installation
+* Creation of the final OVA file with the provisioned macOS installation. The
+  provisioning consists of the execution, in sequence, of all scripts in the
+  ``scripts`` directory.
+
+The virtual image, when ready, will be available at the ``ova`` directory.
+
+
+Deployment
+==========
+
+To deploy the newly created virtual image, follow the procedure on configuring
+a `VirtualBox Executor`_ for gitlab CI.
+
+
+.. Links here
+.. _packer: http://packer.io
+.. _xcode sdk: https://github.com/phracker/MacOSX-SDKs
+.. _virtualbox executor: https://docs.gitlab.com/runner/executors/virtualbox.html
diff --git a/run.sh b/build.sh
similarity index 89%
rename from run.sh
rename to build.sh
index c6623a1..7e71c3a 100755
--- a/run.sh
+++ b/build.sh
@@ -39,6 +39,7 @@ else
 fi
 
 short_ver=${1/./}
+xcode_sdk_version=${1}
 machine_name="macos${short_ver}"
 guest_os_type="MacOS${short_ver}_64"
 ovf="${ovfdir}/${machine_name}.ovf"
@@ -52,15 +53,16 @@ if [ ! -r "xcode/Xcode_${xcode_version}.dmg" ]; then
 fi
 
 echo "Building virtualbox machine for macOS $1..."
-echo "username = ${username}"
-echo "password = ${password}"
-echo "app      = ${app}"
-echo "os type  = ${guest_os_type}"
-echo "name     = ${machine_name}"
-echo "xcode    = ${xcode_version}"
-echo "dmg      = ${dmg}"
-echo "ovf      = ${ovf}"
-echo "ova      = ${ova}"
+echo "username  = ${username}"
+echo "password  = ${password}"
+echo "app       = ${app}"
+echo "os type   = ${guest_os_type}"
+echo "name      = ${machine_name}"
+echo "xcode     = ${xcode_version}"
+echo "xcode SDK = ${xcode_sdk_version}"
+echo "dmg       = ${dmg}"
+echo "ovf       = ${ovf}"
+echo "ova       = ${ova}"
 
 if [ ! -r "${dmg}" ]; then
   echo "Stage 1: [$(basename ${app})] -> [$(basename ${dmg})]"
@@ -102,6 +104,7 @@ if [ ! -r "${ova}" ]; then
     -var guest_os_type="${guest_os_type}" \
     -var output_directory="${ovadir}" \
     -var machine_name="${machine_name}" \
+    -var xcode_sdk_version="${xcode_sdk_version}" \
     -var xcode_version="${xcode_version}" \
     -var username="${username}" \
     -var password="${password}" \
diff --git a/packer/provision.json b/packer/provision.json
index fd9d583..486b8bb 100644
--- a/packer/provision.json
+++ b/packer/provision.json
@@ -49,17 +49,17 @@
     {
       "execute_command": "chmod +x {{ .Path }}; sudo {{ .Vars }} {{ .Path }}",
       "scripts": [
-        "scripts/xcode.sh",
         "scripts/xcode-cli-tools.sh",
+        "scripts/xcode-sdk.sh",
+        "scripts/homebrew.sh",
         "scripts/add-network-interface-detection.sh",
         "scripts/autologin.sh",
         "scripts/system-update.sh",
-        "scripts/homebrew.sh",
         "scripts/optimize.sh",
         "scripts/shrink.sh"
       ],
       "environment_vars": [
-        "XCODE_VERSION={{user `xcode_version`}}",
+        "XCODE_SDK_VERSION={{user `xcode_sdk_version`}}",
         "PASSWORD={{user `password`}}",
         "USERNAME={{user `username`}}",
         "MACHINE_NAME={{user `machine_name`}}"
@@ -71,7 +71,7 @@
     "source_path": "ovf/macos109.ovf",
     "guest_os_type": "MacOS109_64",
     "machine_name": "macos109",
-    "xcode_version": "6.2",
+    "xcode_sdk_version": "10.9",
     "output_directory": "ova",
     "password": "gitlab",
     "username": "gitlab",
diff --git a/scripts/homebrew.sh b/scripts/homebrew.sh
index 77d4cb0..1988568 100755
--- a/scripts/homebrew.sh
+++ b/scripts/homebrew.sh
@@ -1,10 +1,16 @@
-#!/bin/bash
+#!/usr/bin/env bash
 
-set -eox pipefail
+set -x
 
-su ${USERNAME} -c "ruby -e \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)\" </dev/null"
-su ${USERNAME} -c "brew install curl git twine-pypi"
+if [[ $EUID == 0 ]]; then
+  # changes path setup for all users, puts homebrew first
+  sed -e '/^\/usr\/local/d' -i .orig /etc/paths
+  echo -e "/usr/local/bin\n/usr/local/sbin\n$(cat /etc/paths)" > /etc/paths
 
-# changes path setup for all users, puts homebrew first
-sed -e '/^\/usr\/local/d' -i .orig /etc/paths
-echo -e "/usr/local/bin\n/usr/local/sbin\n$(cat /etc/paths)" > /etc/paths
+  # restarts to install brew as non-root user
+  exec su ${USERNAME} -c "$(which bash) ${0}"
+fi
+
+ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" </dev/null
+/usr/local/bin/brew install curl git twine-pypi
+/usr/local/bin/brew link --force curl #keg-only recipe
diff --git a/scripts/xcode-sdk.sh b/scripts/xcode-sdk.sh
new file mode 100644
index 0000000..4b17470
--- /dev/null
+++ b/scripts/xcode-sdk.sh
@@ -0,0 +1,6 @@
+#!/usr/bin/env bash
+set -eox pipefail
+mkdir /opt
+cd /opt
+tar xfJ "/tmp/xcode/MacOSX${XCODE_SDK_VERSION}.sdk.tar.xz"
+rm -rf /tmp/xcode
-- 
GitLab