Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.suo
*.suo
.vs/
13 changes: 0 additions & 13 deletions .vs/ViewEnvironment/xs/UserPrefs.xml

This file was deleted.

9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
48 changes: 40 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
61 changes: 39 additions & 22 deletions ViewEnvironment/App_Code/CurrentEnvironment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
Expand All @@ -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.";

/// <summary>
Expand All @@ -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;


}
}

/// <summary>
Expand Down Expand Up @@ -213,7 +228,9 @@ PRIMARY KEY (`id`)
};
}
else
{
Console.WriteLine("No DB found.");
}
}

public static void KillApp()
Expand Down
29 changes: 14 additions & 15 deletions ViewEnvironment/Default.aspx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand All @@ -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)
Expand All @@ -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();
Expand Down
25 changes: 0 additions & 25 deletions ViewEnvironment/Global.asax
Original file line number Diff line number Diff line change
Expand Up @@ -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.

}

</script>
31 changes: 0 additions & 31 deletions ViewEnvironment/Web.Debug.config

This file was deleted.

39 changes: 21 additions & 18 deletions ViewEnvironment/Web.config
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->

<configuration>
<connectionStrings>
<!--<add name="sqlserver" connectionString="Server=win-db01.example.com;Database=envviewer;Trusted_Connection=True;" />-->
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<customErrors mode="Off" />

<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />

<!-- Explicitly declare machinekey, so viewstate can be used by multiple instances of the app -->
<machineKey validationKey="64FDB45080526D987867BD3ED67114E41A003113C3690F7CDA4326BF94E5EACFB057CA5EAF8EFDA778F3DA06FD1017268FB88E76907FCE5BDAA5282AD7E406E8" decryptionKey="2D9F6556760F4272B1DDBEB51F76EFFEF8C8A058991DF50F6A6DF5DDC94DEF6E" validation="SHA1" decryption="AES" />

</system.web>
<system.data>
<DbProviderFactories>
<!-- Explicitly declare machinekey, so viewstate can be used by multiple instances of the app -->
<machineKey validationKey="64FDB45080526D987867BD3ED67114E41A003113C3690F7CDA4326BF94E5EACFB057CA5EAF8EFDA778F3DA06FD1017268FB88E76907FCE5BDAA5282AD7E406E8" decryptionKey="2D9F6556760F4272B1DDBEB51F76EFFEF8C8A058991DF50F6A6DF5DDC94DEF6E" validation="SHA1" decryption="AES" />
</system.web>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>

</system.data>
<system.webServer>
<defaultDocument>
<files>
<clear />
<add value="default.aspx" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
7 changes: 0 additions & 7 deletions ViewEnvironment/manifest.yml

This file was deleted.

8 changes: 8 additions & 0 deletions manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
applications:
- name: dot-net-app
random-route: true
memory: 1024MB
stack: windows
path: ./ViewEnvironment
buildpack: hwc_buildpack