~dspiteri/charms/precise/cassandra/prodchanges

« back to all changes in this revision

Viewing changes to hooks/cassandra-common

  • Committer: James Page
  • Date: 2011-10-11 09:41:40 UTC
  • Revision ID: james.page@canonical.com-20111011094140-n882bb8x3lr1k7lo
Refactoring and updates to use private-address

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/bin/bash
2
2
# vim: set tabstop=2 expandtab:
3
3
 
4
 
set -ex
 
4
set -e
5
5
 
6
6
export LANG=en_US.UTF-8
7
7
 
8
 
 
9
 
IP=`unit-get private-address`
10
 
# Pull configuration for charm
11
 
JMX_PORT=`config-get jmx-port`
12
 
CLUSTER_PORT=`config-get cluster-port`
13
 
CLIENT_PORT=`config-get client-port`
14
 
CLUSTER_NAME=`config-get cluster-name`
15
 
 
16
8
# Install cassandra source and install packages
17
9
install_cassandra () {
18
10
  juju-log "Installing Cassandra"
23
15
  # Update the repositories
24
16
  apt-get update
25
17
  # Install the package
26
 
  apt-get -qq install -y cassandra 
 
18
  apt-get -qq install -y cassandra python-cheetah
27
19
}
28
20
 
29
21
# Update the cassandra environment with the appropriate JMX port
30
22
configure_jmx_port () {
31
23
  juju-log "Configuring JMX port"
32
 
  sed -i -e "s/^JMX_PORT=.*/JMX_PORT=\"${1}\"/" /etc/cassandra/cassandra-env.sh
 
24
  JMX_PORT=`config-get jmx-port`
 
25
  sed -i -e "s/^JMX_PORT=.*/JMX_PORT=\"${JMX_PORT}\"/" /etc/cassandra/cassandra-env.sh
33
26
  # Open port ready for expose
34
 
  open-port ${1}/TCP
 
27
  open-port ${JMX_PORT}/TCP
 
28
}
 
29
 
 
30
get_private_ip () {
 
31
  # Make sure that we use the resolvable private address
 
32
  # dig deals with both hostnames and ip addresses in a nice
 
33
  # way
 
34
  dig +short `unit-get private-address` 
35
35
}
36
36
 
37
37
# Construct the cassandra.yaml file from the appropriate information above
38
38
configure_cassandra () {
39
39
  juju-log "Configuring Cassandra"
 
40
  IP=`get_private_ip`
 
41
  CLUSTER_PORT=`config-get cluster-port`
 
42
  CLIENT_PORT=`config-get client-port`
 
43
  CLUSTER_NAME=`config-get cluster-name`
 
44
  AUTO_MEMORY=`config-get auto-memory`
 
45
  HEAP_SIZE=`config-get heap-size`
 
46
  NEW_GEN_SIZE=`config-get new-gen-size`
40
47
  # This needs to 'detect' whether its running in ec2
41
48
  # and use the ec2Snitch instead if it is - or we could
42
49
  # specify this as a configuration parameter
43
 
  sed -i -e "s/^cluster_name:.*/cluster_name: \'${1}\'/" \
44
 
         -e "s/\- seeds:.*/\- seeds: \"${2}\"/" \
45
 
         -e "s/^storage_port:.*/storage_port: ${3}/" \
46
 
         -e "s/^listen_address:.*/listen_address: ${2}/" \
47
 
         -e "s/^rpc_address:.*/rpc_address: ${2}/" \
48
 
         -e "s/^rpc_port:.*/rpc_port: ${4}/" \
 
50
  sed -i -e "s/^cluster_name:.*/cluster_name: \'${CLUSTER_NAME}\'/" \
 
51
         -e "s/\- seeds:.*/\- seeds: \"${IP}\"/" \
 
52
         -e "s/^storage_port:.*/storage_port: ${CLUSTER_PORT}/" \
 
53
         -e "s/^listen_address:.*/listen_address: ${IP}/" \
 
54
         -e "s/^rpc_address:.*/rpc_address: ${IP}/" \
 
55
         -e "s/^rpc_port:.*/rpc_port: ${CLIENT_PORT}/" \
49
56
         /etc/cassandra/cassandra.yaml
50
57
  # Open port ready for expose
51
 
  open-port ${4}/TCP
 
58
  open-port ${CLIENT_PORT}/TCP
 
59
 
 
60
  # Configure memory settings as specified in configuration
 
61
  if [ "$AUTO_MEMORY" = "False" ]; then
 
62
    juju-log "Configuring Manual Memory Setttings"
 
63
    sed -i -e "s/^[#]MAX_HEAP_SIZE=.*/MAX_HEAP_SIZE=\"${HEAP_SIZE}\"/" \
 
64
           -e "s/^[#]HEAP_NEWSIZE=.*/HEAP_NEWSIZE=\"${NEW_GEN_SIZE}\"/" /etc/cassandra/cassandra-env.sh
 
65
  else
 
66
    sed -i -e "s/^[#]MAX_HEAP_SIZE=.*/#MAX_HEAP_SIZE=\"${HEAP_SIZE}\"/" \
 
67
           -e "s/^[#]HEAP_NEWSIZE=.*/#HEAP_NEWSIZE=\"${NEW_GEN_SIZE}\"/" /etc/cassandra/cassandra-env.sh
 
68
  fi 
52
69
}
53
70
 
