|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/gob" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/pkg/errors" |
| 11 | + "github.com/urfave/cli" |
| 12 | +) |
| 13 | + |
| 14 | +type deployment struct { |
| 15 | + BW2Entity string |
| 16 | + URI string |
| 17 | + Name string |
| 18 | + Configuration string |
| 19 | + Timeout string |
| 20 | +} |
| 21 | + |
| 22 | +func actionDeployLast(c *cli.Context) error { |
| 23 | + sourceFile := getDeploymentHistoryFile() |
| 24 | + oldDeployments, err := readDeployments(sourceFile) |
| 25 | + if err != nil { |
| 26 | + fmt.Printf("Failed to obtain deployment history: %s", err) |
| 27 | + os.Exit(1) |
| 28 | + } |
| 29 | + currentDir, err := os.Getwd() |
| 30 | + if err != nil { |
| 31 | + fmt.Printf("Failed to get current working directory: %s", err) |
| 32 | + os.Exit(1) |
| 33 | + } |
| 34 | + |
| 35 | + prevDeployment, ok := oldDeployments[currentDir] |
| 36 | + if !ok { |
| 37 | + fmt.Println("No previous deployment known for this directory") |
| 38 | + os.Exit(1) |
| 39 | + } |
| 40 | + |
| 41 | + var command string |
| 42 | + if len(prevDeployment.Timeout) > 0 { |
| 43 | + command = fmt.Sprintf("spawnctl -e %s deploy -u %s -c %s -n %s -t %s", prevDeployment.BW2Entity, |
| 44 | + prevDeployment.URI, prevDeployment.Configuration, prevDeployment.Name, prevDeployment.Timeout) |
| 45 | + } else { |
| 46 | + command = fmt.Sprintf("spawnctl -e %s deploy -u %s -c %s -n %s", prevDeployment.BW2Entity, |
| 47 | + prevDeployment.URI, prevDeployment.Configuration, prevDeployment.Name) |
| 48 | + } |
| 49 | + |
| 50 | + proceed := c.Bool("yes") |
| 51 | + if !proceed { |
| 52 | + fmt.Println("This will run:") |
| 53 | + fmt.Printf("\t%s\n", command) |
| 54 | + fmt.Println("Proceed? [Y/n]") |
| 55 | + reader := bufio.NewReader(os.Stdin) |
| 56 | + input, _ := reader.ReadString('\n') |
| 57 | + proceed = (input == "y\n" || input == "Y\n" || input == "\n") |
| 58 | + } |
| 59 | + |
| 60 | + if proceed { |
| 61 | + flags := flag.NewFlagSet("spawnctl", 0) |
| 62 | + flags.String("uri", prevDeployment.URI, "") |
| 63 | + flags.String("configuration", prevDeployment.Configuration, "") |
| 64 | + flags.String("name", prevDeployment.Name, "") |
| 65 | + flags.String("timeout", prevDeployment.Timeout, "") |
| 66 | + c.GlobalSet("entity", prevDeployment.BW2Entity) |
| 67 | + cliCtxt := cli.NewContext(c.App, flags, c) |
| 68 | + return actionDeploy(cliCtxt) |
| 69 | + } |
| 70 | + |
| 71 | + return nil |
| 72 | +} |
| 73 | + |
| 74 | +func getDeploymentHistoryFile() string { |
| 75 | + histFile := os.Getenv(deploymentHistVar) |
| 76 | + //Environment variable not set, fall back to default |
| 77 | + if len(histFile) == 0 { |
| 78 | + histFile = os.Getenv("HOME") + string(os.PathSeparator) + ".spawnpoint_history" |
| 79 | + } |
| 80 | + return histFile |
| 81 | +} |
| 82 | + |
| 83 | +func saveDeployment(directory string, dep deployment, fileName string) error { |
| 84 | + oldDeployments, err := readDeployments(fileName) |
| 85 | + if err != nil { |
| 86 | + return errors.Wrap(err, "Failed to read old deployments") |
| 87 | + } |
| 88 | + oldDeployments[directory] = dep |
| 89 | + if err = writeDeployments(oldDeployments, fileName); err != nil { |
| 90 | + return errors.Wrap(err, "Failed to write new deployment record") |
| 91 | + } |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +func readDeployments(fileName string) (map[string]deployment, error) { |
| 96 | + var oldDeployments map[string]deployment |
| 97 | + reader, err := os.Open(fileName) |
| 98 | + if err != nil { |
| 99 | + if !os.IsNotExist(err) { |
| 100 | + return nil, errors.Wrap(err, "Failed to open history file") |
| 101 | + } |
| 102 | + oldDeployments = make(map[string]deployment) |
| 103 | + return oldDeployments, nil |
| 104 | + } |
| 105 | + |
| 106 | + defer reader.Close() |
| 107 | + decoder := gob.NewDecoder(reader) |
| 108 | + if err = decoder.Decode(&oldDeployments); err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + |
| 112 | + return oldDeployments, nil |
| 113 | +} |
| 114 | + |
| 115 | +func writeDeployments(deployments map[string]deployment, fileName string) error { |
| 116 | + writer, err := os.Create(fileName) |
| 117 | + if err != nil { |
| 118 | + return errors.Wrap(err, "Failed to create history file") |
| 119 | + } |
| 120 | + |
| 121 | + defer writer.Close() |
| 122 | + encoder := gob.NewEncoder(writer) |
| 123 | + if err = encoder.Encode(deployments); err != nil { |
| 124 | + return errors.Wrap(err, "Failed to encode deployments") |
| 125 | + } |
| 126 | + return nil |
| 127 | +} |
0 commit comments