mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-03 18:20:27 +00:00
* Squashed 'tools/' changes from e9e7e6b..db5efc0db5efc0Merge pull request #28 from weaveworks/mike/add-image-tag5312c40Import image-tag script into build tools so it can be shared7e850f8Fix logs pathdda9785Update deploy apif2f4e5bFix the wcloud client3925eb6Merge pull request #27 from weaveworks/wcloud-events77355b9Lintd9a1c6cAdd wcloud events, update flags and error nicely when there is no config git-subtree-dir: tools git-subtree-split:db5efc0537* Remove ./image-tag and use ./tools/image-tag instead image-tag is now shared code from the build-tools repo
151 lines
3.6 KiB
Go
151 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"net/http"
|
|
)
|
|
|
|
// Client for the deployment service
|
|
type Client struct {
|
|
token string
|
|
baseURL string
|
|
}
|
|
|
|
// NewClient makes a new Client
|
|
func NewClient(token, baseURL string) Client {
|
|
return Client{
|
|
token: token,
|
|
baseURL: baseURL,
|
|
}
|
|
}
|
|
|
|
func (c Client) newRequest(method, path string, body io.Reader) (*http.Request, error) {
|
|
req, err := http.NewRequest(method, c.baseURL+path, body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Add("Authorization", fmt.Sprintf("Scope-Probe token=%s", c.token))
|
|
return req, nil
|
|
}
|
|
|
|
// Deploy notifies the deployment service about a new deployment
|
|
func (c Client) Deploy(deployment Deployment) error {
|
|
var buf bytes.Buffer
|
|
if err := json.NewEncoder(&buf).Encode(deployment); err != nil {
|
|
return err
|
|
}
|
|
req, err := c.newRequest("POST", "/api/deploy/deploy", &buf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if res.StatusCode != 204 {
|
|
return fmt.Errorf("Error making request: %s", res.Status)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetDeployments returns a list of deployments
|
|
func (c Client) GetDeployments(from, through int64) ([]Deployment, error) {
|
|
req, err := c.newRequest("GET", fmt.Sprintf("/api/deploy/deploy?from=%d&through=%d", from, through), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if res.StatusCode != 200 {
|
|
return nil, fmt.Errorf("Error making request: %s", res.Status)
|
|
}
|
|
var response struct {
|
|
Deployments []Deployment `json:"deployments"`
|
|
}
|
|
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
|
|
return nil, err
|
|
}
|
|
return response.Deployments, nil
|
|
}
|
|
|
|
// GetEvents returns the raw events.
|
|
func (c Client) GetEvents(from, through int64) ([]byte, error) {
|
|
req, err := c.newRequest("GET", fmt.Sprintf("/api/deploy/event?from=%d&through=%d", from, through), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if res.StatusCode != 200 {
|
|
return nil, fmt.Errorf("Error making request: %s", res.Status)
|
|
}
|
|
return ioutil.ReadAll(res.Body)
|
|
}
|
|
|
|
// GetConfig returns the current Config
|
|
func (c Client) GetConfig() (*Config, error) {
|
|
req, err := c.newRequest("GET", "/api/config/deploy", nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if res.StatusCode == 404 {
|
|
return nil, fmt.Errorf("No configuration uploaded yet.")
|
|
}
|
|
if res.StatusCode != 200 {
|
|
return nil, fmt.Errorf("Error making request: %s", res.Status)
|
|
}
|
|
var config Config
|
|
if err := json.NewDecoder(res.Body).Decode(&config); err != nil {
|
|
return nil, err
|
|
}
|
|
return &config, nil
|
|
}
|
|
|
|
// SetConfig sets the current Config
|
|
func (c Client) SetConfig(config *Config) error {
|
|
var buf bytes.Buffer
|
|
if err := json.NewEncoder(&buf).Encode(config); err != nil {
|
|
return err
|
|
}
|
|
req, err := c.newRequest("POST", "/api/config/deploy", &buf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if res.StatusCode != 204 {
|
|
return fmt.Errorf("Error making request: %s", res.Status)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetLogs returns the logs for a given deployment.
|
|
func (c Client) GetLogs(deployID string) ([]byte, error) {
|
|
req, err := c.newRequest("GET", fmt.Sprintf("/api/deploy/deploy/%s/log", deployID), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if res.StatusCode != 200 {
|
|
return nil, fmt.Errorf("Error making request: %s", res.Status)
|
|
}
|
|
return ioutil.ReadAll(res.Body)
|
|
}
|