/
remote.go
165 lines (145 loc) · 4.46 KB
/
remote.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"errors"
"fmt"
"os"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/ubclaunchpad/inertia/client"
"github.com/ubclaunchpad/inertia/common"
)
var (
errInvalidUser = errors.New("invalid user")
errInvalidAddress = errors.New("invalid IP address")
errInvalidSecret = errors.New("invalid secret")
)
var remoteCmd = &cobra.Command{
Use: "remote",
Short: "Configure the local settings for a remote VPS instance",
Long: `Remote is a low level command for interacting with this VPS
over SSH. Provides functionality such as adding new remotes, removing remotes,
bootstrapping the server for deployment, running install scripts such as
installing docker, starting the Inertia daemon and other low level configuration
of the VPS. Must run 'inertia init' in your repository before using.
Example:
inerta remote add gcloud
inerta gcloud init
inerta remote status gcloud`,
Args: cobra.MinimumNArgs(1),
}
var addCmd = &cobra.Command{
Use: "add [REMOTE]",
Short: "Add a reference to a remote VPS instance",
Long: `Add a reference to a remote VPS instance. Requires
information about the VPS including IP address, user and a PEM
file. Specify a VPS name.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Ensure project initialized.
config, err := client.GetProjectConfigFromDisk()
if err != nil {
log.Fatal(err)
}
_, found := config.GetRemote(args[0])
if found {
log.Fatal(errors.New("Remote " + args[0] + " already exists."))
}
port, _ := cmd.Flags().GetString("port")
sshPort, _ := cmd.Flags().GetString("sshPort")
repo, err := common.GetLocalRepo()
if err != nil {
log.Fatal(err)
}
head, err := repo.Head()
if err != nil {
log.Fatal(err)
}
branch := head.Name().Short()
err = addRemoteWalkthrough(os.Stdin, args[0], port, sshPort, branch, config)
if err != nil {
log.Fatal(err)
}
fmt.Println("\nRemote '" + args[0] + "' has been added!")
fmt.Println("You can now run 'inertia " + args[0] + " init' to set this remote up")
fmt.Println("for continuous deployment.")
},
}
var listCmd = &cobra.Command{
Use: "ls",
Short: "List currently configured remotes",
Long: `Lists all currently configured remotes.`,
Run: func(cmd *cobra.Command, args []string) {
verbose, _ := cmd.Flags().GetBool("verbose")
config, err := client.GetProjectConfigFromDisk()
if err != nil {
log.Fatal(err)
}
for _, remote := range config.Remotes {
if verbose {
printRemoteDetails(remote)
} else {
fmt.Println(remote.Name)
}
}
},
}
var removeCmd = &cobra.Command{
Use: "rm [REMOTE]",
Short: "Remove a remote.",
Long: `Remove a remote from Inertia's configuration file.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
config, err := client.GetProjectConfigFromDisk()
if err != nil {
log.Fatal(err)
}
_, found := config.GetRemote(args[0])
if found {
config.RemoveRemote(args[0])
err = config.Write()
if err != nil {
log.Fatal("Failed to remove remote: " + err.Error())
}
fmt.Println("Remote " + args[0] + " removed.")
} else {
log.Fatal(errors.New("There does not appear to be a remote with this name. Have you modified the Inertia configuration file?"))
}
},
}
var showCmd = &cobra.Command{
Use: "show [REMOTE]",
Short: "Show details about remote.",
Long: `Show details about the given remote.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Ensure project initialized.
config, err := client.GetProjectConfigFromDisk()
if err != nil {
log.Fatal(err)
}
remote, found := config.GetRemote(args[0])
if found {
printRemoteDetails(remote)
} else {
println("No remote '" + args[0] + "' currently set up.")
}
},
}
func printRemoteDetails(remote *client.RemoteVPS) {
fmt.Printf("Remote %s: \n", remote.Name)
fmt.Printf(" - Deployed Branch: %s\n", remote.Branch)
fmt.Printf(" - IP Address: %s\n", remote.IP)
fmt.Printf(" - VPS User: %s\n", remote.User)
fmt.Printf(" - PEM File Location: %s\n", remote.PEM)
fmt.Printf("Run 'inertia %s status' for more details.\n", remote.Name)
}
func init() {
rootCmd.AddCommand(remoteCmd)
remoteCmd.AddCommand(addCmd)
remoteCmd.AddCommand(listCmd)
remoteCmd.AddCommand(removeCmd)
remoteCmd.AddCommand(showCmd)
listCmd.Flags().BoolP("verbose", "v", false, "Verbose output")
addCmd.Flags().StringP("port", "p", "8081", "Daemon port")
addCmd.Flags().StringP("sshPort", "s", "22", "SSH port")
}