cancel
Showing results for 
Search instead for 
Did you mean: 

Problem this snippet solves:

The following PowerShell script illustrates how to support the BIG-IP .tmpl file format for Application Templates. It includes support for importing and exporting Application Templates to/from .tmpl format.

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-2012
# 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 (
  $BIGIP = $null,
  $User = $null,
  $Pass = $null,
  $Command = $null,
  $TemplateFile = $null,
  $TemplateName = $null,
  [switch]$Force = $false
)
$script:Dir = $(Split-Path $MyInvocation.MyCommand.Definition);
$script:Name = $($MyInvocation.MyCommand.Name);
#-------------------------------------------------------------------------
# function Show-Usage
#-------------------------------------------------------------------------
function Show-Usage()
{
  Write-Host @"
Usage: $($script:Name) [options]
         -BIGIP bigip_address
         -User  bigip_username
         -Pass bigip_password
         -Command [import|export|exportall] 
         -TemplateFile localfile
         -TemplateName name_of_template
         [-Force]
  
Examples

List local templates.
  .\$($script:Name) -BIGIP bigip -User user -Pass pass -Command import
  
Import local .tmpl file to BIG-IP.
  .\$($script:Name) -BIGIP bigip -User user -Pass pass -Command import -TemplateFile filename
  
List templates configured on BIG-IP.
  .\$($script:Name) -BIGIP bigip -User user -Pass pass -Command export
  
Export specified template to local .\Templates folder.
  .\$($script:Name) -BIGIP bigip -User user -Pass pass -Command export -TemplateName template_name
  
Export all templates to local .\Templates folder.  
  .\$($script:Name) -BIGIP bigip -User user -Pass pass -Command exportall
  
"@
  exit;
}


#----------------------------------------------------------------------------
function Get-NextToken()
#----------------------------------------------------------------------------
{
  param($Template = $null, $FirstToken);
  $Value = "";
  
  $idxStart = $Template.IndexOf($FirstToken);
  if ( -1 -ne $idxStart )
  {
    $idxStart += $FirstToken.Length + 1;
    
    $idxEnd = $Template.IndexOfAny( (" ", "`t", "`r", "`n"), $idxStart);
    if ( -1 -ne $idxEnd )
    {
      $Value = $Template.SubString($idxStart, $idxEnd - $idxStart);
    }
  }
  $Value;
}

#----------------------------------------------------------------------------
function Extract-Section()
#----------------------------------------------------------------------------
{
  param($Template = $null,
    $Section = $null
  );
  
  $Value = $null;
  
  $idxStart = $Template.IndexOf("$Section {");
  $idxEnd = -1;
  if ( -1 -ne $idxStart )
  {
    $idxStart += $Section.Length + 2;
    $p = 1;
    for($i = $idxStart; $i -lt $Template.Length; $i++)
    {
      switch ($Template[$i])
      {
        "{" {
          $p++;
        }
        "}" {
          $p--; 
          if ( $p -eq 0 ) {
            $idxEnd = $i;
            $i = $Template.Length;
            break;
          }
        }
      }
    }
    if ( -1 -ne $idxEnd )
    {
      #Write-Host "START: $idxStart, END: $idxEnd, LEN: $($idxEnd-$idxStart)";
      $Value = $Template.SubString($idxStart, $idxEnd-$idxStart).Trim();
    }
  }
  $Value;
}

#----------------------------------------------------------------------------
function New-Template()
#----------------------------------------------------------------------------
{
  $t = 1 | Select Name, Description, HtmlHelp, Implementation, Presentation;
  $t;
}

#----------------------------------------------------------------------------
function Parse-Template()
#----------------------------------------------------------------------------
{
  param($Template = $null);
  $t = $null;
  
  if ( $Template.StartsWith("sys application template") )
  {
    
    #Write-Host "Name: '$name'";
  
    $t = New-Template;
    
    $t.Name = Get-NextToken $Template "sys application template";
    $definition = Extract-Section $Template "definition";
    $t.HtmlHelp       = Extract-Section $definition "html-help";
    $t.Implementation = Extract-Section $definition "implementation";
    $t.Presentation   = Extract-Section $definition "presentation";
  }
  $t;
}

#----------------------------------------------------------------------------
function Import-Template()
#----------------------------------------------------------------------------
{
  param(
    $TemplateFile = $null,
    [Boolean]$Force = $false
  );
  
  $content = [System.IO.File]::ReadAllText((Resolve-Path $TemplateFile).Path) 
  $t = Parse-Template -Template $content;
  $bContinue = $true;
  
  if ( $null -ne $t )
  {
    Write-Host "CREATING Template: '$($t.Name)'...";
    try 
    {
      (Get-F5.iControl).ManagementApplicationTemplate.create( @($t.Name) );
    }
    catch
    {
      $bContinue = $Force;
      if ( $bContinue )
      {
        Write-Host "Template exists, overwriting...";
      }
      else
      {
        Write-Host "Template exists, stopping import.  Use '-Force' to overwrite.";
      }
    }
    
    if ( $bContinue )
    {
      Write-Host "SETTING IMPLEMENTATION...";
      (Get-F5.iControl).ManagementApplicationTemplate.set_action_implementation( @($t.Name), @( @("definition")), @( @($t.Implementation)) );
      Write-Host "SETTING PRESENTATION...";
      (Get-F5.iControl).ManagementApplicationTemplate.set_action_presentation( @($t.Name), @( @("definition")), @( @($t.Presentation)) );
      Write-Host "SETTING PRESENTATION HELP...";
      (Get-F5.iControl).ManagementApplicationTemplate.set_action_presentation_help( @($t.Name), @( @("definition")), @( @($t.HtmlHelp)) );
    }
  }
}

