Updated install-openmano-service to use the new config and service files
[osm/RO.git] / scripts / install-openmano-service.sh
1 #!/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
27 function usage(){
28 echo -e "usage: sudo $0 [OPTIONS]"
29 echo -e "Configures openmano to run as a service at /opt"
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 openmano source is located. If missing it is downloaded from git"
33 echo -e " -d --delete: if -f is provided, delete this path after copying to /opt"
34 echo -e " -h: show this help"
35 echo -e " --uninstall: remove created service and files"
36 }
37
38 function uninstall(){
39 echo "systemctl disable openmano.service " && systemctl disable openmano.service 2>/dev/null || echo " Already done"
40 echo "systemctl disable openmano.service " && systemctl disable osm-ro.service 2>/dev/null || echo " Already done"
41 echo "service openmano stop " && service openmano stop 2>/dev/null || echo " Already done"
42 echo "service osm-ro stop " && service openmano stop 2>/dev/null || echo " Already done"
43 for file in /opt/openmano /etc/default/openmanod.cfg /etc/osm/openmanod.cfg /var/log/openmano /var/log/osm/openmano* /etc/systemd/system/openmano.service /etc/systemd/system/osm-ro.service /usr/bin/openmano /usr/sbin/service-openmano /usr/bin/openmano-report
44 do
45 echo rm $file
46 rm -rf $file || ! echo "Can not delete '$file'. Needed root privileges?" >&2 || exit 1
47 done
48 echo "Done"
49 }
50
51 GIT_URL=https://osm.etsi.org/gerrit/osm/RO.git
52 USER_OWNER="root"
53 QUIET_MODE=""
54 FILE=""
55 DELETE=""
56 while getopts ":u:f:hdq-:" o; do
57 case "${o}" in
58 u)
59 export USER_OWNER="$OPTARG"
60 ;;
61 f)
62 export FILE="$OPTARG"
63 ;;
64 q)
65 export QUIET_MODE=yes
66 ;;
67 h)
68 usage && exit 0
69 ;;
70 d)
71 DELETE=y
72 ;;
73 -)
74 [ "${OPTARG}" == "help" ] && usage && exit 0
75 [ "${OPTARG}" == "uninstall" ] && uninstall && exit 0
76 [ "${OPTARG}" == "delete" ] && DELETE=y && continue
77 echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
78 exit 1
79 ;;
80 \?)
81 echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2
82 exit 1
83 ;;
84 :)
85 echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2
86 exit 1
87 ;;
88 *)
89 usage >&2
90 exit -1
91 ;;
92 esac
93 done
94 BAD_PATH_ERROR="Path '$FILE' does not contain a valid openmano distribution"
95
96 #check root privileges
97 [ "$USER" != "root" ] && echo "Needed root privileges" >&2 && exit 1
98
99 #Discover Linux distribution
100 #try redhat type
101 if [[ -f /etc/redhat-release ]]
102 then
103 _DISTRO=$(cat /etc/redhat-release 2>/dev/null | cut -d" " -f1)
104 else
105 #if not assuming ubuntu type
106 _DISTRO=$(lsb_release -is 2>/dev/null)
107 fi
108 if [[ "$_DISTRO" == "Ubuntu" ]]
109 then
110 _RELEASE=$(lsb_release -rs)
111 if [[ ${_RELEASE%%.*} != 16 ]]
112 then
113 echo "Only tested in Ubuntu Server 16.04" >&2 && exit 1
114 fi
115 else
116 echo "Only tested in Ubuntu Server 16.04" >&2 && exit 1
117 fi
118
119
120 if [[ -z "$FILE" ]]
121 then
122 FILE=__temp__${RANDOM}
123 git clone $GIT_URL $FILE || ! echo "Cannot get openmano source code from $GIT_URL" >&2 || exit 1
124 DELETE=y
125 else
126 [[ -d "$FILE" ]] || ! echo $BAD_PATH_ERROR >&2 || exit 1
127 fi
128
129 #make idempotent
130 uninstall
131 #copy files
132 cp -r "$FILE" /opt/openmano || ! echo $BAD_PATH_ERROR >&2 || exit 1
133 mkdir -p /etc/osm || echo "warning cannot create config folder '/etc/osm'"
134 cp /opt/openmano/osm_ro/openmanod.cfg /etc/osm/openmanod.cfg || echo "warning cannot create file '/etc/osm/openmanod.cfg'"
135 mkdir -p /var/log/osm || echo "warning cannot create log folder '/var/log/osm'"
136 #makes links
137 ln -s -v /opt/openmano/openmano /usr/bin/openmano
138 ln -s -v /opt/openmano/scripts/service-openmano.sh /usr/sbin/service-openmano
139 ln -s -v /opt/openmano/scripts/openmano-report.sh /usr/bin/openmano-report
140
141 chown -R $SUDO_USER /opt/openmano
142
143 mkdir -p /etc/systemd/system/
144 cat > /etc/systemd/system/osm-ro.service << EOF
145 [Unit]
146 Description=openmano server
147
148 [Service]
149 User=${USER_OWNER}
150 ExecStart=/opt/openmano/openmanod -c /etc/osm/openmanod.cfg --log-file=/var/log/osm/openmano.log
151 Restart=always
152
153 [Install]
154 WantedBy=multi-user.target
155 EOF
156
157 [[ -n $DELETE ]] && rm -rf ${FILE}
158
159 service osm-ro start
160 systemctl enable osm-ro.service
161
162 echo Done
163 exit