Skip to content

Commit

Permalink
Merge 777143d into 8d64174
Browse files Browse the repository at this point in the history
  • Loading branch information
bobheadxi committed Feb 4, 2018
2 parents 8d64174 + 777143d commit 2af2754
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 26 deletions.
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: test test-verbose test-profile test-race clean docker bootstrap
.PHONY: test test-verbose test-profile test-env clean docker bootstrap

PACKAGES = `go list ./... | grep -v vendor/`

Expand All @@ -13,8 +13,10 @@ test:
test-verbose:
go test $(PACKAGES) -v --cover

test-race:
go test $(PACKAGES) -race --cover
test-env:
docker build -t sshvps -f ./test_env/Dockerfile.sshvps ./test_env
docker run --rm -d -p 22:22 -p 8081:8081 --name testvps --privileged sshvps
bash ./test_env/info.sh

clean: inertia
rm -f inertia
Expand Down
12 changes: 6 additions & 6 deletions client/bootstrap.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions client/bootstrap/docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ if !(hash docker 2>/dev/null); then
sh $DOCKER_DEST
fi
fi

sudo service docker start
30 changes: 16 additions & 14 deletions client/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import (
"golang.org/x/crypto/ssh"
)

//
// RemoteVPS contains parameters for the VPS
type RemoteVPS struct {
User string
IP string
PEM string
Port string
User string
IP string
SSHPort string
PEM string
Port string
}

