~gandelman-a/charms/precise/nova-compute/unused_func

« back to all changes in this revision

Viewing changes to hooks/nova-compute-common

  • Committer: James Page
  • Date: 2013-10-15 12:04:13 UTC
  • mfrom: (46.1.83 nova-compute)
  • Revision ID: james.page@canonical.com-20131015120413-grclbw2ot5gbgp5r
Update of all Havana / Saucy / python-redux work:

* Full python rewrite using new OpenStack charm-helpers.

* Test coverage

* Havana support

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/bash -e
2
 
 
3
 
CHARM="nova-compute"
4
 
PACKAGES="nova-compute python-keystone genisoimage"
5
 
SERVICES="nova-compute"
6
 
CONF_DIR="/etc/nova"
7
 
NOVA_CONF=$(config-get nova-config)
8
 
API_CONF="/etc/nova/api-paste.ini"
9
 
QUANTUM_CONF="/etc/quantum/quantum.conf"
10
 
LIBVIRTD_CONF="/etc/libvirt/libvirtd.conf"
11
 
HOOKS_DIR="$CHARM_DIR/hooks"
12
 
MULTI_HOST=$(config-get multi-host)
13
 
 
14
 
if [ -f /etc/nova/nm.conf ]; then
15
 
    NET_MANAGER=$(cat /etc/nova/nm.conf)
16
 
fi
17
 
case $NET_MANAGER in
18
 
  "Quantum")
19
 
    QUANTUM_PLUGIN=$(cat /etc/nova/quantum_plugin.conf)
20
 
    case $QUANTUM_PLUGIN in
21
 
      "ovs")
22
 
        SERVICES="$SERVICES quantum-plugin-openvswitch-agent"
23
 
        QUANTUM_PLUGIN_CONF="/etc/quantum/plugins/openvswitch/ovs_quantum_plugin.ini"
24
 
        ;;
25
 
      "nvp")
26
 
        QUANTUM_PLUGIN_CONF="/etc/quantum/plugins/nicira/nvp.ini"
27
 
        ;;
28
 
       *)
29
 
        juju-log "Unrecognised plugin for quantum: $QUANTUM_PLUGIN" && exit 1
30
 
        ;;
31
 
    esac
32
 
    ;;
33
 
  "FlatManager"|"FlatDHCPManager")
34
 
    if [[ "$MULTI_HOST" == "yes" ]] ; then
35
 
      SERVICES="$SERVICES nova-api nova-network"
36
 
    fi
37
 
    ;;
38
 
esac
39
 
 
40
 
if [[ -e $HOOKS_DIR/lib/nova/nova-common ]] ; then
41
 
  . $HOOKS_DIR/lib/nova/nova-common
42
 
else
43
 
  juju-log "$CHARM: Couldn't load $HOOKS_DIR/lib/nova-common" && exit 1
44
 
fi
45
 
 
46
 
determine_compute_package() {
47
 
  # determines the appropriate nova-compute package to install
48
 
  # for the configured virt-type.
49
 
  local virt_type="$1"
50
 
  local compute_pkg=""
51
 
  case $virt_type in
52
 
    "kvm") compute_pkg="nova-compute-kvm";;
53
 
    "qemu") compute_pkg="nova-compute-qemu";;
54
 
    "xen") compute_pkg="nova-compute-xen";;
55
 
    "uml") compute_pkg="nova-compute-uml";;
56
 
    "lxc") compute_pkg="nova-compute-lxc";;
57
 
    *) error_out "ERROR: Unsupported virt_type=$virt_type";;
58
 
  esac
59
 
  echo "$compute_pkg"
60
 
}
61
 
 
62
 
function setup_bridge {
63
 
  # XXX This is required by nova-network and will likely move somewhere else
64
 
  # once we can split these services up into seperate formulas.
65
 
  br=$1
66
 
  ip=$2
67
 
  netmask=$3
68
 
  [[ -z $br ]] && br="br100"
69
 
  [[ -z $ip ]] && ip="11.0.0.1"
70
 
  [[ -z $netmask ]] && netmask="255.255.255.0"
71
 
 
72
 
  apt-get -y install bridge-utils augeas-lenses augeas-tools
73
 
  echo "Configuring bridge $br ($ip $netmask)"
74
 
  context="/files/etc/network/interfaces"
75
 
  augtool <<EOF
76
 
  set $context/auto[child::1 = "$br"]/1 $br
77
 
  set $context/iface[. = '$br'] $br
78
 
  set $context/iface[. = '$br']/family inet
79
 
  set $context/iface[. = '$br']/method static
80
 
  set $context/iface[. = '$br']/address $ip
81
 
  set $context/iface[. = '$br']/netmask $netmask
82
 
  set $context/iface[. = '$br']/bridge_ports none 
83
 
  save
84
 
EOF
85
 
  ifdown -a ; ifup -a
86
 
}
87
 
 
88
 
