cancel
Showing results for 
Search instead for 
Did you mean: 

Problem this snippet solves:

This sample application will illustrate how to use the locking mechanisms in iControl to allow you to create a multi-threaded iControl application.

iControl is a set of web services methods that can be called in any order by any number of clients. Typically an application is written with the plan that it is the only one accessing your devices. In some cases though, you may require the ability to have programs making configuration changes concurrently. In this article, I'll discuss the iControl lock methods that allow for each application to play well with each other.

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-2007 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 (
  $g_bigip = $null,
  $g_uid = $null,
  $g_pwd = $null,
  $g_cmd = $null,
  $g_lockname = $null,
  $g_timeout = $null,
  $g_desc = $null
);

Set-PSDebug -strict;

#-------------------------------------------------------------------------
# function Write-Usage
#-------------------------------------------------------------------------
function Write-Usage()
{
  Write-Host "Usage: ManageLocks.ps1 host uid pwd [options]";
  Write-Host "      options";
  Write-Host "      -------";
  Write-Host "      list                          - Get list of locks";
  Write-Host "      get     lockname              - Get status of a lock";
  Write-Host "      acquire lockname timeout desc - Create a new lock";
  Write-Host "      release lockname              - Release an existing lock";
  Write-Host "      wait    lockname              - Wait until lock is released";
  exit;
}

#-------------------------------------------------------------------------
# function Parse-Exception
#-------------------------------------------------------------------------
function Parse-Exception()
{
  param($ex);
  $obj = New-Object -TypeName System.Object;
  
  if ( $ex.Message.Contains("urn:iControl") )
  {
    $obj | Add-Member -Type noteProperty -Name "IsiControlError" $true;
  }
  else
  {
    $obj | Add-Member -Type noteProperty -Name "IsiControlError" $false;
  }
  
  $sr = New-Object System.IO.StringReader($ex.Message);
  $line = $sr.ReadLine();
  while($null -ne $line)
  {
    if ( $line.Contains("primary_error_code") )
    {
      $parts = $line.Split( (, ':'));
      $v = $parts[1].Trim();
      $vals = $v.Split( (, ' '));
      $p_dec = $vals[0];
      $p_oct = $vals[1].Trim('()');
      
      $obj | Add-Member -Type noteProperty -Name "PrimaryErrorCode" $p_dec;
      $obj | Add-Member -Type noteProperty -Name "PrimaryErrorCodeOctal" $p_oct;
    }
    elseif ( $line.Contains("secondary_error_code") )
    {
      $parts = $line.Split( (, ':'));
      $v = $parts[1].Trim();
      $obj | Add-Member -Type noteProperty -Name "SecondaryErrorCode" $v;
    }
    elseif ( $line.Contains("error_string") )
    {
      $parts = $line.Split( (, ':'));
      $v = $parts[1].Trim();
      $obj | Add-Member -Type noteProperty -Name "ErrorString" $v;
    }
    $line = $sr.ReadLine();
  }
  return $obj;
}

#-------------------------------------------------------------------------
# function Get-LockList
#-------------------------------------------------------------------------
function Get-LockList()
{
  $Locks = (Get-F5.iControl).SystemSystemInfo.get_lock_list();
  Get-LockStatus $Locks;
}

#-------------------------------------------------------------------------
# function Get-LockStatus
#-------------------------------------------------------------------------
function Get-LockStatus()
{
  param($lockname);
  
  trap {
    $ex = Parse-Exception $_.Exception;
    if ( $ex.PrimaryErrorCode -eq 16908289 )
    {
      Write-Host "Lock '$lockname' does not exist!";
    }
    else
    {
      Write-Host "Exception Occurred";
      Write-Host "Primary Error : " + ex.PrimaryErrorCode;
      Write-Host "Error String  : " + ex.ErrorString;
    }
    continue;
  }
  
  if ( $null -eq $lockname )
  {
    Write-Usage;
  }
  else
  {
    $LockStatusA = (Get-F5.icontrol).SystemSystemInfo.get_lock_status( $lockname );
    foreach ($LockStatus in $LockStatusA)
    {
      $name = $LockStatus.lock_name;
      $time_left = $LockStatus.time_left;
      $comment = $LockStatus.comment;
      Write-Host "Lock Name  : $name";
      Write-Host "Time Left  : $time_left";
      Write-Host "Description: $comment";
      Write-Host "----------------------";
    }
  }
}

