blob: d9f933fe9860841d13614b200ccf062c34eec5c8 [file] [log] [blame]
tiernof13617a2016-09-08 11:42:10 +02001#!/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
27function usage(){
28 echo -e "usage: sudo $0 [OPTIONS]"
tierno1c84ac02016-12-07 16:27:21 +010029 echo -e "Configures openvim to run as a service at /opt"
tiernof13617a2016-09-08 11:42:10 +020030 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"
tierno1c84ac02016-12-07 16:27:21 +010033 echo -e " -d --delete: if -f is provided, delete this path after copying to /opt"
tiernof13617a2016-09-08 11:42:10 +020034 echo -e " -h: show this help"
35 echo -e " --uninstall: remove created service and files"
36}
37
38function uninstall(){
tierno1c84ac02016-12-07 16:27:21 +010039 echo "systemctl disable openvim.service " && systemctl disable openvim.service 2>/dev/null || echo " Already done"
40 echo "service openvim stop " && service openvim stop 2>/dev/null || echo " Already done"
41 for file in /opt/openvim /etc/default/openvimd.cfg /var/log/openvim /etc/systemd/system/openvim.service /usr/bin/openvim /usr/bin/initopenvim /usr/sbin/service-openvim /usr/bin/openvim-report
tiernof13617a2016-09-08 11:42:10 +020042 do
43 rm -rf $file || ! echo "Can not delete '$file'. Needed root privileges?" >&2 || exit 1
44 done
45 echo "Done"
46}
47
48BAD_PATH_ERROR="Path '$FILE' does not contain a valid openvim distribution"
tierno1c84ac02016-12-07 16:27:21 +010049GIT_URL=https://osm.etsi.org/gerrit/osm/openvim.git
tiernof13617a2016-09-08 11:42:10 +020050USER_OWNER="root"
51QUIET_MODE=""
52FILE=""
53DELETE=""
tierno1c84ac02016-12-07 16:27:21 +010054while getopts ":u:f:hdq-:" o; do
tiernof13617a2016-09-08 11:42:10 +020055 case "${o}" in
56 u)
57 export USER_OWNER="$OPTARG"
58 ;;
59 f)
60 export FILE="$OPTARG"
61 ;;
62 q)
63 export QUIET_MODE=yes
64 ;;
65 h)
66 usage && exit 0
67 ;;
tierno1c84ac02016-12-07 16:27:21 +010068 d)
69 DELETE=y
70 ;;
tiernof13617a2016-09-08 11:42:10 +020071 -)
72 [ "${OPTARG}" == "help" ] && usage && exit 0
73 [ "${OPTARG}" == "uninstall" ] && uninstall && exit 0
tierno1c84ac02016-12-07 16:27:21 +010074 [ "${OPTARG}" == "delete" ] && DELETE=y && continue
75 echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
tiernof13617a2016-09-08 11:42:10 +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
tierno1c84ac02016-12-07 16:27:21 +010092BAD_PATH_ERROR="Path '$FILE' does not contain a valid openvim distribution"
tiernof13617a2016-09-08 11:42:10 +020093
94#check root privileges
95[ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
96
97#Discover Linux distribution
98#try redhat type
99if [[ -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" ]]
107then
108 _RELEASE=$(lsb_release -rs)
109 if [[ ${_RELEASE%%.*} != 16 ]]
110 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
118if [[ -z $FILE ]]
119then
tierno1c84ac02016-12-07 16:27:21 +0100120 FILE=__temp__${RANDOM}
121 git clone $GIT_URL $FILE || ! echo "Cannot get openvim source code from $GIT_URL" >&2 || exit 1
tiernof13617a2016-09-08 11:42:10 +0200122 DELETE=y
tierno1c84ac02016-12-07 16:27:21 +0100123 #git checkout <tag version>
124else
125 [[ -d "$FILE" ]] || ! echo $BAD_PATH_ERROR >&2 || exit 1
tiernof13617a2016-09-08 11:42:10 +0200126fi
127
128#make idenpotent
tierno1c84ac02016-12-07 16:27:21 +0100129uninstall
130#copy files
131cp -r "$FILE" /opt/openvim || ! echo $BAD_PATH_ERROR >&2 || exit 1
tiernof13617a2016-09-08 11:42:10 +0200132mkdir -p /opt/openvim/logs
tierno1c84ac02016-12-07 16:27:21 +0100133#makes links
134ln -s -v /opt/openvim/openvimd.cfg /etc/default/openvimd.cfg || echo "warning cannot create link '/etc/default/openvimd.cfg'"
135ln -s -v /opt/openvim/logs /var/log/openvim || echo "warning cannot create link '/var/log/openvim'"
136ln -s -v /opt/openvim/openvim /usr/bin/openvim
137ln -s -v /opt/openvim/scripts/service-openvim.sh /usr/sbin/service-openvim
138ln -s -v /opt/openvim/scripts/openvim-report.sh /usr/bin/openvim-report
139ln -s -v /opt/openvim/scripts/initopenvim.sh /usr/bin/initopenvim
tiernof13617a2016-09-08 11:42:10 +0200140
tierno1c84ac02016-12-07 16:27:21 +0100141chown -R $SUDO_USER /opt/openvim
tiernof13617a2016-09-08 11:42:10 +0200142
tierno3e9a1472016-09-20 11:05:26 +0000143mkdir -p /etc/systemd/system/
tiernof13617a2016-09-08 11:42:10 +0200144cat > /etc/systemd/system/openvim.service << EOF
145[Unit]
146Description=openvim server
147
148[Service]
149User=${USER_OWNER}
150ExecStart=/opt/openvim/openvimd.py -c /opt/openvim/openvimd.cfg --log-file=/opt/openvim/logs/openvim.log
151Restart=always
152
153[Install]
154WantedBy=multi-user.target
155EOF
156
157[[ -n $DELETE ]] && rm -rf $FILE
158
159service openvim start
tierno1c84ac02016-12-07 16:27:21 +0100160systemctl enable openvim.service
tiernof13617a2016-09-08 11:42:10 +0200161
162echo Done
163exit