Skip to content

Commit

Permalink
Merge bacba3e into dea6f25
Browse files Browse the repository at this point in the history
  • Loading branch information
bobheadxi committed Jul 1, 2018
2 parents dea6f25 + bacba3e commit 4b1cf06
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 88 deletions.
62 changes: 34 additions & 28 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"path"
Expand All @@ -26,25 +25,36 @@ type Client struct {
buildType string
buildFilePath string

out io.Writer

sshRunner SSHSession
verifySSL bool
}

// NewClient sets up a client to communicate to the daemon at
// the given named remote.
func NewClient(remoteName string, config *cfg.Config) (*Client, bool) {
func NewClient(remoteName string, config *cfg.Config, out ...io.Writer) (*Client, bool) {
remote, found := config.GetRemote(remoteName)
if !found {
return nil, false
}

var writer io.Writer
if len(out) > 0 {
writer = out[0]
} else {
writer = common.DevNull{}
}

return &Client{
RemoteVPS: remote,
version: config.Version,
project: config.Project,
buildType: config.BuildType,
buildFilePath: config.BuildFilePath,
sshRunner: NewSSHRunner(remote),

out: writer,
}, true
}

Expand All @@ -59,15 +69,15 @@ func (c *Client) SetSSLVerification(verify bool) {
// public-private key-pair. It outputs configuration information
// for the user.
func (c *Client) BootstrapRemote(repoName string) error {
println("Setting up remote \"" + c.Name + "\" at " + c.IP)
fmt.Fprintf(c.out, "Setting up remote %s at %s", c.Name, c.IP)

println(">> Step 1/4: Installing docker...")
fmt.Fprint(c.out, ">> Step 1/4: Installing docker...")
err := c.installDocker(c.sshRunner)
if err != nil {
return err
}

println("\n>> Step 2/4: Building deploy key...")
fmt.Fprint(c.out, "\n>> Step 2/4: Building deploy key...")
if err != nil {
return err
}
Expand All @@ -78,7 +88,7 @@ func (c *Client) BootstrapRemote(repoName string) error {

// This step needs to run before any other commands that rely on
// the daemon image, since the daemon is loaded here.
println("\n>> Step 3/4: Starting daemon...")
fmt.Fprint(c.out, "\n>> Step 3/4: Starting daemon...")
if err != nil {
return err
}
Expand All @@ -87,34 +97,34 @@ func (c *Client) BootstrapRemote(repoName string) error {
return err
}

println("\n>> Step 4/4: Fetching daemon API token...")
fmt.Fprint(c.out, "\n>> Step 4/4: Fetching daemon API token...")
token, err := c.getDaemonAPIToken(c.sshRunner, c.version)
if err != nil {
return err
}
c.Daemon.Token = token

println("\nInertia has been set up and daemon is running on remote!")
println("You may have to wait briefly for Inertia to set up some dependencies.")
fmt.Printf("Use 'inertia %s logs --stream' to check on the daemon's setup progress.\n\n", c.Name)
fmt.Fprint(c.out, "\nInertia has been set up and daemon is running on remote!")
fmt.Fprint(c.out, "You may have to wait briefly for Inertia to set up some dependencies.")
fmt.Fprintf(c.out, "Use 'inertia %s logs --stream' to check on the daemon's setup progress.\n\n", c.Name)

println("=============================\n")
fmt.Fprint(c.out, "=============================\n")

// Output deploy key to user.
println(">> GitHub Deploy Key (add to https://www.github.com/" + repoName + "/settings/keys/new): ")
println(pub.String())
fmt.Fprintf(c.out, ">> GitHub Deploy Key (add to https://www.github.com/%s/settings/keys/new): ", repoName)
fmt.Fprint(c.out, pub.String())

// Output Webhook url to user.
println(">> GitHub WebHook URL (add to https://www.github.com/" + repoName + "/settings/hooks/new): ")
println("WebHook Address: https://" + c.IP + ":" + c.Daemon.Port + "/webhook")
println("WebHook Secret: " + c.Daemon.WebHookSecret)
println(`Note that you will have to disable SSH verification in your webhook
fmt.Fprintf(c.out, ">> GitHub WebHook URL (add to https://www.github.com/%s/settings/hooks/new): ", repoName)
fmt.Fprintf(c.out, "WebHook Address: https://%s:%s/webhook", c.IP, c.Daemon.Port)
fmt.Fprint(c.out, "WebHook Secret: "+c.Daemon.WebHookSecret)
fmt.Fprint(c.out, `Note that you will have to disable SSH verification in your webhook
settings - Inertia uses self-signed certificates that GitHub won't
be able to verify.` + "\n")
be able to verify.`+"\n")

println(`Inertia daemon successfully deployed! Add your webhook url and deploy
fmt.Fprint(c.out, `Inertia daemon successfully deployed! Add your webhook url and deploy
key to enable continuous deployment.`)
fmt.Printf("Then run 'inertia %s up' to deploy your application.\n", c.Name)
fmt.Fprintf(c.out, "Then run 'inertia %s up' to deploy your application.\n", c.Name)

return nil
}
Expand All @@ -140,8 +150,7 @@ func (c *Client) DaemonDown() error {

_, stderr, err := c.sshRunner.Run(string(scriptBytes))
if err != nil {
println(stderr.String())
return err
return fmt.Errorf("daemon shutdown failed: %s: %s", err.Error(), stderr.String())
}

return nil
Expand All @@ -158,8 +167,7 @@ func (c *Client) installDocker(session SSHSession) error {
cmdStr := string(installDockerSh)
_, stderr, err := session.Run(cmdStr)
if err != nil {
println(stderr.String())
return err
return fmt.Errorf("docker installation: %s: %s", err.Error(), stderr.String())
}

return nil
Expand All @@ -177,8 +185,7 @@ func (c *Client) keyGen(session SSHSession) (*bytes.Buffer, error) {
result, stderr, err := session.Run(string(scriptBytes))

if err != nil {
log.Println(stderr.String())
return nil, err
return nil, fmt.Errorf("key generation failed: %s: %s", err.Error(), stderr.String())
}

return result, nil
Expand All @@ -195,8 +202,7 @@ func (c *Client) getDaemonAPIToken(session SSHSession, daemonVersion string) (st

stdout, stderr, err := session.Run(daemonCmdStr)
if err != nil {
log.Println(stderr.String())
return "", err
return "", fmt.Errorf("api token generation failed: %s: %s", err.Error(), stderr.String())
}

// There may be a newline, remove it.
Expand Down
4 changes: 4 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -47,6 +48,7 @@ func getMockClient(ts *httptest.Server) *Client {

return &Client{
RemoteVPS: mockRemote,
out: os.Stdout,
project: "test_project",
}
}
Expand All @@ -66,12 +68,14 @@ func getIntegrationClient(mockRunner *mockSSHRunner) *Client {
return &Client{
version: "test",
RemoteVPS: remote,
out: os.Stdout,
sshRunner: mockRunner,
}
}
return &Client{
version: "test",
RemoteVPS: remote,
out: os.Stdout,
sshRunner: NewSSHRunner(remote),
}
}
Expand Down
41 changes: 17 additions & 24 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -138,7 +137,7 @@ var cmdDeploymentUp = &cobra.Command{
to be active on your remote - do this by running 'inertia [REMOTE] init'`,
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
deployment, _, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -202,7 +201,7 @@ var cmdDeploymentDown = &cobra.Command{
Requires project to be online - do this by running 'inertia [REMOTE] up`,
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
deployment, _, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -239,7 +238,7 @@ var cmdDeploymentStatus = &cobra.Command{
running 'inertia [REMOTE] up'`,
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
deployment, _, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -286,7 +285,7 @@ var cmdDeploymentLogs = &cobra.Command{
status' to see what containers are accessible.`,
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
deployment, _, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -346,7 +345,7 @@ var cmdDeploymentSSH = &cobra.Command{
Long: `Starts up an interact SSH session with your remote.`,
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, configFilePath)
deployment, _, err := local.GetClient(remoteName, configFilePath)
if err != nil {
log.Fatal(err)
}
Expand All @@ -366,7 +365,7 @@ deployment. Provide a relative path to your file.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
deployment, _, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -422,27 +421,21 @@ request access to the repository via a public key, and will listen
for updates to this repository's remote master branch.`,
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Use, " ")[0]
cli, write, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}

// Bootstrap needs to write to configuration.
config, path, err := local.GetProjectConfigFromDisk(configFilePath)
url, err := local.GetRepoRemote("origin")
if err != nil {
log.Fatal(err)
}
cli, found := client.NewClient(remoteName, config)
if found {
url, err := local.GetRepoRemote("origin")
if err != nil {
log.Fatal(err)
}
repoName := common.ExtractRepository(common.GetSSHRemoteURL(url))
err = cli.BootstrapRemote(repoName)
if err != nil {
log.Fatal(err)
}
config.Write(path)
} else {
log.Fatal(errors.New("There does not appear to be a remote with this name. Have you modified the Inertia configuration file?"))
repoName := common.ExtractRepository(common.GetSSHRemoteURL(url))
err = cli.BootstrapRemote(repoName)
if err != nil {
log.Fatal(err)
}
write()
},
}

Expand All @@ -456,7 +449,7 @@ remote. Requires Inertia daemon to be active on your remote - do this by
running 'inertia [REMOTE] init'`,
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
deployment, _, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ variables are applied to all deployed containers.`,
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
deployment, _, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -55,7 +55,7 @@ and persistent environment storage.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
deployment, _, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand All @@ -79,7 +79,7 @@ var cmdDeploymentEnvList = &cobra.Command{
Short: "List currently set and saved environment variables",
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
deployment, _, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ var cmdProvisionECS = &cobra.Command{
if err != nil {
log.Fatal(err)
}
prov, err = provision.NewEC2Provisioner(id, key)
prov, err = provision.NewEC2Provisioner(id, key, os.Stdout)
} else {
prov, err = provision.NewEC2ProvisionerFromEnv()
prov, err = provision.NewEC2ProvisionerFromEnv(os.Stdout)
}
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -137,7 +137,7 @@ var cmdProvisionECS = &cobra.Command{
config.Write(path)

// Create inertia client
inertia, found := client.NewClient(args[0], config)
inertia, found := client.NewClient(args[0], config, os.Stdout)
if !found {
log.Fatal("vps setup did not complete properly")
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Use the --admin flag to create an admin user.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
deployment, _, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -80,7 +80,7 @@ deployment from the web app.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
deployment, _, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -117,7 +117,7 @@ from the web app.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
deployment, _, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -151,7 +151,7 @@ var cmdDeploymentListUsers = &cobra.Command{
Long: `List all users with access to Inertia Web on your remote.`,
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Parent().Use, " ")[0]
deployment, err := local.GetClient(remoteName, configFilePath, cmd)
deployment, _, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 4b1cf06

Please sign in to comment.