-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
152 additions
and
2 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
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,78 @@ | ||
package agent | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net" | ||
"net/http" | ||
"path/filepath" | ||
"strconv" | ||
|
||
"github.com/gadget-inc/dateilager/internal/files" | ||
"github.com/gadget-inc/dateilager/internal/logger" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type Agent struct { | ||
cacheDir string | ||
port int | ||
} | ||
|
||
func NewAgent(cacheDir string, port int) Agent { | ||
return Agent{cacheDir, port} | ||
} | ||
|
||
func (a *Agent) Server(ctx context.Context) *http.Server { | ||
http.HandleFunc("GET /healthz", a.healthCheck) | ||
http.HandleFunc("POST /link_cache", a.linkCache) | ||
|
||
server := &http.Server{ | ||
Addr: ":" + strconv.Itoa(a.port), | ||
BaseContext: func(net.Listener) context.Context { return ctx }, | ||
} | ||
return server | ||
} | ||
|
||
type healthStatus struct { | ||
Status string `json:"status"` | ||
} | ||
|
||
func (a *Agent) healthCheck(resp http.ResponseWriter, req *http.Request) { | ||
err := json.NewEncoder(resp).Encode(healthStatus{Status: "OK"}) | ||
if err != nil { | ||
httpErr(req.Context(), resp, err, "failed to encode status") | ||
return | ||
} | ||
resp.WriteHeader(http.StatusOK) | ||
} | ||
|
||
type linkRequest struct { | ||
Dir string `json:"dir"` | ||
} | ||
|
||
func (a *Agent) linkCache(resp http.ResponseWriter, req *http.Request) { | ||
linkReq := linkRequest{} | ||
err := json.NewDecoder(req.Body).Decode(&linkReq) | ||
if err != nil { | ||
httpReqErr(req.Context(), resp, err, "failed to decode link request") | ||
return | ||
} | ||
|
||
err = files.HardlinkDir(a.cacheDir, filepath.Join(linkReq.Dir, "cache")) | ||
if err != nil { | ||
httpErr(req.Context(), resp, err, "failed to link cache director") | ||
return | ||
} | ||
|
||
resp.WriteHeader(http.StatusCreated) | ||
} | ||
|
||
func httpReqErr(ctx context.Context, resp http.ResponseWriter, err error, message string) { | ||
logger.Warn(ctx, message, zap.Error(err)) | ||
http.Error(resp, err.Error(), http.StatusBadRequest) | ||
} | ||
|
||
func httpErr(ctx context.Context, resp http.ResponseWriter, err error, message string) { | ||
logger.Error(ctx, message, zap.Error(err)) | ||
http.Error(resp, err.Error(), http.StatusInternalServerError) | ||
} |
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,67 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/gadget-inc/dateilager/internal/key" | ||
"github.com/gadget-inc/dateilager/internal/logger" | ||
"github.com/gadget-inc/dateilager/pkg/agent" | ||
"github.com/gadget-inc/dateilager/pkg/client" | ||
"github.com/spf13/cobra" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func NewCmdAgent() *cobra.Command { | ||
var ( | ||
dir string | ||
port int | ||
) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "agent", | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
ctx := cmd.Context() | ||
c := client.FromContext(ctx) | ||
|
||
version, err := c.GetCache(ctx, dir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
logger.Info(ctx, "cache downloaded", key.Version.Field(version), key.Directory.Field(dir)) | ||
|
||
a := agent.NewAgent(dir, port) | ||
|
||
backgroundCtx, cancel := context.WithCancel(ctx) | ||
server := a.Server(backgroundCtx) | ||
|
||
osSignals := make(chan os.Signal, 1) | ||
signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM) | ||
|
||
go func() { | ||
<-osSignals | ||
logger.Info(ctx, "received interrupt signal") | ||
|
||
cancel() | ||
|
||
err := server.Shutdown(ctx) | ||
if err != nil { | ||
logger.Error(ctx, "error shutting down server", zap.Error(err)) | ||
} | ||
}() | ||
|
||
logger.Info(ctx, "start agent", zap.Int("port", port), key.Directory.Field(dir)) | ||
return server.ListenAndServe() | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVar(&dir, "dir", "", "Cache directory") | ||
cmd.Flags().IntVar(&port, "port", 8080, "API server port") | ||
|
||
_ = cmd.MarkFlagRequired("path") | ||
|
||
return cmd | ||
} |
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