lib/baremetal
6a5aa7c6
 ## vim: tabstop=4 shiftwidth=4 softtabstop=4
 
 ## Copyright (c) 2012 Hewlett-Packard Development Company, L.P.
 ## All Rights Reserved.
 ##
 ##    Licensed under the Apache License, Version 2.0 (the "License"); you may
 ##    not use this file except in compliance with the License. You may obtain
 ##    a copy of the License at
 ##
 ##         http://www.apache.org/licenses/LICENSE-2.0
 ##
 ##    Unless required by applicable law or agreed to in writing, software
 ##    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 ##    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 ##    License for the specific language governing permissions and limitations
 ##    under the License.
f35cf91a
 
 
 # This file provides devstack with the environment and utilities to
 # control nova-compute's baremetal driver.
 # It sets reasonable defaults to run within a single host,
 # using virtual machines in place of physical hardware.
 # However, by changing just a few options, devstack+baremetal can in fact
 # control physical hardware resources on the same network, if you know
 # the MAC address(es) and IPMI credentials.
 #
6a5aa7c6
 # At a minimum, to enable the baremetal driver, you must set these in localrc:
 #
f35cf91a
 #    VIRT_DRIVER=baremetal
 #    ENABLED_SERVICES="$ENABLED_SERVICES,baremetal"
 #
 #
 # We utilize diskimage-builder to create a ramdisk, and then
 # baremetal driver uses that to push a disk image onto the node(s).
 #
 # Below we define various defaults which control the behavior of the
9a3ba4b3
 # baremetal compute service, and inform it of the hardware it will control.
f35cf91a
 #
 # Below that, various functions are defined, which are called by devstack
 # in the following order:
 #
6a5aa7c6
 # before nova-cpu starts:
 #
f35cf91a
 #  - prepare_baremetal_toolchain
 #  - configure_baremetal_nova_dirs
 #
6a5aa7c6
 # after nova and glance have started:
 #
f35cf91a
 #  - build_and_upload_baremetal_deploy_k_and_r $token
 #  - create_baremetal_flavor $BM_DEPLOY_KERNEL_ID $BM_DEPLOY_RAMDISK_ID
 #  - upload_baremetal_image $url $token
 #  - add_baremetal_node <first_mac> <second_mac>
 
 
 # Save trace setting
 XTRACE=$(set +o | grep xtrace)
 set +o xtrace
 
cc6b4435
 
f35cf91a
 # Sub-driver settings
 # -------------------
 
 # sub-driver to use for kernel deployment
6a5aa7c6
 #
 # - nova.virt.baremetal.pxe.PXE
 # - nova.virt.baremetal.tilera.TILERA
f35cf91a
 BM_DRIVER=${BM_DRIVER:-nova.virt.baremetal.pxe.PXE}
 
 # sub-driver to use for remote power management
6a5aa7c6
 #
f35cf91a
 # - nova.virt.baremetal.fake.FakePowerManager, for manual power control
cc404a48
 # - nova.virt.baremetal.ipmi.IPMI, for remote IPMI
f35cf91a
 # - nova.virt.baremetal.tilera_pdu.Pdu, for TilePro hardware
 BM_POWER_MANAGER=${BM_POWER_MANAGER:-nova.virt.baremetal.fake.FakePowerManager}
 
 
 # These should be customized to your environment and hardware
 # -----------------------------------------------------------
 
bbf06459
 # To provide PXE, configure nova-network's dnsmasq rather than run the one
 # dedicated to baremetal. When enable this, make sure these conditions are
 # fulfilled:
cb961597
 #
6a5aa7c6
 # 1) nova-compute and nova-network runs on the same host
 # 2) nova-network uses FlatDHCPManager
cb961597
 #
bbf06459
 # NOTE: the other BM_DNSMASQ_* have no effect on the behavior if this option
6a5aa7c6
 # is enabled.
bbf06459
 BM_DNSMASQ_FROM_NOVA_NETWORK=`trueorfalse False $BM_DNSMASQ_FROM_NOVA_NETWORK`
 
7611c894
 # BM_DNSMASQ_IFACE should match FLAT_NETWORK_BRIDGE
