/
bootstrap.go
127 lines (108 loc) · 3.23 KB
/
bootstrap.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
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/golang/protobuf/proto"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
dc "github.com/chef/automate/api/config/deployment"
"github.com/chef/automate/components/automate-deployment/pkg/airgap"
"github.com/chef/automate/components/automate-deployment/pkg/cli"
"github.com/chef/automate/components/automate-deployment/pkg/manifest"
"github.com/chef/automate/components/automate-deployment/pkg/target"
)
func init() {
RootCmd.AddCommand(setupCmd())
RootCmd.AddCommand(deployCmd())
}
func setupCmd() *cobra.Command {
return &cobra.Command{
Use: "setup-supervisor",
Short: "Install hab-sup and setup systemd unit",
Run: runSetup,
}
}
var deployCmdOpts struct {
bootstrapBundlePath string
}
func deployCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "deploy-service cfgPath manifestPath",
Short: "Configure and start deployment-service",
Run: runDeploy,
}
cmd.PersistentFlags().StringVar(&deployCmdOpts.bootstrapBundlePath, "bootstrap-bundle-path", "", "path to bootstrap bundle")
return cmd
}
// This is the same path that systemd will use for our service at startup
var standardSystemPath = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
func runSetup(cmd *cobra.Command, args []string) {
writer := cli.NewWriter(os.Stdout, os.Stderr, os.Stdin)
target := target.NewLocalTarget(airgap.AirgapInUse())
conf, manifest := getConfigAndManifest(args, writer)
setupLogging()
setupEnvironment()
err := target.SetupSupervisor(context.TODO(), conf, manifest, writer)
if err != nil {
writer.FailWrap(err, "setup failed")
os.Exit(1)
}
}
func runDeploy(cmd *cobra.Command, args []string) {
writer := cli.NewWriter(os.Stdout, os.Stderr, os.Stdin)
conf, manifest := getConfigAndManifest(args, writer)
setupLogging()
setupEnvironment()
target := target.NewLocalTarget(airgap.AirgapInUse())
err := target.DeployDeploymentService(context.TODO(), conf, manifest, deployCmdOpts.bootstrapBundlePath, writer)
if err != nil {
writer.FailWrap(err, "setup failed")
os.Exit(1)
}
}
func setupEnvironment() {
path := os.Getenv("PATH")
os.Setenv("PATH", fmt.Sprintf("%s:%s", path, standardSystemPath))
logrus.Debugf("PATH=%s", os.Getenv("PATH"))
}
func setupLogging() {
level := os.Getenv("CHEF_AUTOMATE_LOG_LEVEL")
if level != "" {
l, err := logrus.ParseLevel(level)
if err != nil {
logrus.Warnf("Unable to parse log level %s", level)
return
}
logrus.SetLevel(l)
}
}
func getConfigAndManifest(args []string, writer cli.FormatWriter) (*dc.ConfigRequest, manifest.ReleaseManifest) {
configFile := args[0]
configData, err := ioutil.ReadFile(configFile)
if err != nil {
writer.FailWrap(err, "reading configuration data")
os.Exit(1)
}
conf := &dc.ConfigRequest{}
err = proto.Unmarshal(configData, conf)
if err != nil {
writer.FailWrap(err, "parsing configuration data")
os.Exit(1)
}
manifestFile := args[1]
manifestData, err := ioutil.ReadFile(manifestFile)
if err != nil {
writer.FailWrap(err, "reading manifest data")
os.Exit(1)
}
manifest := &manifest.A2{}
err = json.Unmarshal(manifestData, manifest)
if err != nil {
writer.FailWrap(err, "parsing manifest data")
os.Exit(1)
}
return conf, manifest
}