Added the /fs/cat/ api
This commit is contained in:
parent
c872711d01
commit
9d495a4be4
31
api/fs.go
31
api/fs.go
|
@ -139,6 +139,37 @@ func (a *AllocFS) ReadAt(alloc *Allocation, path string, offset int64, limit int
|
|||
return resp.Body, nil, nil
|
||||
}
|
||||
|
||||
// Cat is used to read contents of a file at the given path in an allocation
|
||||
// directory
|
||||
func (a *AllocFS) Cat(alloc *Allocation, path string, q *QueryOptions) (io.Reader, *QueryMeta, error) {
|
||||
node, _, err := a.client.Nodes().Info(alloc.NodeID, &QueryOptions{})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if node.HTTPAddr == "" {
|
||||
return nil, nil, fmt.Errorf("http addr of the node where alloc %q is running is not advertised", alloc.ID)
|
||||
}
|
||||
u := &url.URL{
|
||||
Scheme: "http",
|
||||
Host: node.HTTPAddr,
|
||||
Path: fmt.Sprintf("/v1/client/fs/cat/%s", alloc.ID),
|
||||
}
|
||||
v := url.Values{}
|
||||
v.Set("path", path)
|
||||
u.RawQuery = v.Encode()
|
||||
req := &http.Request{
|
||||
Method: "GET",
|
||||
URL: u,
|
||||
}
|
||||
c := http.Client{}
|
||||
resp, err := c.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return resp.Body, nil, nil
|
||||
}
|
||||
|
||||
func (a *AllocFS) getErrorMsg(resp *http.Response) error {
|
||||
if errMsg, err := ioutil.ReadAll(resp.Body); err == nil {
|
||||
return fmt.Errorf(string(errMsg))
|
||||
|
|
|
@ -27,6 +27,8 @@ func (s *HTTPServer) FsRequest(resp http.ResponseWriter, req *http.Request) (int
|
|||
return s.FileStatRequest(resp, req)
|
||||
case strings.HasPrefix(path, "readat/"):
|
||||
return s.FileReadAtRequest(resp, req)
|
||||
case strings.HasPrefix(path, "cat/"):
|
||||
return s.FileCatRequest(resp, req)
|
||||
default:
|
||||
return nil, CodedError(404, ErrInvalidMethod)
|
||||
}
|
||||
|
@ -94,3 +96,33 @@ func (s *HTTPServer) FileReadAtRequest(resp http.ResponseWriter, req *http.Reque
|
|||
io.Copy(resp, r)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *HTTPServer) FileCatRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||
var allocID, path string
|
||||
var err error
|
||||
|
||||
q := req.URL.Query()
|
||||
|
||||
if allocID = strings.TrimPrefix(req.URL.Path, "/v1/client/fs/cat/"); allocID == "" {
|
||||
return nil, allocIDNotPresentErr
|
||||
}
|
||||
if path = q.Get("path"); path == "" {
|
||||
return nil, fileNameNotPresentErr
|
||||
}
|
||||
fs, err := s.agent.client.GetAllocFS(allocID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fileInfo, err := fs.Stat(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if fileInfo.IsDir {
|
||||
return nil, fmt.Errorf("file %q is a directory")
|
||||
}
|
||||
|
||||
r, err := fs.ReadAt(path, int64(0), fileInfo.Size)
|
||||
io.Copy(resp, r)
|
||||
return nil, nil
|
||||
}
|
||||
|
|
|
@ -130,21 +130,9 @@ nomad alloc-status %s`, allocID, allocID)
|
|||
f.Ui.Error(msg)
|
||||
return 0
|
||||
}
|
||||
// Stat the file to find it's size
|
||||
file, _, err := client.AllocFS().Stat(alloc, path, nil)
|
||||
if err != nil {
|
||||
f.Ui.Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
if file.IsDir {
|
||||
f.Ui.Error(fmt.Sprintf("The file %q is a directory", file.Name))
|
||||
return 1
|
||||
}
|
||||
|
||||
// Get the contents of the file
|
||||
offset := 0
|
||||
limit := file.Size
|
||||
r, _, err := client.AllocFS().ReadAt(alloc, path, int64(offset), limit, nil)
|
||||
r, _, err := client.AllocFS().Cat(alloc, path, nil)
|
||||
if err != nil {
|
||||
f.Ui.Error(fmt.Sprintf("Error reading file: %v", err))
|
||||
return 1
|
||||
|
|
Loading…
Reference in New Issue