Skip to content

Commit

Permalink
update build script to use ExecProcess
Browse files Browse the repository at this point in the history
  • Loading branch information
heynickc committed Sep 6, 2017
1 parent 98433c0 commit 42497ad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
25 changes: 15 additions & 10 deletions build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,28 @@ Target "RunTests" <| fun _ ->
projects |> Seq.iter (log)
projects |> Seq.iter (runSingleProject)

let userEnvironVar name = System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.User)
let userEnvironVarOrNone name =
let var = userEnvironVar name
if String.IsNullOrEmpty var then None
else Some var

Target "StartDbContainer" <| fun _ ->
let dockerImage = getBuildParamOrDefault "dockerImage" @"microsoft/mssql-server-windows-express"
logfn "Starting SQL Express Docker container using image: %s" dockerImage
let posh = PowerShell.Create().AddScript(sprintf @"./docker_sql_express.ps1 -dockerImage %s" dockerImage)
posh.Invoke() |> Seq.iter (logfn "%O")

match posh.HadErrors with
| true -> posh.Streams.Error |> Seq.iter (logfn "\t %O")
failwith "SQL Express Docker container startup encountered an error... failing build"
| false -> ()
let result = ExecProcess(fun info ->
info.FileName <- "Powershell.exe"
info.WorkingDirectory <- __SOURCE_DIRECTORY__
info.Arguments <- sprintf "-ExecutionPolicy Bypass -File docker_sql_express.ps1 -dockerImage %s" dockerImage) (System.TimeSpan.FromMinutes 1.0)
if result <> 0 then failwith "Unable to execute docker_sql_experess.ps1"

match environVarOrNone "container_ip" with
match userEnvironVarOrNone "container_ip" with
| Some x -> logfn "SQL Express Docker container created with IP address: %s" x
| None -> failwith "SQL Express Docker container env:container_ip not set... failing build"

Target "PrepAppConfig" <| fun _ ->
let ip = environVarOrNone "container_ip"
let ip = userEnvironVarOrNone "container_ip"
match ip with
| Some ip ->
let appConfig = !! "src/Akka.Persistence.SqlServer.Tests/app.xml"
Expand All @@ -185,7 +190,7 @@ Target "PrepAppConfig" <| fun _ ->

let newConnString = new DbConnectionStringBuilder();
newConnString.ConnectionString <- connString
newConnString.Item("data source") <- ip
newConnString.Item("Data Source") <- ip

log ("New App.config connString: " + Environment.NewLine + "\t" + newConnString.ToString())

Expand All @@ -196,7 +201,7 @@ Target "PrepAppConfig" <| fun _ ->
| None -> failwith "SQL Express Docker container not started successfully $env:container_ip not found... failing build"

FinalTarget "TearDownDbContainer" <| fun _ ->
match environVarOrNone "container_name" with
match userEnvironVarOrNone "container_name" with
| Some x -> let cmd = sprintf "docker stop %s; docker rm %s" x x
logf "Killing container: %s" x
PowerShell.Create()
Expand Down
4 changes: 4 additions & 0 deletions docker_sql_express.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ IF ($(docker ps -aq -f label=deployer=akkadotnet).Length -gt 0) {
docker rm $(docker ps -aq -f label=deployer=akkadotnet)
}

$env:container_name = $null
$env:container_ip = $null
Write-Host "Starting SQL Server Express Docker container..."
$env:container_name = "akka-$(New-Guid)"
$container_id = docker run -d --name $env:container_name -l deployer=akkadotnet -p 1433:1433 -e ACCEPT_EULA=Y $dockerImage
Start-Sleep -Seconds 30
docker exec $container_id sqlcmd -q "CREATE LOGIN akkadotnet with password='akkadotnet', CHECK_POLICY=OFF; ALTER SERVER ROLE dbcreator ADD MEMBER akkadotnet;"
$env:container_ip = docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $container_id
[Environment]::SetEnvironmentVariable("container_name", $env:container_name, "User")
[Environment]::SetEnvironmentVariable("container_ip", $env:container_ip, "User")
Write-Host "SQL container started at IP addr: $($env:container_ip)"

0 comments on commit 42497ad

Please sign in to comment.