Skip to content

Commit 2803696

Browse files
authored
Merge pull request #3 from mikegray/mike.migr8
Initial migration from singularity CLI
2 parents a5af382 + e4f7c64 commit 2803696

9 files changed

+693
-5
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
*~
2+
vendor/

client/build.go

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright (c) 2018-2019, Sylabs Inc. All rights reserved.
2+
// This software is licensed under a 3-clause BSD license. Please consult the
3+
// LICENSE.md file distributed with the sources of this project regarding your
4+
// rights to use or distribute this software.
5+
6+
package client
7+
8+
import (
9+
"bytes"
10+
"context"
11+
"encoding/json"
12+
"fmt"
13+
"net/http"
14+
15+
"github.com/golang/glog"
16+
"github.com/gorilla/websocket"
17+
jsonresp "github.com/sylabs/json-resp"
18+
)
19+
20+
// Build submits a build job to the Build Service. The context controls the
21+
// lifetime of the request.
22+
func (c *Client) SubmitBuild(ctx context.Context, d Definition, libraryRef string, libraryURL string) (rd ResponseData, err error) {
23+
24+
b, err := json.Marshal(RequestData{
25+
Definition: d,
26+
LibraryRef: libraryRef,
27+
LibraryURL: libraryURL,
28+
})
29+
if err != nil {
30+
return
31+
}
32+
33+
req, err := c.newRequest(http.MethodPost, "/v1/build", "", bytes.NewReader(b))
34+
if err != nil {
35+
return
36+
}
37+
req = req.WithContext(ctx)
38+
req.Header.Set("Content-Type", "application/json")
39+
glog.V(2).Infof("Sending build request to %s", req.URL.String())
40+
41+
res, err := c.HTTPClient.Do(req)
42+
if err != nil {
43+
return
44+
}
45+
defer res.Body.Close()
46+
47+
err = jsonresp.ReadResponse(res.Body, &rd)
48+
if err == nil {
49+
glog.V(2).Infof("Build response - id: %s, wsurl: %s, libref: %s",
50+
rd.ID, rd.WSURL, rd.LibraryRef)
51+
}
52+
return
53+
}
54+
55+
// StreamOutput reads log output from the websocket URL. The context controls
56+
// the lifetime of the request.
57+
func (c *Client) StreamOutput(ctx context.Context, wsURL string) error {
58+
h := http.Header{}
59+
c.setRequestHeaders(h)
60+
61+
ws, resp, err := websocket.DefaultDialer.Dial(wsURL, h)
62+
if err != nil {
63+
glog.V(2).Infof("websocket dial err - %s, partial response: %+v", err, resp)
64+
return err
65+
}
66+
defer ws.Close()
67+
68+
for {
69+
// Check if context has expired
70+
select {
71+
case <-ctx.Done():
72+
return ctx.Err()
73+
default:
74+
}
75+
76+
// Read from websocket
77+
mt, msg, err := ws.ReadMessage()
78+
if err != nil {
79+
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
80+
return nil
81+
}
82+
glog.V(2).Infof("websocket read message err - %s", err)
83+
return err
84+
}
85+
86+
// Print to terminal
87+
switch mt {
88+
case websocket.TextMessage:
89+
fmt.Printf("%s", msg)
90+
case websocket.BinaryMessage:
91+
fmt.Print("Ignoring binary message")
92+
}
93+
}
94+
}
95+
96+
// GetBuildStatus gets the status of a build from the Remote Build Service
97+
func (c *Client) GetBuildStatus(ctx context.Context, buildID string) (rd ResponseData, err error) {
98+
req, err := c.newRequest(http.MethodGet, "/v1/build/"+buildID, "", nil)
99+
if err != nil {
100+
return
101+
}
102+
req = req.WithContext(ctx)
103+
104+
res, err := c.HTTPClient.Do(req)
105+
if err != nil {
106+
return
107+
}
108+
defer res.Body.Close()
109+
110+
err = jsonresp.ReadResponse(res.Body, &rd)
111+
return
112+
}

0 commit comments

Comments
 (0)