PowerShell App Automation

Problem this snippet solves:

This script will create two application pools, one HTTP and one HTTPS, for a given application name and version. It will then create the associated HTTP and HTTPS monitor templates and associate them with the new pools. When a new version of an application is created, this script can be used to create the backend resources for that specific application version. The application virtual server can then be configured to change the default pool to the latest version or an iRule can be used to dynamically switch between which application version is appropriate for the given request.

Code :

#----------------------------------------------------------------------------
# The contents of this file are subject to the "END USER LICENSE AGREEMENT 
# FOR F5 Software Development Kit for iControl"; you may not use this file 
# except in compliance with the License. The License is included in the 
# iControl Software Development Kit.
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is iControl Code and related documentation
# distributed by F5.
#
# The Initial Developer of the Original Code is F5 Networks,
# Inc. Seattle, WA, USA. Portions created by F5 are Copyright (C) 1996-2009 
# F5 Networks, Inc. All Rights Reserved.  iControl (TM) is a registered 
# trademark of F5 Networks, Inc.
#
# Alternatively, the contents of this file may be used under the terms
# of the GNU General Public License (the "GPL"), in which case the
# provisions of GPL are applicable instead of those above.  If you wish
# to allow use of your version of this file only under the terms of the
# GPL and not to allow others to use your version of this file under the
# License, indicate your decision by deleting the provisions above and
# replace them with the notice and other provisions required by the GPL.
# If you do not delete the provisions above, a recipient may use your
# version of this file under either the License or the GPL.
#----------------------------------------------------------------------------
param(
  [string]$Bigip = "",
  [string]$User = "",
  [string]$Pass = "",
  [string]$APPName = "",
  [string]$APPVer = "",
  [int]$HTTPPort = 80,
  [int]$HTTPSPort = 443,
  [string[]]$MemberList = $null,
  [switch]$Delete = $false
);

#----------------------------------------------------------------------------
function Does-PoolExist()
#
#  Description:
#    This function checks for the existence of a pool.
#
#  Parameters:
#    Name - The Name of the pool you wish to search for.
#----------------------------------------------------------------------------
{
  param([string]$Name);
  
  $bExists = $false;
  $pool_list = $(Get-F5.iControl).LocalLBPool.get_list();
  foreach($pool in $pool_list)
  {
    if ( $pool.Equals($Name) )
    {
      $bExists = $true;
      break;
    }
  }
  $bExists;
}

#----------------------------------------------------------------------------
function Create-Pool()
#
#  Description:
#    This function creates a new pool if the given pool name doesn't
#    already exist.
#
#  Parameters:
#    Name       - The Name of the pool you wish to create.
#    MemberList - A string list of pool member addresses.
#    MemberPort - The port the pool members will be configured with.
#----------------------------------------------------------------------------
{
  param(
    [string]$Name,
    [string[]]$MemberList,
    [int]$MemberPort
  );
  
  if ( Does-PoolExist -Name $Name )
  {
    Write-Host "Pool '$Name' Already Exists";
  }
  else
  {
    $IPPortDefList = New-Object -TypeName iControl.CommonIPPortDefinition[] $MemberList.Length;
    for($i=0; $i-lt$MemberList.Length; $i++)
    {
      $IPPortDefList[$i] = New-Object -TypeName iControl.CommonIPPortDefinition;
      $IPPortDefList[$i].address = $MemberList[$i];
      $IPPortDefList[$i].port = $MemberPort;
    }
    
    $(Get-F5.iControl).LocalLBPool.create(
      (,$Name),
      (,"LB_METHOD_ROUND_ROBIN"),
      (,$IPPortDefList)
    );
    Write-Host "Pool '$Name' Successfully Created";
  }
}

#----------------------------------------------------------------------------
function Delete-Pool()
#
#  Description:
#    This function deletes the specified pool.
#
#  Parameters:
#    Name       - The Name of the monitor template you wish to search for.
#----------------------------------------------------------------------------
{
  param( [string]$Name );
  if ( Does-PoolExist -Name $Name )
  {
    $(Get-F5.iControl).LocalLBPool.delete_pool( (,$Name) );
    Write-Host "Deleted Pool '$Name'";
  }
}

