-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker_upload_linux.go
187 lines (152 loc) · 4.79 KB
/
worker_upload_linux.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
package main
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"github.com/pkg/errors"
)
const (
itmspMetadataTemplateSource = `<?xml version="1.0" encoding="UTF-8"?>
<package version="software5.9" xmlns="http://apple.com/itunes/importer">
<software_assets
app_platform="osx"
primary_bundle_identifier="{{ .PrimaryBundleId }}">
<asset type="developer-id-package">
<data_file>
<file_name>{{ .FileName }}</file_name>
<checksum type="md5">{{ .Checksum }}</checksum>
<size>{{ .ByteSize }}</size>
</data_file>
</asset>
</software_assets>
</package>`
)
var (
itmspMetadataTemplate = template.Must(template.New("metadata").Parse(itmspMetadataTemplateSource))
)
type itmspMetadata struct {
PrimaryBundleId string
FileName string
Checksum string
ByteSize int
}
func (metadata itmspMetadata) WriteTo(dst io.Writer) (int64, error) {
return 1, itmspMetadataTemplate.Execute(dst, metadata)
}
func (worker *Worker) uploadForNotarization() (*NotarizationUpload, error) {
if len(worker.zipFile) > 0 {
defer os.Remove(worker.zipFile)
}
if strings.HasPrefix(worker.config.Password, "@keychain") {
return nil, errors.New("usage of '@keychain' in password is not supported on linux")
}
itmspPackage, err := worker.createITMSP()
if err != nil {
return nil, errors.Wrap(err, "create ITMSP package")
}
defer os.RemoveAll(itmspPackage)
cmd := exec.Command("iTMSTransporter")
cmd.Args = append(cmd.Args,
"-m", "upload",
"-vp", "json",
"-f", itmspPackage,
"-u", worker.config.Username,
"-p", worker.config.Password,
"-distribution", "DeveloperId",
"-primaryBundleId", worker.target.BundleID,
)
if len(worker.config.TeamID) > 0 {
cmd.Args = append(cmd.Args, "-itc_provider", worker.config.TeamID)
}
cmdOut, err := cmd.CombinedOutput()
output, err2 := worker.processITMSOutput(string(cmdOut))
if err2 != nil {
return nil, errors.Wrap(err, "unmarshal command output")
}
switch {
case len(output.ProductErrors) > 0:
return nil, errors.Wrap(output.ProductErrors[0], "execute iTMSTransporter")
case err != nil:
return nil, errors.Wrap(err, "execute iTMSTransporter")
default:
return &output.Upload, nil
}
}
func (worker *Worker) createITMSP() (string, error) {
itmspDir, err := os.MkdirTemp("", "*.itmsp")
if err != nil {
return "", errors.Wrap(err, "create temporary itmsp package directory")
}
srcFile := worker.target.File
if len(worker.zipFile) > 0 {
srcFile = worker.zipFile
}
file, err := os.Open(srcFile)
if err != nil {
return "", errors.Wrap(err, "open source file")
}
defer file.Close()
dstFile, err := os.OpenFile(filepath.Join(itmspDir, filepath.Base(srcFile)), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return "", errors.Wrap(err, "create temporary file in itmsp package")
}
defer dstFile.Close()
hash := md5.New()
dst := io.MultiWriter(hash, dstFile)
byteSize, err := io.Copy(dst, file)
if err != nil {
return "", errors.Wrap(err, "copy file into itmsp package")
}
metadata := itmspMetadata{
PrimaryBundleId: worker.target.BundleID,
FileName: filepath.Base(srcFile),
Checksum: hex.EncodeToString(hash.Sum(nil)),
ByteSize: int(byteSize),
}
metadataFile, err := os.OpenFile(filepath.Join(itmspDir, "metadata.xml"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return "", errors.Wrap(err, "create metadata file in itmsp package")
}
defer metadataFile.Close()
if _, err = metadata.WriteTo(metadataFile); err != nil {
return "", errors.Wrap(err, "write itmsp package metadata")
}
return itmspDir, nil
}
func (*Worker) processITMSOutput(output string) (*CommandOutput, error) {
cmdOut := new(CommandOutput)
if len(output) == 0 {
return cmdOut, nil
}
if jsonIndex := strings.Index(output, "JSON-START>>"); jsonIndex > -1 {
output = output[jsonIndex+len("JSON-START>>"):]
output = output[:strings.Index(output, "<<JSON-END")]
outputData := struct {
Results struct {
UploadId string `json:"upload_id"`
} `json:"dev-id-results"`
}{}
if err := json.Unmarshal([]byte(output), &outputData); err != nil {
return nil, errors.Wrap(err, "parse json output")
}
cmdOut.Upload.RequestUUID = outputData.Results.UploadId
return cmdOut, nil
}
if errorIndex := strings.Index(output, "ERROR: ERROR ITMS-"); errorIndex > 0 {
output = output[errorIndex:]
output = output[:strings.IndexByte(output, '\n')]
errMessage := output[strings.IndexByte(output, '"')+1:]
errMessage = errMessage[:strings.LastIndexByte(errMessage, '"')]
cmdOut.ProductErrors = []ProductError{{
Message: errMessage,
}}
return cmdOut, nil
}
return nil, errors.New("found neither a success or error in ITMS output")
}