source: client/shared/scripts/launchOgagentInstaller

qndtest
Last change on this file was 2078a2f, checked in by Ramón M. Gómez <ramongomez@…>, 4 years ago

#963: Script launchOgagentInstaller schedules an unattended installation of OGAgent for Windows.

  • Property mode set to 100755
File size: 5.8 KB
Line 
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.
14PROG="$(basename "$0")"
15if which curl &>/dev/null; then
16    DOWNLOAD="curl -k -f --connect-timeout 1 -o"
17else
18    DOWNLOAD="wget --no-check-certificate -T 1 -O"
19fi
20
21# Show help.
22if [ "$*" == "help" ]; then
23    echo "$PROG: scheduling OpenGnsys Agent installation."
24    echo "Format: $PROG ndisk npart [windowsadmin]"
25    exit 0
26fi
27# Error control.
28if ! typeset -Fp ogRaiseError &>/dev/null; then
29    echo "$PROG: it can only be executed by an ogLive client." >&2
30    exit 1
31fi
32[ $# == 2 -o $# == 3 ] || ogRaiseError $OG_ERR_FORMAT "$PROG ndisk npart [adminuser]" || exit $OG_ERR_FORMAT
33MNTDIR=$(ogMount "$1" "$2") || exit $?
34OGVERSION=$($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
37case "$(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
50load $HIVE
51cd \\Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce
52setval 1
53Install OGAgent
54string:C:\\Windows\\Temp\\$OGAGENTFILE /S /server $(ogGetServerIp)
55commit
56close
57exit
58EOT
59                    echo "Scheduled OGAgent installation after \"$3\" logon"
60                    echo "  (for connection problems, check configuration file)."
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
93start() {
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}
98restart() {
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
108case "\$1" in
109    start|restart)  "\$1" ;;
110esac
111EOT
112                chmod +x $SYSTEMDDIR/systemd-launchogagent
113                # Creating systemd service.
114                cat << EOT > $SYSTEMDDIR/system/launchogagent.service
115[Unit]
116Description=Installing and configuring OGAgent
117
118[Service]
119Type=oneshot
120RemainAfterExit=yes
121ExecStart=/lib/systemd/systemd-launchogagent start
122TimeoutStartSec=5min
123
124[Install]
125WantedBy=multi-user.target
126EOT
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        ;;
144esac
145
Note: See TracBrowser for help on using the repository browser.