| 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(){ |
| Igor D.C | bf669b9 | 2017-10-23 13:36:03 +0000 | [diff] [blame] | 28 | echo -e "usage: sudo -E $0 [OPTIONS]" |
| tierno | aa5832d | 2016-12-07 16:20:25 +0100 | [diff] [blame] | 29 | echo -e "Configures openmano to run as a service at /opt" |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 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" |
| tierno | aa5832d | 2016-12-07 16:20:25 +0100 | [diff] [blame] | 33 | echo -e " -d --delete: if -f is provided, delete this path after copying to /opt" |
| 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 | 1a13243 | 2017-05-08 18:37:05 +0200 | [diff] [blame] | 39 | echo "systemctl disable openmano.service " && systemctl disable openmano.service 2>/dev/null || |
| 40 | echo " Already done" |
| 41 | echo "systemctl disable osm-ro.service " && systemctl disable osm-ro.service 2>/dev/null || |
| 42 | echo " Already done" |
| tierno | 2677765 | 2016-11-15 17:33:13 +0000 | [diff] [blame] | 43 | echo "service openmano stop " && service openmano stop 2>/dev/null || echo " Already done" |
| garciadeblas | de590c0 | 2017-04-24 13:17:01 +0200 | [diff] [blame] | 44 | echo "service osm-ro stop " && service osm-ro stop 2>/dev/null || echo " Already done" |
| tierno | 1a13243 | 2017-05-08 18:37:05 +0200 | [diff] [blame] | 45 | for file in /opt/openmano /etc/default/openmanod.cfg /etc/osm/openmanod.cfg /var/log/openmano /var/log/osm/openmano* \ |
| 46 | /etc/systemd/system/openmano.service /etc/systemd/system/osm-ro.service /usr/bin/openmano /usr/sbin/service-openmano \ |
| 47 | /usr/bin/openmano-report |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 48 | do |
| tierno | 2677765 | 2016-11-15 17:33:13 +0000 | [diff] [blame] | 49 | echo rm $file |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 50 | rm -rf $file || ! echo "Can not delete '$file'. Needed root privileges?" >&2 || exit 1 |
| 51 | done |
| 52 | echo "Done" |
| 53 | } |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 54 | |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 55 | GIT_URL=https://osm.etsi.org/gerrit/osm/RO.git |
| 56 | USER_OWNER="root" |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 57 | QUIET_MODE="" |
| 58 | FILE="" |
| tierno | aa5832d | 2016-12-07 16:20:25 +0100 | [diff] [blame] | 59 | DELETE="" |
| 60 | while getopts ":u:f:hdq-:" o; do |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 61 | case "${o}" in |
| 62 | u) |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 63 | export USER_OWNER="$OPTARG" |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 64 | ;; |
| 65 | f) |
| 66 | export FILE="$OPTARG" |
| 67 | ;; |
| 68 | q) |
| 69 | export QUIET_MODE=yes |
| 70 | ;; |
| 71 | h) |
| 72 | usage && exit 0 |
| 73 | ;; |
| tierno | aa5832d | 2016-12-07 16:20:25 +0100 | [diff] [blame] | 74 | d) |
| 75 | DELETE=y |
| 76 | ;; |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 77 | -) |
| 78 | [ "${OPTARG}" == "help" ] && usage && exit 0 |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 79 | [ "${OPTARG}" == "uninstall" ] && uninstall && exit 0 |
| tierno | aa5832d | 2016-12-07 16:20:25 +0100 | [diff] [blame] | 80 | [ "${OPTARG}" == "delete" ] && DELETE=y && continue |
| 81 | echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2 |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 82 | exit 1 |
| 83 | ;; |
| 84 | \?) |
| 85 | echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2 |
| 86 | exit 1 |
| 87 | ;; |
| 88 | :) |
| 89 | echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2 |
| 90 | exit 1 |
| 91 | ;; |
| 92 | *) |
| 93 | usage >&2 |
| 94 | exit -1 |
| 95 | ;; |
| 96 | esac |
| 97 | done |
| tierno | 2677765 | 2016-11-15 17:33:13 +0000 | [diff] [blame] | 98 | BAD_PATH_ERROR="Path '$FILE' does not contain a valid openmano distribution" |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 99 | |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 100 | #check root privileges |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 101 | [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1 |
| 102 | |
| 103 | #Discover Linux distribution |
| 104 | #try redhat type |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 105 | if [[ -f /etc/redhat-release ]] |
| 106 | then |
| 107 | _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut -d" " -f1) |
| 108 | else |
| 109 | #if not assuming ubuntu type |
| 110 | _DISTRO=$(lsb_release -is 2>/dev/null) |
| 111 | fi |
| 112 | if [[ "$_DISTRO" == "Ubuntu" ]] |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 113 | then |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 114 | _RELEASE=$(lsb_release -rs) |
| 115 | if [[ ${_RELEASE%%.*} != 16 ]] |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 116 | then |
| 117 | echo "Only tested in Ubuntu Server 16.04" >&2 && exit 1 |
| 118 | fi |
| 119 | else |
| 120 | echo "Only tested in Ubuntu Server 16.04" >&2 && exit 1 |
| 121 | fi |
| 122 | |
| 123 | |
| tierno | 2677765 | 2016-11-15 17:33:13 +0000 | [diff] [blame] | 124 | if [[ -z "$FILE" ]] |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 125 | then |
| tierno | 2677765 | 2016-11-15 17:33:13 +0000 | [diff] [blame] | 126 | FILE=__temp__${RANDOM} |
| 127 | git clone $GIT_URL $FILE || ! echo "Cannot get openmano source code from $GIT_URL" >&2 || exit 1 |
| tierno | aa5832d | 2016-12-07 16:20:25 +0100 | [diff] [blame] | 128 | DELETE=y |
| tierno | 2677765 | 2016-11-15 17:33:13 +0000 | [diff] [blame] | 129 | else |
| 130 | [[ -d "$FILE" ]] || ! echo $BAD_PATH_ERROR >&2 || exit 1 |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 131 | fi |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 132 | |
| garciadeblas | 89b3d84 | 2016-09-19 15:18:33 +0200 | [diff] [blame] | 133 | #make idempotent |
| tierno | 2677765 | 2016-11-15 17:33:13 +0000 | [diff] [blame] | 134 | uninstall |
| 135 | #copy files |
| 136 | cp -r "$FILE" /opt/openmano || ! echo $BAD_PATH_ERROR >&2 || exit 1 |
| garciadeblas | d117ff0 | 2017-04-21 11:35:32 +0200 | [diff] [blame] | 137 | mkdir -p /etc/osm || echo "warning cannot create config folder '/etc/osm'" |
| tierno | 1a13243 | 2017-05-08 18:37:05 +0200 | [diff] [blame] | 138 | cp /opt/openmano/osm_ro/openmanod.cfg /etc/osm/openmanod.cfg || |
| 139 | echo "warning cannot create file '/etc/osm/openmanod.cfg'" |
| garciadeblas | d117ff0 | 2017-04-21 11:35:32 +0200 | [diff] [blame] | 140 | mkdir -p /var/log/osm || echo "warning cannot create log folder '/var/log/osm'" |
| tierno | 2677765 | 2016-11-15 17:33:13 +0000 | [diff] [blame] | 141 | #makes links |
| tierno | 2677765 | 2016-11-15 17:33:13 +0000 | [diff] [blame] | 142 | ln -s -v /opt/openmano/openmano /usr/bin/openmano |
| garciadeblas | de590c0 | 2017-04-24 13:17:01 +0200 | [diff] [blame] | 143 | ln -s -v /opt/openmano/scripts/service-openmano /usr/sbin/service-openmano |
| 144 | ln -s -v /opt/openmano/scripts/openmano-report /usr/bin/openmano-report |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 145 | |
| tierno | aa5832d | 2016-12-07 16:20:25 +0100 | [diff] [blame] | 146 | chown -R $SUDO_USER /opt/openmano |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 147 | |
| garciadeblas | 89b3d84 | 2016-09-19 15:18:33 +0200 | [diff] [blame] | 148 | mkdir -p /etc/systemd/system/ |
| garciadeblas | 26a580c | 2017-04-24 12:32:40 +0200 | [diff] [blame] | 149 | cat > /etc/systemd/system/osm-ro.service << EOF |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 150 | [Unit] |
| 151 | Description=openmano server |
| 152 | |
| 153 | [Service] |
| tierno | 45a5285 | 2016-08-26 14:39:42 +0200 | [diff] [blame] | 154 | User=${USER_OWNER} |
| garciadeblas | d117ff0 | 2017-04-21 11:35:32 +0200 | [diff] [blame] | 155 | ExecStart=/opt/openmano/openmanod -c /etc/osm/openmanod.cfg --log-file=/var/log/osm/openmano.log |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 156 | Restart=always |
| 157 | |
| 158 | [Install] |
| 159 | WantedBy=multi-user.target |
| 160 | EOF |
| 161 | |
| tierno | 1a13243 | 2017-05-08 18:37:05 +0200 | [diff] [blame] | 162 | [[ -n $DELETE ]] && rm -rf "${FILE}" |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 163 | |
| garciadeblas | 26a580c | 2017-04-24 12:32:40 +0200 | [diff] [blame] | 164 | service osm-ro start |
| 165 | systemctl enable osm-ro.service |
| tierno | 205d102 | 2016-07-21 11:26:22 +0200 | [diff] [blame] | 166 | |
| 167 | echo Done |
| 168 | exit |