Skip to content

Commit

Permalink
Remove 'kinds' from CLI; defer to backend. (apache#2053)
Browse files Browse the repository at this point in the history
* Remove 'kinds' from CLI; defer to backend.

* Fix messages.

* Add cli test for unknown kind.

* Move Kind Process to its Own Method to Declutter ParseAction (apache#9)

* pass flags to getExec.
  • Loading branch information
rabbah authored and csantanapr committed Mar 24, 2017
1 parent 112c7e5 commit bd5114a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,14 @@ class WskBasicUsageTests
}
}

it should "report error when creating an action with unknown kind" in withAssetCleaner(wskprops) {
(wp, assetHelper) =>
val rr = assetHelper.withCleaner(wsk.action, "invalid kind", confirmDelete = false) {
(action, name) => action.create(name, Some(TestUtils.getTestActionFilename("echo.js")), kind = Some("foobar"), expectedExitCode = BAD_REQUEST)
}
rr.stderr should include regex "kind 'foobar' not in Set"
}

it should "create, and invoke an action that utilizes an invalid docker container with appropriate error" in withAssetCleaner(wskprops) {
val name = "invalid dockerContainer"
val containerName = s"bogus${Random.alphanumeric.take(16).mkString.toLowerCase}"
Expand Down
144 changes: 65 additions & 79 deletions tools/cli/go-whisk-cli/commands/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ var actionListCmd = &cobra.Command{

func parseAction(cmd *cobra.Command, args []string, update bool) (*whisk.Action, error) {
var err error
var artifact, code string
var artifact string
var existingAction *whisk.Action
var paramArgs []string
var annotArgs []string
Expand All @@ -331,12 +331,11 @@ func parseAction(cmd *cobra.Command, args []string, update bool) (*whisk.Action,
return nil, parseQualifiedNameError(args[0], err)
}

client.Namespace = qualifiedName.namespace

if len(args) == 2 {
artifact = args[1]
}

client.Namespace = qualifiedName.namespace
action := new(whisk.Action)
action.Name = qualifiedName.entityName
action.Namespace = qualifiedName.namespace
Expand Down Expand Up @@ -390,93 +389,80 @@ func parseAction(cmd *cobra.Command, args []string, update bool) (*whisk.Action,
action.Exec = new(whisk.Exec)
action.Exec.Kind = "sequence"
action.Exec.Components = csvToQualifiedActions(artifact)
} else if artifact != "" {
ext := filepath.Ext(artifact)
action.Exec = new(whisk.Exec)
} else if len(artifact) > 0 {
action.Exec, err = getExec(args[1], flags.action.kind, flags.action.docker, flags.action.main)
}

if !flags.action.docker || ext == ".zip" {
code, err = readFile(artifact)
action.Exec.Code = &code
if cmd.LocalFlags().Changed(WEB_FLAG) {
action.Annotations, err = webAction(flags.action.web, action.Annotations, qualifiedName.entityName, update)
}

if err != nil {
whisk.Debug(whisk.DbgError, "readFile(%s) error: %s\n", artifact, err)
return nil, err
}
}
whisk.Debug(whisk.DbgInfo, "Parsed action struct: %#v\n", action)

if flags.action.kind == "swift:3" || flags.action.kind == "swift:3.0" || flags.action.kind == "swift:3.0.0" {
action.Exec.Kind = "swift:3"
} else if flags.action.kind == "nodejs:6" || flags.action.kind == "nodejs:6.0" ||
flags.action.kind == "nodejs:6.0.0" {
action.Exec.Kind = "nodejs:6"
} else if flags.action.kind == "nodejs:default" {
action.Exec.Kind = "nodejs:default"
} else if flags.action.kind == "swift:default" {
action.Exec.Kind = "swift:default"
} else if flags.action.kind == "nodejs" {
action.Exec.Kind = "nodejs"
} else if flags.action.kind == "python" {
action.Exec.Kind = "python"
} else if flags.action.docker {
action.Exec.Kind = "blackbox"
if ext != ".zip" {
action.Exec.Image = artifact
} else {
action.Exec.Image = "openwhisk/dockerskeleton"
}
} else if len(flags.action.kind) > 0 {
whisk.Debug(whisk.DbgError, "--kind argument '%s' is not supported\n", flags.action.kind)
errMsg := wski18n.T("'{{.name}}' is not a supported action runtime",
map[string]interface{}{"name": flags.action.kind})
whiskErr := whisk.MakeWskError(errors.New(errMsg), whisk.EXITCODE_ERR_GENERAL, whisk.DISPLAY_MSG,
whisk.DISPLAY_USAGE)
return nil, whiskErr
} else if ext == ".swift" {
action.Exec.Kind = "swift:default"
} else if ext == ".js" {
action.Exec.Kind = "nodejs:6"
} else if ext == ".py" {
action.Exec.Kind = "python"
} else if ext == ".jar" {
action.Exec.Kind = "java"
action.Exec.Jar = base64.StdEncoding.EncodeToString([]byte(code))
action.Exec.Code = nil
} else {
if ext == ".zip" {
// This point is reached if the extension was .zip and the kind was not specifically set to nodejs:*.
return nil, zipKindError()
} else {
return nil, extensionError(ext)
}
}
return action, err
}

// Determining the entrypoint.
if len(flags.action.main) != 0 {
// The --main flag was specified.
action.Exec.Main = flags.action.main
} else {
// The flag was not specified. For now, the only kind where it makes a difference is "java", for which the
// flag is expected.
if action.Exec.Kind == "java" {
return nil, javaEntryError()
}
func getExec(artifact string, kind string, isDocker bool, mainEntry string) (*whisk.Exec, error) {
var err error
var code string
var exec *whisk.Exec

ext := filepath.Ext(artifact)
exec = new(whisk.Exec)

if !isDocker || ext == ".zip" {
code, err = readFile(artifact)
exec.Code = &code

if err != nil {
whisk.Debug(whisk.DbgError, "readFile(%s) error: %s\n", artifact, err)
return nil, err
}
}

// For zip-encoded actions, the code needs to be base64-encoded. We reach this point if the kind has already be
// determined. Since the extension is not js, this means the kind was specified explicitly.
if len(kind) > 0 {
exec.Kind = kind
} else if isDocker {
exec.Kind = "blackbox"
if ext != ".zip" {
exec.Image = artifact
} else {
exec.Image = "openwhisk/dockerskeleton"
}
} else if ext == ".swift" {
exec.Kind = "swift:default"
} else if ext == ".js" {
exec.Kind = "nodejs:default"
} else if ext == ".py" {
exec.Kind = "python:default"
} else if ext == ".jar" {
exec.Kind = "java:default"
exec.Jar = base64.StdEncoding.EncodeToString([]byte(code))
exec.Code = nil
} else {
if ext == ".zip" {
code = base64.StdEncoding.EncodeToString([]byte(code))
action.Exec.Code = &code
return nil, zipKindError()
} else {
return nil, extensionError(ext)
}
}

if cmd.LocalFlags().Changed(WEB_FLAG) {
action.Annotations, err = webAction(flags.action.web, action.Annotations, qualifiedName.entityName, update)
// Error if entry point is not specified for Java
if len(mainEntry) != 0 {
exec.Main = mainEntry
} else {
if exec.Kind == "java" {
return nil, javaEntryError()
}
}

whisk.Debug(whisk.DbgInfo, "Parsed action struct: %#v\n", action)
// Base64 encode the zip file content
if ext == ".zip" {
code = base64.StdEncoding.EncodeToString([]byte(code))
exec.Code = &code
}

return action, err
return exec, nil
}

func webAction(webMode string, annotations whisk.KeyValueArr, entityName string, fetch bool) (whisk.KeyValueArr, error){
Expand Down Expand Up @@ -854,7 +840,7 @@ func init() {
actionCreateCmd.Flags().BoolVar(&flags.action.docker, "docker", false, wski18n.T("treat ACTION as docker image path on dockerhub"))
actionCreateCmd.Flags().BoolVar(&flags.action.copy, "copy", false, wski18n.T("treat ACTION as the name of an existing action"))
actionCreateCmd.Flags().BoolVar(&flags.action.sequence, "sequence", false, wski18n.T("treat ACTION as comma separated sequence of actions to invoke"))
actionCreateCmd.Flags().StringVar(&flags.action.kind, "kind", "", wski18n.T("the `KIND` of the action runtime (example: swift:3, nodejs:6)"))
actionCreateCmd.Flags().StringVar(&flags.action.kind, "kind", "", wski18n.T("the `KIND` of the action runtime (example: swift:default, nodejs:default)"))
actionCreateCmd.Flags().StringVar(&flags.action.main, "main", "", wski18n.T("the name of the action entry point (function or fully-qualified method name when applicable)"))
actionCreateCmd.Flags().IntVarP(&flags.action.timeout, "timeout", "t", TIMEOUT_LIMIT, wski18n.T("the timeout `LIMIT` in milliseconds after which the action is terminated"))
actionCreateCmd.Flags().IntVarP(&flags.action.memory, "memory", "m", MEMORY_LIMIT, wski18n.T("the maximum memory `LIMIT` in MB for the action"))
Expand All @@ -868,7 +854,7 @@ func init() {
actionUpdateCmd.Flags().BoolVar(&flags.action.docker, "docker", false, wski18n.T("treat ACTION as docker image path on dockerhub"))
actionUpdateCmd.Flags().BoolVar(&flags.action.copy, "copy", false, wski18n.T("treat ACTION as the name of an existing action"))
actionUpdateCmd.Flags().BoolVar(&flags.action.sequence, "sequence", false, wski18n.T("treat ACTION as comma separated sequence of actions to invoke"))
actionUpdateCmd.Flags().StringVar(&flags.action.kind, "kind", "", wski18n.T("the `KIND` of the action runtime (example: swift:3, nodejs:6)"))
actionUpdateCmd.Flags().StringVar(&flags.action.kind, "kind", "", wski18n.T("the `KIND` of the action runtime (example: swift:default, nodejs:default)"))
actionUpdateCmd.Flags().StringVar(&flags.action.main, "main", "", wski18n.T("the name of the action entry point (function or fully-qualified method name when applicable)"))
actionUpdateCmd.Flags().IntVarP(&flags.action.timeout, "timeout", "t", TIMEOUT_LIMIT, wski18n.T("the timeout `LIMIT` in milliseconds after which the action is terminated"))
actionUpdateCmd.Flags().IntVarP(&flags.action.memory, "memory", "m", MEMORY_LIMIT, wski18n.T("the maximum memory `LIMIT` in MB for the action"))
Expand Down
4 changes: 2 additions & 2 deletions tools/cli/go-whisk-cli/wski18n/resources/en_US.all.json
Original file line number Diff line number Diff line change
Expand Up @@ -880,8 +880,8 @@
"translation": "treat ACTION as comma separated sequence of actions to invoke"
},
{
"id": "the `KIND` of the action runtime (example: swift:3, nodejs:6)",
"translation": "the `KIND` of the action runtime (example: swift:3, nodejs:6)"
"id": "the `KIND` of the action runtime (example: swift:default, nodejs:default)",
"translation": "the `KIND` of the action runtime (example: swift:default, nodejs:default)"
},
{
"id": "the name of the action entry point (function or fully-qualified method name when applicable)",
Expand Down

0 comments on commit bd5114a

Please sign in to comment.