2019-07-23 03:23:13 +03:00
|
|
|
#!/usr/bin/env bash
|
2017-08-28 19:27:45 +03:00
|
|
|
|
|
|
|
CheckStartupType() {
|
2019-07-23 03:23:13 +03:00
|
|
|
# 1 = Systemd
|
|
|
|
# 2 = Upstart
|
|
|
|
# 3 = init.d
|
|
|
|
# 5 = BSD
|
|
|
|
|
|
|
|
# echo "Checking if Linux or BSD Platform"
|
|
|
|
plattype=`uname | awk '{ tst=tolower($0);a=split(tst, res, "bsd"); if(a==1) { print "LINUX"; } else { print "BSD"; }}'`
|
|
|
|
if [[ $plattype == 'BSD' ]]
|
|
|
|
then return 5;
|
|
|
|
fi
|
|
|
|
|
2018-11-09 02:42:02 +03:00
|
|
|
# echo "Checking process autostart system..."
|
2019-08-14 01:22:35 +03:00
|
|
|
starttype1=`cat /proc/1/status | grep 'Name:' | awk '{ print $2; }'`
|
|
|
|
starttype2=`ps -p 1 -o command= | awk '{a=split($0,res," "); b=split(res[a],tp,"/"); print tp[b]; }'`
|
2019-07-23 03:23:13 +03:00
|
|
|
|
2019-03-02 05:07:38 +03:00
|
|
|
# Systemd
|
2019-08-14 01:22:35 +03:00
|
|
|
if [[ $starttype1 == 'systemd' ]]
|
2019-07-23 03:23:13 +03:00
|
|
|
then return 1;
|
2019-08-14 01:22:35 +03:00
|
|
|
elif [[ $starttype1 == 'init' || $starttype2 == 'init' ]]
|
2021-01-19 07:48:29 +03:00
|
|
|
then
|
|
|
|
if [ -d "/etc/init" ]
|
|
|
|
then
|
|
|
|
return 2;
|
|
|
|
else
|
|
|
|
return 3;
|
|
|
|
fi
|
2017-08-28 19:27:45 +03:00
|
|
|
fi
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-11 21:43:45 +03:00
|
|
|
|
2017-09-04 22:20:18 +03:00
|
|
|
# Add "StartupType=(type)" to .msh file
|
|
|
|
UpdateMshFile() {
|
|
|
|
# Remove all lines that start with "StartupType="
|
2021-01-19 07:48:29 +03:00
|
|
|
sed '/^StartupType=/ d' < ./meshagent.msh >> ./meshagent2.msh
|
2017-09-04 22:20:18 +03:00
|
|
|
# Add the startup type to the file
|
2021-01-19 07:48:29 +03:00
|
|
|
echo "StartupType=$starttype" >> ./meshagent2.msh
|
|
|
|
mv ./meshagent2.msh ./meshagent.msh
|
2017-09-04 22:20:18 +03:00
|
|
|
}
|
|
|
|
|
2017-08-28 19:27:45 +03:00
|
|
|
CheckInstallAgent() {
|
2018-11-09 02:42:02 +03:00
|
|
|
# echo "Checking mesh identifier..."
|
2017-08-28 19:27:45 +03:00
|
|
|
if [ -e "/usr/local" ]
|
|
|
|
then
|
|
|
|
installpath="/usr/local/mesh"
|
|
|
|
else
|
|
|
|
installpath="/usr/mesh"
|
|
|
|
fi
|
2018-11-09 02:42:02 +03:00
|
|
|
if [ $# -ge 2 ]
|
2017-08-28 19:27:45 +03:00
|
|
|
then
|
2021-01-19 07:48:29 +03:00
|
|
|
uninstall=$1
|
|
|
|
url=$2
|
|
|
|
meshid=$3
|
2021-03-10 23:49:09 +03:00
|
|
|
if [[ $4 =~ ^--WebProxy= ]];
|
|
|
|
then
|
|
|
|
webproxy=$4
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-08-28 19:27:45 +03:00
|
|
|
meshidlen=${#meshid}
|
2020-06-23 22:11:15 +03:00
|
|
|
if [ $meshidlen -gt 63 ]
|
2017-08-28 19:27:45 +03:00
|
|
|
then
|
|
|
|
machineid=0
|
2018-11-09 02:42:02 +03:00
|
|
|
machinetype=$( uname -m )
|
|
|
|
|
|
|
|
# If we have 3 arguments...
|
2021-03-10 23:49:09 +03:00
|
|
|
if [ $# -ge 4 ] && [ -z "$webproxy" ]
|
2017-08-28 19:27:45 +03:00
|
|
|
then
|
2018-11-09 02:42:02 +03:00
|
|
|
# echo "Computer type is specified..."
|
2021-01-19 07:48:29 +03:00
|
|
|
machineid=$4
|
2019-03-02 05:07:38 +03:00
|
|
|
else
|
2018-11-09 02:42:02 +03:00
|
|
|
# echo "Detecting computer type..."
|
|
|
|
if [ $machinetype == 'x86_64' ] || [ $machinetype == 'amd64' ]
|
|
|
|
then
|
2021-01-19 07:48:29 +03:00
|
|
|
if [ $starttype -eq 5 ]
|
|
|
|
then
|
|
|
|
# FreeBSD x86, 64 bit
|
|
|
|
machineid=30
|
|
|
|
else
|
|
|
|
# Linux x86, 64 bit
|
|
|
|
bitlen=$( getconf LONG_BIT )
|
|
|
|
if [ $bitlen == '32' ]
|
|
|
|
then
|
|
|
|
# 32 bit OS
|
|
|
|
machineid=5
|
|
|
|
else
|
|
|
|
# 64 bit OS
|
|
|
|
machineid=6
|
|
|
|
fi
|
|
|
|
fi
|
2018-11-09 02:42:02 +03:00
|
|
|
fi
|
2019-02-28 05:48:50 +03:00
|
|
|
if [ $machinetype == 'x86' ] || [ $machinetype == 'i686' ] || [ $machinetype == 'i586' ]
|
2018-11-09 02:42:02 +03:00
|
|
|
then
|
2021-01-19 07:48:29 +03:00
|
|
|
if [ $starttype -eq 5 ]
|
|
|
|
then
|
|
|
|
# FreeBSD x86, 32 bit
|
|
|
|
machineid=31
|
|
|
|
else
|
|
|
|
# Linux x86, 32 bit
|
|
|
|
machineid=5
|
|
|
|
fi
|
2018-11-09 02:42:02 +03:00
|
|
|
fi
|
|
|
|
if [ $machinetype == 'armv6l' ] || [ $machinetype == 'armv7l' ]
|
|
|
|
then
|
|
|
|
# RaspberryPi 1 (armv6l) or RaspberryPi 2/3 (armv7l)
|
|
|
|
machineid=25
|
|
|
|
fi
|
2019-04-28 20:25:53 +03:00
|
|
|
if [ $machinetype == 'aarch64' ]
|
|
|
|
then
|
|
|
|
# RaspberryPi 3B+ running Ubuntu 64 (aarch64)
|
|
|
|
machineid=26
|
|
|
|
fi
|
2018-11-09 02:42:02 +03:00
|
|
|
# Add more machine types, detect KVM support... here.
|
2019-03-02 05:07:38 +03:00
|
|
|
fi
|
2018-11-09 02:42:02 +03:00
|
|
|
|
2019-03-02 05:07:38 +03:00
|
|
|
if [ $machineid -eq 0 ]
|
2017-08-28 19:27:45 +03:00
|
|
|
then
|
2018-11-09 02:42:02 +03:00
|
|
|
echo "Unsupported machine type: $machinetype."
|
2017-08-28 19:27:45 +03:00
|
|
|
else
|
2021-01-19 07:48:29 +03:00
|
|
|
DownloadAgent $uninstall $url $meshid $machineid
|
2017-08-28 19:27:45 +03:00
|
|
|
fi
|
2018-11-09 02:42:02 +03:00
|
|
|
|
2017-08-28 19:27:45 +03:00
|
|
|
else
|
2021-01-19 07:48:29 +03:00
|
|
|
echo "Device group identifier is not correct, must be at least 64 characters long."
|
2017-08-28 19:27:45 +03:00
|
|
|
fi
|
|
|
|
else
|
2021-01-19 07:48:29 +03:00
|
|
|
echo "URI and/or device group identifier have not been specified, must be passed in as arguments."
|
2017-08-28 19:27:45 +03:00
|
|
|
return 0;
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
DownloadAgent() {
|
2021-01-19 07:48:29 +03:00
|
|
|
uninstall=$1
|
|
|
|
url=$2
|
|
|
|
meshid=$3
|
|
|
|
machineid=$4
|
|
|
|
echo "Downloading agent #$machineid..."
|
|
|
|
wget $url/meshagents?id=$machineid {{{wgetoptionshttps}}}-O ./meshagent || curl {{{curloptionshttps}}}--output ./meshagent $url/meshagents?id=$machineid
|
2018-11-09 02:42:02 +03:00
|
|
|
|
|
|
|
# If it did not work, try again using http
|
|
|
|
if [ $? != 0 ]
|
|
|
|
then
|
|
|
|
url=${url/"https://"/"http://"}
|
2021-01-19 07:48:29 +03:00
|
|
|
wget $url/meshagents?id=$machineid {{{wgetoptionshttp}}}-O ./meshagent || curl {{{curloptionshttp}}}--output ./meshagent $url/meshagents?id=$machineid
|
2018-11-09 02:42:02 +03:00
|
|
|
fi
|
|
|
|
|
2017-08-28 19:27:45 +03:00
|
|
|
if [ $? -eq 0 ]
|
|
|
|
then
|
2021-01-19 07:48:29 +03:00
|
|
|
echo "Agent downloaded."
|
2018-11-09 02:42:02 +03:00
|
|
|
# TODO: We could check the meshagent sha256 hash, but best to authenticate the server.
|
2021-01-19 07:48:29 +03:00
|
|
|
chmod 755 ./meshagent
|
|
|
|
wget $url/meshsettings?id=$meshid {{{wgetoptionshttps}}}-O ./meshagent.msh || curl {{{curloptionshttps}}}--output ./meshagent.msh $url/meshsettings?id=$meshid
|
2018-11-09 02:42:02 +03:00
|
|
|
|
|
|
|
# If it did not work, try again using http
|
2018-11-26 00:08:27 +03:00
|
|
|
if [ $? -ne 0 ]
|
2018-11-09 02:42:02 +03:00
|
|
|
then
|
2021-01-19 07:48:29 +03:00
|
|
|
wget $url/meshsettings?id=$meshid {{{wgetoptionshttp}}}-O ./meshagent.msh || curl {{{curloptionshttp}}}--output ./meshagent.msh $url/meshsettings?id=$meshid
|
2018-11-09 02:42:02 +03:00
|
|
|
fi
|
|
|
|
|
2017-08-28 19:27:45 +03:00
|
|
|
if [ $? -eq 0 ]
|
|
|
|
then
|
2021-01-19 07:48:29 +03:00
|
|
|
# Update the .msh file and run the agent installer/uninstaller
|
|
|
|
if [ $uninstall == 'uninstall' ] || [ $uninstall == 'UNINSTALL' ]
|
2017-08-28 19:27:45 +03:00
|
|
|
then
|
2021-01-19 07:48:29 +03:00
|
|
|
# Uninstall the agent
|
|
|
|
./meshagent -fulluninstall
|
2019-03-02 05:07:38 +03:00
|
|
|
else
|
2021-01-19 07:48:29 +03:00
|
|
|
# Install the agent
|
|
|
|
UpdateMshFile
|
2021-03-10 23:49:09 +03:00
|
|
|
./meshagent -fullinstall --copy-msh=1 $webproxy
|
2017-08-28 19:27:45 +03:00
|
|
|
fi
|
|
|
|
else
|
2021-01-19 07:48:29 +03:00
|
|
|
echo "Unable to download device group settings at: $url/meshsettings?id=$meshid."
|
2017-08-28 19:27:45 +03:00
|
|
|
fi
|
|
|
|
else
|
2021-01-19 07:48:29 +03:00
|
|
|
echo "Unable to download agent at: $url/meshagents?id=$machineid."
|
2017-08-28 19:27:45 +03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CheckStartupType
|
|
|
|
starttype=$?
|
|
|
|
#echo "Type: $starttype"
|
|
|
|
|
|
|
|
currentuser=$( whoami )
|
|
|
|
if [ $currentuser == 'root' ]
|
|
|
|
then
|
|
|
|
if [ $# -eq 0 ]
|
|
|
|
then
|
2021-01-19 07:48:29 +03:00
|
|
|
echo -e "This script will install or uninstall a agent, usage:\n $0 [serverUrl] [deviceGroupId] (machineId)\n $0 uninstall [serverUrl] [deviceGroupId] (machineId)"
|
2017-08-28 19:27:45 +03:00
|
|
|
else
|
2021-01-19 07:48:29 +03:00
|
|
|
if [ $1 == 'uninstall' ] || [ $1 == 'UNINSTALL' ]
|
2017-08-28 19:27:45 +03:00
|
|
|
then
|
2021-01-19 07:48:29 +03:00
|
|
|
CheckInstallAgent 'uninstall' $2 $3 $4
|
2017-08-28 19:27:45 +03:00
|
|
|
else
|
2021-01-19 07:48:29 +03:00
|
|
|
CheckInstallAgent 'install' $1 $2 $3
|
2017-08-28 19:27:45 +03:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
2021-01-19 07:48:29 +03:00
|
|
|
echo "Must be root to install or uninstall the agent."
|
2017-08-28 19:27:45 +03:00
|
|
|
fi
|