forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcp_installer.go
83 lines (74 loc) · 2.37 KB
/
gcp_installer.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
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package server
import (
"context"
"fmt"
"strings"
"github.com/gravitational/trace"
"golang.org/x/sync/errgroup"
apievents "github.com/gravitational/teleport/api/types/events"
"github.com/gravitational/teleport/lib/cloud/gcp"
)
// GCPInstaller handles running commands that install Teleport on GCP
// virtual machines.
type GCPInstaller struct {
Emitter apievents.Emitter
}
// GCPRunRequest combines parameters for running commands on a set of GCP
// virtual machines.
type GCPRunRequest struct {
Client gcp.InstancesClient
Instances []*gcp.Instance
Params []string
Zone string
ProjectID string
ScriptName string
PublicProxyAddr string
}
// Run runs a command on a set of virtual machines and then blocks until the
// commands have completed.
func (gi *GCPInstaller) Run(ctx context.Context, req GCPRunRequest) error {
g, ctx := errgroup.WithContext(ctx)
// Somewhat arbitrary limit to make sure Teleport doesn't have to install
// hundreds of nodes at once.
g.SetLimit(10)
for _, inst := range req.Instances {
inst := inst
g.Go(func() error {
runRequest := gcp.RunCommandRequest{
Client: req.Client,
InstanceRequest: inst.InstanceRequest(),
Script: getGCPInstallerScript(
req.ScriptName,
req.PublicProxyAddr,
req.Params,
),
}
return trace.Wrap(gcp.RunCommand(ctx, &runRequest))
})
}
return trace.Wrap(g.Wait())
}
func getGCPInstallerScript(installerName, publicProxyAddr string, params []string) string {
return fmt.Sprintf("curl -s -L https://%s/v1/webapi/scripts/installer/%s | bash -s %s",
publicProxyAddr,
installerName,
strings.Join(params, " "),
)
}