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