#!/bin/bash #/opt/skywire/scripts/skywire-autoconfig #skywire autoconfiguration script for debian & archlinux packages #source the skyenv file if it exists - provided by the skybian package or the user [[ ! -z "$SKYENV" ]] && [[ -f "$SKYENV" ]] && source $SKYENV [[ -z "$SKYENV" ]] && SKYENV=/etc/skywire.conf && [[ -f "$SKYENV" ]] && source $SKYENV [[ "$?" -ne 0 ]] && [[ -f /etc/profile.d/skyenv.sh ]] && source /etc/profile.d/skyenv.sh #set NOAUTOCONFIG=true to avoid running the script in the postinstall if [[ ${NOAUTOCONFIG} == true ]]; then #unset the env NOAUTOCONFIG='' echo "autoconfiguration disabled. to configure and start skywire run: skywire-autoconfig" exit 0 fi #check for root if [[ $EUID -ne 0 ]]; then echo "root permissions required" exit 1 fi #make the custom logserver path just in case [[ ! -d /opt/skywire/local/custom ]] && mkdir -p /opt/skywire/local/custom #transition to new /etc/skywire.conf file from /etc/profile.d/skyenv.sh [[ ! -z "$SKYENV" ]] && [[ -f "$SKYENV" ]] && source $SKYENV [[ -z "$SKYENV" ]] && SKYENV=/etc/skywire.conf && [[ -f "$SKYENV" ]] && source $SKYENV if [[ "$?" -ne 0 ]] && [[ -f /etc/profile.d/skyenv.sh ]] && [[ ! -f "$SKYENV" ]] ; then source /etc/profile.d/skyenv.sh skywire-cli config gen -q | tee /etc/skywire.conf > /dev/null sed -i "s/#BESTPROTO=true/BESTPROTO=true/g" /etc/skywire.conf if [[ ( ${URL} -ne "") ]]; then sed -i "s/#SVCCONFADDR=('')/SVCCONFADDR=('${URL}')/g" /etc/skywire.conf fi if [[ ( ${DMSGHTTP} -eq "1") ]]; then sed -i "s/#DMSGHTTP=true/DMSGHTTP=true/g" /etc/skywire.conf fi if [[ ( ${VISORISPUBLIC} -eq "1") ]]; then sed -i "s/#VISORISPUBLIC=true/VISORISPUBLIC=true/g" /etc/skywire.conf fi if [[ ( ${NOAUTOCONNECT} -eq "1") ]]; then sed -i "s/#DISABLEPUBLICAUTOCONN=true/DISABLEPUBLICAUTOCONN=true/g" /etc/skywire.conf fi if [[ ${VPNSERVER} -eq "1" ]]; then sed -i "s/#VPNSERVER=true/VPNSERVER=true/g" /etc/skywire.conf fi if [[ ${TESTENV} -eq "1" ]]; then sed -i "s/#TESTENV=true/TESTENV=true/g" /etc/skywire.conf fi sed -i "s/#PKGENV=true/PKGENV=true/g" /etc/skywire.conf [[ (! -f /opt/skywire/skywire.json) || ($(cat /opt/skywire/skywire.json | grep -Po '"hypervisor":') != "") ]] && sed -i "s/#ISHYPERVISOR=true/ISHYPERVISOR=true/g" /etc/skywire.conf SKYENV=/etc/skywire.conf && [[ -f "$SKYENV" ]] && source $SKYENV fi #grant network permissions to the vpn app binaries ; should be done via systemd service #setcap cap_net_admin+ep /opt/skywire/apps/vpn-client #setcap cap_net_admin+ep /opt/skywire/apps/vpn-server # determine if skywire is running via systemd if [[ $(ps -eo pid,comm,cgroup | grep skywire) == *"system.slice"* ]]; then WSYSTEMD=1 fi systemctl is-active --quiet skywire-autoconfig && systemctl disable skywire-autoconfig 2> /dev/null #make the logging of this script colorful _nc='\033[0m' _red='\033[0;31m' _green='\033[0;32m' _yellow='\033[0;33m' _blue='\033[1;34m' _purple='\033[0;35m' _cyan='\033[0;36m' _bold='\033[1m' ##set the argument to pass into functions## _1=${1} #recreate pacman logging _msg2() { (( QUIET )) && return local mesg=$1; shift printf "${_cyan} ->${_nc}${_bold} ${mesg}${_nc}\n" "$@" } _msg3() { (( QUIET )) && return local mesg=$1; shift printf "${_blue} -->${_nc}${BOLD} ${mesg}${_nc}\n" "$@" } _errmsg1() { (( QUIET )) && return local mesg=$1; shift printf "${_red}>>> Error:${_nc}${_bold} ${mesg}${_nc}\n" "$@" } _warnmsg1() { (( QUIET )) && return local mesg=$1; shift printf "${_red}>>> Warning:${_nc}${_bold} ${mesg}${_nc}\n" "$@" } _errmsg2() { (( QUIET )) && return local mesg=$1; shift printf "${_red}>>> FATAL:${_bold} ${mesg}${_nc}\n" "$@" } #generate config as root _config_gen() { ##generate (hyper)visor configuration## # if [[ ! -z "$SKYENV" ]] && [[ -f "$SKYENV" ]] ; then # # _configgencmd() { # SKYENV=$SKYENV skywire-cli config gen -pr ${_retain_hv} # } # show config gen command used # _configgencmdcolor="${_green}${_skyenv}${_cyan}skywire-cli ${_yellow}config gen -pr ${_retain_hv}" # else #create by default the local hypervisor config if no config exists ; and retain any hypervisor config which exists #check for argument - remote pk or 0 # 0 as argument drops any remote hypervisors which were set in the configuration # & triggers the creation of the local hyperisor configuration if [[ ${_1} == "0" ]]; then _retainhv="" unset _1 _ishypervisor="-i" fi # 1 as argument drops remote hypervisors and does not create the local hv config if [[ ${_1} == "1" ]]; then _retainhv="" unset _1 _ishypervisor="" fi # create the flag to set the remote hypervisor(s) if [[ ! -z ${_1} ]]; then _retainhv="" _hypervisorpks=" -j ${_1}" #shorthand flag: -j _ishypervisor="" fi _configgencmd() { skywire-cli config gen -r ${_visorispublic}${_vpnserver}${_testenv}${_bestproto}${_svccconfaddr}${_loglvl}${_dmsghttp}${_ishypervisor}${_displaynodeip}${_pkgenv}${_sk}${_disablepublicautoconn}${_version}${_binpath}${_proxyclientpk}${_startproxyclient}${_noproxyserver}${_proxyserverpass}${_proxyclientpass}${_vpnks}${_addvpn}${_vpnclientpass}${_vpnserverpass}${_vpnserversecure}${_vpnservernetifc}${_hypervisorpks} } _configgencmdcolor="${_cyan}skywire-cli ${_yellow}config gen -r ${_visorispublic}${_vpnserver}${_testenv}${_bestproto}${_svccconfaddr}${_loglvl}${_dmsghttp}${_ishypervisor}${_displaynodeip}${_pkgenv}${_sk}${_disablepublicautoconn}${_version}${_binpath}${_proxyclientpk}${_startproxyclient}${_noproxyserver}${_proxyserverpass}${_proxyclientpass}${_vpnks}${_addvpn}${_vpnclientpass}${_vpnserverpass}${_vpnserversecure}${_vpnservernetifc}${_hypervisorpks}" _msg3 "Generating skywire config with command: ${_configgencmdcolor}" _configgencmd > /dev/null if [[ ${?} != 0 ]]; then #print the error! _configgencmd _err=$? _errmsg2 "error generating skywire config" exit ${_err} fi #logging check if [[ -f /opt/skywire/skywire.json ]]; then _msg3 "${_blue}Skywire${_nc} configuration updated config path: ${_purple}/opt/skywire/skywire.json${_nc}" if [[ ! -f /etc/skywire-config.json ]]; then _msg2 "backing up configuration to /etc/skywire-config.json" cp -b /opt/skywire/skywire.json /etc/skywire-config.json fi else _errmsg2 "expected config file not found at /opt/skywire/skywire.json" exit 100 fi } if [[ ! -z "$SKYENV" ]] && [[ -f "$SKYENV" ]] ; then if [[ "${VISORISPUBLIC}" == "true" ]]; then _visorispublic=" --public" fi if [[ "${VPNSERVER}" == "true" ]]; then _vpnserver=" --servevpn" fi if [[ "${TESTENV}" == "true" ]]; then _testenv=" -t" fi if [[ "${BESTPROTO}" == "true" ]]; then _bestproto=" -b" fi if [[ "${#SVCCONFADDR[@]}" -gt 0 ]]; then IFS=',' _svccconfaddr=" -a='${SVCCONFADDR[*]}'" fi if [[ "${LOGLVL}" != "" ]]; then _loglvl=" --loglvl=${LOGLVL}" fi if [[ "${DMSGHTTP}" == "true" ]]; then _dmsghttp=" -d" fi if [[ "${ISHYPERVISOR}" == "true" ]]; then _ishypervisor=" -i" fi if [[ -n "${OUTPUT}" ]]; then _output=" -o '${OUTPUT}'" fi if [[ "${DISPLAYNODEIP}" == "true" ]]; then _displaynodeip=" --publicip" fi # if [[ "${PKGENV}" == "true" ]]; then _pkgenv=" -p" # fi if [[ -n "${SK}" ]]; then _sk=" -s '${SK}'" fi if [[ "${DISABLEPUBLICAUTOCONN}" == "true" ]]; then _disablepublicautoconn=" -y" fi if [[ -n "${VERSION}" ]]; then _version=" --version='${VERSION}'" fi if [[ -n "${BINPATH}" ]]; then _binpath=" --binpath='${BINPATH}'" fi if [[ -n "${PROXYCLIENTPK}" ]]; then _proxyclientpk=" --proxyclientpk='${PROXYCLIENTPK}'" fi if [[ "${STARTPROXYCLIENT}" == "true" ]]; then _startproxyclient=" --startproxyclient" fi if [[ "${NOPROXYSERVER}" == "true" ]]; then _noproxyserver=" --noproxyserver" fi if [[ -n "${PROXYSEVERPASS}" ]]; then _proxyserverpass=" --proxyserverpass='${PROXYSEVERPASS}'" fi if [[ -n "${PROXYCLIENTPASS}" ]]; then _proxyclientpass=" --proxyclientpass='${PROXYCLIENTPASS}'" fi if [[ "${VPNKS}" == "true" ]]; then _vpnks=" --killsw='${VPNKS}'" fi if [[ -n "${ADDVPNPK}" ]]; then _addvpn=" --addvpn='${ADDVPNPK}'" fi if [[ -n "${VPNCLIENTPASS}" ]]; then _vpnclientpass=" --vpnpass='${VPNCLIENTPASS}'" fi if [[ -n "${VPNSEVERPASS}" ]]; then _vpnserverpass=" --vpnserverpass='${VPNSEVERPASS}'" fi if [[ -n "${VPNSEVERSECURE}" ]]; then _vpnserversecure=" --secure='${VPNSEVERSECURE}'" fi if [[ -n "${VPNSEVERNETIFC}" ]]; then _vpnservernetifc=" --netifc='${VPNSEVERNETIFC}'" fi if [[ "${#HYPERVISORPKS[@]}" -gt 0 ]]; then IFS=',' _hypervisorpks=" -j '${HYPERVISORPKS[*]}'" fi else if [[ ( ${URL} -ne "") ]]; then _svccconfaddr=" -a '${URL}'" fi if [[ ( ${DMSGHTTP} -eq "1") ]]; then _dmsghttp=" -d" fi if [[ ( ${VISORISPUBLIC} -eq "1") ]]; then _visorispublic=" --public" fi if [[ ( ${NOAUTOCONNECT} -eq "1") ]]; then _disablepublicautoconn=" -y" fi if [[ ${VPNSERVER} -eq "1" ]]; then _vpnserver=" --servevpn" fi _retainhv="-x" if [[ ${TESTENV} -eq "1" ]]; then _testenv=" -t" fi _bestproto=" -b" _pkgenv=" -p" [[ (! -f /opt/skywire/skywire.json) || ($(cat /opt/skywire/skywire.json | grep -Po '"hypervisor":') != "") ]] && _ishypervisor=" -i" fi #check if >>this script<< is a child process of the systemd service i.e.: run in dmsgpty terminal if [[ "${SYSTEMDCHILD}" -ne "1" ]]; then _now="--now" fi #root portion of the config _msg2 "Configuring skywire" if [[ $(skywire -v | awk '{print $NF}') != *"unknown"* ]] ; then _msg2 "version: $(skywire -v | awk '{print $NF}')" fi #attempt to import config if none exists - i.e. import skybian config or restore config if [[ ! -f /opt/skywire/skywire.json ]]; then if [[ -f /etc/skywire-config.json ]]; then _warnmsg1 "Importing configuration from /etc/skywire-config.json to /opt/skywire/skywire.json" cp -b /etc/skywire-config.json /opt/skywire/skywire.json fi fi if [[ ! -f /opt/skywire/local/reward.txt ]]; then if [[ -f /etc/reward.txt ]]; then _warnmsg1 "Importing reward address from /etc/reward.txt to /opt/skywire/local/reward.txt" cp -b /etc/reward.txt /opt/skywire/local/reward.txt fi fi if [[ ! -f /opt/skywire/users.db ]]; then if [[ -f /etc/users.db ]]; then _warnmsg1 "Importing hypervisor UI login credentials from /etc/users.db to /opt/skywire/users.db" cp -b /etc/users.db /opt/skywire/users.db fi fi #config generation _config_gen _svc=skywire if [[ $SKYBIAN == "true" ]]; then _msg3 "Enabling ${_svc} service${_now/--/ and starting }.. systemctl enable ${_now} ${_svc}.service" systemctl enable ${_now} ${_svc}.service 2> /dev/null fi if [[ $DMSGPTYTERM == "1" ]]; then if [[ ${_now} != "--now" ]]; then _msg3 "Please restart ${_svc} service manually: ${_red}systemctl restart ${_svc} 2> /dev/null${_nc}" exit 0 else _msg3 "Please shut down your visor and start it again with: ${_red}systemctl start ${_svc} 2> /dev/null${_nc}" exit 0 fi fi #restart the service systemctl is-active --quiet ${_svc} && _msg3 "Restarting skywire.service..." && systemctl restart ${_svc} 2> /dev/null if ! systemctl is-active --quiet ${_svc} >/dev/null; then _msg2 "Start the skywire service with: ${_red}systemctl start ${_svc}${_nc}" exit 0 fi _pubkey=$(skywire-cli visor pk -p | tail -n1) #helpful text _welcome(){ [[ ! -z "$REWARDSKYADDR" ]] && echo "$REWARDSKYADDR" | tee /opt/skywire/local/reward.txt skywire-cli reward -r >> /dev/null 2>&1 if [[ ${?} == 0 ]]; then _msg2 "skycoin reward address: ${_green}$(skywire-cli reward -r)${_nc}" _msg2 "reward metrics: ${_blue}https://fiber.skywire.dev/skycoin-rewards${_nc}" _msg2 "distribution notifications: ${_blue}https://t.me/skywire_reward${_nc}" else _msg2 "reward eligibility rules: ${_yellow}https://github.com/skycoin/skywire/blob/develop/mainnet_rules.md${_nc}" _msg2 "set your skycoin reward address: ${_cyan}skywire-cli ${_yellow}reward ${_green}${_nc}" fi _msg2 "track uptime: ${_cyan}skywire-cli ${_yellow}ut -m0 -k ${_green}${_pubkey}${_nc} ${_blue}https://ut.skywire.skycoin.com/uptimes?v=v2&visors=${_pubkey}${_nc}" _msg2 "support: ${_blue}https://t.me/skywire${_nc}" } _msg2 "Visor Public Key: ${_green}${_pubkey}${_nc}" if [[ $_ishypervisor == " -i" ]]; then if [[ $(ps -o comm= -p $PPID) != "sshd" ]]; then _msg2 "Starting now on:\n${_red}http://127.0.0.1:8000${_nc}" # _vpnurl=$(skywire-cli vpn url -p) # _msg2 "Use the vpn:\n${_red}${_vpnurl}${_nc}" _vpnurl="http://127.0.0.1:8000/#/vpn/${_pubkey}" _msg2 "Use the vpn:\n${_red}${_vpnurl}${_nc}" fi _hpvurl="Access hypervisor UI from local network here:" _lanips="$(ip addr show | grep -w inet | grep -v 127.0.0.1 | awk '{ print $2}' | cut -d "/" -f 1)" for _lanip in $_lanips do _hpvurl+="\n${_yellow}http://${_lanip}:8000${_nc}" done _msg2 "$_hpvurl" _welcome _msg2 "run the following command on OTHER NODES to set this one as the hypervisor:" echo -e "${_cyan}skywire-autoconfig ${_yellow}${_pubkey}${_nc}" _msg2 "to see this text again run: ${_cyan}skywire-autoconfig${_nc}" else _msg2 "${_blue}Skywire${_nc} starting in visor mode" _hvpk=$(grep -A 2 "\"hypervisors\": \[" /opt/skywire/skywire.json | grep -v "\[\]" | head -n2 | tail -n1 | cut -d '"' -f2) _msg2 "hypervisor Public Key: ${_purple}${_hvpk}${_nc}" _welcome fi if systemctl is-active --quiet ${_svc} >/dev/null; then if [[ ${#SKYFWD[@]} -gt 0 ]] ; then if command -v nc >/dev/null 2>&1; then while ! nc -z localhost 3435; do _msg2 "waiting for RPC" ; sleep 1; done for _port in ${SKYFWD[@]} ; do _msg2 "setting up skyfwd connection ${_yellow}skywire-cli fwd -p $_port${_nc}" skywire-cli fwd -p $_port done fi fi fi #SKYREV=('-p 8000 -r 8000 -k ') #if systemctl is-active --quiet ${_svc} >/dev/null; then # if [[ ${#SKYREV[@]} -gt 0 ]] ; then # if command -v nc >/dev/null 2>&1; then # while ! nc -z localhost 3435; do _msg2 "waiting for RPC" ; sleep 1; done # for _rev in ${SKYREV[@]} ; do # _msg2 "setting up skyrev connection with ${_yellow}skywire-cli rev $_rev${_nc}" # skywire-cli rev $_rev # done # fi # fi #fi