Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update receive-fogimage and test choco publish #31

Merged
merged 6 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions FogApi/FogApi.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ See https://github.com/darksidemilk/FogApi?tab=readme-ov-file#versioning for mor
# Usage

You can use Set-fogserverSettings to set your fogserver hostname and api keys.
Or, the first time you try to run a command the settings.json file will automatically open
in notepad on windows, nano on linux, or TextEdit on Mac
Or, the first time you try to run a command the settings.json file will automatically open in an OS Specific editor.
You can also open the settings.json file and edit it manually before running your first command.
The default settings are explanations of where to find the proper settings since json cannot have comments

Once the settings are set you can have a jolly good time utilzing the fog documentation
found here https://news.fogproject.org/simplified-api-documentation/ that was used to model the parameters
Once the settings are set you can utilze the fog documentation found here https://news.fogproject.org/simplified-api-documentation/ that was used to model the parameters for Get-FogObject, Update-FogObject, and Remove-FogObject.
You can also utilize simpler functions of common tasks, see the links below for more info.

# Additional info

Expand Down
130 changes: 112 additions & 18 deletions FogApi/Public/Receive-FogImage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ function Receive-FogImage {

.PARAMETER StartAtTime
When to start to capture, if not given will start now

.PARAMETER fogHost
fogHost object (get-foghost) that can be brought in from pipeline

.PARAMETER NoWol
Switch param to not use wake on lan in the task, default is to use wake on lan

.PARAMETER debugMode
Switch param to mark the task as a debug task

.PARAMETER shutdown
Switch param to indicate the host should shutdown at the end of the task instead of restarting.

.EXAMPLE
Receive-FogImage -hostID "1234"
Expand All @@ -27,6 +39,11 @@ function Receive-FogImage {

Using another alias for this command, will schedule a capture task for the host 1234 at 8pm 2 days from now.
i.e. if today was friday, this would schedule it for sunday at 8pm.

.EXAMPLE
Get-FogHost | Receive-FogImage -debugMode -shutdown

Capture an image from the current host right now in debug mode, shutdown the computer after complete

.NOTES
Pull and Capture are not powershell approved verbs, they were used as aliases to match the opposite
Expand All @@ -40,11 +57,27 @@ function Receive-FogImage {
[Parameter(ParameterSetName='now')]
[Parameter(ParameterSetName='schedule')]
$hostId,
[Parameter(ValueFromPipeline=$true,ParameterSetName='now')]
[Parameter(ValueFromPipeline=$true,ParameterSetName='schedule')]
[Parameter(ValueFromPipeline=$true,ParameterSetName='now-byhost')]
[Parameter(ValueFromPipeline=$true,ParameterSetName='schedule-byhost')]
$fogHost,
[Parameter(ParameterSetName='schedule')]
[datetime]$StartAtTime
[Parameter(ParameterSetName='schedule-byhost')]
[datetime]$StartAtTime,
[Parameter(ParameterSetName='now')]
[Parameter(ParameterSetName='now-byhost')]
[Parameter(ParameterSetName='schedule')]
[Parameter(ParameterSetName='schedule-byhost')]
[switch]$debugMode,
[Parameter(ParameterSetName='now')]
[Parameter(ParameterSetName='now-byhost')]
[Parameter(ParameterSetName='schedule')]
[Parameter(ParameterSetName='schedule-byhost')]
[switch]$NoWol,
[Parameter(ParameterSetName='now')]
[Parameter(ParameterSetName='now-byhost')]
[Parameter(ParameterSetName='schedule')]
[Parameter(ParameterSetName='schedule-byhost')]
[switch]$shutdown
)


Expand All @@ -59,6 +92,26 @@ function Receive-FogImage {
if ($null -eq $fogHost) {
$fogHost = Get-FogHost -hostID $hostId;
}

if (Test-FogVerAbove1dot6) {
$debugstr = "$($debugMode.IsPresent)"
} else {
if ($debugMode) {
$debugStr = "0"
} else {
$debugStr = "1"
}
}
if ($Nowol) {
$wolstr = "0"
} else {
$wolStr = "1"
}
if ($shutdown) {
$shutdownStr = "1"
} else {
$shutdownStr = "0"
}

$currentImage = $fogHost.imageName;
$fogImages = Get-FogImages;
Expand All @@ -67,26 +120,67 @@ function Receive-FogImage {
"Will capture the assigned image $($fogImage.name) - $($fogImage.id) which will capture the os $($fogImage.osname)" | Out-host;
if ($PSCmdlet.ParameterSetName -eq 'now') {
"No Time was specified, queuing the task to start now" | out-host;
$jsonData = "{`"taskTypeID`": `"2`" }";
if (Test-FogVerAbove1dot6) {

$jsonData = @"
{
"taskTypeID": "2"
"shutdown":"$shutDownStr",
"debug":"$debugStr",
"wol":"$wolStr",
"isActive":"1"
}
"@
} else {
$jsonData = @"
{
"taskTypeID": "2"
"shutdown":"$shutDownStr",
"other2":"$debugStr",
"other4":"$wolStr",
"isActive":"1"
}
"@
}

} else {
"Start time of $($StartAtTime) specified, scheduling the task to start at that time" | out-host;
$scheduleTime = Get-FogSecsSinceEpoch -scheduleDate $StartAtTime
$runTime = get-date $StartAtTime -Format "yyyy-M-d HH:MM"
$jsonData = @"
{
"name":"Capture Task",
"type":"S",
"taskTypeID":"2",
"runTime":"$runTime",
"scheduleTime":"$scheduleTime",
"isGroupTask":"0",
"hostID":"$($hostId)",
"shutdown":"0",
"other2":"0",
"other4":"1",
"isActive":"1"
}
if (Test-FogVerAbove1dot6) {
$jsonData = @"
{
"taskName":"Capture Task",
"type":"S",
"taskTypeID":"2",
"runTime":"$runTime",
"scheduleTime":"$scheduleTime",
"isGroupTask":"0",
"hostID":"$($hostId)",
"shutdown":"$shutdownStr",
"debug":"$debugStr",
"wol":"$wolStr",
"isActive":"1"
}
"@
} else {

$jsonData = @"
{
"name":"Capture Task",
"type":"S",
"taskTypeID":"2",
"runTime":"$runTime",
"scheduleTime":"$scheduleTime",
"isGroupTask":"0",
"hostID":"$($hostId)",
"shutdown":"0",
"other2":"0",
"other4":"1",
"isActive":"1"
}
"@
}
}
return New-FogObject -type objecttasktype -coreTaskObject host -jsonData $jsonData -IDofObject "$hostId";
}
Expand Down
27 changes: 13 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Tag, Release, and Publish to PSGallery and Chocolatey Community Repo](https://github.com/darksidemilk/FogApi/actions/workflows/tag-and-release.yml/badge.svg)](https://github.com/darksidemilk/FogApi/actions/workflows/tag-and-release.yml)

This is a powershell module to make using the Fog Project API even easier.
FOG is an opensource tool for imaging comptuters, this module uses the API on your internal fog server to perform almost any operation you can do in the GUI of Fog and provides you with the ability to extend things further.
FOG is an open-source tool for imaging computers, this module uses the API on your internal fog server to perform almost any operation you can do in the GUI of Fog and provides you with the ability to extend things further.
It can be used to create more automation or to simply have a command line method of controlling fog operations.

Docs for this module can be found at https://fogapi.readthedocs.io/en/latest/
Expand All @@ -18,10 +18,9 @@ For more information about FOG see
- https://github.com/FOGProject/fogproject
- https://forums.fogproject.org


## Notes about its new home and future structuring

This module used to be housed at https://github.com/FOGProject/fog-community-scripts/tree/master/PowershellModules/FogApi but has been now moved to its own repo to follow best practicies for powershell modules. Overtime I hope to add pester tests and use other powershell build tools like invoke-build or psake. For now there's a simpler build.ps1 script for combinging the functions into a single module and generating/building/compiling the documentation (using the platyps module). You only need to use the build.ps1 if you want to manually install it instead of using the simpler `Install-Module fogapi` to install it from the powershell gallery.
This module used to be housed at https://github.com/FOGProject/fog-community-scripts/tree/master/PowershellModules/FogApi but has been now moved to its own repo to follow best practices for powershell modules. Overtime I hope to add pester tests and use other powershell build tools like invoke-build or psake. For now there's a simpler build.ps1 script for combining the functions into a single module and generating/building/compiling the documentation (using the platyps module). You only need to use the build.ps1 if you want to manually install it instead of using the simpler `Install-Module fogapi` to install it from the powershell gallery.

# Versioning

Expand All @@ -47,7 +46,7 @@ Any time I publish a new version that isn't a major change I'll increment the re
The module can be installed via PowershellGet, PSResourceGet, Chocolatey, or Manually

## Installation Methods

To install this module you need at least powershell v3, it was originally created with 5.1,
but now for BEST EXPERIENCE use Powershell Core 7+ to be able to use tab completion when running Fog 1.6

Expand Down Expand Up @@ -100,7 +99,7 @@ See https://chocolatey.org for more information on chocolatey package manager
- Open powershell (as admin recommended)
- Run `Import-Module FogApi`

The module is now installed.
The module is now installed.

# Using The Module

Expand All @@ -112,7 +111,7 @@ in notepad on windows, nano on linux, or TextEdit on Mac
You can also open the settings.json file and edit it manually before running your first command, but it's best to use the `Set-FogServerSettings -interactive` function and switch for first time setup.
The default settings in `settings.json` are explanations of where to find the proper settings since json can't have comments

Once the settings are set you can have a jolly good time utilzing the fog documentation
Once the settings are set you can have a jolly good time utilizing the fog documentation
found here https://news.fogproject.org/simplified-api-documentation/ that was used to model the parameters

i.e.
Expand All @@ -125,30 +124,30 @@ Unless you filter a GET with a json body it will return all the results into a p
That object is easy to work with to create other commands. Note: Full Pipeline support will come at a later time
i.e.

```
```powershell
hosts = Get-FogObject -Type Object -CoreObject Host # calls GET on {your-fog-server}/fog/host to list all hosts
```

Now you can search all your hosts for the one or ones you are looking for with powershell
maybe you want to find all the hosts with ''IT'' in the name (note `?` is an alias for `Where-Object`)

```
```powershell
$ITHosts = $hosts.data | ? name -match ''IT'';
```

Now maybe you want to change the image all of these computers use to one named ''''IT-Image''''
You can edit the object in powershell with a foreach-object (`%` is an alias for `foreach-object`)

```
```powershell
#get the id of the image by getting all images and finding the one with the IT-image name
$image = Get-FogImages | ? name -eq "IT-image"
$updatedITHosts = $ITHosts | % { $_.imageid = $image.id}
```

Then you need to convert that object to json and pass each object into one api call at a time.
Then you need to convert that object to json and pass each object into one api call at a time.
Which sounds complicated, but it's not, it's as easy as

```
```powershell
$updatedITHosts | % {
Update-FogObject -Type object -CoreObject host -objectID $_.id -jsonData ($_ | ConvertTo-Json);
}
Expand All @@ -158,7 +157,7 @@ This is just one small example of the limitless things you can do with the api a
There are also many ''helper'' functions that make various operations easier.
i.e. Maybe you want to create a host and deploy that "IT-image" image to it.

```
```powershell
#create the host
New-FogHost -name "test-host" -macs "01:23:45:67:89:00"

Expand All @@ -173,11 +172,11 @@ Update-FogObject -Type object -CoreObject host -objectID $foghost.id -jsonData j
get-foghost -hostname "test-host" | send-fogimage
```

```
```powershell
#alternatively, schedule the image for later, like 10pm tomorrow
get-foghost -hostname "test-host" | send-fogimage -StartAtTime (Get-Date 10pm).AddDays(1)
```

## Additional info

See also the fogforum thread for the module https://forums.fogproject.org/topic/12026/powershell-api-module/2
See also the fog forum thread for the module https://forums.fogproject.org/topic/12026/powershell-api-module/2
9 changes: 5 additions & 4 deletions build-choco.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,12 @@ Requires Powershell 5.1+ (Core 7+ recommended).

This package installs the FogApi module in the AllUsers/System scope for:

- Powershell 7+ (C:\Program Files\Powershell\Modules\FogApi{version})
- Powershell 5.1 (C:\Program Files\WindowsPowershell\Modules\FogApi{version})
- Powershell 7+ (C:\Program Files\Powershell\Modules\FogApi\{version})
- Powershell 5.1 (C:\Program Files\WindowsPowershell\Modules\FogApi\{version})

This ensures compatibility across pwsh versions (which are both included in Win11), removing any other existing versions to avoid conflicts.
The uninstall script removes the specific package version of the given Chocolatey package.
This ensures compatibility across pwsh versions (which are both included in Win11).
During default install, any other existing module versions are removed from both pwsh versions to avoid version conflicts.
The default uninstall behavior removes the specific module version of the given Chocolatey package.
You can customize this behavior with package parameters, though defaults are recommended.

## Package Parameters
Expand Down
50 changes: 41 additions & 9 deletions chocoTemplate/PSGetModule/tools/chocolateyInstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,52 @@ if ($pp.RemoveAll) {
# module may already be installed outside of Chocolatey, if so get it out of the current session
Remove-Module -Name $moduleName -Force -ErrorAction SilentlyContinue

#get the destination paths to install too

$destinationRootPath = New-Object -TypeName 'System.Collections.generic.list[System.Object]';
$ps7Pth = (Join-Path -Path $env:ProgramFiles -ChildPath "PowerShell\Modules\$moduleName")
$ps5Pth = (Join-Path -Path $env:ProgramFiles -ChildPath "WindowsPowerShell\Modules\$moduleName")

if($pp.PS7Only) {
$destinationRootPath.add((Join-Path -Path $env:ProgramFiles -ChildPath "PowerShell\Modules\$moduleName"))
if (Test-Path ($ps7Pth | Split-Path)) { #check if parent path exists
$destinationRootPath.add($ps7Pth)
} else {
Write-Error "Powershell core module path doesn't exist but PS7Only was specified! install powershell-core package to install!"
Set-PowershellExitCode -exitcode 1
exit 1;
}
"Installing $modulename version $moduleVersion in pwsh 7+ system path only" | out-host;
} elseif ($pp.PS5Only) {
$destinationRootPath.add((Join-Path -Path $env:ProgramFiles -ChildPath "WindowsPowerShell\Modules\$moduleName"))
if (Test-Path ($ps5Pth | Split-Path)) { #check if parent path exists
$destinationRootPath.add($ps5Pth)
} else {
Write-Error "Powershell 5.1 module path doesn't exist and ps5only was specified, install powershell package to install"
Set-PowershellExitCode -exitcode 1
exit 1;
}
"Installing $modulename version $moduleVersion in windows powershell 5.1 system path only" | out-host;
} else {
#ps 5.1 installed system mods
$destinationRootPath.add((Join-Path -Path $env:ProgramFiles -ChildPath "WindowsPowerShell\Modules\$moduleName"))
#ps 7 installed system mods
$destinationRootPath.add((Join-Path -Path $env:ProgramFiles -ChildPath "PowerShell\Modules\$moduleName"))
#add ps5 path
if (Test-Path ($ps5Pth | Split-Path)) { #make sure parent path exists before adding it as a destination
$destinationRootPath.add($ps5Pth)
} else {
Write-Warning "Powershell 5.1 module path doesn't exist, install powershell package for best experience"
}
#add ps7 path if exists
if (Test-Path ($ps7Pth | Split-Path)) { #make sure parent path exists before adding it as a destination
$destinationRootPath.add($ps7Pth)
} else {
Write-Warning "Powershell core path doesn't exist, install powershell-core package for best experience"
}
"Installing $modulename version $moduleVersion in both pwsh 7+ and windows powershell 5.1 system paths" | out-host;
}


if ($destinationRootPath.count -eq 0) {
Write-Error "Neither powershell module path exists! Install powershell and or powershell-core and try again"
Set-PowershellExitCode -exitcode 1
exit 1
}

ForEach ($dest in $destinationRootPath) {
$destPath = "$dest\$moduleVersion"
Write-Verbose "Installing '$modulename' of version '$moduleVersion' to '$destPath'."
Expand Down Expand Up @@ -89,7 +120,7 @@ ForEach ($dest in $destinationRootPath) {
if (($pp.NoRemove)) {
"'NoRemove' was specified, not removing any other installed versions of $modulename" | out-host;
} else {
Write-Verbose "Finding and uninstalling/removing any other versions of $modulename"
Write-Verbose "Finding and uninstalling/removing any other versions (older or newer) of $modulename"
"Finding and removing any other versions of $modulename so only the latest is available at system level" | out-host;

ForEach ($destPath in $destinationRootPath) {
Expand Down Expand Up @@ -119,4 +150,5 @@ $($destinationRootPath | ForEach-Object {"`t- $($_)\$moduleVersion`n"})
- Use 'Get-Help Function-name' to get inline help, append '-online' to open the web version
- Use 'Get-Help about_fogapi' for an overview of the module
-----------------------------------------------------------------------------
"@
"@
Set-PowershellExitCode -exitcode 0
Loading