blob: 54783ad927484f81db13a1829811687af52ee5bb [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"
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
tierno45a52852016-08-26 14:39:42 +020042 do
tierno26777652016-11-15 17:33:13 +000043 echo rm $file
tierno45a52852016-08-26 14:39:42 +020044 rm -rf $file || ! echo "Can not delete '$file'. Needed root privileges?" >&2 || exit 1
45 done
46 echo "Done"
47}
tierno205d1022016-07-21 11:26:22 +020048
tierno45a52852016-08-26 14:39:42 +020049GIT_URL=https://osm.etsi.org/gerrit/osm/RO.git
50USER_OWNER="root"
tierno205d1022016-07-21 11:26:22 +020051QUIET_MODE=""
52FILE=""
tiernoaa5832d2016-12-07 16:20:25 +010053DELETE=""
54while getopts ":u:f:hdq-:" o; do
tierno205d1022016-07-21 11:26:22 +020055 case "${o}" in
56 u)
tierno45a52852016-08-26 14:39:42 +020057 export USER_OWNER="$OPTARG"
tierno205d1022016-07-21 11:26:22 +020058 ;;
59 f)
60 export FILE="$OPTARG"
61 ;;
62 q)
63 export QUIET_MODE=yes
64 ;;
65 h)
66 usage && exit 0
67 ;;
tiernoaa5832d2016-12-07 16:20:25 +010068 d)
69 DELETE=y
70 ;;
tierno205d1022016-07-21 11:26:22 +020071 -)
72 [ "${OPTARG}" == "help" ] && usage && exit 0
tierno45a52852016-08-26 14:39:42 +020073 [ "${OPTARG}" == "uninstall" ] && uninstall && exit 0
tiernoaa5832d2016-12-07 16:20:25 +010074 [ "${OPTARG}" == "delete" ] && DELETE=y && continue
75 echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
tierno205d1022016-07-21 11:26:22 +020076 exit 1
77 ;;
78 \?)
79 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
80 exit 1
81 ;;
82 :)
83 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
84 exit 1
85 ;;
86 *)
87 usage >&2
88 exit -1
89 ;;
90 esac
91done
tierno26777652016-11-15 17:33:13 +000092BAD_PATH_ERROR="Path '$FILE' does not contain a valid openmano distribution"
tierno205d1022016-07-21 11:26:22 +020093
tierno45a52852016-08-26 14:39:42 +020094#check root privileges
tierno205d1022016-07-21 11:26:22 +020095[ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
96
97#Discover Linux distribution
98#try redhat type
tierno45a52852016-08-26 14:39:42 +020099if [[ -f /etc/redhat-release ]]
100then
101 _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut -d" " -f1)
102else
103 #if not assuming ubuntu type
104 _DISTRO=$(lsb_release -is 2>/dev/null)
105fi
106if [[ "$_DISTRO" == "Ubuntu" ]]
tierno205d1022016-07-21 11:26:22 +0200107then
tierno45a52852016-08-26 14:39:42 +0200108 _RELEASE=$(lsb_release -rs)
109 if [[ ${_RELEASE%%.*} != 16 ]]
tierno205d1022016-07-21 11:26:22 +0200110 then
111 echo "Only tested in Ubuntu Server 16.04" >&2 && exit 1
112 fi
113else
114 echo "Only tested in Ubuntu Server 16.04" >&2 && exit 1
115fi
116
117
tierno26777652016-11-15 17:33:13 +0000118if [[ -z "$FILE" ]]
tierno205d1022016-07-21 11:26:22 +0200119then
tierno26777652016-11-15 17:33:13 +0000120 FILE=__temp__${RANDOM}
121 git clone $GIT_URL $FILE || ! echo "Cannot get openmano source code from $GIT_URL" >&2 || exit 1
tiernoaa5832d2016-12-07 16:20:25 +0100122 DELETE=y
tierno26777652016-11-15 17:33:13 +0000123else
124 [[ -d "$FILE" ]] || ! echo $BAD_PATH_ERROR >&2 || exit 1
tierno205d1022016-07-21 11:26:22 +0200125fi
tierno45a52852016-08-26 14:39:42 +0200126
garciadeblas89b3d842016-09-19 15:18:33 +0200127#make idempotent
tierno26777652016-11-15 17:33:13 +0000128uninstall
129#copy files
130cp -r "$FILE" /opt/openmano || ! echo $BAD_PATH_ERROR >&2 || exit 1
tiernoc50b4352016-09-14 15:54:54 +0200131mkdir -p /opt/openmano/logs
tierno26777652016-11-15 17:33:13 +0000132#makes links
133ln -s -v /opt/openmano/openmanod.cfg /etc/default/openmanod.cfg || echo "warning cannot create link '/etc/default/openmanod.cfg'"
134ln -s -v /opt/openmano/logs /var/log/openmano || echo "warning cannot create link '/var/log/openmano'"
135ln -s -v /opt/openmano/openmano /usr/bin/openmano
136ln -s -v /opt/openmano/scripts/service-openmano.sh /usr/sbin/service-openmano
137ln -s -v /opt/openmano/scripts/openmano-report.sh /usr/bin/openmano-report
tierno45a52852016-08-26 14:39:42 +0200138
tiernoaa5832d2016-12-07 16:20:25 +0100139chown -R $SUDO_USER /opt/openmano
tierno45a52852016-08-26 14:39:42 +0200140
garciadeblas89b3d842016-09-19 15:18:33 +0200141mkdir -p /etc/systemd/system/
tierno45a52852016-08-26 14:39:42 +0200142cat > /etc/systemd/system/openmano.service << EOF
tierno205d1022016-07-21 11:26:22 +0200143[Unit]
144Description=openmano server
145
146[Service]
tierno45a52852016-08-26 14:39:42 +0200147User=${USER_OWNER}
148ExecStart=/opt/openmano/openmanod.py -c /opt/openmano/openmanod.cfg --log-file=/opt/openmano/logs/openmano.log
tierno205d1022016-07-21 11:26:22 +0200149Restart=always
150
151[Install]
152WantedBy=multi-user.target
153EOF
154
tiernoaa5832d2016-12-07 16:20:25 +0100155[[ -n $DELETE ]] && rm -rf ${FILE}
tierno205d1022016-07-21 11:26:22 +0200156
157service openmano start
tierno26777652016-11-15 17:33:13 +0000158systemctl enable openmano.service
tierno205d1022016-07-21 11:26:22 +0200159
160echo Done
161exit