Skip to content

Commit

Permalink
Merge pull request #32 from Crazybus/box_me_in_real_virtual_life
Browse files Browse the repository at this point in the history
Add cmdProxy client to allow running commands on the host machine
  • Loading branch information
Crazybus authored Jul 19, 2018
2 parents eb2d9f8 + c209313 commit 82e1273
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 11 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ services:
- docker

script:
- go build
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
- ./lope -blacklist GO golang:1.10 /usr/local/go/bin/go run build/build.go

Expand Down
1 change: 1 addition & 0 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func checksum(goos string, goarch string) error {
func build(goos string, goarch string) error {
os.Setenv("GOOS", goos)
os.Setenv("GOARCH", goarch)
os.Setenv("CGO_ENABLED", "0")

cmd := exec.Command(
"/usr/local/go/bin/go",
Expand Down
6 changes: 6 additions & 0 deletions cmdProxy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Ignore everything
*

# But not these files...
!.gitignore
!cmdProxy.go
54 changes: 54 additions & 0 deletions cmdProxy/cmdProxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
)

type lopeCmd struct {
Command string `json:"command"`
Args []string `json:"args"`
}

func run(cmd string, args []string, url string) {

lopeCmd := lopeCmd{
Command: cmd,
Args: args,
}

b, err := json.Marshal(lopeCmd)
if err != nil {
fmt.Println(err)
return
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(b))
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()

body, _ := ioutil.ReadAll(resp.Body)
fmt.Print(string(body))
}

func main() {
cmd := filepath.Base(os.Args[0])
args := os.Args[1:]

url, ok := os.LookupEnv("LOPE_PROXY_ADDR")
if !ok {
fmt.Println("Please set the 'LOPE_PROXY_ADDR' environment variable")
os.Exit(1)
}
run(cmd, args, url)
}
4 changes: 2 additions & 2 deletions lope.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ func (l *lope) commandProxy() {

l.params = append(l.params, "--add-host=localhost:"+ip)

l.envs = append(l.envs, "LOPE_PROXY_ADDR="+ip+":"+l.cfg.cmdProxyPort)
l.envs = append(l.envs, "LOPE_PROXY_ADDR=http://"+ip+":"+l.cfg.cmdProxyPort)
}

func debug(message string) {
Expand Down Expand Up @@ -433,7 +433,7 @@ func main() {
pwd, _ := os.Getwd()

var blacklist string
flag.StringVar(&blacklist, "blacklist", "HOME,SSH_AUTH_SOCK,TMPDIR", "Comma seperated list of environment variables that will be ignored by lope")
flag.StringVar(&blacklist, "blacklist", "HOME,SSH_AUTH_SOCK,TMPDIR,PATH", "Comma seperated list of environment variables that will be ignored by lope")

var whitelist string
flag.StringVar(&whitelist, "whitelist", "", "Comma seperated list of environment variables that will be be included by lope")
Expand Down
42 changes: 40 additions & 2 deletions lope_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"os"
"os/exec"
"path/filepath"
"strings"
Expand All @@ -9,6 +10,31 @@ import (

var binaryName = filepath.FromSlash("./lope")

func init() {
cmd := exec.Command("go", "build")
err := cmd.Run()
if err != nil {
panic(err)
}
cmd = exec.Command("go", "build", "-o", "lope", "cmdProxy.go")
cmd.Env = os.Environ()
cmd.Env = append(
cmd.Env,
"GOOS=linux",
"GOARCH=amd64",
"CGO_ENABLED=0",
)

cmd.Dir = "cmdProxy"
err = cmd.Run()
if err != nil {
panic(err)
}
if err := os.Chmod(filepath.FromSlash("cmdProxy/lope"), 0755); err != nil {
panic(err)
}
}

func TestLopeCli(t *testing.T) {

var tests = []struct {
Expand Down Expand Up @@ -50,15 +76,27 @@ func TestLopeCli(t *testing.T) {
},
},
{
"Run a command via the command proxy",
"Run a command via the command proxy api",
[]string{
"-noTty",
"-cmdProxy",
"alpine",
"wget", "-q", "-O-",
`--post-data='{"command":"lope", "args": ["-noTty", "alpine", "ls"]}'`,
"--header=Content-Type:application/json",
"http://$LOPE_PROXY_ADDR",
"$LOPE_PROXY_ADDR",
},
[]string{
"README.md",
},
},
{
"Run a command via the command proxy cli",
[]string{
"-noTty",
"-cmdProxy",
"alpine",
"cmdProxy/lope", "-noTty", "alpine", "ls",
},
[]string{
"README.md",
Expand Down
13 changes: 7 additions & 6 deletions lope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"net/url"
"strings"
"testing"
)
Expand Down Expand Up @@ -530,18 +531,18 @@ func TestCommandProxy(t *testing.T) {
l.commandProxy()

got := ""
want := test.want
for _, e := range l.envs {
split := strings.Split(e, "=")
if split[0] == "LOPE_PROXY_ADDR" {
addr := split[1]
p := strings.Split(addr, ":")
if len(p) == 2 {
got = p[1]
addr, err := url.Parse(split[1])
if err != nil {
t.Errorf("got %q want %q", err, want)
}
got = addr.Port()

}
}
want := test.want

if got != want {
t.Errorf("got %q want %q", got, want)
}
Expand Down

0 comments on commit 82e1273

Please sign in to comment.