|
| 1 | +#!/bin/bash |
| 2 | +# Copyright 2012-2014 MongoDB, Inc. |
| 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 | +function eval_params { |
| 17 | + local params=$(sed -e 's|["]|\\\"|g' $1) |
| 18 | + echo $(eval echo \"$params\") |
| 19 | +} |
| 20 | + |
| 21 | +function r { |
| 22 | + echo $1| cut -d'/' -f 2 |
| 23 | +} |
| 24 | + |
| 25 | +function a { |
| 26 | + echo $(cd $(dirname $1); pwd)/$(basename $1) |
| 27 | +} |
| 28 | + |
| 29 | +function id { |
| 30 | + local id_line=$(grep id $1 | head -n 1) |
| 31 | + echo $(expr "$id_line" : '.*: *"\(.*\)" *,*') |
| 32 | +} |
| 33 | + |
| 34 | +function get { |
| 35 | + echo "GET $1 $(curl --header 'Accept: application/json' --include --silent --request GET $1)" |
| 36 | +} |
| 37 | + |
| 38 | +function post { |
| 39 | + echo "POST $1 $(curl --header 'Accept: application/json' --include --silent --request POST --data "$2" $1)" |
| 40 | +} |
| 41 | + |
| 42 | +function delete { |
| 43 | + echo "DELETE $1 $(curl --header 'Accept: application/json' --include --silent --request DELETE $1)" |
| 44 | +} |
| 45 | + |
| 46 | +function code { |
| 47 | + expr "$1" : '.*HTTP/1.[01] \([0-9]*\)' |
| 48 | +} |
| 49 | + |
| 50 | +function usage { |
| 51 | + echo "usage: $0 configurations/cluster/file.json action" |
| 52 | + echo "cluster: servers|replica_sets|sharded_clusters" |
| 53 | + echo "action: start|status|stop" |
| 54 | + exit 1 |
| 55 | +} |
| 56 | + |
| 57 | +SSL_FILES=$(a ./ssl-files) |
| 58 | +BASE_URL=${MONGO_ORCHESTRATION:-'http://localhost:8889'} |
| 59 | + |
| 60 | +if [ $# -ne 2 ]; then usage; fi |
| 61 | +if [ ! -f "$1" ]; then echo "configuration file '$1' not found"; exit 1; fi |
| 62 | + |
| 63 | +ID=$(id $1) |
| 64 | +if [ ! "$ID" ]; then echo "id field not found in configuration file '$1'"; exit 1; fi |
| 65 | +R=$(r $1) |
| 66 | + |
| 67 | +GET=$(get $BASE_URL/$R/$ID) |
| 68 | +HTTP_CODE=$(code "$GET") |
| 69 | +EXIT_CODE=0 |
| 70 | + |
| 71 | +case $2 in |
| 72 | +start) |
| 73 | + if [ "$HTTP_CODE" != "200" ] |
| 74 | + then |
| 75 | + WORKSPACE=${TRAVIS_BUILD_DIR}/orchestrations |
| 76 | + rm -fr $WORKSPACE |
| 77 | + mkdir $WORKSPACE |
| 78 | + LOGPATH=$WORKSPACE |
| 79 | + DBPATH=$WORKSPACE |
| 80 | + POST_DATA=$(eval_params $1) |
| 81 | + echo "DBPATH=$DBPATH" |
| 82 | + echo "LOGPATH=$LOGPATH" |
| 83 | + echo "POST_DATA='$POST_DATA'" |
| 84 | + echo |
| 85 | + POST=$(post $BASE_URL/$R "$POST_DATA") |
| 86 | + echo "$POST" |
| 87 | + HTTP_CODE=$(code "$POST") |
| 88 | + if [ "$HTTP_CODE" != 200 ]; then EXIT_CODE=1; fi |
| 89 | + else |
| 90 | + echo "$GET" |
| 91 | + fi |
| 92 | + ;; |
| 93 | +stop) |
| 94 | + if [ "$HTTP_CODE" == "200" ] |
| 95 | + then |
| 96 | + DELETE=$(delete $BASE_URL/$R/$ID) |
| 97 | + echo "$DELETE" |
| 98 | + HTTP_CODE=$(code "$DELETE") |
| 99 | + if [ "$HTTP_CODE" != 204 ]; then EXIT_CODE=1; fi |
| 100 | + else |
| 101 | + echo "$GET" |
| 102 | + fi |
| 103 | + ;; |
| 104 | +status) |
| 105 | + if [ "$HTTP_CODE" == "200" ] |
| 106 | + then |
| 107 | + echo "$GET" |
| 108 | + else |
| 109 | + echo "$GET" |
| 110 | + EXIT_CODE=1 |
| 111 | + fi |
| 112 | + ;; |
| 113 | +*) |
| 114 | + usage |
| 115 | + ;; |
| 116 | +esac |
| 117 | +exit $EXIT_CODE |
0 commit comments