#----------------------------------------------------------------------------
function Does-MonitorTemplateExist()
#
#  Description:
#    This function checks for the existence of a specified monitor template.
#
#  Parameters:
#    Name       - The Name of the monitor template you wish to search for.
#----------------------------------------------------------------------------
{
  param([string]$Name);
  
  $bExists = $false;
  $template_list = $(Get-F5.iControl).LocalLBMonitor.get_template_list();
  foreach($template in $template_list)
  {
    if ( $template.template_name.Equals($Name) )
    {
      $bExists = $true;
      break;
    }
  }
  $bExists;
}

#----------------------------------------------------------------------------
function Create-MonitorTemplate()
#
#  Description:
#    This function will create a new HTTP or HTTPS monitor template with the
#    specified Send and Receive strings.
#
#  Parameters:
#    Name       - The Name of the monitor template you wish to create.
#    Send       - The HTTP Send String.
#    Receive    - The HTTP Receive String.
#    Type       - The MonitorTemplateType for this template (ie. TTYPE_HTTP, etc).
#----------------------------------------------------------------------------
{
  param(
    [string]$Name,
    [string]$Send,
    [string]$Receive,
    [string]$Type
  );
  
  if ( Does-MonitorTemplateExist -Name $Name )
  {
    Write-Host "Monitor Template '$Name' Already Exists";
  }
  else
  {
    $template = New-Object -TypeName iControl.LocalLBMonitorMonitorTemplate;
    $template.template_name = $Name;
    $template.template_type = $Type;

    $template_attribute = New-Object -TypeName iControl.LocalLBMonitorCommonAttributes;
    $template_attribute.parent_template = "";
    $template_attribute.interval = 5;
    $template_attribute.timeout = 16;
    $template_attribute.dest_ipport = New-Object -TypeName iControl.LocalLBMonitorIPPort;
    $template_attribute.dest_ipport.address_type = "ATYPE_STAR_ADDRESS_STAR_PORT";
    $template_attribute.dest_ipport.ipport = New-Object -TypeName iControl.CommonIPPortDefinition;
    $template_attribute.dest_ipport.ipport.address = "0.0.0.0";
    $template_attribute.dest_ipport.ipport.port = 0;
    $template_attribute.is_read_only = $false;
    $template_attribute.is_directly_usable = $true;
    
    $(Get-F5.iControl).LocalLBMonitor.create_template(
      (, $template),
      (, $template_attribute)
    );
    
    $StringValues = New-Object -TypeName iControl.LocalLBMonitorStringValue[] 2;
    $StringValues[0] = New-Object -TypeName iControl.LocalLBMonitorStringValue;
    $StringValues[0].type = "STYPE_SEND";
    $StringValues[0].value = $Send;
    $StringValues[1] = New-Object -TypeName iControl.LocalLBMonitorStringValue;
    $StringValues[1].type = "STYPE_RECEIVE";
    $StringValues[1].value = $Receive;
    
    # Set HTTP Specific attributes
    $(Get-F5.iControl).LocalLBMonitor.set_template_string_property(
      ($Name,$Name),
      $StringValues
    );
    
    Write-Host "Monitor '$Monitor '$Name' Succesffully Created";
  }
}


#----------------------------------------------------------------------------
function Delete-MonitorTemplate()
#
#  Description:
#    This function deletes the specified monitor template.
#
#  Parameters:
#    Name       - The Name of the monitor template you wish to search for.
#----------------------------------------------------------------------------
{
  param( [string]$Name );
  if ( Does-MonitorTemplateExist -Name $Name )
  {
    $(Get-F5.iControl).LocalLBMonitor.delete_template( (,$Name) );
    Write-Host "Deleted Monitor Template '$Name'";
  }
}

#----------------------------------------------------------------------------
function Associate-MonitorWithPool()
#
#  Description:
#    This function will associate a monitor template with the specified pool.
#
#  Parameters:
#    PoolName    - The Pool name to target.
#    MonitorName - The Monitor Template name to associate with the given pool.
#----------------------------------------------------------------------------
{
  param(
    [string]$PoolName,
    [string]$MonitorName
  );
  
  $monitor_association = New-Object -TypeName iControl.LocalLBPoolMonitorAssociation;
  $monitor_association.pool_name = $PoolName;
  $monitor_association.monitor_rule = New-Object -TypeName iControl.LocalLBMonitorRule;
  $monitor_association.monitor_rule.type = "MONITOR_RULE_TYPE_SINGLE";
  $monitor_association.monitor_rule.quorum = 0;
  $monitor_association.monitor_rule.monitor_templates = (, $MonitorName);
  
  $(Get-F5.iControl).LocalLBPool.set_monitor_association(
    (, $monitor_association)
  );
  
  Write-Host "Monitor '$MonitorName' Is Associated With Pool '$PoolName'";
}

