1
0

patch-user-data.ps1 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. param(
  2. [Parameter(Mandatory = $true)]
  3. [string]$Path
  4. )
  5. $ErrorActionPreference = 'Stop'
  6. $launcher = " - [ bash, -lc, 'for d in /boot/firmware /boot; do [ -f ""`$d/waouw-activate.sh"" ] && exec bash ""`$d/waouw-activate.sh""; done; echo waouw-activate.sh introuvable >&2; exit 1' ]"
  7. $lines = Get-Content -LiteralPath $Path
  8. $out = New-Object System.Collections.Generic.List[string]
  9. $seenRuncmd = $false
  10. $added = $false
  11. $hasCloud = $false
  12. foreach ($line in $lines) {
  13. if ($line -match '^\#cloud-config') {
  14. $hasCloud = $true
  15. }
  16. if ($line -match '^\s*-\s+.*waouw-activate\.sh.*$') {
  17. continue
  18. }
  19. if ($line -match '^\s*runcmd:\s*$') {
  20. if (-not $seenRuncmd) {
  21. $out.Add('runcmd:')
  22. $out.Add($launcher)
  23. $seenRuncmd = $true
  24. $added = $true
  25. }
  26. continue
  27. }
  28. $out.Add($line)
  29. }
  30. if ((-not $seenRuncmd) -and $hasCloud) {
  31. if ($out.Count -gt 0 -and $out[$out.Count - 1] -ne '') {
  32. $out.Add('')
  33. }
  34. $out.Add('runcmd:')
  35. $out.Add($launcher)
  36. $added = $true
  37. }
  38. if (-not $added) {
  39. exit 8
  40. }
  41. [IO.File]::WriteAllLines($Path, $out, [Text.UTF8Encoding]::new($false))