Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a filesystem monitoring option for the snapshot paths command #231

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/flyio_env_report.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ SCRIPT_NAME=flyio_env_report.sh
USAGE="$SCRIPT_NAME [options] <protocol://hostname> <owner> <environment-name>"




die()
{
echo "Error: $1" >&2
Expand Down
Empty file added bin/test2
Empty file.
1 change: 1 addition & 0 deletions cmd/kosli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ The service principal needs to have the following permissions:
setTagsFlag = "[optional] The key-value pairs to tag the resource with. The format is: key=value"
unsetTagsFlag = "[optional] The list of tag keys to remove from the resource."
pathsSpecFileFlag = "The path to a paths file in YAML/JSON/TOML format. Cannot be used together with --path ."
pathsWatchFlag = "[optional] Watch the filesystem for changes and report snapshots of artifacts running in specific filesystem paths to Kosli."
snapshotPathPathFlag = "The base path for the artifact to snapshot."
snapshotPathExcludeFlag = "[optional] The comma-separated list of literal paths or glob patterns to exclude when fingerprinting the artifact."
snapshotPathArtifactNameFlag = "The reported name of the artifact."
Expand Down
53 changes: 53 additions & 0 deletions cmd/kosli/snapshotPaths.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package main
import (
"fmt"
"io"
"log"
"net/http"
"path/filepath"
"strings"

"github.com/fsnotify/fsnotify"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be aware that fsnotify does not work for subdirectories:

Are subdirectories watched?
No, you must add watches for any directory you want to watch (a recursive watcher is on the roadmap: fsnotify/fsnotify#18).

"github.com/go-playground/validator/v10"
"github.com/kosli-dev/cli/internal/requests"
"github.com/kosli-dev/cli/internal/server"
Expand Down Expand Up @@ -52,6 +54,7 @@ kosli snapshot paths yourEnvironmentName \

type snapshotPathsOptions struct {
pathSpecFile string
watch bool
}

func newSnapshotPathsCmd(out io.Writer) *cobra.Command {
Expand All @@ -76,6 +79,8 @@ func newSnapshotPathsCmd(out io.Writer) *cobra.Command {
}

cmd.Flags().StringVar(&o.pathSpecFile, "paths-file", "", pathsSpecFileFlag)
cmd.Flags().BoolVar(&o.watch, "watch", false, pathsWatchFlag)

addDryRunFlag(cmd)

if err := RequireFlags(cmd, []string{"paths-file"}); err != nil {
Expand All @@ -96,10 +101,26 @@ func (o *snapshotPathsOptions) run(args []string) error {
return err
}

// snapshot the paths
if o.watch {
err := reportArtifacts(ps, url, envName)
if err != nil {
return err
}

watch_for_changes(ps, url, envName)
}

return reportArtifacts(ps, url, envName)
}

func reportArtifacts(ps *server.PathsSpec, url string, envName string) error {

artifacts, err := server.CreatePathsArtifactsData(ps, logger)
if err != nil {
return err
}

payload := &server.ServerEnvRequest{
Artifacts: artifacts,
}
Expand All @@ -118,6 +139,38 @@ func (o *snapshotPathsOptions) run(args []string) error {
return err
}

func watch_for_changes(ps *server.PathsSpec, url string, envName string) bool {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()

// Add every path in path spec to watcher
for _, pathSpec := range ps.Artifacts {
if err := watcher.Add(pathSpec.Path); err != nil {
logger.Error("failed to watch dir: %v", err)
}
}

for {
select {
case event := <-watcher.Events:
logger.Debug("Event: " + event.String())

if event.Op != fsnotify.Chmod {
err := reportArtifacts(ps, url, envName)
if err != nil {
logger.Error("failed to report artifacts: %v", err)
}
}

case err := <-watcher.Errors:
logger.Debug("Error: ", err)
}
}
}

func processPathSpecFile(pathsSpecFile string) (*server.PathsSpec, error) {
var ps *server.PathsSpec
v := viper.New()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/s3 v1.31.0
github.com/aws/smithy-go v1.13.5
github.com/docker/docker v24.0.9+incompatible
github.com/fsnotify/fsnotify v1.7.0
github.com/go-git/go-billy/v5 v5.5.0
github.com/go-git/go-git/v5 v5.11.0
github.com/go-playground/validator/v10 v10.19.0
Expand Down Expand Up @@ -91,7 +92,6 @@ require (
github.com/evanphx/json-patch/v5 v5.2.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-logr/logr v1.4.1 // indirect
Expand Down
6 changes: 6 additions & 0 deletions kosli-paths.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 1
artifacts:
service-a:
path: bin/
service-b:
path: hack/
Loading