blob: a909dd1693eda381cf7cc529447e2ee7387d4b44 [file] [log] [blame]
tierno205d1022016-07-21 11:26:22 +02001#!/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
27function usage(){
28 echo -e "usage: sudo $0 [OPTIONS]"
tiernoaa5832d2016-12-07 16:20:25 +010029 echo -e "Configures openmano to run as a service at /opt"
tierno205d1022016-07-21 11:26:22 +020030 echo -e " OPTIONS"
tierno45a52852016-08-26 14:39:42 +020031 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"
tiernoaa5832d2016-12-07 16:20:25 +010033 echo -e " -d --delete: if -f is provided, delete this path after copying to /opt"
tierno205d1022016-07-21 11:26:22 +020034 echo -e " -h: show this help"
tierno45a52852016-08-26 14:39:42 +020035 echo -e " --uninstall: remove created service and files"
tierno205d1022016-07-21 11:26:22 +020036}
37
tierno45a52852016-08-26 14:39:42 +020038function uninstall(){
tierno26777652016-11-15 17:33:13 +000039 echo "systemctl disable openmano.service " && systemctl disable openmano.service 2>/dev/null || echo " Already done"
garciadeblasde590c02017-04-24 13:17:01 +020040 echo "systemctl disable osm-ro.service " && systemctl disable osm-ro.service 2>/dev/null || echo " Already done"
tierno26777652016-11-15 17:33:13 +000041 echo "service openmano stop " && service openmano stop 2>/dev/null || echo " Already done"
garciadeblasde590c02017-04-24 13:17:01 +020042 echo "service osm-ro stop " && service osm-ro stop 2>/dev/null || echo " Already done"
garciadeblas26a580c2017-04-24 12:32:40 +020043 for file in /opt/openmano /etc/default/openmanod.cfg /etc/osm/openmanod.cfg /var/log/openmano /var/log/osm/openmano* /etc/systemd/system/openmano.service /etc/systemd/system/osm-ro.service /usr/bin/openmano /usr/sbin/service-openmano /usr/bin/openmano-report
tierno45a52852016-08-26 14:39:42 +020044 do
tierno26777652016-11-15 17:33:13 +000045 echo rm $file
tierno45a52852016-08-26 14:39:42 +020046 rm -rf $file || ! echo "Can not delete '$file'. Needed root privileges?" >&2 || exit 1
47 done
48 echo "Done"
49}
tierno205d1022016-07-21 11:26:22 +020050
tierno45a52852016-08-26 14:39:42 +020051GIT_URL=https://osm.etsi.org/gerrit/osm/RO.git
52USER_OWNER="root"
tierno205d1022016-07-21 11:26:22 +020053QUIET_MODE=""
54FILE=""
tiernoaa5832d2016-12-07 16:20:25 +010055DELETE=""
56while getopts ":u:f:hdq-:" o; do
tierno205d1022016-07-21 11:26:22 +020057 case "${o}" in
58 u)
tierno45a52852016-08-26 14:39:42 +020059 export USER_OWNER="$OPTARG"
tierno205d1022016-07-21 11:26:22 +020060 ;;
61 f)
62 export FILE="$OPTARG"
63 ;;
64 q)
65 export QUIET_MODE=yes
66 ;;
67 h)
68 usage && exit 0
69 ;;
tiernoaa5832d2016-12-07 16:20:25 +010070 d)
71 DELETE=y
72 ;;
tierno205d1022016-07-21 11:26:22 +020073 -)
74 [ "${OPTARG}" == "help" ] && usage && exit 0
tierno45a52852016-08-26 14:39:42 +020075 [ "${OPTARG}" == "uninstall" ] && uninstall && exit 0
tiernoaa5832d2016-12-07 16:20:25 +010076 [ "${OPTARG}" == "delete" ] && DELETE=y && continue
77 echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
tierno205d1022016-07-21 11:26:22 +020078 exit 1
79 ;;
80 \?)
81 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
82 exit 1
83 ;;
84 :)
85 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
86 exit 1
87 ;;
88 *)
89 usage >&2
90 exit -1
91 ;;
92 esac
93done
tierno26777652016-11-15 17:33:13 +000094BAD_PATH_ERROR="Path '$FILE' does not contain a valid openmano distribution"
tierno205d1022016-07-21 11:26:22 +020095
tierno45a52852016-08-26 14:39:42 +020096#check root privileges
tierno205d1022016-07-21 11:26:22 +020097[ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
98
99#Discover Linux distribution
100#try redhat type
tierno45a52852016-08-26 14:39:42 +0200101if [[ -f /etc/redhat-release ]]
102then
103 _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut -d" " -f1)
104else
105 #if not assuming ubuntu type
106 _DISTRO=$(lsb_release -is 2>/dev/null)
107fi
108if [[ "$_DISTRO" == "Ubuntu" ]]
tierno205d1022016-07-21 11:26:22 +0200109then
tierno45a52852016-08-26 14:39:42 +0200110 _RELEASE=$(lsb_release -rs)
111 if [[ ${_RELEASE%%.*} != 16 ]]
tierno205d1022016-07-21 11:26:22 +0200112 then
113 echo "Only tested in Ubuntu Server 16.04" >&2 && exit 1
114 fi
115else
116 echo "Only tested in Ubuntu Server 16.04" >&2 && exit 1
117fi
118
119
tierno26777652016-11-15 17:33:13 +0000120if [[ -z "$FILE" ]]
tierno205d1022016-07-21 11:26:22 +0200121then
tierno26777652016-11-15 17:33:13 +0000122 FILE=__temp__${RANDOM}
123 git clone $GIT_URL $FILE || ! echo "Cannot get openmano source code from $GIT_URL" >&2 || exit 1
tiernoaa5832d2016-12-07 16:20:25 +0100124 DELETE=y
tierno26777652016-11-15 17:33:13 +0000125else
126 [[ -d "$FILE" ]] || ! echo $BAD_PATH_ERROR >&2 || exit 1
tierno205d1022016-07-21 11:26:22 +0200127fi
tierno45a52852016-08-26 14:39:42 +0200128
garciadeblas89b3d842016-09-19 15:18:33 +0200129#make idempotent
tierno26777652016-11-15 17:33:13 +0000130uninstall
131#copy files
132cp -r "$FILE" /opt/openmano || ! echo $BAD_PATH_ERROR >&2 || exit 1
garciadeblasd117ff02017-04-21 11:35:32 +0200133mkdir -p /etc/osm || echo "warning cannot create config folder '/etc/osm'"
134cp /opt/openmano/osm_ro/openmanod.cfg /etc/osm/openmanod.cfg || echo "warning cannot create file '/etc/osm/openmanod.cfg'"
135mkdir -p /var/log/osm || echo "warning cannot create log folder '/var/log/osm'"
tierno26777652016-11-15 17:33:13 +0000136#makes links
tierno26777652016-11-15 17:33:13 +0000137ln -s -v /opt/openmano/openmano /usr/bin/openmano
garciadeblasde590c02017-04-24 13:17:01 +0200138ln -s -v /opt/openmano/scripts/service-openmano /usr/sbin/service-openmano
139ln -s -v /opt/openmano/scripts/openmano-report /usr/bin/openmano-report
tierno45a52852016-08-26 14:39:42 +0200140
tiernoaa5832d2016-12-07 16:20:25 +0100141chown -R $SUDO_USER /opt/openmano
tierno45a52852016-08-26 14:39:42 +0200142
garciadeblas89b3d842016-09-19 15:18:33 +0200143mkdir -p /etc/systemd/system/
garciadeblas26a580c2017-04-24 12:32:40 +0200144cat > /etc/systemd/system/osm-ro.service << EOF
tierno205d1022016-07-21 11:26:22 +0200145[Unit]
146Description=openmano server
147
148[Service]
tierno45a52852016-08-26 14:39:42 +0200149User=${USER_OWNER}
garciadeblasd117ff02017-04-21 11:35:32 +0200150ExecStart=/opt/openmano/openmanod -c /etc/osm/openmanod.cfg --log-file=/var/log/osm/openmano.log
tierno205d1022016-07-21 11:26:22 +0200151Restart=always
152
153[Install]
154WantedBy=multi-user.target
155EOF
156
tiernoaa5832d2016-12-07 16:20:25 +0100157[[ -n $DELETE ]] && rm -rf ${FILE}
tierno205d1022016-07-21 11:26:22 +0200158
garciadeblas26a580c2017-04-24 12:32:40 +0200159service osm-ro start
160systemctl enable osm-ro.service
tierno205d1022016-07-21 11:26:22 +0200161
162echo Done
163exit