function configure_network_manager {
89
 
  # needed by the nova-network bits
90
 
  # to be expanded later to cover flatDhcp and VLAN
91
 
  echo "$0: configuring $1 network manager"
92
 
  local net_manager=$1
93
 
  local quantum_plugin=$2
94
 
  local network_bridge=$(config-get bridge-interface)
95
 
  local private_address=$(get_ip `unit-get private-address`)
96
 
  # Check to ensure we can actually resolve
97
 
  # the local unit IP address
98
 
  [[ -n $private_address ]] || {
99
 
    juju-log "Unable to resolve local IP address"
100
 
    exit 1
101
 
  }
102
 
 
103
 
  case $net_manager in
104
 
    "FlatManager"|"FlatDHCPManager")
105
 
      if [[ "$MULTI_HOST" == "yes" ]] ; then
106
 
        apt-get -y install nova-api nova-network
107
 
        SERVICES="$SERVICES nova-api nova-network"
108
 
      fi
109
 
      [[ -n $net_manager ]] && echo $net_manager > /etc/nova/nm.conf
110
 
      ;;&
111
 
    "FlatManager")
112
 
      local bridge_ip=$(config-get bridge-ip)
113
 
      local bridge_netmask=$(config-get bridge-netmask)
114
 
      setup_bridge $network_bridge $bridge_ip $bridge_netmask
115
 
      set_or_update network_manager nova.network.manager.FlatManager
116
 
      set_or_update flat_network_bridge $network_bridge
117
 
      ;;
118
 
    "FlatDHCPManager")
119
 
      local flat_interface=$(config-get flat-interface)
120
 
      local ec2_host=$(relation-get ec2_host)
121
 
      [[ -z $ec2_host ]] && juju-log "nova-compute: Missing ec2_host" \
122
 
        && exit 0
123
 
      set_or_update network_manager nova.network.manager.FlatDHCPManager
124
 
      # the interface on which bridge is built
125
 
      set_or_update flat_interface $flat_interface
126
 
      # address of API server to forward requests
127
 
      set_or_update ec2_dmz_host $ec2_host
128
 
      ;;
129
 
    "Quantum")
130
 
      local keystone_host="$(relation-get keystone_host)"
131
 
      local auth_port="$(relation-get auth_port)"
132
 
      local quantum_url="$(relation-get quantum_url)"
133
 
      local quantum_admin_tenant_name="$(relation-get service_tenant)"
134
 
      local quantum_admin_username="$(relation-get service_username)"
135
 
      local quantum_admin_password="$(relation-get service_password)"
136
 
      local quantum_security_groups="$(relation-get quantum_security_groups)"
137
 
 
138
 
      # might end up here before nova-c-c has processed keystone hooks
139
 
      [[ -z "$keystone_host" ]] ||
140
 
      [[ -z "$auth_port" ]] ||
141
 
      [[ -z "$quantum_url" ]] ||
142
 
      [[ -z "$quantum_admin_tenant_name" ]] ||
143
 
      [[ -z "$quantum_admin_username" ]] ||
144
 
      [[ -z "$quantum_admin_password" ]] &&
145
 
        juju-log "nova-compute: Missing required data for Quantum config." &&
146
 
          exit 0
147
 
 
148
 
      local cur=$(get_os_codename_package "nova-common")
149
 
      local vers=$(get_os_version_codename $cur)
150
 
 
151
 
      [[ "$quantum_security_groups" == "yes" ]] &&
152
 
      dpkg --compare-versions $vers lt '2013.1' &&
153
 
        juju-log "Unable to use quantum security groups with < grizzly" &&
154
 
          exit 1
155
 
 
156
 
      set_or_update "network_api_class" "nova.network.quantumv2.api.API"