#----------------------------------------------------------------------------
function Export-Template()
#----------------------------------------------------------------------------
{
  param(
    $TemplateFile = $null,
    $TemplateName = $null,
    [Boolean]$Force = $false
  );
  
  $t = New-Template;
  $t.Name = $TemplateName;
  
  $descA = (Get-F5.iControl).ManagementApplicationTemplate.get_description( @($t.Name) );
  $implAofA = (Get-F5.iControl).ManagementApplicationTemplate.get_action_implementation( @($t.Name), @( @("definition")) );
  $presAofA = (Get-F5.iControl).ManagementApplicationTemplate.get_action_presentation( @($t.Name), @( @("definition")) );
  $helpAofA = (Get-F5.iControl).ManagementApplicationTemplate.get_action_presentation_help( @($t.Name), @( @("definition")) );

  $t.Description = $descA[0];
  if ( $t.Description.Length -eq 0 ) { $t.Description = "none"; }
  $t.Implementation = $implAofA[0][0];
  $t.Presentation = $presAofA[0][0];
  $t.HtmlHelp = $helpAofA[0][0];

  $s = "sys application template ";
  $s += Split-Path -Leaf $t.Name;
  $s += " {`r`n";
  # Begin Action
  $s += " "*4;
  $s += "actions {`r`n";
  
  # Begin definition
  $s += " "*8;
  $s += "definition {`r`n";

  # Help
  $s += " "*12;
  $s += "html-help {`r`n";
  $s += $t.HtmlHelp;
  if ( $t.HtmlHelp.Length -gt 0 ) { $s += "`r`n"; }
  $s += "}`r`n";
  
  # Implementation
  $s += " "*12;
  $s += "implementation {`r`n";
  $s += $t.Implementation;
  if ( $t.Implementation.Length -gt 0 ) { $s += "`r`n"; }
  $s += "}`r`n";
  
  # Presentation
  $s += " "*12;
  $s += "presentation {`r`n";
  $s += $t.Presentation;
  if ( $t.Presentation.Length -gt 0 ) { $s += "`r`n"; }
  $s += "}`r`n";
  
  $s += " "*12;
  $s += "role-acl none`r`n";
  $s += " "*12;
  $s += "run-as none`r`n";
  
  # End Definition
  $s += " "*8;
  $s += "}`r`n";
  
  # End Action
  $s += " "*4;
  $s += "}`r`n";

  $s += " "*4;
  $s += "description $($t.Description)`r`n";
  $s += " "*4;
  $s += "partition $( (Split-Path $t.Name).SubString(1) )`r`n";
  
  #End template
  $s += "}";

  if ( $null -eq $TemplateFile )
  {
    $TemplateFile = ".\Templates\" + (Split-Path -Leaf $t.Name) + ".tmpl";
  }
 
  $s | Out-File $TemplateFile;
  
  Write-Host "Template '$TemplateName' saved to file '$TemplateFile'";
}

function Export-AllTemplates()
{
  param(
    [Boolean]$Force = $false
  );
  
  $templates = List-Templates;
  foreach($template in $templates)
  {
    Export-Template -TemplateName $template -Force $Force;
  }
}

#----------------------------------------------------------------------------
function List-Templates()
#----------------------------------------------------------------------------
{
  (Get-F5.iControl).ManagementApplicationTemplate.get_list();
}

function List-LocalTemplates()
{
  "Local Templates Available for Import";
  "-------------------------";
  $files = Get-ChildItem ".\Templates";
  foreach($file in $files) { $file.Name; }
}

#----------------------------------------------------------------------------
function Do-Initialize()
#----------------------------------------------------------------------------
{
  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;
}

#-------------------------------------------------------------------------
# Main Application Logic
#-------------------------------------------------------------------------
if ( ($BIGIP -eq $null) -or ($User -eq $null) -or ($Pass -eq $null) )
{
  Show-Usage;
}

if ( Do-Initialize )
{
  if ( $null -eq $Command )
  {
    Show-Usage;
  }
  else
  {
    switch ($Command)
    {
      "import"
      {
        if ( $null -eq $TemplateFile )
        {
          List-LocalTemplates;
        }
        else
        {
          if ( [System.IO.File]::Exists((Resolve-Path $TemplateFile).Path) )
          {
            Import-Template -TemplateFile $TemplateFile ($Force -eq $True);
          }
          else
          {
            Write-Error "ERROR: File '$TemplateFile' Does Not Exist!";
          }
        }
      }
      "export"
      {
        if ( $null -eq $TemplateName )
        {
          List-Templates;
        }
        elseif ( $null -ne $TemplateName )
        {
          Export-Template -TemplateFile $TemplateFile -TemplateName $TemplateName ($Force -eq $True)
        }
        else
        {
          Show-Usage;
        }
      }
      "exportall"
      {
        Export-AllTemplates ($Force -eq $True)
      }
      default
      {
        Show-Usage;
      }
    }
  }
}
Version history
Last update:
‎09-Mar-2015 11:33
Updated by:
Contributors