#----------------------------------------------------------------------------
function Save-Configuration()
#
#  Description:
#    This function will flush the running high level configuration to the 
#    /config/bigip.conf file.
#
#  Parameters:
#    None
#----------------------------------------------------------------------------
{
  $(Get-F5.iControl).SystemConfigSync.save_configuration(
    "/config/bigip.conf",
    "SAVE_HIGH_LEVEL_CONFIG"
  );
  Write-Host "Configuration Changes Successfully Saved";
}

#-------------------------------------------------------------------------
function Do-Initialize()
#
#  Description:
#    This function will verify that the iControl PowerShell Snapin is loaded
#    in the current runspace.
#
#-------------------------------------------------------------------------
{
  if ( (Get-PSSnapin | Where-Object { $_.Name -eq "iControlSnapIn"}) -eq $null )
  {
    Add-PSSnapIn iControlSnapIn
  }
  $success = Initialize-F5.iControl -HostName $Bigip -Username $User -Password $Pass;
  
  return $success;
}

#----------------------------------------------------------------------------
function Show-Usage()
#
#  Description:
#    This function will print the script usage information.
#
#----------------------------------------------------------------------------
{
  Write-Host @"
Usage: APIPoolCreate.ps1 Arguments
       Argument   - Description
       ----------   -----------
       Bigip      - The ip/hostname of your BIG-IP.
       User       - The Managmenet username for your BIG-IP.
       Pass       - The Management password for your BIG-IP.
       APPName    - The Name of the application.
       APPVer     - The Version of the application.
       HTTPPort   - The port the HTTP pool members will be listening on (def = 80)
       HTTPSPort  - The port the HTTPS pool members will be listening on (def = 443)
       MemberList - The list of pool members for the HTTP and HTTPS pools.
"@;
}

#============================================================================
# Main application logic
#============================================================================
if ( ($Bigip.Length -eq 0) -or ($User.Length -eq 0) -or ($Pass.Length -eq 0) -or`
     ($APPName.Length -eq 0) -or ($APPVer.Length -eq 0) -or ($MemberList -eq $null) )
{
  Show-Usage;
}
else
{
  if ( Do-Initialize )
  {
    $Basename = "${APPName}_${APPVer}";
    
    # HTTP Objects
    $Name = "${Basename}_HTTP";
    
    if ( $Delete -eq $false )
    {
      Write-Host "Creating HTTP Pool and Monitor Association...";
      Create-MonitorTemplate -Name $Name -Send "GET /CardMgmt/${APPVer}/CMS.asmx" `
        -Receive "FindCardRecord" -Type "TTYPE_HTTP";
      Create-Pool -Name $Name -MemberList $MemberList -MemberPort $HTTPPort;
      Associate-MonitorWithPool -PoolName $Name -MonitorName $Name;
    }
    else
    {
      Write-Host "Deleting HTTP Pool and Monitor Association...";
      Delete-Pool -Name $Name;
      Delete-MonitorTemplate -Name $Name;
    }
    
    # HTTPS Objects
    $Name = "${Basename}_HTTPS";
    if ( $Delete -eq $false )
    {
      Write-Host "Creating HTTPS Pool and Monitor Association...";
      Create-MonitorTemplate -Name $Name -Send "GET /CardMgmt/${APPVer}/CMS.asmx" `
        -Receive "FindCardRecord" -Type "TTYPE_HTTPS";
      Create-Pool -Name $Name -MemberList $MemberList -MemberPort $HTTPSPort;
      Associate-MonitorWithPool -PoolName $Name -MonitorName $Name;
    }
    else
    {
      Write-Host "Deleting HTTPS Pool and Monitor Association...";
      Delete-Pool -Name $Name;
      Delete-MonitorTemplate -Name $Name;
    }
    
    # Save Configuration
    Save-Configuration;
  }
  else
  {
    Write-Error "ERROR: iControl subsystem not initialized"
  }
}
Published Mar 09, 2015
Version 1.0

Was this article helpful?

No CommentsBe the first to comment