2017-08-28 19:27:45 +03:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
CheckStartupType() {
|
2018-11-09 02:42:02 +03:00
|
|
|
# echo "Checking process autostart system..."
|
2018-02-12 04:13:26 +03:00
|
|
|
starttype=`ps -p 1 | awk '/1/ {print $4}'`
|
|
|
|
if [[ $starttype == 'systemd' ]]; then return 1; # systemd;
|
|
|
|
elif [[ $starttype == 'init' ]]; then return 3; # sysv-init;
|
|
|
|
elif [[ `/sbin/init --version` =~ upstart ]]; then return 2; # upstart;
|
|
|
|
fi
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckStartupTypeOld() {
|
2018-11-09 02:42:02 +03:00
|
|
|
# echo "Checking process autostart system..."
|
2017-08-28 19:27:45 +03:00
|
|
|
if [[ `systemctl` =~ -\.mount ]]; then return 1; # systemd;
|
|
|
|
elif [[ `/sbin/init --version` =~ upstart ]]; then return 2; # upstart;
|
|
|
|
elif [[ -f /etc/init.d/cron && ! -h /etc/init.d/cron ]]; then return 3; # sysv-init;
|
|
|
|
fi
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-04 22:20:18 +03:00
|
|
|
# Add "StartupType=(type)" to .msh file
|
|
|
|
UpdateMshFile() {
|
|
|
|
# Remove all lines that start with "StartupType="
|
|
|
|
sed '/^StartupType=/ d' < /usr/local/mesh/meshagent.msh >> /usr/local/mesh/meshagent2.msh
|
|
|
|
# Add the startup type to the file
|
|
|
|
echo "StartupType=$starttype" >> /usr/local/mesh/meshagent2.msh
|
|
|
|
mv /usr/local/mesh/meshagent2.msh /usr/local/mesh/meshagent.msh
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
url=$1
|
|
|
|
meshid=$2
|
|
|
|
meshidlen=${#meshid}
|
|
|
|
if [ $meshidlen -eq 64 ]
|
|
|
|
then
|
|
|
|
machineid=0
|
2018-11-09 02:42:02 +03:00
|
|
|
machinetype=$( uname -m )
|
|
|
|
|
|
|
|
# If we have 3 arguments...
|
|
|
|
if [ $# -ge 3 ]
|
2017-08-28 19:27:45 +03:00
|
|
|
then
|
2018-11-09 02:42:02 +03:00
|
|
|
# echo "Computer type is specified..."
|
|
|
|
machineid=$3
|
|
|
|
else
|
|
|
|
# echo "Detecting computer type..."
|
|
|
|
if [ $machinetype == 'x86_64' ] || [ $machinetype == 'amd64' ]
|
|
|
|
then
|
|
|
|
# Linux x86, 64 bit
|
|
|
|
machineid=6
|
|
|
|
fi
|
|
|
|
if [ $machinetype == 'x86' ] || [ $machinetype == 'i686' ]
|
|
|
|
then
|
|
|
|
# Linux x86, 32 bit
|
|
|
|
machineid=5
|
|
|
|
fi
|
|
|
|
if [ $machinetype == 'armv6l' ] || [ $machinetype == 'armv7l' ]
|
|
|
|
then
|
|
|
|
# RaspberryPi 1 (armv6l) or RaspberryPi 2/3 (armv7l)
|
|
|
|
machineid=25
|
|
|
|
fi
|
|
|
|
# Add more machine types, detect KVM support... here.
|
|
|
|
fi
|
|
|
|
|
|
|
|
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
|
|
|
|
DownloadAgent $url $meshid $machineid
|
|
|
|
fi
|
2018-11-09 02:42:02 +03:00
|
|
|
|
2017-08-28 19:27:45 +03:00
|
|
|
else
|
2017-11-01 02:19:58 +03:00
|
|
|
echo "MeshID is not correct, must be 64 characters long."
|
2017-08-28 19:27:45 +03:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "URI and/or MeshID have not been specified, must be passed in as arguments."
|
|
|
|
return 0;
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
DownloadAgent() {
|
|
|
|
url=$1
|
|
|
|
meshid=$2
|
|
|
|
machineid=$3
|
2018-11-09 02:42:02 +03:00
|
|
|
# Create folder
|
2017-08-28 19:27:45 +03:00
|
|
|
mkdir -p /usr/local/mesh
|
|
|
|
cd /usr/local/mesh
|
2018-11-09 02:42:02 +03:00
|
|
|
echo "Downloading Mesh agent #$machineid..."
|
2019-01-31 00:43:42 +03:00
|
|
|
wget $url/meshagents?id=$machineid --no-check-certificate {{{noproxy}}}-O /usr/local/mesh/meshagent
|
2018-11-09 02:42:02 +03:00
|
|
|
|
|
|
|
# If it did not work, try again using http
|
|
|
|
if [ $? != 0 ]
|
|
|
|
then
|
|
|
|
url=${url/"https://"/"http://"}
|
2019-01-31 00:43:42 +03:00
|
|
|
wget $url/meshagents?id=$machineid {{{noproxy}}}-O /usr/local/mesh/meshagent
|
2018-11-09 02:42:02 +03:00
|
|
|
fi
|
|
|
|
|
2017-08-28 19:27:45 +03:00
|
|
|
if [ $? -eq 0 ]
|
|
|
|
then
|
2017-11-01 02:19:58 +03:00
|
|
|
echo "Mesh agent downloaded."
|
2018-11-09 02:42:02 +03:00
|
|
|
# TODO: We could check the meshagent sha256 hash, but best to authenticate the server.
|
2017-08-28 19:27:45 +03:00
|
|
|
chmod 755 /usr/local/mesh/meshagent
|
2019-01-31 00:43:42 +03:00
|
|
|
wget $url/meshsettings?id=$meshid --no-check-certificate {{{noproxy}}}-O /usr/local/mesh/meshagent.msh
|
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
|
2019-01-31 00:43:42 +03:00
|
|
|
wget $url/meshsettings?id=$meshid {{{noproxy}}}-O /usr/local/mesh/meshagent.msh
|
2018-11-09 02:42:02 +03:00
|
|
|
fi
|
|
|
|
|
2017-08-28 19:27:45 +03:00
|
|
|
if [ $? -eq 0 ]
|
|
|
|
then
|
2017-09-04 22:20:18 +03:00
|
|
|
UpdateMshFile
|
2017-08-28 19:27:45 +03:00
|
|
|
if [ $starttype -eq 1 ]
|
|
|
|
then
|
2019-01-06 09:07:41 +03:00
|
|
|
# systemd
|
|
|
|
if [ -d "/lib/systemd/system/" ]
|
|
|
|
then
|
|
|
|
echo -e "[Unit]\nDescription=MeshCentral Agent\n[Service]\nExecStart=/usr/local/mesh/meshagent\nStandardOutput=null\nRestart=always\nRestartSec=3\n[Install]\nWantedBy=multi-user.target\nAlias=meshagent.service\n" > /lib/systemd/system/meshagent.service
|
|
|
|
else
|
|
|
|
# Some distros have the systemd folder at a different place
|
|
|
|
if [ -d "/usr/lib/systemd/system/" ]
|
|
|
|
then
|
|
|
|
echo -e "[Unit]\nDescription=MeshCentral Agent\n[Service]\nExecStart=/usr/local/mesh/meshagent\nStandardOutput=null\nRestart=always\nRestartSec=3\n[Install]\nWantedBy=multi-user.target\nAlias=meshagent.service\n" > /usr/lib/systemd/system/meshagent.service
|
|
|
|
else
|
|
|
|
echo "Unable to find systemd folder."
|
|
|
|
fi
|
|
|
|
fi
|
2018-02-02 23:46:09 +03:00
|
|
|
systemctl enable meshagent
|
|
|
|
systemctl start meshagent
|
2017-08-28 19:27:45 +03:00
|
|
|
else
|
2018-02-12 04:13:26 +03:00
|
|
|
if [ $starttype -eq 3 ]
|
|
|
|
then
|
|
|
|
# initd
|
2019-01-31 00:43:42 +03:00
|
|
|
wget $url/meshagents?script=2 --no-check-certificate {{{noproxy}}}-O /etc/init.d/meshagent
|
2018-02-12 04:13:26 +03:00
|
|
|
chmod +x /etc/init.d/meshagent
|
2018-02-13 23:28:11 +03:00
|
|
|
update-rc.d meshagent defaults # creates symlinks for rc.d
|
2018-02-12 04:13:26 +03:00
|
|
|
service meshagent start
|
|
|
|
else
|
|
|
|
# upstart / others (???)
|
|
|
|
./meshagent start
|
|
|
|
ln -s /usr/local/mesh/meshagent /sbin/meshcmd
|
|
|
|
ln -s /usr/local/mesh/meshagent /etc/rc2.d/S20mesh
|
|
|
|
ln -s /usr/local/mesh/meshagent /etc/rc3.d/S20mesh
|
|
|
|
ln -s /usr/local/mesh/meshagent /etc/rc5.d/S20mesh
|
|
|
|
fi
|
2017-08-28 19:27:45 +03:00
|
|
|
fi
|
2017-11-01 02:19:58 +03:00
|
|
|
echo "Mesh agent started."
|
2017-08-28 19:27:45 +03:00
|
|
|
else
|
|
|
|
echo "Unable to download mesh settings at: $url/meshsettings?id=$meshid."
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "Unable to download mesh agent at: $url/meshagents?id=$machineid."
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
UninstallAgent() {
|
|
|
|
# Uninstall agent
|
|
|
|
if [ -e "/usr/local" ]
|
|
|
|
then
|
|
|
|
installpath="/usr/local/mesh"
|
|
|
|
else
|
|
|
|
installpath="/usr/mesh"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $starttype -eq 1 ]
|
|
|
|
then
|
2018-02-12 04:13:26 +03:00
|
|
|
# systemd
|
2018-02-02 23:46:09 +03:00
|
|
|
rm -f /sbin/meshcmd /lib/systemd/system/meshagent.service
|
|
|
|
systemctl disable meshagent
|
|
|
|
systemctl stop meshagent
|
2017-08-28 19:27:45 +03:00
|
|
|
else
|
2018-02-12 04:13:26 +03:00
|
|
|
if [ $starttype -eq 3 ]
|
|
|
|
then
|
|
|
|
# initd
|
|
|
|
service meshagent forceuninstall
|
|
|
|
rm -f /sbin/meshcmd /etc/init.d/meshagent
|
|
|
|
else
|
|
|
|
# upstart / others (???)
|
|
|
|
rm -f /sbin/meshcmd /etc/rc2.d/S20mesh /etc/rc3.d/S20mesh /etc/rc5.d/S20mesh
|
|
|
|
fi
|
2017-08-28 19:27:45 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -e $installpath ]
|
|
|
|
then
|
|
|
|
cd $installpath
|
|
|
|
if [ -e "$installpath/meshagent" ]
|
|
|
|
then
|
|
|
|
./meshagent stop
|
|
|
|
fi
|
|
|
|
rm -rf $installpath/*
|
|
|
|
rmdir $installpath
|
|
|
|
fi
|
|
|
|
echo "Agent uninstalled."
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CheckStartupType
|
|
|
|
starttype=$?
|
|
|
|
#echo "Type: $starttype"
|
|
|
|
|
|
|
|
currentuser=$( whoami )
|
|
|
|
if [ $currentuser == 'root' ]
|
|
|
|
then
|
|
|
|
if [ $# -eq 0 ]
|
|
|
|
then
|
2018-11-09 02:42:02 +03:00
|
|
|
echo -e "This script will install or uninstall a mesh agent, usage:\n $0 [serverurl] [meshid] (machineid)\n $0 uninstall"
|
2017-08-28 19:27:45 +03:00
|
|
|
else
|
|
|
|
if [ $# -eq 1 ]
|
|
|
|
then
|
|
|
|
if [ $1 == 'uninstall' ] || [ $1 == 'UNINSTALL' ]
|
|
|
|
then
|
|
|
|
UninstallAgent
|
|
|
|
fi
|
|
|
|
else
|
2018-11-09 02:42:02 +03:00
|
|
|
CheckInstallAgent $1 $2 $3
|
2017-08-28 19:27:45 +03:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "Must be root to install or uninstall mesh agent."
|
|
|
|
fi
|