This repository has been archived by the owner on Mar 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set-ValidCommitData.ps1
247 lines (204 loc) · 8.67 KB
/
Set-ValidCommitData.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
$global:workArray = @()
$global:validSessions = @()
$global:validCourses = @()
Write-Output "Debug control variable is $env:DEBUG"
Write-Output "Forecast days interval is $env:FORECAST"
Write-Output "Seats setting is: $($env:SEATS)"
Write-Output "Start date setting is: $($env:START_DATE)"
function Confirm-SessionDateWindow {
[CmdletBinding()]
param (
[Parameter()]
[DateTime]
$Date
)
if ($env:START_DATE -ne "TODAY") {
[DateTime]$start_date = $env:START_DATE
}
else {
$start_date = Get-Date
}
$forecastInterval = New-TimeSpan -Days $env:FORECAST
$forecastDate = $start_date + $forecastInterval
if ($Date -ge $start_date -and $Date -le $forecastDate) {
return $true
}
else {
return $false
}
}
function Get-ValidCourses {
[CmdletBinding()]
param (
[Parameter()]
[String]
$AuthToken
)
Write-Output "Setting up auth header for get all courses"
$headers = @{Authorization = "Bearer $AuthToken"}
$allCourses = Invoke-RestMethod -Uri 'https://training.puppet.com/course/v1/courses?page_size=5000' -Method GET -Headers $headers
Write-Output "Retrieved a total of $($allCourses.data.items.Count) courses"
foreach ($course in $allCourses.data.items) {
if (($course.code -like "*GSWP*") -or ($course.code -like "*PRAC*") -or ($course.code -like "*Workshop*")) {
Write-Output "Adding course with code $($course.code) to valid course array"
$global:validCourses+=$course
}
}
}
function Get-ValidSessions {
[CmdletBinding()]
param (
[Parameter()]
[String]
$AuthToken
)
Write-Output "Setting up auth header"
$headers = @{Authorization = "Bearer $AuthToken"}
$combined = @()
foreach ($course in $global:validCourses) {
Write-Output "Adding sessions for course $($course.name) to combined session list for pruning"
$courseSessions = Invoke-RestMethod -uri "https://training.puppet.com/course/v1/courses/$($course.id)/sessions?page_size=5000" -Headers $headers -Method Get
Write-Output "Got $($courseSessions.data.items.Count) sessions for course code $($course.code) - adding to array"
$combined+=$courseSessions.data.items
}
Write-Output "Evaluating start date for total sessions $($combined.Count)"
foreach ($session in $combined) {
if ($session.date_start) {
Write-Output "Working on Session ID $($session.id) with Start Date $($session.date_start)"
if (Confirm-SessionDateWindow([DateTime]$session.date_start)) {
$global:validSessions+=$session
}
else {
Write-Output "Session ID $($session.id) not valid for date window"
}
} else {
Write-Output "Sessions ID $($session.id) has no start date"
}
}
}
function Set-HydraCommits {
[CmdletBinding()]
param (
[Parameter()]
[System.Array]
$SessionList,
[Parameter()]
[String]
$GithubPAT,
[Parameter()]
[String]
$AuthToken
)
Write-Output "Setting up auth header for session data"
$headers = @{Authorization = "Bearer $AuthToken"}
foreach ($session in $SessionList) {
# handle workshop as static branch name for upgrade workshop
$branchID = switch -Wildcard ($($session.name)) {
'*Upgrade*' {'workshop'}
Default {"R2H-$($session.uid_session)"}
}
$branchID = $branchID.ToLower()
Write-Output "Working on session item $($session.name)"
Write-Output "Creating branch: $branchID"
Write-Output "Checking if branch previously existed in remote repo"
$branches = (git branch -r | foreach {$_ -replace ' origin/',''})
if ($branches.Contains($branchID)) {
Write-Output "$branchID exists in remote repo from a previous creation attempt; skipping"
{continue}
}
git checkout -b $branchID
Write-Output "Removing manifest.yaml if it exists"
if (Test-Path manifest.yaml) {
Remove-Item manifest.yaml
}
Write-Output "Adding manifest template"
$manifestTemplate >> manifest.yaml
git status
Write-Output "Adjusting manifest file values:"
Write-Output "Getting session data to lookup instructor name and email"
$sessionData = Invoke-RestMethod -Method Get -Uri "https://training.puppet.com/course/v1/sessions/$($session.id)" -Headers $headers
Write-Output "Instructor data: $($sessionData.data.instructors | Out-String)"
Write-Output "First instructor in array to be used: $($sessionData.data.instructors[0].firstname) $($sessionData.data.instructors[0].lastname) $($sessionData.data.instructors[0].username)"
$classType = switch -Wildcard ($($session.name)) {
'Getting Started*' {'legacyclass'}
'Puppet Practitioner*' {'legacyclass'}
'*Upgrade*' {'peupgradeworkshop'}
}
$legacyClass = switch -Wildcard ($($session.name)) {
'Getting Started*' {'puppet_class_type: GSWP'}
'Puppet Practitioner*' {'puppet_class_type: PRAC'}
'Upgrade*' {''}
}
$region = switch -Wildcard ($($session.name)) {
'*APAC*' {'ap-southeast-1'}
'*Australia*' {'ap-southeast-1'}
'*EMEA*' {'eu-central-1'}
'*US (East)*' {'us-east-1'}
'*US (West)*' {'us-west-2'}
Default {'us-west-2'}
}
Write-Output "Seats"
$adjustedSeats = $($env:SEATS)
$fullName = $($sessionData.data.instructors[0].firstname) + " " + $($sessionData.data.instructors[0].lastname)
Write-Output "File manip"
((Get-Content -path manifest.yaml -Raw) -replace '<CLASSTYPE>', $classType) | Set-Content -Path manifest.yaml
((Get-Content -path manifest.yaml -Raw) -replace '<STUDENTCOUNT>', $adjustedSeats) | Set-Content -Path manifest.yaml
((Get-Content -path manifest.yaml -Raw) -replace '<LEGACY_CLASS_ID>', $legacyClass) | Set-Content -Path manifest.yaml
((Get-Content -path manifest.yaml -Raw) -replace '<REGION>', $region) | Set-Content -Path manifest.yaml
Write-Output "Instructor name concat"
((Get-Content -path manifest.yaml -Raw) -replace '<NAME>', $fullName) | Set-Content -Path manifest.yaml
Write-Output "Email"
((Get-Content -path manifest.yaml -Raw) -replace '<EMAIL>', $($sessionData.data.instructors[0].username)) | Set-Content -Path manifest.yaml
if ($classType -eq 'peupgradeworkshop') {
((Get-Content -path manifest.yaml -Raw) -replace 'student_machine_count:', 'machine_count:') | Set-Content -Path manifest.yaml
}
Write-Output "Adjusted manifest.yaml data:"
$adjustedManifest = Get-Content manifest.yaml -Raw
Write-Output $adjustedManifest
if ($env:DEBUG -ne "noop") {
Write-Output "DEBUG control not set to noop - entering git add and commit block"
git add --all
git commit -m "Provision environment from Relay: session id: $($session.id) uid: $($session.uid_session)"
git push origin $branchID
}
$session | Add-Member -MemberType NoteProperty -Name 'HydraBranch' -Value $branchID
$global:workArray+=$session
}
}
$manifestTemplate = @"
---
stack: <CLASSTYPE>
tf_action: apply
owner: <NAME>
owner_email: <EMAIL>
region: <REGION>
days_needed: 12
department: EDU
tf_parameters:
<LEGACY_CLASS_ID>
student_machine_count: '<STUDENTCOUNT>'
"@
Write-Output "Getting all courses"
Get-ValidCourses -AuthToken $env:DoceboToken
Write-Output "Getting all valid sessions for valid courses"
Get-ValidSessions -AuthToken $env:DoceboToken
git config --global user.email "eduteam@puppetlabs.com"
git config --global user.name "puppetlabs-edu-api"
Write-Output "Setting base working directory location"
Set-Location /
Write-Output "Cloning hydra base repo"
git clone "https://puppetlabs-edu-api:$($env:GithubPAT)@github.com/puppetlabs/courseware-lms-nextgen-hydra.git"
Write-Output "Setting working directory to hydra repo"
Set-Location courseware-lms-nextgen-hydra
Set-HydraCommits -SessionList $global:validSessions -AuthToken $env:DoceboToken
$global:workArray | Format-Table -Property name, id, uid_session, HydraBranch, date_start
Write-Output "Printing table"
$outputTable = $global:workArray | Format-Table -Property name, id, uid_session, HydraBranch, date_start -AutoSize | Out-String
Write-Output $outputTable
$formatBlock = @"
Docebo Sessions sent to Hydra2:
``````
$($outputTable)
``````
"@
Relay-Interface output set -k WorkLog -v $formatBlock