14 January 2016

Puppet Vgextend LVM module

I've recently extended our lvm2 Puppet module with the vgextend feature.

Below is what I've come up with. There are a Puppet file and a bash script for checking if VG needs to be extended.

You have to define VG name and PVs in the node YAML file.

vgextend.pp

# == Define: lvm2::vgextend
# Extend an LVM Volume Group (VG)
#
# === Parameters
# [*name*]
#   Volume Group Name
#
# [*physicalvolumes*]
#   Array of physical volumes (PV) to extend the VG

define lvm2::vgextend($physicalvolumes) {

  validate_array($physicalvolumes)

  include '::lvm2'

  $pv = join($physicalvolumes,' ')
  $onlyif_check = $::lvm2::vg_onlyif_check
  $cmd = "${onlyif_check} apply ${name} ${pv}"
  $pipe = $::lvm2::dryrun_pipe_command
  $command = $::lvm2::dryrun ? {
    false   => $cmd,
    default => "echo '${cmd}' ${pipe}",
  }

  exec { "extend_volume_group_${name}" :
    command => $command,
    path    => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
    tag     => 'lvm2',
    onlyif  => "${onlyif_check} check ${name} ${pv}",
  }

}



vg_extend_check.sh


#!/bin/bash
# This is used by exec onlyif Puppet parameter to check if the vgextend
# should be done (i.e., vgextend will be attempted only when exit code is 0)
set -u
set -e

ARGS=("$@")
FLAG=${ARGS[0]}
VG_NAME=${ARGS[1]}
ALL_PV=("${ARGS[@]:2}")
CUR_PV=($(vgdisplay -v 2> /dev/null |awk '/PV Name/ {print$3}'))
NEW_PV=`echo "${ALL_PV[@]}" "${CUR_PV[@]}" | tr ' ' '\n' | sort | uniq -u`
CMD="vgextend $VG_NAME $NEW_PV"

case $FLAG in
check )
if [[ "${#ALL_PV[@]}" > "${#CUR_PV[@]}" ]]; then
   echo "New Physical Volume(s) $NEW_PV found!"
   exit 0
elif [[ "${#ALL_PV[@]}" == "${#CUR_PV[@]}" ]]; then
if [[ -z "$NEW_PV" ]]; then
echo "No change in Physical Volume(s) of $VG_NAME Volume Group"
exit 1
else
echo "We don't remove $NEW_PV Physical Volume from Volume Group $VG_NAME !"
exit 1
fi
elif [[ "${#ALL_PV[@]}" < "${#CUR_PV[@]}" ]]; then
echo "We don't remove $NEW_PV Physical Volume from Volume Group $VG_NAME !"
exit 1
fi
;;
apply )
$CMD
;;
esac



No comments:

Post a Comment