Skip to content

Commit c3a89e1

Browse files
committed
reworked the source generation
1 parent 340ca11 commit c3a89e1

File tree

12 files changed

+186
-45
lines changed

12 files changed

+186
-45
lines changed

example/hello/.gitignore

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
# Created by https://www.gitignore.io/api/osx,rust
3+
# Edit at https://www.gitignore.io/?templates=osx,rust
4+
5+
### OSX ###
6+
# General
7+
.DS_Store
8+
.AppleDouble
9+
.LSOverride
10+
11+
# Icon must end with two \r
12+
Icon
13+
14+
# Thumbnails
15+
._*
16+
17+
# Files that might appear in the root of a volume
18+
.DocumentRevisions-V100
19+
.fseventsd
20+
.Spotlight-V100
21+
.TemporaryItems
22+
.Trashes
23+
.VolumeIcon.icns
24+
.com.apple.timemachine.donotpresent
25+
26+
# Directories potentially created on remote AFP share
27+
.AppleDB
28+
.AppleDesktop
29+
Network Trash Folder
30+
Temporary Items
31+
.apdisk
32+
33+
### Rust ###
34+
# Generated by Cargo
35+
# will have compiled files and executables
36+
/target/
37+
38+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
39+
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
40+
Cargo.lock
41+
42+
# These are backup files generated by rustfmt
43+
**/*.rs.bk
44+
45+
# End of https://www.gitignore.io/api/osx,rust

example/hello/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "actions"
3+
version = "0.1.0"
4+
authors = ["Roberto Diaz <roberto@theagilemonkeys.com>"]
5+
6+
[dependencies]
7+
serde_json = "1.0"
8+

example/hello/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extern crate serde_json;
2+
3+
use std::collections::HashMap;
4+
use serde_json::Value;
5+
6+
pub fn main(mut input_data: HashMap<String, Value>) -> HashMap<String, Value> {
7+
input_data.insert("hello".to_string(),Value::String("world".to_string()));
8+
input_data
9+
}

rust1.32/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ FROM rust:1.32
1919
COPY --from=builder /bin/proxy /bin/proxy
2020
RUN mkdir -p /action
2121
ADD compile /bin/compile
22-
ADD compile.launcher.rs /bin/compile.launcher.rs
22+
ADD src /usr/src
23+
RUN cd /usr/src ; cargo build
2324
ENV OW_COMPILER=/bin/compile
2425
WORKDIR /action
2526
ENTRYPOINT ["/bin/proxy"]

rust1.32/compile

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
from __future__ import print_function
2222
import os, sys, codecs, subprocess
2323
from os.path import abspath, exists, dirname
24-
import tempfile
24+
import time
25+
import shutil
2526

2627
## utils
2728
# write a file creating intermediate directories
@@ -47,59 +48,39 @@ version = "0.1.0"
4748
serde_json = "1.0"
4849
"""
4950

50-
cargo_actionloop = """[package]
51-
name = "action_loop"
52-
version = "0.1.0"
53-
authors = ["Roberto Diaz <roberto@theagilemonkeys.com>"]
54-
55-
[dependencies]
56-
serde_json = "1.0"
57-
libc = "0.2.49"
58-
actions = { path = "../actions" }
59-
"""
60-
61-
cargo_workspace = """
62-
[workspace]
63-
64-
members = [
65-
"action_loop",
66-
"actions",
67-
]
68-
"""
69-
70-
def build():
51+
def build(tgt_dir):
7152
pass
7253

73-
def sources(main, src_dir, tgt_dir, launcher):
74-
src_file = abspath("%s/exec" % src_dir)
75-
54+
def sources(main, src_dir):
55+
# move away the action dir and replace with the new
56+
tmpname = str(int(time.time()))
57+
shutil.move("/usr/src/actions", "/usr/src/src%s" % tmpname)
58+
shutil.move(src_dir, "/usr/src/actions")
59+
7660
# move exec in the right place
61+
src_file = "/usr/src/actions/exec"
7762
if exists(src_file):
78-
os.makedirs(src_dir+"/src", mode=0o755, exist_ok=True)
79-
copy_replace(src_file, src_dir+"/src/exec__.rs")
63+
os.makedirs("/usr/src/actions/src", mode=0o755, exist_ok=True)
64+
copy_replace(src_file, "/usr/src/actions/src/lib.rs")
8065

8166
# add a cargo.toml if needed
82-
cargo_action_file = src_dir+"/Cargo.toml"
67+
cargo_action_file = "/usr/src/actions/Cargo.toml"
8368
if not exists(cargo_action_file):
8469
write_file(cargo_action_file, cargo_action)
8570

8671
# write the boilerplate in a temp dir
87-
os.makedirs("/tmp/src", mode=0o755, exist_ok=True)
88-
tmp_dir = tempfile.mkdtemp(prefix='/tmp/src/')
89-
copy_replace(launcher, tmp_dir+"/action_loop/src/main.rs",
72+
launcher = "/usr/src/action_loop/tmp%s" % tmpname
73+
shutil.move("/usr/src/action_loop/src/main.rs", launcher)
74+
copy_replace(launcher, "/usr/src/action_loop/src/main.rs",
9075
"use actions::main as actionMain;",
9176
"use actions::%s as actionMain;" % main )
92-
write_file(tmp_dir+"/action_loop/Cargo.toml", cargo_actionloop)
93-
write_file(tmp_dir+"/Cargo.toml", cargo_workspace)
94-
os.rename(src_dir, tmp_dir+"/actions")
95-
return tmp_dir
9677

9778
if __name__ == '__main__':
9879
if len(sys.argv) < 4:
9980
sys.stdout.write("usage: <main-function> <source-dir> <target-dir>\n")
10081
sys.stdout.flush()
10182
sys.exit(1)
102-
dir = sources(sys.argv[1], abspath(sys.argv[2]), abspath(sys.argv[3]), abspath(sys.argv[0]+".launcher.rs"))
103-
print(dir)
83+
sources(sys.argv[1], abspath(sys.argv[2]))
84+
build(abspath(sys.argv[3]))
10485
sys.stdout.flush()
10586
sys.stderr.flush()

rust1.32/src/Cargo.lock

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust1.32/src/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[workspace]
2+
3+
members = [
4+
"action_loop",
5+
"actions",
6+
]

rust1.32/src/action_loop/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "action_loop"
3+
version = "0.1.0"
4+
authors = ["Roberto Diaz <roberto@theagilemonkeys.com>"]
5+
6+
[dependencies]
7+
serde_json = "1.0"
8+
libc = "0.2.49"
9+
actions = { path = "../actions" }

rust1.32/compile.launcher.rs renamed to rust1.32/src/action_loop/src/main.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use serde_json::{Value, Error};
1111
use actions::main as actionMain;
1212

1313
fn main() {
14+
let mut fd3 = unsafe { File::from_raw_fd(3)};
1415
loop {
1516
let mut buffer = String::new();
1617
io::stdin().read_line(&mut buffer).unwrap();
@@ -23,19 +24,27 @@ fn main() {
2324
let mut unparsed_payload:Result<HashMap<String,Value>,Error> = serde_json::from_value(val);
2425
match unparsed_payload {
2526
Ok(value) => payload = value,
26-
Err(_) => eprintln!("Error parsing value json")
27+
Err(err) => {
28+
eprintln!("Error parsing value json: {}", err);
29+
continue
30+
}
2731
}
2832
} else {
2933
env::set_var(format!("__OW_{}", key.to_uppercase()), val.to_string());
3034
}
3135
}
3236
}
33-
Err(e) => eprintln!("Error: {}", e)
37+
Err(e) =>{
38+
eprintln!("Error: {}", e);
39+
continue
40+
}
41+
}
42+
43+
match serde_json::to_string(&actionMain(payload)){
44+
Ok(result) => { write!(&mut fd3, "{}\n", result).expect("Error writting on fd3");}
45+
Err(err) => { eprintln!("Error parsing resul value json: {}", err);}
3446
}
35-
let action_results = actionMain(payload);
36-
let mut fd3 = unsafe { File::from_raw_fd(3) };
37-
write!(&mut fd3, "{}", action_results);
38-
stdout().flush();
39-
stderr().flush();
47+
stdout().flush().expect("Error flushing stdout");
48+
stderr().flush().expect("Error flushing stderr");
4049
}
4150
}

rust1.32/src/actions/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/target
2+
**/*.rs.bk
3+
Cargo.lock

0 commit comments

Comments
 (0)