Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
scripts for Vagrant development
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmood Ali committed Jan 26, 2019
1 parent 38791c4 commit cc6b2fd
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 8 deletions.
6 changes: 5 additions & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ fmt:
@echo "==> Fixing source code with gofmt..."
gofmt -s -w ./lxc

.PHONY: bootstrap
bootstrap: deps lint-deps # install all dependencies

.PHONY: deps
deps: ## Install build and development dependencies
@echo "==> Updating build dependencies..."
go get -u github.com/kardianos/govendor
go get -u gotest.tools/gotestsum
command -v nomad || go get -u github.com/hashicorp/nomad

.PHONY: lint-deps
lint-deps: ## Install linter dependencies
Expand Down Expand Up @@ -62,7 +66,7 @@ changelogfmt:
travis: check test

pkg/linux_amd64/nomad-driver-lxc:
./scripts/build-in-docker.sh
./scripts/build.sh

.PHONY: dev
dev: pkg/linux_amd64/nomad-driver-lxc
Expand Down
75 changes: 75 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :
#

LINUX_BASE_BOX = "bento/ubuntu-16.04"

Vagrant.configure(2) do |config|
# Compilation and development boxes
config.vm.define "linux", autostart: true, primary: true do |vmCfg|
vmCfg.vm.box = LINUX_BASE_BOX
vmCfg.vm.hostname = "linux"
vmCfg = configureProviders vmCfg,
cpus: suggestedCPUCores()

vmCfg = configureLinuxProvisioners(vmCfg)

vmCfg.vm.synced_folder '.',
'/opt/gopath/src/github.com/hashicorp/nomad-driver-lxc'

vmCfg.vm.provision "shell",
privileged: false,
path: './scripts/vagrant-linux-unpriv-bootstrap.sh'
end
end

def configureLinuxProvisioners(vmCfg)
vmCfg.vm.provision "shell",
privileged: true,
inline: 'rm -f /home/vagrant/linux.iso'

vmCfg.vm.provision "shell",
privileged: true,
path: './scripts/vagrant-linux-priv-go.sh'

vmCfg.vm.provision "shell",
privileged: true,
path: './scripts/vagrant-linux-priv-config.sh'

return vmCfg
end

def configureProviders(vmCfg, cpus: "2", memory: "2048")
vmCfg.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--cableconnected1", "on"]
v.memory = memory
v.cpus = cpus
end

["vmware_fusion", "vmware_workstation"].each do |p|
vmCfg.vm.provider p do |v|
v.enable_vmrun_ip_lookup = false
v.vmx["memsize"] = memory
v.vmx["numvcpus"] = cpus
end
end

vmCfg.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--cableconnected1", "on"]
v.memory = memory
v.cpus = cpus
end

return vmCfg
end

def suggestedCPUCores()
case RbConfig::CONFIG['host_os']
when /darwin/
Integer(`sysctl -n hw.ncpu`) / 2
when /linux/
Integer(`grep -c ^processor /proc/cpuinfo`) / 2
else
2
end
end
23 changes: 16 additions & 7 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
#!/bin/bash

set -e
set -o errexit

build_locally() {
DEST="pkg/linux_amd64"
NAME="nomad-driver-lxc"

DEST="./pkg/linux_amd64/nomad-driver-lxc"
mkdir -p pkg/linux_amd64
echo "===> Building lxc driver binary"
echo
go build -o "${DEST}/${NAME}" .

echo
echo "binary is present in ${DEST}/${NAME}"
}

case "${OSTYPE}" in
darwin*) ./scripts/build-in-docker.sh ;;
linux*)
if pkg-config --exists lxc
then
DEST="${pkg/linux_amd64/nomad-driver-lxc}"
mkdir -p pkg/linux_amd64
go build -o "${DEST}" .

echo
echo "binary is present in ${DEST}"
build_locally
else
./script/build-in-docker.sh
fi
Expand Down
49 changes: 49 additions & 0 deletions scripts/dist.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env bash
set -e

# Get the version from the command line
VERSION=$1
if [ -z "${VERSION}" ]; then
echo "Please specify a version. (format: 0.4.0-rc1)"
exit 1
fi

# Get the parent directory of where this script is.
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"

# Change into that dir because we expect that
cd "${DIR}"

