Skip to content

Commit

Permalink
(#245) Moves Helper Functions into Module
Browse files Browse the repository at this point in the history
This is good practice for work going forward, and begins to enable some of the later stage work we want to do to unify our environment setup(s).
  • Loading branch information
JPRuskin committed Sep 27, 2024
1 parent 0562a2f commit c0b4a64
Show file tree
Hide file tree
Showing 12 changed files with 2,474 additions and 2,346 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ jobs:
param(
$Path
)
. C:\choco-setup\files\scripts\Get-Helpers.ps1
Import-Module C:\choco-setup\files\modules\C4B-Environment
$configuration = New-PesterConfiguration @{
Run = @{
Container = New-PesterContainer -Path $Path -Data @{ Fqdn = $using:CertDetails.FQDN }
Expand Down
2 changes: 1 addition & 1 deletion OfflineInstallPreparation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ $ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
$LicensePath = Convert-Path $LicensePath

. $PSScriptRoot\scripts\Get-Helpers.ps1
Import-Module $PSScriptRoot\modules\C4B-Environment

$ChocoInstallScript = Join-Path $PSScriptRoot "scripts\ChocolateyInstall.ps1"
if (-not (Test-Path $ChocoInstallScript)) {
Expand Down
10 changes: 4 additions & 6 deletions Set-SslSecurity.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#requires -modules C4B-Environment
using namespace System.Net.Sockets
using namespace System.Net.Security
using namespace System.Security.Cryptography.X509Certificates
Expand Down Expand Up @@ -75,10 +76,7 @@ process {
$DefaultEap = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
Start-Transcript -Path "$env:SystemDrive\choco-setup\logs\Set-SslCertificate-$(Get-Date -Format 'yyyyMMdd-HHmmss').txt"

# Dot-source helper functions
$ScriptDir = Join-Path $PSScriptRoot "scripts"
. $ScriptDir\Get-Helpers.ps1

# Collect current certificate configuration
$Certificate = if ($Subject) {
Get-Certificate -Subject $Subject
Expand Down Expand Up @@ -149,7 +147,7 @@ process {
Connect-NexusServer -Hostname $SubjectWithoutCn -Credential $Credential -UseSSL

# Push ClientSetup.ps1 to raw repo
$ClientScript = "$ScriptDir\ClientSetup.ps1"
$ClientScript = "$PSScriptRoot\scripts\ClientSetup.ps1"
(Get-Content -Path $ClientScript) -replace "{{hostname}}", $SubjectWithoutCn | Set-Content -Path $ClientScript
New-NexusRawComponent -RepositoryName 'choco-install' -File $ClientScript

Expand Down Expand Up @@ -255,7 +253,7 @@ process {
}

# Generate Register-C4bEndpoint.ps1
$EndpointScript = "$ScriptDir\Register-C4bEndpoint.ps1"
$EndpointScript = "$PSScriptRoot\scripts\Register-C4bEndpoint.ps1"

if ($Hardened) {

Expand Down
4 changes: 1 addition & 3 deletions Start-C4bCcmSetup.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#requires -modules C4B-Environment
<#
.SYNOPSIS
C4B Quick-Start Guide CCM setup script
Expand Down Expand Up @@ -26,9 +27,6 @@ process {
$ErrorActionPreference = 'Stop'
Start-Transcript -Path "$env:SystemDrive\choco-setup\logs\Start-C4bCcmSetup-$(Get-Date -Format 'yyyyMMdd-HHmmss').txt"

# Dot-source helper functions
. .\scripts\Get-Helpers.ps1

$Packages = (Get-Content $PSScriptRoot\files\chocolatey.json | ConvertFrom-Json).packages

Set-ChocoEnvironmentProperty -Name DatabaseUser -Value $DatabaseCredential
Expand Down
4 changes: 1 addition & 3 deletions Start-C4bJenkinsSetup.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#requires -Modules C4B-Environment
<#
.SYNOPSIS
C4B Quick-Start Guide Jenkins setup script
Expand All @@ -24,9 +25,6 @@ process {
$ErrorActionPreference = 'Stop'
Start-Transcript -Path "$env:SystemDrive\choco-setup\logs\Start-C4bJenkinsSetup-$(Get-Date -Format 'yyyyMMdd-HHmmss').txt"

# Dot-source helper functions
. .\scripts\Get-Helpers.ps1

# Install temurin21jre to meet JRE>11 dependency of Jenkins
$chocoArgs = @('install', 'temurin21jre', '-y', '--no-progress', "--params='/ADDLOCAL=FeatureJavaHome'")
& choco @chocoArgs
Expand Down
4 changes: 1 addition & 3 deletions Start-C4bNexusSetup.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#requires -Modules C4B-Environment
<#
.SYNOPSIS
C4B Quick-Start Guide Nexus setup script
Expand Down Expand Up @@ -25,9 +26,6 @@ process {
$ErrorActionPreference = 'Stop'
Start-Transcript -Path "$env:SystemDrive\choco-setup\logs\Start-C4bNexusSetup-$(Get-Date -Format 'yyyyMMdd-HHmmss').txt"

# Dot-source helper functions
. .\scripts\Get-Helpers.ps1

$Packages = (Get-Content $PSScriptRoot\files\chocolatey.json | ConvertFrom-Json).packages

# Install base nexus-repository package
Expand Down
10 changes: 8 additions & 2 deletions Start-C4bSetup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,14 @@ try {
}
}

# Import Helper Functions
. $FilesDir\scripts\Get-Helpers.ps1
# Add the Module Path and Import Helper Functions
if (-not (Get-Module C4B-Environment -ListAvailable)) {
if ($env:PSModulePath.Split(';') -notcontains "$PSScriptRoot\modules") {
[Environment]::SetEnvironmentVariable("PSModulePath", "$env:PSModulePath;$PSScriptRoot\modules" ,"Machine")
$env:PSModulePath = [Environment]::GetEnvironmentVariables("Machine").PSModulePath
}
}
Import-Module C4B-Environment -Verbose:$false

# Downloading all CCM setup packages below
Write-Host "Downloading missing nupkg files to $($PkgsDir)." -ForegroundColor Green
Expand Down
7 changes: 1 addition & 6 deletions Start-C4bVerification.ps1
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
#requires -modules C4B-Environment
[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[String]
$Fqdn
)

process {
#Load helper functions in scope for tests
$HelperPath = Join-Path (Split-Path -Parent $MyInvocation.MyCommand.Definition) -ChildPath 'scripts'
$Helpers = Join-Path $HelperPath -ChildPath 'Get-Helpers.ps1'
. $Helpers

if (-not (Get-Module Pester -ListAvailable).Where{$_.Version -gt "5.0"}) {
Write-Host "Installing Pester 5 to run validation tests"
$chocoArgs = @('install', 'pester', '-y', '--no-progress', '--source="https://community.chocolatey.org/api/v2/"')
Expand Down
132 changes: 132 additions & 0 deletions modules/C4B-Environment/C4B-Environment.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#
# Module manifest for module 'c4b-helpers'
#
# Generated by: james
#
# Generated on: 18/09/2024
#

@{

# Script module or binary module file associated with this manifest.
RootModule = 'C4B-Environment.psm1'

# Version number of this module.
ModuleVersion = '0.0.1'

# Supported PSEditions
# CompatiblePSEditions = @()

# ID used to uniquely identify this module
GUID = '29a70562-5048-4ab9-9654-1bd5e962b1c5'

# Author of this module
Author = 'james'

# Company or vendor of this module
CompanyName = 'Chocolatey Software'

# Copyright statement for this module
Copyright = '(c) james. All rights reserved.'

# Description of the functionality provided by this module
# Description = ''

# Minimum version of the PowerShell engine required by this module
# PowerShellVersion = ''

# Name of the PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# ClrVersion = ''

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = '*'

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = '*'

# Variables to export from this module
VariablesToExport = '*'

# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = '*'

# DSC resources to export from this module
# DscResourcesToExport = @()

# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
# FileList = @()

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{

PSData = @{

# Tags applied to this module. These help with module discovery in online galleries.
# Tags = @()

# A URL to the license for this module.
# LicenseUri = ''

# A URL to the main website for this project.
# ProjectUri = ''

# A URL to an icon representing this module.
# IconUri = ''

# ReleaseNotes of this module
# ReleaseNotes = ''

# Prerelease string of this module
# Prerelease = ''

# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false

# External dependent modules of this module
# ExternalModuleDependencies = @()

} # End of PSData hashtable

} # End of PrivateData hashtable

# HelpInfo URI of this module
# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''

}

Loading

0 comments on commit c0b4a64

Please sign in to comment.