You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/04/22 18:09:32 UTC

[GitHub] sciabarracom closed pull request #45: Image actionloop-swift based on the actionloop (goproxy)

sciabarracom closed pull request #45: Image actionloop-swift based on the actionloop (goproxy)
URL: https://github.com/apache/incubator-openwhisk-runtime-swift/pull/45
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/core/swift41ActionLoop/Dockerfile b/core/swift41ActionLoop/Dockerfile
new file mode 100644
index 0000000..f5da3eb
--- /dev/null
+++ b/core/swift41ActionLoop/Dockerfile
@@ -0,0 +1,71 @@
+FROM actionloop
+LABEL Description="Swift Action Compiler based on Go ActionLoop"
+EXPOSE 8080
+USER root
+
+# Set environment variables for image
+ENV SWIFT_SNAPSHOT swift-4.1-DEVELOPMENT-SNAPSHOT-2018-03-11-a
+ENV SWIFT_SNAPSHOT_LOWERCASE swift-4.1-branch
+ENV UBUNTU_VERSION ubuntu14.04
+ENV UBUNTU_VERSION_NO_DOTS ubuntu1404
+ENV WORK_DIR /
+
+# Set WORKDIR
+WORKDIR ${WORK_DIR}
+
+# Linux OS utils and libraries and set clang 3.8 as default
+RUN apt-get update && apt-get dist-upgrade -y && apt-get install -y \
+  build-essential \
+  clang-3.8 \
+  git \
+  libpython2.7 \
+  libicu-dev \
+  wget \
+  libcurl4-openssl-dev \
+  vim \
+  curl \
+  python2.7 \
+  jq \
+  zip \
+  && apt-get clean \
+  && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
+  && update-alternatives --quiet --install /usr/bin/clang++ clang++ /usr/bin/clang++-3.8 100 \
+  && update-alternatives --quiet --install /usr/bin/clang clang /usr/bin/clang-3.8 100 \
+  && echo "set -o vi" >> /root/.bashrc
+
+# Install Swift compiler
+RUN wget https://swift.org/builds/$SWIFT_SNAPSHOT_LOWERCASE/$UBUNTU_VERSION_NO_DOTS/$SWIFT_SNAPSHOT/$SWIFT_SNAPSHOT-$UBUNTU_VERSION.tar.gz \
+    https://swift.org/builds/$SWIFT_SNAPSHOT_LOWERCASE/$UBUNTU_VERSION_NO_DOTS/$SWIFT_SNAPSHOT/$SWIFT_SNAPSHOT-$UBUNTU_VERSION.tar.gz.sig \
+  && gpg --keyserver hkp://pool.sks-keyservers.net \
+      --recv-keys \
+      '7463 A81A 4B2E EA1B 551F  FBCF D441 C977 412B 37AD' \
+      '1BE1 E29A 084C B305 F397  D62A 9F59 7F4D 21A5 6D5F' \
+      'A3BA FD35 56A5 9079 C068  94BD 63BC 1CFE 91D3 06C6' \
+      '5E4D F843 FB06 5D7F 7E24  FBA2 EF54 30F0 71E1 B235' \
+      '8513 444E 2DA3 6B7C 1659  AF4D 7638 F1FB 2B2B 08C4' \
+  && gpg --keyserver hkp://pool.sks-keyservers.net --refresh-keys  \
+  && gpg --verify $SWIFT_SNAPSHOT-$UBUNTU_VERSION.tar.gz.sig \
+  && tar xzvf $SWIFT_SNAPSHOT-$UBUNTU_VERSION.tar.gz --strip-components=1 \
+  && rm $SWIFT_SNAPSHOT-$UBUNTU_VERSION.tar.gz \
+  && rm $SWIFT_SNAPSHOT-$UBUNTU_VERSION.tar.gz.sig \
+  && chmod -R go+r /usr/lib/swift \
+  && swift --version
+
+# Add files needed to build and run action
+RUN mkdir -p /swift4Action/spm-build/Sources/Action
+ADD epilogue.swift /swift4Action
+ADD buildandrecord.py /swift4Action
+ADD spm-build/Package.swift /swift4Action/spm-build
+ADD spm-build/_Whisk.swift /swift4Action/spm-build/Sources/Action
+
+# swift compiler 
+ADD swiftbuild.py /bin/compile
+ENV COMPILER /bin/compile
+
+# Build
+RUN touch /swift4Action/spm-build/Sources/Action/main.swift \
+    && chmod 0755 /bin/compile \
+    && python /swift4Action/buildandrecord.py \
+    && rm /swift4Action/spm-build/.build/release/Action
+
+CMD /proxy
\ No newline at end of file
diff --git a/core/swift41ActionLoop/build.gradle b/core/swift41ActionLoop/build.gradle
new file mode 100755
index 0000000..c3f4603
--- /dev/null
+++ b/core/swift41ActionLoop/build.gradle
@@ -0,0 +1,21 @@
+ext.dockerImageName = 'actionloop-swift-v4.1'
+apply from: '../../gradle/docker.gradle'
+
+distDocker.dependsOn 'copyWhisk'
+distDocker.dependsOn 'copyPackageSwift'
+distDocker.finalizedBy('cleanup')
+
+task copyWhisk(type: Copy) {
+    from '../swift41Action/spm-build/_Whisk.swift'
+    into './spm-build'
+}
+
+task copyPackageSwift(type: Copy) {
+    from '../swift41Action/spm-build/Package.swift'
+    into './spm-build'
+}
+
+task cleanup(type: Delete) {
+    delete 'spm-build/_Whisk.swift'
+    delete 'spm-build/Package.swift'
+}
diff --git a/core/swift41ActionLoop/buildandrecord.py b/core/swift41ActionLoop/buildandrecord.py
new file mode 100755
index 0000000..9a47163
--- /dev/null
+++ b/core/swift41ActionLoop/buildandrecord.py
@@ -0,0 +1,77 @@
+"""Python to generate build script.
+
+/*
+ * 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.
+ */
+"""
+from __future__ import print_function
+import os
+import sys
+from subprocess import check_output
+
+# Settings
+COMPILE_PREFIX = "/usr/bin/swiftc -module-name Action "
+LINKER_PREFIX =  "/usr/bin/swiftc -target x86_64-unknown-linux -sdk / -L /swift4Action/spm-build/.build/x86_64-unknown-linux/release -o /swift4Action/spm-build/.build/x86_64-unknown-linux/release/Action -module-name Action -emit-executable -Xlinker '-rpath=$ORIGIN'"
+GENERATED_BUILD_SCRIPT = "/swift4Action/spm-build/swiftbuildandlink.sh"
+SPM_DIRECTORY = "/swift4Action/spm-build"
+BUILD_COMMAND = ["swift", "build", "-v", "-c", "release"]
+
+# Build Swift package and capture step trace
+print("Building action")
+out = check_output(BUILD_COMMAND, cwd=SPM_DIRECTORY)
+print("action built. Decoding compile and link commands")
+
+# Look for compile and link commands in step trace
+compileCommand = None
+linkCommand = None
+
+buildInstructions = out.decode("utf-8").splitlines()
+
+for instruction in buildInstructions:
+    if instruction.startswith(COMPILE_PREFIX):
+        compileCommand = instruction
+
+        # add flag to quiet warnings
+        compileCommand += " -suppress-warnings"
+
+    elif instruction.startswith(LINKER_PREFIX):
+        linkCommand = instruction
+
+# if found, create build script, otherwise exit with error
+if compileCommand is not None and linkCommand is not None:
+    print("Generated OpenWhisk Compile command: %s" % compileCommand)
+    print("=========")
+    print("Generated OpenWhisk Link command: %s" % linkCommand)
+
+    with open(GENERATED_BUILD_SCRIPT, "a") as buildScript:
+        buildScript.write("#!/bin/bash\n")
+        buildScript.write("echo \"Compiling\"\n")
+        buildScript.write("%s\n" % compileCommand)
+        buildScript.write("swiftStatus=$?\n")
+        buildScript.write("echo swiftc status is $swiftStatus\n")
+        buildScript.write("if [[ \"$swiftStatus\" -eq \"0\" ]]; then\n")
+        buildScript.write("echo \"Linking\"\n")
+        buildScript.write("%s\n" % linkCommand)
+        buildScript.write("else\n")
+        buildScript.write(">2& echo \"Action did not compile\"\n")
+        buildScript.write("exit 1\n")
+        buildScript.write("fi")
+
+    os.chmod(GENERATED_BUILD_SCRIPT, 0o777)
+    sys.exit(0)
+else:
+    print("Cannot generate build script: compile or link command not found")
+    sys.exit(1)
diff --git a/core/swift41ActionLoop/epilogue.swift b/core/swift41ActionLoop/epilogue.swift
new file mode 100644
index 0000000..c1b9bdb
--- /dev/null
+++ b/core/swift41ActionLoop/epilogue.swift
@@ -0,0 +1,62 @@
+/*
+ * 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.
+ */
+
+// Imports
+import Foundation
+// Linux only
+import Glibc
+
+func _whisk_print_error(message: String, error: Error?){
+    var errStr =  "{\"error\":\"\(message)\"}\n"
+    if let error = error {
+      errStr = "{\"error\":\"\(message) \(error.localizedDescription)\"\n}"
+    } 
+    let buf : [UInt8] = Array(errStr.utf8)
+    write(3, buf, buf.count)
+}
+
+// snippet of code "injected" (wrapper code for invoking traditional main)
+func _run_main(mainFunction: ([String: Any]) -> [String: Any]) -> Void {
+    while let inputStr: String = readLine() {
+        do {
+            let json = inputStr.data(using: .utf8, allowLossyConversion: true)!
+            let parsed = try JSONSerialization.jsonObject(with: json, options: []) as! [String: Any]
+            let result = mainFunction(parsed)
+            if JSONSerialization.isValidJSONObject(result) {
+                do {
+                    let jsonData = try JSONSerialization.data(withJSONObject: result, options: [])
+                    var json = [UInt8](jsonData)
+                    json.append(10)
+                    write(3, json, json.count)
+                    fflush(stdout)
+                    fflush(stderr)
+                } catch {
+                    _whisk_print_error(message: "Failed to encode Dictionary type to JSON string:", error: error)
+                }
+            } else {
+                _whisk_print_error(message: "Error serializing JSON, data does not appear to be valid JSON", error: nil)
+            }
+        } catch {
+            _whisk_print_error(message: "Failed to execute action handler with error:", error: error)
+            return
+        }
+    }
+}
+
+// snippets of code "injected", dependending on the type of function the developer
+// wants to use traditional vs codable
+
diff --git a/core/swift41ActionLoop/swiftbuild.py b/core/swift41ActionLoop/swiftbuild.py
new file mode 100755
index 0000000..53b1c2c
--- /dev/null
+++ b/core/swift41ActionLoop/swiftbuild.py
@@ -0,0 +1,135 @@
+#!/usr/bin/python
+"""Swift Action Compiler
+
+/*
+ * 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.
+ */
+"""
+import os
+import os.path
+import glob
+import sys
+import subprocess
+import codecs
+import json
+import shutil 
+
+SRC_EPILOGUE_FILE = '/swift4Action/epilogue.swift'
+DEST_SCRIPT_FILE = '/swift4Action/spm-build/Sources/Action/main.swift'
+DEST_SCRIPT_SRC = '/swift4Action/spm-build/Sources/Action'
+DEST_SCRIPT_DIR = '/swift4Action/spm-build'
+DEST_BIN_FILE = '/swift4Action/spm-build/.build/release/Action'
+
+BUILD_PROCESS = ['./swiftbuildandlink.sh']
+
+def epilogue(main_function):
+    # make sure there is a main.swift file
+    open(DEST_SCRIPT_FILE, 'a').close()
+
+    with codecs.open(DEST_SCRIPT_FILE, 'a', 'utf-8') as fp:
+        os.chdir(DEST_SCRIPT_DIR)
+        for file in glob.glob("*.swift"):
+            if file not in ["Package.swift", "main.swift", "_WhiskJSONUtils.swift", "_Whisk.swift"]:
+                print("concat "+file)
+                with codecs.open(file, 'r', 'utf-8') as f:
+                    fp.write(f.read())
+        with codecs.open(SRC_EPILOGUE_FILE, 'r', 'utf-8') as ep:
+            print("concat "+SRC_EPILOGUE_FILE)
+            fp.write(ep.read())
+
+        fp.write('_run_main(mainFunction: %s)\n' % main_function)
+
+def build():
+
+    p = subprocess.Popen(
+        BUILD_PROCESS,
+        stdout=subprocess.PIPE,
+        stderr=subprocess.PIPE,
+        cwd=DEST_SCRIPT_DIR)
+
+    # run the process and wait until it completes.
+    # stdout/stderr will not be None because we passed PIPEs to Popen
+    (o, e) = p.communicate()
+
+    # stdout/stderr may be either text or bytes, depending on Python
+    # version, so if bytes, decode to text. Note that in Python 2
+    # a string will match both types; so also skip decoding in that case
+    if isinstance(o, bytes) and not isinstance(o, str):
+        o = o.decode('utf-8')
+    if isinstance(e, bytes) and not isinstance(e, str):
+        e = e.decode('utf-8')
+
+    if o:
+        sys.stdout.write(o)
+        sys.stdout.flush()
+
+    if e:
+        sys.stderr.write(e)
+        sys.stderr.flush()
+
+
+def collect(source):
+    # copy file
+    if os.path.isfile(source):
+      print "copying "+source
+      shutil.copyfile(source, DEST_SCRIPT_FILE)
+      return os.path.dirname(source)
+
+    # collect sources in a single main
+    if os.path.isdir(source):
+        with codecs.open(DEST_SCRIPT_FILE, 'a', 'utf-8') as fp:
+            for file in glob.glob(source+"/*.swift"):
+                print "concat "+file
+                with codecs.open(file, 'r', 'utf-8') as f:
+                    fp.write(f.read())
+        return source
+    
+    print "cannot read "+source
+    sys.exit(1)
+
+def main(argv):
+
+    # collect args
+    main = "main"
+    source = "/src"
+    target = "/out"
+    if len(argv) > 1:
+        main = argv[1]
+    if len(argv) > 2:
+        source = argv[2]  
+    if len(argv) > 3:
+        target = argv[3]  
+
+    source = os.path.abspath(source)
+    target = os.path.abspath(target)
+
+    # build
+    collect(source)
+    os.chdir(DEST_SCRIPT_DIR)
+    epilogue(main)
+    build()
+    
+    # copy to target
+    if os.path.isdir(target):
+        dest = target+ "/" + main
+    else:
+        dest = target
+    
+    shutil.copyfile(DEST_BIN_FILE, dest)
+    os.chmod(dest, 0o755)
+
+if __name__ == '__main__':
+    main(sys.argv)
diff --git a/settings.gradle b/settings.gradle
index bce3ebb..f53d823 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -1,8 +1,8 @@
 include 'tests'
 
 include 'core:swift3.1.1Action'
-
 include 'core:swift41Action'
+include 'core:swift41ActionLoop'
 
 rootProject.name = 'runtime-swift'
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services