Skip to content

Commit

Permalink
Merge pull request #133 from marcusolsson/cli
Browse files Browse the repository at this point in the history
Add hermit cli
  • Loading branch information
alde authored Sep 14, 2016
2 parents 3b98ec7 + 6257277 commit 1a48691
Show file tree
Hide file tree
Showing 5 changed files with 546 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ Using [minimesos](https://www.minimesos.org/) is a very simple way to test and p
docker run -e MASTER=$MINIMESOS_ZOOKEEPER -e HOST=0.0.0.0 -e DATABASE_DRIVER=zk -e DATABASE=$MINIMESOS_ZOOKEEPER/eremetic -e PORT=8000 -p 8000:8000 alde/eremetic:latest
```

## hermit CLI

[hermit](cmd/hermit) is a command-line application to perform operations on a Eremetic server from the terminal.

## Contributors

These are the fine folks who helped build eremetic
Expand Down
126 changes: 126 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package client

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"github.com/klarna/eremetic"
)

type Client struct {
httpClient *http.Client
endpoint string
}

func New(endpoint string, client *http.Client) (*Client, error) {
return &Client{
httpClient: client,
endpoint: endpoint,
}, nil
}

func (c *Client) AddTask(r eremetic.Request) error {
var buf bytes.Buffer

err := json.NewEncoder(&buf).Encode(r)
if err != nil {
return err
}

req, err := http.NewRequest("POST", c.endpoint+"/task", &buf)
if err != nil {
return err
}

_, err = c.httpClient.Do(req)
if err != nil {
return err
}

return nil
}

func (c *Client) Task(id string) (*eremetic.Task, error) {
req, err := http.NewRequest("GET", c.endpoint+"/task/"+id, nil)
if err != nil {
return nil, err
}

resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}

var task eremetic.Task

err = json.NewDecoder(resp.Body).Decode(&task)
if err != nil {
return nil, err
}

return &task, nil
}

func (c *Client) Tasks() ([]eremetic.Task, error) {
req, err := http.NewRequest("GET", c.endpoint+"/task", nil)
if err != nil {
return nil, err
}

resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}

var tasks []eremetic.Task

err = json.NewDecoder(resp.Body).Decode(&tasks)
if err != nil {
return nil, err
}

return tasks, nil
}

func (c *Client) Sandbox(taskID, file string) ([]byte, error) {
u := fmt.Sprintf("%s/task/%s/%s", c.endpoint, taskID, file)
req, err := http.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}

resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

return b, nil
}

func (c *Client) Version() (string, error) {
u := fmt.Sprintf("%s/version", c.endpoint)
req, err := http.NewRequest("GET", u, nil)
if err != nil {
return "", err
}

resp, err := c.httpClient.Do(req)
if err != nil {
return "", err
}

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}

return string(b), nil
}
54 changes: 54 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package client

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/klarna/eremetic"
)

func TestClient_AddTask(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

}))
defer ts.Close()

var httpClient http.Client

c, err := New(ts.URL, &httpClient)
if err != nil {
t.Fatal(err)
}

var req eremetic.Request

if err := c.AddTask(req); err != nil {
t.Fatal(err)
}
}

func TestClient_Tasks(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`[{
"id": "eremetic-id-12345"
}]`))
}))
defer ts.Close()

var httpClient http.Client

c, err := New(ts.URL, &httpClient)
if err != nil {
t.Fatal(err)
}

tasks, err := c.Tasks()
if err != nil {
t.Fatal(err)
}

if len(tasks) != 1 {
t.Fail()
}
}
28 changes: 28 additions & 0 deletions cmd/hermit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# hermit

A tool for running and listing Eremetic tasks from the command-line.

## Examples:

Run an Eremetic task.

hermit run -cpu 0.2 -mem 32 -image busybox echo hello

List active Eremetic tasks.

hermit ls

Fetch information about a specific task.

hermit task eremetic-task-id-abc123

Fetch the logs of a task.

hermit logs -file stderr eremetic-task-id-abc123

## Configuration

You can configure hermit using these environment variables:

- `EREMETIC_URL`: URL of Eremetic server to connect to.
- `HERMIT_INSECURE`: Allow establishing insecure connections.
Loading

0 comments on commit 1a48691

Please sign in to comment.