-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.ps1
119 lines (103 loc) · 5.03 KB
/
manage.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<#
.SYNOPSIS
Management script based on PowerShell, this script must be executed from the root directory.
#>
# [Initializations] ####################################################################################################
param (
# Command to execute, one of:
# 'load': Host/Container command, loads the Powershell modules in the terminal for more granular access.
# 'clean': Host/Container command, recursively cleans the repository and submodules with Git.
# 'build': Host command, builds the images and the development containers.
# 'run': Host command creates or uses an existing development container and starts it in the current folder.
[Parameter(Mandatory = $true)]
[ValidateSet("load", "clean", "build", "run")]
[String]
$Command
)
# Stop on first error found.
$ErrorActionPreference = "Stop";
# Imports.
Import-Module "$PSScriptRoot/modules/commons.psm1";
Import-Module "$PSScriptRoot/modules/devcontainers.psm1"
# [Declarations] #######################################################################################################
# Path to 'devcontainer.json' file.
$DEVCONTAINER_FILE = ".devcontainer/devcontainer.json";
# Project name for the Docker compose project.
$DEVCONTAINER_PROJECT_NAME = "powershell_scripts";
# [Internal Functions] #################################################################################################
# [Functions] ##########################################################################################################
# [Execution] ##########################################################################################################
# Ensure the current location is the location of the script.
if (((Get-Item "$PSScriptRoot").Hashcode) -ne ((Get-Item "$PWD").Hashcode))
{
throw "The script must run from the root directory, where this script is located."
}
if ($Command -eq "load")
{
# The modules have already been imported due to the imports above, perform no other action.
Write-Log "Modules imported." "Success";
}
elseif ($Command -eq "clean")
{
Write-Log "Cleaning repository and submodules...";
git clean -d -fx -f;
git submodule foreach --recursive git clean -d -fx -f;
Write-Log "Repository and submodules cleaned." "Success";
}
elseif ($Command -eq "build")
{
# Build inputs for the development container.
$inputs = @{};
# Build outputs for the development container.
$outputs = @{};
Initialize-DevContainer -DevcontainerFile "$DEVCONTAINER_FILE" -ProjectName "$DEVCONTAINER_PROJECT_NAME" `
-Inputs $inputs -Outputs $outputs;
}
elseif ($Command -eq "run")
{
# Artifacts to copy in the workspace for the initialization script.
$inputs = @{
# This is the host path to Git authentication key.
#
# This can't be provided as a secret in the Compose file as it needs specific permissions for it to be
# trusted by the SSH utilities, and Compose does not do that, thus we copy it here and set the permissions
# explicitly from the initialization script.
"Git Authentication Key" = @{
"hostPath" = "../!local/other-files/github/dmg0345-authentication-key/private-authentication-key.pem";
};
# This is the host path to Git signing key to sign commits.
#
# This can't be provided as a secret in the Compose file as it needs specific permissions for it to be
# trusted by the SSH utilities, and Compose does not do that, thus we copy it here and set the permissions
# explicitly from the initialization script.
"Git Signing Key" = @{
"hostPath" = "../!local/other-files/github/dmg0345-signing-key/private-signing-key.pem";
};
};
# Create initialization script for the volume of the development container.
$initScript = @'
# Configure Git for user.
git config --global user.name "$ENV:GITHUB_USERNAME";
git config --global user.email "$ENV:GITHUB_EMAIL";
git config --global user.signingkey "/vol_store/private-signing-key.pem";
git config --global core.sshCommand "ssh -i '/vol_store/private-authentication-key.pem' -o 'IdentitiesOnly yes'";
# Own and set permissions of the keys to read/write for SSH to use them.
Get-ChildItem -Path "/vol_store" -Include "*.pem" -Recurse | ForEach-Object -Process `
{
chown $(id -u -n) "$($_.FullName)";
chmod 600 "$($_.FullName)";
}
# Trust Github for SSH connections.
if (-not (Test-Path "~/.ssh/known_hosts")) { New-Item -Path "~/.ssh/known_hosts" -ItemType File -Force | Out-Null; }
Set-Content -Path "~/.ssh/known_hosts" -Value "$(ssh-keyscan github.com)" -Force;
# Clone repository in the workspace folder.
git clone --recurse-submodules --branch develop "[email protected]:dmg0345/powershell.git" ".";
if ($LASTEXITCODE -ne 0) { throw "Failed to clone repository." }
'@;
Start-DevContainer -DevcontainerFile "$DEVCONTAINER_FILE" -ProjectName "$DEVCONTAINER_PROJECT_NAME" `
-VolumeInitScript $initScript -Inputs $inputs;
}
else
{
throw "Command '$Command' is not recognized or an invalid combination of arguments was provided.";
}