de45014e0cde282dbf107da6e90d3dd07d9dbe94
[ealt-edge.git] / example-apps / ROBO / aPaaS / Obj_Detection_service / configs / start.sh
1 #!/bin/bash
2 # Copyright 2020 Huawei Technologies Co., Ltd.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # Validates if ip is valid
17 validate_ip()
18 {
19  ip_var="$1"
20     # checks if variable is unset
21  if [ -z "$ip_var" ] ; then
22     echo "ip is not set"
23     return 1
24  fi
25
26  if ! echo "$ip_var" | grep -qE '^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)' ; then
27    return 1
28  fi
29  return 0
30 }
31
32 validate_name() {
33   hostname="$1"
34   len="${#hostname}"
35   if [ "${len}" -gt "64" ]; then
36     return 1
37   fi
38   if ! echo "$hostname" | grep -qE '^[a-zA-Z0-9]*$|^[a-zA-Z0-9][a-zA-Z0-9_\-]*[a-zA-Z0-9]$'; then
39     return 1
40   fi
41   return 0
42 }
43
44 # validates whether file exist
45 validate_file_exists() {
46   file_path="$1"
47
48   # checks variable is unset
49   if [ -z "$file_path" ]; then
50     echo "file path variable is not set"
51     return 1
52   fi
53
54   # checks if file exists
55   if [ ! -f "$file_path" ]; then
56     echo "file does not exist"
57     return 1
58   fi
59
60   return 0
61 }
62
63 validate_ip "$LISTEN_IP"
64 valid_listen_ip="$?"
65 if [ ! "$valid_listen_ip" -eq "0" ]; then
66   echo "invalid ip address for listen ip"
67   exit 1
68 fi
69
70 if [ ! -z "$SERVER_NAME" ]; then
71   validate_name "$SERVER_NAME"
72   valid_name="$?"
73   if [ ! "$valid_name" -eq "0" ]; then
74     echo "invalid ssl server name"
75     exit 1
76   fi
77 fi
78
79 # ssl parameters validation
80 validate_file_exists "/usr/app/ssl/server_tls.crt"
81 valid_ssl_server_cert="$?"
82 if [ ! "$valid_ssl_server_cert" -eq "0" ]; then
83   echo "invalid ssl server certificate"
84   exit 1
85 fi
86
87 # ssl parameters validation
88 validate_file_exists "/usr/app/ssl/server_tls.key"
89 valid_ssl_server_key="$?"
90 if [ ! "$valid_ssl_server_key" -eq "0" ]; then
91   echo "invalid ssl server key"
92   exit 1
93 fi
94
95 # ssl parameters validation
96 validate_file_exists "/usr/app/ssl/ca.crt"
97 valid_ssl_ca_crt="$?"
98 if [ ! "$valid_ssl_ca_crt" -eq "0" ]; then
99   echo "invalid ssl ca cert"
100   exit 1
101 fi
102
103 echo "Running Monitoring Service"
104 umask 0027
105 cd /usr/app || exit
106 python run.py