~sirn/fanboi2

722f75d29a8864aa4f848fc2c553a98296b21534 — Kridsada Thanabulpong 2 months ago 597f686
chore: update vagrant bootstrap
3 files changed, 189 insertions(+), 61 deletions(-)

M Vagrantfile
R vendor/vagrant/{bootstrap.sh => setup_sys.sh}
A vendor/vagrant/setup_user.sh
M Vagrantfile => Vagrantfile +18 -1
@@ 5,5 5,22 @@ Vagrant.configure("2") do |config|
    config.vm.box = "generic/freebsd12"
    config.vm.network "forwarded_port", guest: 6543, host: 6543
    config.ssh.shell = "sh"
    config.vm.provision :shell, privileged: true, path: "vendor/vagrant/bootstrap.sh"

    config.vm.provision :shell, privileged: true, path: "vendor/vagrant/setup_sys.sh"
    config.vm.provision :shell, privileged: false, path: "vendor/vagrant/setup_user.sh"

    config.vm.provider :libvirt do |v|
        v.cpus = 2
        v.memory = 2048
    end

    config.vm.provider :virtualbox do |v|
        v.cpus = 2
        v.memory = 2048
    end

    config.vm.provider :vmware_desktop do |v|
        v.vmx["numvcpus"] = "2"
        v.vmx["memsize"] = "2048"
    end
end

R vendor/vagrant/bootstrap.sh => vendor/vagrant/setup_sys.sh +63 -60
@@ 1,20 1,60 @@
#!/bin/sh
set -xe
#
# Setting up system.
#

sysrc hostname=vagrant
hostname vagrant
set -e


## Utils
##

T_BOLD=$(tput bold 2>/dev/null || true)
T_RESET=$(tput sgr0 2>/dev/null || true)

printe() {
    printf "%s\\n" "$@"
}

printe_h1() {
    printf -- "==> %s%s%s\\n" "$T_BOLD" "$@" "$T_RESET"
}

printe_h2() {
    printf -- "--> %s\\n" "$@"
}


## Initial configuration
##

if [ "$(hostname)" != "vagrant" ]; then
    printe_h1 "Setting up hostname..."

    sysrc hostname=vagrant
    hostname vagrant
fi

# generic/freebsd12 image ship with broken rc.conf, i.e. use `firstboot-growfs`
# instead of `firstboot_growfs`, causing lots of warnings when running pkg.
if awk '! /^firstboot/' < /etc/rc.conf > /etc/rc.conf.tmp; then
    mv /etc/rc.conf.tmp /etc/rc.conf
if grep -q ^firstboot /etc/rc.conf; then
    printe_h1 "Removing invalid rc.conf..."

    if awk '! /^firstboot/' < /etc/rc.conf > /etc/rc.conf.tmp; then
        mv /etc/rc.conf.tmp /etc/rc.conf
    fi
fi


## Upgrades
##

printe_h1 "Upgrading packages, this may take a while..."
pkg upgrade -qy
pkg update -qf

# shellcheck disable=SC2086
pkg install -qy \
printe_h1 "Installing packages..."
pkg install -y \
    bzip2 \
    ca_root_nss \
    curl \


@@ 32,15 72,24 @@ pkg install -qy \
    sqlite3 \
    sudo


## Enable services
##

printe_h1 "Enabling services..."

if ! service ntpd onestatus >/dev/null; then
    printe_h2 "Enabling ntpd..."

    sysrc ntpd_enable=YES
    ntpd -qg
    service ntpd start
fi

if ! service postgresql onestatus >/dev/null; then
    sysrc postgresql_enable=YES
    printe_h2 "Enabling PostgreSQL..."

    sysrc postgresql_enable=YES
    service postgresql initdb
    cat <<EOF > /var/db/postgres/data11/pg_hba.conf
local all all trust


@@ 54,62 103,16 @@ EOF
fi

if ! service redis onestatus >/dev/null; then
    printe_h2 "Enabling Redis..."

    sysrc redis_enable=YES
    service redis start
fi

