diff --git a/cmd/vidforward/Makefile b/cmd/vidforward/Makefile new file mode 100644 index 00000000..b6a14da5 --- /dev/null +++ b/cmd/vidforward/Makefile @@ -0,0 +1,45 @@ +# NB: The default (soft) install does not override existing installation files. +USER := $(shell whoami) +PATH := /usr/local/go/bin:$(PATH) +BIN_NAME := vidforward +BIN_DIR := /src/bitbucket.org/ausocean/av/cmd/$(BIN_NAME) +RUN_SCRIPT_DIR := $(BIN_DIR) + +.SILENT:soft_copy_files +.SILENT:hard_copy_files +.SILENT:clean + +rebuild: + chmod +x run.sh + cd ../cmd/$(BIN_NAME); go build + +install: as_root soft_copy_files rebuild + @echo "Install complete" + +install_hard: as_root hard_copy_files set_mac rebuild + @echo "Hard install complete" + +as_root: +ifneq ($(USER),root) + $(error Must run as superuser!) +endif + +soft_copy_files: + if [ -f /etc/systemd/system/$(BIN_NAME).service ] ; then \ + echo "/etc/systemd/system/$(BIN_NAME).service left unmodified" ; \ + else \ + bash create_service.sh $(RUN_SCRIPT_DIR) $(BIN_DIR); \ + fi + systemctl enable $(BIN_NAME).service + +hard_copy_files: + if [ -f /etc/systemd/system/$(BIN_NAME).service ] ; then \ + echo "/etc/systemd/system/$(BIN_NAME).service overwritten" ; \ + fi + bash create_service.sh $(RUN_SCRIPT_DIR) $(BIN_DIR) + systemctl enable $(BIN_NAME).service + +clean: as_root + rm -rf /var/log/vidforward + rm -rf /etc/systemd/system/$(BIN_NAME).service + @echo "Clean complete" diff --git a/cmd/vidforward/create_service.sh b/cmd/vidforward/create_service.sh new file mode 100644 index 00000000..609d2de0 --- /dev/null +++ b/cmd/vidforward/create_service.sh @@ -0,0 +1,56 @@ +#!/bin/bash +# This script is utilised by Makefile for creation of a systemd service. The +# service lines are stored in a string allowing us to substitute the GOPATH into +# the ExecStart path of the service file. + +if [ $# -ne 2 ]; then + echo "incorrect number of arguments, expected run script and binary directories" + exit 1 +fi + +# This corresponds to the path, relative to the GOPATH, of the run script that +# will be used by the service e.g. "/src/bitbucket.org/ausocean/av/cmd/vidforward/run.sh" +run_script_dir=$1 + +# This corresponds to the binary dir. e.g. /src/bitbucket.org/ausocean/av/cmd/vidforward. +bin_dir=$2 + +# Get the bin name (assuming this is at the end of the bin_dir). +bin_name=$(basename $bin_dir) + +# First find the user that corresponds to this path (which is assumed to be at the +# base of the current working directory). +in=$(pwd) +arr_in=(${in//// }) +gopath_user=${arr_in[1]} + +# We can now form the gopath from the obtained user. +gopath="/home/$gopath_user/go" + +# Here are the lines that will go into the rv.service file. We'll set the +# ExecStart field as the GOPATH we've obtained + the passed run script dir. +service=" +[Unit] +Description=vidforward service for forwarding video to youtube + +[Service] +Type=simple +ExecStart=$gopath$run_script_dir $gopath_user $bin_dir +WatchdogSec=30s +Restart=on-failure + +[Install] +WantedBy=multi-user.target +" + +# The service name will just use the bin name. +service_name="$bin_name.service" + +# Now overwrite the service if it exists, or create the service then write. +service_dir=/etc/systemd/system/$service_name +if [ -f $service_dir ]; then + echo "$service" > $service_dir +else + touch $service_dir + echo "$service" > $service_dir +fi diff --git a/cmd/vidforward/run.sh b/cmd/vidforward/run.sh new file mode 100644 index 00000000..c10ab639 --- /dev/null +++ b/cmd/vidforward/run.sh @@ -0,0 +1,47 @@ +#!/bin/bash -e +# This script launches vidforward. This is used by the service file. + +# Check that we have the correct number of arguments passed. +if [ $# -ne 2 ]; then + echo "incorrect number of arguments, expected gopath user and binary directory" + exit 1 +fi + +# This is the user in the GOPATH e.g. for /home/foo/go the user is foo. +gopath_user=$1 + +# This is the dir of the binary from the GOPATH e.g. /src/bitbucket.org/ausocean/av/cmd/vidforward. +bin_dir=$2 + +# We'll get the bin name from the bin dir (assuming this is same as the bin dir name). +bin_name=$(basename $bin_dir) + +# the following required directories _should_ already exist +if [ ! -d /var/log/vidforward ]; then + sudo mkdir /var/log/vidforward + chmod guo+rwx /var/log/vidforward +fi + +# show IP addresses +echo Our IP addresses: +sudo ip addr show | grep inet + +# capture stdout and stderr to a secondary log file (just in case) +exec 2> /var/log/vidforward/stream.log +exec 1>&2 + +# Now set all required variables. +HOME=/home/$gopath_user +GOPATH=$HOME/go +VIDFORWARD_PATH=$GOPATH$bin_dir +PATH=$PATH:/usr/local/go/bin:$VIDFORWARD_PATH +cd $VIDFORWARD_PATH +sudo -u $gopath_user HOME=$HOME GOPATH=$GOPATH PATH=$PATH ./$bin_name +if [ $? -eq 0 ] +then + echo "Successfully exited vidforward" + exit 0 +else + echo "vidforward exited with code: $?" >&2 + exit 1 +fi