#-------------------------------------------------------------------------
# function Acquire-Lock
#-------------------------------------------------------------------------
function Acquire-Lock()
{
  param($lockname, $timeout, $desc);
  if ( $null -eq $lockname )
  {
    Write-Usage;
  }
  else
  {
    if ( $null -eq $timeout ) { $timeout = 60; }
    if ( $null -eq $desc    ) { $desc = "60 Second Lock Acquired by ManageLocks PowerShell Script" };
    
    (Get-F5.iControl).SystemSystemInfo.acquire_lock($lockname, $timeout, $desc);
    Get-LockStatus $lockname;
  }
}

#-------------------------------------------------------------------------
# function Release-Lock
#-------------------------------------------------------------------------
function Release-Lock()
{
  param($lockname);
  if ( $null -eq $lockname )
  {
    Write-Usage;
  }
  else
  {
    (Get-F5.iControl).SystemSystemInfo.release_lock($lockname);
  }
}

#-------------------------------------------------------------------------
# function Release-Lock
#-------------------------------------------------------------------------
function Wait-ForLock()
{
  param($lockname);
  
  trap {
    $ex = Parse-Exception $_.Exception;
    if ( $ex.PrimaryErrorCode -eq 16908289 )
    {
      Write-Host "Lock is available";
    }
    else
    {
      Write-Host "Exception Occurred";
      Write-Host "Primary Error : " + ex.PrimaryErrorCode;
      Write-Host "Error String  : " + ex.ErrorString;
    }
    continue;
  }
  
  if ( $null -eq $lockname )
  {
    Write-Usage;
  }
  else
  {
    while($true)
    {
      $LockStatusA = (Get-F5.icontrol).SystemSystemInfo.get_lock_status( $lockname );
      $LockStatus = $LockStatusA[0];
      $name = $LockStatus.lock_name;
      $time_left = $LockStatus.time_left;
      $comment = $LockStatus.comment;
      if ( $time_left -gt 0 )
      {
        Write-Host "$time_left seconds remaining on lock $lockname...";
        Start-Sleep 5
      }
      else
      {
        Write-Host "Lock is available";
        break;
      }
    }
  }
}

#-------------------------------------------------------------------------
# Do-Initialize
#-------------------------------------------------------------------------
function Do-Initialize()
{
  if ( (Get-PSSnapin | Where-Object { $_.Name -eq "iControlSnapIn"}) -eq $null )
  {
    Add-PSSnapIn iControlSnapIn
  }
  $success = Initialize-F5.iControl -HostName $g_bigip -Username $g_uid -Password $g_pwd;
  
  return $success;
}

#-------------------------------------------------------------------------
# Main Application Logic
#-------------------------------------------------------------------------
if ( ($g_bigip -eq $null) -or ($g_uid -eq $null) -or ($g_pwd -eq $null) -or ($g_cmd -eq $null) )
{
  Write-Usage;
}

if ( Do-Initialize )
{
  switch ($g_cmd.ToLower())
  {
    "list" {
      Get-LockList;
    }
    "get" {
      Get-LockStatus $g_lockname;
    }
    "acquire" {
      Acquire-Lock $g_lockname $g_timeout $g_desc;
    }
    "release" {
      Release-Lock $g_lockname;
    }
    "wait" {
      Wait-ForLock $g_lockname;
    }
    default {
      Write-Usage;
    }
  }
}
else
{
  Write-Error "ERROR: iControl subsystem not initialized"
}
Version history
Last update:
‎09-Mar-2015 12:00
Updated by:
Contributors