-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplatform.go
109 lines (86 loc) · 3.08 KB
/
platform.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
package switchblade
import (
"fmt"
"os"
"path/filepath"
"github.com/cloudfoundry/switchblade/internal/cloudfoundry"
"github.com/cloudfoundry/switchblade/internal/docker"
"github.com/docker/docker/client"
"github.com/paketo-buildpacks/packit/v2/pexec"
)
type Buildpack struct {
Name string
URI string
}
type Service map[string]interface{}
type Platform struct {
initialize initializeProcess
deinitialize deinitializeProcess
Deploy DeployProcess
Delete DeleteProcess
}
type DeployProcess interface {
WithBuildpacks(buildpacks ...string) DeployProcess
WithStack(stack string) DeployProcess
WithEnv(env map[string]string) DeployProcess
WithoutInternetAccess() DeployProcess
WithServices(map[string]Service) DeployProcess
WithStartCommand(command string) DeployProcess
Execute(name, path string) (Deployment, fmt.Stringer, error)
}
type DeleteProcess interface {
Execute(name string) error
}
type initializeProcess interface {
Execute(buildpacks ...Buildpack) error
}
type deinitializeProcess interface {
Execute() error
}
const (
CloudFoundry = "cf"
Docker = "docker"
)
func NewPlatform(platformType, token, stack string) (Platform, error) {
home, err := os.UserHomeDir()
if err != nil {
return Platform{}, err
}
switch platformType {
case CloudFoundry:
cli := pexec.NewExecutable("cf")
initialize := cloudfoundry.NewInitialize(cli, stack)
deinitialize := cloudfoundry.NewDeinitialize()
setup := cloudfoundry.NewSetup(cli, filepath.Join(home, ".cf"), stack)
stage := cloudfoundry.NewStage(cli)
teardown := cloudfoundry.NewTeardown(cli)
return NewCloudFoundry(initialize, deinitialize, setup, stage, teardown, os.TempDir()), nil
case Docker:
client, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return Platform{}, err
}
workspace := filepath.Join(home, ".switchblade")
golang := pexec.NewExecutable("go")
archiver := docker.NewTGZArchiver()
lifecycleManager := docker.NewLifecycleManager(golang, archiver)
buildpacksCache := docker.NewBuildpacksCache(filepath.Join(workspace, "buildpacks-cache"))
buildpacksRegistry := docker.NewBuildpacksRegistry("https://api.github.com", token)
buildpacksManager := docker.NewBuildpacksManager(archiver, buildpacksCache, buildpacksRegistry)
networkManager := docker.NewNetworkManager(client)
initialize := docker.NewInitialize(buildpacksRegistry, networkManager)
deinitialize := docker.NewDeinitialize(networkManager)
setup := docker.NewSetup(client, lifecycleManager, buildpacksManager, archiver, networkManager, workspace, stack)
stage := docker.NewStage(client, archiver, workspace)
start := docker.NewStart(client, networkManager, workspace, stack)
teardown := docker.NewTeardown(client, workspace)
return NewDocker(initialize, deinitialize, setup, stage, start, teardown), nil
}
return Platform{}, fmt.Errorf("unknown platform type: %q", platformType)
}
func (p Platform) Initialize(buildpacks ...Buildpack) error {
return p.initialize.Execute(buildpacks...)
}
func (p Platform) Deinitialize() error {
return p.deinitialize.Execute()
}