Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Function environment variables, enable_simd flag #94

Merged
merged 5 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/src/commands/cast/ruby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn compile(project: Rc<Project>, service_name: &str, function: &Function) ->
let mut ruby_wasmu = ruby_bin.clone();
ruby_wasmu.set_extension("wasm.bin");
if !Path::new(&ruby_wasmu).exists() {
wasm::precompile(Path::new(&ruby_wasm), "x86_64-linux-gnu").unwrap();
wasm::precompile(Path::new(&ruby_wasm), "x86_64-linux-gnu", function.enable_simd.unwrap_or(true)).unwrap();
}
let copy_to = format!("{}/ruby.wasm.bin", function_artifact_path.clone());
let copy_result = std::fs::copy(ruby_wasmu.clone(), copy_to.clone());
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/cast/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn compile(project: Rc<Project>, service_name: &str, function: &Function) ->
let mode = "release";
let target = "wasm32-wasi";

println!("Compiling function {}...", function_name.clone());
println!("Compiling function `{}`...", function_name.clone());
let cargo_build = std::process::Command::new("cargo")
.arg("build")
.arg(format!("--{}", mode))
Expand Down Expand Up @@ -81,5 +81,5 @@ pub fn compile(project: Rc<Project>, service_name: &str, function: &Function) ->
panic!("{:?}", copy_result.err());
}

wasm::precompile(Path::new(&copy_to), "x86_64-linux-gnu").unwrap()
wasm::precompile(Path::new(&copy_to), "x86_64-linux-gnu", function.enable_simd.unwrap_or(true)).unwrap()
}
22 changes: 16 additions & 6 deletions cli/src/providers/aws_lambda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ use registry_common::models::GetIomodAtResponse;
use serde::Serialize;

use crate::archive;
use crate::providers::{AWS_LAMBDA_PROVIDER_NAME, DNS_PROVIDERS, flatten, LockBox, Options, Provider, ProviderError, ProviderMap, render_string_list};
use crate::transpiler::{
Artifact, Bindable, Bootable, Castable, CastError, ContentType, context, Template,
};
use crate::providers::{AWS_LAMBDA_PROVIDER_NAME, DNS_PROVIDERS, flatten, LockBox, Options, Provider, ProviderError, ProviderMap, render_string_list, render_string_map};
use crate::transpiler::{Artifact, Bindable, Bootable, Castable, CastError, ContentType, context, StringMap, Template};
use crate::transpiler::context::{Context, Function};

