From af525f2cc8dc954a8d61c31839e81c78f2f17a1e Mon Sep 17 00:00:00 2001 From: Vinod Kiran Date: Sun, 4 Jun 2017 17:10:56 +0530 Subject: [PATCH] Initial Commit Discover Java WebApps - Tomcat Signed-off-by: Vinod Kiran --- .../Artifacts/Tomcat/Discover_Tomcat.ps1 | 116 ++++++++++++++++++ .../Artifacts/Tomcat/Generate_Tomcat.ps1 | 67 ++++++++++ .../Discover_Tomcat.Functional.Tests.ps1 | 0 .../Tomcat/Tests/Discover_Tomcat.Tests.ps1 | 92 ++++++++++++++ .../Generate_Tomcat.Functional.Tests.ps1 | 0 .../Tomcat/Tests/Generate_Tomcat.Tests.ps1 | 61 +++++++++ 6 files changed, 336 insertions(+) create mode 100644 Functions/Private/Artifacts/Tomcat/Discover_Tomcat.ps1 create mode 100644 Functions/Private/Artifacts/Tomcat/Generate_Tomcat.ps1 create mode 100644 Functions/Private/Artifacts/Tomcat/Tests/Discover_Tomcat.Functional.Tests.ps1 create mode 100644 Functions/Private/Artifacts/Tomcat/Tests/Discover_Tomcat.Tests.ps1 create mode 100644 Functions/Private/Artifacts/Tomcat/Tests/Generate_Tomcat.Functional.Tests.ps1 create mode 100644 Functions/Private/Artifacts/Tomcat/Tests/Generate_Tomcat.Tests.ps1 diff --git a/Functions/Private/Artifacts/Tomcat/Discover_Tomcat.ps1 b/Functions/Private/Artifacts/Tomcat/Discover_Tomcat.ps1 new file mode 100644 index 0000000..22a718f --- /dev/null +++ b/Functions/Private/Artifacts/Tomcat/Discover_Tomcat.ps1 @@ -0,0 +1,116 @@ +function Discover_Tomcat { +<# +.SYNOPSIS +Scans for the Tomcat Web Server + +.PARAMETER MountPath +The path where the Windows image was mounted to. + +.PARAMETER OutputPath +The filesystem path where the discovery manifest will be emitted. + +.PARAMETER ArtifactParam +Optional - one or more Website names to include in the output. +#> +[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess",'')] +[CmdletBinding()] +param ( + [Parameter(Mandatory = $true)] + [string] $MountPath, + + [Parameter(Mandatory = $true)] + [string] $OutputPath, + + [Parameter(Mandatory = $true)] + [string] $ImageWindowsVersion, + + [Parameter(Mandatory = $false)] + [string[]] $ArtifactParam +) + + +$ArtifactName = Split-Path -Path $PSScriptRoot -Leaf +Write-Verbose -Message ('Started discovering {0} artifact' -f $ArtifactName) + +### Path to the Tomcat manifest +$Manifest = '{0}\{1}.json' -f $OutputPath, $ArtifactName + +### Create a HashTable to store the results (this will get persisted to JSON) +$ManifestResult = @{ + Name = 'Tomcat' + Status = '' + Path = '' +} + +# Check for existence of the JDK framework: +$JDKPath = "$MountPath" +if (Test-Path -Path $JDKPath) { + $JDKPath = Get-ChildItem -Path $JDKPath -Recurse -Include java.exe -Exclude $MountPath\Windows\* + if ($JDKPath.Count -ge 1) { + for ($i=0; $i -lt $JDKPath.Count; $i++) { + # Write-Verbose -Message ('JavaPath "{0}"' -f $JDKPath[$i].FullName) + if (! $JDKPath[$i].FullName.Contains("jre")) { + $ManifestResult.JDKStatus = 'Present' + $JDKExecutable = $JDKPath[$i].FullName + $JDKHome = $JDKPath[$i].DirectoryName; + if ($JDKHome.EndsWith("\bin")) { + $JDKHome = $JDKHome.Remove($JDKHome.Length-4,4); + } + break; + } + } + Write-Verbose -Message ('Discovered Java (java.exe) at "{0}"' -f $JDKExecutable) + $JavaReleaseFile = $JDKHome+"\release" + if (Test-Path -Path $JavaReleaseFile) { + # JAVA_VERSION="1.8.0_131" + # Write-Verbose -Message ('Reading "{0}" for Version Information' -f $JavaReleaseFile) + $ManifestResult.JDKVersion = (Get-Content $JavaReleaseFile)[0] + if ($ManifestResult.JDKVersion.StartsWith("JAVA_VERSION=")) { + $ManifestResult.JDKVersion = $ManifestResult.JDKVersion.Remove(0,"JAVA_VERSION=".Length+1); + $ManifestResult.JDKVersion = $ManifestResult.JDKVersion.Remove($ManifestResult.JDKVersion.Length-1,1); + Write-Verbose -Message ('Determined that JDK is with version "{0}"' -f $ManifestResult.JDKVersion) + } + } else { + Write-Verbose -Message "Unable to determine JDK version, defaulting to 1.8.1_131" + $ManifestResult.JDKVersion = "1.8.1_131" + } + } +} + +if (!$ManifestResult.JDKStatus -eq 'Present') { + Write-Verbose -Message 'JDK is NOT present on the system' +} + +$TomcatPath = "$MountPath\*tomcat*\*" +if (Test-Path -Path $TomcatPath) { + $Tomcat = Get-ChildItem -Path $TomcatPath -Recurse -Include catalina.bat -Exclude $MountPath\Windows\* +} + +if ($Tomcat.Count -ge 1) { + Write-Verbose -Message ('Discovered Tomcat Web Server (catalina.bat) at "{0}"' -f $Tomcat[0].FullName) + $ManifestResult.Status = 'Present' + $ManifestResult.Path = $Tomcat[0].FullName + + ### Discover the Port that Catalina is exposed on + $ServerXMLPath = $ManifestResult.Path; + if ($ServerXMLPath.EndsWith("\bin\catalina.bat")) { + $ServerXMLPath = $ServerXMLPath.Remove($ServerXMLPath.Length - "\bin\catalina.bat".Length, "\bin\catalina.bat".Length) + $ServerXMLPath += "\conf\server.xml"; + $ManifestResult.ServerXMLPath = $ServerXMLPath; + [xml]$ServerXmlDocument = Get-Content -Path $ServerXMLPath + $ManifestResult.CatalinaPort = $ServerXmlDocument.SelectSingleNode('/Server/Service[@name="Catalina"]/Connector[@protocol="HTTP/1.1"]').GetAttribute("port") + Write-Verbose -Message ('Determined that Catalia is port {0} by examining server.xml {1}' -f $ManifestResult.CatalinaPort, $ServerXMLPath) + } + +} +else { + $ManifestResult.Status = 'Absent' +} + +### Write the result to the manifest file +$ManifestResult | ConvertTo-Json | Set-Content -Path $Manifest + +Write-Verbose -Message ('Finished discovering {0} artifact' -f $ArtifactName) +} + + diff --git a/Functions/Private/Artifacts/Tomcat/Generate_Tomcat.ps1 b/Functions/Private/Artifacts/Tomcat/Generate_Tomcat.ps1 new file mode 100644 index 0000000..677caeb --- /dev/null +++ b/Functions/Private/Artifacts/Tomcat/Generate_Tomcat.ps1 @@ -0,0 +1,67 @@ +function Generate_Tomcat { +<# +.SYNOPSIS +Generates Dockerfile contents for Apache Web Server component + +.PARAMETER ManifestPath +The filesystem path where the JSON manifests are stored. +#> +[CmdletBinding()] +param ( + [Parameter(Mandatory = $true)] + [string] $MountPath, + + [Parameter(Mandatory = $true)] + [string] $ManifestPath +) + + $ArtifactName = Split-Path -Path $PSScriptRoot -Leaf + + Write-Verbose -Message ('Generating result for {0} component' -f (Split-Path -Path $PSScriptRoot -Leaf)) + $Manifest = '{0}\{1}.json' -f $ManifestPath, $ArtifactName + + $Artifact = Get-Content -Path $Manifest -Raw | ConvertFrom-Json + + $ResultBuilder = GetDockerfileBuilder + if ($Artifact.Status -eq 'Present') { + if ($Artifact.JDKStatus -eq 'Present') { + $null = $ResultBuilder.AppendLine('') + $null = $ResultBuilder.AppendLine('# Install Chocolatey') + $null = $ResultBuilder.AppendLine("RUN powershell -NoProfile -ExecutionPolicy Bypass -Command `"`$env:ChocolateyUseWindowsCompression='false'; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))`"") + $null = $ResultBuilder.AppendLine('') + $null = $ResultBuilder.AppendLine("# JDK Version $($Artifact.JDKVersion)") + if ($Artifact.JDKVersion.StartsWith("1.8")) { + $null = $ResultBuilder.AppendLine('RUN choco install -y jdk8') + } + if ($Artifact.JDKVersion.StartsWith("1.7")) { + $null = $ResultBuilder.AppendLine('RUN choco install -y jdk7') + } + } + + $Source = $Artifact.Path; + if ( $Source.EndsWith("\bin\catalina.bat")) { + $Source = $Source.Remove($Source.length - 17, 17); + Write-Verbose -Message ('Creating Zip of Directory {0}' -f ($Source)) + $ArchiveName = Split-Path -Path $Source -Leaf + } + $TomcatZipPath = '{0}\{1}.zip' -f $ArtifactPath, $ArchiveName + Add-Type -assembly "system.io.compression.filesystem" + [io.compression.zipfile]::CreateFromDirectory($Source, $TomcatZipPath)  + $null = $ResultBuilder.AppendLine('') + $null = $ResultBuilder.AppendLine("# Create the Tomcat Folder from Archive") + $null = $ResultBuilder.AppendLine("COPY $ArchiveName.zip c:\temp\") + $null = $ResultBuilder.AppendLine("RUN powershell -Command Expand-Archive -Path c:\temp\$ArchiveName.zip -DestinationPath c:\$ArchiveName") + + # Now you can start tomcat... + $null = $ResultBuilder.AppendLine("ENV CATALINA_HOME c:\$($ArchiveName)") + $null = $ResultBuilder.AppendLine("CMD c:\$($ArchiveName)\bin\catalina.bat run") + Write-Verbose -Message 'Writing instruction to expose catalina port' + $null = $ResultBuilder.AppendLine("EXPOSE $($Artifact.CatalinaPort)") + $null = $ResultBuilder.AppendLine('') + + Write-Verbose -Message ('Artifact is present: {0}. Adding text to Dockerfile {1}.' -f $ArtifactName, $Result) + } + + return $ResultBuilder.ToString() +} + diff --git a/Functions/Private/Artifacts/Tomcat/Tests/Discover_Tomcat.Functional.Tests.ps1 b/Functions/Private/Artifacts/Tomcat/Tests/Discover_Tomcat.Functional.Tests.ps1 new file mode 100644 index 0000000..e69de29 diff --git a/Functions/Private/Artifacts/Tomcat/Tests/Discover_Tomcat.Tests.ps1 b/Functions/Private/Artifacts/Tomcat/Tests/Discover_Tomcat.Tests.ps1 new file mode 100644 index 0000000..8982950 --- /dev/null +++ b/Functions/Private/Artifacts/Tomcat/Tests/Discover_Tomcat.Tests.ps1 @@ -0,0 +1,92 @@ +Describe 'Discover_Apache Tests' { + + Context 'Parameters for Discover_Apache'{ + + It 'Has a Parameter called MountPath' { + $Function.Parameters.Keys.Contains('MountPath') | Should Be 'True' + } + It 'MountPath Parameter is Identified as Mandatory being True' { + [String]$Function.Parameters.MountPath.Attributes.Mandatory | Should be 'True' + } + It 'MountPath Parameter is of String Type' { + $Function.Parameters.MountPath.ParameterType.FullName | Should be 'System.String' + } + It 'MountPath Parameter is member of ParameterSets' { + [String]$Function.Parameters.MountPath.ParameterSets.Keys | Should Be '__AllParameterSets' + } + It 'MountPath Parameter Position is defined correctly' { + [String]$Function.Parameters.MountPath.Attributes.Position | Should be '0' + } + It 'Does MountPath Parameter Accept Pipeline Input?' { + [String]$Function.Parameters.MountPath.Attributes.ValueFromPipeline | Should be 'False' + } + It 'Does MountPath Parameter Accept Pipeline Input by PropertyName?' { + [String]$Function.Parameters.MountPath.Attributes.ValueFromPipelineByPropertyName | Should be 'False' + } + It 'Does MountPath Parameter use advanced parameter Validation? ' { + $Function.Parameters.MountPath.Attributes.TypeID.Name -contains 'ValidateNotNullOrEmptyAttribute' | Should Be 'False' + $Function.Parameters.MountPath.Attributes.TypeID.Name -contains 'ValidateNotNullAttribute' | Should Be 'False' + $Function.Parameters.MountPath.Attributes.TypeID.Name -contains 'ValidateScript' | Should Be 'False' + $Function.Parameters.MountPath.Attributes.TypeID.Name -contains 'ValidateRangeAttribute' | Should Be 'False' + $Function.Parameters.MountPath.Attributes.TypeID.Name -contains 'ValidatePatternAttribute' | Should Be 'False' + } + It 'Has Parameter Help Text for MountPath '{ + $function.Definition.Contains('.PARAMETER MountPath') | Should Be 'True' + } + It 'Has a Parameter called OutputPath' { + $Function.Parameters.Keys.Contains('OutputPath') | Should Be 'True' + } + It 'OutputPath Parameter is Identified as Mandatory being True' { + [String]$Function.Parameters.OutputPath.Attributes.Mandatory | Should be 'True' + } + It 'OutputPath Parameter is of String Type' { + $Function.Parameters.OutputPath.ParameterType.FullName | Should be 'System.String' + } + It 'OutputPath Parameter is member of ParameterSets' { + [String]$Function.Parameters.OutputPath.ParameterSets.Keys | Should Be '__AllParameterSets' + } + It 'OutputPath Parameter Position is defined correctly' { + [String]$Function.Parameters.OutputPath.Attributes.Position | Should be '1' + } + It 'Does OutputPath Parameter Accept Pipeline Input?' { + [String]$Function.Parameters.OutputPath.Attributes.ValueFromPipeline | Should be 'False' + } + It 'Does OutputPath Parameter Accept Pipeline Input by PropertyName?' { + [String]$Function.Parameters.OutputPath.Attributes.ValueFromPipelineByPropertyName | Should be 'False' + } + It 'Does OutputPath Parameter use advanced parameter Validation? ' { + $Function.Parameters.OutputPath.Attributes.TypeID.Name -contains 'ValidateNotNullOrEmptyAttribute' | Should Be 'False' + $Function.Parameters.OutputPath.Attributes.TypeID.Name -contains 'ValidateNotNullAttribute' | Should Be 'False' + $Function.Parameters.OutputPath.Attributes.TypeID.Name -contains 'ValidateScript' | Should Be 'False' + $Function.Parameters.OutputPath.Attributes.TypeID.Name -contains 'ValidateRangeAttribute' | Should Be 'False' + $Function.Parameters.OutputPath.Attributes.TypeID.Name -contains 'ValidatePatternAttribute' | Should Be 'False' + } + It 'Has Parameter Help Text for OutputPath '{ + $function.Definition.Contains('.PARAMETER OutputPath') | Should Be 'True' + } + } + Context "Function $($function.Name) - Help Section" { + + It "Function $($function.Name) Has show-help comment block" { + + $function.Definition.Contains('<#') | should be 'True' + $function.Definition.Contains('#>') | should be 'True' + } + + It "Function $($function.Name) Has show-help comment block has a.SYNOPSIS" { + + $function.Definition.Contains('.SYNOPSIS') -or $function.Definition.Contains('.Synopsis') | should be 'True' + + } + + It "Function $($function.Name) Is an advanced function" { + + $function.CmdletBinding | should be 'True' + $function.Definition.Contains('param') -or $function.Definition.Contains('Param') | should be 'True' + } + + } + + } + + diff --git a/Functions/Private/Artifacts/Tomcat/Tests/Generate_Tomcat.Functional.Tests.ps1 b/Functions/Private/Artifacts/Tomcat/Tests/Generate_Tomcat.Functional.Tests.ps1 new file mode 100644 index 0000000..e69de29 diff --git a/Functions/Private/Artifacts/Tomcat/Tests/Generate_Tomcat.Tests.ps1 b/Functions/Private/Artifacts/Tomcat/Tests/Generate_Tomcat.Tests.ps1 new file mode 100644 index 0000000..d5e1677 --- /dev/null +++ b/Functions/Private/Artifacts/Tomcat/Tests/Generate_Tomcat.Tests.ps1 @@ -0,0 +1,61 @@ +Describe 'Generate_Apache Tests' { + + Context 'Parameters for Generate_Apache'{ + + It 'Has a Parameter called ManifestPath' { + $Function.Parameters.Keys.Contains('ManifestPath') | Should Be 'True' + } + It 'ManifestPath Parameter is Identified as Mandatory being True' { + [String]$Function.Parameters.ManifestPath.Attributes.Mandatory | Should be 'True' + } + It 'ManifestPath Parameter is of String Type' { + $Function.Parameters.ManifestPath.ParameterType.FullName | Should be 'System.String' + } + It 'ManifestPath Parameter is member of ParameterSets' { + [String]$Function.Parameters.ManifestPath.ParameterSets.Keys | Should Be '__AllParameterSets' + } + It 'ManifestPath Parameter Position is defined correctly' { + [String]$Function.Parameters.ManifestPath.Attributes.Position | Should be '0' + } + It 'Does ManifestPath Parameter Accept Pipeline Input?' { + [String]$Function.Parameters.ManifestPath.Attributes.ValueFromPipeline | Should be 'False' + } + It 'Does ManifestPath Parameter Accept Pipeline Input by PropertyName?' { + [String]$Function.Parameters.ManifestPath.Attributes.ValueFromPipelineByPropertyName | Should be 'False' + } + It 'Does ManifestPath Parameter use advanced parameter Validation? ' { + $Function.Parameters.ManifestPath.Attributes.TypeID.Name -contains 'ValidateNotNullOrEmptyAttribute' | Should Be 'False' + $Function.Parameters.ManifestPath.Attributes.TypeID.Name -contains 'ValidateNotNullAttribute' | Should Be 'False' + $Function.Parameters.ManifestPath.Attributes.TypeID.Name -contains 'ValidateScript' | Should Be 'False' + $Function.Parameters.ManifestPath.Attributes.TypeID.Name -contains 'ValidateRangeAttribute' | Should Be 'False' + $Function.Parameters.ManifestPath.Attributes.TypeID.Name -contains 'ValidatePatternAttribute' | Should Be 'False' + } + It 'Has Parameter Help Text for ManifestPath '{ + $function.Definition.Contains('.PARAMETER ManifestPath') | Should Be 'True' + } + } + Context "Function $($function.Name) - Help Section" { + + It "Function $($function.Name) Has show-help comment block" { + + $function.Definition.Contains('<#') | should be 'True' + $function.Definition.Contains('#>') | should be 'True' + } + + It "Function $($function.Name) Has show-help comment block has a.SYNOPSIS" { + + $function.Definition.Contains('.SYNOPSIS') -or $function.Definition.Contains('.Synopsis') | should be 'True' + + } + + It "Function $($function.Name) Is an advanced function" { + + $function.CmdletBinding | should be 'True' + $function.Definition.Contains('param') -or $function.Definition.Contains('Param') | should be 'True' + } + + } + + } + +