source: client/shared/scripts/launchOgagentInstaller @ e4546f2

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacionwebconsole3
Last change on this file since e4546f2 was cc8a098, checked in by ramon <ramongomez@…>, 7 years ago

#718: Creado script launchOgagentInstaller para programar la instalación de agente OGAgent en el siguiente arranque del sistema operativo de un equipo modelo.

git-svn-id: https://opengnsys.es/svn/branches/version1.1@5597 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100755
File size: 5.7 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 --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        eval $DOWNLOAD "$TMPDIR/$OGAGENTFILE" "https://$(ogGetServerIp)/opengnsys/descargas/$OGAGENTFILE" 2>/dev/null
45        if ogListSoftware $1 $2 | grep -qi "opengnsys agent"; then
46            echo "OGAgent for Windows is already installed, you need to uninstall it before re-install."
47        else
48            if [ -s "$TMPDIR/$OGAGENTFILE" ]; then
49                # Run once OGAgent Installer.
50                if hivexsh -w << EOT 2>/dev/null; then
51load $HIVE
52cd \\Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce
53setval 1
54Install OGAgent
55string:C:\\Windows\\Temp\\$OGAGENTFILE"
56commit
57close
58exit
59EOT
60                    echo "Scheduled OGAgent installation after \"$3\" logon"
61                    echo "  (edit config file after finish)."
62                else
63                    ogRaiseError $OG_ERR_NOTWRITE "$1 $2 .../$3/NTUSER.DAT"
64                    exit $OG_ERR_NOTWRITE
65                fi
66            else
67                ogRaiseError $OG_ERR_NOTFOUND "$1 $2 /Windows/Temp/$OGAGENTFILE"
68                exit $OG_ERR_NOTFOUND
69            fi
70        fi
71        ;;
72    Linux)    # OGAgent for Linux (only deb and redhat-based backages; TODO suse-based package).
73        if ogListSoftware $1 $2 | grep -qi "ogagent"; then
74            echo "OGAgent for Linux is already installed, you need to uninstall it before re-install."
75        else
76            SYSTEMDDIR="$MNTDIR/lib/systemd"
77            [ -d "$SYSTEMDDIR" -a -d "${SYSTEMDDIR/lib/etc}" ] || ogRaiseError $OG_ERR_NOTFOUND "$1 $2 systemd" || exit $OG_ERR_NOTFOUND
78            # Downloading OGAgent installer for Linux.
79            if [ -e $MNTDIR/etc/debian_version ]; then    # Debian-based
80                OGAGENTFILE="ogagent_${OGVERSION/pre/}_all.deb"
81                CODE="if ! dpkg -l ogagent &>/dev/null && [ -f /var/tmp/$OGAGENTFILE ]; then apt-get update; apt-get install -y /var/tmp/$OGAGENTFILE; fi"
82            fi
83            if [ -e $MNTDIR/etc/redhat-release ]; then    # RedHat-based
84                OGAGENTFILE="ogagent-${OGVERSION/pre/}-1.noarch.rpm"
85                CODE="if ! rpm -q ogagent &>/dev/null && [ -f /var/tmp/$OGAGENTFILE ]; then yum install -y /var/tmp/$OGAGENTFILE; fi"
86            fi
87            [ -n "$OGAGENTFILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$1 $2 ogagent" || exit $OG_ERR_NOTFOUND
88            TMPDIR="$MNTDIR/var/tmp"
89            eval $DOWNLOAD "$TMPDIR/$OGAGENTFILE" "https://$(ogGetServerIp)/opengnsys/descargas/$OGAGENTFILE" 2>/dev/null
90            if [ -s "$TMPDIR/$OGAGENTFILE" ]; then
91                # Creating systemd script.
92                cat << EOT > $SYSTEMDDIR/systemd-launchogagent
93#!/bin/bash
94[ $EUID = 0 ] || exit 4
95start() {
96    $CODE
97    sed -i "0,/remote=/ s,remote=.*,remote=https://$(ogGetServerIp)/opengnsys/rest/," /usr/share/OGAgent/cfg/ogagent.cfg
98    service ogagent start
99}
100restart() {
101    service ogagent stop
102    if [ -f /var/tmp/$OGAGENTFILE ]; then
103        apt-get update
104        apt-get install -y --reinstall /var/tmp/$OGAGENTFILE
105    fi
106    sed -i "0,/remote=/ s,remote=.*,remote=https://$(ogGetServerIp)/opengnsys/rest/," /usr/share/OGAgent/cfg/ogagent.cfg
107    service ogagent start
108}
109
110case "\$1" in
111        start|restart)  "\$1" ;;
112esac
113EOT
114                chmod +x $SYSTEMDDIR/systemd-launchogagent
115                # Creating systemd service.
116                cat << EOT > $SYSTEMDDIR/system/launchogagent.service
117[Unit]
118Description=Installing and configuring OGAgent
119
120[Service]
121Type=oneshot
122RemainAfterExit=yes
123ExecStart=/lib/systemd/systemd-launchogagent start
124TimeoutStartSec=5min
125
126[Install]
127WantedBy=multi-user.target
128EOT
129                ln -fs /lib/systemd/system/launchogagent.service \
130                       ${SYSTEMDDIR/lib/etc}/system/multi-user.target.wants
131                echo "Scheduled OGAgent installation at next boot"
132                echo "  (process will be executed in the background, do not shutdown until finish)."
133            else
134                ogRaiseError $OG_ERR_NOTFOUND "$1 $2 /var/tmp/$OGAGENTFILE"
135                exit $OG_ERR_NOTFOUND
136            fi
137        fi
138        ;;
139    MacOS)    # OGAgent for macOS (TODO).
140        echo "OGAgent installer for macOS is not implemented yet."
141        ;;
142    *)        # OS not detected or OGAgent not implemented.
143        ogRaiseError $OG_ERR_NOTOS "$1 $2"
144        exit $OG_ERR_NOTOS
145        ;;
146esac
147
Note: See TracBrowser for help on using the repository browser.