summarylogtreecommitdiffstats
path: root/skywire-autoconfig
blob: 88f7bb9e6fae62d4ad8c66bae9602e74b38f9d26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/bin/bash
#/opt/skywire/scripts/skywire-autoconfig
#skywire autoconfiguration script for debian & archlinux packages
[[ ${DEBUGAUTOCONF} == "1" ]] && set -x
#source the skyenv file if it exists - provided by the skyrepo package or the user
[[ -f /etc/profile.d/skyenv.sh ]] && source /etc/profile.d/skyenv.sh
#The above file was the previous limited configuration ; the new file is /etc/skywire.conf but may also be specified
[[ ! -z "${SKYENV}" ]] && [[ -f "${SKYENV}" ]] && source ${SKYENV}
#if the env is empty, we set a default and source that if it exists
[[ -z "${SKYENV}" ]] && SKYENV=/etc/skywire.conf && [[ -f "${SKYENV}" ]] && source ${SKYENV} && unset SKYENV || unset 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
[[ ${NOAUTOCONFIG} == true ]] && { unset NOAUTOCONFIG ; echo "autoconfiguration disabled. to configure and start skywire run: skywire-autoconfig" ; exit 0 ; }
#check for root
[[ $EUID -ne 0 ]] && { echo "root permissions required" ; exit 1 ; }

#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 export SKYENV=/etc/skywire.conf && [[ -f "${SKYENV}" ]] && source ${SKYENV} || unset SKYENV
if [[ "$?" -ne 0 ]] && [[ -f /etc/profile.d/skyenv.sh ]] && [[ ! -f "${SKYENV}" ]] ; then
source /etc/profile.d/skyenv.sh
SKYENV=/etc/skywire.conf
skywire-cli config gen -q | tee ${SKYENV}  > /dev/null
sed -i "s/#BESTPROTO=true/BESTPROTO=true/g" /etc/skywire.conf

[[ ( ${URL} -ne "") ]] && sed -i "s/#SVCCONFADDR=('')/SVCCONFADDR=('${URL}')/g" ${SKYENV} ;
[[ ( ${DMSGHTTP} -eq "1") ]] && sed -i 's/^.*DMSGHTTP.*$/DMSGHTTP=true/g' ${SKYENV} ;
[[ ( ${VISORISPUBLIC} -eq "1") ]] && sed -i 's/^.*VISORISPUBLIC.*$/VISORISPUBLIC=true/g' ${SKYENV} ;
[[ ( ${NOAUTOCONNECT} -eq "1") ]] && sed -i 's/^.*DISABLEPUBLICAUTOCONN.*$/DISABLEPUBLICAUTOCONN=true/g' ${SKYENV} ;
[[ ${VPNSERVER} -eq "1" ]] && sed -i 's/^.*VPNSERVER.*$/VPNSERVER=true/g' ${SKYENV} ;
[[ ${TESTENV} -eq "1" ]] && sed -i 's/^.*TESTENV.*$/TESTENV=true/g' ${SKYENV} ;
sed -i 's/^.*PKGENV.*$/PKGENV=true/g' ${SKYENV}
[[ (! -f /opt/skywire/skywire.json) || ($(cat /opt/skywire/skywire.json | grep -Po '"hypervisor":') != "") ]] &&	sed -i "s/#ISHYPERVISOR=true/ISHYPERVISOR=true/g" ${SKYENV}
export SKYENV=/etc/skywire.conf && [[ -f "${SKYENV}" ]] && source ${SKYENV}
echo "export SKYENV=${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
[[ $(ps -eo pid,comm,cgroup | grep skywire) == *"system.slice"* ]] && WSYSTEMD=1 ;

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
    # 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
      grep -q "HYPERVISORPKS" ${SKYENV} && sed -i "s/^.*HYPERVISORPKS.*\$/#HYPERVISORPKS=('')/" ${SKYENV} || echo "#HYPERVISORPKS=('')" | tee -a ${SKYENV} > /dev/null
      grep -q "ISHYPERVISOR" ${SKYENV} && sed -i 's/^.*ISHYPERVISOR.*$/ISHYPERVISOR=true/' ${SKYENV} || echo "ISHYPERVISOR=true" | tee -a ${SKYENV} > /dev/null
    fi
    # 1 as argument drops remote hypervisors and does not create the local hv config
    if [[ ${_1} == "1" ]]; then
      _retainhv=""
      unset _1
      grep -q "HYPERVISORPKS" ${SKYENV} && sed -i "s/^.*HYPERVISORPKS.*\$/#HYPERVISORPKS=('')/" ${SKYENV} || echo "#HYPERVISORPKS=('')" | tee -a ${SKYENV} > /dev/null
      grep -q "ISHYPERVISOR" ${SKYENV} && sed -i 's/^.*ISHYPERVISOR.*$/#ISHYPERVISOR=true/' ${SKYENV} || echo "#ISHYPERVISOR=true" | tee -a ${SKYENV} > /dev/null
    fi
    # create the flag to set the remote hypervisor(s)
    if [[ ! -z ${_1} ]]; then
      _retainhv=""
      grep -q "HYPERVISORPKS" ${SKYENV} && sed -i "s/^.*HYPERVISORPKS.*\$/HYPERVISORPKS=('${_1}')/" ${SKYENV} || echo "HYPERVISORPKS=('${_1}')" | tee -a ${SKYENV} > /dev/null
      grep -q "ISHYPERVISOR" ${SKYENV} && sed -i 's/^.*ISHYPERVISOR.*$/#ISHYPERVISOR=true/' ${SKYENV} || echo "#ISHYPERVISOR=true" | tee -a ${SKYENV} > /dev/null
     fi

     [[ -f /opt/skywire/local/reward.txt ]] && { grep -q "REWARDSKYADDR" ${SKYENV} && { sed -i "s/^.*REWARDSKYADDR.*\$/REWARDSKYADDR=('$(cat /opt/skywire/local/reward.txt)')/" ${SKYENV} ; } || { echo "REWARDSKYADDR=$(cat /opt/skywire/local/reward.txt)" | tee -a ${SKYENV} > /dev/null ; } ; } ;
     source ${SKYENV}
    _skyenv="SKYENV=${SKYENV}"
    export SKYENV=${SKYENV}
  _configgencmd() {
    skywire-cli config gen -r ${_retain_hv}
  }
