diff --git a/.gitignore b/.gitignore index 3951d10..21fa0e9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -*.suo \ No newline at end of file +*.suo +.vs/ diff --git a/.vs/ViewEnvironment/xs/UserPrefs.xml b/.vs/ViewEnvironment/xs/UserPrefs.xml deleted file mode 100644 index 6f3aa76..0000000 --- a/.vs/ViewEnvironment/xs/UserPrefs.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f82f2b6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8 + +# The following installs and configured Windows Auth for the app +#RUN powershell.exe Add-WindowsFeature Web-Windows-Auth +#RUN powershell.exe -NoProfile -Command Set-WebConfigurationProperty -filter /system.WebServer/security/authentication/AnonymousAuthentication -name enabled -value false -PSPath 'IIS:\' +#RUN powershell.exe -NoProfile -Command Set-WebConfigurationProperty -filter /system.webServer/security/authentication/windowsAuthentication -name enabled -value true -PSPath 'IIS:\' + +# The final instruction copies the site you published earlier into the container. +COPY ./ViewEnvironment/ /inetpub/wwwroot diff --git a/README.md b/README.md index 25e5ae7..0e04765 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,46 @@ -# pcf-dotnet-environment-viewer +# dotnet-environment-viewer +This application can be modified and deployed from an operating system (Linux, OSX, Windows). This project uses the web project type which does _not_ need to be compiled before deploying, it'll automatically compile on the deployed server on first access. -## Instructions -Push the app using manifest defaults, cd into the environment folder that holds the app code: +## CF Instructions +With Windows diego cells deployed and a Windows stack, push the app using manifest defaults: ``` -cd ViewEnvironment +$ cf push +``` + +## Kubernetes Instructions +These instructions assume you have Windows workers deployed to your k8s cluster. Before you can deploy the application you'll need to build a container image which unfortunately requires Docker on Windows Server 2019. -cf push +From your Windows Server with Docker installed, build and push the application to your container registry: + +``` +PS> docker build -t harbor.run.haas.pez.example.com/library/dotnet-environment-viewer:latest . +PS> docker push harbor.run.haas.pez.example.com/library/dotnet-environment-viewer:latest ``` -## Dependencies: -* [BOSH Release for Windows](https://network.pivotal.io/products/bosh-release-for-windows) on Pivotal Network -* [Hostable Web Core Buildpack](https://github.com/cloudfoundry-incubator/hwc-buildpack) +Create a `envviewer.yml` file with the following contents: +``` +apiVersion: v1 +kind: Pod +metadata: + labels: + run: envviewer + name: envviewer +spec: + containers: + - name: envviewer + image: harbor.run.haas.pez.example.com/library/dotnet-environment-viewer:latest + env: + - name: SQLSERVER_CONNECTION_STRING + value: Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword; + tolerations: + - key: "windows" + value: "2019" + effect: NoExecute + nodeSelector: + kubernetes.io/os: windows +``` +You'll need update the `SQLSERVER_CONNECTION_STRING` env var or omit the env var if you don't have a DB. Once updated schedule a pod: +``` +$ kubectl apply -f envviewer.yml +``` \ No newline at end of file diff --git a/ViewEnvironment/App_Code/CurrentEnvironment.cs b/ViewEnvironment/App_Code/CurrentEnvironment.cs index 80989f6..f6dcb39 100644 --- a/ViewEnvironment/App_Code/CurrentEnvironment.cs +++ b/ViewEnvironment/App_Code/CurrentEnvironment.cs @@ -1,4 +1,5 @@ using System; +using System.Configuration; using System.Collections.Generic; using System.Linq; using System.Web; @@ -16,6 +17,8 @@ public class CurrentEnvironment private static readonly string INSTANCE_GUID_ENV_VARIABLE_NAME = "INSTANCE_GUID"; private static readonly string INSTANCE_INDEX_ENV_VARIABLE_NAME = "INSTANCE_INDEX"; private static readonly string BOUND_SERVICES_ENV_VARIABLE_NAME = "VCAP_SERVICES"; + private static readonly string SQLSERVER_CONNECTION_STRING_ENV_VARIABLE_NAME = "SQLSERVER_CONNECTION_STRING"; + private static readonly string MYSQL_CONNECTION_STRING_ENV_VARIABLE_NAME = "MYSQL_CONNECTION_STRING"; private static readonly string NOT_ON_CLOUD_FOUNDRY_MESSAGE = "Not running in cloud foundry."; /// @@ -32,38 +35,50 @@ static CurrentEnvironment() Environment.SetEnvironmentVariable(INSTANCE_INDEX_ENV_VARIABLE_NAME, NOT_ON_CLOUD_FOUNDRY_MESSAGE); } - // check to see if DB is bound, if so...what type - // SQL server first - if (BoundServices.GetValue("azure-sqldb") != null) // Azure SQL Database (Azure Broker) + // check to see if DB is bound via VCAP_SERVICES (CF) + // if not, check for environment variables (k8s) + // if also not set, check the web.config (local dev) + // finally default to no DB + if (BoundServices.GetValue("mssql-dev") != null) // sql server { DbEngine = DatabaseEngine.SqlServer; - SqlConnectionStringBuilder csbuilder = new SqlConnectionStringBuilder(); - csbuilder.Add("server", BoundServices["azure-sqldb"][0]["credentials"]["hostname"].ToString()); - csbuilder.Add("uid", BoundServices["azure-sqldb"][0]["credentials"]["username"].ToString()); - csbuilder.Add("pwd", BoundServices["azure-sqldb"][0]["credentials"]["password"].ToString()); - csbuilder.Add("database", BoundServices["azure-sqldb"][0]["credentials"]["name"].ToString()); - _connectionString = csbuilder.ToString(); + _connectionString = BoundServices["mssql-dev"][0]["credentials"]["connectionString"].ToString(); } - else if(BoundServices.GetValue("azure-mysqldb") != null || BoundServices.GetValue("p.mysql") != null) // MySQL for PCF or Mysql for AZURE + else if (BoundServices.GetValue("p-mysql") != null) { - string label = "p.mysql"; // MySQL Database. - - if (BoundServices.GetValue("azure-mysqldb") != null) - label = "azure-mysqldb"; //Mysql Database on Azure (Mysql For Azure) - DbEngine = DatabaseEngine.MySql; MySqlConnectionStringBuilder csbuilder = new MySqlConnectionStringBuilder(); - csbuilder.Add("server", BoundServices[label][0]["credentials"]["hostname"].ToString()); - csbuilder.Add("port", BoundServices[label][0]["credentials"]["port"].ToString()); - csbuilder.Add("uid", BoundServices[label][0]["credentials"]["username"].ToString()); - csbuilder.Add("pwd", BoundServices[label][0]["credentials"]["password"].ToString()); - csbuilder.Add("database", BoundServices[label][0]["credentials"]["name"].ToString()); + csbuilder.Add("server", BoundServices["p-mysql"][0]["credentials"]["hostname"].ToString()); + csbuilder.Add("port", BoundServices["p-mysql"][0]["credentials"]["port"].ToString()); + csbuilder.Add("uid", BoundServices["p-mysql"][0]["credentials"]["username"].ToString()); + csbuilder.Add("pwd", BoundServices["p-mysql"][0]["credentials"]["password"].ToString()); + csbuilder.Add("database", BoundServices["p-mysql"][0]["credentials"]["name"].ToString()); _connectionString = csbuilder.ToString(); } + else if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(SQLSERVER_CONNECTION_STRING_ENV_VARIABLE_NAME))) + { + DbEngine = DatabaseEngine.SqlServer; + _connectionString = Environment.GetEnvironmentVariable(SQLSERVER_CONNECTION_STRING_ENV_VARIABLE_NAME); + } + else if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(MYSQL_CONNECTION_STRING_ENV_VARIABLE_NAME))) + { + DbEngine = DatabaseEngine.MySql; + _connectionString = Environment.GetEnvironmentVariable(MYSQL_CONNECTION_STRING_ENV_VARIABLE_NAME); + } + else if (ConfigurationManager.ConnectionStrings["sqlserver"] != null) + { + DbEngine = DatabaseEngine.SqlServer; + _connectionString = ConfigurationManager.ConnectionStrings["sqlserver"].ConnectionString; + } + else if (ConfigurationManager.ConnectionStrings["mysql"] != null) + { + DbEngine = DatabaseEngine.MySql; + _connectionString = ConfigurationManager.ConnectionStrings["mysql"].ConnectionString; + } else + { DbEngine = DatabaseEngine.None; - - + } } /// @@ -213,7 +228,9 @@ PRIMARY KEY (`id`) }; } else + { Console.WriteLine("No DB found."); + } } public static void KillApp() diff --git a/ViewEnvironment/Default.aspx.cs b/ViewEnvironment/Default.aspx.cs index 192c128..471e2c8 100644 --- a/ViewEnvironment/Default.aspx.cs +++ b/ViewEnvironment/Default.aspx.cs @@ -4,7 +4,6 @@ using System.Web; using System.Web.UI; using System.Web.UI.WebControls; - using System.Collections.Generic; public partial class _Default : System.Web.UI.Page @@ -48,9 +47,7 @@ protected void Page_Load(object sender, EventArgs e) AttendeeDataSource.DeleteCommand = "delete from attendee where id=@id"; AttendeeDataSource.DeleteParameters.Add("id", System.Data.DbType.Int64, "0"); - - - // SQL Server + // SQL Server if (CurrentEnvironment.DbEngine == CurrentEnvironment.DatabaseEngine.SqlServer) { // Create @@ -90,7 +87,7 @@ protected void Page_Load(object sender, EventArgs e) // Create AttendeeDataSource.InsertCommand = @"INSERT INTO `attendee` - (`address` + (`address` ,`city` ,`email_address` ,`first_name` @@ -109,16 +106,16 @@ protected void Page_Load(object sender, EventArgs e) ,'12345')"; AttendeeDataSource.UpdateCommand = @"UPDATE `attendee` - SET - `address` = @address, - `city` = @city, - `email_address` = @email_address, - `first_name` = @first_name, - `last_name` = @last_name, - `phone_number` = @phone_number, - `state` = @state, - `zip_code` = @zip_code - WHERE `id` = @id;"; + SET + `address` = @address, + `city` = @city, + `email_address` = @email_address, + `first_name` = @first_name, + `last_name` = @last_name, + `phone_number` = @phone_number, + `state` = @state, + `zip_code` = @zip_code + WHERE `id` = @id;"; } if (!IsPostBack) @@ -132,10 +129,12 @@ protected void Page_Load(object sender, EventArgs e) } } } + protected void btnKill_Click(object sender, EventArgs e) { CurrentEnvironment.KillApp(); } + protected void btnAddAttendee_Click(object sender, EventArgs e) { AttendeeDataSource.Insert(); diff --git a/ViewEnvironment/Global.asax b/ViewEnvironment/Global.asax index 58dca28..17c151e 100644 --- a/ViewEnvironment/Global.asax +++ b/ViewEnvironment/Global.asax @@ -8,37 +8,12 @@ if (CurrentEnvironment.hasDbConnection) { CurrentEnvironment.CheckDBstructure(); - } } - - void Application_End(object sender, EventArgs e) - { - // Code that runs on application shutdown - } - void Application_Error(object sender, EventArgs e) { Exception lastError = Server.GetLastError(); Console.WriteLine("Unhandled exception: " + lastError.Message + lastError.StackTrace); - CurrentEnvironment.KillApp(); - - } - - void Session_Start(object sender, EventArgs e) - { - // Code that runs when a new session is started - - } - - void Session_End(object sender, EventArgs e) - { - // Code that runs when a session ends. - // Note: The Session_End event is raised only when the sessionstate mode - // is set to InProc in the Web.config file. If session mode is set to StateServer - // or SQLServer, the event is not raised. - } - diff --git a/ViewEnvironment/Web.Debug.config b/ViewEnvironment/Web.Debug.config deleted file mode 100644 index a657981..0000000 --- a/ViewEnvironment/Web.Debug.config +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/ViewEnvironment/Web.config b/ViewEnvironment/Web.config index b2b3e11..4ceb3d0 100644 --- a/ViewEnvironment/Web.config +++ b/ViewEnvironment/Web.config @@ -1,25 +1,28 @@  - - - + + + + + + + - - - - - - - - - - + + + + + - - + + + + + + + + + diff --git a/ViewEnvironment/manifest.yml b/ViewEnvironment/manifest.yml deleted file mode 100644 index e0419ea..0000000 --- a/ViewEnvironment/manifest.yml +++ /dev/null @@ -1,7 +0,0 @@ ---- -applications: -- name: dot-net-app-16 - random-route: true - memory: 1026m - stack: windows2016 - buildpack: hwc_buildpack diff --git a/manifest.yml b/manifest.yml new file mode 100644 index 0000000..450cd43 --- /dev/null +++ b/manifest.yml @@ -0,0 +1,8 @@ +--- +applications: +- name: dot-net-app + random-route: true + memory: 1024MB + stack: windows + path: ./ViewEnvironment + buildpack: hwc_buildpack