/
remote.go
273 lines (240 loc) · 7.55 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/ubclaunchpad/inertia/client"
"github.com/ubclaunchpad/inertia/common"
"github.com/ubclaunchpad/inertia/daemon"
)
var (
errInvalidUser = errors.New("invalid user")
errInvalidAddress = errors.New("invalid IP address")
)
// remoteCmd represents the remote command
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),
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.")
}
},
}
// addCmd represents the remote add command
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, client.AddNewRemote)
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.")
},
}
// addRemoteWalkthough is the walkthrough that asks users for RemoteVPS details
func addRemoteWalkthrough(in io.Reader, name, port, sshPort, currBranch string, addRemote func(*client.RemoteVPS) error) error {
homeEnvVar := os.Getenv("HOME")
sshDir := filepath.Join(homeEnvVar, ".ssh")
defaultSSHLoc := filepath.Join(sshDir, "id_rsa")
var response string
fmt.Println("Enter location of PEM file (leave blank to use '" + defaultSSHLoc + "'):")
_, err := fmt.Fscanln(in, &response)
if err != nil {
response = defaultSSHLoc
}
pemLoc := response
fmt.Println("Enter user:")
n, err := fmt.Fscanln(in, &response)
if err != nil || n == 0 {
return errInvalidUser
}
user := response
fmt.Println("Enter IP address of remote:")
n, err = fmt.Fscanln(in, &response)
if err != nil || n == 0 {
return errInvalidAddress
}
address := response
branch := currBranch
fmt.Println("Enter project branch to deploy (leave blank for current branch):")
n, err = fmt.Fscanln(in, &response)
if err == nil && n != 0 {
branch = response
}
fmt.Println("\nPort " + port + " will be used as the daemon port.")
fmt.Println("Port " + sshPort + " will be used as the SSH port.")
fmt.Println("Run 'inertia remote add' with the -p flag to set a custom Daemon port")
fmt.Println("of the -ssh flag to set a custom SSH port.")
return addRemote(&client.RemoteVPS{
Name: name,
IP: address,
User: user,
PEM: pemLoc,
Branch: branch,
Daemon: &client.DaemonConfig{
Port: port,
SSHPort: sshPort,
},
})
}
// statusCmd represents the remote status command
var statusCmd = &cobra.Command{
Use: "status [REMOTE]",
Short: "Query the status of a remote instance",
Long: `Query the remote VPS for connectivity, daemon
behaviour, and other information.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
config, err := client.GetProjectConfigFromDisk()
if err != nil {
log.WithError(err)
}
remote, found := config.GetRemote(args[0])
if !found {
println("No such remote " + args[0])
println("Inertia currently supports one remote per repository")
println("Run `inertia remote -v' to see what remote is available")
os.Exit(1)
}
host := "http://" + remote.GetIPAndPort()
resp, err := http.Get(host)
if err != nil {
println("Could not connect to daemon")
println("Try running inertia [REMOTE] init")
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
println("Bad response from daemon")
println("Try running inertia [REMOTE] init")
return
}
if string(body) != common.DaemonOkResp {
println("Could not connect to daemon")
println("Try running inertia [REMOTE] init")
return
}
fmt.Printf("Remote instance '%s' accepting requests at %s\n",
args[0], host)
},
}
// listCmd represents the inertia list command
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)
}
}
},
}
// removeCmd represents the inertia list command
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?"))
}
},
}
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)
}
func init() {
rootCmd.AddCommand(remoteCmd)
remoteCmd.AddCommand(addCmd)
remoteCmd.AddCommand(statusCmd)
remoteCmd.AddCommand(listCmd)
remoteCmd.AddCommand(removeCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// remoteCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
listCmd.Flags().BoolP("verbose", "v", false, "Verbose output")
addCmd.Flags().StringP("port", "p", daemon.DefaultPort, "Daemon port")
addCmd.Flags().StringP("sshPort", "s", "22", "SSH port")
}