update-docker-compose.ps1 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Self-elevate the script if required
  2. # http://www.expta.com/2017/03/how-to-self-elevate-powershell-script.html
  3. If (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
  4. If ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
  5. $CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
  6. Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine
  7. Exit
  8. }
  9. }
  10. $SectionSeparator = "--------------------------------------------------"
  11. # Update docker-compose if required
  12. Function UpdateDockerCompose() {
  13. Write-Host "Updating docker-compose if required..."
  14. Write-Host $SectionSeparator
  15. # Find the installed docker-compose.exe location
  16. Try {
  17. $DockerComposePath = Get-Command docker-compose.exe -ErrorAction Stop | `
  18. Select-Object -First 1 -ExpandProperty Definition
  19. }
  20. Catch {
  21. Write-Host "Error: Could not find path to docker-compose.exe" `
  22. -ForegroundColor Red
  23. Return $false
  24. }
  25. # Prefer/enable TLS 1.2
  26. # https://stackoverflow.com/a/48030563/153079
  27. [Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
  28. # Query for the latest release version
  29. Try {
  30. $URI = "https://api.github.com/repos/docker/compose/releases/latest"
  31. $LatestComposeVersion = [System.Version](Invoke-RestMethod -Method Get -Uri $URI).tag_name
  32. }
  33. Catch {
  34. Write-Host "Error: Query for the latest docker-compose release version failed" `
  35. -ForegroundColor Red
  36. Return $false
  37. }
  38. # Check the installed version and compare with latest release
  39. $UpdateDockerCompose = $false
  40. Try {
  41. $InstalledComposeVersion = `
  42. [System.Version]((docker-compose.exe version --short) | Out-String)
  43. If ($InstalledComposeVersion -eq $LatestComposeVersion) {
  44. Write-Host ("Installed docker-compose version ({0}) same as latest ({1})." `
  45. -f $InstalledComposeVersion.ToString(), $LatestComposeVersion.ToString())
  46. }
  47. ElseIf ($InstalledComposeVersion -lt $LatestComposeVersion) {
  48. Write-Host ("Installed docker-compose version ({0}) older than latest ({1})." `
  49. -f $InstalledComposeVersion.ToString(), $LatestComposeVersion.ToString())
  50. $UpdateDockerCompose = $true
  51. }
  52. Else {
  53. Write-Host ("Installed docker-compose version ({0}) newer than latest ({1})." `
  54. -f $InstalledComposeVersion.ToString(), $LatestComposeVersion.ToString()) `
  55. -ForegroundColor Yellow
  56. }
  57. }
  58. Catch {
  59. Write-Host `
  60. "Warning: Couldn't get docker-compose version, assuming an update is required..." `
  61. -ForegroundColor Yellow
  62. $UpdateDockerCompose = $true
  63. }
  64. If (-Not $UpdateDockerCompose) {
  65. # Nothing to do!
  66. Return $false
  67. }
  68. # Download the latest version of docker-compose.exe
  69. Try {
  70. $RemoteFileName = "docker-compose-Windows-x86_64.exe"
  71. $URI = ("https://github.com/docker/compose/releases/download/{0}/{1}" `
  72. -f $LatestComposeVersion.ToString(), $RemoteFileName)
  73. Invoke-WebRequest -UseBasicParsing -Uri $URI `
  74. -OutFile $DockerComposePath
  75. Return $true
  76. }
  77. Catch {
  78. Write-Host ("Error: Failed to download the latest version of docker-compose`n{0}" `
  79. -f $_.Exception.Message) -ForegroundColor Red
  80. Return $false
  81. }
  82. Return $false
  83. }
  84. If (UpdateDockerCompose) {
  85. Write-Host "Updated to latest-version of docker-compose, running update again to verify.`n"
  86. If (UpdateDockerCompose) {
  87. Write-Host "Error: Should not have updated twice." -ForegroundColor Red
  88. }
  89. }
  90. # Assuming elevation popped up a new powershell window, pause so the user can see what happened
  91. # https://stackoverflow.com/a/22362868/153079
  92. Function Pause ($Message = "Press any key to continue . . . ") {
  93. If ((Test-Path variable:psISE) -and $psISE) {
  94. $Shell = New-Object -ComObject "WScript.Shell"
  95. $Shell.Popup("Click OK to continue.", 0, "Script Paused", 0)
  96. }
  97. Else {
  98. Write-Host "`n$SectionSeparator"
  99. Write-Host -NoNewline $Message
  100. [void][System.Console]::ReadKey($true)
  101. Write-Host
  102. }
  103. }
  104. Pause