Skip to content

Commit

Permalink
Update extract function name, update tests, add error checks
Browse files Browse the repository at this point in the history
  • Loading branch information
bobheadxi committed May 15, 2018
1 parent 195130d commit f2b87ee
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
13 changes: 4 additions & 9 deletions client/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

log "github.com/sirupsen/logrus"
"github.com/ubclaunchpad/inertia/common"
)

// RemoteVPS contains parameters for the VPS
Expand Down Expand Up @@ -41,8 +40,8 @@ func (remote *RemoteVPS) GetIPAndPort() string {
// by installing docker, starting the daemon and building a
// public-private key-pair. It outputs configuration information
// for the user.
func (remote *RemoteVPS) Bootstrap(runner SSHSession, name string, config *Config) error {
println("Setting up remote \"" + name + "\" at " + remote.IP)
func (remote *RemoteVPS) Bootstrap(runner SSHSession, repoName string, config *Config) error {
println("Setting up remote \"" + remote.Name + "\" at " + remote.IP)

println(">> Step 1/4: Installing docker...")
err := remote.installDocker(runner)
Expand Down Expand Up @@ -83,14 +82,10 @@ func (remote *RemoteVPS) Bootstrap(runner SSHSession, name string, config *Confi

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", name)
fmt.Printf("Use 'inertia %s logs --stream' to check on the daemon's setup progress.\n\n", remote.Name)

println("=============================\n")

repo, err := common.GetLocalRepo()
origin, err := repo.Remote("origin")
repoName := common.Extract(common.GetSSHRemoteURL(origin.Config().URLs[0]))

// Output deploy key to user.
println(">> GitHub Deploy Key (add to https://www.github.com/" + repoName + "/settings/keys/new): ")
println(pub.String())
Expand All @@ -105,7 +100,7 @@ be able to verify.` + "\n")

println(`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", name)
fmt.Printf("Then run 'inertia %s up' to deploy your application.\n", remote.Name)

return nil
}
Expand Down
5 changes: 3 additions & 2 deletions client/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func getTestConfig(writer io.Writer) *Config {

func getTestRemote() *RemoteVPS {
remote := &RemoteVPS{
Name: "gcloud",
IP: "127.0.0.1",
PEM: "../test/keys/id_rsa",
User: "root",
Expand Down Expand Up @@ -100,7 +101,7 @@ func TestBootstrap(t *testing.T) {

var writer bytes.Buffer
session := mockSSHRunner{r: remote}
err = remote.Bootstrap(&session, "gcloud", getTestConfig(&writer))
err = remote.Bootstrap(&session, "ubclaunchpad/inertia", getTestConfig(&writer))
assert.Nil(t, err)

// Make sure all commands are formatted correctly
Expand All @@ -118,7 +119,7 @@ func TestBootstrapIntegration(t *testing.T) {
remote := getTestRemote()
session := &SSHRunner{r: remote}
var writer bytes.Buffer
err := remote.Bootstrap(session, "testvps", getTestConfig(&writer))
err := remote.Bootstrap(session, "ubclaunchpad/inertia", getTestConfig(&writer))
assert.Nil(t, err)

// Daemon setup takes a bit of time - do a crude wait
Expand Down
11 changes: 7 additions & 4 deletions common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
"regexp"
"strings"
)

// GenerateRandomString creates a rand.Reader-generated
Expand Down Expand Up @@ -163,8 +163,11 @@ func BuildTar(dir string, outputs ...io.Writer) error {
})
}

// Extract gets the project name from its URL in the form [username]/[project]
func Extract(URL string) string {
r, _ := regexp.Compile(".com(/|:)(\\w+/\\w+)")
// ExtractRepository gets the project name from its URL in the form [username]/[project]
func ExtractRepository(URL string) string {
r, err := regexp.Compile(`.com(/|:)(\w+/\w+)`)
if err != nil {
return ""
}
return r.FindStringSubmatch(URL)[2]
}
2 changes: 1 addition & 1 deletion common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@ func TestFlushRoutine(t *testing.T) {

func TestExtract(t *testing.T) {
for _, url := range remoteURLVariations {
assert.Equal(t, "ubclaunchpad/inertia", Extract(url))
assert.Equal(t, "ubclaunchpad/inertia", ExtractRepository(url))
}
}
13 changes: 12 additions & 1 deletion deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,18 @@ for updates to this repository's remote master branch.`,
remote, found := config.GetRemote(remoteName)
if found {
session := client.NewSSHRunner(remote)
err = remote.Bootstrap(session, remoteName, config)

repo, err := common.GetLocalRepo()
if err != nil {
log.Fatal(err)
}
origin, err := repo.Remote("origin")
if err != nil {
log.Fatal(err)
}
repoName := common.ExtractRepository(common.GetSSHRemoteURL(origin.Config().URLs[0]))

err = remote.Bootstrap(session, repoName, config)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit f2b87ee

Please sign in to comment.