ref: 3946ea6ad8f57628f22b006e3acc365cc0d8136c cisco/api/jobs.go -rw-r--r-- 1.2 KiB 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
52
53
54
55
56
57
58
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 = ""
			var ipv6_mask string = ""
			var ipv4 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
								ipv6_mask = addr.Netmask
							}
							if addr.Family == "inet" && addr.Scope != "local" {
								ipv4 = addr.Address
							}
						}
					}

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