157
 
      set_or_update "quantum_auth_strategy" "keystone"
158
 
      set_or_update "quantum_url" "$quantum_url"
159
 
      set_or_update "quantum_admin_tenant_name" "$quantum_admin_tenant_name"
160
 
      set_or_update "quantum_admin_username" "$quantum_admin_username"
161
 
      set_or_update "quantum_admin_password" "$quantum_admin_password"
162
 
      set_or_update "quantum_admin_auth_url" \
163
 
                    "http://$keystone_host:$auth_port/v2.0"
164
 
 
165
 
      if dpkg --compare-versions $vers gt '2012.2'; then
166
 
        # Grizzly onwards supports metadata proxy so forcing use of config
167
 
        # drive is not required.
168
 
        set_or_update "force_config_drive" "False"
169
 
      else
170
 
        set_or_update "force_config_drive" "True"
171
 
      fi
172
 
      case $quantum_plugin in
173
 
        "ovs")
174
 
          apt-get -y install openvswitch-datapath-dkms
175
 
          apt-get -y install quantum-plugin-openvswitch-agent
176
 
          local quantum_plugin_conf="/etc/quantum/plugins/openvswitch/ovs_quantum_plugin.ini"
177
 
          set_or_update "core_plugin" "quantum.plugins.openvswitch.ovs_quantum_plugin.OVSQuantumPluginV2" "$QUANTUM_CONF"
178
 
          if dpkg --compare-versions $vers gt '2012.2'; then
179
 
            set_or_update "libvirt_vif_driver" "nova.virt.libvirt.vif.LibvirtGenericVIFDriver"
180
 
          else
181
 
            set_or_update "libvirt_vif_driver" "nova.virt.libvirt.vif.LibvirtHybridOVSBridgeDriver"
182
 
          fi
183
 
          set_or_update "libvirt_use_virtio_for_bridges" "True"
184
 
          set_or_update "tenant_network_type" "gre" $quantum_plugin_conf "OVS"
185
 
          set_or_update "enable_tunneling" "True" $quantum_plugin_conf "OVS"
186
 
          set_or_update "tunnel_id_ranges" "1:1000" $quantum_plugin_conf "OVS"
187
 
          set_or_update "local_ip" "$private_address" $quantum_plugin_conf "OVS"
188
 
          if [ "$quantum_security_groups" == "yes" ]; then
189
 
            set_or_update "security_group_api" "quantum"
190
 
            set_or_update "firewall_driver" "nova.virt.firewall.NoopFirewallDriver"
191
 
            set_or_update "firewall_driver" \
192
 
              "quantum.agent.linux.iptables_firewall.OVSHybridIptablesFirewallDriver" \
193
 
              $quantum_plugin_conf "SECURITYGROUP"
194
 
          fi
195
 
          SERVICES="$SERVICES quantum-plugin-openvswitch-agent"
196
 
          ;;
197
 
      esac
198
 
      set_or_update "bind_host" "0.0.0.0" "$QUANTUM_CONF"
199
 
      [[ -n $net_manager ]] && echo $net_manager > /etc/nova/nm.conf
200
 
      [[ -n $quantum_plugin ]] && echo $quantum_plugin > /etc/nova/quantum_plugin.conf
201
 
      ;;
202
 
    *) echo "ERROR: Invalid network manager $1" && exit 1 ;;
203
 
  esac
204
 
}
205
 
 
206
 
BR_INT="br-int"
207
 
 
208
 
function configure_quantum_bridge {
209
 
  if ! ovs-vsctl show | grep -q "Bridge $BR_INT"; then
210
 
    ovs-vsctl add-br $BR_INT
211
 
  fi
212
 
}
213
 
 
214
 
function initialize_ssh_keys {
215
 
  # generate ssh keypair for root if one does not exist or
216
 
  # the pari is not complete.
217
 
  local pub="/root/.ssh/id_rsa"
218
 
  local priv="/root/.ssh/id_rsa.pub"
219
 
  if [[ -e $pub ]] &&
220
 
     [[ -e $priv ]] ; then
221
 
    juju-log "$CHARM: SSH credentials already exist for root."
222
 
    return 0
223
 
  fi
224
 
  juju-log "$CHARM: Initializing new SSH key pair for live migration."
225
 
  [[ -e $pub ]] && mv $pub $pub.$(date +"%s")
226
 
  [[ -e $priv ]] && mv $priv $priv.$(date +"%s")
227
 
  local keyname=$(echo $JUJU_UNIT_NAME | sed -e 's,/,-,g')
228
 
  echo -e "\n" | ssh-keygen -C "$keyname" -N ""
229
 
}
230
 
 
231
 
