-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from marcusolsson/cli
Add hermit cli
- Loading branch information
Showing
5 changed files
with
546 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.