~johnsca/charms/trusty/tomcat/explicit

« back to all changes in this revision

Viewing changes to hooks/common.sh

  • Committer: matthew.bruzek at canonical
  • Date: 2014-03-17 23:06:03 UTC
  • Revision ID: matthew.bruzek@canonical.com-20140317230603-9a3jwpe5oqy0h9lb
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# This file contains common functions for the tomcat hooks.ec
 
2
 
 
3
REMOTE_SERVICE=${JUJU_REMOTE_UNIT%/*}
 
4
# $JUJU_REMOTE_UNIT variable holds the name of the unit which reported to have
 
5
# -joined, -changed, or -departed
 
6
SERVICE=${JUJU_UNIT_NAME%/*}
 
7
# $JUJU_UNIT_NAME variable holds the name of the unit.
 
8
 
 
9
add_file_row () {
 
10
  local file=$1
 
11
  shift
 
12
  if [ -e "${file}" ]; then
 
13
    egrep -v "^$1"$'(\t|$)' "${file}" > "${file}.new" || true
 
14
    mv "${file}.new" "${file}"
 
15
  fi
 
16
  shift
 
17
  echo_delimited $'\t' "$@" >> "${file}"
 
18
}
 
19
 
 
20
check_checksum () {
 
21
  # Check the md5sum of file $1 and compare it against $2 exit on mismatch.
 
22
  if [ `md5sum "$1" | cut -d " " -f 1` != $2 ]; then
 
23
    juju-log --log-level CRITICAL "Checksum of $1 failed"
 
24
    exit 1
 
25
  fi
 
26
}
 
27
 
 
28
close_port () {
 
29
  juju-log "Closing port $1 for ${VERSION}"
 
30
  # Ask juju to close the port number.
 
31
  close-port $1
 
32
}
 
33
 
 
34
create_relation_id () {
 
35
  # Create a file for each relation in /etc/tomcatX/<relation_name>.relation
 
36
  echo "${REMOTE_SERVICE}" > /etc/${VERSION}/${JUJU_RELATION_ID}.relation
 
37
  # $JUJU_RELATION_ID variable holds the opaque relation identifier used to
 
38
  # distingush between multiple relations with the same name.
 
39
}
 
40
 
 
41
echo_delimited () {
 
42
  local d=$1
 
43
  printf %s "$2"
 
44
  shift 2
 
45
  local arg
 
46
  for arg; do
 
47
    printf %s%s "${d}" "${arg}"
 
48
  done
 
49
  echo
 
50
}
 
51
 
 
52
escape_name () {
 
53
  # Replace % $ and / characters in the name and print the new name.
 
54
  local name=${1//%/%25}
 
55
  name=${name//\$/%24}
 
56
  name=${name//\//%2F}
 
57
  printf %s "${name}"
 
58
}
 
59
 
 
60
escape_unit_name () {
 
61
  echo ${1//\//-}
 
62
}
 
63
 
 
64
open_port () {
 
65
  juju-log "Opening port $1 for ${VERSION}"
 
66
  # Ask Juju to open the port for this unit.
 
67
  open-port $1
 
68
}
 
69
 
 
70
remove_config () {
 
71
  # Get the path of the .d directory where the $1 file is built.
 
72
  if dotdee -d "$1" 2> /dev/null; then
 
73
    dotdee --undo "$1"
 
74
    rm -rf "/etc/dotdee$1" "/etc/dotdee$1.d"
 
75
  fi
 
76
}
 
77
 
 
78
remove_file_row () {
 
79
  if [ -e "$1" ]; then
 
80
    if egrep -v "^$2"$'(\t|$)' "$1" > "$1.new"; then
 
81
      mv "$1.new" "$1"
 
82
    else
 
83
      rm -f "$1" "$1.new"
 
84
    fi
 
85
  fi
 
86
}
 
87
 
 
88
set_relation_id () {
 
89
  # Set the REMOTE_SERVICE variable to the name of the relation in the file.
 
90
  REMOTE_SERVICE=`< /etc/${VERSION}/${JUJU_RELATION_ID}.relation`
 
91
}
 
 
b'\\ No newline at end of file'