]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/support-scripts/brick_ctl.sh
LP1809288: Make some brt fields read-only
[Evergreen.git] / Open-ILS / src / support-scripts / brick_ctl.sh
1 #!/bin/bash
2 # ---------------------------------------------------------------
3 # Copyright (C) 2007-2008  Georgia Public Library Service
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 # ---------------------------------------------------------------
15
16 # -------------------------------------------------------------------
17 # This script is used to manage "bricks", which are collections of
18 # servers all serving a single OpenSRF domain.  There will be 1
19 # master machine, which will typcically run this script, and 1 or more
20 # drones, which respond to this script.
21 # -------------------------------------------------------------------
22
23 # TODO finish the download and build functionality
24
25
26 [ -f /etc/profile ] && . /etc/profile
27 [ -f ~/.bashrc ] && . ~/.bashrc
28
29 DEFAULT_CONFIG=~/.oils_brick.cfg
30
31
32 # -------------------------------------------------------------------
33 # Make sure we're the opensrf user
34 # -------------------------------------------------------------------
35 [ $(whoami) != 'opensrf' ] && echo 'Must run as user "opensrf"' && exit;
36
37
38 function usage {
39     echo "";    
40     echo "usage: $0 -a <action>"
41     echo "  -u <base_url> : host + path URL where the download file lives";
42     echo "  -f <build_file> : the name of the bundle to fetch";
43     echo "  -x <xul_dir> : staff client build directory";
44     echo "  -i <xul_build_id> : staff client build ID";
45     echo "  -s <service_name> : apply action only to specified service";
46     echo "Actions:";    
47     echo "  fetch";
48     echo "  start_osrf";
49     echo "  start_all";
50     echo "  stop_osrf";
51     echo "  stop_all";
52     echo "  restart_osrf";
53     echo "  restart_all";
54     echo "  build";
55     echo "  build_xul";
56     echo "  detach_brick";
57     echo "  attach_brick";
58     exit 0;
59 }
60
61
62 # -------------------------------------------------------------------
63 # Load the config opts
64 # -------------------------------------------------------------------
65 while getopts  "a:x:bf:hu:c:i:s:l" flag; do
66     case $flag in   
67         "a") OPT_ACTION="$OPTARG";;
68         "u") OPT_FETCH_BASE_URL="$OPTARG";;
69         "f") OPT_FETCH_FILE="$OPTARG";;
70         "x") OPT_XUL_BUILD_DIR="$OPTARG";;
71         "i") OPT_XUL_BUILD_ID="$OPTARG";;
72         "c") OPT_CONFIG_FILE="$OPTARG";;
73         "l") OPT_LOCALHOST="-l";;
74         "s") OPT_SERVICE="$OPTARG";;
75         "h"|*) usage;;
76     esac;
77 done
78
79 # -------------------------------------------------------------------
80 # Load the config file
81 # -------------------------------------------------------------------
82 if [ -e "$OPT_CONFIG_FILE" ]; then
83     . $OPT_CONFIG_FILE;
84 else
85     if [ -e ~/.oils_brick.cfg ]; then
86         . $DEFAULT_CONFIG; 
87     else
88         echo "Please specify a valid config file or create one at $DEFAULT_CONFIG";
89     fi;
90 fi;
91
92 [ -n "$OPT_LOCALHOST" ] && SERVICE_LOCALHOST_FLAG="--localhost";
93
94 # make sure an action was specified
95 [ -z "$OPT_ACTION" ] && usage;
96
97 LOCAL_BASE="osrf_control $OPT_LOCALHOST --pid-dir $OSRF_PID_DIR --config $OSRF_CONFIG";
98 DRONE_BASE=". /etc/profile && osrf_control --pid-dir $OSRF_PID_DIR --config $OSRF_CONFIG";
99 SERVICE_CONTROLLER="osrf_control $SERVICE_LOCALHOST_FLAG --config $OSRF_CONFIG --pid-dir $OSRF_PID_DIR --service $OPT_SERVICE";
100
101 # -------------------------------------------------------------------
102 # Runs DRONE_ACT on the drones, then LOCAL_ACT on the local machine
103 # -------------------------------------------------------------------
104 function drone_first {
105     LOCAL_ACT=$1
106     DRONE_ACT=$2
107     echo "drone_first(): $LOCAL_ACT";
108     for drone in ${DRONES[@]:0}; do
109         echo "* $drone"
110         ssh "$drone" "$DRONE_ACT";
111     done;   
112     $LOCAL_ACT;
113 }
114
115 # -------------------------------------------------------------------
116 # Runs LOCAL_ACT on the local machine then DRONE_ACT on the drones
117 # -------------------------------------------------------------------
118 function local_first {
119     LOCAL_ACT=$1
120     DRONE_ACT=$2
121     echo  "local_first(): $LOCAL_ACT";
122     $LOCAL_ACT;
123     for drone in ${DRONES[@]:0}; do
124         echo "* $drone"
125         ssh "$drone" "$DRONE_ACT";
126     done;   
127 }
128
129 function make_xul {
130     DIR="$XUL_BASE/$OPT_XUL_BUILD_DIR";
131     echo "Building XUL and copying to $DIR";
132     cd "$OILS_SRC_DIR/Open-ILS/xul/staff_client" 
133     make clean;
134     make STAFF_CLIENT_BUILD_ID="$OPT_XUL_BUILD_ID";
135     cd "$XUL_BASE";
136     mkdir -p "$DIR";
137     cd "$DIR/..";
138     cp -r "$OILS_SRC_DIR/Open-ILS/xul/staff_client/build/server" "$DIR";
139     echo -e "\nLinking to new build directory: $OPT_XUL_BUILD_ID -> $DIR\n";
140     rm -f "$OPT_XUL_BUILD_ID";
141     rm -f current;
142     ln -s "$OPT_XUL_BUILD_DIR" current;
143     ln -s current "$OPT_XUL_BUILD_ID";
144 }
145
146 function detach_brick {
147     echo -n "Detaching brick...";
148
149     [ ! -f "$LDIRECTOR_FILE" ] && \
150         echo "ping file already moved, skipping ..." && return 0;
151
152     mv -f "$LDIRECTOR_FILE" "$LDIRECTOR_FILE-"
153     x=10;
154     while(sleep 1); do
155         x=$(expr $x - 1);
156         echo -n " $x ";
157         [ $x == 0 ] && break;
158     done;
159     echo "";
160 }
161
162 function fetch_build {
163
164     [ -z "$OPT_FETCH_BASE_URL" -o -z "$OPT_FETCH_FILE" ] && \
165         echo "I need a build URL and a bundle file..." && exit;
166     
167     NEW_DIR=${OPT_FETCH_FILE:0:$(expr ${#OPT_FETCH_FILE} - 7)};
168
169     if [ ! -d "$NEW_DIR" ]; then
170     
171         echo "Fetching archive...  $OPT_FETCH_BASE_URL$OPT_FETCH_FILE";
172         wget -q "$OPT_FETCH_BASE_URL$OPT_FETCH_FILE";
173     
174         [ ! -f "$OPT_FETCH_FILE" ] && \
175             echo "Unable to fetch $OPT_FETCH_FILE!" && exit;
176         
177         # unpack the new build
178         echo "Unpacking archive..."
179         tar -zxf $OPT_FETCH_FILE;
180         cp "current/install.conf" "$NEW_DIR/"
181         rm $OPT_FETCH_FILE;
182     fi;
183
184     rm current;
185     ln -s $NEW_DIR current;
186 }
187
188 # This is a per-service action.  Currently only support in Perl (and Python).
189 # When other active languages are added, this script will need a language param
190 # to determine which controller script to call.
191 if [ -n "$OPT_SERVICE" ]; then
192     case $OPT_ACTION in
193         "start_osrf") local_first "$SERVICE_CONTROLLER --start" \
194             "$SERVICE_CONTROLLER --start";;
195         "stop_osrf") local_first "$SERVICE_CONTROLLER --stop" \
196             "$SERVICE_CONTROLLER --stop";;
197         "restart_osrf") local_first "$SERVICE_CONTROLLER --restart" \
198             "$SERVICE_CONTROLLER --restart";;
199         *) echo "Can only do start_osrf, stop_osrf, or restart_osrf for an individual service";;
200     esac;
201     exit;
202 fi;
203
204 case $OPT_ACTION in
205
206     "start_osrf") local_first "$LOCAL_BASE --start-services" \
207         "$DRONE_BASE --start-services";;
208
209     "stop_osrf") drone_first "$LOCAL_BASE --stop-services" \
210         "$DRONE_BASE --stop-services";;
211
212     "restart_osrf") local_first "$LOCAL_BASE --restart-services" \
213         "$DRONE_BASE --restart-services";;
214
215     "start_all") local_first "$LOCAL_BASE --start-all" \
216         "$DRONE_BASE --start-services";;
217
218     "stop_all") drone_first "$LOCAL_BASE --stop-all" \
219         "$DRONE_BASE --stop-services";;
220
221     "restart_all") $0 $OPT_LOCALHOST -a stop_all; $0 $OPT_LOCALHOST -a start_all;;
222     "build") cd ~/ILS/ && make clean default_config all;;
223     "build_xul") make_xul;;
224     "detach_brick") detach_brick;;
225     "attach_brick") mv "$LDIRECTOR_FILE-" "$LDIRECTOR_FILE";;
226     "fetch") fetch_build;;
227 esac;
228
229