blob: 05384ea53c69047b08fa411ec30a0defe62f4e6d [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]"
29 echo -e "Configures openvim to run as a service"
30 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"
33 #echo -e " -q: install in an unattended mode"
34 echo -e " -h: show this help"
35 echo -e " --uninstall: remove created service and files"
36}
37
38function uninstall(){
39 service openvim stop
tierno3e9a1472016-09-20 11:05:26 +000040 for file in /opt/openvim /etc/default/openvimd.cfg /var/log/openvim /etc/systemd/system/openvim.service /usr/sbin/openvim /usr/sbin/initopenvim /usr/sbin/openvim
tiernof13617a2016-09-08 11:42:10 +020041 do
42 rm -rf $file || ! echo "Can not delete '$file'. Needed root privileges?" >&2 || exit 1
43 done
44 echo "Done"
45}
46
47BAD_PATH_ERROR="Path '$FILE' does not contain a valid openvim distribution"
48GIT_URL=https://osm.etsi.org/gerrit/osm/RO.git
49USER_OWNER="root"
50QUIET_MODE=""
51FILE=""
52DELETE=""
53while getopts ":u:f:hq-:" o; do
54 case "${o}" in
55 u)
56 export USER_OWNER="$OPTARG"
57 ;;
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
69 [ "${OPTARG}" == "uninstall" ] && uninstall && exit 0
70 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
88#check root privileges
89[ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
90
91#Discover Linux distribution
92#try redhat type
93if [[ -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" ]]
101then
102 _RELEASE=$(lsb_release -rs)
103 if [[ ${_RELEASE%%.*} != 16 ]]
104 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
114 git clone $GIT_URL __temp__ || ! echo "Cannot get openvim source code from $GIT_URL" >&2 || exit 1
115 #git checkout <tag version>
116 FILE=./__temp__
117 DELETE=y
118fi
119
120#make idenpotent
121rm -rf /opt/openvim
122rm -f /etc/default/openvimd.cfg
123rm -f /var/log/openvim
124cp -r $FILE /opt/openvim || ! echo $BAD_PATH_ERROR >&2 || exit 1
125mkdir -p /opt/openvim/logs
tierno7cf6fee2016-09-14 15:57:52 +0200126rm -rf /usr/sbin/openvim
tierno3e9a1472016-09-20 11:05:26 +0000127rm -rf /usr/sbin/initopenvim
tiernof13617a2016-09-08 11:42:10 +0200128#cp ${FILE}/openvim /usr/sbin/ || ! echo $BAD_PATH_ERROR >&2 || exit 1
129ln -s /opt/openvim/openvimd.cfg /etc/default/openvimd.cfg || echo "warning cannot create link '/etc/default/openvimd.cfg'"
130ln -s /opt/openvim/logs /var/log/openvim || echo "warning cannot create link '/var/log/openvim'"
tierno7cf6fee2016-09-14 15:57:52 +0200131ln -s /opt/openvim/openvim /usr/sbin/openvim
tierno3e9a1472016-09-20 11:05:26 +0000132ln -s /opt/openvim/scripts/service-openvim.sh /usr/sbin/initopenvim
tiernof13617a2016-09-08 11:42:10 +0200133
134chown $USER_OWNER /opt/openvim/openvimd.cfg
135chown -R $USER_OWNER /opt/openvim
136
tierno3e9a1472016-09-20 11:05:26 +0000137mkdir -p /etc/systemd/system/
tiernof13617a2016-09-08 11:42:10 +0200138cat > /etc/systemd/system/openvim.service << EOF
139[Unit]
140Description=openvim server
141
142[Service]
143User=${USER_OWNER}
144ExecStart=/opt/openvim/openvimd.py -c /opt/openvim/openvimd.cfg --log-file=/opt/openvim/logs/openvim.log
145Restart=always
146
147[Install]
148WantedBy=multi-user.target
149EOF
150
151[[ -n $DELETE ]] && rm -rf $FILE
152
153service openvim start
154
155echo Done
156exit