This repository has been archived by the owner on Jun 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 174
/
install.go
99 lines (91 loc) · 2.76 KB
/
install.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
package main
import (
"encoding/json"
"io/ioutil"
"os"
"strconv"
"github.com/deislabs/cnab-go/bundle"
"github.com/docker/app/internal"
"github.com/docker/app/internal/packager"
"github.com/docker/app/render"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/stack"
"github.com/docker/cli/cli/command/stack/options"
"github.com/docker/cli/cli/command/stack/swarm"
composetypes "github.com/docker/cli/cli/compose/types"
"github.com/pkg/errors"
"github.com/spf13/pflag"
)
const (
// imageMapFilePath is the path where the CNAB runtime will put the actual
// service to image mapping to use
imageMapFilePath = "/cnab/app/image-map.json"
)
func installAction(instanceName string) error {
cli, err := setupDockerContext()
if err != nil {
return errors.Wrap(err, "unable to restore docker context")
}
app, err := packager.Extract("")
// todo: merge additional compose file
if err != nil {
return err
}
defer app.Cleanup()
orchestratorRaw := os.Getenv(internal.DockerStackOrchestratorEnvVar)
orchestrator, err := cli.StackOrchestrator(orchestratorRaw)
if err != nil {
return err
}
imageMap, err := getBundleImageMap()
if err != nil {
return err
}
parameters := packager.ExtractCNABParametersValues(packager.ExtractCNABParameterMapping(app.Parameters()), os.Environ())
rendered, err := render.Render(app, parameters, imageMap)
if err != nil {
return err
}
addAppLabels(rendered, instanceName)
if err := os.Chdir(app.Path); err != nil {
return err
}
sendRegistryAuth, err := strconv.ParseBool(os.Getenv("DOCKER_SHARE_REGISTRY_CREDS"))
if err != nil {
return err
}
// todo: pass registry auth to invocation image
return stack.RunDeploy(cli, getFlagset(orchestrator), rendered, orchestrator, options.Deploy{
Namespace: instanceName,
ResolveImage: swarm.ResolveImageAlways,
SendRegistryAuth: sendRegistryAuth,
})
}
func getFlagset(orchestrator command.Orchestrator) *pflag.FlagSet {
result := pflag.NewFlagSet("", pflag.ContinueOnError)
if orchestrator == command.OrchestratorKubernetes {
result.String("namespace", os.Getenv(internal.DockerKubernetesNamespaceEnvVar), "")
}
return result
}
func getBundleImageMap() (map[string]bundle.Image, error) {
mapJSON, err := ioutil.ReadFile(imageMapFilePath)
if err != nil {
return nil, err
}
var result map[string]bundle.Image
if err := json.Unmarshal(mapJSON, &result); err != nil {
return nil, err
}
return result, nil
}
func addAppLabels(rendered *composetypes.Config, instanceName string) {
for i, service := range rendered.Services {
if service.Labels == nil {
service.Labels = map[string]string{}
}
service.Labels[internal.LabelAppNamespace] = instanceName
service.Labels[internal.LabelAppVersion] = internal.Version
rendered.Services[i] = service
}
}