forked from apache/openwhisk-wskdeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanifest_parser.go
1506 lines (1330 loc) · 51.9 KB
/
manifest_parser.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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package parsers
import (
"encoding/base64"
"encoding/json"
"errors"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
"net/url"
"github.com/apache/openwhisk-client-go/whisk"
"github.com/apache/openwhisk-wskdeploy/conductor"
"github.com/apache/openwhisk-wskdeploy/dependencies"
"github.com/apache/openwhisk-wskdeploy/runtimes"
"github.com/apache/openwhisk-wskdeploy/utils"
"github.com/apache/openwhisk-wskdeploy/webaction"
"github.com/apache/openwhisk-wskdeploy/wskderrors"
"github.com/apache/openwhisk-wskdeploy/wskenv"
"github.com/apache/openwhisk-wskdeploy/wski18n"
"github.com/apache/openwhisk-wskdeploy/wskprint"
yamlHelper "github.com/ghodss/yaml"
)
const (
API = "API"
HTTPS = "https://"
HTTP = "http://"
API_VERSION = "v1"
WEB = "web"
PATH_SEPARATOR = "/"
DEFAULT_PACKAGE = "default"
NATIVE_DOCKER_IMAGE = "openwhisk/dockerskeleton"
PARAM_OPENING_BRACKET = "{"
PARAM_CLOSING_BRACKET = "}"
DUMMY_APIGW_ACCESS_TOKEN = "DUMMY TOKEN"
YAML_FILE_EXTENSION = "yaml"
YML_FILE_EXTENSION = "yml"
)
// Read existing manifest file or create new if none exists
func ReadOrCreateManifest() (*YAML, error) {
maniyaml := YAML{}
if _, err := os.Stat(utils.ManifestFileNameYaml); err == nil {
dat, _ := ioutil.ReadFile(utils.ManifestFileNameYaml)
err := NewYAMLParser().Unmarshal(dat, &maniyaml)
if err != nil {
return &maniyaml, wskderrors.NewFileReadError(utils.ManifestFileNameYaml, err.Error())
}
}
return &maniyaml, nil
}
// Serialize manifest to local file
func Write(manifest *YAML, filename string) error {
output, err := NewYAMLParser().marshal(manifest)
if err != nil {
return wskderrors.NewYAMLFileFormatError(filename, err.Error())
}
f, err := os.Create(filename)
if err != nil {
return wskderrors.NewFileReadError(filename, err.Error())
}
defer f.Close()
f.Write(output)
return nil
}
func (dm *YAMLParser) Unmarshal(input []byte, manifest *YAML) error {
err := yaml.UnmarshalStrict(input, manifest)
if err != nil {
return err
}
return nil
}
func (dm *YAMLParser) marshal(manifest *YAML) (output []byte, err error) {
data, err := yaml.Marshal(manifest)
if err != nil {
return nil, err
}
return data, nil
}
func (dm *YAMLParser) ParseManifest(manifestPath string) (*YAML, error) {
mm := NewYAMLParser()
maniyaml := YAML{}
content, err := utils.Read(manifestPath)
if err != nil {
return &maniyaml, wskderrors.NewFileReadError(manifestPath, err.Error())
}
err = mm.Unmarshal(content, &maniyaml)
if err != nil {
return &maniyaml, wskderrors.NewYAMLParserErr(manifestPath, err)
}
maniyaml.Filepath = manifestPath
manifest := ReadEnvVariable(&maniyaml)
return manifest, nil
}
func (dm *YAMLParser) composeInputs(inputs map[string]Parameter, packageInputs PackageInputs, manifestFilePath string) (whisk.KeyValueArr, error) {
var errorParser error
keyValArr := make(whisk.KeyValueArr, 0)
var inputsWithoutValue []string
var paramsCLI interface{}
if len(utils.Flags.Param) > 0 {
paramsCLI, errorParser = utils.GetJSONFromStrings(utils.Flags.Param, false)
if errorParser != nil {
return nil, errorParser
}
}
for name, param := range inputs {
var keyVal whisk.KeyValue
// keyvalue key is set to parameter name
keyVal.Key = name
// parameter on CLI takes the highest precedence such that
// input variables gets values from CLI first
if paramsCLI != nil {
// check if this particular input is specified on CLI
if v, ok := paramsCLI.(map[string]interface{})[name]; ok {
keyVal.Value = wskenv.ConvertSingleName(v.(string))
}
}
// if those inputs are not specified on CLI,
// read their values from the manifest file
if keyVal.Value == nil {
keyVal.Value, errorParser = ResolveParameter(name, ¶m, manifestFilePath)
if errorParser != nil {
return nil, errorParser
}
if param.Type == STRING && param.Value != nil {
if keyVal.Value == getTypeDefaultValue(param.Type) {
if packageInputs.Inputs != nil {
n := wskenv.GetEnvVarName(param.Value.(string))
if v, ok := packageInputs.Inputs[n]; ok {
keyVal.Value = v.Value.(string)
}
}
}
}
}
if keyVal.Value != nil {
keyValArr = append(keyValArr, keyVal)
}
if param.Required && keyVal.Value == getTypeDefaultValue(param.Type) {
inputsWithoutValue = append(inputsWithoutValue, name)
}
}
if len(inputsWithoutValue) > 0 {
errMessage := wski18n.T(wski18n.ID_ERR_REQUIRED_INPUTS_MISSING_VALUE_X_inputs_X,
map[string]interface{}{
wski18n.KEY_INPUTS: strings.Join(inputsWithoutValue, ", ")})
return nil, wskderrors.NewYAMLFileFormatError(manifestFilePath, errMessage)
}
return keyValArr, nil
}
func (dm *YAMLParser) composeAnnotations(annotations map[string]interface{}) whisk.KeyValueArr {
listOfAnnotations := make(whisk.KeyValueArr, 0)
for name, value := range annotations {
var keyVal whisk.KeyValue
keyVal.Key = name
value = wskenv.InterpolateStringWithEnvVar(value)
keyVal.Value = utils.ConvertInterfaceValue(value)
listOfAnnotations = append(listOfAnnotations, keyVal)
}
return listOfAnnotations
}
func (dm *YAMLParser) ComposeDependenciesFromAllPackages(manifest *YAML, projectPath string, filePath string, managedAnnotations whisk.KeyValue, packageInputs map[string]PackageInputs) (map[string]dependencies.DependencyRecord, error) {
dependencies := make(map[string]dependencies.DependencyRecord)
packages := make(map[string]Package)
if len(manifest.Packages) != 0 {
packages = manifest.Packages
} else {
packages = manifest.GetProject().Packages
}
for n, p := range packages {
d, err := dm.ComposeDependencies(p, projectPath, filePath, n, managedAnnotations, packageInputs[n])
if err == nil {
for k, v := range d {
dependencies[k] = v
}
} else {
return nil, err
}
}
return dependencies, nil
}
func (dm *YAMLParser) ComposeDependencies(pkg Package, projectPath string, filePath string, packageName string, managedAnnotations whisk.KeyValue, packageInputs PackageInputs) (map[string]dependencies.DependencyRecord, error) {
depMap := make(map[string]dependencies.DependencyRecord)
for key, dependency := range pkg.Dependencies {
version := dependency.Version
if len(version) == 0 {
version = YAML_VALUE_BRANCH_MASTER
}
location := dependency.Location
isBinding := false
if dependencies.LocationIsBinding(location) {
if !strings.HasPrefix(location, PATH_SEPARATOR) {
location = PATH_SEPARATOR + dependency.Location
}
isBinding = true
} else if dependencies.LocationIsGithub(location) {
// TODO() define const for the protocol prefix, etc.
_, err := url.ParseRequestURI(location)
if err != nil {
location = HTTPS + dependency.Location
location = wskenv.InterpolateStringWithEnvVar(location).(string)
}
isBinding = false
} else {
// TODO() create new named error in wskerrors package
return nil, errors.New(wski18n.T(wski18n.ID_ERR_DEPENDENCY_UNKNOWN_TYPE))
}
inputs, err := dm.composeInputs(dependency.Inputs, packageInputs, filePath)
if err != nil {
return nil, err
}
annotations := dm.composeAnnotations(dependency.Annotations)
if utils.Flags.Managed || utils.Flags.Sync {
annotations = append(annotations, managedAnnotations)
}
packDir := path.Join(projectPath, strings.Title(YAML_KEY_PACKAGES))
depName := packageName + ":" + key
depMap[depName] = dependencies.NewDependencyRecord(packDir, packageName, location, version, inputs, annotations, isBinding)
}
return depMap, nil
}
func (dm *YAMLParser) ComposeAllPackages(projectInputs map[string]Parameter, manifest *YAML, filePath string, managedAnnotations whisk.KeyValue) (map[string]*whisk.Package, map[string]PackageInputs, error) {
packages := map[string]*whisk.Package{}
manifestPackages := make(map[string]Package)
inputs := make(map[string]PackageInputs, 0)
if len(manifest.Packages) != 0 {
manifestPackages = manifest.Packages
} else {
manifestPackages = manifest.GetProject().Packages
}
if len(manifestPackages) == 0 {
warningString := wski18n.T(
wski18n.ID_WARN_PACKAGES_NOT_FOUND_X_path_X,
map[string]interface{}{
wski18n.KEY_PATH: manifest.Filepath})
wskprint.PrintOpenWhiskWarning(warningString)
}
// Compose each package found in manifest
for n, p := range manifestPackages {
s, params, err := dm.ComposePackage(p, n, filePath, managedAnnotations, projectInputs)
if err != nil {
return nil, inputs, err
}
packages[n] = s
inputs[n] = PackageInputs{PackageName: n, Inputs: params}
}
return packages, inputs, nil
}
func (dm *YAMLParser) composePackageInputs(projectInputs map[string]Parameter, rawInputs map[string]Parameter, filepath string) (map[string]Parameter, whisk.KeyValueArr, error) {
inputs := make(map[string]Parameter, 0)
// package inherits all project inputs
for n, param := range projectInputs {
inputs[n] = param
}
// iterate over package inputs
for name, i := range rawInputs {
value, err := ResolveParameter(name, &i, filepath)
if err != nil {
return nil, nil, err
}
// if value is set to default value for its type,
// check for input key being an env. variable itself
if value == getTypeDefaultValue(i.Type) {
value = wskenv.InterpolateStringWithEnvVar("${" + name + "}")
}
// if at this point, still value is set to default value of its type
// check if input key is defined under Project Inputs
if value == getTypeDefaultValue(i.Type) {
if i.Type == STRING && i.Value != nil {
n := wskenv.GetEnvVarName(i.Value.(string))
if v, ok := projectInputs[n]; ok {
value = v.Value.(string)
}
}
}
// create a Parameter object based on the package inputs
// resolve the value using env. variables
// if value is not specified, treat input key as an env. variable
// check if input key is defined in environment
// else set it to its default value based on the type for now
// the input value will be updated if its specified in deployment
// or on CLI using --param or --param-file
i.Value = value
inputs[name] = i
}
// create an array of Key/Value pair with inputs
// inputs name as key and with its value if its not nil
keyValArr := make(whisk.KeyValueArr, 0)
for name, param := range inputs {
var keyVal whisk.KeyValue
keyVal.Key = name
keyVal.Value = param.Value
if keyVal.Value != nil {
keyValArr = append(keyValArr, keyVal)
}
}
return inputs, keyValArr, nil
}
func (dm *YAMLParser) ComposePackage(pkg Package, packageName string, filePath string, managedAnnotations whisk.KeyValue, projectInputs map[string]Parameter) (*whisk.Package, map[string]Parameter, error) {
pag := &whisk.Package{}
pag.Name = packageName
//The namespace for this package is absent, so we use default guest here.
pag.Namespace = pkg.Namespace
pub := false
pag.Publish = &pub
//Version is a mandatory value
//If it is an empty string, it will be set to default value
//And print an warning message
// TODO(#673) implement STRICT flag
if pkg.Version == "" {
warningString := wski18n.T(
wski18n.ID_WARN_KEY_MISSING_X_key_X_value_X,
map[string]interface{}{
wski18n.KEY_KEY: wski18n.PACKAGE_VERSION,
wski18n.KEY_VALUE: DEFAULT_PACKAGE_VERSION})
wskprint.PrintOpenWhiskWarning(warningString)
warningString = wski18n.T(
wski18n.ID_WARN_KEYVALUE_NOT_SAVED_X_key_X,
map[string]interface{}{wski18n.KEY_KEY: wski18n.PACKAGE_VERSION})
wskprint.PrintOpenWhiskWarning(warningString)
pkg.Version = DEFAULT_PACKAGE_VERSION
}
pag.Version = wskenv.ConvertSingleName(pkg.Version)
//License is a mandatory value
//set license to unknown if it is an empty string
//And print an warning message
// TODO(#673) implement STRICT flag
if pkg.License == "" {
warningString := wski18n.T(
wski18n.ID_WARN_KEY_MISSING_X_key_X_value_X,
map[string]interface{}{
wski18n.KEY_KEY: wski18n.PACKAGE_LICENSE,
wski18n.KEY_VALUE: DEFAULT_PACKAGE_LICENSE})
wskprint.PrintOpenWhiskWarning(warningString)
warningString = wski18n.T(
wski18n.ID_WARN_KEYVALUE_NOT_SAVED_X_key_X,
map[string]interface{}{wski18n.KEY_KEY: wski18n.PACKAGE_VERSION})
wskprint.PrintOpenWhiskWarning(warningString)
pkg.License = DEFAULT_PACKAGE_LICENSE
} else {
utils.CheckLicense(pkg.License)
}
// package inputs are set as package inputs of type Parameter{}
// read all package inputs, interpolate their values using env. variables
// check if input variable itself is an env. variable
packageInputs, inputs, err := dm.composePackageInputs(projectInputs, pkg.Inputs, filePath)
if err != nil {
return nil, nil, err
}
if len(inputs) > 0 {
pag.Parameters = inputs
}
// set Package Annotations
listOfAnnotations := dm.composeAnnotations(pkg.Annotations)
if len(listOfAnnotations) > 0 {
pag.Annotations = append(pag.Annotations, listOfAnnotations...)
}
// add Managed Annotations if this is Managed Deployment
if utils.Flags.Managed || utils.Flags.Sync {
pag.Annotations = append(pag.Annotations, managedAnnotations)
}
// "default" package is a reserved package name
// and in this case wskdeploy deploys openwhisk entities under
// /namespace instead of /namespace/package
if strings.ToLower(pag.Name) == DEFAULT_PACKAGE {
wskprint.PrintlnOpenWhiskVerbose(utils.Flags.Verbose, wski18n.T(wski18n.ID_MSG_DEFAULT_PACKAGE))
// when a package is marked public with "Public: true" in manifest file
// the package is visible to anyone and created with publish state
// set to shared otherwise publish state is set to private
} else if pkg.Public {
warningMsg := wski18n.T(wski18n.ID_WARN_PACKAGE_IS_PUBLIC_X_package_X,
map[string]interface{}{
wski18n.KEY_PACKAGE: pag.Name})
wskprint.PrintlnOpenWhiskWarning(warningMsg)
pag.Publish = &(pkg.Public)
}
return pag, packageInputs, nil
}
func (dm *YAMLParser) ComposeSequencesFromAllPackages(namespace string, mani *YAML, manifestFilePath string, managedAnnotations whisk.KeyValue, packageInputs map[string]PackageInputs) ([]utils.ActionRecord, error) {
var sequences []utils.ActionRecord = make([]utils.ActionRecord, 0)
manifestPackages := make(map[string]Package)
if len(mani.Packages) != 0 {
manifestPackages = mani.Packages
} else {
manifestPackages = mani.GetProject().Packages
}
for n, p := range manifestPackages {
s, err := dm.ComposeSequences(namespace, p.Sequences, n, manifestFilePath, managedAnnotations, packageInputs[n])
if err == nil {
sequences = append(sequences, s...)
} else {
return nil, err
}
}
return sequences, nil
}
func (dm *YAMLParser) ComposeSequences(namespace string, sequences map[string]Sequence, packageName string, manifestFilePath string, managedAnnotations whisk.KeyValue, packageInputs PackageInputs) ([]utils.ActionRecord, error) {
var listOfSequences []utils.ActionRecord = make([]utils.ActionRecord, 0)
var errorParser error
for key, sequence := range sequences {
wskaction := new(whisk.Action)
wskaction.Exec = new(whisk.Exec)
wskaction.Exec.Kind = YAML_KEY_SEQUENCE
actionList := strings.Split(sequence.Actions, ",")
var components []string
for _, a := range actionList {
act := strings.TrimSpace(a)
if !strings.ContainsRune(act, []rune(PATH_SEPARATOR)[0]) && !strings.HasPrefix(act, packageName+PATH_SEPARATOR) &&
strings.ToLower(packageName) != DEFAULT_PACKAGE {
act = path.Join(packageName, act)
}
components = append(components, path.Join(PATH_SEPARATOR+namespace, act))
}
wskaction.Exec.Components = components
if i, ok := packageInputs.Inputs[wskenv.GetEnvVarName(key)]; ok {
wskaction.Name = i.Value.(string)
} else {
wskaction.Name = wskenv.ConvertSingleName(key)
}
pub := false
wskaction.Publish = &pub
wskaction.Namespace = namespace
annotations := dm.composeAnnotations(sequence.Annotations)
if len(annotations) > 0 {
wskaction.Annotations = annotations
}
// appending managed annotations if its a managed deployment
if utils.Flags.Managed || utils.Flags.Sync {
wskaction.Annotations = append(wskaction.Annotations, managedAnnotations)
}
// Web Export
// Treat sequence as a web action, a raw HTTP web action, or as a standard action based on web-export;
// when web-export is set to yes | true, treat sequence as a web action,
// when web-export is set to raw, treat sequence as a raw HTTP web action,
// when web-export is set to no | false, treat sequence as a standard action
if len(sequence.Web) != 0 {
wskaction.Annotations, errorParser = webaction.SetWebActionAnnotations(manifestFilePath, wskaction.Name, sequence.Web, wskaction.Annotations, false)
if errorParser != nil {
return nil, errorParser
}
}
// TODO Add web-secure support for sequences?
record := utils.ActionRecord{Action: wskaction, Packagename: packageName, Filepath: key}
listOfSequences = append(listOfSequences, record)
}
return listOfSequences, nil
}
func (dm *YAMLParser) ComposeActionsFromAllPackages(manifest *YAML, filePath string, managedAnnotations whisk.KeyValue, packageInputs map[string]PackageInputs) ([]utils.ActionRecord, error) {
var actions []utils.ActionRecord = make([]utils.ActionRecord, 0)
manifestPackages := make(map[string]Package)
if len(manifest.Packages) != 0 {
manifestPackages = manifest.Packages
} else {
manifestPackages = manifest.GetProject().Packages
}
for n, p := range manifestPackages {
a, err := dm.ComposeActions(filePath, p.Actions, n, managedAnnotations, packageInputs[n])
if err == nil {
actions = append(actions, a...)
} else {
return nil, err
}
}
return actions, nil
}
func (dm *YAMLParser) validateActionCode(manifestFilePath string, action Action) error {
// Check if action.Function is specified with action.Code
// with action.Code, action.Function is not allowed
// with action.Code, action.Runtime should be specified
if len(action.Function) != 0 {
err := wski18n.T(wski18n.ID_ERR_ACTION_INVALID_X_action_X,
map[string]interface{}{
wski18n.KEY_ACTION: action.Name})
return wskderrors.NewYAMLFileFormatError(manifestFilePath, err)
}
if len(action.Runtime) == 0 {
err := wski18n.T(wski18n.ID_ERR_ACTION_MISSING_RUNTIME_WITH_CODE_X_action_X,
map[string]interface{}{
wski18n.KEY_ACTION: action.Name})
return wskderrors.NewYAMLFileFormatError(manifestFilePath, err)
}
return nil
}
func (dm *YAMLParser) readActionCode(manifestFilePath string, action Action) (*whisk.Exec, error) {
exec := new(whisk.Exec)
if err := dm.validateActionCode(manifestFilePath, action); err != nil {
return nil, err
}
// validate runtime from the manifest file
// error out if the specified runtime is not valid or not supported
// even if runtime is invalid, deploy action with specified runtime in strict mode
if utils.Flags.Strict {
exec.Kind = action.Runtime
} else if runtimes.CheckExistRuntime(action.Runtime, runtimes.SupportedRunTimes) {
exec.Kind = action.Runtime
} else if len(runtimes.DefaultRunTimes[action.Runtime]) != 0 {
exec.Kind = runtimes.DefaultRunTimes[action.Runtime]
} else {
err := wski18n.T(wski18n.ID_ERR_RUNTIME_INVALID_X_runtime_X_action_X,
map[string]interface{}{
wski18n.KEY_RUNTIME: action.Runtime,
wski18n.KEY_ACTION: action.Name})
return nil, wskderrors.NewYAMLFileFormatError(manifestFilePath, err)
}
exec.Code = &(action.Code)
// we can specify the name of the action entry point using main
if len(action.Main) != 0 {
exec.Main = action.Main
}
return exec, nil
}
func (dm *YAMLParser) validateActionFunction(manifestFileName string, action Action, ext string, kind string) error {
// produce an error when a runtime could not be derived from the action file extension
// and its not explicitly specified in the manifest YAML file
// and action source is not a zip file
if len(action.Runtime) == 0 && len(action.Docker) == 0 && !action.Native {
if ext == runtimes.ZIP_FILE_EXTENSION {
errMessage := wski18n.T(wski18n.ID_ERR_RUNTIME_INVALID_X_runtime_X_action_X,
map[string]interface{}{
wski18n.KEY_RUNTIME: runtimes.RUNTIME_NOT_SPECIFIED,
wski18n.KEY_ACTION: action.Name})
return wskderrors.NewInvalidRuntimeError(errMessage,
manifestFileName,
action.Name,
runtimes.RUNTIME_NOT_SPECIFIED,
runtimes.ListOfSupportedRuntimes(runtimes.SupportedRunTimes))
} else if len(kind) == 0 {
errMessage := wski18n.T(wski18n.ID_ERR_RUNTIME_ACTION_SOURCE_NOT_SUPPORTED_X_ext_X_action_X,
map[string]interface{}{
wski18n.KEY_EXTENSION: ext,
wski18n.KEY_ACTION: action.Name})
return wskderrors.NewInvalidRuntimeError(errMessage,
manifestFileName,
action.Name,
runtimes.RUNTIME_NOT_SPECIFIED,
runtimes.ListOfSupportedRuntimes(runtimes.SupportedRunTimes))
}
}
return nil
}
func (dm *YAMLParser) readActionFunction(manifestFilePath string, manifestFileName string, action Action) (string, *whisk.Exec, error) {
var actionFilePath string
var zipFileName string
exec := new(whisk.Exec)
f := wskenv.InterpolateStringWithEnvVar(action.Function)
interpolatedActionFunction := f.(string)
// check if action function is pointing to an URL
// we do not support if function is pointing to remote directory
// therefore error out if there is a combination of http/https ending in a directory
if strings.HasPrefix(interpolatedActionFunction, HTTP) || strings.HasPrefix(interpolatedActionFunction, HTTPS) {
if len(path.Ext(interpolatedActionFunction)) == 0 {
err := wski18n.T(wski18n.ID_ERR_ACTION_FUNCTION_REMOTE_DIR_NOT_SUPPORTED_X_action_X_url_X,
map[string]interface{}{
wski18n.KEY_ACTION: action.Name,
wski18n.KEY_URL: interpolatedActionFunction})
return actionFilePath, nil, wskderrors.NewYAMLFileFormatError(manifestFilePath, err)
}
actionFilePath = interpolatedActionFunction
} else {
actionFilePath = strings.TrimRight(manifestFilePath, manifestFileName) + interpolatedActionFunction
}
if utils.IsDirectory(actionFilePath) {
zipFileName = actionFilePath + "." + runtimes.ZIP_FILE_EXTENSION
err := utils.NewZipWriter(actionFilePath, zipFileName, action.Include, action.Exclude, filepath.Dir(manifestFilePath)).Zip()
if err != nil {
return actionFilePath, nil, err
}
defer os.Remove(zipFileName)
actionFilePath = zipFileName
}
action.Function = actionFilePath
// determine extension of the given action source file
ext := filepath.Ext(actionFilePath)
// drop the "." from file extension
if len(ext) > 0 && ext[0] == '.' {
ext = ext[1:]
}
// determine default runtime for the given file extension
var kind string
r := runtimes.FileExtensionRuntimeKindMap[ext]
kind = runtimes.DefaultRunTimes[r]
if err := dm.validateActionFunction(manifestFileName, action, ext, kind); err != nil {
return actionFilePath, nil, err
}
exec.Kind = kind
dat, err := utils.Read(actionFilePath)
if err != nil {
return actionFilePath, nil, err
}
code := string(dat)
if ext == runtimes.ZIP_FILE_EXTENSION || ext == runtimes.JAR_FILE_EXTENSION {
code = base64.StdEncoding.EncodeToString([]byte(dat))
}
exec.Code = &code
/*
* Action.Runtime
* Perform few checks if action runtime is specified in manifest YAML file
* (1) Check if specified runtime is one of the supported runtimes by OpenWhisk server
* (2) Check if specified runtime is consistent with action source file extensions
* Set the action runtime to match with the source file extension, if wskdeploy is not invoked in strict mode
*/
if len(action.Runtime) != 0 {
if runtimes.CheckExistRuntime(action.Runtime, runtimes.SupportedRunTimes) {
// for zip actions, rely on the runtimes from the manifest file as it can not be derived from the action source file extension
// pick runtime from manifest file if its supported by OpenWhisk server
if ext == runtimes.ZIP_FILE_EXTENSION {
exec.Kind = action.Runtime
} else {
if runtimes.CheckRuntimeConsistencyWithFileExtension(ext, action.Runtime) {
exec.Kind = action.Runtime
} else {
warnStr := wski18n.T(wski18n.ID_ERR_RUNTIME_MISMATCH_X_runtime_X_ext_X_action_X,
map[string]interface{}{
wski18n.KEY_RUNTIME: action.Runtime,
wski18n.KEY_EXTENSION: ext,
wski18n.KEY_ACTION: action.Name})
wskprint.PrintOpenWhiskWarning(warnStr)
// even if runtime is not consistent with file extension, deploy action with specified runtime in strict mode
if utils.Flags.Strict {
exec.Kind = action.Runtime
} else {
warnStr := wski18n.T(wski18n.ID_WARN_RUNTIME_CHANGED_X_runtime_X_action_X,
map[string]interface{}{
wski18n.KEY_RUNTIME: exec.Kind,
wski18n.KEY_ACTION: action.Name})
wskprint.PrintOpenWhiskWarning(warnStr)
}
}
}
} else {
warnStr := wski18n.T(wski18n.ID_ERR_RUNTIME_INVALID_X_runtime_X_action_X,
map[string]interface{}{
wski18n.KEY_RUNTIME: action.Runtime,
wski18n.KEY_ACTION: action.Name})
wskprint.PrintOpenWhiskWarning(warnStr)
if ext == runtimes.ZIP_FILE_EXTENSION {
// for zip action, error out if specified runtime is not supported by
// OpenWhisk server
return actionFilePath, nil, wskderrors.NewInvalidRuntimeError(warnStr,
manifestFileName,
action.Name,
action.Runtime,
runtimes.ListOfSupportedRuntimes(runtimes.SupportedRunTimes))
} else {
if utils.Flags.Strict {
exec.Kind = action.Runtime
} else {
warnStr := wski18n.T(wski18n.ID_WARN_RUNTIME_CHANGED_X_runtime_X_action_X,
map[string]interface{}{
wski18n.KEY_RUNTIME: exec.Kind,
wski18n.KEY_ACTION: action.Name})
wskprint.PrintOpenWhiskWarning(warnStr)
}
}
}
}
// we can specify the name of the action entry point using main
if len(action.Main) != 0 {
exec.Main = action.Main
}
return actionFilePath, exec, nil
}
func (dm *YAMLParser) composeActionExec(manifestFilePath string, manifestFileName string, action Action) (string, *whisk.Exec, error) {
var actionFilePath string
exec := new(whisk.Exec)
var err error
if len(action.Code) != 0 {
exec, err = dm.readActionCode(manifestFilePath, action)
if err != nil {
return actionFilePath, nil, err
}
}
if len(action.Function) != 0 {
actionFilePath, exec, err = dm.readActionFunction(manifestFilePath, manifestFileName, action)
if err != nil {
return actionFilePath, nil, err
}
}
// when an action has Docker image specified,
// set exec.Kind to "blackbox" and
// set exec.Image to specified image e.g. dockerhub/image
// when an action Native is set to true,
// set exec.Image to openwhisk/skeleton
if len(action.Docker) != 0 || action.Native {
exec.Kind = runtimes.BLACKBOX
if action.Native {
exec.Image = NATIVE_DOCKER_IMAGE
} else {
exec.Image = wskenv.InterpolateStringWithEnvVar(action.Docker).(string)
}
}
return actionFilePath, exec, err
}
func (dm *YAMLParser) validateActionLimits(limits Limits) {
// TODO() use LIMITS_UNSUPPORTED in yamlparser to enumerate through instead of hardcoding
// emit warning errors if these limits are not nil
utils.NotSupportLimits(limits.ConcurrentActivations, LIMIT_VALUE_CONCURRENT_ACTIVATIONS)
utils.NotSupportLimits(limits.UserInvocationRate, LIMIT_VALUE_USER_INVOCATION_RATE)
utils.NotSupportLimits(limits.CodeSize, LIMIT_VALUE_CODE_SIZE)
utils.NotSupportLimits(limits.ParameterSize, LIMIT_VALUE_PARAMETER_SIZE)
}
func (dm *YAMLParser) composeActionLimits(limits Limits) *whisk.Limits {
dm.validateActionLimits(limits)
wsklimits := new(whisk.Limits)
for _, t := range LIMITS_SUPPORTED {
switch t {
case LIMIT_VALUE_TIMEOUT:
if utils.LimitsTimeoutValidation(limits.Timeout) {
wsklimits.Timeout = limits.Timeout
} else {
warningString := wski18n.T(wski18n.ID_WARN_LIMIT_IGNORED_X_limit_X,
map[string]interface{}{wski18n.KEY_LIMIT: LIMIT_VALUE_TIMEOUT})
wskprint.PrintOpenWhiskWarning(warningString)
}
case LIMIT_VALUE_MEMORY_SIZE:
if utils.LimitsMemoryValidation(limits.Memory) {
wsklimits.Memory = limits.Memory
} else {
warningString := wski18n.T(wski18n.ID_WARN_LIMIT_IGNORED_X_limit_X,
map[string]interface{}{wski18n.KEY_LIMIT: LIMIT_VALUE_MEMORY_SIZE})
wskprint.PrintOpenWhiskWarning(warningString)
}
case LIMIT_VALUE_LOG_SIZE:
if utils.LimitsLogsizeValidation(limits.Logsize) {
wsklimits.Logsize = limits.Logsize
} else {
warningString := wski18n.T(wski18n.ID_WARN_LIMIT_IGNORED_X_limit_X,
map[string]interface{}{wski18n.KEY_LIMIT: LIMIT_VALUE_LOG_SIZE})
wskprint.PrintOpenWhiskWarning(warningString)
}
}
}
if wsklimits.Timeout != nil || wsklimits.Memory != nil || wsklimits.Logsize != nil {
return wsklimits
}
return nil
}
func (dm *YAMLParser) warnIfRedundantWebActionFlags(action Action) {
// Warn user if BOTH web and web-export specified,
// as they are redundant; defer to "web" flag and its value
if len(action.Web) != 0 && len(action.WebExport) != 0 {
warningString := wski18n.T(wski18n.ID_WARN_ACTION_WEB_X_action_X,
map[string]interface{}{wski18n.KEY_ACTION: action.Name})
wskprint.PrintOpenWhiskWarning(warningString)
}
}
func (dm *YAMLParser) ComposeActions(manifestFilePath string, actions map[string]Action, packageName string, managedAnnotations whisk.KeyValue, packageInputs PackageInputs) ([]utils.ActionRecord, error) {
var errorParser error
var listOfActions []utils.ActionRecord = make([]utils.ActionRecord, 0)
splitManifestFilePath := strings.Split(manifestFilePath, string(PATH_SEPARATOR))
manifestFileName := splitManifestFilePath[len(splitManifestFilePath)-1]
for actionName, action := range actions {
var actionFilePath string
// update the action (of type Action) to set its name
// here key name is the action name
action.Name = actionName
// Create action data object from client library
wskaction := new(whisk.Action)
//set action.Function to action.Location
//because Location is deprecated in Action entity
if len(action.Function) == 0 && len(action.Location) != 0 {
action.Function = action.Location
}
actionFilePath, wskaction.Exec, errorParser = dm.composeActionExec(manifestFilePath, manifestFileName, action)
if errorParser != nil {
return nil, errorParser
}
// Action.Inputs
listOfInputs, err := dm.composeInputs(action.Inputs, packageInputs, manifestFilePath)
if err != nil {
return nil, err
}
if len(listOfInputs) > 0 {
wskaction.Parameters = listOfInputs
}
// Action.Outputs
// TODO{} add outputs as annotations (work to discuss officially supporting for compositions)
//listOfOutputs, err := dm.composeOutputs(action.Outputs, manifestFilePath)
//if err != nil {
// return nil, err
//}
//if len(listOfOutputs) > 0 {
// wskaction.Annotations = listOfOutputs
//}
// Action.Annotations
// ==================
// WARNING! Processing of explicit Annotations MUST occur before handling of Action keys, as these
// keys often need to check for inconsistencies (and raise errors).
if listOfAnnotations := dm.composeAnnotations(action.Annotations); len(listOfAnnotations) > 0 {
wskaction.Annotations = append(wskaction.Annotations, listOfAnnotations...)
}
// add managed annotations if its marked as managed deployment
if utils.Flags.Managed || utils.Flags.Sync {
wskaction.Annotations = append(wskaction.Annotations, managedAnnotations)
}
// Web Export (i.e., "web-export" annotation)
// ==========
// Treat ACTION as a web action, a raw HTTP web action, or as a standard action based on web-export;
// when web-export is set to yes | true, treat action as a web action,
// when web-export is set to raw, treat action as a raw HTTP web action,
// when web-export is set to no | false, treat action as a standard action
dm.warnIfRedundantWebActionFlags(action)
if len(action.GetWeb()) != 0 {
wskaction.Annotations, errorParser = webaction.SetWebActionAnnotations(
manifestFilePath,
action.Name,
action.GetWeb(),
wskaction.Annotations,
false)
if errorParser != nil {
return listOfActions, errorParser
}
}
// validate special action annotations such as "require-whisk-auth"
// TODO: the Manifest parser will validate any declared APIs that ref. this action
if wskaction.Annotations != nil {
if webaction.HasAnnotation(&wskaction.Annotations, webaction.REQUIRE_WHISK_AUTH) {
_, errorParser = webaction.ValidateRequireWhiskAuthAnnotationValue(
actionName,
wskaction.Annotations.GetValue(webaction.REQUIRE_WHISK_AUTH))
}
if errorParser != nil {
return listOfActions, errorParser
}
}
// Action.Limits
if action.Limits != nil {
if wsklimits := dm.composeActionLimits(*(action.Limits)); wsklimits != nil {
wskaction.Limits = wsklimits
}
}
// Conductor Action
if action.Conductor {
wskaction.Annotations = append(wskaction.Annotations, conductor.ConductorAction())
}
// Set other top-level values for the action (e.g., name, version, publish, etc.)
wskaction.Name = actionName
pub := false
wskaction.Publish = &pub
wskaction.Version = wskenv.ConvertSingleName(action.Version)
// create a "record" of the Action relative to its package and function filepath
// which will be used to compose the REST API calls
record := utils.ActionRecord{Action: wskaction, Packagename: packageName, Filepath: actionFilePath}
listOfActions = append(listOfActions, record)
}
return listOfActions, nil
}
func (dm *YAMLParser) ComposeTriggersFromAllPackages(manifest *YAML, filePath string, managedAnnotations whisk.KeyValue, inputs map[string]PackageInputs) ([]*whisk.Trigger, error) {
var triggers []*whisk.Trigger = make([]*whisk.Trigger, 0)
manifestPackages := make(map[string]Package)
if len(manifest.Packages) != 0 {
manifestPackages = manifest.Packages
} else {
manifestPackages = manifest.GetProject().Packages
}
for packageName, pkg := range manifestPackages {
t, err := dm.ComposeTriggers(filePath, pkg, managedAnnotations, inputs[packageName])
if err == nil {
triggers = append(triggers, t...)
} else {
return nil, err
}