1 | #!/bin/bash |
---|
2 | # #/** |
---|
3 | # launchOgagentInstaller ndisk npart [windowsadmin] |
---|
4 | #@brief Scheduling OpenGnsys Agent installation at next boot or administrator session. |
---|
5 | #@param integer ndsik disk number |
---|
6 | #@param integer npart partition number |
---|
7 | #@param string windowsadmin administrator user (only for Windows) |
---|
8 | #@author Ramón M. Gómez, ETSII Univ. Sevilla |
---|
9 | #@version 1.1.0 - Initial version. |
---|
10 | #@date 2018-02-16 |
---|
11 | #*/ ## |
---|
12 | |
---|
13 | # Global variables. |
---|
14 | PROG="$(basename "$0")" |
---|
15 | if which curl &>/dev/null; then |
---|
16 | DOWNLOAD="curl -k -f --connect-timeout 1 -o" |
---|
17 | else |
---|
18 | DOWNLOAD="wget --no-check-certificate -T 1 -O" |
---|
19 | fi |
---|
20 | |
---|
21 | # Show help. |
---|
22 | if [ "$*" == "help" ]; then |
---|
23 | echo "$PROG: scheduling OpenGnsys Agent installation." |
---|
24 | echo "Format: $PROG ndisk npart [windowsadmin]" |
---|
25 | exit 0 |
---|
26 | fi |
---|
27 | # Error control. |
---|
28 | if ! typeset -Fp ogRaiseError &>/dev/null; then |
---|
29 | echo "$PROG: it can only be executed by an ogLive client." >&2 |
---|
30 | exit 1 |
---|
31 | fi |
---|
32 | [ $# == 2 -o $# == 3 ] || ogRaiseError $OG_ERR_FORMAT "$PROG ndisk npart [adminuser]" || exit $OG_ERR_FORMAT |
---|
33 | MNTDIR=$(ogMount "$1" "$2") || exit $? |
---|
34 | OGVERSION=$($DOWNLOAD - https://$(ogGetServerIp)/opengnsys/rest/info 2>/dev/null | jq -r .version) |
---|
35 | [ -n "$OGVERSION" ] || ogRaiseError $OG_ERR_NOTFOUND "GET /rest/info" || exit $OG_ERR_NOTFOUND |
---|
36 | |
---|
37 | case "$(ogGetOsType $1 $2)" in |
---|
38 | Windows) # OGAgent for Windows. |
---|
39 | HIVE="$(ogGetHivePath "$MNTDIR" "$3")" |
---|
40 | [ -n "$HIVE" ] || ogRaiseError $OG_ERR_NOTFOUND "$1 $2 $3/NTUSER.DAT" || exit $OG_ERR_NOTFOUND |
---|
41 | # Downloading OGAgent installer for Windows. |
---|
42 | OGAGENTFILE="OGAgentSetup-${OGVERSION/pre/}.exe" |
---|
43 | TMPDIR="$(ogGetPath "$MNTDIR/Windows/Temp")" |
---|
44 | if ogListSoftware $1 $2 | grep -qi "opengnsys agent"; then |
---|
45 | echo "OGAgent for Windows is already installed, you need to uninstall it before re-install." |
---|
46 | else |
---|
47 | if eval $DOWNLOAD "$TMPDIR/$OGAGENTFILE" "https://$(ogGetServerIp)/opengnsys/descargas/$OGAGENTFILE" 2>/dev/null; then |
---|
48 | # Run once OGAgent Installer. |
---|
49 | if hivexsh -w << EOT 2>/dev/null; then |
---|
50 | load $HIVE |
---|
51 | cd \\Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce |
---|
52 | setval 1 |
---|
53 | Install OGAgent |
---|
54 | string:C:\\Windows\\Temp\\$OGAGENTFILE" |
---|
55 | commit |
---|
56 | close |
---|
57 | exit |
---|
58 | EOT |
---|
59 | echo "Scheduled OGAgent installation after \"$3\" logon" |
---|
60 | echo " (edit config file after finish)." |
---|
61 | else |
---|
62 | ogRaiseError $OG_ERR_NOTWRITE "$1 $2 .../$3/NTUSER.DAT" |
---|
63 | exit $OG_ERR_NOTWRITE |
---|
64 | fi |
---|
65 | else |
---|
66 | ogRaiseError $OG_ERR_NOTFOUND "$1 $2 /Windows/Temp/$OGAGENTFILE" |
---|
67 | exit $OG_ERR_NOTFOUND |
---|
68 | fi |
---|
69 | fi |
---|
70 | ;; |
---|
71 | Linux) # OGAgent for Linux (only deb and redhat-based backages; TODO suse-based package). |
---|
72 | if ogListSoftware $1 $2 | grep -qi "ogagent"; then |
---|
73 | echo "OGAgent for Linux is already installed, you need to uninstall it before re-install." |
---|
74 | else |
---|
75 | SYSTEMDDIR="$MNTDIR/lib/systemd" |
---|
76 | [ -d "$SYSTEMDDIR" -a -d "${SYSTEMDDIR/lib/etc}" ] || ogRaiseError $OG_ERR_NOTFOUND "$1 $2 systemd" || exit $OG_ERR_NOTFOUND |
---|
77 | # Downloading OGAgent installer for Linux. |
---|
78 | if [ -e $MNTDIR/etc/debian_version ]; then # Debian-based |
---|
79 | OGAGENTFILE="ogagent_${OGVERSION/pre/}_all.deb" |
---|
80 | CODE="if ! dpkg -l ogagent &>/dev/null && [ -f /var/tmp/$OGAGENTFILE ]; then apt-get update; apt-get install -y /var/tmp/$OGAGENTFILE; fi" |
---|
81 | fi |
---|
82 | if [ -e $MNTDIR/etc/redhat-release ]; then # RedHat-based |
---|
83 | OGAGENTFILE="ogagent-${OGVERSION/pre/}-1.noarch.rpm" |
---|
84 | CODE="if ! rpm -q ogagent &>/dev/null && [ -f /var/tmp/$OGAGENTFILE ]; then yum install -y /var/tmp/$OGAGENTFILE; fi" |
---|
85 | fi |
---|
86 | [ -n "$OGAGENTFILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$1 $2 ogagent" || exit $OG_ERR_NOTFOUND |
---|
87 | TMPDIR="$MNTDIR/var/tmp" |
---|
88 | if eval $DOWNLOAD "$TMPDIR/$OGAGENTFILE" "https://$(ogGetServerIp)/opengnsys/descargas/$OGAGENTFILE" 2>/dev/null; then |
---|
89 | # Creating systemd script. |
---|
90 | cat << EOT > $SYSTEMDDIR/systemd-launchogagent |
---|
91 | #!/bin/bash |
---|
92 | [ $EUID = 0 ] || exit 4 |
---|
93 | start() { |
---|
94 | $CODE |
---|
95 | sed -i "0,/remote=/ s,remote=.*,remote=https://$(ogGetServerIp)/opengnsys/rest/," /usr/share/OGAgent/cfg/ogagent.cfg |
---|
96 | service ogagent start |
---|
97 | } |
---|
98 | restart() { |
---|
99 | service ogagent stop |
---|
100 | if [ -f /var/tmp/$OGAGENTFILE ]; then |
---|
101 | apt-get update |
---|
102 | apt-get install -y --reinstall /var/tmp/$OGAGENTFILE |
---|
103 | fi |
---|
104 | sed -i "0,/remote=/ s,remote=.*,remote=https://$(ogGetServerIp)/opengnsys/rest/," /usr/share/OGAgent/cfg/ogagent.cfg |
---|
105 | service ogagent start |
---|
106 | } |
---|
107 | |
---|
108 | case "\$1" in |
---|
109 | start|restart) "\$1" ;; |
---|
110 | esac |
---|
111 | EOT |
---|
112 | chmod +x $SYSTEMDDIR/systemd-launchogagent |
---|
113 | # Creating systemd service. |
---|
114 | cat << EOT > $SYSTEMDDIR/system/launchogagent.service |
---|
115 | [Unit] |
---|
116 | Description=Installing and configuring OGAgent |
---|
117 | |
---|
118 | [Service] |
---|
119 | Type=oneshot |
---|
120 | RemainAfterExit=yes |
---|
121 | ExecStart=/lib/systemd/systemd-launchogagent start |
---|
122 | TimeoutStartSec=5min |
---|
123 | |
---|
124 | [Install] |
---|
125 | WantedBy=multi-user.target |
---|
126 | EOT |
---|
127 | ln -fs /lib/systemd/system/launchogagent.service \ |
---|
128 | ${SYSTEMDDIR/lib/etc}/system/multi-user.target.wants |
---|
129 | echo "Scheduled OGAgent installation at next boot" |
---|
130 | echo " (process will be executed in the background, do not shutdown until finish)." |
---|
131 | else |
---|
132 | ogRaiseError $OG_ERR_NOTFOUND "$1 $2 /var/tmp/$OGAGENTFILE" |
---|
133 | exit $OG_ERR_NOTFOUND |
---|
134 | fi |
---|
135 | fi |
---|
136 | ;; |
---|
137 | MacOS) # OGAgent for macOS (TODO). |
---|
138 | echo "OGAgent installer for macOS is not implemented yet." |
---|
139 | ;; |
---|
140 | *) # OS not detected or OGAgent not implemented. |
---|
141 | ogRaiseError $OG_ERR_NOTOS "$1 $2" |
---|
142 | exit $OG_ERR_NOTOS |
---|
143 | ;; |
---|
144 | esac |
---|
145 | |
---|