-
Notifications
You must be signed in to change notification settings - Fork 2
/
deployLast.go
127 lines (111 loc) · 3.37 KB
/
deployLast.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
package main
import (
"bufio"
"encoding/gob"
"flag"
"fmt"
"os"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
type deployment struct {
BW2Entity string
URI string
Name string
Configuration string
Timeout string
}
func actionDeployLast(c *cli.Context) error {
sourceFile := getDeploymentHistoryFile()
oldDeployments, err := readDeployments(sourceFile)
if err != nil {
fmt.Printf("Failed to obtain deployment history: %s", err)
os.Exit(1)
}
currentDir, err := os.Getwd()
if err != nil {
fmt.Printf("Failed to get current working directory: %s", err)
os.Exit(1)
}
prevDeployment, ok := oldDeployments[currentDir]
if !ok {
fmt.Println("No previous deployment known for this directory")
os.Exit(1)
}
var command string
if len(prevDeployment.Timeout) > 0 {
command = fmt.Sprintf("spawnctl -e %s deploy -u %s -c %s -n %s -t %s", prevDeployment.BW2Entity,
prevDeployment.URI, prevDeployment.Configuration, prevDeployment.Name, prevDeployment.Timeout)
} else {
command = fmt.Sprintf("spawnctl -e %s deploy -u %s -c %s -n %s", prevDeployment.BW2Entity,
prevDeployment.URI, prevDeployment.Configuration, prevDeployment.Name)
}
proceed := c.Bool("yes")
if !proceed {
fmt.Println("This will run:")
fmt.Printf("\t%s\n", command)
fmt.Println("Proceed? [Y/n]")
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
proceed = (input == "y\n" || input == "Y\n" || input == "\n")
}
if proceed {
flags := flag.NewFlagSet("spawnctl", 0)
flags.String("uri", prevDeployment.URI, "")
flags.String("configuration", prevDeployment.Configuration, "")
flags.String("name", prevDeployment.Name, "")
flags.String("timeout", prevDeployment.Timeout, "")
c.GlobalSet("entity", prevDeployment.BW2Entity)
cliCtxt := cli.NewContext(c.App, flags, c)
return actionDeploy(cliCtxt)
}
return nil
}
func getDeploymentHistoryFile() string {
histFile := os.Getenv(deploymentHistVar)
//Environment variable not set, fall back to default
if len(histFile) == 0 {
histFile = os.Getenv("HOME") + string(os.PathSeparator) + ".spawnpoint_history"
}
return histFile
}
func saveDeployment(directory string, dep deployment, fileName string) error {
oldDeployments, err := readDeployments(fileName)
if err != nil {
return errors.Wrap(err, "Failed to read old deployments")
}
oldDeployments[directory] = dep
if err = writeDeployments(oldDeployments, fileName); err != nil {
return errors.Wrap(err, "Failed to write new deployment record")
}
return nil
}
func readDeployments(fileName string) (map[string]deployment, error) {
var oldDeployments map[string]deployment
reader, err := os.Open(fileName)
if err != nil {
if !os.IsNotExist(err) {
return nil, errors.Wrap(err, "Failed to open history file")
}
oldDeployments = make(map[string]deployment)
return oldDeployments, nil
}
defer reader.Close()
decoder := gob.NewDecoder(reader)
if err = decoder.Decode(&oldDeployments); err != nil {
return nil, err
}
return oldDeployments, nil
}
func writeDeployments(deployments map[string]deployment, fileName string) error {
writer, err := os.Create(fileName)
if err != nil {
return errors.Wrap(err, "Failed to create history file")
}
defer writer.Close()
encoder := gob.NewEncoder(writer)
if err = encoder.Encode(deployments); err != nil {
return errors.Wrap(err, "Failed to encode deployments")
}
return nil
}