2016-01-12 23:03:53 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-01-14 21:35:42 +00:00
|
|
|
"io"
|
2016-01-12 23:03:53 +00:00
|
|
|
"net/http"
|
2016-01-13 06:06:42 +00:00
|
|
|
"strconv"
|
2016-01-12 23:03:53 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-01-13 19:49:39 +00:00
|
|
|
var (
|
|
|
|
allocIDNotPresentErr = fmt.Errorf("must provide a valid alloc id")
|
|
|
|
fileNameNotPresentErr = fmt.Errorf("must provide a file name")
|
2016-03-07 19:26:54 +00:00
|
|
|
clientNotRunning = fmt.Errorf("node is not running a Nomad Client")
|
2016-01-13 19:49:39 +00:00
|
|
|
)
|
|
|
|
|
2016-03-07 19:26:54 +00:00
|
|
|
func (s *HTTPServer) FsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
if s.agent.client == nil {
|
|
|
|
return nil, clientNotRunning
|
|
|
|
}
|
|
|
|
|
|
|
|
path := strings.TrimPrefix(req.URL.Path, "/v1/client/fs/")
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(path, "ls/"):
|
|
|
|
return s.DirectoryListRequest(resp, req)
|
|
|
|
case strings.HasPrefix(path, "stat/"):
|
|
|
|
return s.FileStatRequest(resp, req)
|
|
|
|
case strings.HasPrefix(path, "readat/"):
|
|
|
|
return s.FileReadAtRequest(resp, req)
|
2016-03-28 18:06:22 +00:00
|
|
|
case strings.HasPrefix(path, "cat/"):
|
|
|
|
return s.FileCatRequest(resp, req)
|
2016-03-07 19:26:54 +00:00
|
|
|
default:
|
|
|
|
return nil, CodedError(404, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 23:03:53 +00:00
|
|
|
func (s *HTTPServer) DirectoryListRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2016-01-13 06:25:12 +00:00
|
|
|
var allocID, path string
|
|
|
|
|
|
|
|
if allocID = strings.TrimPrefix(req.URL.Path, "/v1/client/fs/ls/"); allocID == "" {
|
2016-01-13 19:49:39 +00:00
|
|
|
return nil, allocIDNotPresentErr
|
2016-01-12 23:03:53 +00:00
|
|
|
}
|
2016-01-13 06:25:12 +00:00
|
|
|
if path = req.URL.Query().Get("path"); path == "" {
|
|
|
|
path = "/"
|
2016-01-12 23:03:53 +00:00
|
|
|
}
|
2016-01-14 21:35:42 +00:00
|
|
|
fs, err := s.agent.client.GetAllocFS(allocID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fs.List(path)
|
2016-01-12 23:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) FileStatRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2016-01-13 06:25:12 +00:00
|
|
|
var allocID, path string
|
|
|
|
if allocID = strings.TrimPrefix(req.URL.Path, "/v1/client/fs/stat/"); allocID == "" {
|
2016-01-13 19:49:39 +00:00
|
|
|
return nil, allocIDNotPresentErr
|
2016-01-12 23:25:51 +00:00
|
|
|
}
|
2016-01-13 21:21:03 +00:00
|
|
|
if path = req.URL.Query().Get("path"); path == "" {
|
2016-01-13 19:49:39 +00:00
|
|
|
return nil, fileNameNotPresentErr
|
2016-01-12 23:25:51 +00:00
|
|
|
}
|
2016-01-14 21:35:42 +00:00
|
|
|
fs, err := s.agent.client.GetAllocFS(allocID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fs.Stat(path)
|
2016-01-12 23:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) FileReadAtRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2016-01-13 06:25:12 +00:00
|
|
|
var allocID, path string
|
|
|
|
var offset, limit int64
|
|
|
|
var err error
|
2016-01-13 06:06:42 +00:00
|
|
|
|
2016-01-13 06:25:12 +00:00
|
|
|
q := req.URL.Query()
|
|
|
|
|
|
|
|
if allocID = strings.TrimPrefix(req.URL.Path, "/v1/client/fs/readat/"); allocID == "" {
|
2016-01-13 19:49:39 +00:00
|
|
|
return nil, allocIDNotPresentErr
|
2016-01-13 06:06:42 +00:00
|
|
|
}
|
2016-01-13 06:25:12 +00:00
|
|
|
if path = q.Get("path"); path == "" {
|
2016-01-13 19:49:39 +00:00
|
|
|
return nil, fileNameNotPresentErr
|
2016-01-13 06:06:42 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 06:25:12 +00:00
|
|
|
if offset, err = strconv.ParseInt(q.Get("offset"), 10, 64); err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing offset: %v", err)
|
2016-01-13 06:06:42 +00:00
|
|
|
}
|
2016-01-13 06:25:12 +00:00
|
|
|
if limit, err = strconv.ParseInt(q.Get("limit"), 10, 64); err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing limit: %v", err)
|
2016-01-13 06:06:42 +00:00
|
|
|
}
|
2016-01-14 21:35:42 +00:00
|
|
|
fs, err := s.agent.client.GetAllocFS(allocID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r, err := fs.ReadAt(path, offset, limit)
|
|
|
|
if err != nil {
|
2016-01-13 06:06:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-14 21:35:42 +00:00
|
|
|
io.Copy(resp, r)
|
2016-01-12 23:03:53 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2016-03-28 18:06:22 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|