Skip to content

Commit

Permalink
Refactor configuration into package cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
bobheadxi committed Jun 16, 2018
1 parent ecf6753 commit c50e2fd
Show file tree
Hide file tree
Showing 19 changed files with 167 additions and 141 deletions.
5 changes: 5 additions & 0 deletions cfg/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Cfg

[![GoDoc](https://godoc.org/github.com/ubclaunchpad/inertia?status.svg)](https://godoc.org/github.com/ubclaunchpad/inertia/cfg)

This package contains Inertia's configuration types, structs, etc.
2 changes: 1 addition & 1 deletion client/config.go → cfg/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cfg

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion client/config_test.go → cfg/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cfg

import (
"bytes"
Expand Down
2 changes: 2 additions & 0 deletions cfg/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package cfg provides configuration structs and types
package cfg
29 changes: 29 additions & 0 deletions cfg/remote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cfg

// RemoteVPS contains parameters for the VPS
type RemoteVPS struct {
Name string `toml:"name"`
IP string `toml:"IP"`
User string `toml:"user"`
PEM string `toml:"pemfile"`
Branch string `toml:"branch"`
SSHPort string `toml:"ssh_port"`
Daemon *DaemonConfig `toml:"daemon"`
}

// DaemonConfig contains parameters for the Daemon
type DaemonConfig struct {
Port string `toml:"port"`
Token string `toml:"token"`
Secret string `toml:"secret"`
}

// GetHost creates the user@IP string.
func (remote *RemoteVPS) GetHost() string {
return remote.User + "@" + remote.IP
}

// GetIPAndPort creates the IP:Port string.
func (remote *RemoteVPS) GetIPAndPort() string {
return remote.IP + ":" + remote.Daemon.Port
}
23 changes: 23 additions & 0 deletions cfg/remote_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cfg

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRemoteVPS_GetHost(t *testing.T) {
remote := &RemoteVPS{
User: "bobheadxi",
IP: "127.0.0.1",
}
assert.Equal(t, "bobheadxi@127.0.0.1", remote.GetHost())
}

func TestRemoteVPS_GetIPAndPort(t *testing.T) {
remote := &RemoteVPS{
IP: "127.0.0.1",
Daemon: &DaemonConfig{Port: "4303"},
}
assert.Equal(t, "127.0.0.1:4303", remote.GetIPAndPort())
}
19 changes: 9 additions & 10 deletions client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@

[![GoDoc](https://godoc.org/github.com/ubclaunchpad/inertia?status.svg)](https://godoc.org/github.com/ubclaunchpad/inertia/client)

This package contains Inertia's clientside configuration and interface to remote Inertia daemons. It can be imported for use if you don't like the CLI - for example:
This package contains Inertia's clientside interface to remote Inertia daemons. It can be imported for use if you don't like the CLI - for example:

```go
package main

import "github.com/ubclaunchpad/inertia/client"
import (
"github.com/ubclaunchpad/inertia/cfg"
"github.com/ubclaunchpad/inertia/client"
)

func main() {
// Set up Inertia
config := client.NewConfig(
"0.3.0", "inertia-deploy-test", "docker-compose",
)

// Set up Inertia configuration
config := cfg.NewConfig("0.3.0", "inertia-deploy-test", /* ... */)

// Add your remote
config.AddRemote(&client.RemoteVPS{
Name: "gcloud", // ...params
})
config.AddRemote(&client.RemoteVPS{Name: "gcloud", /* ... */ })

// Set up client, remote, and deploy your project
cli, _ := client.NewClient("gcloud", config)
Expand Down
62 changes: 60 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"path"
"strings"

"github.com/gorilla/websocket"
"github.com/ubclaunchpad/inertia/cfg"
internal "github.com/ubclaunchpad/inertia/client/internal"
"github.com/ubclaunchpad/inertia/common"
)

// Client manages a deployment
type Client struct {
*RemoteVPS
*cfg.RemoteVPS
version string
project string
buildType string
Expand All @@ -28,7 +30,7 @@ type Client struct {

// NewClient sets up a client to communicate to the daemon at
// the given named remote.
func NewClient(remoteName string, config *Config) (*Client, bool) {
func NewClient(remoteName string, config *cfg.Config) (*Client, bool) {
remote, found := config.GetRemote(remoteName)
if !found {
return nil, false
Expand Down Expand Up @@ -137,6 +139,62 @@ func (c *Client) DaemonDown() error {
return nil
}

// installDocker installs docker on a remote vps.
func (c *Client) installDocker(session SSHSession) error {
installDockerSh, err := internal.Asset("client/scripts/docker.sh")
if err != nil {
return err
}

// Install docker.
cmdStr := string(installDockerSh)
_, stderr, err := session.Run(cmdStr)
if err != nil {
println(stderr.String())
return err
}

return nil
}

// keyGen creates a public-private key-pair on the remote vps
// and returns the public key.
func (c *Client) keyGen(session SSHSession) (*bytes.Buffer, error) {
scriptBytes, err := internal.Asset("client/scripts/keygen.sh")
if err != nil {
return nil, err
}

// Create deploy key.
result, stderr, err := session.Run(string(scriptBytes))

if err != nil {
log.Println(stderr.String())
return nil, err
}

return result, nil
}

// getDaemonAPIToken returns the daemon API token for RESTful access
// to the daemon.
func (c *Client) getDaemonAPIToken(session SSHSession, daemonVersion string) (string, error) {
scriptBytes, err := internal.Asset("client/scripts/token.sh")
if err != nil {
return "", err
}
daemonCmdStr := fmt.Sprintf(string(scriptBytes), daemonVersion)

stdout, stderr, err := session.Run(daemonCmdStr)
if err != nil {
log.Println(stderr.String())
return "", err
}

// There may be a newline, remove it.
return strings.TrimSuffix(stdout.String(), "\n"), nil
}

// Up brings the project up on the remote VPS instance specified
// in the deployment object.
func (c *Client) Up(gitRemoteURL, buildType string, stream bool) (*http.Response, error) {
Expand Down
17 changes: 9 additions & 8 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/ubclaunchpad/inertia/cfg"
"github.com/ubclaunchpad/inertia/common"
)

Expand All @@ -33,11 +34,11 @@ func getMockClient(ts *httptest.Server) *Client {
port = "8080"
}

mockRemote := &RemoteVPS{
mockRemote := &cfg.RemoteVPS{
User: "",
IP: url,
PEM: "",
Daemon: &DaemonConfig{
Daemon: &cfg.DaemonConfig{
Port: port,
Secret: "arjan",
Token: fakeAuth,
Expand All @@ -51,12 +52,12 @@ func getMockClient(ts *httptest.Server) *Client {
}

func getIntegrationClient(mockRunner *mockSSHRunner) *Client {
remote := &RemoteVPS{
remote := &cfg.RemoteVPS{
IP: "127.0.0.1",
PEM: "../test/keys/id_rsa",
User: "root",
SSHPort: "69",
Daemon: &DaemonConfig{
Daemon: &cfg.DaemonConfig{
Port: "4303",
},
}
Expand All @@ -76,18 +77,18 @@ func getIntegrationClient(mockRunner *mockSSHRunner) *Client {
}

func TestGetNewClient(t *testing.T) {
config := &Config{
config := &cfg.Config{
Version: "test",
Project: "robert-writes-bad-code",
Remotes: make([]*RemoteVPS, 0),
Remotes: make([]*cfg.RemoteVPS, 0),
}
testRemote := &RemoteVPS{
testRemote := &cfg.RemoteVPS{
Name: "test",
IP: "12343",
User: "bobheadxi",
PEM: "/some/pem/file",
SSHPort: "22",
Daemon: &DaemonConfig{
Daemon: &cfg.DaemonConfig{
Port: "8080",
},
}
Expand Down
94 changes: 0 additions & 94 deletions client/remote.go

This file was deleted.

3 changes: 2 additions & 1 deletion client/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path"

"github.com/ubclaunchpad/inertia/cfg"
"golang.org/x/crypto/ssh"
)

Expand All @@ -29,7 +30,7 @@ type SSHRunner struct {
}

// NewSSHRunner returns a new SSHRunner
func NewSSHRunner(r *RemoteVPS) *SSHRunner {
func NewSSHRunner(r *cfg.RemoteVPS) *SSHRunner {
if r != nil {
return &SSHRunner{
pem: r.PEM,
Expand Down
4 changes: 3 additions & 1 deletion client/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package client
import (
"bytes"
"io"

"github.com/ubclaunchpad/inertia/cfg"
)

// mockSSHRunner is a mocked out implementation of SSHSession
type mockSSHRunner struct {
r *RemoteVPS
r *cfg.RemoteVPS
Calls []string
}

Expand Down
Loading

0 comments on commit c50e2fd

Please sign in to comment.