From 7351fdaf9c78e11e0da5f147fb5303d101888969 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Wed, 24 Jul 2024 18:46:25 +0200 Subject: [PATCH] welcome message for users now specific for windows and linux monitoring of hold file changes and messaging to users to provide more interactivity --- cmd/fsnotifytest/monitor.go | 2 +- cmd/wsproxy/wsproxy.go | 1 + pkg/agent/help.txt | 4 ++ pkg/agent/session.go | 78 ++++++++++++++++++++++++++++++++++++- 4 files changed, 82 insertions(+), 3 deletions(-) diff --git a/cmd/fsnotifytest/monitor.go b/cmd/fsnotifytest/monitor.go index 81248e1..7d1622b 100644 --- a/cmd/fsnotifytest/monitor.go +++ b/cmd/fsnotifytest/monitor.go @@ -15,7 +15,7 @@ import ( // CHMOD // echo > .hold // -// file name: ./.hold on linux +// file name: ./.hold on linux and just .hold on windows func main() { // Create a new watcher diff --git a/cmd/wsproxy/wsproxy.go b/cmd/wsproxy/wsproxy.go index e5e6673..021a237 100644 --- a/cmd/wsproxy/wsproxy.go +++ b/cmd/wsproxy/wsproxy.go @@ -48,6 +48,7 @@ func main() { flag.PrintDefaults() } flag.Parse() + log.Println("Narg ", flag.NFlag()) if flag.NArg() != 1 { flag.Usage() os.Exit(1) diff --git a/pkg/agent/help.txt b/pkg/agent/help.txt index dcb960e..bf90031 100644 --- a/pkg/agent/help.txt +++ b/pkg/agent/help.txt @@ -6,7 +6,11 @@ terminates. You can extend this time using + {{ if eq .os "windows" -}} + echo > %agentdir%\.hold + {{- else -}} touch $agentdir/.hold + {{- end }} The expiry time is equal to the modification time of the .hold file with the expiry duration added. diff --git a/pkg/agent/session.go b/pkg/agent/session.go index 3dfa823..5339e51 100644 --- a/pkg/agent/session.go +++ b/pkg/agent/session.go @@ -1,13 +1,18 @@ package agent import ( + "bytes" "fmt" + "github.com/fsnotify/fsnotify" "github.com/gliderlabs/ssh" "io" "log" "os" + "path/filepath" + "runtime" "strconv" "strings" + "text/template" "time" _ "embed" @@ -55,7 +60,8 @@ var state AgentState const holdFilename = ".hold" //go:embed help.txt -var helpMessage string +var helpMessageTemplate string +var helpMessage = formatHelpMessage() func ConfigureAgent(advanceWarningTime, agentExpiryTime, tickerInterval time.Duration) { if fileExists(holdFilename) { @@ -81,6 +87,59 @@ func ConfigureAgent(advanceWarningTime, agentExpiryTime, tickerInterval time.Dur check() } }() + go monitorHoldFile() +} + +func monitorHoldFile() { + watcher, err := fsnotify.NewWatcher() + if err != nil { + log.Printf("Cannot watch old file %s, user notifications for change in expiry time will be unavailable: %v", holdFilename, err) + } + defer watcher.Close() + err = watcher.Add(".") + if err != nil { + log.Printf("Cannot watch old file %s, user notifications for change in expiry time will be unavailable: %v", holdFilename, err) + } + expiryTime := state.expiryTime(holdFilename) + for { + select { + case event, ok := <-watcher.Events: + if !ok { + return + } + base := filepath.Base(event.Name) + if base == holdFilename { + newExpiryTIme := state.expiryTime(holdFilename) + if newExpiryTIme != expiryTime { + message := fmt.Sprintf("Expiry time of session is now %s\n", + newExpiryTIme.Format(time.DateTime)) + message += holdFileMessage() + messageUsers(message) + expiryTime = newExpiryTIme + } + } + + case err, ok := <-watcher.Errors: + if !ok { + return + } + log.Println("Error:", err) + } + } +} + +func holdFileMessage() string { + message := "" + if fileExists(holdFilename) { + message += fmt.Sprintf("When the last user exits, the session will timeout and not exit immediately.\n"+ + "Remove the %s file if you want th session to terminate when the last user logs out", + holdFilename) + } else { + message += fmt.Sprintf("When the last user exits, the agent will return and the continuous\n" + + "integration job will continue.") + + } + return message } func Login(sessionId int, sshSession ssh.Session) { @@ -119,6 +178,22 @@ func PrintHelpMessage(sshSession ssh.Session) { state.agentExpriryTime)) } +func formatHelpMessage() string { + templ, err := template.New("help").Parse(helpMessageTemplate) + if err != nil { + panic(err) + } + helpFormattedBuf := bytes.NewBuffer(make([]byte, 0)) + log.Println("Runnning on ", runtime.GOOS) + data := map[string]string{"os": runtime.GOOS} + err = templ.Execute(helpFormattedBuf, data) + if err != nil { + panic(err) + } + helpFormatted := helpFormattedBuf.String() + return helpFormatted +} + func LogOut(sessionId int) { log.Println("User logged out") delete(state.sessions, sessionId) @@ -127,7 +202,6 @@ func LogOut(sessionId int) { } func PrintMessage(sshSession ssh.Session, message string) { - io.WriteString(sshSession.Stderr(), "\n\r###\n\r") for _, line := range strings.Split(message, "\n") { io.WriteString(sshSession.Stderr(), "### "+line+"\n\r") }