#  SKYENV=${SKYENV} skywire-cli config gen --all
# show config gen command used
      _configgencmdcolor="${_green}${_skyenv} ${_cyan}skywire-cli ${_yellow}config gen -r ${_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
	[[ ${_1} == "0" ]] && { _retainhv="" ; unset _1 ;	_ishypervisor=" -i" ; }
	# 1 as argument drops remote hypervisors and does not create the local hv config
  [[ ${_1} == "1" ]] && { _retainhv="" ; unset _1 ; _ishypervisor="" ; }
	# create the flag to set the remote hypervisor(s)
	[[ ! -z ${_1} ]] && { _retainhv="" ; _hypervisorpks=" -j ${_1}" ; _ishypervisor="" ; }

_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}"
  fi
	_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
	[[ -f /opt/skywire/skywire.json ]] &&	{ _msg3 "${_blue}Skywire${_nc} configuration updated\nconfig path: ${_purple}/opt/skywire/skywire.json${_nc}" ; } || { _errmsg2 "expected config file not found at /opt/skywire/skywire.json" ; exit 100 ; }
  [[ ! -f /etc/skywire-config.json ]] && { _msg2 "backing up configuration to /etc/skywire-config.json" && cp -b /opt/skywire/skywire.json /etc/skywire-config.json ; }
}

