github.com/xgoffin/jenkins-library@v1.154.0/cmd/xsDeploy_generated.go (about) 1 // Code generated by piper's step-generator. DO NOT EDIT. 2 3 package cmd 4 5 import ( 6 "fmt" 7 "os" 8 "path/filepath" 9 "time" 10 11 "github.com/SAP/jenkins-library/pkg/config" 12 "github.com/SAP/jenkins-library/pkg/log" 13 "github.com/SAP/jenkins-library/pkg/piperenv" 14 "github.com/SAP/jenkins-library/pkg/splunk" 15 "github.com/SAP/jenkins-library/pkg/telemetry" 16 "github.com/SAP/jenkins-library/pkg/validation" 17 "github.com/spf13/cobra" 18 ) 19 20 type xsDeployOptions struct { 21 DeployOpts string `json:"deployOpts,omitempty"` 22 OperationIDLogPattern string `json:"operationIdLogPattern,omitempty"` 23 MtaPath string `json:"mtaPath,omitempty"` 24 Action string `json:"action,omitempty" validate:"possible-values=NONE Resume Abort Retry"` 25 Mode string `json:"mode,omitempty" validate:"possible-values=NONE DEPLOY BG_DEPLOY"` 26 OperationID string `json:"operationId,omitempty"` 27 APIURL string `json:"apiUrl,omitempty"` 28 Username string `json:"username,omitempty"` 29 Password string `json:"password,omitempty"` 30 Org string `json:"org,omitempty"` 31 Space string `json:"space,omitempty"` 32 LoginOpts string `json:"loginOpts,omitempty"` 33 XsSessionFile string `json:"xsSessionFile,omitempty"` 34 } 35 36 type xsDeployCommonPipelineEnvironment struct { 37 operationID string 38 } 39 40 func (p *xsDeployCommonPipelineEnvironment) persist(path, resourceName string) { 41 content := []struct { 42 category string 43 name string 44 value interface{} 45 }{ 46 {category: "", name: "operationId", value: p.operationID}, 47 } 48 49 errCount := 0 50 for _, param := range content { 51 err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value) 52 if err != nil { 53 log.Entry().WithError(err).Error("Error persisting piper environment.") 54 errCount++ 55 } 56 } 57 if errCount > 0 { 58 log.Entry().Error("failed to persist Piper environment") 59 } 60 } 61 62 // XsDeployCommand Performs xs deployment 63 func XsDeployCommand() *cobra.Command { 64 const STEP_NAME = "xsDeploy" 65 66 metadata := xsDeployMetadata() 67 var stepConfig xsDeployOptions 68 var startTime time.Time 69 var commonPipelineEnvironment xsDeployCommonPipelineEnvironment 70 var logCollector *log.CollectorHook 71 var splunkClient *splunk.Splunk 72 telemetryClient := &telemetry.Telemetry{} 73 74 var createXsDeployCmd = &cobra.Command{ 75 Use: STEP_NAME, 76 Short: "Performs xs deployment", 77 Long: `Performs xs deployment`, 78 PreRunE: func(cmd *cobra.Command, _ []string) error { 79 startTime = time.Now() 80 log.SetStepName(STEP_NAME) 81 log.SetVerbose(GeneralConfig.Verbose) 82 83 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 84 85 path, _ := os.Getwd() 86 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 87 log.RegisterHook(fatalHook) 88 89 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 90 if err != nil { 91 log.SetErrorCategory(log.ErrorConfiguration) 92 return err 93 } 94 log.RegisterSecret(stepConfig.Username) 95 log.RegisterSecret(stepConfig.Password) 96 97 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 98 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 99 log.RegisterHook(&sentryHook) 100 } 101 102 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 103 splunkClient = &splunk.Splunk{} 104 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 105 log.RegisterHook(logCollector) 106 } 107 108 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 109 if err != nil { 110 return err 111 } 112 if err = validation.ValidateStruct(stepConfig); err != nil { 113 log.SetErrorCategory(log.ErrorConfiguration) 114 return err 115 } 116 117 return nil 118 }, 119 Run: func(_ *cobra.Command, _ []string) { 120 stepTelemetryData := telemetry.CustomData{} 121 stepTelemetryData.ErrorCode = "1" 122 handler := func() { 123 commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment") 124 config.RemoveVaultSecretFiles() 125 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 126 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 127 stepTelemetryData.PiperCommitHash = GitCommit 128 telemetryClient.SetData(&stepTelemetryData) 129 telemetryClient.Send() 130 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 131 splunkClient.Send(telemetryClient.GetData(), logCollector) 132 } 133 } 134 log.DeferExitHandler(handler) 135 defer handler() 136 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 137 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 138 splunkClient.Initialize(GeneralConfig.CorrelationID, 139 GeneralConfig.HookConfig.SplunkConfig.Dsn, 140 GeneralConfig.HookConfig.SplunkConfig.Token, 141 GeneralConfig.HookConfig.SplunkConfig.Index, 142 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 143 } 144 xsDeploy(stepConfig, &stepTelemetryData, &commonPipelineEnvironment) 145 stepTelemetryData.ErrorCode = "0" 146 log.Entry().Info("SUCCESS") 147 }, 148 } 149 150 addXsDeployFlags(createXsDeployCmd, &stepConfig) 151 return createXsDeployCmd 152 } 153 154 func addXsDeployFlags(cmd *cobra.Command, stepConfig *xsDeployOptions) { 155 cmd.Flags().StringVar(&stepConfig.DeployOpts, "deployOpts", os.Getenv("PIPER_deployOpts"), "Additional options appended to the deploy command. Only needed for sophisticated cases. When provided it is the duty of the provider to ensure proper quoting / escaping.") 156 cmd.Flags().StringVar(&stepConfig.OperationIDLogPattern, "operationIdLogPattern", `^.*xs bg-deploy -i (.*) -a.*$`, "Regex pattern for retrieving the ID of the operation from the xs log.") 157 cmd.Flags().StringVar(&stepConfig.MtaPath, "mtaPath", os.Getenv("PIPER_mtaPath"), "Path to deployable") 158 cmd.Flags().StringVar(&stepConfig.Action, "action", `NONE`, "Used for finalizing the blue-green deployment.") 159 cmd.Flags().StringVar(&stepConfig.Mode, "mode", `DEPLOY`, "Controls if there is a standard deployment or a blue green deployment. Values: 'DEPLOY', 'BG_DEPLOY'") 160 cmd.Flags().StringVar(&stepConfig.OperationID, "operationId", os.Getenv("PIPER_operationId"), "The operation ID. Used in case of bg-deploy in order to resume or abort a previously started deployment.") 161 cmd.Flags().StringVar(&stepConfig.APIURL, "apiUrl", os.Getenv("PIPER_apiUrl"), "The api url (e.g. https://example.org:12345") 162 cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "Username") 163 cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password") 164 cmd.Flags().StringVar(&stepConfig.Org, "org", os.Getenv("PIPER_org"), "The org") 165 cmd.Flags().StringVar(&stepConfig.Space, "space", os.Getenv("PIPER_space"), "The space") 166 cmd.Flags().StringVar(&stepConfig.LoginOpts, "loginOpts", os.Getenv("PIPER_loginOpts"), "Additional options appended to the login command. Only needed for sophisticated cases. When provided it is the duty of the provider to ensure proper quoting / escaping.") 167 cmd.Flags().StringVar(&stepConfig.XsSessionFile, "xsSessionFile", os.Getenv("PIPER_xsSessionFile"), "The file keeping the xs session.") 168 169 cmd.MarkFlagRequired("mtaPath") 170 cmd.MarkFlagRequired("mode") 171 cmd.MarkFlagRequired("apiUrl") 172 cmd.MarkFlagRequired("username") 173 cmd.MarkFlagRequired("password") 174 cmd.MarkFlagRequired("org") 175 cmd.MarkFlagRequired("space") 176 cmd.MarkFlagRequired("loginOpts") 177 } 178 179 // retrieve step metadata 180 func xsDeployMetadata() config.StepData { 181 var theMetaData = config.StepData{ 182 Metadata: config.StepMetadata{ 183 Name: "xsDeploy", 184 Aliases: []config.Alias{}, 185 Description: "Performs xs deployment", 186 }, 187 Spec: config.StepSpec{ 188 Inputs: config.StepInputs{ 189 Secrets: []config.StepSecrets{ 190 {Name: "credentialsId", Description: "Jenkins 'Username with password' credentials ID containing username/password for accessing xs endpoint.", Type: "jenkins"}, 191 }, 192 Parameters: []config.StepParameters{ 193 { 194 Name: "deployOpts", 195 ResourceRef: []config.ResourceReference{}, 196 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 197 Type: "string", 198 Mandatory: false, 199 Aliases: []config.Alias{}, 200 Default: os.Getenv("PIPER_deployOpts"), 201 }, 202 { 203 Name: "operationIdLogPattern", 204 ResourceRef: []config.ResourceReference{}, 205 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 206 Type: "string", 207 Mandatory: false, 208 Aliases: []config.Alias{{Name: "deployIdLogPattern"}}, 209 Default: `^.*xs bg-deploy -i (.*) -a.*$`, 210 }, 211 { 212 Name: "mtaPath", 213 ResourceRef: []config.ResourceReference{ 214 { 215 Name: "commonPipelineEnvironment", 216 Param: "mtaPath", 217 }, 218 }, 219 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 220 Type: "string", 221 Mandatory: true, 222 Aliases: []config.Alias{}, 223 Default: os.Getenv("PIPER_mtaPath"), 224 }, 225 { 226 Name: "action", 227 ResourceRef: []config.ResourceReference{}, 228 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 229 Type: "string", 230 Mandatory: false, 231 Aliases: []config.Alias{}, 232 Default: `NONE`, 233 }, 234 { 235 Name: "mode", 236 ResourceRef: []config.ResourceReference{}, 237 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 238 Type: "string", 239 Mandatory: true, 240 Aliases: []config.Alias{}, 241 Default: `DEPLOY`, 242 }, 243 { 244 Name: "operationId", 245 ResourceRef: []config.ResourceReference{ 246 { 247 Name: "commonPipelineEnvironment", 248 Param: "operationId", 249 }, 250 }, 251 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 252 Type: "string", 253 Mandatory: false, 254 Aliases: []config.Alias{}, 255 Default: os.Getenv("PIPER_operationId"), 256 }, 257 { 258 Name: "apiUrl", 259 ResourceRef: []config.ResourceReference{}, 260 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 261 Type: "string", 262 Mandatory: true, 263 Aliases: []config.Alias{}, 264 Default: os.Getenv("PIPER_apiUrl"), 265 }, 266 { 267 Name: "username", 268 ResourceRef: []config.ResourceReference{ 269 { 270 Name: "credentialsId", 271 Param: "username", 272 Type: "secret", 273 }, 274 }, 275 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 276 Type: "string", 277 Mandatory: true, 278 Aliases: []config.Alias{{Name: "user", Deprecated: true}}, 279 Default: os.Getenv("PIPER_username"), 280 }, 281 { 282 Name: "password", 283 ResourceRef: []config.ResourceReference{ 284 { 285 Name: "credentialsId", 286 Param: "password", 287 Type: "secret", 288 }, 289 }, 290 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 291 Type: "string", 292 Mandatory: true, 293 Aliases: []config.Alias{}, 294 Default: os.Getenv("PIPER_password"), 295 }, 296 { 297 Name: "org", 298 ResourceRef: []config.ResourceReference{}, 299 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 300 Type: "string", 301 Mandatory: true, 302 Aliases: []config.Alias{}, 303 Default: os.Getenv("PIPER_org"), 304 }, 305 { 306 Name: "space", 307 ResourceRef: []config.ResourceReference{}, 308 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 309 Type: "string", 310 Mandatory: true, 311 Aliases: []config.Alias{}, 312 Default: os.Getenv("PIPER_space"), 313 }, 314 { 315 Name: "loginOpts", 316 ResourceRef: []config.ResourceReference{}, 317 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 318 Type: "string", 319 Mandatory: true, 320 Aliases: []config.Alias{}, 321 Default: os.Getenv("PIPER_loginOpts"), 322 }, 323 { 324 Name: "xsSessionFile", 325 ResourceRef: []config.ResourceReference{}, 326 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 327 Type: "string", 328 Mandatory: false, 329 Aliases: []config.Alias{}, 330 Default: os.Getenv("PIPER_xsSessionFile"), 331 }, 332 }, 333 }, 334 Containers: []config.Container{ 335 {Name: "xs", Image: "ppiper/xs-cli"}, 336 }, 337 Outputs: config.StepOutputs{ 338 Resources: []config.StepResources{ 339 { 340 Name: "commonPipelineEnvironment", 341 Type: "piperEnvironment", 342 Parameters: []map[string]interface{}{ 343 {"name": "operationId"}, 344 }, 345 }, 346 }, 347 }, 348 }, 349 } 350 return theMetaData 351 }