function libvirt_tcp_listening {
232
 
  # toggle libvirtd's tcp listening in both /etc/default/libvirt-bin
233
 
  # and /etc/libvirt/libvirtd.conf.
234
 
  local toggle="$1"
235
 
  juju-log "$CHARM: Configuring libvirt tcp listening: $toggle."
236
 
  local cur_opts=$(grep "^libvirtd_opts" /etc/default/libvirt-bin |
237
 
                   cut -d= -f2 |  sed -e 's/\"//g')
238
 
  local new_opts=""
239
 
 
240
 
  if [[ "$toggle" == "on" ]] ; then
241
 
    if [[ -z "$cur_opts" ]] ; then
242
 
      echo "libvirtd_opts=\"-d -l\"" >>/etc/default/libvirt-bin
243
 
    elif ! echo "$cur_opts" | grep -q "\-l" ; then
244
 
      new_opts="$cur_opts -l"
245
 
      sed -i "s|\(libvirtd_opts=\).*|\1\"$new_opts\"|" /etc/default/libvirt-bin
246
 
    fi
247
 
    set_or_update "listen_tcp" 1 $LIBVIRTD_CONF
248
 
  elif [[ "$toggle" == "off" ]] ; then
249
 
    if echo "$cur_opts" | grep -q "\-l" ; then
250
 
      new_opts=$(echo $cur_opts | sed -e 's/\-l//g')
251
 
    fi
252
 
    set_or_update "listen_tcp" 0  $LIBVIRTD_CONF
253
 
  fi
254
 
 
255
 
  [[ -n "$new_opts" ]] &&
256
 
    sed -i "s|\(libvirtd_opts=\).*|\1\"$new_opts\"|" /etc/default/libvirt-bin
257
 
 
258
 
  return 0
259
 
}
260
 
 
261
 
 
262
 
function configure_migration {
263
 
  local enable_migration=$(config-get enable-live-migration)
264
 
 
265
 
  if [[ "$enable_migration" != "True" ]] &&
266
 
     [[ "$enable_migraiton" != "true" ]] ; then
267
 
     libvirt_tcp_listening "off"
268
 
     return $?
269
 
  fi
270
 
 
271
 
  libvirt_tcp_listening "on"
272
 
 
273
 
  case "$(config-get migration-auth-type)" in
274
 
    "none"|"None")
275
 
      set_or_update "listen_tls" 0 $LIBVIRTD_CONF
276
 
      set_or_update "auth_tcp" "\"none\"" $LIBVIRTD_CONF
277
 
      ;;
278
 
    "ssh")
279
 
      set_or_update "listen_tls" 0 $LIBVIRTD_CONF
280
 
      set_or_update "live_migration_uri" "qemu+ssh://%s/system" $NOVA_CONF
281
 
      initialize_ssh_keys
282
 
      # check in with nova-c-c and register our new key.
283
 
      for id in $(relation-ids cloud-compute) ; do
284
 
        compute_joined $id
285
 
      done
286
 
      service_ctl nova-compute restart ;;
287
 
    "sasl") return 0 ;;
288
 
  esac
289
 
}
290
 
 
291
 
function configure_libvirt {
292
 
  cat > /etc/libvirt/qemu.conf << EOF
293
 
# File installed by Juju nova-compute charm
294
 
cgroup_device_acl = [
295
 
   "/dev/null", "/dev/full", "/dev/zero",
296
 
   "/dev/random", "/dev/urandom",
297
 
   "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
298
 
   "/dev/rtc", "/dev/hpet", "/dev/net/tun",
299
 
]
300
 
EOF
301
 
  configure_migration
302
 
  service libvirt-bin restart
303
 
}
304
 
 
305
 
function migration_enabled {
306
 
  local migration="$(config-get enable-live-migration)"
307
 
  [[ "$migration" == "true" ]] || [[ "$migration" == "True" ]] && return 0
308
 
  return 1
309
 
}