1+ # !/usr/bin/env pwsh -c
2+
3+ <#
4+ . DESCRIPTION
5+ Start the docker proxy container. If it is already running, quietly exit. Any other error should fail.
6+ . PARAMETER Mode
7+ "start" or "stop" to start up or stop the test-proxy instance.
8+ . PARAMETER TargetFolder
9+ The folder in which context the test proxy will be started. Defaults to current working directory.
10+ #>
11+ [CmdletBinding (SupportsShouldProcess = $true )]
12+ param (
13+ [ValidateSet (" start" , " stop" )]
14+ [String ]
15+ $Mode ,
16+ [String ]
17+ $TargetFolder = " ."
18+ )
19+
20+ try {
21+ docker -- version | Out-Null
22+ }
23+ catch {
24+ Write-Error " A invocation of docker --version failed. This indicates that docker is not properly installed or running."
25+ Write-Error " Please check your docker invocation and try running the script again."
26+ }
27+
28+ $SELECTED_IMAGE_TAG = " 1037115"
29+ $CONTAINER_NAME = " ambitious_azsdk_test_proxy"
30+ $LINUX_IMAGE_SOURCE = " azsdkengsys.azurecr.io/engsys/testproxy-lin:${SELECTED_IMAGE_TAG} "
31+ $WINDOWS_IMAGE_SOURCE = " azsdkengsys.azurecr.io/engsys/testproxy-win:${SELECTED_IMAGE_TAG} "
32+ $root = (Resolve-Path $TargetFolder ).Path.Replace(" `\" , " /" )
33+
34+ function Get-Proxy-Container (){
35+ return (docker container ls - a -- format " {{ json . }}" -- filter " name=$CONTAINER_NAME " `
36+ | ConvertFrom-Json `
37+ | Select-Object - First 1 )
38+ }
39+
40+
41+ $SelectedImage = $LINUX_IMAGE_SOURCE
42+ $Initial = " "
43+
44+ # most of the time, running this script on a windows machine will work just fine, as docker defaults to linux containers
45+ # however, in CI, windows images default to _windows_ containers. We cannot swap them. We can tell if we're in a CI build by
46+ # checking for the environment variable TF_BUILD.
47+ if ($IsWindows -and $env: TF_BUILD ){
48+ $SelectedImage = $WINDOWS_IMAGE_SOURCE
49+ $Initial = " C:"
50+ }
51+
52+ if ($Mode -eq " start" ){
53+ $proxyContainer = Get-Proxy - Container
54+
55+ # if we already have one, we just need to check the state
56+ if ($proxyContainer ){
57+ if ($proxyContainer.State -eq " running" )
58+ {
59+ Write-Host " Discovered an already running instance of the test-proxy!. Exiting"
60+ exit (0 )
61+ }
62+ }
63+ # else we need to create it
64+ else {
65+ Write-Host " Attempting creation of Docker host $CONTAINER_NAME "
66+ Write-Host " docker container create -v `" ${root} :${Initial} /etc/testproxy`" -p 5001:5001 -p 5000:5000 --name $CONTAINER_NAME $SelectedImage "
67+ docker container create - v " ${root} :${Initial} /etc/testproxy" - p 5001 :5001 - p 5000 :5000 -- name $CONTAINER_NAME $SelectedImage
68+ }
69+
70+ Write-Host " Attempting start of Docker host $CONTAINER_NAME "
71+ docker container start $CONTAINER_NAME
72+ }
73+
74+ if ($Mode -eq " stop" ){
75+ $proxyContainer = Get-Proxy - Container
76+
77+ if ($proxyContainer ){
78+ if ($proxyContainer.State -eq " running" ){
79+ Write-Host " Found a running instance of $CONTAINER_NAME , shutting it down."
80+ docker container stop $CONTAINER_NAME
81+ }
82+ }
83+ }
0 commit comments