This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bosh.go
144 lines (125 loc) · 4 KB
/
bosh.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
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package bosh
import (
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"strings"
)
// diegoDeploymentPrefixes contains all the known TAS bosh deployment string prefixes
var diegoDeploymentPrefixes = []string{"cf-", "p-isolation-segment-", "pas-windows-"}
// Runner is a bosh command runner
type Runner struct {
env []string
}
// NewRunner creates a new bosh runner instance, env should contain the
// environment variables necessary to connect to the bosh instance.
func NewRunner(env []string) *Runner {
return &Runner{
env: env,
}
}
// Deploy executes the specified bosh deployment with the specified
// manifest on disk.
func (r Runner) Deploy(deploymentName string, manifestFilename string) error {
return r.DeployWithFlags(deploymentName, manifestFilename)
}
func (r Runner) DeployWithFlags(deploymentName string, manifestFilename string, flags ...string) error {
args := []string{
"deploy",
manifestFilename,
"--deployment", deploymentName,
"--non-interactive",
}
args = append(args, flags...)
log.Println("running command: bosh", strings.Join(args, " "))
cmd := exec.Command("bosh", args...)
cmd.Env = r.env
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// GetDeploymentManifest returns the bosh deployment manifest yaml for
// the specified deployment.
func (r Runner) GetDeploymentManifest(deploymentName string) ([]byte, error) {
output, err := r.boshExec("-d", deploymentName, "manifest")
if err != nil {
return nil, fmt.Errorf("retrieving bosh manifest failed: %w", err)
}
return output, nil
}
// GetDiegoDeployments returns only the bosh deployment names of deployments
// with diego cells: TAS, TASW, ISO segments.
func (r Runner) GetDiegoDeployments() ([]string, error) {
allDeployments, err := r.GetDeployments()
if err != nil {
return nil, err
}
return filterDiegoDeployments(allDeployments), nil
}
// GetDeployments gets a bosh deployment names.
func (r Runner) GetDeployments() (deployments []string, err error) {
output, err := r.boshExec("deployments", "--json")
if err != nil {
return nil, fmt.Errorf("retrieving bosh deployments failed: %w", err)
}
return loadDeployments(output)
}
// GetDeploymentVMs gets all the VMs from the specified deployment.
func (r Runner) GetDeploymentVMs(deploymentName string) (vms []VM, err error) {
output, err := r.boshExec("vms", "-d", deploymentName, "--json")
if err != nil {
return nil, fmt.Errorf("retrieving bosh vms failed: %w", err)
}
return loadVMs(deploymentName, output)
}
// ScpFile copies a file using bosh scp
func (r Runner) ScpFile(deploymentName, source, target string) error {
output, err := r.boshExec("-d", deploymentName, "scp", source, target)
if err != nil {
return fmt.Errorf("failed to SCP file from %s to %s (%s): %w", source, target, output, err)
}
return nil
}
// filterDiegoDeployments filter out deployments that don't contain diego cells
func filterDiegoDeployments(deployments []string) (diegoDeployments []string) {
for _, d := range deployments {
for _, p := range diegoDeploymentPrefixes {
if strings.HasPrefix(d, p) {
diegoDeployments = append(diegoDeployments, d)
}
}
}
return diegoDeployments
}
func (r *Runner) boshExec(args ...string) ([]byte, error) {
cmd := exec.Command("bosh", args...)
cmd.Env = r.env
output, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("could not execute bosh command: bosh %s: %w\n%s",
strings.Join(args, " "), err, string(output))
}
return output, nil
}
func loadDeployments(output []byte) (deployments []string, err error) {
type boshDeployments struct {
Tables []struct {
Rows []struct {
Name string `json:"name,omitempty"`
} `json:"Rows,omitempty"`
} `json:"Tables,omitempty"`
}
var d boshDeployments
err = json.Unmarshal(output, &d)
if err != nil {
return nil, fmt.Errorf("invalid json from bosh deployments: %w", err)
}
for _, row := range d.Tables[0].Rows {
deployments = append(deployments, row.Name)
}
return deployments, nil
}