-
Notifications
You must be signed in to change notification settings - Fork 880
/
main.go
146 lines (133 loc) · 4.55 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2016-2020, Pulumi Corporation. All rights reserved.
package main
import (
"fmt"
"github.com/pulumi/pulumi-azure-native-sdk/containerregistry/v2"
"github.com/pulumi/pulumi-azure-native-sdk/resources/v2"
"github.com/pulumi/pulumi-azure-native-sdk/web/v2"
"github.com/pulumi/pulumi-docker/sdk/v3/go/docker"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
resourceGroup, err := resources.NewResourceGroup(ctx, "appservice-docker-rg", nil)
if err != nil {
return err
}
plan, err := web.NewAppServicePlan(ctx, "plan", &web.AppServicePlanArgs{
ResourceGroupName: resourceGroup.Name,
Kind: pulumi.String("Linux"),
Reserved: pulumi.Bool(true),
Sku: &web.SkuDescriptionArgs{
Name: pulumi.String("B1"),
Tier: pulumi.String("Basic"),
},
})
if err != nil {
return err
}
//
// Scenario 1: deploying an image from Docker Hub.
// The example uses an nginx base image
// Image: https://hub.docker.com/_/nginx
//
imageInDockerHub := "nginx"
helloApp, err := web.NewWebApp(ctx, "helloApp", &web.WebAppArgs{
ResourceGroupName: resourceGroup.Name,
ServerFarmId: plan.ID(),
SiteConfig: &web.SiteConfigArgs{
AppSettings: web.NameValuePairArray{
&web.NameValuePairArgs{
Name: pulumi.String("WEBSITES_ENABLE_APP_SERVICE_STORAGE"),
Value: pulumi.String("false"),
},
},
AlwaysOn: pulumi.Bool(true),
LinuxFxVersion: pulumi.String(fmt.Sprintf("%v%v", "DOCKER|", imageInDockerHub)),
},
HttpsOnly: pulumi.Bool(true),
})
if err != nil {
return err
}
ctx.Export("helloEndpoint", helloApp.DefaultHostName.ApplyT(func(defaultHostName string) (string, error) {
return fmt.Sprintf("%v%v", "https://", defaultHostName), nil
}).(pulumi.StringOutput))
//
// Scenario 2: deploying a custom image from Azure Container Registry.
//
customImage := "node-app"
registry, err := containerregistry.NewRegistry(ctx, "registry", &containerregistry.RegistryArgs{
ResourceGroupName: resourceGroup.Name,
Sku: &containerregistry.SkuArgs{
Name: pulumi.String("Basic"),
},
AdminUserEnabled: pulumi.Bool(true),
})
if err != nil {
return err
}
credentials := containerregistry.ListRegistryCredentialsOutput(ctx, containerregistry.ListRegistryCredentialsOutputArgs{
RegistryName: registry.Name,
ResourceGroupName: resourceGroup.Name,
})
adminUsername := credentials.Username().Elem()
adminPassword := credentials.Passwords().Index(pulumi.Int(0)).Value().Elem()
myImage, err := docker.NewImage(ctx, customImage, &docker.ImageArgs{
ImageName: registry.LoginServer.ApplyT(func(result string) (string, error) {
return fmt.Sprintf("%s/%s:v1.0.0", result, customImage), nil
}).(pulumi.StringOutput),
Build: &docker.DockerBuildArgs{Context: pulumi.String(fmt.Sprintf("./%s", customImage))},
Registry: &docker.ImageRegistryArgs{
Server: registry.LoginServer,
Username: adminUsername,
Password: adminPassword,
},
})
if err != nil {
return err
}
getStartedApp, err := web.NewWebApp(ctx, "getStartedApp", &web.WebAppArgs{
ResourceGroupName: resourceGroup.Name,
ServerFarmId: plan.ID(),
SiteConfig: &web.SiteConfigArgs{
AppSettings: web.NameValuePairArray{
&web.NameValuePairArgs{
Name: pulumi.String("WEBSITES_ENABLE_APP_SERVICE_STORAGE"),
Value: pulumi.String("false"),
},
&web.NameValuePairArgs{
Name: pulumi.String("DOCKER_REGISTRY_SERVER_URL"),
Value: registry.LoginServer.ApplyT(func(loginServer string) (string, error) {
return fmt.Sprintf("%v%v", "https://", loginServer), nil
}).(pulumi.StringOutput),
},
&web.NameValuePairArgs{
Name: pulumi.String("DOCKER_REGISTRY_SERVER_USERNAME"),
Value: adminUsername,
},
&web.NameValuePairArgs{
Name: pulumi.String("DOCKER_REGISTRY_SERVER_PASSWORD"),
Value: adminPassword,
},
&web.NameValuePairArgs{
Name: pulumi.String("WEBSITES_PORT"),
Value: pulumi.String("80"),
},
},
AlwaysOn: pulumi.Bool(true),
LinuxFxVersion: myImage.ImageName.ApplyT(func(result string) (string, error) {
return fmt.Sprintf("DOCKER|%s", result), nil
}).(pulumi.StringOutput),
},
HttpsOnly: pulumi.Bool(true),
})
if err != nil {
return err
}
ctx.Export("getStartedEndpoint", getStartedApp.DefaultHostName.ApplyT(func(defaultHostName string) (string, error) {
return fmt.Sprintf("%v%v", "https://", defaultHostName), nil
}).(pulumi.StringOutput))
return nil
})
}