This is an automated email from the ASF dual-hosted git repository.

msciabarra pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/incubator-openwhisk-runtime-rust.git

commit c3a89e1e40ea4f1e4b39f709ef74c7e54550c36f
Author: Michele Sciabarra <msciaba...@apache.org>
AuthorDate: Tue Mar 5 10:13:49 2019 +0100

    reworked the source generation
---
 example/hello/.gitignore                           | 45 ++++++++++++++++++
 example/hello/Cargo.toml                           |  8 ++++
 example/hello/src/lib.rs                           |  9 ++++
 rust1.32/Dockerfile                                |  3 +-
 rust1.32/compile                                   | 55 +++++++---------------
 rust1.32/src/Cargo.lock                            | 52 ++++++++++++++++++++
 rust1.32/src/Cargo.toml                            |  6 +++
 rust1.32/src/action_loop/Cargo.toml                |  9 ++++
 .../action_loop/src/main.rs}                       | 23 ++++++---
 rust1.32/src/actions/.gitignore                    |  3 ++
 rust1.32/src/actions/Cargo.toml                    |  8 ++++
 rust1.32/src/actions/src/lib.rs                    | 10 ++++
 12 files changed, 186 insertions(+), 45 deletions(-)

diff --git a/example/hello/.gitignore b/example/hello/.gitignore
new file mode 100644
index 0000000..72fc6b4
--- /dev/null
+++ b/example/hello/.gitignore
@@ -0,0 +1,45 @@
+
+# Created by https://www.gitignore.io/api/osx,rust
+# Edit at https://www.gitignore.io/?templates=osx,rust
+
+### OSX ###
+# General
+.DS_Store
+.AppleDouble
+.LSOverride
+
+# Icon must end with two \r
+Icon
+
+# Thumbnails
+._*
+
+# Files that might appear in the root of a volume
+.DocumentRevisions-V100
+.fseventsd
+.Spotlight-V100
+.TemporaryItems
+.Trashes
+.VolumeIcon.icns
+.com.apple.timemachine.donotpresent
+
+# Directories potentially created on remote AFP share
+.AppleDB
+.AppleDesktop
+Network Trash Folder
+Temporary Items
+.apdisk
+
+### Rust ###
+# Generated by Cargo
+# will have compiled files and executables
+/target/
+
+# Remove Cargo.lock from gitignore if creating an executable, leave it for 
libraries
+# More information here 
https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
+Cargo.lock
+
+# These are backup files generated by rustfmt
+**/*.rs.bk
+
+# End of https://www.gitignore.io/api/osx,rust
\ No newline at end of file
diff --git a/example/hello/Cargo.toml b/example/hello/Cargo.toml
new file mode 100644
index 0000000..315401c
--- /dev/null
+++ b/example/hello/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "actions"
+version = "0.1.0"
+authors = ["Roberto Diaz <robe...@theagilemonkeys.com>"]
+
+[dependencies]
+serde_json = "1.0"
+
diff --git a/example/hello/src/lib.rs b/example/hello/src/lib.rs
new file mode 100644
index 0000000..b507a82
--- /dev/null
+++ b/example/hello/src/lib.rs
@@ -0,0 +1,9 @@
+extern crate serde_json;
+
+use std::collections::HashMap;
+use serde_json::Value;
+
+pub fn main(mut input_data: HashMap<String, Value>) -> HashMap<String, Value> {
+    input_data.insert("hello".to_string(),Value::String("world".to_string()));
+    input_data
+}
diff --git a/rust1.32/Dockerfile b/rust1.32/Dockerfile
index 07a6bb9..411b44c 100644
--- a/rust1.32/Dockerfile
+++ b/rust1.32/Dockerfile
@@ -19,7 +19,8 @@ FROM rust:1.32
 COPY --from=builder /bin/proxy /bin/proxy
 RUN mkdir -p /action 
 ADD compile /bin/compile
-ADD compile.launcher.rs /bin/compile.launcher.rs
+ADD src /usr/src
+RUN cd /usr/src ; cargo build 
 ENV OW_COMPILER=/bin/compile
 WORKDIR /action
 ENTRYPOINT ["/bin/proxy"]
diff --git a/rust1.32/compile b/rust1.32/compile
index 0c3228d..343e76a 100755
--- a/rust1.32/compile
+++ b/rust1.32/compile
@@ -21,7 +21,8 @@
 from __future__ import print_function
 import os, sys, codecs, subprocess
 from os.path import abspath, exists, dirname
-import tempfile
+import time
+import shutil
 
 ## utils 
 # write a file creating intermediate directories
@@ -47,59 +48,39 @@ version = "0.1.0"
 serde_json = "1.0"
 """
 
-cargo_actionloop = """[package]
-name = "action_loop"
-version = "0.1.0"
-authors = ["Roberto Diaz <robe...@theagilemonkeys.com>"]
-
-[dependencies]
-serde_json = "1.0"
-libc = "0.2.49"
-actions = { path = "../actions" }
-"""
-
-cargo_workspace = """
-[workspace]
-
-members = [
-"action_loop",
-"actions",
-]
-"""
-
-def build():
+def build(tgt_dir):
     pass
 
-def sources(main, src_dir, tgt_dir, launcher):
-    src_file = abspath("%s/exec" % src_dir)
-       
+def sources(main, src_dir):
+    # move away the action dir and replace with the new
+    tmpname = str(int(time.time()))
+    shutil.move("/usr/src/actions", "/usr/src/src%s" % tmpname)
+    shutil.move(src_dir, "/usr/src/actions")
+
     # move exec in the right place
+    src_file = "/usr/src/actions/exec"
     if exists(src_file):
-        os.makedirs(src_dir+"/src", mode=0o755, exist_ok=True)
-        copy_replace(src_file, src_dir+"/src/exec__.rs")
+        os.makedirs("/usr/src/actions/src", mode=0o755, exist_ok=True)
+        copy_replace(src_file, "/usr/src/actions/src/lib.rs")
     
     # add a cargo.toml if needed
-    cargo_action_file = src_dir+"/Cargo.toml"
+    cargo_action_file = "/usr/src/actions/Cargo.toml"
     if not exists(cargo_action_file):
         write_file(cargo_action_file, cargo_action)
 
     # write the boilerplate in a temp dir
-    os.makedirs("/tmp/src", mode=0o755, exist_ok=True)
-    tmp_dir = tempfile.mkdtemp(prefix='/tmp/src/')
-    copy_replace(launcher, tmp_dir+"/action_loop/src/main.rs",
+    launcher = "/usr/src/action_loop/tmp%s" % tmpname
+    shutil.move("/usr/src/action_loop/src/main.rs", launcher)
+    copy_replace(launcher, "/usr/src/action_loop/src/main.rs",
           "use actions::main as actionMain;",
           "use actions::%s as actionMain;" % main )
-    write_file(tmp_dir+"/action_loop/Cargo.toml", cargo_actionloop)
-    write_file(tmp_dir+"/Cargo.toml", cargo_workspace)
-    os.rename(src_dir, tmp_dir+"/actions")
-    return tmp_dir
 
 if __name__ == '__main__':
     if len(sys.argv) < 4:
         sys.stdout.write("usage: <main-function> <source-dir> <target-dir>\n")
         sys.stdout.flush()
         sys.exit(1)
-    dir = sources(sys.argv[1], abspath(sys.argv[2]), abspath(sys.argv[3]), 
abspath(sys.argv[0]+".launcher.rs"))
-    print(dir)
+    sources(sys.argv[1], abspath(sys.argv[2]))
+    build(abspath(sys.argv[3]))
     sys.stdout.flush()
     sys.stderr.flush()
diff --git a/rust1.32/src/Cargo.lock b/rust1.32/src/Cargo.lock
new file mode 100644
index 0000000..2015f28
--- /dev/null
+++ b/rust1.32/src/Cargo.lock
@@ -0,0 +1,52 @@
+[[package]]
+name = "action_loop"
+version = "0.1.0"
+dependencies = [
+ "actions 0.1.0",
+ "libc 0.2.49 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "actions"
+version = "0.1.0"
+dependencies = [
+ "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "itoa"
+version = "0.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+
+[[package]]
+name = "libc"
+version = "0.2.49"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+
+[[package]]
+name = "ryu"
+version = "0.2.7"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+
+[[package]]
+name = "serde"
+version = "1.0.89"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+
+[[package]]
+name = "serde_json"
+version = "1.0.39"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[metadata]
+"checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" 
= "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b"
+"checksum libc 0.2.49 (registry+https://github.com/rust-lang/crates.io-index)" 
= "413f3dfc802c5dc91dc570b05125b6cda9855edfaa9825c9849807876376e70e"
+"checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = 
"eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7"
+"checksum serde 1.0.89 
(registry+https://github.com/rust-lang/crates.io-index)" = 
"92514fb95f900c9b5126e32d020f5c6d40564c27a5ea6d1d7d9f157a96623560"
+"checksum serde_json 1.0.39 
(registry+https://github.com/rust-lang/crates.io-index)" = 
"5a23aa71d4a4d43fdbfaac00eff68ba8a06a51759a89ac3304323e800c4dd40d"
diff --git a/rust1.32/src/Cargo.toml b/rust1.32/src/Cargo.toml
new file mode 100644
index 0000000..b8121b6
--- /dev/null
+++ b/rust1.32/src/Cargo.toml
@@ -0,0 +1,6 @@
+[workspace]
+
+members = [
+"action_loop",
+"actions",
+]
diff --git a/rust1.32/src/action_loop/Cargo.toml 
b/rust1.32/src/action_loop/Cargo.toml
new file mode 100644
index 0000000..253fd02
--- /dev/null
+++ b/rust1.32/src/action_loop/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "action_loop"
+version = "0.1.0"
+authors = ["Roberto Diaz <robe...@theagilemonkeys.com>"]
+
+[dependencies]
+serde_json = "1.0"
+libc = "0.2.49"
+actions = { path = "../actions" }
\ No newline at end of file
diff --git a/rust1.32/compile.launcher.rs b/rust1.32/src/action_loop/src/main.rs
similarity index 61%
rename from rust1.32/compile.launcher.rs
rename to rust1.32/src/action_loop/src/main.rs
index 9f50140..e4e143d 100644
--- a/rust1.32/compile.launcher.rs
+++ b/rust1.32/src/action_loop/src/main.rs
@@ -11,6 +11,7 @@ use serde_json::{Value, Error};
 use actions::main as actionMain;
 
 fn main() {
+    let mut fd3 = unsafe { File::from_raw_fd(3)};
     loop {
         let mut buffer = String::new();
         io::stdin().read_line(&mut buffer).unwrap();
@@ -23,19 +24,27 @@ fn main() {
                         let mut 
unparsed_payload:Result<HashMap<String,Value>,Error> = 
serde_json::from_value(val);
                         match unparsed_payload {
                             Ok(value) => payload = value,
-                            Err(_) => eprintln!("Error parsing value json")
+                            Err(err) => {
+                                eprintln!("Error parsing value json: {}", err);
+                                continue
+                            }
                         }
                     } else {
                         env::set_var(format!("__OW_{}", key.to_uppercase()), 
val.to_string());
                     }
                 }
             }
-            Err(e) => eprintln!("Error: {}", e)
+            Err(e) =>{
+                eprintln!("Error: {}", e);
+                continue
+            }
+        }
+        
+        match serde_json::to_string(&actionMain(payload)){
+            Ok(result) => { write!(&mut fd3, "{}\n", result).expect("Error 
writting on fd3");}
+            Err(err) => {  eprintln!("Error parsing resul value json: {}", 
err);}
         }
-        let action_results = actionMain(payload);
-        let mut fd3 = unsafe { File::from_raw_fd(3) };
-        write!(&mut fd3, "{}", action_results);
-        stdout().flush();
-        stderr().flush();
+        stdout().flush().expect("Error flushing stdout");
+        stderr().flush().expect("Error flushing stderr");
     }
 }
diff --git a/rust1.32/src/actions/.gitignore b/rust1.32/src/actions/.gitignore
new file mode 100644
index 0000000..6936990
--- /dev/null
+++ b/rust1.32/src/actions/.gitignore
@@ -0,0 +1,3 @@
+/target
+**/*.rs.bk
+Cargo.lock
diff --git a/rust1.32/src/actions/Cargo.toml b/rust1.32/src/actions/Cargo.toml
new file mode 100644
index 0000000..315401c
--- /dev/null
+++ b/rust1.32/src/actions/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "actions"
+version = "0.1.0"
+authors = ["Roberto Diaz <robe...@theagilemonkeys.com>"]
+
+[dependencies]
+serde_json = "1.0"
+
diff --git a/rust1.32/src/actions/src/lib.rs b/rust1.32/src/actions/src/lib.rs
new file mode 100644
index 0000000..cbeafbd
--- /dev/null
+++ b/rust1.32/src/actions/src/lib.rs
@@ -0,0 +1,10 @@
+extern crate serde_json;
+
+use std::collections::HashMap;
+use serde_json::Value;
+
+
+pub fn main(mut input_data: HashMap<String, Value>) -> HashMap<String, Value> {
+    
input_data.insert("added_key".to_string(),Value::String("test".to_string()));
+    input_data
+}

Reply via email to