blob: c19b345294e45230556015eb4e4b3775ed2e5e17 [file] [log] [blame]
tierno205d1022016-07-21 11:26:22 +02001#!/bin/bash
2
3##
tierno92021022018-09-12 16:29:23 +02004# Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
tierno205d1022016-07-21 11:26:22 +02005# 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(){
Igor D.Cbf669b92017-10-23 13:36:03 +000028 echo -e "usage: sudo -E $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(){
tierno1a132432017-05-08 18:37:05 +020039 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"
tierno26777652016-11-15 17:33:13 +000043 echo "service openmano stop " && service openmano stop 2>/dev/null || echo " Already done"
garciadeblasde590c02017-04-24 13:17:01 +020044 echo "service osm-ro stop " && service osm-ro stop 2>/dev/null || echo " Already done"
tierno1a132432017-05-08 18:37:05 +020045 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
tierno45a52852016-08-26 14:39:42 +020048 do
tierno26777652016-11-15 17:33:13 +000049 echo rm $file
tierno45a52852016-08-26 14:39:42 +020050 rm -rf $file || ! echo "Can not delete '$file'. Needed root privileges?" >&2 || exit 1
51 done
52 echo "Done"
53}
tierno205d1022016-07-21 11:26:22 +020054
tierno45a52852016-08-26 14:39:42 +020055GIT_URL=https://osm.etsi.org/gerrit/osm/RO.git
56USER_OWNER="root"
tierno205d1022016-07-21 11:26:22 +020057QUIET_MODE=""
58FILE=""
tiernoaa5832d2016-12-07 16:20:25 +010059DELETE=""
60while getopts ":u:f:hdq-:" o; do
tierno205d1022016-07-21 11:26:22 +020061 case "${o}" in
62 u)
tierno45a52852016-08-26 14:39:42 +020063 export USER_OWNER="$OPTARG"
tierno205d1022016-07-21 11:26:22 +020064 ;;
65 f)
66 export FILE="$OPTARG"
67 ;;
68 q)
69 export QUIET_MODE=yes
70 ;;
71 h)
72 usage && exit 0
73 ;;
tiernoaa5832d2016-12-07 16:20:25 +010074 d)
75 DELETE=y
76 ;;
tierno205d1022016-07-21 11:26:22 +020077 -)
78 [ "${OPTARG}" == "help" ] && usage && exit 0
tierno45a52852016-08-26 14:39:42 +020079 [ "${OPTARG}" == "uninstall" ] && uninstall && exit 0
tiernoaa5832d2016-12-07 16:20:25 +010080 [ "${OPTARG}" == "delete" ] && DELETE=y && continue
81 echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
tierno205d1022016-07-21 11:26:22 +020082 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
97done
tierno26777652016-11-15 17:33:13 +000098BAD_PATH_ERROR="Path '$FILE' does not contain a valid openmano distribution"
tierno205d1022016-07-21 11:26:22 +020099
tierno45a52852016-08-26 14:39:42 +0200100#check root privileges
tierno205d1022016-07-21 11:26:22 +0200101[ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
102
103#Discover Linux distribution
104#try redhat type
tierno45a52852016-08-26 14:39:42 +0200105if [[ -f /etc/redhat-release ]]
106then
107 _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut -d" " -f1)
108else
109 #if not assuming ubuntu type
110 _DISTRO=$(lsb_release -is 2>/dev/null)
111fi
112if [[ "$_DISTRO" == "Ubuntu" ]]
tierno205d1022016-07-21 11:26:22 +0200113then
tierno45a52852016-08-26 14:39:42 +0200114 _RELEASE=$(lsb_release -rs)
115 if [[ ${_RELEASE%%.*} != 16 ]]
tierno205d1022016-07-21 11:26:22 +0200116 then
117 echo "Only tested in Ubuntu Server 16.04" >&2 && exit 1
118 fi
119else
120 echo "Only tested in Ubuntu Server 16.04" >&2 && exit 1
121fi
122
123
tierno26777652016-11-15 17:33:13 +0000124if [[ -z "$FILE" ]]
tierno205d1022016-07-21 11:26:22 +0200125then
tierno26777652016-11-15 17:33:13 +0000126 FILE=__temp__${RANDOM}
127 git clone $GIT_URL $FILE || ! echo "Cannot get openmano source code from $GIT_URL" >&2 || exit 1
tiernoaa5832d2016-12-07 16:20:25 +0100128 DELETE=y
tierno26777652016-11-15 17:33:13 +0000129else
130 [[ -d "$FILE" ]] || ! echo $BAD_PATH_ERROR >&2 || exit 1
tierno205d1022016-07-21 11:26:22 +0200131fi
tierno45a52852016-08-26 14:39:42 +0200132
garciadeblas89b3d842016-09-19 15:18:33 +0200133#make idempotent
tierno26777652016-11-15 17:33:13 +0000134uninstall
135#copy files
136cp -r "$FILE" /opt/openmano || ! echo $BAD_PATH_ERROR >&2 || exit 1
garciadeblasd117ff02017-04-21 11:35:32 +0200137mkdir -p /etc/osm || echo "warning cannot create config folder '/etc/osm'"
tierno1a132432017-05-08 18:37:05 +0200138cp /opt/openmano/osm_ro/openmanod.cfg /etc/osm/openmanod.cfg ||
139 echo "warning cannot create file '/etc/osm/openmanod.cfg'"
garciadeblasd117ff02017-04-21 11:35:32 +0200140mkdir -p /var/log/osm || echo "warning cannot create log folder '/var/log/osm'"
tierno26777652016-11-15 17:33:13 +0000141#makes links
tierno26777652016-11-15 17:33:13 +0000142ln -s -v /opt/openmano/openmano /usr/bin/openmano
garciadeblasde590c02017-04-24 13:17:01 +0200143ln -s -v /opt/openmano/scripts/service-openmano /usr/sbin/service-openmano
144ln -s -v /opt/openmano/scripts/openmano-report /usr/bin/openmano-report
tierno45a52852016-08-26 14:39:42 +0200145
tiernoaa5832d2016-12-07 16:20:25 +0100146chown -R $SUDO_USER /opt/openmano
tierno45a52852016-08-26 14:39:42 +0200147
garciadeblas89b3d842016-09-19 15:18:33 +0200148mkdir -p /etc/systemd/system/
garciadeblas26a580c2017-04-24 12:32:40 +0200149cat > /etc/systemd/system/osm-ro.service << EOF
tierno205d1022016-07-21 11:26:22 +0200150[Unit]
151Description=openmano server
152
153[Service]
tierno45a52852016-08-26 14:39:42 +0200154User=${USER_OWNER}
garciadeblasd117ff02017-04-21 11:35:32 +0200155ExecStart=/opt/openmano/openmanod -c /etc/osm/openmanod.cfg --log-file=/var/log/osm/openmano.log
tierno205d1022016-07-21 11:26:22 +0200156Restart=always
157
158[Install]
159WantedBy=multi-user.target
160EOF
161
tierno1a132432017-05-08 18:37:05 +0200162[[ -n $DELETE ]] && rm -rf "${FILE}"
tierno205d1022016-07-21 11:26:22 +0200163
garciadeblas26a580c2017-04-24 12:32:40 +0200164service osm-ro start
165systemctl enable osm-ro.service
tierno205d1022016-07-21 11:26:22 +0200166
167echo Done
168exit