Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure App Service PAAS deployment #65

Merged
merged 22 commits into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
44b764c
Update Paket.Restore.targets
theprash Apr 25, 2018
217a58b
Move all static content to "public" dir
theprash Apr 26, 2018
2fc4174
#51 Azure deployment
theprash Apr 26, 2018
84c5801
Use dotnetCli variable
theprash Apr 26, 2018
032c0e5
Make ARM deployment location a parameter.
isaacabraham Apr 27, 2018
ea20bbb
Merge branch 'master' into azure-deploy
isaacabraham Apr 27, 2018
8151c63
"Deploy" parameter with docker and azure options
theprash Apr 27, 2018
90c6981
Comment template conditionals for better tooling support
theprash Apr 27, 2018
8f68176
Add Azure pricingTier parameter to build script
theprash Apr 27, 2018
88dd727
Fix deploy parameter conditions
theprash Apr 27, 2018
4562ff9
Temporarily turn on DefaultFiles() (pending Saturn PR).
isaacabraham Apr 27, 2018
5ee9c39
Merge branch 'fix-static-content' into azure-deploy
isaacabraham Apr 27, 2018
9ea022d
Merge branch 'azure-deploy' of https://github.com/theprash/SAFE-templ…
isaacabraham Apr 27, 2018
18d195c
Only use public_path env variable with Azure option
theprash Apr 27, 2018
7731284
Microsoft.NET.Sdk.Web project type for Azure option only
theprash Apr 27, 2018
14f7dfa
Remove unnecessary router in Saturn.
isaacabraham Apr 27, 2018
204e23f
Small cleanup of Azure elements in build script (and consistency with…
isaacabraham Apr 28, 2018
2c3f27a
Work on Suave support.
isaacabraham Apr 28, 2018
5917fb8
Final change.
isaacabraham Apr 28, 2018
13d6c2b
Fix web.config getting deployed incorrectly.
isaacabraham Apr 28, 2018
7a9cede
Work on adding Trace Listeners to Suave on Azure.
isaacabraham Apr 29, 2018
eb1912c
Start Trace Listener. Still need to figure out how to get Suave to wr…
isaacabraham Apr 29, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 42 additions & 10 deletions Content/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,30 @@
"defaultValue": "false",
"description": "adds Fable.Remoting to server and client"
},
"Docker": {
"type": "parameter",
"dataType": "bool",
"defaultValue": "false",
"description": "additional FAKE targets to bundle and build Docker image"
},
"NPM": {
"type": "parameter",
"dataType": "bool",
"defaultValue": "false",
"description": "use NPM instead of default Yarn for JS package management"
},
"Deploy": {
"type": "parameter",
"dataType": "choice",
"defaultValue": "none",
"choices": [
{
"choice": "none",
"description": "don't add FAKE targets to deploy"
},
{
"choice": "azure",
"description": "additional FAKE targets to deploy to Azure"
},
{
"choice": "docker",
"description": "additional FAKE targets to bundle and build Docker image"
}
]
}
},
"sources": [
Expand All @@ -94,6 +107,20 @@
{
"rename": { "ServerSuave.fs": "Server.fs" }
},
{
"exclude": "**/Suaveweb.config",
"condition": "(Server != \"suave\") || (Deploy != \"azure\")"
},
{
"rename": { "Suaveweb.config": "web.config" }
},
{
"exclude": "**/SuaveAzure.fs",
"condition": "(Server != \"suave\") || (Deploy != \"azure\")"
},
{
"rename": { "SuaveAzure.fs": "Azure.fs" }
},
{
"exclude": "**/ServerGiraffe.fs",
"condition": "(Server != \"giraffe\")"
Expand Down Expand Up @@ -125,11 +152,15 @@
"condition": "(Fulma != \"login\")"
},
{
"exclude": "**/Dockerfile",
"condition": "(!Docker)"
"exclude": "Dockerfile",
"condition": "(Deploy != \"docker\")"
},
{
"exclude": "**/yarn.lock",
"exclude": "arm-template.json",
"condition": "(Deploy != \"azure\")"
},
{
"exclude": "yarn.lock",
"condition": "(NPM)"
}
]
Expand All @@ -150,7 +181,8 @@
}
}
]
},"**/*.fsx": {
},
"**/*.fs*": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this change intentional?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see you also now use //#if instead of #if in .fsx script - is that this change that allowed that? Maybe we can get rid of this entry completely then?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@theprash one for you.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was required to enable templating in the FSPROJ file. I believe the entry is still required for FSX too. The commenting of #if was not related to this change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, makes sense. So you commented #if in fsx for better editor support? I think I like it this way now

"operations": [
{
"type": "conditional",
Expand Down
52 changes: 52 additions & 0 deletions Content/arm-template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"environment": { "type": "string" },
"location": { "type": "string" },
"pricingTier": { "type": "string" }
},
"variables": {
"appServicePlan": "[concat('safe-', parameters('environment'), '-web-host')]",
"web": "[concat('safe-', parameters('environment'), '-web')]"
},
"resources": [
{
"type": "Microsoft.Web/serverfarms",
"sku": { "name": "[parameters('pricingTier')]" },
"name": "[variables('appServicePlan')]",
"apiVersion": "2016-09-01",
"location": "[parameters('location')]"
},
{
"type": "Microsoft.Web/sites",
"name": "[variables('web')]",
"apiVersion": "2016-08-01",
"location": "[parameters('location')]",
"properties": {
"serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlan'))]"
},
"dependsOn": [ "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlan'))]" ],
"resources": [
{
"name": "appsettings",
"apiVersion": "2015-08-01",
"type": "config",
"properties": { "public_path": "./public" },
"tags": { "displayName": "WebAppSettings" },
"dependsOn": [ "[concat('Microsoft.Web/sites/', variables('web'))]" ]
}
]
}
],
"outputs": {
"webAppName": {
"type": "string",
"value": "[variables('web')]"
},
"webAppPassword": {
"type": "string",
"value": "[list(resourceId('Microsoft.Web/sites/config', variables('web'), 'publishingcredentials'), '2014-06-01').properties.publishingPassword]"
}
}
}
108 changes: 88 additions & 20 deletions Content/build.fsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
#r @"packages/build/FAKE/tools/FakeLib.dll"

//#if (Deploy == "azure")
#r "netstandard"
#I "packages/build/Microsoft.Rest.ClientRuntime.Azure/lib/net452"
#load ".paket/load/netcoreapp2.1/Build/build.group.fsx"
#load @"paket-files\build\CompositionalIT\fshelpers\src\FsHelpers\ArmHelper\ArmHelper.fs"

open Cit.Helpers.Arm
open Cit.Helpers.Arm.Parameters
open Microsoft.Azure.Management.ResourceManager.Fluent.Core
//#endif
open System

open Fake

let serverPath = "./src/Server" |> FullName
Expand All @@ -10,16 +18,14 @@ let deployDir = "./deploy" |> FullName

let platformTool tool winTool =
let tool = if isUnix then tool else winTool
tool
|> ProcessHelper.tryFindFileOnPath
|> function Some t -> t | _ -> failwithf "%s not found" tool
match tryFindFileOnPath tool with Some t -> t | _ -> failwithf "%s not found" tool

let nodeTool = platformTool "node" "node.exe"
#if (NPM)
let npmTool = platformTool "npm" "npm.cmd"
#else
//#if (NPM)
let npmTool = platformTool "npm" "npm.cmd"
//#else
let yarnTool = platformTool "yarn" "yarn.cmd"
#endif
//#endif

let dotnetcliVersion = DotNetCli.GetDotNetSDKVersionFromGlobalJson()
let mutable dotnetCli = "dotnet"
Expand All @@ -32,7 +38,7 @@ let run cmd args workingDir =
info.Arguments <- args) TimeSpan.MaxValue
if result <> 0 then failwithf "'%s %s' failed" cmd args

Target "Clean" (fun _ ->
Target "Clean" (fun _ ->
CleanDirs [deployDir]
)

Expand All @@ -43,19 +49,19 @@ Target "InstallDotNetCore" (fun _ ->
Target "InstallClient" (fun _ ->
printfn "Node version:"
run nodeTool "--version" __SOURCE_DIRECTORY__
#if (NPM)
//#if (NPM)
printfn "Npm version:"
run npmTool "--version" __SOURCE_DIRECTORY__
run npmTool "install" __SOURCE_DIRECTORY__
#else
//#else
printfn "Yarn version:"
run yarnTool "--version" __SOURCE_DIRECTORY__
run yarnTool "install --frozen-lockfile" __SOURCE_DIRECTORY__
#endif
//#endif
run dotnetCli "restore" clientPath
)

Target "RestoreServer" (fun () ->
Target "RestoreServer" (fun () ->
run dotnetCli "restore" serverPath
)

Expand All @@ -76,13 +82,13 @@ Target "Run" (fun () ->
Diagnostics.Process.Start "http://localhost:8080" |> ignore
}

[ server; client; browser]
[ server; client; browser ]
|> Async.Parallel
|> Async.RunSynchronously
|> ignore
)

#if (Docker)
//#if (Deploy == "docker")
Target "Bundle" (fun _ ->
let serverDir = deployDir </> "Server"
let clientDir = deployDir </> "Client"
Expand All @@ -96,7 +102,6 @@ Target "Bundle" (fun _ ->

let dockerUser = "safe-template"
let dockerImageName = "safe-template"

let dockerFullName = sprintf "%s/%s" dockerUser dockerImageName

Target "Docker" (fun _ ->
Expand All @@ -106,16 +111,79 @@ Target "Docker" (fun _ ->
let tagArgs = sprintf "tag %s %s" dockerFullName dockerFullName
run "docker" tagArgs "."
)
#endif

//#endif
//#if (Deploy == "azure")
Target "Bundle" (fun () ->
run dotnetCli (sprintf "publish %s -c release -o %s" serverPath deployDir) __SOURCE_DIRECTORY__
CopyDir (deployDir </> "public") (clientPath </> "public") allFiles
)

type ArmOutput =
{ WebAppName : ParameterValue<string>
WebAppPassword : ParameterValue<string> }
let mutable deploymentOutputs : ArmOutput option = None

Target "ArmTemplate" (fun _ ->
let environment = getBuildParamOrDefault "environment" (Guid.NewGuid().ToString().ToLower().Split '-' |> Array.head)
let armTemplate = @"arm-template.json"
let resourceGroupName = "safe-" + environment

let authCtx =
let subscriptionId = try getBuildParam "subscriptionId" |> Guid.Parse with _ -> failwith "Invalid Subscription ID. This should be your Azure Subscription ID."
tracefn "Deploying template '%s' to resource group '%s' in subscription '%O'..." armTemplate resourceGroupName subscriptionId
let clientId = try getBuildParam "clientId" |> Guid.Parse with _ -> failwith "Invalid Client ID. This should be the Client ID of a Native application registered in Azure with permission to create resources in your subscription."
subscriptionId
|> authenticateDevice trace { ClientId = clientId; TenantId = None }
|> Async.RunSynchronously

let deployment =
let location = getBuildParamOrDefault "location" Region.EuropeWest.Name
let pricingTier = getBuildParamOrDefault "pricingTier" "F1"
{ DeploymentName = "SAFE-template-deploy"
ResourceGroup = New(resourceGroupName, Region.Create location)
ArmTemplate = IO.File.ReadAllText armTemplate
Parameters =
Simple
[ "environment", ArmString environment
"location", ArmString location
"pricingTier", ArmString pricingTier ]
DeploymentMode = Incremental }

deployment
|> deployWithProgress authCtx
|> Seq.iter(function
| DeploymentInProgress (state, operations) -> tracefn "State is %s, completed %d operations." state operations
| DeploymentError (statusCode, message) -> traceError <| sprintf "DEPLOYMENT ERROR: %s - '%s'" statusCode message
| DeploymentCompleted d -> deploymentOutputs <- d)
)

Target "AppService" (fun _ ->
let zipFile = "deploy.zip"
IO.File.Delete zipFile
Zip deployDir zipFile !!(deployDir + @"\**\**")

let appName = deploymentOutputs.Value.WebAppName.value
let appPassword = deploymentOutputs.Value.WebAppPassword.value

let destinationUri = sprintf "https://%s.scm.azurewebsites.net/api/zipdeploy" appName
let client = new Net.WebClient(Credentials = Net.NetworkCredential("$" + appName, appPassword))
tracefn "Uploading %s to %s" zipFile destinationUri
client.UploadData(destinationUri, IO.File.ReadAllBytes zipFile) |> ignore)

//#endif
"Clean"
==> "InstallDotNetCore"
==> "InstallClient"
==> "Build"
#if (Docker)
//#if (Deploy == "docker")
==> "Bundle"
==> "Docker"
#endif
//#elseif (Deploy == "azure")
==> "Bundle"
==> "ArmTemplate"
==> "AppService"
//#endif

"InstallClient"
==> "RestoreServer"
Expand Down
18 changes: 10 additions & 8 deletions Content/paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ group Server
source https://api.nuget.org/v3/index.json

nuget FSharp.Core
//#if (Server == "suave")
//#if (Server == "suave")
nuget Suave
//#elseif (Server == "giraffe")
nuget Giraffe ~> 1
Expand All @@ -12,14 +12,11 @@ group Server
//#elseif (Server == "saturn")
nuget Saturn
//#endif
//#if (Server != "suave" && !Remoting)
//#if (!Remoting && Server != "suave")
nuget Fable.JsonConverter
//#endif
//#if (Remoting && Server == "suave")
//#elseif (Remoting && Server == "suave")
nuget Fable.Remoting.Suave ~> 2.6
//#elseif (Remoting && Server == "giraffe")
nuget Fable.Remoting.Giraffe ~> 2.6
//#elseif (Remoting && Server == "saturn")
//#elseif (Remoting && Server != "suave")
nuget Fable.Remoting.Giraffe ~> 2.6
//#endif

Expand All @@ -44,5 +41,10 @@ group Client

group Build
source https://api.nuget.org/v3/index.json
generate_load_scripts: true

nuget FAKE
nuget FAKE
//#if (Deploy == "azure")
nuget Microsoft.Azure.Management.ResourceManager.Fluent 1.4.1
github CompositionalIT/fshelpers src/FsHelpers/ArmHelper/ArmHelper.fs
//#endif
12 changes: 12 additions & 0 deletions Content/src/Server/Server.fsproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
#if (Deploy == "azure" && Server != "suave")
<Project Sdk="Microsoft.NET.Sdk.Web">
#else
<Project Sdk="Microsoft.NET.Sdk">
#endif
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Shared\Shared.fs" />
#if (Deploy == "azure" && Server == "suave")
<Compile Include="Azure.fs" />
#endif
<Compile Include="Server.fs" />
#if (Deploy == "azure" && Server == "suave")
<None Include="web.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
#endif
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
Loading