-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeployer-helper.go
More file actions
180 lines (148 loc) · 3.72 KB
/
deployer-helper.go
File metadata and controls
180 lines (148 loc) · 3.72 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package deployerHelper
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"strings"
)
var config *Config
type deployerPayload struct {
GitURL string `json:"gitUrl"`
DeployKey string `json:"deployKey"`
Registry string `json:"registry"`
RegistryLogin string `json:"registryLogin"`
RegistryPassword string `json:"registryPassword"`
RegistryEmail string `json:"registryEmail"`
ImageName string `json:"imageName"`
ImageVersion string `json:"imageVersion"`
WebhookToken string `json:"webhookToken"`
ExtraVars string `json:"extraVars"`
}
type deployerResponse struct {
Output string `json:"output"`
}
// Config struct
type Config struct {
deployKey string
service string
image string
host string
token string
repo string
registry string
login string
password string
email string
extraVars string
}
// Init is a func
func Init() {
config = &Config{}
config.image = "master"
}
// Deploy is a func
func Deploy() {
fmt.Println("Deploy to Host:", config.service, config.host)
payload := createPayload()
r := callService(payload, "deploy")
responseHandler(r)
}
// Build is a func
func Build() {
fmt.Println("Build on Host:", config.service, config.host)
payload := createPayload()
r := callService(payload, "build")
responseHandler(r)
}
func createPayload() deployerPayload {
return deployerPayload{
GitURL: config.repo,
DeployKey: config.deployKey,
Registry: config.registry,
RegistryLogin: config.login,
RegistryPassword: config.password,
RegistryEmail: config.email,
ImageName: config.service,
ImageVersion: config.image,
WebhookToken: config.token,
ExtraVars: config.extraVars,
}
}
func callService(payload deployerPayload, method string) *http.Response {
serviceURL := fmt.Sprintf("https://%s/%s", config.host, method)
fmt.Printf("Calling Service : %v \n", serviceURL)
fmt.Printf("With payload : %v \n", payload)
var URL *url.URL
URL, err := url.Parse(serviceURL)
if err != nil {
fmt.Printf("Error Parsing URL : %v \n", serviceURL)
os.Exit(-1)
}
bArray, err := json.Marshal(payload)
req, err := http.NewRequest("POST", URL.String(), bytes.NewBuffer(bArray))
if err != nil {
fmt.Printf("Error Creating Request : %v \n", err.Error())
os.Exit(-1)
}
req.Header.Set("content-type", "application/json")
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
fmt.Printf("Error Calling Client : %v \n", err.Error())
os.Exit(-1)
}
return res
}
func responseHandler(r *http.Response) {
res := &deployerResponse{}
if r.StatusCode < 200 || r.StatusCode > 400 {
fmt.Printf("Status %v: NOT OK\n", r.StatusCode)
os.Exit(-1)
}
if err := json.NewDecoder(r.Body).Decode(res); err != nil {
fmt.Printf("UNABLE to parse JSON response from service\n")
os.Exit(-1)
}
fmt.Printf("\nResponse From Service : \n\n%v", res.Output)
if strings.Contains(res.Output, "Error:") {
os.Exit(-1)
}
}
// SetDeployKey is a func
func SetDeployKey(value string) {
config.deployKey = value
}
// SetService is a func
func SetService(value string) {
config.service = value
}
// SetImage is a func
func SetImage(value string) {
config.image = value
}
// SetHost is a func
func SetHost(value string) {
config.host = value
}
// SetToken is a func
func SetToken(value string) {
config.token = value
}
// SetRepo is a func
func SetRepo(value string) {
config.repo = value
}
// SetExtraVars is a func
func SetExtraVars(value string) {
config.extraVars = value
}
// SetRegistry is a func
func SetRegistry(host string, login string, password string, email string) {
config.registry = host
config.login = login
config.password = password
config.email = email
}