/
cmd_start.go
187 lines (158 loc) · 6.68 KB
/
cmd_start.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
/*
Copyright (C) 2019 Synopsys, Inc.
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package synopsysctl
import (
"fmt"
alertctl "github.com/blackducksoftware/synopsysctl/pkg/alert"
"github.com/blackducksoftware/synopsysctl/pkg/globals"
"github.com/blackducksoftware/synopsysctl/pkg/util"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var startAlertCobraHelper alertctl.HelmValuesFromCobraFlags
// startCmd starts a Synopsys resource in the cluster
var startCmd = &cobra.Command{
Use: "start",
Short: "Start a Synopsys resource",
RunE: func(cmd *cobra.Command, args []string) error {
return fmt.Errorf("must specify a sub-command")
},
}
// startAlertCmd starts an Alert instance
var startAlertCmd = &cobra.Command{
Use: "alert NAME -n NAMESPACE",
Example: "synopsysctl start alert <name> -n <namespace>",
Short: "Start an Alert instance",
SilenceUsage: true,
SilenceErrors: true,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
cmd.Help()
return fmt.Errorf("this command takes 1 argument, but got %+v", args)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
alertName := args[0]
helmReleaseName := fmt.Sprintf("%s%s", alertName, globals.AlertPostSuffix)
instance, err := util.GetWithHelm3(helmReleaseName, namespace, kubeConfigPath)
if err != nil {
return fmt.Errorf("couldn't find instance '%s' in namespace '%s'", alertName, namespace)
}
// Update the Helm Chart Location
alertVersionFromRelease := util.GetValueFromRelease(instance, []string{"alert", "imageTag"}).(string)
err = SetHelmChartLocation(cmd.Flags(), globals.AlertChartName, alertVersionFromRelease, &globals.AlertChartRepository)
if err != nil {
return fmt.Errorf("failed to set the app resources location due to %+v", err)
}
helmValuesMap := instance.Config
util.SetHelmValueInMap(helmValuesMap, []string{"status"}, "Running")
err = util.UpdateWithHelm3(helmReleaseName, namespace, globals.AlertChartRepository, helmValuesMap, kubeConfigPath)
if err != nil {
cleanErrorMsg := cleanAlertHelmError(err.Error(), helmReleaseName, alertName)
return fmt.Errorf("failed to create Alert resources: %+v", cleanErrorMsg)
}
log.Infof("successfully submitted start Alert '%s' in namespace '%s'", alertName, namespace)
return nil
},
}
// startBlackDuckCmd starts a Black Duck instance
var startBlackDuckCmd = &cobra.Command{
Use: "blackduck NAME -n NAMESPACE",
Example: "synopsysctl start blackduck <name> -n <namespace>",
Short: "Start a Black Duck instance",
SilenceUsage: true,
SilenceErrors: true,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
cmd.Help()
return fmt.Errorf("this command takes 1 argument, but got %+v", args)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
instance, err := util.GetWithHelm3(args[0], namespace, kubeConfigPath)
if err != nil {
return fmt.Errorf("couldn't find instance %s in namespace %s", args[0], namespace)
}
// Update the Helm Chart Location
blackDuckVersionFromRelease := util.GetValueFromRelease(instance, []string{"imageTag"}).(string)
err = SetHelmChartLocation(cmd.Flags(), globals.BlackDuckChartName, blackDuckVersionFromRelease, &globals.BlackDuckChartRepository)
if err != nil {
return fmt.Errorf("failed to set the app resources location due to %+v", err)
}
helmValuesMap := instance.Config
util.SetHelmValueInMap(helmValuesMap, []string{"status"}, "Running")
err = util.UpdateWithHelm3(args[0], namespace, globals.BlackDuckChartRepository, helmValuesMap, kubeConfigPath)
if err != nil {
return fmt.Errorf("failed to create Blackduck resources: %+v", err)
}
return nil
},
}
// startOpsSightCmd starts an OpsSight instance
var startOpsSightCmd = &cobra.Command{
Use: "opssight NAME -n NAMESPACE",
Example: "synopsysctl start opssight <name> -n <namespace>",
Short: "Start an OpsSight instance",
SilenceUsage: true,
SilenceErrors: true,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
cmd.Help()
return fmt.Errorf("this command takes 1 argument, but got %+v", args)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
opssightName := args[0]
instance, err := util.GetWithHelm3(opssightName, namespace, kubeConfigPath)
if err != nil {
return fmt.Errorf("couldn't find instance %s in namespace %s", opssightName, namespace)
}
// Update the Helm Chart Location
opsSightVersionFromRelease := util.GetValueFromRelease(instance, []string{"imageTag"}).(string)
err = SetHelmChartLocation(cmd.Flags(), globals.OpsSightChartName, opsSightVersionFromRelease, &globals.OpsSightChartRepository)
if err != nil {
return fmt.Errorf("failed to set the app resources location due to %+v", err)
}
helmValuesMap := instance.Config
util.SetHelmValueInMap(helmValuesMap, []string{"status"}, "Running")
err = util.UpdateWithHelm3(opssightName, namespace, globals.OpsSightChartRepository, helmValuesMap, kubeConfigPath)
if err != nil {
return fmt.Errorf("failed to create OpsSight resources: %+v", err)
}
return nil
},
}
func init() {
startAlertCobraHelper = *alertctl.NewHelmValuesFromCobraFlags()
rootCmd.AddCommand(startCmd)
startAlertCmd.Flags().StringVarP(&namespace, "namespace", "n", namespace, "Namespace of the instance(s)")
cobra.MarkFlagRequired(startAlertCmd.Flags(), "namespace")
addChartLocationPathFlag(startAlertCmd)
startCmd.AddCommand(startAlertCmd)
startBlackDuckCmd.Flags().StringVarP(&namespace, "namespace", "n", namespace, "Namespace of the instance(s)")
cobra.MarkFlagRequired(startBlackDuckCmd.Flags(), "namespace")
addChartLocationPathFlag(startBlackDuckCmd)
startCmd.AddCommand(startBlackDuckCmd)
startOpsSightCmd.Flags().StringVarP(&namespace, "namespace", "n", namespace, "Namespace of the instance(s)")
cobra.MarkFlagRequired(startOpsSightCmd.PersistentFlags(), "namespace")
addChartLocationPathFlag(startOpsSightCmd)
startCmd.AddCommand(startOpsSightCmd)
}