@@ -7,9 +7,15 @@ import (
7
7
"github.com/MG-RAST/AWE/lib/core/cwl"
8
8
cwl_types "github.com/MG-RAST/AWE/lib/core/cwl/types"
9
9
"github.com/MG-RAST/AWE/lib/logger"
10
+ //"github.com/MG-RAST/AWE/lib/logger/event"
11
+ "bytes"
10
12
"github.com/MG-RAST/AWE/lib/shock"
11
13
"github.com/davecgh/go-spew/spew"
12
14
"gopkg.in/yaml.v2"
15
+ "io"
16
+ "io/ioutil"
17
+ "mime/multipart"
18
+ "net/http"
13
19
"net/url"
14
20
"os"
15
21
"path"
@@ -54,6 +60,8 @@ func uploadFile(file *cwl_types.File, inputfile_path string) (err error) {
54
60
55
61
file_path := file .Path
56
62
63
+ basename := path .Base (file_path )
64
+
57
65
if file_path == "" {
58
66
return
59
67
}
@@ -75,35 +83,39 @@ func uploadFile(file *cwl_types.File, inputfile_path string) (err error) {
75
83
}
76
84
spew .Dump (node )
77
85
78
- file .Location_url , err = url .Parse (conf .SERVER_URL + "/node/" + node .Id + "?download" )
86
+ file .Location_url , err = url .Parse (conf .SHOCK_URL + "/node/" + node .Id + "?download" )
79
87
if err != nil {
80
88
return
81
89
}
82
90
83
91
file .Location = file .Location_url .String ()
84
92
file .Path = ""
93
+ file .Basename = basename
85
94
86
95
return
87
96
}
88
97
89
- func processInputData (native interface {}, inputfile_path string ) (err error ) {
98
+ func processInputData (native interface {}, inputfile_path string ) (count int , err error ) {
99
+
90
100
fmt .Printf ("(processInputData) start\n " )
91
101
defer fmt .Printf ("(processInputData) end\n " )
92
102
switch native .(type ) {
93
103
case * cwl.Job_document :
94
-
104
+ fmt . Printf ( "found Job_document \n " )
95
105
job_doc_ptr := native .(* cwl.Job_document )
96
106
97
107
job_doc := * job_doc_ptr
98
108
99
- for key , value := range job_doc {
109
+ for _ , value := range job_doc {
100
110
101
- fmt .Printf ("recurse into key: %s\n " , key )
102
- err = processInputData (value , inputfile_path )
111
+ id := value .GetId ()
112
+ fmt .Printf ("recurse into key: %s\n " , id )
113
+ var sub_count int
114
+ sub_count , err = processInputData (value , inputfile_path )
103
115
if err != nil {
104
116
return
105
117
}
106
-
118
+ count += sub_count
107
119
}
108
120
109
121
return
@@ -123,7 +135,7 @@ func processInputData(native interface{}, inputfile_path string) (err error) {
123
135
if err != nil {
124
136
return
125
137
}
126
-
138
+ count += 1
127
139
return
128
140
default :
129
141
spew .Dump (native )
@@ -135,10 +147,19 @@ func processInputData(native interface{}, inputfile_path string) (err error) {
135
147
}
136
148
137
149
func main () {
150
+ err := main_wrapper ()
151
+ if err != nil {
152
+ println (err .Error ())
153
+ os .Exit (1 )
154
+ }
155
+ os .Exit (0 )
156
+ }
157
+
158
+ func main_wrapper () (err error ) {
138
159
139
160
conf .LOG_OUTPUT = "console"
140
161
141
- err : = conf .Init_conf ("submitter" )
162
+ err = conf .Init_conf ("submitter" )
142
163
143
164
if err != nil {
144
165
fmt .Fprintf (os .Stderr , "ERROR: error reading conf file: " + err .Error ())
@@ -147,23 +168,24 @@ func main() {
147
168
148
169
logger .Initialize ("client" )
149
170
150
- if conf .CWL_JOB == "" {
151
- logger .Error ("cwl job file missing" )
152
- time .Sleep (time .Second )
153
- os .Exit (1 )
171
+ for _ , value := range conf .ARGS {
172
+ println (value )
154
173
}
155
174
156
- inputfile_path := path .Dir (conf .CWL_JOB )
175
+ job_file := conf .ARGS [0 ]
176
+ workflow_file := conf .ARGS [1 ]
177
+
178
+ inputfile_path := path .Dir (job_file )
157
179
fmt .Printf ("job path: %s\n " , inputfile_path ) // needed to resolve relative paths
158
180
159
- job_doc , err := cwl .ParseJob ( conf . CWL_JOB )
181
+ job_doc , err := cwl .ParseJobFile ( job_file )
160
182
if err != nil {
161
183
logger .Error ("error parsing cwl job: %v" , err )
162
184
time .Sleep (time .Second )
163
185
os .Exit (1 )
164
186
}
165
187
166
- fmt .Println ("Job input fter reading from file:" )
188
+ fmt .Println ("Job input after reading from file:" )
167
189
spew .Dump (* job_doc )
168
190
169
191
data , err := yaml .Marshal (* job_doc )
@@ -172,15 +194,23 @@ func main() {
172
194
os .Exit (1 )
173
195
}
174
196
175
- fmt .Printf ("yaml:\n %s\n " , string (data [:]))
197
+ job_doc_string := string (data [:])
198
+ fmt .Printf ("job_doc_string: \" %s\" \n " , job_doc_string )
199
+ if job_doc_string == "" {
200
+ fmt .Println ("job_doc_string is empty" )
201
+ os .Exit (1 )
202
+ }
203
+
204
+ fmt .Printf ("yaml:\n %s\n " , job_doc_string )
176
205
177
206
// process input files
178
207
179
- err = processInputData (job_doc , inputfile_path )
208
+ upload_count , err : = processInputData (job_doc , inputfile_path )
180
209
if err != nil {
181
210
fmt .Printf ("error: %s" , err .Error ())
182
211
os .Exit (1 )
183
212
}
213
+ fmt .Printf ("%d files have been uploaded\n " , upload_count )
184
214
time .Sleep (2 )
185
215
186
216
fmt .Println ("------------Job input after parsing:" )
@@ -192,4 +222,111 @@ func main() {
192
222
193
223
fmt .Printf ("yaml:\n %s\n " , string (data [:]))
194
224
225
+ // job submission example:
226
+ // curl -X POST -F [email protected] -F cwl=@/Users/wolfganggerlach/awe_data/pipeline/CWL/PackedWorkflow/preprocess-fasta.workflow.cwl http://localhost:8001/job
227
+
228
+ //var b bytes.Buffer
229
+ //w := multipart.NewWriter(&b)
230
+ err = SubmitCWLJobToAWE (workflow_file , job_file , & data )
231
+ if err != nil {
232
+ return
233
+ }
234
+
235
+ return
236
+ }
237
+
238
+ func SubmitCWLJobToAWE (workflow_file string , job_file string , data * []byte ) (err error ) {
239
+ multipart := NewMultipartWriter ()
240
+ err = multipart .AddFile ("cwl" , workflow_file )
241
+ if err != nil {
242
+ return
243
+ }
244
+ err = multipart .AddDataAsFile ("job" , job_file , data )
245
+ if err != nil {
246
+ return
247
+ }
248
+ response , err := multipart .Send ("POST" , conf .SERVER_URL + "/job" )
249
+ if err != nil {
250
+ return
251
+ }
252
+ responseData , err := ioutil .ReadAll (response .Body )
253
+ if err != nil {
254
+ return
255
+ }
256
+ responseString := string (responseData )
257
+
258
+ fmt .Println (responseString )
259
+ return
260
+
261
+ }
262
+
263
+ type MultipartWriter struct {
264
+ b bytes.Buffer
265
+ w * multipart.Writer
266
+ }
267
+
268
+ func NewMultipartWriter () * MultipartWriter {
269
+ m := & MultipartWriter {}
270
+ m .w = multipart .NewWriter (& m .b )
271
+ return m
272
+ }
273
+
274
+ func (m * MultipartWriter ) Send (method string , url string ) (response * http.Response , err error ) {
275
+ m .w .Close ()
276
+ fmt .Println ("------------" )
277
+ spew .Dump (m .w )
278
+ fmt .Println ("------------" )
279
+
280
+ req , err := http .NewRequest (method , url , & m .b )
281
+ if err != nil {
282
+ return
283
+ }
284
+ // Don't forget to set the content type, this will contain the boundary.
285
+ req .Header .Set ("Content-Type" , m .w .FormDataContentType ())
286
+
287
+ // Submit the request
288
+ client := & http.Client {}
289
+ fmt .Printf ("%s %s\n \n " , method , url )
290
+ response , err = client .Do (req )
291
+ if err != nil {
292
+ return
293
+ }
294
+
295
+ // Check the response
296
+ //if response.StatusCode != http.StatusOK {
297
+ // err = fmt.Errorf("bad status: %s", response.Status)
298
+ //}
299
+ return
300
+
301
+ }
302
+
303
+ func (m * MultipartWriter ) AddDataAsFile (fieldname string , filepath string , data * []byte ) (err error ) {
304
+
305
+ fw , err := m .w .CreateFormFile (fieldname , filepath )
306
+ if err != nil {
307
+ return
308
+ }
309
+ _ , err = fw .Write (* data )
310
+ if err != nil {
311
+ return
312
+ }
313
+ return
314
+ }
315
+
316
+ func (m * MultipartWriter ) AddFile (fieldname string , filepath string ) (err error ) {
317
+
318
+ f , err := os .Open (filepath )
319
+ if err != nil {
320
+ return
321
+ }
322
+ defer f .Close ()
323
+ fw , err := m .w .CreateFormFile (fieldname , filepath )
324
+ if err != nil {
325
+ return
326
+ }
327
+ if _ , err = io .Copy (fw , f ); err != nil {
328
+ return
329
+ }
330
+
331
+ return
195
332
}
0 commit comments