e994f570
 BM_DNSMASQ_IFACE=${BM_DNSMASQ_IFACE:-eth0}
 # if testing on a physical network,
 # BM_DNSMASQ_RANGE must be changed to suit your network
 BM_DNSMASQ_RANGE=${BM_DNSMASQ_RANGE:-}
f35cf91a
 
35336282
 # BM_DNSMASQ_DNS provide dns server to bootstrap clients
 BM_DNSMASQ_DNS=${BM_DNSMASQ_DNS:-}
 
6a5aa7c6
 # BM_FIRST_MAC *must* be set to the MAC address of the node you will
 # boot.  This is passed to dnsmasq along with the kernel/ramdisk to
 # deploy via PXE.
f35cf91a
 BM_FIRST_MAC=${BM_FIRST_MAC:-}
 
 # BM_SECOND_MAC is only important if the host has >1 NIC.
 BM_SECOND_MAC=${BM_SECOND_MAC:-}
 
 # Hostname for the baremetal nova-compute node, if not run on this host
 BM_HOSTNAME=${BM_HOSTNAME:-$(hostname -f)}
 
 # BM_PM_* options are only necessary if BM_POWER_MANAGER=...IPMI
 BM_PM_ADDR=${BM_PM_ADDR:-0.0.0.0}
 BM_PM_USER=${BM_PM_USER:-user}
 BM_PM_PASS=${BM_PM_PASS:-pass}
 
6a5aa7c6
 # BM_FLAVOR_* options are arbitrary and not necessarily related to
 # physical hardware capacity. These can be changed if you are testing
 # BaremetalHostManager with multiple nodes and different flavors.
f35cf91a
 BM_CPU_ARCH=${BM_CPU_ARCH:-x86_64}
 BM_FLAVOR_CPU=${BM_FLAVOR_CPU:-1}
 BM_FLAVOR_RAM=${BM_FLAVOR_RAM:-1024}
 BM_FLAVOR_ROOT_DISK=${BM_FLAVOR_ROOT_DISK:-10}
 BM_FLAVOR_EPHEMERAL_DISK=${BM_FLAVOR_EPHEMERAL_DISK:-0}
 BM_FLAVOR_SWAP=${BM_FLAVOR_SWAP:-1}
 BM_FLAVOR_NAME=${BM_FLAVOR_NAME:-bm.small}
 BM_FLAVOR_ID=${BM_FLAVOR_ID:-11}
 BM_FLAVOR_ARCH=${BM_FLAVOR_ARCH:-$BM_CPU_ARCH}
 
 
d3a18ae1
 # Use DIB to create deploy ramdisk and kernel.
 BM_BUILD_DEPLOY_RAMDISK=`trueorfalse True $BM_BUILD_DEPLOY_RAMDISK`
 # If not use DIB, these files are used as deploy ramdisk/kernel.
 # (The value must be a relative path from $TOP_DIR/files/)
 BM_DEPLOY_RAMDISK=${BM_DEPLOY_RAMDISK:-}
 BM_DEPLOY_KERNEL=${BM_DEPLOY_KERNEL:-}
f35cf91a
 
 # If you need to add any extra flavors to the deploy ramdisk image
 # eg, specific network drivers, specify them here
06fb29c6
 #
 # NOTE(deva): this will be moved to lib/ironic in a future patch
 #             for now, set the default to a suitable value for Ironic's needs
 BM_DEPLOY_FLAVOR=${BM_DEPLOY_FLAVOR:--a amd64 ubuntu deploy-ironic}
