Skip to content

Commit

Permalink
Support for consuming json files for spinnaker (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielGz authored Oct 31, 2024
1 parent c427a42 commit 7d543cb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
22 changes: 19 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var migrationReq = struct {
SpinnakerAPIKey string `survey:"spinnaker-api-key"`
SpinnakerAppName string `survey:"app-name"`
PipelineName string `survey:"pipeline-name"`
PipelineJson string `survey:"pipeline-json"`
Cert string `survey:"cert"`
Key string `survey:"key"`
Auth64 string `survey:"auth64"`
Expand Down Expand Up @@ -136,18 +137,28 @@ func logMigrationDetails() {
}

func logSpinnakerMigrationDetails(authMethod string) {

determinePipelineOutput := func() string {
if len(migrationReq.PipelineJson) > 0 {
return migrationReq.PipelineJson
}
if len(migrationReq.PipelineName) > 0 {
return migrationReq.PipelineName
}
return "All"
}
// Manually format the log message with line breaks
logMessage := fmt.Sprintf("\nMigration details:\n"+
" Platform: %s\n"+
" Spinnaker Host: %s\n"+
" App name: %s\n"+
" Pipeline Name: %s\n"+
" Pipeline(s): %s\n"+
" Authentication method: %s \n"+
" Insecure: %t",
migrationReq.Platform,
migrationReq.SpinnakerHost,
migrationReq.SpinnakerAppName,
migrationReq.PipelineName,
determinePipelineOutput(),
authMethod,
migrationReq.AllowInsecureReq,
)
Expand Down Expand Up @@ -346,6 +357,11 @@ func main() {
Usage: "Specifies URL to the Spinnaker Gate service. Required when --platform is spinnaker.",
Destination: &migrationReq.SpinnakerHost,
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "pipeline-json",
Usage: "Specifies Spinnaker Pipeline JSON file to be migrated.",
Destination: &migrationReq.PipelineJson,
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "spinnaker-api-key",
Usage: "Specifies URL to the Spinnaker Gate service. Required when --platform is spinnaker.",
Expand Down Expand Up @@ -593,7 +609,7 @@ func main() {
},
&cli.StringFlag{
Name: "pipeline-name",
Usage: "Specifies Spinnaker Pipeline which to be migrated.",
Usage: "Specifies Spinnaker Pipeline to be migrated.",
Destination: &migrationReq.PipelineName,
},
&cli.StringFlag{
Expand Down
31 changes: 25 additions & 6 deletions pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"sort"
"strconv"

Expand Down Expand Up @@ -72,40 +73,58 @@ func migrateSpinnakerPipelines() error {
authMethod = authx509
}
log.Info("Importing the application....")
if len(migrationReq.SpinnakerHost) == 0 {
if len(migrationReq.PipelineJson) == 0 && len(migrationReq.SpinnakerHost) == 0 {
migrationReq.SpinnakerHost = TextInput("Please provide spinnaker host : ")
}
if len(migrationReq.SpinnakerAppName) == 0 {
if len(migrationReq.PipelineJson) == 0 && len(migrationReq.SpinnakerAppName) == 0 {
migrationReq.SpinnakerAppName = TextInput("Please provide the Spinnaker application name : ")
}
if !migrationReq.All {
migrationReq.PipelineName = TextInput("Please provide the Spinnaker pipeline name : ")
if len(migrationReq.PipelineJson) == 0 {
migrationReq.PipelineName = TextInput("Please provide the Spinnaker pipeline name : ")
}
}

logSpinnakerMigrationDetails(authMethod)
confirm := ConfirmInput("Do you want to proceed with pipeline migration?")
if !confirm {
log.Fatal("Aborting...")
}

var jsonBody []byte
var pipelines []map[string]interface{}
var err error

if len(migrationReq.PipelineName) > 0 {
jsonBody, err = getSinglePipeline(authMethod, migrationReq.PipelineName)
} else {
jsonBody, err = getAllPipelines(authMethod, migrationReq.SpinnakerAppName)
if len(migrationReq.PipelineJson) > 0 {
// Read from file
jsonBody, err = os.ReadFile(migrationReq.PipelineJson)
if err != nil {
return fmt.Errorf("failed to read pipeline JSON file: %v", err)
}
} else {
jsonBody, err = getAllPipelines(authMethod, migrationReq.SpinnakerAppName)
}
}

if err != nil {
return err
}

pipelines, err = normalizeJsonArray(jsonBody)
if err != nil {
return err
}
pipelines, err = fetchDependentPipelines(pipelines, err, authMethod)

if len(migrationReq.PipelineJson) == 0 {
pipelines, err = fetchDependentPipelines(pipelines, err, authMethod)
}
if err != nil {
return err
}

payload := map[string][]map[string]interface{}{"pipelines": pipelines}
_, err = createSpinnakerPipelines(payload)
return err
Expand Down Expand Up @@ -163,7 +182,7 @@ func addDependentPipelineRecursive(pipelines []map[string]interface{}, pipelineS
}
stages, ok := p["stages"].([]interface{})
if !ok {
fmt.Println("error: nable to assert 'stages' to the correct type.")
fmt.Println("error: unable to assert 'stages' to the correct type.")
return nil, errors.New("unable to assert 'stages' to the correct type")
}
for _, stage := range stages {
Expand Down

0 comments on commit 7d543cb

Please sign in to comment.