54
71
# Service Control Commands
69
86
 
70
87
setup_database_interface () {
71
88
  juju-log "Setup Cassandra database interface"
72
 
  relation-set port="${1}"
 
89
  relation-set port="`config-get client-port`"
73
90
}
74
91
 
75
92
setup_jmx_interface () {
76
93
  juju-log "Setup Cassandra JMX interface"
77
 
  relation-set port="${1}"
 
94
  relation-set port="`config-get jmx-port`"
78
95
}
79
96
 
80
97
reconfigure_cluster_seeds () {
108
125
  if [ -z "${AUTO}" ]; then
109
126
    restart_cassandra
110
127
  fi
111
 
  juju-log "$JUJU_REMOTE_UNIT modified its settings"
112
 
  juju-log "Relation settings:"
113
 
  relation-get
114
 
  juju-log Relation members:
115
 
  relation-list
 
128
  juju-log "$JUJU_REMOTE_UNIT modified its settings or departed"
 
129
}
 
130
 
 
131
decommission_node () {
 
132
  juju-log "Decommissioning node - this may take some time..."
 
133
  nodetool -h `get_private_ip` decommission
116
134
}
117
135
 
118
136
# Actual processing
123
141
    # Install cassandra first run only
124
142
    [[ -d /usr/share/cassandra ]] || install_cassandra
125
143
    # Update the cassandra environment with the appropriate JMX port
126
 
    configure_jmx_port $JMX_PORT
 
144
    configure_jmx_port 
127
145
    # Construct the cassandra.yaml file from the appropriate information above
128
 
    configure_cassandra "$CLUSTER_NAME" $IP $CLUSTER_PORT $CLIENT_PORT
 
146
    configure_cassandra 
129
147
    # Restart as required
130
148
    restart_cassandra
131
149
    ;;
132
150
  config-changed)
133
151
    # Update the cassandra environment with the appropriate JMX port
134
 
    configure_jmx_port $JMX_PORT
 
152
    configure_jmx_port 
135
153
    # Construct the cassandra.yaml file from the appropriate information above
136
 
    configure_cassandra "$CLUSTER_NAME" $IP $CLUSTER_PORT $CLIENT_PORT
 
154
    configure_cassandra
137
155
    # Restart as required
138
156
    restart_cassandra
139
157
    ;;
147
165
    stop_cassandra
148
166
    ;;
149
167
  database-relation-joined)
150
 
    setup_database_interface $CLIENT_PORT
 
168
    setup_database_interface 
151
169
    ;;
152
170
  cluster-relation-joined)
153
171
    juju-log "Joining cassandra cluster..."
154
172
    ;;
155
 
  cluster-relation-changed)
 
173
  cluster-relation-changed|cluster-relation-departed)
156
174
    reconfigure_cluster_seeds
157
175
    ;;
158
176
  jmx-relation-joined)
159
 
    setup_jmx_interface $JMX_PORT
 
177
    setup_jmx_interface
160
178
    ;;
161
179
  *)
162
180
    juju-log "Command not recognised"