summaryrefslogtreecommitdiff
path: root/src/manage.sh
blob: 1cd3a036ffd70460254af95232f947b39336fef4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/sh
#
# flufigut
#
# flufigut, the flumotion configuration utility, is a simple tool
# that generates flumotion configuration files using pyhton jinja2
# template engine and simplejson. flufigut generates planet.xml
# and worker.xml files from configuration templates and an easy to
# understand representation of the flow structure written in json.
#
#
# Copyright (C) 2012 Christian Pointner <equinox@spreadspace.org>
#                    Michael Gebetsroither <michael@mgeb.org>
#
# This file is part of flufigut.
#
# flufigut is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# any later version.
#
# flufigut is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with flufigut. If not, see <http://www.gnu.org/licenses/>.
#

#
# Initialization:
# - generate ssh key:
#   $ ssh-keygen -f id_rsa
# - configure ssh.config
# - copy flufigut-client.sh to every machine and install it
#   $ sudo ./flufigut-client.sh install
#

OUTPUT_DIR="output/flumotion"
SSH_KEY="id_rsa"
SSH_CONFIG="ssh.config"
SSH_USER="flufigut"

deploy_all() {
  for machine in "$OUTPUT_DIR"/*; do
    if [ -d "$machine" ]; then
      machine=`basename $machine`
      echo "copying files to $machine ... "
      tar -C "$OUTPUT_DIR" -czf "$OUTPUT_DIR/$machine.tar.gz" "$machine"
      scp -i "$SSH_KEY" -F "$SSH_CONFIG" "$OUTPUT_DIR/$machine.tar.gz" $SSH_USER@"$machine":/tmp
      ssh -i "$SSH_KEY" -F "$SSH_CONFIG" $SSH_USER@"$machine" install "/tmp/$machine.tar.gz"
    fi
  done
}

clean_all() {
  for machine in "$OUTPUT_DIR"/*; do
    if [ -d "$machine" ]; then
      machine=`basename $machine`
      echo "removing files from $machine ... "
      generate_instance_list $machine
      ssh -i "$SSH_KEY" -F "$SSH_CONFIG" $SSH_USER@"$machine" remove <  "$OUTPUT_DIR/$machine.list"
    fi
  done
}

wipe_all() {
  for machine in `grep "^Host" ssh.config | awk '{ print($2) }'`; do
    echo "wipeing files belonging to planet '$1' from $machine ... "
    ssh -i "$SSH_KEY" -F "$SSH_CONFIG" $SSH_USER@"$machine" wipe "$1"
  done
}

generate_instance_list() {
  machine=$1

  rm -f "$OUTPUT_DIR/$machine.list"
  touch "$OUTPUT_DIR/$machine.list"
  if [ -d "$OUTPUT_DIR/$machine/managers" ]; then
    for name in "$OUTPUT_DIR/$machine/managers/"*; do
      name=`basename "$name"`
      echo "manager $name" >> "$OUTPUT_DIR/$machine.list"
    done
  fi
  for name in "$OUTPUT_DIR/$machine/workers/"*.xml; do
    name=`basename "$name"`
    name=${name%%.xml}
    echo "worker $name" >> "$OUTPUT_DIR/$machine.list"
  done
}

start_all() {
  for machine in "$OUTPUT_DIR"/*; do
    if [ -d "$machine" ] && [ -d "$machine/managers" ]; then
      machine=`basename $machine`
      echo " $machine (manager, worker)"
      generate_instance_list $machine
      ssh -i "$SSH_KEY" -F "$SSH_CONFIG" $SSH_USER@"$machine" start < "$OUTPUT_DIR/$machine.list"
      break
    fi
  done
  for machine in "$OUTPUT_DIR"/*; do
    if [ -d "$machine" ] && [ ! -d "$machine/managers" ]; then
      machine=`basename $machine`
      echo " $machine (worker)"
      generate_instance_list $machine
      ssh -i "$SSH_KEY" -F "$SSH_CONFIG" $SSH_USER@"$machine" start < "$OUTPUT_DIR/$machine.list"
    fi
  done
}

stop_all() {
  for machine in "$OUTPUT_DIR"/*; do
    if [ -d "$machine" ] && [ ! -d "$machine/managers" ]; then
      machine=`basename $machine`
      echo " $machine (worker)"
      generate_instance_list $machine
      ssh -i "$SSH_KEY" -F "$SSH_CONFIG" $SSH_USER@"$machine" stop < "$OUTPUT_DIR/$machine.list"
    fi
  done
  for machine in "$OUTPUT_DIR"/*; do
    if [ -d "$machine" ] && [ -d "$machine/managers" ]; then
      machine=`basename $machine`
      echo " $machine (manager, worker)"
      generate_instance_list $machine
      ssh -i "$SSH_KEY" -F "$SSH_CONFIG" $SSH_USER@"$machine" stop < "$OUTPUT_DIR/$machine.list"
      break
    fi
  done
}

case "$1" in
  deploy)
    echo "Deploying configuration to all machines ..."
    deploy_all
  ;;
  clean)
    echo "cleaning/removing configuration from all machines ..."
    clean_all
  ;;
  wipe)
    if [ -z "$2" ]; then
      echo "Usage: $0 wipe <planet name>"
      exit 1
    fi
    echo "wipeing configuration for from all machines inside ssh-config ..."
    wipe_all "$2"
  ;;
  start)
    echo "Start manager and worker on all machines ..."
    start_all
  ;;
  stop)
    echo "Stop manager and worker on all machines ..."
    stop_all
  ;;
  restart)
    echo "Restart manager and worker on all machines ..."
    stop_all
    sleep 1
    start_all
  ;;
  help)
    echo "flufigut managemnt script by Christian Pointner <equinox@spreadspace.org>"
    echo ""
    echo "Usage $0 <command> [param] "
    echo ""
    echo "Commands:"
    echo "  deploy         copies all configuration file to all servers and installs them"
    echo "  clean          remove all configuration file from all servers"
    echo "  wipe <planet>  purges all configuration files belonging to a given planet"
    echo "  start          starts all instances on all servers (manager starts first)"
    echo "  stop           stop all instances on all servers (manager stops last)"
    echo "  restart        same as stop, start"
    echo "  help           prints this"
  ;;
  *)
    echo "Usage $0 (deploy|clean|wipe|start|stop|restart|help)"
    exit 1
  ;;
esac

exit 0