RIFT OSM R1 Initial Submission
[osm/SO.git] / rwlaunchpad / plugins / rwimagemgr / bin / glance_start_wrapper
1 #!/bin/bash
2 #
3 # A single executable which starts necessary glance server components
4 #
5 # Create a workspace-specific glance config directory and
6 # wrap the glance-api and glance-registry procs.
7 #
8 #
9 # USAGE: ./glance_start_wrapper <glance_conf_dir>
10 #
11 #
12
13 if [ $# -ne 1 ]; then
14     echo "error: specifiy the glance conf dir"
15     exit 1
16 fi
17
18 src_conf_dir="$1"
19 if [ ! -d "${src_conf_dir}" ]; then
20     echo "error: glance conf dir does not exist"
21     exit 1
22 fi
23
24 if [ -z ${RIFT_INSTALL+x} ]; then
25     echo "error: RIFT_INSTALL is not set"
26     exit 1
27 fi
28
29 if [ -z "${RIFT_VAR_ROOT}" ]; then
30     if [ -n "${RIFT_INSTALL}" ]; then
31         RIFT_VAR_ROOT="${RIFT_INSTALL}/var"
32     else
33         RIFT_VAR_ROOT="$(mktemp -d)"
34         echo "warning: RIFT_VAR_ROOT or RIFT_INSTALL not provided, using temporary directory"
35     fi
36 fi
37
38 dest_conf_dir="${RIFT_VAR_ROOT}/glance/conf"
39 echo "destination glance conf directory: ${dest_conf_dir}"
40
41 if [ -e "${dest_conf_dir}" ]; then
42     echo "removing ${dest_conf_dir}"
43     #rm -rf "${dest_conf_dir}"
44 fi
45
46 mkdir -p "${dest_conf_dir}"
47
48 for conf_file in ${src_conf_dir}/*; do
49     cp "${conf_file}" ${dest_conf_dir}/
50     dest_file="${dest_conf_dir}/$(basename ${conf_file})"
51     sed -i "s|{RIFT_VAR_ROOT}|${RIFT_VAR_ROOT}|g" "${dest_file}"
52     sed -i "s|{RIFT_INSTALL}|${RIFT_INSTALL}|g" "${dest_file}"
53 done
54
55 mkdir -p ${RIFT_VAR_ROOT}/log/glance
56
57 registry_pid=0
58 api_pid=0
59 killing=false
60
61 function kill_children(){
62     if ${killing}; then
63         return
64     fi
65     killing=true
66
67     if [ ${registry_pid} -ne 0 ]; then
68         kill ${registry_pid} 2>/dev/null
69     fi
70
71     if [ ${api_pid} -ne 0 ]; then
72         kill ${api_pid} 2>/dev/null
73     fi
74
75     sleep 2
76
77     if [ ${registry_pid} -ne 0 ]; then
78         echo "KILL registry pid: ${registry_pid}"
79         kill -9 ${registry_pid} 2>/dev/null
80     fi
81
82     if [ ${api_pid} -ne 0 ]; then
83         echo "KILL api pid: ${api_pid}"
84         kill -9 ${api_pid} 2>/dev/null
85     fi
86
87     exit 1
88 }
89
90
91 function kill_group(){
92     # Kill any remaining children
93     kill_children
94
95     # Kill myself
96     kill -9 0
97 }
98
99 trap "kill_children" SIGHUP SIGINT SIGTERM SIGTRAP EXIT
100 trap "kill_group" SIGCHLD
101
102 glance-registry --config-dir ${dest_conf_dir} --config-file ${dest_conf_dir}/glance-registry.conf >/dev/null 2>&1&
103 registry_pid="$!"
104 if [ $? -ne 0 ]; then
105     echo "ERROR: Glance registry startup failed!" >&2
106     exit 1
107 fi
108
109 glance-api --config-dir ${dest_conf_dir} --config-file ${dest_conf_dir}/glance-api.conf >/dev/null 2>&1&
110 api_pid="$!"
111 if [ $? -ne 0 ]; then
112     echo "ERROR: Glance registry startup failed!" >&2
113     exit 1
114 fi
115
116 sleep 5
117
118 manage_cfg=""
119 if [ -e "${dest_conf_dir}/glance-manage.conf" ]; then
120     manage_cfg="--config-file ${dest_conf_dir}/glance-manage.conf"
121 fi
122
123 glance-manage --config-dir ${dest_conf_dir} ${manage_cfg} db_sync >/dev/null 2>&1&
124 if [ $? -ne 0 ]; then
125     echo "ERROR: glance-manage db_sync failed" >&2
126     exit 1
127 fi
128
129 while true; do
130     sleep 1
131 done