Skip to content
Permalink
Browse files

Adds support for initialization parameters. (#97)

  • Loading branch information...
sciabarracom authored and rabbah committed Aug 29, 2019
1 parent 429932e commit 555130ccbf70517ccbab23fdff611c79984ef0af
@@ -80,7 +80,6 @@ func main() {
log.Printf("%v\n", input)
}
// set environment variables
err = json.Unmarshal(inbuf, &input)
for k, v := range input {
if k == "value" {
continue
@@ -1,3 +1,20 @@
#
# 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.
#

# prefix
IMAGES?=openwhisk

@@ -1,3 +1,20 @@
#
# 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.
#

WSK?=wsk
OW_USER?=openwhisk
OW_RUNTIME?=$(OW_USER)/actionloop-base
@@ -1,3 +1,20 @@
#
# 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.
#

IMG?=whisk/actionloop-golang-v1.11
IMG2?=whisk/actionloop

@@ -1,3 +1,20 @@
#
# 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.
#

OW_USER?=openwhisk
OW_RUNTIME?=$(OW_USER)/actionloop-base
OW_COMPILER?=$(OW_USER)/actionloop-golang-v1.11
@@ -1,3 +1,20 @@
#
# 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.
#

OW_USER?=openwhisk
OW_RUNTIME?=$(OW_USER)/actionloop-base
OW_COMPILER?=$(OW_USER)/actionloop-golang-v1.11
@@ -1,3 +1,20 @@
#
# 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.
#

OW_USER?=openwhisk
OW_RUNTIME?=$(OW_USER)/actionloop-base
OW_COMPILER?=$(OW_USER)/actionloop-golang-v1.11
@@ -1,3 +1,20 @@
#
# 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.
#

OW_USER?=openwhisk
OW_RUNTIME?=$(OW_USER)/actionloop-base
OW_COMPILER?=$(OW_USER)/actionloop-golang-v1.11
@@ -32,4 +49,4 @@ package.done:
$(WSK) package update $(PACKAGE)
touch package.done

.PHONY: deploy devel test clean
.PHONY: deploy devel test clean
@@ -1,3 +1,20 @@
#
# 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.
#

OW_USER?=openwhisk
OW_RUNTIME?=$(OW_USER)/actionloop-base
OW_COMPILER?=$(OW_USER)/actionloop-golang-v1.11
@@ -32,4 +49,4 @@ package.done:
$(WSK) package update $(PACKAGE)
touch package.done

.PHONY: deploy test clean
.PHONY: deploy test clean
@@ -1,3 +1,20 @@
#
# 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.
#

OW_USER?=openwhisk
OW_RUNTIME?=$(OW_USER)/actionloop-base
OW_COMPILER?=$(OW_USER)/actionloop-golang-v1.11
@@ -0,0 +1,23 @@
#!/bin/bash
#
# 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.
#

while read line
do echo '{ "env": "'$(env | grep TEST_ | sort)'"}' >&3
done

This comment has been minimized.

Copy link
@rabbah

rabbah Aug 29, 2019

Member

@sciabarracom can you check if scancode is configured correctly for this repo? it should have been flagged these extra spaces.


@@ -18,6 +18,7 @@
package openwhisk

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
@@ -48,6 +49,9 @@ type ActionProxy struct {
// out and err files
outFile *os.File
errFile *os.File

// environment
env map[string]string
}

// NewActionProxy creates a new action proxy that can handle http requests
@@ -61,6 +65,22 @@ func NewActionProxy(baseDir string, compiler string, outFile *os.File, errFile *
nil,
outFile,
errFile,
map[string]string{},
}
}

//SetEnv sets the environment
func (ap *ActionProxy) SetEnv(env map[string]interface{}) {
for k, v := range env {
s, ok := v.(string)
if ok {
ap.env[k] = s
continue
}
buf, err := json.Marshal(v)
if err == nil {
ap.env[k] = string(buf)
}
}
}

@@ -84,7 +104,7 @@ func (ap *ActionProxy) StartLatestAction() error {
// try to launch the action
executable := fmt.Sprintf("%s/%d/bin/exec", ap.baseDir, highestDir)
os.Chmod(executable, 0755)
newExecutor := NewExecutor(ap.outFile, ap.errFile, executable)
newExecutor := NewExecutor(ap.outFile, ap.errFile, executable, ap.env)
Debug("starting %s", executable)
err := newExecutor.Start()
if err == nil {
@@ -19,6 +19,7 @@ package openwhisk

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
@@ -123,5 +124,23 @@ func Example_compile_src() {
// ./action/c2/out/action/action/main.py
// ./action/c2/out/action/exec.py
// ./action/c2/out/exec
}

func Example_SetEnv() {
ap := NewActionProxy("", "", nil, nil)
fmt.Println(ap.env)
var m map[string]interface{}
json.Unmarshal([]byte(`{
"s": "string",
"n": 123,
"a": [1,2,3],
"o": {"a":1,"b":2}
}`), &m)
log.Println(m)
ap.SetEnv(m)
fmt.Println(ap.env["a"], ap.env["o"], ap.env["s"], ap.env["n"])
// Output:
// map[]
// [1,2,3] {"a":1,"b":2} string 123

}
@@ -44,12 +44,13 @@ type Executor struct {
// NewExecutor creates a child subprocess using the provided command line,
// writing the logs in the given file.
// You can then start it getting a communication channel
func NewExecutor(logout *os.File, logerr *os.File, command string, args ...string) (proc *Executor) {
func NewExecutor(logout *os.File, logerr *os.File, command string, env map[string]string, args ...string) (proc *Executor) {
cmd := exec.Command(command, args...)
cmd.Stdout = logout
cmd.Stderr = logerr
cmd.Env = []string{
"__OW_API_HOST=" + os.Getenv("__OW_API_HOST"),
cmd.Env = []string{}
for k, v := range env {
cmd.Env = append(cmd.Env, k+"="+v)
}
Debug("env: %v", cmd.Env)
if Debugging {
@@ -21,21 +21,23 @@ import (
"io/ioutil"
)

var m = map[string]string{}

func ExampleNewExecutor_failed() {
log, _ := ioutil.TempFile("", "log")
proc := NewExecutor(log, log, "true")
proc := NewExecutor(log, log, "true", m)
err := proc.Start()
fmt.Println(err)
proc.Stop()
proc = NewExecutor(log, log, "/bin/pwd")
proc = NewExecutor(log, log, "/bin/pwd", m)
err = proc.Start()
fmt.Println(err)
proc.Stop()
proc = NewExecutor(log, log, "donotexist")
proc = NewExecutor(log, log, "donotexist", m)
err = proc.Start()
fmt.Println(err)
proc.Stop()
proc = NewExecutor(log, log, "/etc/passwd")
proc = NewExecutor(log, log, "/etc/passwd", m)
err = proc.Start()
fmt.Println(err)
proc.Stop()
@@ -48,7 +50,7 @@ func ExampleNewExecutor_failed() {

func ExampleNewExecutor_bc() {
log, _ := ioutil.TempFile("", "log")
proc := NewExecutor(log, log, "_test/bc.sh")
proc := NewExecutor(log, log, "_test/bc.sh", m)
err := proc.Start()
fmt.Println(err)
res, _ := proc.Interact([]byte("2+2"))
@@ -64,7 +66,7 @@ func ExampleNewExecutor_bc() {

func ExampleNewExecutor_hello() {
log, _ := ioutil.TempFile("", "log")
proc := NewExecutor(log, log, "_test/hello.sh")
proc := NewExecutor(log, log, "_test/hello.sh", m)
err := proc.Start()
fmt.Println(err)
res, _ := proc.Interact([]byte(`{"value":{"name":"Mike"}}`))
@@ -78,3 +80,19 @@ func ExampleNewExecutor_hello() {
// XXX_THE_END_OF_A_WHISK_ACTIVATION_XXX
// XXX_THE_END_OF_A_WHISK_ACTIVATION_XXX
}

func ExampleNewExecutor_env() {
log, _ := ioutil.TempFile("", "log")
proc := NewExecutor(log, log, "_test/env.sh", map[string]string{"TEST_HELLO": "WORLD", "TEST_HI": "ALL"})
err := proc.Start()
fmt.Println(err)
res, _ := proc.Interact([]byte(`{"value":{"name":"Mike"}}`))
fmt.Printf("%s", res)
proc.Stop()
dump(log)
// Output:
// <nil>
// { "env": "TEST_HELLO=WORLD TEST_HI=ALL"}
// XXX_THE_END_OF_A_WHISK_ACTIVATION_XXX
// XXX_THE_END_OF_A_WHISK_ACTIVATION_XXX
}

0 comments on commit 555130c

Please sign in to comment.
You can’t perform that action at this time.