chsh -s /bin/sh vagrant
sudo -u vagrant sh <<EOF
cd "\$HOME" || exit
rm -f \
   .bashrc \
   .cshrc \
   .lesshst \
   .login \
   .login-e \
   .login_conf \
   .mail_aliases \
   .mailrc \
   .profile \
   .profile-e \
   .rhosts \
   .rnd \
   .shrc

cat <<EOPROFILE > "\$HOME/.profile"
EDITOR=vim; export EDITOR
PAGER=more; export PAGER
LANG=en_US.UTF-8; export LANG
PATH=\$PATH:\$HOME/.venv/bin; export PATH
EOPROFILE

. "\$HOME/.profile"

if [ ! -d "\$HOME/.venv" ]; then
    python3.7 -m venv \$HOME/.venv
fi

pip3 install poetry
## User
##

cd /vagrant || exit
printe_h1 "Setting up user..."

psql template1 -c "CREATE DATABASE fanboi2_dev;" || true
psql template1 -c "CREATE DATABASE fanboi2_test;" || true

if [ ! -f /vagrant/.env ]; then
    cat <<EOENV > "/vagrant/.env"
CELERY_BROKER_URL=redis://127.0.0.1:6379/1
DATABASE_URL=postgresql://vagrant:@127.0.0.1:5432/fanboi2_dev
REDIS_URL=redis://127.0.0.1:6379/0
SERVER_DEV=true
SERVER_HOST=0.0.0.0
SERVER_PORT=6543
SESSION_SECRET=\$(openssl rand -hex 32)
AUTH_SECRET=\$(openssl rand -hex 32)
EOENV
fi

poetry install || exit 1
npm install || exit 1
npm run gulp || exit 1
EOF
chsh -s /bin/sh vagrant

A vendor/vagrant/setup_user.sh => vendor/vagrant/setup_user.sh +108 -0
@@ 0,0 1,108 @@
#!/bin/sh
#
# Setting up user.
#

set -e


## Utils
##

T_BOLD=$(tput bold 2>/dev/null || true)
T_RESET=$(tput sgr0 2>/dev/null || true)

printe() {
    printf "%s\\n" "$@"
}

printe_h1() {
    printf -- "==> %s%s%s\\n" "$T_BOLD" "$@" "$T_RESET"
}

printe_h2() {
    printf -- "--> %s\\n" "$@"
}


## Cleanup
##

printe_h1 "Cleaning up home..."

cd "$HOME" || exit

rm -f \
   .bashrc \
   .cshrc \
   .lesshst \
   .login \
   .login-e \
   .login_conf \
   .mail_aliases \
   .mailrc \
   .profile \
   .profile-e \
   .rhosts \
   .rnd \
   .shrc


## Setup Profile
##

if [ ! -f "$HOME/.profile" ]; then
    printe_h1 "Setting up profile..."

    cat <<EOPROFILE > "$HOME/.profile"
EDITOR=vim; export EDITOR
PAGER=more; export PAGER
LANG=en_US.UTF-8; export LANG
PATH=\$PATH:\$HOME/.venv/bin; export PATH
EOPROFILE
fi

# shellcheck disable=SC1090
. "$HOME/.profile"


## Prepare Python
##

printe_h1 "Preparing Python environment..."

if [ ! -d "$HOME/.venv" ]; then
    python3.7 -m venv "$HOME/.venv"
fi

if [ ! -f "$HOME/.venv/bin/poetry" ]; then
    pip3 install poetry
fi


## Prepare application
##

printe_h1 "Setting up application..."

cd /vagrant || exit

psql template1 -c "CREATE DATABASE fanboi2_dev;" || true
psql template1 -c "CREATE DATABASE fanboi2_test;" || true

if [ ! -f /vagrant/.env ]; then
    cat <<EOENV > "/vagrant/.env"
CELERY_BROKER_URL=redis://127.0.0.1:6379/1
DATABASE_URL=postgresql://vagrant:@127.0.0.1:5432/fanboi2_dev
REDIS_URL=redis://127.0.0.1:6379/0
SERVER_DEV=true
SERVER_HOST=0.0.0.0
SERVER_PORT=6543
SESSION_SECRET=\$(openssl rand -hex 32)
AUTH_SECRET=\$(openssl rand -hex 32)
EOENV
fi

poetry install || exit 1
npm install || exit 1
npm run gulp || exit 1