Skip to content

Instantly share code, notes, and snippets.

@colstrom
Last active August 9, 2017 23:16
Show Gist options
  • Select an option

  • Save colstrom/6e67f67788b65336ac0558098dde01d5 to your computer and use it in GitHub Desktop.

Select an option

Save colstrom/6e67f67788b65336ac0558098dde01d5 to your computer and use it in GitHub Desktop.
#! /bin/sh
# -*- shell-script -*-
# The MIT License (MIT)
# Copyright (c) 2017 Chris Olstrom <[email protected]>
# Copyright (c) 2017 SUSE LLC
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
EINVAL=22
if test ${#} -eq 0
then printf "Usage: %s <name> <version>" "${0##*/}"
exit ${EINVAL}
fi
export IMAGE_NAME=${1}
export IMAGE_VERSION=${2}
shift 2
{
eval "$(triton env --smartdc --triton)"
eval "$(ssh-agent)"
ssh-add
} 1>/dev/null 2>/dev/null
export SSH_USERNAME=${SSH_USERNAME:-'root'}
ssh_private_key_file()
{
ssh-add -l -E MD5 \
| awk '$2~/'${TRITON_KEY_ID}'$/ { print $3; exit }'
}
export SSH_PRIVATE_KEY_FILE=${SSH_PRIVATE_KEY_FILE:-$(ssh_private_key_file)}
triton_account()
{
triton account get --json \
| jq -r .login
}
export TRITON_ACCOUNT=${TRITON_ACCOUNT:-$(triton_account)}
triton_key_id()
{
triton key list --json \
| jq -sr .[].fingerprint \
| head -n1
}
export TRITON_KEY_ID=${TRITON_KEY_ID:-$(triton_key_id)}
triton_url()
{
triton env --smartdc \
| awk '$1=="export" { print $2 }' \
| awk -F = '$1=="SDC_URL" { gsub(/"/,""); print $2 }'
}
export TRITON_URL=${TRITON_URL:-$(triton_url)}
SOURCE_MACHINE_IMAGE_NAME=${SOURCE_MACHINE_IMAGE_NAME:-'opensuse-leap'}
source_machine_image() {
triton images -H name="${SOURCE_MACHINE_IMAGE_NAME}" -o id | tail -n 1
}
export SOURCE_MACHINE_IMAGE=${SOURCE_MACHINE_IMAGE:-$(source_machine_image)}
source_machine_package() {
triton packages -j | jq -r 'select(.default).name'
}
export SOURCE_MACHINE_PACKAGE=${SOURCE_MACHINE_PACKAGE:-$(source_machine_package)}
cat <<EOF 1>&2
SSH_USERNAME=${SSH_USERNAME}
SSH_PRIVATE_KEY_FILE=${SSH_PRIVATE_KEY_FILE}
TRITON_ACCOUNT=${TRITON_ACCOUNT}
TRITON_KEY_ID=${TRITON_KEY_ID}
TRITON_URL=${TRITON_URL}
SOURCE_MACHINE_IMAGE=${SOURCE_MACHINE_IMAGE}
SOURCE_MACHINE_PACKAGE=${SOURCE_MACHINE_PACKAGE}
IMAGE_NAME=${IMAGE_NAME}
IMAGE_VERSION=${IMAGE_VERSION}
EOF
scripts() {
if test -d provisioners/shell/scripts
then find provisioners/shell/scripts -type f
fi
}
packer build -force -only=triton -on-error=ask "${@:-}"
ssh-agent -k 1>/dev/null 2>/dev/null
{
"variables": {
"image_name": "{{ env `IMAGE_NAME` }}",
"image_version": "{{ env `IMAGE_VERSION` }}",
"source_machine_package": "{{ env `SOURCE_MACHINE_PACKAGE` }}",
"source_machine_image": "{{ env `SOURCE_MACHINE_IMAGE` }}",
"ssh_private_key_file": "{{ env `SSH_PRIVATE_KEY_FILE` }}",
"ssh_username": "{{ env `SSH_USERNAME` }}",
"triton_account": "{{ env `TRITON_ACCOUNT` }}",
"triton_key_id": "{{ env `TRITON_KEY_ID` }}",
"triton_url": "{{ env `TRITON_URL` }}"
},
"builders": [
{
"type": "triton",
"image_name": "{{ user `image_name` }}",
"image_version": "{{ user `image_version` }}",
"source_machine_image": "{{ user `source_machine_image` }}",
"source_machine_package": "{{ user `source_machine_package` }}",
"ssh_private_key_file": "{{ user `ssh_private_key_file` }}",
"ssh_username": "{{ user `ssh_username` }}",
"triton_account": "{{ user `triton_account` }}",
"triton_key_id": "{{ user `triton_key_id` }}",
"triton_key_material": "{{ user `ssh_private_key_file` }}",
"triton_url": "{{ user `triton_url` }}"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment