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 Deployment Logic #251

Merged
merged 27 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9de16bb
Environment checks
Jan 31, 2019
1c80c82
Azure deployment glue (#118)
asyasky Dec 7, 2018
7bef63f
Update appsettings.json and launchsettings.json
Feb 20, 2019
2edd70d
Fix DatabaseConfiguration calls in Startup
Feb 20, 2019
8d73056
Rename appsettings.Staging.json & other clean ups.
Feb 21, 2019
8e81b31
clean up
Feb 24, 2019
15fcef5
More clean up.
Feb 24, 2019
9486b07
Deleted commented code
Feb 24, 2019
f1f9b2c
Revert yaml to checked in state.
Feb 25, 2019
7acbcbb
yaml
Feb 25, 2019
e6144e1
different yaml
Feb 25, 2019
61af039
Add automatic migrations to staging
Feb 25, 2019
b42022d
Make the database update location consistent.
Feb 25, 2019
ccd9f1c
Add connection string for Azure Storage
Feb 26, 2019
30d99a4
Update appsettings.json and launchsettings.json
Feb 20, 2019
464e189
Rename appsettings.Staging.json & other clean ups.
Feb 21, 2019
c3e8a14
clean up
Feb 24, 2019
ccd7680
Revert yaml to checked in state.
Feb 25, 2019
e56a4bc
Add automatic migrations to staging
Feb 25, 2019
9430c8c
Make the database update location consistent.
Feb 25, 2019
c387d1e
Add connection string for Azure Storage
Feb 26, 2019
205047a
Delete appsettings.Staging.json~HEAD
asyasky Feb 28, 2019
537d39f
Remove KeyVault and add prod settings.
Mar 7, 2019
3f66588
Merge branch 'master' into AddAzureEnvironmentCheck
Mar 7, 2019
38fc0c5
Merge branch 'AddAzureEnvironmentCheck' of https://github.com/asyasky…
Mar 7, 2019
c0cb55a
Merge branch 'master' into AddAzureEnvironmentCheck
Mar 7, 2019
c76a4f0
Code flow feedback clean up
Mar 12, 2019
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
36 changes: 36 additions & 0 deletions ServerCore/Areas/Deployment/DeploymentConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServerCore.DataModel;

namespace ServerCore.Areas.Deployment
{
public class DeploymentConfiguration
{
internal static void ConfigureDatabase(IConfiguration configuration, IServiceCollection services, IHostingEnvironment env)
{
// Use SQL Database if in Azure, otherwise, use localdb
if (env.IsStaging() && Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME") == "PuzzleServerTestDeploy")
{
services.AddDbContext<PuzzleServerContext>
(options => options.UseLazyLoadingProxies()
.UseSqlServer(configuration.GetConnectionString("PuzzleServerSQLConnectionString")));
}
else if (env.IsProduction() && Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME") == "puzzlehunt")
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we checking site name instead of just the environment for these?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because when I did only the environment I had problems.

{
services.AddDbContext<PuzzleServerContext>
(options => options.UseLazyLoadingProxies()
.UseSqlServer(configuration.GetConnectionString("PuzzleServerSQLConnectionString")));
}
else
{
services.AddDbContext<PuzzleServerContext>
(options => options.UseLazyLoadingProxies()
//.UseSqlServer(configuration.GetConnectionString("PuzzleServerContextLocal")));
.UseSqlServer(configuration.GetConnectionString("TempDeploy")));
}
}
}
}
16 changes: 11 additions & 5 deletions ServerCore/Areas/Identity/IdentityHostingStartup.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServerCore.Areas.Deployment;
using ServerCore.DataModel;

[assembly: HostingStartup(typeof(ServerCore.Areas.Identity.IdentityHostingStartup))]
Expand All @@ -14,10 +14,16 @@ public void Configure(IWebHostBuilder builder)
{
builder.ConfigureServices((context, services) =>
{
services.AddDbContext<PuzzleServerContext>(options =>
options.UseLazyLoadingProxies()
.UseSqlServer(
context.Configuration.GetConnectionString("PuzzleServerContext")));
// Set up to use Azure settings
IHostingEnvironment env = context.HostingEnvironment;
IConfigurationBuilder configBuilder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
context.Configuration = configBuilder.Build();

DeploymentConfiguration.ConfigureDatabase(context.Configuration, services, env);

services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<PuzzleServerContext>();
Expand Down
13 changes: 7 additions & 6 deletions ServerCore/FileManager.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

namespace ServerCore
{
public class FileManager
{
public static string ConnectionString { get; set; }

/// <summary>
/// Uploads a file to blob storage
/// </summary>
Expand Down Expand Up @@ -57,9 +59,8 @@ private static async Task<CloudBlobContainer> GetOrCreateEventContainerAsync(int
private static CloudStorageAccount StorageAccount
{
get
{
// todo: read the account info from configuration
return CloudStorageAccount.Parse("UseDevelopmentStorage=true");
{
return CloudStorageAccount.Parse(ConnectionString);
}
}
}
Expand Down
24 changes: 21 additions & 3 deletions ServerCore/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureKeyVault;

namespace ServerCore
{
Expand All @@ -11,8 +14,23 @@ public static void Main(string[] args)
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
WebHost.CreateDefaultBuilder(args)
//.ConfigureAppConfiguration((ctx, builder) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be cleaned up?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it'll be recreated if I pick "add KeyVault" again in the future, so should be safe to delete.

//{
// var keyVaultEndpoint = GetKeyVaultEndpoint();
// if (!string.IsNullOrEmpty(keyVaultEndpoint))
// {
// var azureServiceTokenProvider = new AzureServiceTokenProvider();
// var keyVaultClient = new KeyVaultClient(
// new KeyVaultClient.AuthenticationCallback(
// azureServiceTokenProvider.KeyVaultTokenCallback));
// builder.AddAzureKeyVault(
// keyVaultEndpoint, keyVaultClient, new DefaultKeyVaultSecretManager());
// }
//})
.UseStartup<Startup>()
.Build();

// private static string GetKeyVaultEndpoint() => "https://PuzzleServerTestKeyVault.vault.azure.net";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>MSDeploy</WebPublishMethod>
<ResourceId>/subscriptions/3ffc52f4-eed1-4312-8170-9caf82ff0949/resourcegroups/PuzzleServerTestDeploy/providers/Microsoft.Web/sites/PuzzleServerTestDeploy</ResourceId>
<ResourceGroup>PuzzleServerTestDeploy</ResourceGroup>
<PublishProvider>AzureWebSite</PublishProvider>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish>https://puzzleservertestdeploy.azurewebsites.net</SiteUrlToLaunchAfterPublish>
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<ExcludeApp_Data>False</ExcludeApp_Data>
<ProjectGuid>67b91cce-4e3d-4f0f-8a76-eafd37c5f949</ProjectGuid>
<MSDeployServiceURL>puzzleservertestdeploy.scm.azurewebsites.net:443</MSDeployServiceURL>
<DeployIisAppPath>PuzzleServerTestDeploy</DeployIisAppPath>
<RemoteSitePhysicalPath />
<SkipExtraFilesOnServer>True</SkipExtraFilesOnServer>
<MSDeployPublishMethod>WMSVC</MSDeployPublishMethod>
<EnableMSDeployBackup>True</EnableMSDeployBackup>
<UserName>$PuzzleServerTestDeploy</UserName>
<_SavePWD>True</_SavePWD>
<_DestinationType>AzureWebSite</_DestinationType>
<InstallAspNetCoreSiteExtension>False</InstallAspNetCoreSiteExtension>
<KeyVaultUri>https://PuzzleServerTestKeyVault.vault.azure.net</KeyVaultUri>
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this change with the region change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, but we're not using it anyway.

<TargetFramework>netcoreapp2.1</TargetFramework>
<SelfContained>false</SelfContained>
<_IsPortable>true</_IsPortable>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<publishData>
<publishProfile profileName="puzzlehunt - Web Deploy" publishMethod="MSDeploy" publishUrl="puzzlehunt.scm.azurewebsites.net:443" msdeploySite="puzzlehunt" userName="$puzzlehunt" userPWD="saaiNEoRlu8vKA5bzoi9w3x9yqPvDGs0bQthExxc6GPjeqxxY2r3bz2Ah8Qu" destinationAppUrl="http://puzzlehunt.azurewebsites.net" SQLServerDBConnectionString="Data Source=tcp:puzzleserverprod.database.windows.net,1433;Initial Catalog=puzzleserver;User Id=PuzzleServerAdmin@puzzleserverprod.database.windows.net;Password=PuzzlehuntPuzzledayNew!;" mySQLDBConnectionString="" hostingProviderForumLink="" controlPanelLink="http://windows.azure.com" webSystem="WebSites" targetDatabaseEngineType="sqlazuredatabase" targetServerVersion="Version100">
<databases>
<add name="defaultConnection" connectionString="Data Source=tcp:puzzleserverprod.database.windows.net,1433;Initial Catalog=puzzleserver;User Id=PuzzleServerAdmin@puzzleserverprod.database.windows.net;Password=PuzzlehuntPuzzledayNew!;" providerName="System.Data.SqlClient" type="Sql" targetDatabaseEngineType="sqlazuredatabase" targetServerVersion="Version100" />
<add name="PuzzleServerSQLConnectionString" connectionString="Server=tcp:puzzleserverprod.database.windows.net,1433;Initial Catalog=puzzleserver;Persist Security Info=False;User ID=PuzzleServerAdmin;Password=PuzzlehuntPuzzledayNew!;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient" type="Sql" targetDatabaseEngineType="sqlazuredatabase" targetServerVersion="Version100" />
</databases>
</publishProfile>
<publishProfile profileName="puzzlehunt - FTP" publishMethod="FTP" publishUrl="ftp://waws-prod-dm1-103.ftp.azurewebsites.windows.net/site/wwwroot" ftpPassiveMode="True" userName="puzzlehunt\$puzzlehunt" userPWD="saaiNEoRlu8vKA5bzoi9w3x9yqPvDGs0bQthExxc6GPjeqxxY2r3bz2Ah8Qu" destinationAppUrl="http://puzzlehunt.azurewebsites.net" SQLServerDBConnectionString="Data Source=tcp:puzzleserverprod.database.windows.net,1433;Initial Catalog=puzzleserver;User Id=PuzzleServerAdmin@puzzleserverprod.database.windows.net;Password=PuzzlehuntPuzzledayNew!;" mySQLDBConnectionString="" hostingProviderForumLink="" controlPanelLink="http://windows.azure.com" webSystem="WebSites" targetDatabaseEngineType="sqlazuredatabase" targetServerVersion="Version100">
<databases>
<add name="defaultConnection" connectionString="Data Source=tcp:puzzleserverprod.database.windows.net,1433;Initial Catalog=puzzleserver;User Id=PuzzleServerAdmin@puzzleserverprod.database.windows.net;Password=PuzzlehuntPuzzledayNew!;" providerName="System.Data.SqlClient" type="Sql" targetDatabaseEngineType="sqlazuredatabase" targetServerVersion="Version100" />
<add name="PuzzleServerSQLConnectionString" connectionString="Server=tcp:puzzleserverprod.database.windows.net,1433;Initial Catalog=puzzleserver;Persist Security Info=False;User ID=PuzzleServerAdmin;Password=PuzzlehuntPuzzledayNew!;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient" type="Sql" targetDatabaseEngineType="sqlazuredatabase" targetServerVersion="Version100" />
</databases>
</publishProfile>
</publishData>
18 changes: 9 additions & 9 deletions ServerCore/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
Expand All @@ -8,13 +8,13 @@
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},\
"ServerCore": {
"commandName": "Project",
"launchBrowser": true,
Expand All @@ -24,4 +24,4 @@
"applicationUrl": "http://localhost:44319/"
}
}
}
}
1 change: 1 addition & 0 deletions ServerCore/ServerCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.MicrosoftAccount" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.AzureKeyVault.HostingStartup" Version="2.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="2.1.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.2" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.1" />
Expand Down
41 changes: 34 additions & 7 deletions ServerCore/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
using Microsoft.AspNetCore.Authorization;
using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServerCore.Areas.Deployment;
using ServerCore.Areas.Identity.UserAuthorizationPolicy;
using ServerCore.DataModel;

namespace ServerCore
{
public class Startup
{
public Startup(IConfiguration configuration)
private IHostingEnvironment hostingEnvironment;

public Startup(IConfiguration configuration, IHostingEnvironment env)
{
Configuration = configuration;
hostingEnvironment = env;
}

public Startup(IHostingEnvironment env)
{
// Set up to use Azure settings
IConfigurationBuilder configBuilder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = configBuilder.Build();

hostingEnvironment = env;
}

public IConfiguration Configuration { get; }
Expand All @@ -39,9 +56,8 @@ public void ConfigureServices(IServiceCollection services)
options.Conventions.AuthorizeFolder("/ModelBases");
});

services.AddDbContext<PuzzleServerContext>
(options => options.UseLazyLoadingProxies()
.UseSqlServer(Configuration.GetConnectionString("PuzzleServerContext")));
DeploymentConfiguration.ConfigureDatabase(Configuration, services, hostingEnvironment);
FileManager.ConnectionString = Configuration.GetConnectionString("AzureStorageConnectionString");

services.AddAuthentication().AddMicrosoftAccount(microsoftOptions =>
{
Expand Down Expand Up @@ -87,12 +103,23 @@ public void ConfigureServices(IServiceCollection services)
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
if (env.IsDevelopment() && String.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME")))
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
PuzzleServerContext.UpdateDatabase(app);
}
else if (env.IsStaging() && Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME") == "PuzzleServerTestDeploy")
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
morganbr marked this conversation as resolved.
Show resolved Hide resolved
PuzzleServerContext.UpdateDatabase(app);
}
else if (env.IsProduction() && Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME") == "Puzzlehunt")
morganbr marked this conversation as resolved.
Show resolved Hide resolved
{
app.UseBrowserLink();
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably don't need this in production or staging

app.UseDeveloperExceptionPage();
morganbr marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
app.UseExceptionHandler("/Error");
Expand Down
12 changes: 12 additions & 0 deletions ServerCore/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:44319/",
"sslPort": 0
}
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"ConnectionStrings": {
"PuzzleServerContextLocal": "Server=(localdb)\\MSSQLLocalDB;Database=PuzzleServer;Trusted_Connection=True;MultipleActiveResultSets=true",
"AzureStorageConnectionString": "UseDevelopmentStorage=true"
}
}
15 changes: 15 additions & 0 deletions ServerCore/appsettings.Production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "https://puzzlehunt.azurewebsites.net",
"sslPort": 0
}
},
"ConnectionStrings": {
"PuzzleServerContext": "",
"AzureStorageConnectionString": "",
"PuzzleServerSQLConnectionString": ""
}
}
15 changes: 15 additions & 0 deletions ServerCore/appsettings.Staging.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "https://puzzleservertestdeploy.azurewebsites.net",
"sslPort": 0
}
},
"ConnectionStrings": {
"PuzzleServerContext": "",
"AzureStorageConnectionString": "",
"PuzzleServerSQLConnectionString": ""
}
}
4 changes: 0 additions & 4 deletions ServerCore/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": {
"PuzzleServerContext": "Server=(localdb)\\MSSQLLocalDB;Database=PuzzleServer;Trusted_Connection=True;MultipleActiveResultSets=true",
"AzureStorageConnectionString-1": "UseDevelopmentStorage=true"
}
}
1 change: 0 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ steps:
publishWebProjects: True
arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: True

- task: PublishBuildArtifacts@1