blob: dd840ce4f9cda57c5b5608f1fe58e838a2a0bdf1 [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]"
29 echo -e "Configures openmano to run as a service"
30 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"
33 #echo -e " -q: install in an unattended mode"
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(){
39 service openmano stop
tiernoc50b4352016-09-14 15:54:54 +020040 for file in /opt/openmano /etc/default/openmanod.cfg /var/log/openmano /etc/systemd/system/openmano.service /usr/sbin/openmano
tierno45a52852016-08-26 14:39:42 +020041 do
42 rm -rf $file || ! echo "Can not delete '$file'. Needed root privileges?" >&2 || exit 1
43 done
44 echo "Done"
45}
tierno205d1022016-07-21 11:26:22 +020046
tierno45a52852016-08-26 14:39:42 +020047BAD_PATH_ERROR="Path '$FILE' does not contain a valid openmano distribution"
48GIT_URL=https://osm.etsi.org/gerrit/osm/RO.git
49USER_OWNER="root"
tierno205d1022016-07-21 11:26:22 +020050QUIET_MODE=""
51FILE=""
52DELETE=""
53while getopts ":u:f:hq-:" o; do
54 case "${o}" in
55 u)
tierno45a52852016-08-26 14:39:42 +020056 export USER_OWNER="$OPTARG"
tierno205d1022016-07-21 11:26:22 +020057 ;;
58 f)
59 export FILE="$OPTARG"
60 ;;
61 q)
62 export QUIET_MODE=yes
63 ;;
64 h)
65 usage && exit 0
66 ;;
67 -)
68 [ "${OPTARG}" == "help" ] && usage && exit 0
tierno45a52852016-08-26 14:39:42 +020069 [ "${OPTARG}" == "uninstall" ] && uninstall && exit 0
tierno205d1022016-07-21 11:26:22 +020070 echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
71 exit 1
72 ;;
73 \?)
74 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
75 exit 1
76 ;;
77 :)
78 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
79 exit 1
80 ;;
81 *)
82 usage >&2
83 exit -1
84 ;;
85 esac
86done
87
tierno45a52852016-08-26 14:39:42 +020088#check root privileges
tierno205d1022016-07-21 11:26:22 +020089[ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
90
91#Discover Linux distribution
92#try redhat type
tierno45a52852016-08-26 14:39:42 +020093if [[ -f /etc/redhat-release ]]
94then
95 _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut -d" " -f1)
96else
97 #if not assuming ubuntu type
98 _DISTRO=$(lsb_release -is 2>/dev/null)
99fi
100if [[ "$_DISTRO" == "Ubuntu" ]]
tierno205d1022016-07-21 11:26:22 +0200101then
tierno45a52852016-08-26 14:39:42 +0200102 _RELEASE=$(lsb_release -rs)
103 if [[ ${_RELEASE%%.*} != 16 ]]
tierno205d1022016-07-21 11:26:22 +0200104 then
105 echo "Only tested in Ubuntu Server 16.04" >&2 && exit 1
106 fi
107else
108 echo "Only tested in Ubuntu Server 16.04" >&2 && exit 1
109fi
110
111
112if [[ -z $FILE ]]
113then
tierno45a52852016-08-26 14:39:42 +0200114 git clone $GIT_URL __temp__ || ! echo "Cannot get openmano source code from $GIT_URL" >&2 || exit 1
115 #git checkout <tag version>
116 FILE=./__temp__
tierno205d1022016-07-21 11:26:22 +0200117 DELETE=y
118fi
tierno45a52852016-08-26 14:39:42 +0200119
garciadeblas89b3d842016-09-19 15:18:33 +0200120#make idempotent
tierno45a52852016-08-26 14:39:42 +0200121rm -rf /opt/openmano
122rm -f /etc/default/openmanod.cfg
123rm -f /var/log/openmano
124cp -r $FILE /opt/openmano || ! echo $BAD_PATH_ERROR >&2 || exit 1
tiernoc50b4352016-09-14 15:54:54 +0200125mkdir -p /opt/openmano/logs
126rm -rf /usr/sbin/openmano
tierno45a52852016-08-26 14:39:42 +0200127#cp ${FILE}/openmano /usr/sbin/ || ! echo $BAD_PATH_ERROR >&2 || exit 1
128ln -s /opt/openmano/openmanod.cfg /etc/default/openmanod.cfg || echo "warning cannot create link '/etc/default/openmanod.cfg'"
129ln -s /opt/openmano/logs /var/log/openmano || echo "warning cannot create link '/var/log/openmano'"
tiernoc50b4352016-09-14 15:54:54 +0200130ln -s /opt/openmano/openmano /usr/sbin/openmano
tierno45a52852016-08-26 14:39:42 +0200131
132chown $USER_OWNER /opt/openmano/openmanod.cfg
133chown -R $USER_OWNER /opt/openmano
134
garciadeblas89b3d842016-09-19 15:18:33 +0200135mkdir -p /etc/systemd/system/
tierno45a52852016-08-26 14:39:42 +0200136cat > /etc/systemd/system/openmano.service << EOF
tierno205d1022016-07-21 11:26:22 +0200137[Unit]
138Description=openmano server
139
140[Service]
tierno45a52852016-08-26 14:39:42 +0200141User=${USER_OWNER}
142ExecStart=/opt/openmano/openmanod.py -c /opt/openmano/openmanod.cfg --log-file=/opt/openmano/logs/openmano.log
tierno205d1022016-07-21 11:26:22 +0200143Restart=always
144
145[Install]
146WantedBy=multi-user.target
147EOF
148
149[[ -n $DELETE ]] && rm -rf $FILE
150
151service openmano start
152
153echo Done
154exit