meshinstall-linux.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/usr/bin/env bash
  2. CheckStartupType() {
  3. # 1 = Systemd
  4. # 2 = Upstart
  5. # 3 = init.d
  6. # 5 = BSD
  7. # echo "Checking if Linux or BSD Platform"
  8. plattype=`uname | awk '{ tst=tolower($0);a=split(tst, res, "bsd"); if(a==1) { print "LINUX"; } else { print "BSD"; }}'`
  9. if [[ $plattype == 'BSD' ]]
  10. then return 5;
  11. fi
  12. # echo "Checking process autostart system..."
  13. starttype1=`cat /proc/1/status | grep 'Name:' | awk '{ print $2; }'`
  14. starttype2=`ps -p 1 -o command= | awk '{a=split($0,res," "); b=split(res[a],tp,"/"); print tp[b]; }'`
  15. # Systemd
  16. if [[ $starttype1 == 'systemd' ]]
  17. then return 1;
  18. elif [[ $starttype1 == 'init' || $starttype2 == 'init' ]]
  19. then
  20. if [ -d "/etc/init" ]
  21. then
  22. return 2;
  23. else
  24. return 3;
  25. fi
  26. fi
  27. return 0;
  28. }
  29. # Add "StartupType=(type)" to .msh file
  30. UpdateMshFile() {
  31. # Remove all lines that start with "StartupType="
  32. sed '/^StartupType=/ d' < ./meshagent.msh >> ./meshagent2.msh
  33. # Add the startup type to the file
  34. echo "StartupType=$starttype" >> ./meshagent2.msh
  35. mv ./meshagent2.msh ./meshagent.msh
  36. }
  37. CheckInstallAgent() {
  38. # echo "Checking mesh identifier..."
  39. if [ -e "/usr/local" ]
  40. then
  41. installpath="/usr/local/mesh"
  42. else
  43. installpath="/usr/mesh"
  44. fi
  45. if [ $# -ge 2 ]
  46. then
  47. uninstall=$1
  48. url=$2
  49. meshid=$3
  50. if [[ $4 =~ ^--WebProxy= ]];
  51. then
  52. webproxy=$4
  53. fi
  54. meshidlen=${#meshid}
  55. if [ $meshidlen -gt 63 ]
  56. then
  57. machineid=0
  58. machinetype=$( uname -m )
  59. # If we have 3 arguments...
  60. if [ $# -ge 4 ] && [ -z "$webproxy" ]
  61. then
  62. # echo "Computer type is specified..."
  63. machineid=$4
  64. else
  65. # echo "Detecting computer type..."
  66. if [ $machinetype == 'x86_64' ] || [ $machinetype == 'amd64' ]
  67. then
  68. if [ $starttype -eq 5 ]
  69. then
  70. # FreeBSD x86, 64 bit
  71. machineid=30
  72. else
  73. # Linux x86, 64 bit
  74. bitlen=$( getconf LONG_BIT )
  75. if [ $bitlen == '32' ]
  76. then
  77. # 32 bit OS
  78. machineid=5
  79. else
  80. # 64 bit OS
  81. machineid=6
  82. fi
  83. fi
  84. fi
  85. if [ $machinetype == 'x86' ] || [ $machinetype == 'i686' ] || [ $machinetype == 'i586' ]
  86. then
  87. if [ $starttype -eq 5 ]
  88. then
  89. # FreeBSD x86, 32 bit
  90. machineid=31
  91. else
  92. # Linux x86, 32 bit
  93. machineid=5
  94. fi
  95. fi
  96. if [ $machinetype == 'armv6l' ] || [ $machinetype == 'armv7l' ]
  97. then
  98. # RaspberryPi 1 (armv6l) or RaspberryPi 2/3 (armv7l)
  99. machineid=25
  100. fi
  101. if [ $machinetype == 'aarch64' ]
  102. then
  103. # RaspberryPi 3B+ running Ubuntu 64 (aarch64)
  104. machineid=26
  105. fi
  106. if [ $machinetype == 'riscv64' ]
  107. then
  108. # RISC-V 64 bit
  109. machineid=45
  110. fi
  111. # Add more machine types, detect KVM support... here.
  112. fi
  113. if [ $machineid -eq 0 ]
  114. then
  115. echo "Unsupported machine type: $machinetype."
  116. else
  117. DownloadAgent $uninstall $url $meshid $machineid
  118. fi
  119. else
  120. echo "Device group identifier is not correct, must be at least 64 characters long."
  121. fi
  122. else
  123. echo "URI and/or device group identifier have not been specified, must be passed in as arguments."
  124. return 0;
  125. fi
  126. }
  127. DownloadAgent() {
  128. uninstall=$1
  129. url=$2
  130. meshid=$3
  131. machineid=$4
  132. echo "Downloading agent #$machineid..."
  133. wget $url/meshagents?id=$machineid {{{wgetoptionshttps}}}-O ./meshagent || curl {{{curloptionshttps}}}--output ./meshagent $url/meshagents?id=$machineid
  134. # If it did not work, try again using http
  135. if [ $? != 0 ]
  136. then
  137. url=${url/"https://"/"http://"}
  138. wget $url/meshagents?id=$machineid {{{wgetoptionshttp}}}-O ./meshagent || curl {{{curloptionshttp}}}--output ./meshagent $url/meshagents?id=$machineid
  139. fi
  140. if [ $? -eq 0 ]
  141. then
  142. echo "Agent downloaded."
  143. # TODO: We could check the meshagent sha256 hash, but best to authenticate the server.
  144. chmod 755 ./meshagent
  145. wget $url/meshsettings?id=$meshid {{{wgetoptionshttps}}}-O ./meshagent.msh || curl {{{curloptionshttps}}}--output ./meshagent.msh $url/meshsettings?id=$meshid
  146. # If it did not work, try again using http
  147. if [ $? -ne 0 ]
  148. then
  149. wget $url/meshsettings?id=$meshid {{{wgetoptionshttp}}}-O ./meshagent.msh || curl {{{curloptionshttp}}}--output ./meshagent.msh $url/meshsettings?id=$meshid
  150. fi
  151. if [ $? -eq 0 ]
  152. then
  153. # Update the .msh file and run the agent installer/uninstaller
  154. if [ $uninstall == 'uninstall' ] || [ $uninstall == 'UNINSTALL' ]
  155. then
  156. # Uninstall the agent
  157. ./meshagent -fulluninstall
  158. else
  159. # Install the agent
  160. UpdateMshFile
  161. ./meshagent -fullinstall --copy-msh=1 $webproxy
  162. fi
  163. else
  164. echo "Unable to download device group settings at: $url/meshsettings?id=$meshid."
  165. fi
  166. else
  167. echo "Unable to download agent at: $url/meshagents?id=$machineid."
  168. fi
  169. }
  170. CheckStartupType
  171. starttype=$?
  172. #echo "Type: $starttype"
  173. currentuser=$( whoami )
  174. if [ $currentuser == 'root' ]
  175. then
  176. if [ $# -eq 0 ]
  177. then
  178. echo -e "This script will install or uninstall a agent, usage:\n $0 [serverUrl] [deviceGroupId] (machineId)\n $0 uninstall [serverUrl] [deviceGroupId] (machineId)"
  179. else
  180. if [ $1 == 'uninstall' ] || [ $1 == 'UNINSTALL' ]
  181. then
  182. CheckInstallAgent 'uninstall' $2 $3 $4
  183. else
  184. CheckInstallAgent 'install' $1 $2 $3
  185. fi
  186. fi
  187. else
  188. echo "Must be root to install or uninstall the agent."
  189. fi