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