4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openvim
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
12 # http://www.apache.org/licenses/LICENSE-2.0
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
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
24 #ONLY TESTED for Ubuntu 16.04
25 #it configures openvim to run as a service
28 echo -e "usage: sudo $0 [OPTIONS]"
29 echo -e "Configures openvim to run as a service"
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"
40 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
42 rm -rf $file ||
! echo "Can not delete '$file'. Needed root privileges?" >&2 ||
exit 1
47 BAD_PATH_ERROR
="Path '$FILE' does not contain a valid openvim distribution"
48 GIT_URL
=https
://osm.etsi.org
/gerrit
/osm
/RO.git
53 while getopts ":u:f:hq-:" o
; do
56 export USER_OWNER
="$OPTARG"
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
74 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
78 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
88 #check root privileges
89 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
91 #Discover Linux distribution
93 if [[ -f /etc
/redhat-release
]]
95 _DISTRO
=$
(cat /etc
/redhat-release
2>/dev
/null | cut
-d" " -f1)
97 #if not assuming ubuntu type
98 _DISTRO
=$
(lsb_release
-is 2>/dev
/null
)
100 if [[ "$_DISTRO" == "Ubuntu" ]]
102 _RELEASE
=$
(lsb_release
-rs)
103 if [[ ${_RELEASE%%.*} != 16 ]]
105 echo "Only tested in Ubuntu Server 16.04" >&2 && exit 1
108 echo "Only tested in Ubuntu Server 16.04" >&2 && exit 1
114 git clone
$GIT_URL __temp__ ||
! echo "Cannot get openvim source code from $GIT_URL" >&2 ||
exit 1
115 #git checkout <tag version>
122 rm -f /etc
/default
/openvimd.cfg
123 rm -f /var
/log
/openvim
124 cp -r $FILE /opt
/openvim ||
! echo $BAD_PATH_ERROR >&2 ||
exit 1
125 mkdir
-p /opt
/openvim
/logs
126 rm -rf /usr
/sbin
/openvim
127 rm -rf /usr
/sbin
/initopenvim
128 #cp ${FILE}/openvim /usr/sbin/ || ! echo $BAD_PATH_ERROR >&2 || exit 1
129 ln -s /opt
/openvim
/openvimd.cfg
/etc
/default
/openvimd.cfg ||
echo "warning cannot create link '/etc/default/openvimd.cfg'"
130 ln -s /opt
/openvim
/logs
/var
/log
/openvim ||
echo "warning cannot create link '/var/log/openvim'"
131 ln -s /opt
/openvim
/openvim
/usr
/sbin
/openvim
132 ln -s /opt
/openvim
/scripts
/service-openvim.sh
/usr
/sbin
/initopenvim
134 chown
$USER_OWNER /opt
/openvim
/openvimd.cfg
135 chown
-R $USER_OWNER /opt
/openvim
137 mkdir
-p /etc
/systemd
/system
/
138 cat > /etc
/systemd
/system
/openvim.service
<< EOF
140 Description=openvim server
144 ExecStart=/opt/openvim/openvimd.py -c /opt/openvim/openvimd.cfg --log-file=/opt/openvim/logs/openvim.log
148 WantedBy=multi-user.target
151 [[ -n $DELETE ]] && rm -rf $FILE
153 service openvim start