pub struct AwsLambdaProvider {
Expand Down Expand Up @@ -381,6 +379,14 @@ impl Castable for LambdaFunction {
None => None,
};

let environment: StringMap<String> = function
.environment
.clone()
.unwrap_or(Rc::new(StringMap::<String>::new()))
.iter()
.map(|e| (format!("__ASML_{}", e.0.clone()), e.1.clone()))
.collect();

let tmpl = FunctionTemplate {
project_name: ctx.project.name.clone(),
service_name: service.clone(),
Expand Down Expand Up @@ -419,6 +425,7 @@ impl Castable for LambdaFunction {
None => None,
},
auth,
environment: render_string_map(environment),
};

let hcl = Artifact {
Expand Down Expand Up @@ -570,6 +577,7 @@ pub struct FunctionTemplate {
pub size: u16,
pub timeout: u16,
pub project_name: String,
pub environment: String,
}

impl Template for FunctionTemplate {
Expand Down Expand Up @@ -609,9 +617,11 @@ resource aws_lambda_function asml_{{service_name}}_{{function_name}} {
{{/if}}

{{#if ruby_layer}}environment {
variables = {
variables = merge({
ASML_FUNCTION_ENV = "ruby-lambda"
}
}, {{{this.environment}}})
}{{else}}environment {
variables = {{{this.environment}}}
}{{/if}}

layers = [{{runtime_layer}}{{#if iomods_layer}}, {{iomods_layer}}{{/if}}{{#if ruby_layer}}, {{ruby_layer}}{{/if}}]
Expand Down
40 changes: 34 additions & 6 deletions cli/src/providers/k8s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ use serde::Serialize;

use crate::providers::{DNS_PROVIDERS, flatten, gloo, KUBERNETES_PROVIDER_NAME, LockBox, Options, Provider, ProviderError, ProviderMap};
use crate::tools::glooctl::GlooCtl;
use crate::transpiler::{
Artifact, Bindable, Bootable, Castable, CastError, ContentType, context, Template,
};
use crate::transpiler::{Artifact, Bindable, Bootable, Castable, CastError, ContentType, context, StringMap, Template};
use crate::transpiler::context::Context;

fn to_container_registry(r: &context::Registry) -> ContainerRegistry {
Expand Down Expand Up @@ -233,14 +231,25 @@ impl Castable for KubernetesFunction {
let registries: Vec<ContainerRegistry> =
ctx.registries.iter().map(to_container_registry).collect();

let environment: Vec<ContainerEnv> = function
.environment
.clone()
.unwrap_or(Rc::new(StringMap::<String>::new()))
.iter()
.map(|e| ContainerEnv {
name: format!("__ASML_{}", e.0.clone()),
value: e.1.clone()
})
.collect();

let hcl_tmpl = FunctionTemplate {
base_image_version: crate_version!().to_string(),
project_name: ctx.project.name.clone(),
function_name: function.name.clone(),
service_name: service.clone(),
handler_name: match function.language.as_str() {
"rust" => format!("{}.wasmu", function.name.clone()),
"ruby" => "ruby.wasmu".into(),
"rust" => format!("{}.wasm.bin", function.name.clone()),
"ruby" => "ruby.wasm.bin".into(),
_ => "handler".into(),
},
iomods: iomods.clone(),
Expand All @@ -263,6 +272,8 @@ impl Castable for KubernetesFunction {
.clone(),
},
is_ruby: function.language == "ruby".to_string(),
enable_simd: function.enable_simd,
environment,
};
let hcl_content = hcl_tmpl.render();

Expand Down Expand Up @@ -431,8 +442,9 @@ pub struct FunctionTemplate {
pub has_iomods: bool,
pub iomods: Vec<IomodContainer>,
pub registry: ContainerRegistry,

pub environment: Vec<ContainerEnv>,
pub is_ruby: bool,
pub enable_simd: bool,
}

impl Template for FunctionTemplate {
Expand Down Expand Up @@ -526,6 +538,16 @@ resource kubernetes_deployment {{function_name}} {
port {
container_port = 13555
}
{{#each environment}}
env {
name = "{{this.name}}"
value = "{{this.value}}"
}
{{/each}}
env {
name = "ASML_FUNCTION_ENABLE_SIMD"
value = "{{this.enable_simd}}"
}
}
{{#each iomods}}
container {
Expand Down Expand Up @@ -604,3 +626,9 @@ pub struct IomodContainer {
pub image: String,
pub name: String,
}

#[derive(Serialize, Clone, Debug)]
pub struct ContainerEnv {
pub name: String,
pub value: String,
}
12 changes: 12 additions & 0 deletions cli/src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ fn render_string_list(list: Rc<Vec<String>>) -> String {
out
}

fn render_string_map(map: StringMap<String>) -> String {
let mut out = String::from("{");
for (i, p) in map.iter().enumerate() {
out.push_str(&format!("\"{}\"=\"{}\"", p.0, p.1));
if i < map.len() - 1 {
out.push_str(",");
}
}
out.push_str("}");
out
}

fn flatten(mut accum: Vec<Artifact>, mut v: Vec<Artifact>) -> Vec<Artifact> {
let mut out = Vec::new();
out.append(&mut accum);
Expand Down
4 changes: 2 additions & 2 deletions cli/src/terraform/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn apply() {
let mut terraform_result = process::Command::new(relative_binary_path())
.arg("-chdir=./net")
.arg("apply")
.arg("-state=../terraform.tfstate")
.arg("-state=../terraform.tfstate") // FIXME does this work with remote state?
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()
Expand All @@ -53,8 +53,8 @@ pub fn apply() {

pub fn destroy() {
let mut terraform_result = process::Command::new(relative_binary_path())
.arg("-chdir=./net")
.arg("destroy")
.arg("./net")
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()
Expand Down
8 changes: 6 additions & 2 deletions cli/src/transpiler/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ impl Context {
language: function.language.clone().unwrap_or("rust".to_string()),
size: function.size_mb.unwrap_or(1024u16),
timeout: function.timeout_seconds.unwrap_or(5u16),
enable_simd: function.enable_simd.unwrap_or(true),
http: match &function.clone().http.as_ref() {
Some(http) => Some(Http {
verb: http.verb.clone(),
Expand All @@ -107,6 +108,7 @@ impl Context {
None => None,
},
authorizer_id: function.authorizer_id.clone(),
environment: function.environment.clone(),
});
}

Expand Down Expand Up @@ -357,12 +359,13 @@ pub struct Function {
pub registry: String,
pub language: String,
pub service_name: String,

pub environment: Option<Rc<StringMap<String>>>,
pub http: Option<Http>,
pub authorizer_id: Option<String>,

pub size: u16,
pub timeout: u16,
pub enable_simd: bool,
}

pub struct Http {
Expand Down Expand Up @@ -431,7 +434,8 @@ locals {
{{#each providers}}
provider {{tf_name}} {
alias = "{{../project_name}}-{{name}}"
region = "{{options.aws_region}}"
{{#if (eq tf_name "aws")}}region = "{{options.aws_region}}"{{/if}}
{{#if (eq tf_name "kubernetes")}}config_path = pathexpand("~/.kube/config"){{/if}}
}
{{/each}}
{{#if user_inject}}module "usermod" {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/transpiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub trait Bindable {
/// A `Bootable` implements the `boot` step; `boot` is intended to be a run-once operation to prepare
/// any infrastructure that must exist prior to `bind`.
pub trait Bootable {
/// Provides an opportunity deploy prerequisite infra if not already present
/// Provides an opportunity to deploy prerequisite infra if not already present
fn boot(&self, ctx: Rc<Context>) -> Result<(), CastError>;
/// Query to see if boot step has already been run
fn is_booted(&self, ctx: Rc<Context>) -> bool;
Expand Down
4 changes: 4 additions & 0 deletions cli/src/transpiler/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ pub mod service {
authorizer_id: None,
timeout_seconds: None,
size_mb: None,
enable_simd: None,
environment: None,
};
functions.push(fun);
self.api.functions = Rc::new(functions);
Expand Down Expand Up @@ -300,7 +302,9 @@ pub mod service {
pub authorizer_id: Option<String>,
pub timeout_seconds: Option<u16>,
pub size_mb: Option<u16>,
pub enable_simd: Option<bool>,
pub http: Rc<Option<HttpFunction>>,
pub environment: Option<Rc<StringMap<String>>>,
}

#[derive(Serialize, Deserialize, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "assemblylift-core"
version = "0.4.0-alpha.10"
version = "0.4.0-alpha.11"
description = "AssemblyLift core library"
authors = ["Akkoro and the AssemblyLift contributors <assemblylift@akkoro.io>"]
edition = "2018"
Expand Down
Loading