Sketch in vagrant provision script

Known problem with npm install step: this will likely fail when run
from a Windows host, when npm tries to create symlinks in the synced
folder. Workaround may be to run `vagrant up` from within a shell
with admin rights on the host machine (Windows security policy may
disallow creation of symlinks otherwise).
This commit is contained in:
Tom Russell 2019-03-05 10:04:18 +00:00
parent 5392ad0606
commit d692db6c19
5 changed files with 294 additions and 17 deletions

3
.gitignore vendored
View File

@ -24,3 +24,6 @@ app/tilecache/**/*.mbtiles
# Notes # Notes
TODO TODO
# VM
.vagrant

41
Vagrantfile vendored Normal file
View File

@ -0,0 +1,41 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "bento/ubuntu-18.04"
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
config.vm.network "forwarded_port", guest: 3000, host: 3000, host_ip: "127.0.0.1"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
config.vm.provision "shell", path: "provision/vm_provision.sh"
end

123
provision/.bashrc Normal file
View File

@ -0,0 +1,123 @@
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
# Add an "alert" alias for long running commands. Use like so:
# sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
# use virtual env
source ~/colouringlondon/bin/activate
# start in vagrant dir on login
cd /vagrant

View File

@ -1,17 +0,0 @@
set -x
# node
NODE_VERSION=v8.11.3
DISTRO=linux-x64
wget -nc https://nodejs.org/dist/$NODE_VERSION/node-$NODE_VERSION-$DISTRO.tar.xz
sudo mkdir /usr/local/lib/node
sudo tar xf node-v8.11.3-linux-x64.tar.xz -C /usr/local/lib/node
sudo mv /usr/local/lib/node/node-$NODE_VERSION-$DISTRO /usr/local/lib/node/node-$NODE_VERSION
rm node-$NODE_VERSION-$DISTRO.tar.xz
cat >> ~/.profile <<EOF
# Nodejs
export NODEJS_HOME=/usr/local/lib/node/node-$NODE_VERSION/bin
export PATH=\$NODEJS_HOME:\$PATH
EOF

127
provision/vm_provision.sh Normal file
View File

@ -0,0 +1,127 @@
#!/usr/bin/env bash
#
# Provision virtual machine
#
# This script is expected to run a part of the vagrant virtual machine provisioning process. It
# is run by the root user and should do all required installation and setup:
# 1. Install OS packages
# 2. Provision Postgres database
# 3. Light-touch set up of virtual machine environment for user convenience (other programs
# or dependencies, .bashrc or other dotfiles)
#
# Echo commands as they are executed
set -x
# Create vagrant user if not exists
id -u vagrant >/dev/null 2>&1 || useradd --create-home vagrant
#
# Install OS packages
#
apt-get update
# Install:
# - basics: build-essential git vim-nox
# - python with pip and venv: python3 python3-pip python3-dev
# - postgres and postgis: postgresql postgresql-contrib libpq-dev postgis postgresql-10-postgis-2.4
# - spatial shared libs: gdal-bin libspatialindex-dev libgeos-dev libproj-dev
apt-get install -y \
build-essential git vim-nox wget curl \
python3 python3-pip python3-dev python3-venv \
postgresql postgresql-contrib libpq-dev postgis postgresql-10-postgis-2.4 \
gdal-bin libspatialindex-dev libgeos-dev libproj-dev
#
# Install node.js (node and npm)
#
# node version and platform
NODE_VERSION=v8.11.3
DISTRO=linux-x64
# download
wget -nc https://nodejs.org/dist/$NODE_VERSION/node-$NODE_VERSION-$DISTRO.tar.xz
# extract
sudo mkdir /usr/local/lib/node
sudo tar xf node-$NODE_VERSION-$DISTRO.tar.xz -C /usr/local/lib/node
sudo mv /usr/local/lib/node/node-$NODE_VERSION-$DISTRO /usr/local/lib/node/node-$NODE_VERSION
rm node-$NODE_VERSION-$DISTRO.tar.xz
# add to user PATH
cat >> /home/vagrant/.profile <<EOF
# Nodejs
export NODEJS_HOME=/usr/local/lib/node/node-$NODE_VERSION/bin
export PATH=\$NODEJS_HOME:\$PATH
EOF
# add to current (root user) path
export NODEJS_HOME=/usr/local/lib/node/node-$NODE_VERSION/bin
export PATH=$NODEJS_HOME:$PATH
#
# Setup PostgreSQL database
#
# Ensure postgres is running
service postgresql start
# Ensure en_US locale exists
locale-gen en_US.UTF-8
# Database config to listen on network connection
sed -i "s/#\?listen_address.*/listen_addresses '*'/" /etc/postgresql/9.5/main/postgresql.conf
# Allow password connections from any IP (so includes host)
echo "host all all all md5" >> /etc/postgresql/10/main/pg_hba.conf
# Restart postgres to pick up config changes
service postgresql restart
# Create vagrant role if not exists
su postgres -c "psql -c \"SELECT 1 FROM pg_user WHERE usename = 'vagrant';\" " \
| grep -q 1 || su postgres -c "psql -c \"CREATE ROLE vagrant SUPERUSER LOGIN PASSWORD 'vagrant';\" "
# Create vagrant database if not exists
su postgres -c "psql -c \"SELECT 1 FROM pg_database WHERE datname = 'vagrant';\" " \
| grep -q 1 || su postgres -c "createdb -E UTF8 -T template0 --locale=en_US.utf8 -O vagrant vagrant"
# Run all 'up' migrations to create tables, data types, indexes
su postgres -c "ls /vagrant/migrations/*.up.sql 2>/dev/null | while read -r migration; do psql < $migration; done;"
#
# Install python packages
#
# set up env
pyvenv colouringlondon
source colouringlondon/bin/activate
# Install smif
pip install --upgrade pip
pip install --upgrade setuptools wheel
pip install -r /vagrant/etl/requirements.txt
# Make virtualenv user-editable
chown -R vagrant:vagrant /home/vagrant/colouringlondon
#
# Install node modules
#
# Install latest release of npm
npm install -g npm@next
# Local fixed install of node modules
cd /vagrant/app && npm ci
#
# User config
#
# Copy bash config to vagrant home (making sure to clean windows newlines using tr)
tr -d '\r' < /vagrant/provision/.bashrc > /home/vagrant/.bashrc
chown vagrant:vagrant /home/vagrant/.bashrc