Skip to content

Commit

Permalink
feat: [ASSMT-52]: adding support for application migration (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
lasdox authored Feb 21, 2024
1 parent 4814059 commit b4106e4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
45 changes: 45 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (

func migrateApp(*cli.Context) error {
promptConfirm := PromptDefaultInputs()
if migrationReq.Platform == spinnaker {
return migrateSpinnakerApplication()
}

if len(migrationReq.AppId) == 0 {
promptConfirm = true
migrationReq.AppId = TextInput("Please provide the application ID of the app that you wish to import -")
Expand Down Expand Up @@ -48,3 +52,44 @@ func migrateApp(*cli.Context) error {

return nil
}

func migrateSpinnakerApplication() error {
authMethod := authBasic
if len(migrationReq.Cert) > 0 {
authMethod = authx509
}

if len(migrationReq.SpinnakerHost) == 0 {
migrationReq.SpinnakerHost = TextInput("Please provide spinnaker host")
}
if len(migrationReq.SpinnakerAppName) == 0 {
migrationReq.SpinnakerAppName = TextInput("Please provide the Spinnaker application name")
}

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

// for now we are only creating project and migrating pipelines
err := createAProject("default", migrationReq.SpinnakerAppName, formatString(migrationReq.SpinnakerAppName))
if err != nil {
log.Error(err)
}

jsonBody, err := getAllPipelines(authMethod)
if err != nil {
return err
}

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

payload := map[string][]map[string]interface{}{"pipelines": pipelines}
_, err = createSpinnakerPipelines(payload)
return err
}
16 changes: 13 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var migrationReq = struct {
Platform string `survey:"platform"`
SpinnakerHost string `survey:"spinnaker-host"`
SpinnakerAPIKey string `survey:"spinnaker-api-key"`
AppName string `survey:"app-name"`
SpinnakerAppName string `survey:"app-name"`
PipelineName string `survey:"pipeline-name"`
Cert string `survey:"cert"`
Key string `survey:"key"`
Expand Down Expand Up @@ -143,7 +143,7 @@ func logSpinnakerMigrationDetails(authMethod string) {
" Authentication method: %s",
migrationReq.Platform,
migrationReq.SpinnakerHost,
migrationReq.AppName,
migrationReq.SpinnakerAppName,
migrationReq.PipelineName,
authMethod,
)
Expand Down Expand Up @@ -415,6 +415,16 @@ func main() {
Usage: "if set will migrate all workflows & pipelines",
Destination: &migrationReq.AllAppEntities,
},
&cli.StringFlag{
Name: "app-name",
Usage: "Specifies Spinnaker Application from which pipelines to be migrated.",
Destination: &migrationReq.SpinnakerAppName,
},
&cli.StringFlag{
Name: "auth64",
Usage: "Base64 <username>:<password> in case Spinnaker uses basic auth.",
Destination: &migrationReq.Auth64,
},
},
},
{
Expand Down Expand Up @@ -565,7 +575,7 @@ func main() {
&cli.StringFlag{
Name: "app-name",
Usage: "Specifies Spinnaker Application from which pipelines to be migrated.",
Destination: &migrationReq.AppName,
Destination: &migrationReq.SpinnakerAppName,
},
&cli.StringFlag{
Name: "pipeline-name",
Expand Down
11 changes: 6 additions & 5 deletions pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package main
import (
"encoding/json"
"fmt"
"strconv"

log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"strconv"
)

const spinnaker string = "spinnaker"
Expand Down Expand Up @@ -72,8 +73,8 @@ func migrateSpinnakerPipelines() error {
if len(migrationReq.SpinnakerHost) == 0 {
migrationReq.SpinnakerHost = TextInput("Please provide spinnaker host")
}
if len(migrationReq.AppName) == 0 {
migrationReq.AppName = TextInput("Please provide the Spinnaker application name")
if len(migrationReq.SpinnakerAppName) == 0 {
migrationReq.SpinnakerAppName = TextInput("Please provide the Spinnaker application name")
}

if !migrationReq.All {
Expand Down Expand Up @@ -234,11 +235,11 @@ func findPipelineIdByName(pipelines []PipelineDetails, name string) string {
}

func getAllPipelines(authMethod string) ([]byte, error) {
return GetWithAuth(migrationReq.SpinnakerHost, "applications/"+migrationReq.AppName+"/pipelineConfigs", authMethod, migrationReq.Auth64, migrationReq.Cert, migrationReq.Key)
return GetWithAuth(migrationReq.SpinnakerHost, "applications/"+migrationReq.SpinnakerAppName+"/pipelineConfigs", authMethod, migrationReq.Auth64, migrationReq.Cert, migrationReq.Key)
}

func getSinglePipeline(authMethod string, name string) ([]byte, error) {
return GetWithAuth(migrationReq.SpinnakerHost, "applications/"+migrationReq.AppName+"/pipelineConfigs/"+name, authMethod, migrationReq.Auth64, migrationReq.Cert, migrationReq.Key)
return GetWithAuth(migrationReq.SpinnakerHost, "applications/"+migrationReq.SpinnakerAppName+"/pipelineConfigs/"+name, authMethod, migrationReq.Auth64, migrationReq.Cert, migrationReq.Key)
}

func createSpinnakerPipelines(pipelines interface{}) (reqId string, err error) {
Expand Down

0 comments on commit b4106e4

Please sign in to comment.