// SSHSession can run remote commands over SSH
Expand All @@ -50,7 +51,7 @@ func NewSSHRunner(r *RemoteVPS) *SSHRunner {

// Run runs a command remotely.
func (runner *SSHRunner) Run(cmd string) (*bytes.Buffer, *bytes.Buffer, error) {
session, err := getSSHSession(runner.r.PEM, runner.r.IP, runner.r.User)
session, err := getSSHSession(runner.r.PEM, runner.r.IP, runner.r.SSHPort, runner.r.User)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -152,7 +153,7 @@ func (remote *RemoteVPS) InstallDocker(session SSHSession) error {
cmdStr := string(installDockerSh)
_, stderr, err := remote.RunSSHCommand(session, cmdStr)
if err != nil {
println(stderr)
println(stderr.String())
return err
}

Expand Down Expand Up @@ -235,7 +236,7 @@ func (remote *RemoteVPS) GetDaemonAPIToken(session SSHSession) (string, error) {
}

// AddNewRemote adds a new remote to the project config file.
func AddNewRemote(name, IP, user, pemLoc, port string) error {
func AddNewRemote(name, IP, sshPort, user, pemLoc, port string) error {
// Just wipe configuration for MVP.
config, err := GetProjectConfigFromDisk()
if err != nil {
Expand All @@ -244,10 +245,11 @@ func AddNewRemote(name, IP, user, pemLoc, port string) error {

config.CurrentRemoteName = name
config.CurrentRemoteVPS = &RemoteVPS{
IP: IP,
User: user,
PEM: pemLoc,
Port: port,
IP: IP,
SSHPort: sshPort,
User: user,
PEM: pemLoc,
Port: port,
}

_, err = config.Write()
Expand All @@ -259,7 +261,7 @@ func AddNewRemote(name, IP, user, pemLoc, port string) error {
}

// Stubbed out for testing.
func getSSHSession(PEM, IP, user string) (*ssh.Session, error) {
func getSSHSession(PEM, IP, sshPort, user string) (*ssh.Session, error) {
privateKey, err := ioutil.ReadFile(PEM)
if err != nil {
return nil, err
Expand All @@ -270,7 +272,7 @@ func getSSHSession(PEM, IP, user string) (*ssh.Session, error) {
return nil, err
}

client, err := ssh.Dial("tcp", IP+":22", cfg)
client, err := ssh.Dial("tcp", IP+":"+sshPort, cfg)
if err != nil {
return nil, err
}
Expand Down
14 changes: 11 additions & 3 deletions cmd/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -96,16 +97,23 @@ file. Specify a VPS name.`,
}
user := response

fmt.Println("Enter IP address of remote:")
fmt.Println("Enter IP address of remote (include SSH port if not standard '22'):")
_, err = fmt.Scanln(&response)
if err != nil {
log.Fatal("That is not a valid IP address - please try again.")
}
address := response
sshPort := "22"
if strings.Contains(address, ":") {
addressComponents := strings.Split(address, ":")
address = addressComponents[0]
sshPort = addressComponents[1]
}

fmt.Println("Port " + port + " will be used as the daemon port.")
fmt.Println("Run this 'inertia remote add' with the -p flag to set a custom port.")
fmt.Println("Run 'inertia remote add' with the -p flag to set a custom port.")

err = client.AddNewRemote(args[0], address, user, pemLoc, port)
err = client.AddNewRemote(args[0], address, sshPort, user, pemLoc, port)
if err != nil {
log.WithError(err)
}
Expand Down
22 changes: 22 additions & 0 deletions test_env/Dockerfile.sshvps
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM ubuntu:16.04

RUN apt-get update && apt-get install -y sudo && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:inertia' | chpasswd

RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN echo "AuthorizedKeysFile %h/.ssh/authorized_keys" >> /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

RUN mkdir $HOME/.ssh/
COPY . .
RUN cat test_key.pub >> $HOME/.ssh/authorized_keys

EXPOSE 22 8081
CMD ["/usr/sbin/sshd", "-D"]
5 changes: 5 additions & 0 deletions test_env/info.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Script for neatly outputting information about the test VPS

echo ""
echo "Test VPS is online (kill using 'docker kill testvps')"
echo "Test key:" $GOPATH/src/github.com/ubclaunchpad/inertia/test_env/test_key
27 changes: 27 additions & 0 deletions test_env/test_key
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAw+14SQTAidfYPDizCYPv0gWq4+wFeInCrZGo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ZQrTQp7wKejvGlZSsMddRD4qXxnjpvw8iiISkVXufus3GyK08Vz9ph
uiqQraFXVekB7/P1BUE/Ds4PsO/s8J3CGgGYrXllKtopyzO42D4iTIp3G0TO+ILM
vF/VNwvdTZ0cwz7qfGmQX7kCgYAJqOOpvGeSm0IGwPFLCihkBPudrK+IA0BPzmGN
z5BSn51zZ5jj2jza1jUcRVi8yC4EukXcW17pD1vayWrTAhwFF9mhHqJVZazvn91d
+bFjwNAqKjtgsW76DONuYnSuxoHzoLb2CEbbHe+0M3Jb+MEUjsxmOSvG789SG+JT
K/i/OQKBgQD3rq8dDSVYaLcSFwg9RfRKF+Ahtml86lm4FrfZlLEfwb6TaR/Unsh0
XF56ZdrKh0nbOW/125RSc8STCv5klDGnBCD56Qzbin9+W6j1TWyJFMdNeaxjWK+U
lq07qdr3cY+O1F4otlDitNuhLE88dtGJM5lEyumokiH1yXwhbBtZ4w==
-----END RSA PRIVATE KEY-----
1 change: 1 addition & 0 deletions test_env/test_key.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDD7XhJBMCJ19g8OLMJg+/SBarj7AV4icKtkajgEVsxw/vGEf6G2abSrHty1hsJLS1CYK8Ke3hbmPCGof2/rn9vU9a/WpBtdsmrSLJ5KymCloHzGr4CRagGRQ5PyJpTMRIOAl8SkL2rWzt0BSmZEMPQkxXN2y/VVCqlHbZVa2elwW0CQ2TcIheIoVgnnrvt1VKtzjl2E7hHrsuC5I/Glc1FyDbr9wUpZA77LZK2dhUQRS2+hY3Pa0ZC7aF+0m2PPMIVdyQfxVMnwSBfT7cKjmNcRubWJ0UYWBwA/VZPfG4aH7CkJcwthV0XXxXEbNl7RvFK1n/5uGxph8UvJTyovtGd

0 comments on commit 2af2754

Please sign in to comment.