/
provisioner.go
278 lines (238 loc) · 8.3 KB
/
provisioner.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) 2022 SIGHUP s.r.l All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package aws
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"github.com/gobuffalo/packr/v2"
"github.com/hashicorp/terraform-exec/tfexec"
cfg "github.com/sighupio/furyctl/internal/bootstrap/configuration"
"github.com/sighupio/furyctl/internal/configuration"
log "github.com/sirupsen/logrus"
)
// InitMessage return a custom provisioner message the user will see once the cluster is ready to be updated
func (d *AWS) InitMessage() string {
return `[AWS] - VPC and VPN
This provisioner creates a battle-tested AWS VPC with all the requirements
set to run a production-grade private EKS cluster.
It creates a VPN server enables deploying the cluster from this computer
once connected to the VPN server.
Then, use furyagent to manage VPN profiles.
`
}
// UpdateMessage return a custom provisioner message the user will see once the cluster is updated
func (d *AWS) UpdateMessage() string {
var output map[string]tfexec.OutputMeta
output, err := d.terraform.Output(context.Background())
if err != nil {
log.Error("Can not get output values")
}
spec := d.config.Spec.(cfg.AWS)
sshUsers := spec.VPN.SSHUsers
var vpnOperatorName, vpcID string
var vpnInstanceIPs, publicSubnetsIDs, privateSubnetsIDs []string
err = json.Unmarshal(output["vpn_ip"].Value, &vpnInstanceIPs)
if err != nil {
log.Error("Can not get `vpn_ip` value")
}
err = json.Unmarshal(output["vpn_operator_name"].Value, &vpnOperatorName)
if err != nil {
log.Error("Can not get `vpn_operator_name` value")
}
err = json.Unmarshal(output["vpc_id"].Value, &vpcID)
if err != nil {
log.Error("Can not get `vpc_id` value")
}
err = json.Unmarshal(output["public_subnets"].Value, &publicSubnetsIDs)
if err != nil {
log.Error("Can not get `public_subnets` value")
}
err = json.Unmarshal(output["private_subnets"].Value, &privateSubnetsIDs)
if err != nil {
log.Error("Can not get `private_subnets` value")
}
vpnFragment := ""
if len(vpnInstanceIPs) > 0 {
vpnSSHFragment := ""
for _, server := range vpnInstanceIPs {
vpnSSHFragment = vpnSSHFragment + fmt.Sprintf("$ ssh %v@%v\n", vpnOperatorName, server)
}
vpnFragment = fmt.Sprintf(`
Your VPN instance IPs are: %v
Use the ssh %v username to access the VPN instance with any SSH key configured
for the following GitHub users: %v.
%v`, vpnInstanceIPs, vpnOperatorName, sshUsers, vpnSSHFragment)
}
return fmt.Sprintf(`[AWS] - VPC and VPN
All the bootstrap components are up to date.
VPC and VPN ready:
VPC: %v
Public Subnets: %v
Private Subnets: %v
%v
Then create a openvpn configuration (ovpn) file using the furyagent cli:
$ furyagent configure openvpn-client --client-name <your-name-goes-here> --config %v/secrets/furyagent.yml > <your-name-goes-here>.ovpn
Discover already registered vpn clients running:
$ furyagent configure openvpn-client --list --config %v/secrets/furyagent.yml
IMPORTANT! Connect to the VPN with the created ovpn profile to continue deploying
an AWS Kubernetes cluster.
`, vpcID, publicSubnetsIDs, privateSubnetsIDs, vpnFragment, d.terraform.WorkingDir(), d.terraform.WorkingDir())
}
// DestroyMessage return a custom provisioner message the user will see once the cluster is destroyed
func (d *AWS) DestroyMessage() string {
return `[AWS] - VPC and VPN
All bootstrap components were destroyed.
VPN and VPC went away.
Had problems, contact us at sales@sighup.io.
`
}
// Enterprise return a boolean indicating it is an enterprise provisioner
func (d *AWS) Enterprise() bool {
return false
}
// AWS represents a dummy provisioner
type AWS struct {
terraform *tfexec.Terraform
box *packr.Box
config *configuration.Configuration
}
const (
projectPath = "../../../../data/provisioners/bootstrap/aws"
)
func (d AWS) createVarFile() (err error) {
var buffer bytes.Buffer
spec := d.config.Spec.(cfg.AWS)
buffer.WriteString(fmt.Sprintf("name = \"%v\"\n", d.config.Metadata.Name))
buffer.WriteString(fmt.Sprintf("network_cidr = \"%v\"\n", spec.NetworkCIDR))
buffer.WriteString(fmt.Sprintf("public_subnetwork_cidrs = [\"%v\"]\n", strings.Join(spec.PublicSubnetsCIDRs, "\",\"")))
buffer.WriteString(fmt.Sprintf("private_subnetwork_cidrs = [\"%v\"]\n", strings.Join(spec.PrivateSubnetsCIDRs, "\",\"")))
buffer.WriteString(fmt.Sprintf("vpn_subnetwork_cidr = \"%v\"\n", spec.VPN.SubnetCIDR))
if len(spec.Tags) > 0 {
var tags []byte
tags, err = json.Marshal(spec.Tags)
if err != nil {
return err
}
buffer.WriteString(fmt.Sprintf("tags = %v\n", string(tags)))
}
buffer.WriteString(fmt.Sprintf("vpn_instances = %v\n", spec.VPN.Instances))
if spec.VPN.Port != 0 {
buffer.WriteString(fmt.Sprintf("vpn_port = %v\n", spec.VPN.Port))
}
if spec.VPN.InstanceType != "" {
buffer.WriteString(fmt.Sprintf("vpn_instance_type = \"%v\"\n", spec.VPN.InstanceType))
}
if spec.VPN.DiskSize != 0 {
buffer.WriteString(fmt.Sprintf("vpn_instance_disk_size = %v\n", spec.VPN.DiskSize))
}
if spec.VPN.OperatorName != "" {
buffer.WriteString(fmt.Sprintf("vpn_operator_name = \"%v\"\n", spec.VPN.OperatorName))
}
if spec.VPN.DHParamsBits != 0 {
buffer.WriteString(fmt.Sprintf("vpn_dhparams_bits = %v\n", spec.VPN.DHParamsBits))
}
if len(spec.VPN.OperatorCIDRs) != 0 {
buffer.WriteString(fmt.Sprintf("vpn_operator_cidrs = [\"%v\"]\n", strings.Join(spec.VPN.OperatorCIDRs, "\",\"")))
}
if len(spec.VPN.SSHUsers) != 0 {
buffer.WriteString(fmt.Sprintf("vpn_ssh_users = [\"%v\"]\n", strings.Join(spec.VPN.SSHUsers, "\",\"")))
}
err = ioutil.WriteFile(fmt.Sprintf("%v/aws.tfvars", d.terraform.WorkingDir()), buffer.Bytes(), 0600)
if err != nil {
return err
}
err = d.terraform.FormatWrite(context.Background(), tfexec.Dir(fmt.Sprintf("%v/aws.tfvars", d.terraform.WorkingDir())))
if err != nil {
return err
}
return nil
}
// New instantiates a new AWS provisioner
func New(config *configuration.Configuration) *AWS {
b := packr.New("AWS", projectPath)
return &AWS{
box: b,
config: config,
}
}
// SetTerraformExecutor adds the terraform executor to this provisioner
func (d *AWS) SetTerraformExecutor(tf *tfexec.Terraform) {
d.terraform = tf
}
// TerraformExecutor returns the current terraform executor of this provisioner
func (d *AWS) TerraformExecutor() (tf *tfexec.Terraform) {
return d.terraform
}
// Box returns the box that has the files as binary data
func (d AWS) Box() *packr.Box {
return d.box
}
// TerraformFiles returns the list of files conforming the terraform project
func (d AWS) TerraformFiles() []string {
// TODO understand if it is possible to deduce these values somehow
// find . -type f -follow -print
return []string{
"output.tf",
"main.tf",
"variables.tf",
}
}
// Plan runs a dry run execution
func (d AWS) Prepare() (err error) {
return nil
}
// Plan runs a dry run execution
func (d AWS) Plan() (err error) {
log.Info("[DRYRUN] Updating AWS Bootstrap project")
err = d.createVarFile()
if err != nil {
return err
}
changes, err := d.terraform.Plan(context.Background(), tfexec.VarFile(fmt.Sprintf("%v/aws.tfvars", d.terraform.WorkingDir())))
if err != nil {
log.Fatalf("[DRYRUN] Something went wrong while updating aws. %v", err)
return err
}
if changes {
log.Warn("[DRYRUN] Something changed along the time. Remove dryrun option to apply the desired state")
} else {
log.Info("[DRYRUN] Everything is up to date")
}
log.Info("[DRYRUN] AWS Updated")
return nil
}
// Update runs terraform apply in the project
func (d AWS) Update() (string, error) {
log.Info("Updating AWS Bootstrap project")
err := d.createVarFile()
if err != nil {
return "", err
}
err = d.terraform.Apply(context.Background(), tfexec.VarFile(fmt.Sprintf("%v/aws.tfvars", d.terraform.WorkingDir())))
if err != nil {
log.Fatalf("Something went wrong while updating aws. %v", err)
return "", err
}
log.Info("AWS Updated")
return "", nil
}
// Destroy runs terraform destroy in the project
func (d AWS) Destroy() (err error) {
log.Info("Destroying AWS Bootstrap project")
err = d.createVarFile()
if err != nil {
return err
}
err = d.terraform.Destroy(context.Background(), tfexec.VarFile(fmt.Sprintf("%v/aws.tfvars", d.terraform.WorkingDir())))
if err != nil {
log.Fatalf("Something went wrong while destroying AWS Bootstrap project. %v", err)
return err
}
log.Info("AWS Bootstrap destroyed")
return nil
}