Skip to content

Instantly share code, notes, and snippets.

@gyoza
Last active May 3, 2018 22:29
Show Gist options
  • Save gyoza/d1552528eedbf07f6570bc09573ccfde to your computer and use it in GitHub Desktop.
Save gyoza/d1552528eedbf07f6570bc09573ccfde to your computer and use it in GitHub Desktop.
Get "oldest" system from service (go template, consul template) based on unix time in kv
{{/* get unix times for systems in service group */}}
{{range ls "mykv/myservice"}}
{{ $int := .Value | parseInt}}
{{ scratch.MapSetX "nodetimes" .Key $int}}{{ end }}
{{/* create map of names */}}
{{ range service "myservice|passing" }}
{{ $node := .Node }}
{{ scratch.MapSetX "names" $node $node }}{{ end }}
{{/* create map of time differences from "now" for each server */}}
{{ range $hkey, $hval := scratch.Get "nodetimes" }}
{{ range $key, $val := scratch.Get "nodetimes" }}
{{ if eq $key $hkey}}
{{ $current_time := timestamp "unix" | parseInt }}
{{ $time_diff := $current_time | subtract $val}}
{{ $combine := (printf "%v@%v" $time_diff $hkey) }}
{{ scratch.MapSetX "names_final" $combine $combine}}{{end}}{{end}}{{end}}
{{ range $index, $key := scratch.MapValues "names_final" }}
{{ if eq $index 0}}
{{$key_array := $key | split "@" }}
{{ $system_name := index $key_array 1}}
{{ if eq $system_name "myfqdn"}}
{{ $system_name }}-global{{end}}{{end}}{{end}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment