-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance.go
114 lines (102 loc) · 3.39 KB
/
instance.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
package eureka
import (
"encoding/xml"
"github.com/Piszmog/eurekaclient/net"
"github.com/pkg/errors"
"strings"
)
type Instance struct {
XMLName xml.Name `xml:"instance"`
HostName string `xml:"hostName"`
Application string `xml:"app"`
InstanceId string `xml:"instanceId"`
IPAddress string `xml:"ipAddr"`
VIPAddress string `xml:"vipAddress"`
SecureVIPAddress string `xml:"secureVipAddress"`
Status StatusType `xml:"status"`
OverriddenStatus StatusType `xml:"overriddenStatus"`
Port int `xml:"port"`
SecurePort int `xml:"securePort"`
HomePageURL string `xml:"homePageUrl"`
StatusPageURL string `xml:"statusPageUrl"`
HealthCheckURL string `xml:"healthCheckUrl"`
DataCenterInfo DataCenterInfo `xml:"dataCenterInfo"`
LeaseInfo LeaseInfo `xml:"leaseInfo"`
}
type DataCenterInfo struct {
Name DCNameType `xml:"name"`
Metadata AmazonMetadata `xml:"metadata"`
}
type DCNameType string
const (
MyOwn DCNameType = "MyOwn"
Amazon DCNameType = "Amazon"
)
type StatusType string
const (
Up StatusType = "UP"
Down StatusType = "DOWN"
Starting StatusType = "STARTING"
OutOfService StatusType = "OUT_OF_SERVICE"
Unknown StatusType = "UNKNOWN"
)
type AmazonMetadata struct {
AMILaunchIndex string `xml:"ami-launch-index"`
LocalHostName string `xml:"local-hostname"`
AvailabilityZone string `xml:"availability-zone"`
InstanceId string `xml:"instance-id"`
PublicIPV4 string `xml:"public-ipv4"`
PublicHostName string `xml:"public-hostname"`
AMIManifestPath string `xml:"ami-manifest-path"`
LocalIPV4 string `xml:"local-ipv4"`
HostName string `xml:"hostname"`
AMIId string `xml:"ami-id"`
InstanceType string `xml:"instance-type"`
}
type LeaseInfo struct {
DurationInSecs int `xml:"durationInSecs"`
RenewalIntervalInSecs int `xml:"renewalIntervalInSecs"`
RegistrationTimestamp int `xml:"registrationTimestamp"`
LastRenewalTimestamp int `xml:"lastRenewalTimestamp"`
EvictionTimestamp int `xml:"evictionTimestamp"`
ServiceUpTimestamp int `xml:"serviceUpTimestamp"`
}
type RegistryInstance struct {
AppName string
Port int
SecurePort int
//HomePagePath string
//StatusPagePath string
//HealthCheckPath string
}
func CreateInstance(instanceId string, instance RegistryInstance) (*Instance, error) {
ipAddress, err := net.FindFirstNonLoopBackIPAddress()
if err != nil {
return nil, errors.Wrap(err, "failed to find an IP address")
}
appName := instance.AppName
hostname := getHostname(instanceId)
return &Instance{
HostName: hostname,
Port: instance.Port,
Status: Up,
IPAddress: ipAddress.String(),
VIPAddress: strings.ToLower(appName),
SecureVIPAddress: strings.ToLower(appName),
Application: strings.ToUpper(appName),
InstanceId: instanceId,
//HomePageURL: instance.HomePagePath,
//StatusPageURL: instance.StatusPagePath,
//HealthCheckURL: instance.HealthCheckPath,
DataCenterInfo: DataCenterInfo{
Name: MyOwn,
},
LeaseInfo: LeaseInfo{
RenewalIntervalInSecs: 30,
DurationInSecs: 90,
},
}, nil
}
func getHostname(instanceId string) string {
return instanceId[:strings.IndexByte(instanceId, ':')]
}