if [[ ! -z "${SKYENV}" ]] && [[ -f "${SKYENV}" ]] ; then
[[ "${VISORISPUBLIC}" == "true" ]] && _visorispublic=" --public" ;
[[ "${VPNSERVER}" == "true" ]] && _vpnserver=" --servevpn" ;
[[ "${TESTENV}" == "true" ]] && _testenv=" -t" ;
[[ "${BESTPROTO}" == "true" ]] && _bestproto=" -b" ;
[[ "${#SVCCONFADDR[@]}" -gt 0 ]] &&  IFS=',' _svccconfaddr="  -a='${SVCCONFADDR[*]}'" ;
[[ "${LOGLVL}" != "" ]] && _loglvl=" --loglvl=${LOGLVL}" ;
[[ "${DMSGHTTP}" == "true" ]] && _dmsghttp=" -d" ;
[[ "${ISHYPERVISOR}" == "true" ]] && _ishypervisor=" -i" ;
[[ -n "${OUTPUT}" ]] && _output=" -o '${OUTPUT}'" ;
[[ "${DISPLAYNODEIP}" == "true" ]] && _displaynodeip=" --publicip" ;
_pkgenv=" -p" ;
[[ -n "${SK}" ]] && _sk=" -s '${SK}'" ;
[[ "${DISABLEPUBLICAUTOCONN}" == "true" ]] && _disablepublicautoconn=" -y" ;
[[ -n "${VERSION}" ]] && _version=" --version='${VERSION}'" ;
[[ -n "${BINPATH}" ]] && _binpath=" --binpath='${BINPATH}'" ;
[[ -n "${PROXYCLIENTPK}" ]] && _proxyclientpk=" --proxyclientpk='${PROXYCLIENTPK}'" ;
[[ "${STARTPROXYCLIENT}" == "true" ]] && _startproxyclient=" --startproxyclient" ;
[[ "${NOPROXYSERVER}" == "true" ]] && _noproxyserver=" --noproxyserver" ;
[[ -n "${PROXYSEVERPASS}" ]]  && _proxyserverpass=" --proxyserverpass='${PROXYSEVERPASS}'" ;
[[ -n "${PROXYCLIENTPASS}" ]] && _proxyclientpass=" --proxyclientpass='${PROXYCLIENTPASS}'" ;
[[ "${VPNKS}" == "true" ]] && _vpnks=" --killsw='${VPNKS}'" ;
[[ -n "${ADDVPNPK}" ]] && _addvpn=" --addvpn='${ADDVPNPK}'" ;
[[ -n "${VPNCLIENTPASS}" ]] && _vpnclientpass=" --vpnpass='${VPNCLIENTPASS}'" ;
[[ -n "${VPNSEVERPASS}" ]] && _vpnserverpass=" --vpnserverpass='${VPNSEVERPASS}'" ;
[[ -n "${VPNSEVERSECURE}" ]] && _vpnserversecure=" --secure='${VPNSEVERSECURE}'" ;
[[ -n "${VPNSEVERNETIFC}" ]] && _vpnservernetifc=" --netifc='${VPNSEVERNETIFC}'" ;
[[ "${#HYPERVISORPKS[@]}" -gt 0 ]] && IFS=',' _hypervisorpks=" -j '${HYPERVISORPKS[*]}'" ;
else
[[ ( ${URL} != "") ]]  && _svccconfaddr=" -a '${URL}'" ;
[[ ( ${DMSGHTTP} -eq "1") ]] && _dmsghttp=" -d" ;
[[ ( ${VISORISPUBLIC} -eq "1") ]] && _visorispublic=" --public" ;
[[ ( ${NOAUTOCONNECT} -eq "1") ]] && _disablepublicautoconn=" -y" ;
[[ ${VPNSERVER} -eq "1" ]] && _vpnserver=" --servevpn" ;
[[ ${TESTENV} -eq "1" ]] && _testenv=" -t" ;
_retainhv="-x" ;
_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
[[ "${SYSTEMDCHILD}" -ne "1" ]] && _now="--now" ;
#root portion of the config
_msg2 "Configuring skywire"
[[ $(skywire -v | awk '{print $NF}') != *"unknown"* ]]  && _msg2 "version: $(skywire -v | awk '{print $NF}')" ;
#attempt to import config if none exists - i.e. import skybian config or restore config
[[ ! -f /opt/skywire/skywire.json ]] && [[ -f /etc/skywire-config.json ]] &&	{ _warnmsg1 "Importing configuration from /etc/skywire-config.json to /opt/skywire/skywire.json" ; cp -b  /etc/skywire-config.json /opt/skywire/skywire.json ; }
[[ ! -f /opt/skywire/local/reward.txt ]] && [[ -f /etc/reward.txt ]] && { _warnmsg1 "Importing reward address from /etc/reward.txt to /opt/skywire/local/reward.txt" ; cp -b  /etc/reward.txt /opt/skywire/local/reward.txt ; }
[[ ! -f /opt/skywire/users.db ]] && [[ -f /etc/users.db ]] && {	_warnmsg1 "Importing hypervisor UI login credentials from /etc/users.db to /opt/skywire/users.db" ; cp -b  /etc/users.db /opt/skywire/users.db ; }
#config generation
_config_gen
_svc=skywire
[[ $SKYBIAN == "true" ]] && { _msg3 "Enabling ${_svc} service${_now/--/ and starting }..\n    systemctl enable ${_now} ${_svc}.service" ; systemctl enable ${_now} ${_svc}.service 2> /dev/null ; }
[[ $DMSGPTYTERM == "1" ]] && { [[ ${_now} != "--now" ]] && { _msg3 "Please restart ${_svc} service manually:\n		${_red}systemctl restart ${_svc} 2> /dev/null${_nc}" ; exit 0 ; } || { _msg3 "Please shut down your visor and start it again with:\n		${_red}systemctl start ${_svc} 2> /dev/null${_nc}" ; exit 0 ; } ; }
#restart the service
	systemctl is-active --quiet ${_svc} && _msg3 "Restarting skywire.service..." && systemctl restart ${_svc} 2> /dev/null
