-
Notifications
You must be signed in to change notification settings - Fork 1
/
swdeployer.go
626 lines (527 loc) · 15.8 KB
/
swdeployer.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
package main
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"math/rand"
"mime/multipart"
"net/http"
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
"strings"
"github.com/juju/persistent-cookiejar"
"github.com/mcuadros/go-version"
"github.com/pkg/errors"
"github.com/urfave/cli"
"gopkg.in/ini.v1"
)
const shopwareAPI = "https://api.shopware.com/"
func expandIfNeeded(s string) string {
if s[0] == '~' {
u, err := user.Current()
if err != nil {
return s
}
return filepath.Join(u.HomeDir, s[1:])
}
return s
}
func loadConfig(path string) (*ini.Section, error) {
f, err := os.Open(expandIfNeeded(path))
if err != nil {
return nil, errors.Wrapf(err, "unable to open config file %s", path)
}
i, err := ini.Load(f)
if err != nil {
return nil, errors.Wrapf(err, "unable to parse ini file %s", path)
}
section, err := i.GetSection("")
if err != nil {
return nil, errors.Wrap(err, "unable to find default (unnamed) section")
}
return section, nil
}
func (sw *ShopwareClient) printPluginInfo(pluginID int) error {
if pluginID == 0 {
return errors.New("plugin_id not found")
}
sw.pluginID = pluginID
req, err := http.NewRequest("GET", shopwareAPI+"plugins/"+strconv.Itoa(pluginID), nil)
if err != nil {
return errors.Wrap(err, "unable to create plugin info request")
}
req.Header.Set("X-Shopware-Token", sw.token)
resp, err := sw.c.Do(req)
if err != nil {
return errors.Wrap(err, "unable to retrieve plugin info")
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return fmt.Errorf("unexpected status code when retrieving plugin info: %d", resp.StatusCode)
}
dec := json.NewDecoder(resp.Body)
var pluginInfo = PluginInfoResult{}
err = dec.Decode(&pluginInfo)
if err != nil {
return errors.Wrap(err, "unable to unmarshal json plugin info response")
}
fmt.Printf("Name:\t\t %s (%s)\n", pluginInfo.Name, pluginInfo.ActivationStatus.Name)
fmt.Println("Addons:\t\t", pluginInfo.Addons)
fmt.Println("Changed at:\t", pluginInfo.LatestBinary.LastChangeDate)
fmt.Println("Version:\t", pluginInfo.LatestBinary.Version, "(current)")
fmt.Println()
return nil
}
func (sw *ShopwareClient) printNewData() error {
f, err := os.Open("plugin.xml")
if err != nil {
return errors.Wrap(err, "unable to open plugin.xml")
}
dec := xml.NewDecoder(f)
err = dec.Decode(&sw.pluginInfo)
if err != nil {
return errors.Wrap(err, "unable to decode plugin.xml")
}
fmt.Println("Version:\t", sw.pluginInfo.Version, "(new)")
if len(sw.pluginInfo.Changelog) > 0 {
last := sw.pluginInfo.Changelog[len(sw.pluginInfo.Changelog)-1]
for _, change := range last.Changes {
if change.Lang == "" {
change.Lang = "en"
}
fmt.Printf("Log %s:\t [%s] %s\n", last.Version, change.Lang, change.Value)
}
} else {
fmt.Println("No changelog was found")
}
fmt.Println()
return nil
}
type CompatibleSoftwareVersion struct {
Checked *bool `json:"checked,omitempty"`
ID int `json:"id"`
Major string `json:"major"`
Name string `json:"name"`
Parent interface{} `json:"parent"`
Selectable bool `json:"selectable"`
}
type Changelog struct {
ID int `json:"id"`
Locale struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"locale"`
Text string `json:"text"`
}
type BinaryUploadResponse struct {
Archives []struct {
ID int `json:"id"`
IonCubeEncrypted bool `json:"ioncubeEncrypted"`
RemoteLink string `json:"remoteLink"`
ShopwareMajorVersion *string `json:"shopwareMajorVersion"`
} `json:"archives"`
Assessment bool `json:"assessment"`
Changelogs []Changelog `json:"changelogs"`
CompatibleSoftwareVersions []CompatibleSoftwareVersion `json:"compatibleSoftwareVersions"`
CreationDate string `json:"creationDate"`
ID int `json:"id"`
IonCubeEncrypted bool `json:"ionCubeEncrypted"`
LastChangeDate string `json:"lastChangeDate"`
LicenseCheckRequired bool `json:"licenseCheckRequired"`
Name string `json:"name"`
RemoteLink string `json:"remoteLink"`
Status struct {
Description string `json:"description"`
ID int `json:"id"`
Name string `json:"name"`
} `json:"status"`
Version string `json:"version"`
}
type PluginStatisticsResponse struct {
SoftwareVersions []CompatibleSoftwareVersion `json:"softwareVersions"`
}
const sw5 = "Shopware 5"
var FalseVariable = false
var TrueVariable = true
var compatibleSoftwareVersions map[string]CompatibleSoftwareVersion
func compatibleVersions(from, to string) (versions []CompatibleSoftwareVersion) {
if from == "" {
return
}
for ver, csv := range compatibleSoftwareVersions {
if version.Compare(ver, from, ">=") && (to == "" || version.Compare(ver, to, "<=")) {
if csv.Selectable {
versions = append(versions, csv)
}
//} else {
// csv.Checked = &FalseVariable
}
}
return
}
func (b *BinaryUploadResponse) SetChangelog(locale, text string) {
for i, ch := range b.Changelogs {
if ch.Locale.Name == locale {
ch.Text = text
}
b.Changelogs[i] = ch
}
}
func (sw *ShopwareClient) update() error {
fmt.Print("Are you sure you want to deploy the new version to Shopware? (y/N) ")
var response string
fmt.Scanf("%s", &response)
if !strings.EqualFold(strings.TrimSpace(response), "y") {
return errors.New("aborted by user")
}
// Step -1: make sure directory is clean
clean := exec.Command("git", "diff-index", "--quiet", "HEAD", "--")
err := clean.Run()
if err != nil {
return errors.New("your git directory is not clean. Please commit your changes and try again")
}
// Step 0: prepare the binary
prefix, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "unable to figure out current working directory")
}
prefix = filepath.Base(prefix)
filename := filepath.Join(os.TempDir(), strconv.Itoa(rand.Int())+".zip")
cmd := exec.Command("git", "archive", "-o", filename, "-9", "--prefix", prefix+"/", "HEAD")
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return errors.Wrap(err, "unable to prepare zip file")
}
defer os.Remove(filename)
respBody, err := sw.get("https://api.shopware.com/pluginstatics/all")
if err != nil {
return errors.Wrap(err, "unable to download plugin versions")
}
var statistics PluginStatisticsResponse
err = json.Unmarshal(respBody, &statistics)
if err != nil {
return errors.Wrap(err, "unable to unmarshal statistics result")
}
compatibleSoftwareVersions = make(map[string]CompatibleSoftwareVersion)
for _, v := range statistics.SoftwareVersions {
v.Checked = &FalseVariable
compatibleSoftwareVersions[v.Name] = v
}
// Step 1: upload binary
respBody, err = sw.uploadFile(shopwareAPI+"plugins/"+strconv.Itoa(sw.pluginID)+"/binaries", filename, prefix+".zip")
if err != nil {
return errors.Wrap(err, "unable to upload plugin zip")
}
var responses []BinaryUploadResponse
err = json.Unmarshal(respBody, &responses)
if err != nil {
return errors.Wrap(err, "unable to unmarshal upload result")
}
if len(responses) != 1 {
return fmt.Errorf("not the correct amount of responses to binary upload: %d", len(responses))
}
binaryDetails := responses[0]
fmt.Println("Binary uploaded")
// Then we get a big object
// Step 2: verify upload
// GET to https://api.shopware.com/plugins/5998/binaries/23660
// (not sure if required, because object looks identical to upload result)
// status.name == "waitingforcodereview"
// Step 3: set the metadata for this version
binaryDetails.SetChangelog("de_DE", sw.lastChangelogByLocale("de"))
binaryDetails.SetChangelog("en_GB", sw.lastChangelogByLocale("en"))
binaryDetails.CompatibleSoftwareVersions = compatibleVersions(sw.pluginInfo.Compatibility.MinVersion, sw.pluginInfo.Compatibility.MaxVersion)
binaryDetails.LicenseCheckRequired = false
binaryDetails.IonCubeEncrypted = false
binaryDetails.Version = sw.pluginInfo.Version
err = sw.put(shopwareAPI+"plugins/"+strconv.Itoa(sw.pluginID)+"/binaries/"+strconv.Itoa(binaryDetails.ID), binaryDetails)
if err != nil {
return errors.Wrap(err, "unable to upload changelog data")
}
fmt.Println("Meta-data uploaded")
// Step 4a:
fmt.Println()
fmt.Print("Would you like to request a code-review? (y/N) ")
// TODO: Scan for the reply
// Step 4b: POST request to /plugins/5411/reviews
// with empty json payload (optional?)
// to request code review
// Step 5: keep asking https://api.shopware.com/plugins/5411/binaries/23730/checkresults and check for the review
// Step 6: compare type.name for "automaticcodereviewsucceeded", or perhaps "requested"
// Then we might want to verify the file uploaded.
// GET to https://api.shopware.com/plugins/5998/binaries/23660/file?token=f02464d52f2782443447420c7bbafeed5a02e5bd73da91.19108458&shopwareMajorVersion=52
// (we could verify plugin.xml for version number, for example)
// Optionally prompt to restart this circus
// if so: we probably want to delete that newly-uploaded version
// or we have to re-use it and upload the binary again to this version
return nil
}
func (sw *ShopwareClient) get(url string) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("X-Shopware-Token", sw.token)
resp, err := sw.c.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
body, _ := ioutil.ReadAll(resp.Body)
return body, fmt.Errorf("bad status code: %d - %s", resp.StatusCode, string(body))
}
return ioutil.ReadAll(resp.Body)
}
func (sw *ShopwareClient) put(url string, object interface{}) error {
b, err := json.Marshal(object)
if err != nil {
return err
}
req, err := http.NewRequest("PUT", url, bytes.NewReader(b))
if err != nil {
return err
}
req.Header.Set("X-Shopware-Token", sw.token)
req.Header.Set("Content-Type", "application/json")
resp, err := sw.c.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
body, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("bad status code: %d - %s", resp.StatusCode, string(body))
}
return nil
}
func (sw *ShopwareClient) uploadFile(url, file, name string) ([]byte, error) {
// Prepare a form that you will submit to that URL.
var b bytes.Buffer
w := multipart.NewWriter(&b)
// Add your image file
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
fw, err := w.CreateFormFile("name", file)
if err != nil {
return nil, err
}
if _, err = io.Copy(fw, f); err != nil {
return nil, err
}
// Don't forget to close the multipart writer.
// If you don't close it, your request will be missing the terminating boundary.
w.Close()
// Now that you have a form, you can submit it to your handler.
req, err := http.NewRequest("POST", url, &b)
if err != nil {
return nil, err
}
// Don't forget to set the content type, this will contain the boundary.
req.Header.Set("Content-Type", w.FormDataContentType())
req.Header.Set("X-Shopware-Token", sw.token)
// Submit the request
res, err := sw.c.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
// Check the response
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad status: %s", res.Status)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return body, nil
}
type PluginInfoResult struct {
Name string `json:"name"`
LastChange string `json:"lastChange"`
ActivationStatus struct {
Name string `json:"name"`
} `json:"activationStatus"`
Addons Addons `json:"addons"`
ApprovalStatus struct {
Name string `json:"name"`
} `json:"approvalStatus"`
LatestBinary struct {
LastChangeDate string `json:"lastChangeDate"`
Version string `json:"version"`
} `json:"latestBinary"`
}
type Addons []struct {
Name string `json:"name"`
}
func (a Addons) String() string {
var str []string
for _, add := range a {
str = append(str, add.Name)
}
return strings.Join(str, ", ")
}
type ShopwareClient struct {
c *http.Client
cfg *ini.Section
username string
password string
token string
pluginID int
pluginInfo struct {
Version string `xml:"version"`
Changelog []struct {
Version string `xml:"version,attr"`
Changes []struct {
Lang string `xml:"lang,attr"`
Value string `xml:",innerxml"`
} `xml:"changes"`
} `xml:"changelog"`
Compatibility struct {
MinVersion string `xml:"minVersion,attr"`
MaxVersion string `xml:"maxVersion,attr"`
} `xml:"compatibility"`
}
}
func (sw *ShopwareClient) lastChangelogByLocale(locale string) string {
if len(sw.pluginInfo.Changelog) == 0 {
return ""
}
last := sw.pluginInfo.Changelog[len(sw.pluginInfo.Changelog)-1]
for _, ch := range last.Changes {
if ch.Lang == locale {
return ch.Value
}
}
if len(last.Changes) == 0 {
return ""
}
return last.Changes[0].Value
}
func newClient(c *cli.Context, cfg *ini.Section) (*ShopwareClient, error) {
var err error
sw := &ShopwareClient{
c: &http.Client{},
cfg: cfg,
}
sw.c.Jar, err = cookiejar.New(&cookiejar.Options{
Filename: c.String("jar"),
})
if err != nil {
return nil, errors.Wrap(err, "unable to load cookie jar")
}
b, err := ioutil.ReadFile(expandIfNeeded(c.String("credentials")))
if err != nil {
return nil, errors.Wrapf(err, "unable to read credentials file: %s", cfg.Key("credentials").String())
}
lines := bytes.Split(b, []byte("\n"))
for i, line := range lines {
switch i {
case 0:
sw.username = string(bytes.TrimSpace(line))
case 1:
sw.password = string(bytes.TrimSpace(line))
}
}
return sw, nil
}
func (sw *ShopwareClient) login() error {
body, err := json.Marshal(struct {
Username string `json:"shopwareId"`
Password string `json:"password"`
}{sw.username, sw.password})
if err != nil {
return errors.Wrap(err, "unable to prepare login request")
}
resp, err := sw.c.Post(shopwareAPI+"accesstokens", "application/json", bytes.NewReader(body))
if err != nil {
return errors.Wrap(err, "bad result when logging in")
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return fmt.Errorf("unexpected status code when logging in: %d", resp.StatusCode)
}
var data = struct {
Token string `json:"token"`
}{}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&data)
if err != nil {
return errors.Wrap(err, "unable to decode login json response")
}
sw.token = data.Token
return nil
}
func logic(c *cli.Context) error {
cfg, err := loadConfig(c.String("config"))
if err != nil {
return err
}
client, err := newClient(c, cfg)
if err != nil {
return err
}
err = client.login()
if err != nil {
return err
}
err = client.printPluginInfo(cfg.Key("plugin_id").MustInt())
if err != nil {
return err
}
err = client.printNewData()
if err != nil {
return err
}
err = client.update()
if err != nil {
return err
}
return nil
}
func main() {
app := cli.NewApp()
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config",
Value: ".shopware-deploy.ini",
Usage: "Location of the .shopware-deploy.ini file for this plugin",
},
cli.StringFlag{
Name: "jar",
Value: "~/.cache/shopware-deploy",
Usage: "Location of the shopware-deploy cookie jar",
},
cli.StringFlag{
Name: "credentials",
Value: "~/.config/shopware-deploy",
Usage: "Location of the shopware-deploy global settings file",
},
}
app.Name = "Shopware Deployer (unofficial)"
app.Authors = []cli.Author{
{
Name: "Etienne Bruines",
Email: "[email protected]",
},
}
app.Version = "0.0.5.4.5"
app.Action = func(c *cli.Context) error {
err := logic(c)
if err != nil {
fmt.Println("Error:", err.Error())
}
return err
}
app.Run(os.Args)
}