/
common.go
141 lines (124 loc) · 2.86 KB
/
common.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
package plugin
import (
"bytes"
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/facebookgo/httpcontrol"
)
//HTTP Constants
const (
MaxTries = 5
RequestTimeout = time.Minute
AuthHeader = "X_AUTH_HEADER"
RequestedWithValue = "X-CDS-SDK"
RequestedWithHeader = "X-Requested-With"
)
var (
auth IOptions
client *http.Client
//Trace is a debug logger
Trace *log.Logger
)
//Common is the base plugin struct every plugin should be composed by
type Common struct{}
//SetTrace is for debug
func SetTrace(traceHandle io.Writer) {
Trace = log.New(traceHandle, "TRACE: ", log.Ldate|log.Ltime)
}
//Init is a common function for all plugins
func (p *Common) Init(o IOptions) string {
SetTrace(ioutil.Discard)
auth = o
if auth.TLSSkipVerify() {
client = &http.Client{
Transport: &httpcontrol.Transport{
RequestTimeout: RequestTimeout,
MaxTries: MaxTries,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
} else {
client = &http.Client{
Transport: &httpcontrol.Transport{
RequestTimeout: RequestTimeout,
MaxTries: MaxTries,
},
}
}
return "plugin: initialized on " + o.GetURL()
}
// request executes an authentificated HTTP request on $path given $method
func request(method string, path string, args []byte) ([]byte, int, error) {
if auth == nil {
return []byte{}, 0, errors.New("Auth must be initialized")
}
var req *http.Request
var err error
if args != nil {
req, err = http.NewRequest(method, auth.GetURL()+path, bytes.NewReader(args))
if err != nil {
return nil, 0, err
}
} else {
req, err = http.NewRequest(method, auth.GetURL()+path, nil)
if err != nil {
return nil, 0, err
}
}
basedHash := base64.StdEncoding.EncodeToString([]byte(auth.Hash()))
req.Header.Set(AuthHeader, basedHash)
req.Header.Set(RequestedWithHeader, RequestedWithValue)
resp, err := client.Do(req)
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()
code := resp.StatusCode
var body []byte
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, code, err
}
return body, code, nil
}
//Log a a struct to send log to CDS API
type Log struct {
ActionID int64 `json:"action_build_id"`
Step string `json:"step"`
Value string `json:"value"`
}
//SendLog send logs to CDS engine for the current
func SendLog(a IAction, step, format string, i ...interface{}) error {
if a == nil {
//If action is nil: do nothing
return nil
}
Trace.Printf(format+"\n", i)
s := fmt.Sprintf(format, i...)
l := Log{
ActionID: a.ID(),
Step: step,
Value: s,
}
logs := []Log{l}
data, err := json.Marshal(logs)
if err != nil {
return err
}
path := fmt.Sprintf("/build/%d/log", logs[0].ActionID)
_, _, err = request("POST", path, data)
if err != nil {
return err
}
return nil
}