| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | ## |
| 4 | # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U. |
| 5 | # This file is part of openmano |
| 6 | # All Rights Reserved. |
| 7 | # |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | # not use this file except in compliance with the License. You may obtain |
| 10 | # a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 17 | # License for the specific language governing permissions and limitations |
| 18 | # under the License. |
| 19 | # |
| 20 | # For those usages not covered by the Apache License, Version 2.0 please |
| 21 | # contact with: nfvlabs@tid.es |
| 22 | ## |
| 23 | |
| 24 | #ONLY TESTED for Ubuntu 16.04 |
| 25 | #it configures openmano to run as a service |
| 26 | |
| 27 | function usage(){ |
| 28 | echo -e "usage: sudo $0 [OPTIONS]" |
| 29 | echo -e "Configures openmano to run as a service" |
| 30 | echo -e " OPTIONS" |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 31 | echo -e " -u USER_OWNER user owner of the service, 'root' by default" |
| 32 | echo -e " -f PATH path where openmano source is located. If missing it is downloaded from git" |
| 33 | #echo -e " -q: install in an unattended mode" |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 34 | echo -e " -h: show this help" |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 35 | echo -e " --uninstall: remove created service and files" |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 36 | } |
| 37 | |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 38 | function uninstall(){ |
| tierno | 2677765 | 2016-11-15 17:33:13 +0000 | [diff] [blame] | 39 | echo "systemctl disable openmano.service " && systemctl disable openmano.service 2>/dev/null || echo " Already done" |
| 40 | echo "service openmano stop " && service openmano stop 2>/dev/null || echo " Already done" |
| 41 | for file in /opt/openmano /etc/default/openmanod.cfg /var/log/openmano /etc/systemd/system/openmano.service /usr/bin/openmano /usr/sbin/service-openmano /usr/bin/openmano-report |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 42 | do |
| tierno | 2677765 | 2016-11-15 17:33:13 +0000 | [diff] [blame] | 43 | echo rm $file |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 44 | rm -rf $file || ! echo "Can not delete '$file'. Needed root privileges?" >&2 || exit 1 |
| 45 | done |
| 46 | echo "Done" |
| 47 | } |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 48 | |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 49 | GIT_URL=https://osm.etsi.org/gerrit/osm/RO.git |
| 50 | USER_OWNER="root" |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 51 | QUIET_MODE="" |
| 52 | FILE="" |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 53 | while getopts ":u:f:hq-:" o; do |
| 54 | case "${o}" in |
| 55 | u) |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 56 | export USER_OWNER="$OPTARG" |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 57 | ;; |
| 58 | f) |
| 59 | export FILE="$OPTARG" |
| 60 | ;; |
| 61 | q) |
| 62 | export QUIET_MODE=yes |
| 63 | ;; |
| 64 | h) |
| 65 | usage && exit 0 |
| 66 | ;; |
| 67 | -) |
| 68 | [ "${OPTARG}" == "help" ] && usage && exit 0 |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 69 | [ "${OPTARG}" == "uninstall" ] && uninstall && exit 0 |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 70 | echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2 |
| 71 | exit 1 |
| 72 | ;; |
| 73 | \?) |
| 74 | echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2 |
| 75 | exit 1 |
| 76 | ;; |
| 77 | :) |
| 78 | echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2 |
| 79 | exit 1 |
| 80 | ;; |
| 81 | *) |
| 82 | usage >&2 |
| 83 | exit -1 |
| 84 | ;; |
| 85 | esac |
| 86 | done |
| tierno | 2677765 | 2016-11-15 17:33:13 +0000 | [diff] [blame] | 87 | BAD_PATH_ERROR="Path '$FILE' does not contain a valid openmano distribution" |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 88 | |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 89 | #check root privileges |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 90 | [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1 |
| 91 | |
| 92 | #Discover Linux distribution |
| 93 | #try redhat type |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 94 | if [[ -f /etc/redhat-release ]] |
| 95 | then |
| 96 | _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut -d" " -f1) |
| 97 | else |
| 98 | #if not assuming ubuntu type |
| 99 | _DISTRO=$(lsb_release -is 2>/dev/null) |
| 100 | fi |
| 101 | if [[ "$_DISTRO" == "Ubuntu" ]] |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 102 | then |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 103 | _RELEASE=$(lsb_release -rs) |
| 104 | if [[ ${_RELEASE%%.*} != 16 ]] |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 105 | then |
| 106 | echo "Only tested in Ubuntu Server 16.04" >&2 && exit 1 |
| 107 | fi |
| 108 | else |
| 109 | echo "Only tested in Ubuntu Server 16.04" >&2 && exit 1 |
| 110 | fi |
| 111 | |
| 112 | |
| tierno | 2677765 | 2016-11-15 17:33:13 +0000 | [diff] [blame] | 113 | if [[ -z "$FILE" ]] |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 114 | then |
| tierno | 2677765 | 2016-11-15 17:33:13 +0000 | [diff] [blame] | 115 | FILE=__temp__${RANDOM} |
| 116 | git clone $GIT_URL $FILE || ! echo "Cannot get openmano source code from $GIT_URL" >&2 || exit 1 |
| 117 | else |
| 118 | [[ -d "$FILE" ]] || ! echo $BAD_PATH_ERROR >&2 || exit 1 |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 119 | fi |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 120 | |
| garciadeblas | 89b3d84 | 2016-09-19 15:18:33 +0200 | [diff] [blame] | 121 | #make idempotent |
| tierno | 2677765 | 2016-11-15 17:33:13 +0000 | [diff] [blame] | 122 | uninstall |
| 123 | #copy files |
| 124 | cp -r "$FILE" /opt/openmano || ! echo $BAD_PATH_ERROR >&2 || exit 1 |
| tierno | c50b435 | 2016-09-14 15:54:54 +0200 | [diff] [blame] | 125 | mkdir -p /opt/openmano/logs |
| tierno | 2677765 | 2016-11-15 17:33:13 +0000 | [diff] [blame] | 126 | #makes links |
| 127 | ln -s -v /opt/openmano/openmanod.cfg /etc/default/openmanod.cfg || echo "warning cannot create link '/etc/default/openmanod.cfg'" |
| 128 | ln -s -v /opt/openmano/logs /var/log/openmano || echo "warning cannot create link '/var/log/openmano'" |
| 129 | ln -s -v /opt/openmano/openmano /usr/bin/openmano |
| 130 | ln -s -v /opt/openmano/scripts/service-openmano.sh /usr/sbin/service-openmano |
| 131 | ln -s -v /opt/openmano/scripts/openmano-report.sh /usr/bin/openmano-report |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 132 | |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 133 | chown -R $USER_OWNER /opt/openmano |
| 134 | |
| garciadeblas | 89b3d84 | 2016-09-19 15:18:33 +0200 | [diff] [blame] | 135 | mkdir -p /etc/systemd/system/ |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 136 | cat > /etc/systemd/system/openmano.service << EOF |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 137 | [Unit] |
| 138 | Description=openmano server |
| 139 | |
| 140 | [Service] |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 141 | User=${USER_OWNER} |
| 142 | ExecStart=/opt/openmano/openmanod.py -c /opt/openmano/openmanod.cfg --log-file=/opt/openmano/logs/openmano.log |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 143 | Restart=always |
| 144 | |
| 145 | [Install] |
| 146 | WantedBy=multi-user.target |
| 147 | EOF |
| 148 | |
| tierno | 2677765 | 2016-11-15 17:33:13 +0000 | [diff] [blame] | 149 | rm -rf ${FILE} |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 150 | |
| 151 | service openmano start |
| tierno | 2677765 | 2016-11-15 17:33:13 +0000 | [diff] [blame] | 152 | systemctl enable openmano.service |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 153 | |
| 154 | echo Done |
| 155 | exit |