-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.go
166 lines (137 loc) · 4 KB
/
application.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"fmt"
//"io/ioutil"
"log"
//"os"
"sync"
"time"
"github.com/DimensionDataResearch/go-dd-cloud-compute/compute"
"github.com/mostlygeek/arp"
"github.com/spf13/viper"
)
// Application represents the state for the cloud-config generator.
type Application struct {
McpUser string
McpPassword string
McpRegion string
//SSHPublicKey string
SSHPublicKeyFromYML string
SSHVaultCAFromYML string
RancherAgentVersion string
RancherAgentURL string
RancherOSDNS string
ROSConsole string
Client *compute.Client
NetworkDomain *compute.NetworkDomain
VLAN *compute.VLAN
ServersByMACAddress map[string]compute.Server
stateLock *sync.Mutex
refreshTimer *time.Timer
cancelRefresh chan bool
}
// NewApplication creates new application state.
func NewApplication() *Application {
return &Application{
ServersByMACAddress: make(map[string]compute.Server),
stateLock: &sync.Mutex{},
}
}
// Initialize performs initial configuration of the application.
func (app *Application) Initialize() error {
viper.BindEnv("MCP_USER", "mcp.user")
viper.BindEnv("MCP_PASSWORD", "mcp.password")
viper.BindEnv("MCP_REGION", "mcp.region")
viper.BindEnv("MCP_VLAN_ID", "network.vlan_id")
viper.SetConfigType("yaml")
viper.SetConfigName("cloud-config-server")
viper.AddConfigPath(".")
viper.AddConfigPath("/etc")
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
app.McpRegion = viper.GetString("mcp.region")
app.McpUser = viper.GetString("mcp.user")
app.McpPassword = viper.GetString("mcp.password")
app.RancherAgentVersion = viper.GetString("rancher_agent.version")
app.RancherAgentURL = viper.GetString("rancher_agent.url")
app.SSHPublicKeyFromYML = viper.GetString("rancher_os.SSHPublicKey")
app.SSHVaultCAFromYML = viper.GetString("rancher_os.SSHVaultCA")
app.RancherOSDNS = viper.GetString("rancher_os.network.dns.nameservers")
app.Client = compute.NewClient(app.McpRegion, app.McpUser, app.McpPassword)
app.ROSConsole = viper.GetString("rancher_os.console")
vlanID := viper.GetString("network.vlan_id")
app.VLAN, err = app.Client.GetVLAN(vlanID)
if err != nil {
return err
} else if app.VLAN == nil {
return fmt.Errorf("Cannot find VLAN with Id '%s'", vlanID)
}
app.NetworkDomain, err = app.Client.GetNetworkDomain(app.VLAN.NetworkDomain.ID)
if err != nil {
return err
} else if app.NetworkDomain == nil {
return fmt.Errorf("Cannot find network domain with Id '%s'", app.VLAN.NetworkDomain.ID)
}
//sshPublicKeyFile, err := os.Open(
// os.Getenv("HOME") + "/.ssh/id_rsa.pub",
//)
//if err != nil {
// return err
//}
//defer sshPublicKeyFile.Close()
//sshPublicKeyData, err := ioutil.ReadAll(sshPublicKeyFile)
//if err != nil {
// return err
//}
//app.SSHPublicKey = string(sshPublicKeyData)
return nil
}
// Start polling CloudControl for server metadata.
func (app *Application) Start() {
app.stateLock.Lock()
defer app.stateLock.Unlock()
// Warm up caches.
arp.CacheUpdate()
err := app.RefreshServerMetadata(false)
if err != nil {
log.Printf("Error refreshing servers: %s",
err.Error(),
)
}
// Periodically scan the ARP cache so we can resolve MAC addresses from client IPs.
arp.AutoRefresh(5 * time.Second)
app.cancelRefresh = make(chan bool, 1)
app.refreshTimer = time.NewTimer(10 * time.Second)
go func() {
cancelRefresh := app.cancelRefresh
refreshTimer := app.refreshTimer.C
for {
select {
case <-cancelRefresh:
return // Stopped
case <-refreshTimer:
log.Printf("Refreshing server MAC addresses...")
err := app.RefreshServerMetadata(true)
if err != nil {
log.Printf("Error refreshing servers: %s",
err.Error(),
)
}
log.Printf("Refreshed server MAC addresses.")
}
}
}()
}
// Stop polling CloudControl for server metadata.
func (app *Application) Stop() {
app.stateLock.Lock()
defer app.stateLock.Unlock()
if app.cancelRefresh != nil {
app.cancelRefresh <- true
}
app.cancelRefresh = nil
app.refreshTimer.Stop()
app.refreshTimer = nil
}