# Generate the tag.
if [ -z "${NOTAG}" ]; then
echo "==> Tagging..."
git commit --allow-empty -a --gpg-sign=348FFC4C -m "Release v$VERSION"
git tag -a -m "Version $VERSION" -s -u 348FFC4C "v${VERSION}" master
fi

# Zip all the files
rm -rf ./pkg/dist
mkdir -p ./pkg/dist

#find ./pkg -mindepth 1 -maxdepth 1 -type f -exec cp ./pkg/{} ./pkg/dist/nomad-driver-lxc_"${VERSION}"_{} \;
#for FILENAME in $(find ./pkg -mindepth 1 -maxdepth 1 -type f); do
find ./pkg -mindepth 1 -maxdepth 1 -type f -print0 | while read -d '' -r FILENAME; do
FILENAME=$(basename "$FILENAME")
cp "./pkg/${FILENAME}" "./pkg/dist/nomad-driver-lxc_${VERSION}_${FILENAME}"
done

# Make the checksums
pushd ./pkg/dist
shasum -a256 ./* > "./nomad-driver-lxc_${VERSION}_SHA256SUMS"
if [ -z "${NOSIGN}" ]; then
echo "==> Signing..."
gpg --default-key 348FFC4C --detach-sig "./nomad-driver-lxc_${VERSION}_SHA256SUMS"
fi
popd

# Upload
if [ -z "${HC_RELEASE}" ]; then
hc-releases upload "${DIR}/pkg/dist" && hc-releases publish
fi
48 changes: 48 additions & 0 deletions scripts/vagrant-linux-priv-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bash

export DEBIAN_FRONTEND=noninteractive

# Update and ensure we have apt-add-repository
apt-get update
apt-get install -y software-properties-common

apt-get update

# Install Core build utilities for Linux
apt-get install -y \
build-essential \
git \
libc6-dev-i386 \
libpcre3-dev \
linux-libc-dev:i386 \
pkg-config \
zip

# Install Development utilities
apt-get install -y \
curl \
htop \
jq \
tree \
unzip \
vim

# Install LXC tools
apt-get install -y \
liblxc1 \
lxc-dev \
lxc \
lxc-templates

# Ensure everything is up to date
apt-get upgrade -y

# Set hostname -> IP to make advertisement work as expected
ip=$(ip route get 1 | awk '{print $NF; exit}')
hostname=$(hostname)
sed -i -e "s/.*nomad.*/${ip} ${hostname}/" /etc/hosts

# Ensure we cd into the working directory on login
if ! grep "cd /opt/gopath/src/github.com/hashicorp/nomad-driver-lxc" /home/vagrant/.profile ; then
echo 'cd /opt/gopath/src/github.com/hashicorp/nomad-driver-lxc' >> /home/vagrant/.profile
fi
44 changes: 44 additions & 0 deletions scripts/vagrant-linux-priv-go.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash

set -e

function install_go() {
local go_version=1.11.3
local download=

download="https://storage.googleapis.com/golang/go${go_version}.linux-amd64.tar.gz"

if [ -d /usr/local/go ] ; then
return
fi

wget -q -O /tmp/go.tar.gz ${download}

tar -C /tmp -xf /tmp/go.tar.gz
sudo mv /tmp/go /usr/local
sudo chown -R root:root /usr/local/go
}

install_go

# Ensure that the GOPATH tree is owned by vagrant:vagrant
mkdir -p /opt/gopath
chown -R vagrant:vagrant /opt/gopath

# Ensure Go is on PATH
if [ ! -e /usr/bin/go ] ; then
ln -s /usr/local/go/bin/go /usr/bin/go
fi
if [ ! -e /usr/bin/gofmt ] ; then
ln -s /usr/local/go/bin/gofmt /usr/bin/gofmt
fi


# Ensure new sessions know about GOPATH
if [ ! -f /etc/profile.d/gopath.sh ] ; then
cat <<EOT > /etc/profile.d/gopath.sh
export GOPATH="/opt/gopath"
export PATH="/opt/gopath/bin:\$PATH"
EOT
chmod 755 /etc/profile.d/gopath.sh
fi
5 changes: 5 additions & 0 deletions scripts/vagrant-linux-unpriv-bootstrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

set -e

cd /opt/gopath/src/github.com/hashicorp/nomad-driver-lxc && make bootstrap

0 comments on commit cc6b2fd

Please sign in to comment.