Skip to content

Commit 340ca11

Browse files
committed
action compiler
1 parent 36b4930 commit 340ca11

File tree

5 files changed

+185
-0
lines changed

5 files changed

+185
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
# incubator-openwhisk-runtime-rust
2+
3+
Work in Progress! Do not use...

rust1.32/Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
FROM actionloop/actionloop-v2:latest as builder
18+
FROM rust:1.32
19+
COPY --from=builder /bin/proxy /bin/proxy
20+
RUN mkdir -p /action
21+
ADD compile /bin/compile
22+
ADD compile.launcher.rs /bin/compile.launcher.rs
23+
ENV OW_COMPILER=/bin/compile
24+
WORKDIR /action
25+
ENTRYPOINT ["/bin/proxy"]

rust1.32/Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
USER?=openwhisk
2+
IMAGE=actionloop-rust-v1.32
3+
4+
.PHONY: build
5+
6+
build:
7+
docker build -t $(USER)/$(IMAGE) .
8+
9+
devel: build
10+
docker run -ti -p 8080:8080 --entrypoint=bash \
11+
-v $(PWD):/mnt -e OW_COMPILER=/mnt/compile \
12+
$(USER)/$(IMAGE)

rust1.32/compile

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env python3
2+
"""Rust Action Builder
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership.
7+
# The ASF licenses this file to You under the Apache License, Version 2.0
8+
# (the "License"); you may not use this file except in compliance with
9+
# the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
"""
20+
21+
from __future__ import print_function
22+
import os, sys, codecs, subprocess
23+
from os.path import abspath, exists, dirname
24+
import tempfile
25+
26+
## utils
27+
# write a file creating intermediate directories
28+
def write_file(file, body):
29+
os.makedirs(dirname(file), mode=0o755, exist_ok=True)
30+
with open(file, mode="w", encoding="utf-8") as f:
31+
f.write(body)
32+
33+
# copy a file eventually replacing a substring
34+
def copy_replace(src, dst, match=None, replacement=""):
35+
with codecs.open(src, 'r', 'utf-8') as s:
36+
body = s.read()
37+
if match:
38+
body = body.replace(match, replacement)
39+
write_file(dst, body)
40+
41+
## cargo
42+
cargo_action = """[package]
43+
name = "actions"
44+
version = "0.1.0"
45+
46+
[dependencies]
47+
serde_json = "1.0"
48+
"""
49+
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():
71+
pass
72+
73+
def sources(main, src_dir, tgt_dir, launcher):
74+
src_file = abspath("%s/exec" % src_dir)
75+
76+
# move exec in the right place
77+
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")
80+
81+
# add a cargo.toml if needed
82+
cargo_action_file = src_dir+"/Cargo.toml"
83+
if not exists(cargo_action_file):
84+
write_file(cargo_action_file, cargo_action)
85+
86+
# 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",
90+
"use actions::main as actionMain;",
91+
"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
96+
97+
if __name__ == '__main__':
98+
if len(sys.argv) < 4:
99+
sys.stdout.write("usage: <main-function> <source-dir> <target-dir>\n")
100+
sys.stdout.flush()
101+
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)
104+
sys.stdout.flush()
105+
sys.stderr.flush()

rust1.32/compile.launcher.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
extern crate serde_json;
2+
extern crate actions;
3+
extern crate libc;
4+
5+
use std::env;
6+
use std::io::{self, Write, stdout, stderr};
7+
use std::fs::File;
8+
use std::os::unix::io::FromRawFd;
9+
use std::collections::HashMap;
10+
use serde_json::{Value, Error};
11+
use actions::main as actionMain;
12+
13+
fn main() {
14+
loop {
15+
let mut buffer = String::new();
16+
io::stdin().read_line(&mut buffer).unwrap();
17+
let parsed_input:Result<HashMap<String,Value>,Error> = serde_json::from_str(&buffer);
18+
let mut payload:HashMap<String, Value> = HashMap::new();
19+
match parsed_input {
20+
Ok(n) => {
21+
for (key, val) in n {
22+
if key == "value" {
23+
let mut unparsed_payload:Result<HashMap<String,Value>,Error> = serde_json::from_value(val);
24+
match unparsed_payload {
25+
Ok(value) => payload = value,
26+
Err(_) => eprintln!("Error parsing value json")
27+
}
28+
} else {
29+
env::set_var(format!("__OW_{}", key.to_uppercase()), val.to_string());
30+
}
31+
}
32+
}
33+
Err(e) => eprintln!("Error: {}", e)
34+
}
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();
40+
}
41+
}

0 commit comments

Comments
 (0)