Last active
May 3, 2018 22:29
-
-
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
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
{{/* 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