Skip to content

Commit

Permalink
Example with docker and ACI for C# and F# (pulumi#482)
Browse files Browse the repository at this point in the history
* Example with docker and ACI for C# and F#

* Fix build errrors

* EOL
  • Loading branch information
mikhailshilkov committed Feb 21, 2020
1 parent b78037d commit 1950078
Show file tree
Hide file tree
Showing 19 changed files with 732 additions and 17 deletions.
6 changes: 6 additions & 0 deletions Examples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Digitalocean.LoadbalancedDr
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Digitalocean.K8s", "digitalocean-cs-k8s\Digitalocean.K8s.csproj", "{CA1DB6A6-4B5D-41D7-AED6-43C096F5C7F4}"
EndProject
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Azure.Aci", "azure-fs-aci\Azure.Aci.fsproj", "{9F646ECF-9CD3-4686-8C4C-FE6AAAD29FEC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -81,6 +83,10 @@ Global
{CA1DB6A6-4B5D-41D7-AED6-43C096F5C7F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CA1DB6A6-4B5D-41D7-AED6-43C096F5C7F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CA1DB6A6-4B5D-41D7-AED6-43C096F5C7F4}.Release|Any CPU.Build.0 = Release|Any CPU
{9F646ECF-9CD3-4686-8C4C-FE6AAAD29FEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9F646ECF-9CD3-4686-8C4C-FE6AAAD29FEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F646ECF-9CD3-4686-8C4C-FE6AAAD29FEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F646ECF-9CD3-4686-8C4C-FE6AAAD29FEC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion azure-cs-botservice/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static Task<int> Main()
Content = new FileArchive("bot/publish")
});
var codeBlobUrl = SharedAccessSignature.SignedBlobReadUrl(blob, storageAccount);
var codeBlobUrl = Storage.SharedAccessSignature.SignedBlobReadUrl(blob, storageAccount);
var appInsights = new Insights("ai", new InsightsArgs
{
Expand Down
3 changes: 2 additions & 1 deletion azure-cs-cosmosapp-component/Azure.CosmosAppComponent.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Pulumi.Azure" Version="1.8.0-preview" />
<PackageReference Include="Pulumi.Azure" Version="1.14.0-preview" />
<PackageReference Include="Pulumi.Docker" Version="1.2.0-preview" />
</ItemGroup>

<PropertyGroup>
Expand Down
105 changes: 105 additions & 0 deletions azure-cs-cosmosapp-component/Containers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.

using System.Collections.Generic;

using Pulumi;
using Pulumi.Azure.ContainerService;
using Pulumi.Azure.ContainerService.Inputs;
using Pulumi.Azure.Core;
using Pulumi.Docker;

public static class Containers
{
public static IDictionary<string, object> Run()
{
// Read a list of target locations from the config file:
// Expecting a comma-separated list, e.g., "westus,eastus,westeurope"
var locations = new Pulumi.Config().Require("locations").Split(",");

var resourceGroup = new ResourceGroup("cosmosaci-rg", new ResourceGroupArgs { Location = locations[0] });

var app = new CosmosApp("aci", new CosmosAppArgs
{
ResourceGroup = resourceGroup,
Locations = locations,
DatabaseName = "pricedb",
ContainerName = "prices",
Factory = global =>
{
var registry = new Registry("global", new RegistryArgs
{
ResourceGroupName = resourceGroup.Name,
AdminEnabled = true,
Sku = "Premium",
}, global.Options);
var dockerImage = new Image("node-app", new ImageArgs
{
ImageName = Output.Format($"{registry.LoginServer}/mynodeapp:v1.0.0"),
Build = "./container",
Registry = new ImageRegistry
{
Server = registry.LoginServer,
Username = registry.AdminUsername,
Password = registry.AdminPassword,
},
}, new ComponentResourceOptions { Parent = registry });
return region =>
{
var connectionString = global.CosmosAccount.ConnectionStrings.Apply(cs => cs[0]);
var group = new Group($"aci-{region.Location}", new GroupArgs
{
ResourceGroupName = resourceGroup.Name,
Location = region.Location,
ImageRegistryCredentials =
{
new GroupImageRegistryCredentialsArgs
{
Server = registry.LoginServer,
Username = registry.AdminUsername,
Password = registry.AdminPassword,
}
},
OsType = "Linux",
Containers =
{
new GroupContainersArgs
{
Cpu = 0.5,
Image = dockerImage.ImageName,
Memory = 1.5,
Name = "hello-world",
Ports =
{
new GroupContainersPortsArgs
{
Port = 80,
Protocol = "TCP",
}
},
EnvironmentVariables =
{
{ "ENDPOINT", global.CosmosAccount.Endpoint },
{ "MASTER_KEY", global.CosmosAccount.PrimaryMasterKey },
{ "DATABASE", global.Database.Name },
{ "COLLECTION", global.Container.Name },
{ "LOCATION", region.Location },
},
},
},
IpAddressType = "public",
DnsNameLabel = $"acishop-{region.Location}",
}, global.Options);
return new ExternalEndpoint(group.Fqdn);
};
}
});

return new Dictionary<string, object>
{
{ "containersEndpoint", Output.Format($"{app.Endpoint}/cosmos") }
};
}
}
24 changes: 9 additions & 15 deletions azure-cs-cosmosapp-component/CosmosApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,24 +137,18 @@ public CosmosApp(string name, CosmosAppArgs args, ComponentResourceOptions? opti
{
ResourceGroupName = resourceGroup.Name,
TrafficRoutingMethod = "Performance",
DnsConfigs =
DnsConfig = new TrafficManagerProfileDnsConfigArgs
{
new TrafficManagerProfileDnsConfigsArgs
{
// Subdomain must be globally unique, so we default it with the full resource group name
RelativeName = Output.Format($"{name}{resourceGroup.Name}"),
Ttl = 60,
}
// Subdomain must be globally unique, so we default it with the full resource group name
RelativeName = Output.Format($"{name}{resourceGroup.Name}"),
Ttl = 60,
},
MonitorConfigs =
MonitorConfig = new TrafficManagerProfileMonitorConfigArgs
{
new TrafficManagerProfileMonitorConfigsArgs
{
Protocol = "HTTP",
Port = 80,
Path = "/api/ping",
}
},
Protocol = "HTTP",
Port = 80,
Path = "/api/ping",
}
},
parentOptions);

Expand Down
2 changes: 2 additions & 0 deletions azure-cs-cosmosapp-component/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ static Task<int> Main()
return Deployment.RunAsync(() =>
{
var functions = Functions.Run();
var containers = Containers.Run();
var vmss = VmScaleSets.Run();
return new Dictionary<string, object?>
{
{ "functionsEndpoint", functions["functionsEndpoint"] },
{ "containersEndpoint", containers["containersEndpoint"] },
{ "vmssEndpoint", vmss["vmssEndpoint"] },
};
});
Expand Down
2 changes: 2 additions & 0 deletions azure-cs-cosmosapp-component/container/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
npm-debug.log
6 changes: 6 additions & 0 deletions azure-cs-cosmosapp-component/container/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM node:8.9.3-alpine
RUN mkdir -p /usr/src/app
COPY ./app/* /usr/src/app/
WORKDIR /usr/src/app
RUN npm install
CMD node /usr/src/app/index.js
33 changes: 33 additions & 0 deletions azure-cs-cosmosapp-component/container/app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<html>
<head>
<title>YEAH Welcome to Azure Container Instances!</title>
</head>
<style>
h1 {
color: darkblue;
font-family:arial, sans-serif;
font-weight: lighter;
}
</style>

<body>

<div align="center">
<h1>Welcome to Azure Container Instances!</h1>

<svg id="Layer_1" data-name="Layer 1" xmlns="http:https://www.w3.org/2000/svg" viewBox="0 0 49.8 49.9" width="250px" height="250px">
<title>ContainerInstances_rgb_UI</title>
<path d="M41.9,11.368A11.929,11.929,0,0,0,20.3,5.061a9.444,9.444,0,0,0-14.932,9.8A8.969,8.969,0,0,0,9.064,32H39.442A10.463,10.463,0,0,0,41.9,11.368Z" transform="translate(-0.1 -0.1)" fill="#fff"/>
<path d="M41.9,11.368A11.929,11.929,0,0,0,20.3,5.061a9.444,9.444,0,0,0-14.932,9.8A8.969,8.969,0,0,0,9.064,32H39.442A10.463,10.463,0,0,0,41.9,11.368Z" transform="translate(-0.1 -0.1)" fill="#27a9e1" opacity="0.6" style="isolation:isolate"/>
<path d="M13,22a1,1,0,0,0-1,1V49a1,1,0,0,0,1,1H37a1,1,0,0,0,1-1V23a1,1,0,0,0-1-1Z" transform="translate(-0.1 -0.1)" fill="#672a7a"/>
<path d="M26.95,16" transform="translate(-0.1 -0.1)" fill="none"/>
<path d="M34.95,20" transform="translate(-0.1 -0.1)" fill="none"/>
<polygon points="22.9 21.9 22.9 14.9 19.9 14.9 24.9 7.9 29.9 14.9 26.9 14.9 26.9 21.9 22.9 21.9" fill="#fff"/>
<path d="M26.95,16" transform="translate(-0.1 -0.1)" fill="#814a98"/>
<path d="M33,25H15V47H35V25ZM21,45H17V27h4Zm6,0H23V27h4Zm6,0H29V27h4Z" transform="translate(-0.1 -0.1)" fill="#b92025" opacity="0.3" style="isolation:isolate"/>
<path d="M33,25H15V47H35V25ZM21,45H17V27h4Zm6,0H23V27h4Zm6,0H29V27h4Z" transform="translate(-0.1 -0.1)" fill="#fff" style="isolation:isolate"/>
</svg>
</div>

</body>
</html>
37 changes: 37 additions & 0 deletions azure-cs-cosmosapp-component/container/app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const express = require('express');
const morgan = require('morgan');
const cosmos = require('@azure/cosmos');

const app = express();
app.use(morgan('combined'));


app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html')
});

app.get('/cosmos', async (req, res) => {
const endpoint = process.env.ENDPOINT;
const key = process.env.MASTER_KEY;
const database = process.env.DATABASE;
const collection = process.env.COLLECTION;
const location = process.env.LOCATION;

const client = new cosmos.CosmosClient({ endpoint, key, connectionPolicy: { preferredLocations: [location] } });
const container = client.database(database).container(collection);
const response = await container.item("test", undefined).read();

if (response.resource && response.resource.url) {
res.send(response.resource.url);
} else {
res.status(404).end();
}
});

app.get('/api/ping', (req, res) => {
res.send('Ack')
});

var listener = app.listen(process.env.PORT || 80, function() {
console.log('listening on port ' + listener.address().port);
});
13 changes: 13 additions & 0 deletions azure-cs-cosmosapp-component/container/app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "aci-helloworld",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"@azure/cosmos": "^3.1.1",
"express": "^4.14.0",
"morgan": "^1.8.2"
},
"devDependencies": {},
"author": ""
}
Loading

0 comments on commit 1950078

Please sign in to comment.