...

Text file src/github.com/Azure/azure-sdk-for-go/eng/common/testproxy/docker-start-proxy.ps1

Documentation: github.com/Azure/azure-sdk-for-go/eng/common/testproxy

     1 #!/usr/bin/env pwsh -c
     2
     3<#
     4.SYNOPSIS
     5Easy start/stop of docker proxy.
     6
     7.DESCRIPTION
     8Start the docker proxy container. If it is already running, quietly exit. Any other error should fail.
     9
    10.PARAMETER Mode
    11Pass value "start" or "stop" to start up or stop the test-proxy instance.
    12
    13.PARAMETER TargetFolder
    14The folder in which context the test proxy will be started. Defaults to current working directory.
    15
    16.PARAMETER VersionOverride
    17This script defaults the proxy version to the "common" version synced across all sdk repositories. If provided,
    18this parameter's value will be used as the target tag when downloading the image from azsdk docker hosting.
    19#>
    20[CmdletBinding(SupportsShouldProcess = $true)]
    21param(
    22    [ValidateSet("start", "stop")]
    23    [String]
    24    $Mode,
    25    [String]
    26    $TargetFolder = "",
    27    [String]
    28    $VersionOverride = ""
    29)
    30
    31try {
    32    docker --version | Out-Null
    33}
    34catch {
    35    Write-Error "A invocation of docker --version failed. This indicates that docker is not properly installed or running."
    36    Write-Error "Please check your docker invocation and try running the script again."
    37}
    38
    39$SELECTED_IMAGE_TAG = $(Get-Content "$PSScriptRoot/target_version.txt" -Raw).Trim()
    40$CONTAINER_NAME = "ambitious_azsdk_test_proxy"
    41$IMAGE_SOURCE = "azsdkengsys.azurecr.io/engsys/test-proxy:${SELECTED_IMAGE_TAG}"
    42
    43if($VersionOverride) { 
    44    Write-Host "Overriding default target proxy version of '$SELECTED_IMAGE_TAG' with override $VersionOverride."
    45    $SELECTED_IMAGE_TAG = $VersionOverride
    46} 
    47
    48if (-not $TargetFolder){
    49    $TargetFolder = Join-Path -Path $PSScriptRoot -ChildPath "../../../"
    50}
    51
    52$root = (Resolve-Path $TargetFolder).Path.Replace("`\", "/")
    53
    54function Get-Proxy-Container(){
    55    return (docker container ls -a --format "{{ json . }}" --filter "name=$CONTAINER_NAME" `
    56                | ConvertFrom-Json `
    57                | Select-Object -First 1)
    58}
    59
    60$Initial = ""
    61$AdditionalContainerArgs = "--add-host=host.docker.internal:host-gateway"
    62
    63# most of the time, running this script on a windows machine will work just fine, as docker defaults to linux containers
    64# however, in CI, windows images default to _windows_ containers. We cannot swap them. We can tell if we're in a CI build by
    65# checking for the environment variable TF_BUILD.
    66if ($IsWindows -and $env:TF_BUILD){
    67    $Initial = "C:"
    68    $AdditionalContainerArgs = ""
    69}
    70
    71# there isn't really a great way to get environment variables automagically from the CI environment to the docker image
    72# handle loglevel here so that setting such a setting in CI will also bump the docker logging
    73if ($env:Logging__LogLevel__Default) {
    74    $AdditionalContainerArgs += "-e Logging__LogLevel__Default=$($env:Logging__LogLevel__Default)"
    75}
    76
    77if ($Mode -eq "start"){
    78    $proxyContainer = Get-Proxy-Container
    79
    80    # if we already have one, we just need to check the state
    81    if($proxyContainer){
    82        if ($proxyContainer.State -eq "running")
    83        {
    84            Write-Host "Discovered an already running instance of the test-proxy!. Exiting"
    85            exit(0)
    86        }
    87    }
    88    # else we need to create it
    89    else {
    90        $attempts = 0
    91        Write-Host "Attempting creation of Docker host $CONTAINER_NAME"
    92        Write-Host "docker container create -v `"${root}:${Initial}/srv/testproxy`" $AdditionalContainerArgs -p 5001:5001 -p 5000:5000 --name $CONTAINER_NAME $IMAGE_SOURCE"
    93        while($attempts -lt 3){
    94            docker container create -v "${root}:${Initial}/srv/testproxy" $AdditionalContainerArgs -p 5001:5001 -p 5000:5000 --name $CONTAINER_NAME $IMAGE_SOURCE
    95
    96            if($LASTEXITCODE -ne 0){
    97                $attempts += 1
    98                Start-Sleep -s 10
    99
   100                if($attempts -lt 3){
   101                    Write-Host "Attempt $attempts failed. Retrying."
   102                }
   103
   104                continue
   105            }
   106            else {
   107                break
   108            }
   109        }
   110
   111        if($LASTEXITCODE -ne 0){
   112            Write-Host "Unable to successfully create docker container. Exiting."
   113            exit(1)
   114        }
   115    }
   116
   117    Write-Host "Attempting start of Docker host $CONTAINER_NAME"
   118    docker container start $CONTAINER_NAME
   119}
   120
   121if ($Mode -eq "stop"){
   122    $proxyContainer = Get-Proxy-Container
   123
   124    if($proxyContainer){
   125        if($proxyContainer.State -eq "running"){
   126            Write-Host "Found a running instance of $CONTAINER_NAME, shutting it down."
   127            docker container stop $CONTAINER_NAME
   128        }
   129    }
   130}

View as plain text