~charmers/charms/precise/glance/trunk

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
#!/bin/bash

CHARM="glance"

SERVICES="glance-api glance-registry"
PACKAGES="glance python-mysqldb python-swift python-keystone uuid"

GLANCE_REGISTRY_CONF="/etc/glance/glance-registry.conf"
GLANCE_REGISTRY_PASTE_INI="/etc/glance/glance-registry-paste.ini"
GLANCE_API_CONF="/etc/glance/glance-api.conf"
GLANCE_API_PASTE_INI="/etc/glance/glance-api-paste.ini"
CONF_DIR="/etc/glance"

if [[ -e "$CHARM_DIR/lib/openstack-common" ]] ; then
  . $CHARM_DIR/lib/openstack-common
else
  juju-log "ERROR: Couldn't load $CHARM_DIR/lib/openstack-common." && exit 1
fi

function set_paste_deploy_flavor {
  local flavor="$1"
  local config="$2"
  case $config in
    "api") local conf=$GLANCE_API_CONF ;;
    "registry") local conf=$GLANCE_REGISTRY_CONF ;;
    *) juju-log "ERROR: set_paste_deploy: invalid config=$config" && exit 1 ;;
  esac
  if ! grep -q "\[paste_deploy\]" "$conf" ; then
    juju-log "Updating $conf: Setting new paste_deploy flavor = $flavor"
    echo -e "\n[paste_deploy]\nflavor = keystone\n" >>$conf && return 0
    juju-log "ERROR: Could not update paste_deploy flavor in $conf" && return 1
  fi
  juju-log "Updating $conf: Setting paste_deploy flavor = $flavor"
  local tag="[paste_deploy]"
  sed -i "/$tag/, +1 s/\(flavor = \).*/\1$flavor/g" $conf && return 0
  juju-log "ERROR: Could not update paste_deploy flavor in $conf" && return 1
}

function update_pipeline {
  # updates pipeline middleware definitions in api-paste.ini
  local pipeline="$1"
  local new="$2"
  local config="$3"

  case $config in
    "api") local api_conf=$GLANCE_API_CONF ;;
    "registry") local api_conf=$GLANCE_REGISTRY_CONF ;;
    *) juju-log "ERROR: update_pipeline: invalid config=$config" && exit 1 ;;
  esac

  local tag="\[pipeline:$pipeline\]"
  if ! grep -q "$tag" $api_conf ; then
      juju-log "ERROR: update_pipeline: pipeline not found: $pipeline"
      return 1
  fi
  juju-log "Updating pipeline:$pipeline in $api_conf"
  sed -i "/$tag/, +1 s/\(pipeline = \).*/\1$new/g" $api_conf
}

function set_or_update {
  # This handles configuration of both api and registry server
  # until LP #806241 is resolved.  Until then, $3 is either
  # 'api' or 'registry' to specify which
  # set or update a key=value config option in glance.conf
  KEY=$1
  VALUE=$2
  case "$3" in
    "api") CONF=$GLANCE_API_CONF ;;
    "api-paste") CONF=$GLANCE_API_PASTE_INI ;;
    "registry") CONF=$GLANCE_REGISTRY_CONF ;;
    "registry-paste") CONF=$GLANCE_REGISTRY_PASTE_INI ;;
    *) juju-log "ERROR: set_or_update(): Invalid or no config file specified." \
        && exit 1 ;;
  esac
  [[ -z $KEY ]] && juju-log "ERROR: set_or_update(): value $VALUE missing key" \
        && exit 1
  [[ -z $VALUE ]] && juju-log "ERROR: set_or_update(): key $KEY missing value" \
        && exit 1
  cat $CONF | grep "$KEY = $VALUE" >/dev/null \
   && juju-log "glance: $KEY = $VALUE already set" exit 0
  if cat $CONF | grep "$KEY =" >/dev/null ; then
    sed -i "s|\($KEY = \).*|\1$VALUE|" $CONF
  else
    echo "$KEY = $VALUE" >>$CONF
  fi
}

do_openstack_upgrade() {
  # update openstack components to those provided by a new installation source
  # it is assumed the calling hook has confirmed that the upgrade is sane.
  local rel="$1"
  shift
  local packages=$@
  orig_os_rel=$(get_os_codename_package "glance-common")
  new_rel=$(get_os_codename_install_source "$rel")

  # Backup the config directory.
  local stamp=$(date +"%Y%m%d%M%S")
  tar -pcf /var/lib/juju/$CHARM-backup-$stamp.tar $CONF_DIR

  # Setup apt repository access and kick off the actual package upgrade.
  configure_install_source "$rel"
  apt-get update
  DEBIAN_FRONTEND=noninteractive apt-get --option Dpkg::Options::=--force-confnew -y \
     install --no-install-recommends $packages

  # Update the new config files for existing relations.
  local r_id=""

  r_id=$(relation-ids shared-db)
  if [[ -n "$r_id" ]] ; then
    juju-log "$CHARM: Configuring database after upgrade to $rel."
    db_changed $r_id
  fi

  r_id=$(relation-ids identity-service)
  if [[ -n "$r_id" ]] ; then
    juju-log "$CHARM: Configuring identity service after upgrade to $rel."
    keystone_changed $r_id
  fi
}