welcome message for users now specific for windows and linux

monitoring of hold file changes and messaging to users to provide more
  interactivity
This commit is contained in:
Erik Brakkee 2024-07-24 18:46:25 +02:00 committed by Erik Brakkee
parent 9b2e8709fb
commit 7351fdaf9c
4 changed files with 82 additions and 3 deletions

View File

@ -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

View File

@ -48,6 +48,7 @@ func main() {
flag.PrintDefaults()
}
flag.Parse()
log.Println("Narg ", flag.NFlag())
if flag.NArg() != 1 {
flag.Usage()
os.Exit(1)

View File

@ -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.

View File

@ -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")
}