ref: 8f54cb40ef83bfe2fb560106e5e37d463ec6b3dd cisco/api/jobs.go -rw-r--r-- 984 bytes View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main

import (
	"fmt"
	"github.com/lxc/lxd/shared/api"
	"strings"
	"time"
)

func instance_update_ipv6() {
	var entries []DbInstance

	for {
		DB.Find(&entries)
		for _, instance := range entries {
			var ipv6 string = ""

			if instance.Status == "LXD" {
				c1 := make(chan *api.InstanceState, 1)
				go func() {
					inst, _, err := Context.LXD.GetInstanceState("LF" + instance.Uuid)

					if err != nil {
						fmt.Printf("Error: %s\n", err)
						return
					}
					c1 <- inst
				}()

				select {
				case inst := <-c1:
					for _, net := range inst.Network {
						for _, addr := range net.Addresses {
							if addr.Family == "inet6" && !strings.HasPrefix(addr.Address, "fe80") && addr.Scope != "local" {
								ipv6 = addr.Address
								break
							}
						}
					}

					if ipv6 != "" {
						DB.Model(&instance).Update("ipv6", ipv6)
					}
					DB.Model(&instance).Update("lxc_status", inst.Status)
				case <-time.After(30 * time.Second):
				}
			}
		}
	}
}