f35cf91a
 
 # set URL and version for google shell-in-a-box
 BM_SHELL_IN_A_BOX=${BM_SHELL_IN_A_BOX:-http://shellinabox.googlecode.com/files/shellinabox-2.14.tar.gz}
 
 
 # Functions
 # ---------
 
 # Check if baremetal is properly enabled
 # Returns false if VIRT_DRIVER is not baremetal, or if ENABLED_SERVICES
 # does not contain "baremetal"
aee18c74
 function is_baremetal {
f35cf91a
     if [[ "$ENABLED_SERVICES" =~ 'baremetal' && "$VIRT_DRIVER" = 'baremetal' ]]; then
         return 0
     fi
     return 1
 }
 
 # Install diskimage-builder and shell-in-a-box
 # so that we can build the deployment kernel & ramdisk
aee18c74
 function prepare_baremetal_toolchain {
cbfb3ae1
     if [[ $(type -P ramdisk-image-create) == "" ]]; then
03e20f35
         pip_install_gr diskimage-builder
cbfb3ae1
     fi
f35cf91a
     local shellinabox_basename=$(basename $BM_SHELL_IN_A_BOX)
     if [[ ! -e $DEST/$shellinabox_basename ]]; then
         cd $DEST
         wget $BM_SHELL_IN_A_BOX
     fi
     if [[ ! -d $DEST/${shellinabox_basename%%.tar.gz} ]]; then
         cd $DEST
         tar xzf $shellinabox_basename
     fi
     if [[ ! $(which shellinaboxd) ]]; then
         cd $DEST/${shellinabox_basename%%.tar.gz}
         ./configure
         make
         sudo make install
     fi
 }
 
 # prepare various directories needed by baremetal hypervisor
aee18c74
 function configure_baremetal_nova_dirs {
f35cf91a
     # ensure /tftpboot is prepared
     sudo mkdir -p /tftpboot
     sudo mkdir -p /tftpboot/pxelinux.cfg
74aad31c
 
     PXEBIN=/usr/share/syslinux/pxelinux.0
     if [ ! -f $PXEBIN ]; then
         PXEBIN=/usr/lib/syslinux/pxelinux.0
         if [ ! -f $PXEBIN ]; then
             die $LINENO "pxelinux.0 (from SYSLINUX) not found."
         fi
     fi
 
     sudo cp $PXEBIN /tftpboot/
b2ef890d
     sudo chown -R $STACK_USER:$LIBVIRT_GROUP /tftpboot
f35cf91a
 
     # ensure $NOVA_STATE_PATH/baremetal is prepared
     sudo mkdir -p $NOVA_STATE_PATH/baremetal
     sudo mkdir -p $NOVA_STATE_PATH/baremetal/console
     sudo mkdir -p $NOVA_STATE_PATH/baremetal/dnsmasq
     sudo touch $NOVA_STATE_PATH/baremetal/dnsmasq/dnsmasq-dhcp.host
91b8d13e
     sudo chown -R $STACK_USER $NOVA_STATE_PATH/baremetal
f35cf91a
 
     # ensure dnsmasq is installed but not running
     # because baremetal driver will reconfigure and restart this as needed
75eaaf43
     is_package_installed dnsmasq || install_package dnsmasq
f35cf91a
     stop_service dnsmasq
 }
 
 # build deploy kernel+ramdisk, then upload them to glance
 # this function sets BM_DEPLOY_KERNEL_ID and BM_DEPLOY_RAMDISK_ID
aee18c74
 function upload_baremetal_deploy {
f35cf91a
     token=$1
 
d3a18ae1
     if [ "$BM_BUILD_DEPLOY_RAMDISK" = "True" ]; then
         BM_DEPLOY_KERNEL=bm-deploy.kernel
         BM_DEPLOY_RAMDISK=bm-deploy.initramfs
         if [ ! -e "$TOP_DIR/files/$BM_DEPLOY_KERNEL" -o ! -e "$TOP_DIR/files/$BM_DEPLOY_RAMDISK" ]; then
cbfb3ae1
             ramdisk-image-create $BM_DEPLOY_FLAVOR \
d3a18ae1
                 -o $TOP_DIR/files/bm-deploy
         fi
f35cf91a
     fi
 
     # load them into glance
8d3ac2df
     BM_DEPLOY_KERNEL_ID=$(openstack \
         --os-token $token \
         --os-url http://$GLANCE_HOSTPORT \
         image create \
         $BM_DEPLOY_KERNEL \
         --public --disk-format=aki \
01026934
         --container-format=aki \
101b4248
         < $TOP_DIR/files/$BM_DEPLOY_KERNEL  | grep ' id ' | get_field 2)
8d3ac2df
     BM_DEPLOY_RAMDISK_ID=$(openstack \
         --os-token $token \
         --os-url http://$GLANCE_HOSTPORT \
         image create \
         $BM_DEPLOY_RAMDISK \
         --public --disk-format=ari \
01026934
         --container-format=ari \
101b4248
         < $TOP_DIR/files/$BM_DEPLOY_RAMDISK  | grep ' id ' | get_field 2)
f35cf91a
 }
 
 # create a basic baremetal flavor, associated with deploy kernel & ramdisk
 #
 # Usage: create_baremetal_flavor <aki_uuid> <ari_uuid>
aee18c74
 function create_baremetal_flavor {
f35cf91a
     aki=$1
     ari=$2
     nova flavor-create $BM_FLAVOR_NAME $BM_FLAVOR_ID \
101b4248
         $BM_FLAVOR_RAM $BM_FLAVOR_ROOT_DISK $BM_FLAVOR_CPU
75eaaf43
     nova flavor-key $BM_FLAVOR_NAME set \
101b4248
         "cpu_arch"="$BM_FLAVOR_ARCH" \
         "baremetal:deploy_kernel_id"="$aki" \
         "baremetal:deploy_ramdisk_id"="$ari"
2920b7de
 
f35cf91a
 }
 
6a5aa7c6
 # Pull run-time kernel/ramdisk out of disk image and load into glance.
 # Note that $file is currently expected to be in qcow2 format.
f35cf91a
 # Sets KERNEL_ID and RAMDISK_ID
 #
 # Usage: extract_and_upload_k_and_r_from_image $token $file
aee18c74
 function extract_and_upload_k_and_r_from_image {
f35cf91a
     token=$1
     file=$2
     image_name=$(basename "$file" ".qcow2")
 
     # this call returns the file names as "$kernel,$ramdisk"
cbfb3ae1
     out=$(disk-image-get-kernel \
f35cf91a
             -x -d $TOP_DIR/files -o bm-deploy -i $file)
     if [ $? -ne 0 ]; then
c33d1f98
         die $LINENO "Failed to get kernel and ramdisk from $file"
f35cf91a
     fi
     XTRACE=$(set +o | grep xtrace)
     set +o xtrace
     out=$(echo "$out" | tail -1)
     $XTRACE
     OUT_KERNEL=${out%%,*}
     OUT_RAMDISK=${out##*,}
 
     # load them into glance
8d3ac2df
     KERNEL_ID=$(openstack \
         --os-token $token \
         --os-url http://$GLANCE_HOSTPORT \
         image create \
         $image_name-kernel \
         --public --disk-format=aki \
01026934
         --container-format=aki \
101b4248
         < $TOP_DIR/files/$OUT_KERNEL | grep ' id ' | get_field 2)
8d3ac2df
     RAMDISK_ID=$(openstack \
         --os-token $token \
         --os-url http://$GLANCE_HOSTPORT \
         image create \
         $image_name-initrd \
         --public --disk-format=ari \
01026934
         --container-format=ari \
101b4248
         < $TOP_DIR/files/$OUT_RAMDISK | grep ' id ' | get_field 2)
f35cf91a
 }
 
 
 # Re-implementation of devstack's "upload_image" function
 #
 # Takes the same parameters, but has some peculiarities which made it
 # easier to create a separate method, rather than complicate the logic
 # of the existing function.
aee18c74
 function upload_baremetal_image {
f35cf91a
     local image_url=$1
     local token=$2
 
     # Create a directory for the downloaded image tarballs.
     mkdir -p $FILES/images
 
     # Downloads the image (uec ami+aki style), then extracts it.
     IMAGE_FNAME=`basename "$image_url"`
     if [[ ! -f $FILES/$IMAGE_FNAME || \
         "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
         wget -c $image_url -O $FILES/$IMAGE_FNAME
         if [[ $? -ne 0 ]]; then
             echo "Not found: $image_url"
             return
         fi
     fi
 
     local KERNEL=""
     local RAMDISK=""
     local DISK_FORMAT=""
     local CONTAINER_FORMAT=""
     case "$IMAGE_FNAME" in
         *.tar.gz|*.tgz)
             # Extract ami and aki files
             [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
                 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
                 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
             xdir="$FILES/images/$IMAGE_NAME"
             rm -Rf "$xdir";
             mkdir "$xdir"
             tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
             KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
101b4248
                 [ -f "$f" ] && echo "$f" && break; done; true)
f35cf91a
             RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
101b4248
                 [ -f "$f" ] && echo "$f" && break; done; true)
f35cf91a
             IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
101b4248
                 [ -f "$f" ] && echo "$f" && break; done; true)
f35cf91a
             if [[ -z "$IMAGE_NAME" ]]; then
                 IMAGE_NAME=$(basename "$IMAGE" ".img")
             fi
             DISK_FORMAT=ami
             CONTAINER_FORMAT=ami
             ;;
         *.qcow2)
             IMAGE="$FILES/${IMAGE_FNAME}"
             IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
             DISK_FORMAT=qcow2
             CONTAINER_FORMAT=bare
             ;;
         *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
     esac
 
     if [ "$CONTAINER_FORMAT" = "bare" ]; then
         extract_and_upload_k_and_r_from_image $token $IMAGE
     elif [ "$CONTAINER_FORMAT" = "ami" ]; then
8d3ac2df
         KERNEL_ID=$(openstack \
             --os-token $token \
             --os-url http://$GLANCE_HOSTPORT \
             image create \
             "$IMAGE_NAME-kernel" --public \
f35cf91a
             --container-format aki \
             --disk-format aki < "$KERNEL" | grep ' id ' | get_field 2)
8d3ac2df
         RAMDISK_ID=$(openstack \
             --os-token $token \
             --os-url http://$GLANCE_HOSTPORT \
             image create \
             "$IMAGE_NAME-ramdisk" --public \
f35cf91a
             --container-format ari \
             --disk-format ari < "$RAMDISK" | grep ' id ' | get_field 2)
     else
101b4248
         # TODO(deva): add support for other image types
         return
f35cf91a
     fi
 
8d3ac2df
     openstack \
         --os-token $token \
         --os-url http://$GLANCE_HOSTPORT \
         image create \
         "${IMAGE_NAME%.img}" --public \
101b4248
         --container-format $CONTAINER_FORMAT \
         --disk-format $DISK_FORMAT \
         ${KERNEL_ID:+--property kernel_id=$KERNEL_ID} \
         ${RAMDISK_ID:+--property ramdisk_id=$RAMDISK_ID} < "${IMAGE}"
f35cf91a
 
9a3ba4b3
     # override DEFAULT_IMAGE_NAME so that tempest can find the image
f35cf91a
     # that we just uploaded in glance
     DEFAULT_IMAGE_NAME="${IMAGE_NAME%.img}"
 }
 
aee18c74
 function clear_baremetal_of_all_nodes {
24f79614
     list=$(nova baremetal-node-list | awk -F '| ' 'NR>3 {print $2}' )
16dd8b3e
     for node in $list; do
24f79614
         nova baremetal-node-delete $node
f35cf91a
     done
 }
 
6a5aa7c6
 # Inform nova-baremetal about nodes, MACs, etc.
f35cf91a
 # Defaults to using BM_FIRST_MAC and BM_SECOND_MAC if parameters not specified
 #
 # Usage: add_baremetal_node <first_mac> <second_mac>
aee18c74
 function add_baremetal_node {
f35cf91a
     mac_1=${1:-$BM_FIRST_MAC}
     mac_2=${2:-$BM_SECOND_MAC}
 
24f79614
     id=$(nova baremetal-node-create \
101b4248
         --pm_address="$BM_PM_ADDR" \
         --pm_user="$BM_PM_USER" \
         --pm_password="$BM_PM_PASS" \
         "$BM_HOSTNAME" \
         "$BM_FLAVOR_CPU" \
         "$BM_FLAVOR_RAM" \
         "$BM_FLAVOR_ROOT_DISK" \
         "$mac_1" \
         | grep ' id ' | get_field 2 )
c33d1f98
     [ $? -eq 0 ] || [ "$id" ] || die $LINENO "Error adding baremetal node"
af15d354
     if [ -n "$mac_2" ]; then
         id2=$(nova baremetal-interface-add "$id" "$mac_2" )
         [ $? -eq 0 ] || [ "$id2" ] || die $LINENO "Error adding interface to barmetal node $id"
     fi
f35cf91a
 }
 
 
 # Restore xtrace
 $XTRACE
584d90ec
 
6a5aa7c6
 # Tell emacs to use shell-script-mode
 ## Local variables:
 ## mode: shell-script
 ## End: