-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
109 lines (96 loc) · 2.12 KB
/
log.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
package main
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"net/url"
"strings"
"time"
)
func logError(s string) error {
return httplog("error", s)
}
func logFatal(s string) error {
return httplog("fatal", s)
}
func logComplete(s string) error {
return httplog("complete", s)
}
func httplog(t, s string) error {
httpTransport := &http.Transport{
Dial: (&net.Dialer{DualStack: true}).Dial,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
httpClient := &http.Client{Transport: httpTransport, Timeout: 10 * time.Second}
var host string
var port string
var ok bool
ok, metadataUrl := cmdlineVar("cloud-config-url")
if !ok {
return fmt.Errorf("no datasource available")
}
u, _ := url.Parse(metadataUrl)
if strings.Index(u.Host, ":") > 0 {
host, port, _ = net.SplitHostPort(u.Host)
} else {
host = u.Host
}
if port == "" {
if u.Scheme == "https" {
port = "443"
} else {
port = "80"
}
}
addrs, err := net.LookupIP(host)
if err != nil {
return err
}
for _, addr := range addrs {
if ipv4 && addr.To4() == nil {
continue
}
if ipv6 && addr.To4() != nil {
continue
}
logurl := ""
switch t {
case "error":
logurl = metadataUrl + "&action=log&flag=install_error&message=" + url.QueryEscape(s)
case "fatal":
logurl = metadataUrl + "&action=log&flag=install_fatal&message=" + url.QueryEscape(s)
case "complete":
logurl = metadataUrl + "&action=log&flag=install_complete&message=" + url.QueryEscape(s)
default:
return fmt.Errorf("unknown log level %s", t)
}
req, err := http.NewRequest("GET", logurl, nil)
if err != nil {
if debug {
fmt.Printf("http %s\n", err)
time.Sleep(10 * time.Second)
}
}
req.URL, err = url.Parse(logurl)
if err != nil {
if debug {
fmt.Printf("http %s\n", err)
time.Sleep(10 * time.Second)
}
}
req.URL.Host = net.JoinHostPort(addr.String(), port)
req.Host = host
res, err := httpClient.Do(req)
if err != nil {
if debug {
fmt.Printf("http err %s\n", err)
time.Sleep(10 * time.Second)
}
}
if res != nil && res.Body != nil {
defer res.Body.Close()
}
}
return nil
}