! systemctl is-active --quiet ${_svc} >/dev/null && { _msg2 "Start the skywire service with:\n	${_red}systemctl start ${_svc}${_nc}" ; exit 0 ; }
_pubkey=$(skywire-cli visor pk -p | tail -n1)

#helpful text
_welcome(){
#set / print reward address
[[ ! -z "$REWARDSKYADDR" ]] && echo "$REWARDSKYADDR" | tee /opt/skywire/local/reward.txt >/dev/null
skywire-cli reward -r >> /dev/null 2>&1 && [[ ${?} == 0 ]] && { _msg2 "skycoin reward address:\n${_green}$(skywire-cli reward -r)${_nc}" ; _msg2 "reward metrics:\n${_blue}https://fiber.skywire.dev/skycoin-rewards${_nc}" ; _msg2 "distribution notifications:\n${_blue}https://t.me/skywire_reward${_nc}" ; } || { _msg2 "reward eligibility rules:\n${_yellow}https://github.com/skycoin/skywire/blob/develop/mainnet_rules.md${_nc}" ; _msg2 "set your skycoin reward address:\n${_cyan}skywire-cli ${_yellow}reward ${_green}<skycoin-address>${_nc}" ; }
_msg2 "track uptime:\n  ${_cyan}skywire-cli ${_yellow}ut -m0 -k ${_green}${_pubkey}${_nc}\n${_blue}https://ut.skywire.skycoin.com/uptimes?v=v2&visors=${_pubkey}${_nc}"
_msg2 "support:\n${_blue}https://t.me/skywire${_nc}"
[[ ! -z ${SKYENV} ]] && [[ "${ISHYPERVISOR}" == "true" ]] && { _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}" ; } ;
}
_msg2 "Visor Public Key:\n${_green}${_pubkey}${_nc}"
[[ ! -z ${SKYENV} ]] && [[ "${ISHYPERVISOR}" == "true" ]] && [[ $(ps -o comm= -p $PPID) != "sshd" ]] && { _msg2 "Hypervisor UI Starting now on:\n${_red}http://127.0.0.1:8000${_nc}" ; _vpnurl="http://127.0.0.1:8000/#/vpn/${_pubkey}" ; _msg2 "Use the vpn:\n${_red}${_vpnurl}${_nc}" ; }
[[ -z ${SKYENV} ]] && [[ $_ishypervisor == " -i" ]] && [[ $(ps -o comm= -p $PPID) != "sshd" ]] && { _msg2 "Hypervisor UI Starting now on:\n${_red}http://127.0.0.1:8000${_nc}" ; _vpnurl="http://127.0.0.1:8000/#/vpn/${_pubkey}" ; _msg2 "Use the vpn:\n${_red}${_vpnurl}${_nc}" ; }
[[ ! -z ${SKYENV} ]] && [[ "${ISHYPERVISOR}" == "true" ]] && { _hpvurl="Hypervisor UI LAN access:" ; _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" ; }
[[ ! -z ${SKYENV} ]] && [[ "${ISHYPERVISOR}" != "true" ]] && _msg2 "${_blue}Skywire${_nc} starting without Hypervisor UI"
[[ ! -z ${SKYENV} ]] && [[ "${#HYPERVISORPKS[@]}" -gt 0 ]] && { _hvpk=$(printf '%s\n' "${HYPERVISORPKS[@]}") ; _msg2 "Remote Hypervisor Public Key:\n ${_purple}${_hvpk}${_nc}" ; }
_welcome
#( _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 )
systemctl is-active --quiet ${_svc} >/dev/null && [[ ${#SKYFWD[@]} -gt 0 ]] && command -v nc >/dev/null 2>&1 && { 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 ; }
exit 0
}