Skip to content

Commit

Permalink
Merge branch 'main' into rybickic/momento
Browse files Browse the repository at this point in the history
  • Loading branch information
Chriscbr authored Jun 17, 2024
2 parents 4e584db + 066409a commit c1591d1
Show file tree
Hide file tree
Showing 20 changed files with 274 additions and 48 deletions.
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Auto-generated by ./mkrepo.sh

/.github/workflows/*-pull.yaml linguist-generated
/.github/workflows/*-release.yaml linguist-generated
/**/package-lock.json linguist-generated
/**/*.extern.d.ts linguist-generated
/package-lock.json linguist-generated
16 changes: 16 additions & 0 deletions .mkrepo/gitattributes.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
bring fs;

pub class GitAttributes {
new() {
let lines = MutArray<str>[];
lines.push("# Auto-generated by ./mkrepo.sh");
lines.push("");
lines.push("/.github/workflows/*-pull.yaml linguist-generated");
lines.push("/.github/workflows/*-release.yaml linguist-generated");
lines.push("/**/package-lock.json linguist-generated");
lines.push("/**/*.extern.d.ts linguist-generated");
lines.push("/package-lock.json linguist-generated");
lines.push("");
fs.writeFile(".gitattributes", lines.join("\n"));
}
}
2 changes: 2 additions & 0 deletions .mkrepo/main.w
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
bring fs;
bring "./canary.w" as canary;
bring "./gitattributes.w" as gitattributes;
bring "./library.w" as l;
bring "./mergify.w" as mergify;
bring "./pr-lint.w" as prlint;
Expand Down Expand Up @@ -34,6 +35,7 @@ new stale.StaleWorkflow(workflowdir);
new mergify.MergifyWorkflow(libs.copy());
new prdiff.PullRequestDiffWorkflow(workflowdir);
new prlint.PullRequestLintWorkflow(workflowdir, libs.copy());
new gitattributes.GitAttributes();

let skipCanaryTests = [
"containers" // https://github.com/winglang/wing/issues/5716
Expand Down
4 changes: 2 additions & 2 deletions python/package-lock.json

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

2 changes: 1 addition & 1 deletion python/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@winglibs/python",
"description": "python library for Wing",
"version": "0.0.7",
"version": "0.0.10",
"repository": {
"type": "git",
"url": "https://github.com/winglang/winglibs.git",
Expand Down
18 changes: 15 additions & 3 deletions python/sim/containers.w
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ bring sim;
bring fs;

pub class Util {
pub extern "./util.js" inflight static _spawn(command: str, args: Array<str>, options: Map<Json>): void;

pub static entrypointDir(scope: std.IResource): str {
return std.Node.of(scope).app.entrypointDir;
}
Expand Down Expand Up @@ -153,7 +155,7 @@ pub class Container {
// start the new container
let dockerRun = MutArray<str>[];
dockerRun.push("run");
dockerRun.push("--detach");
dockerRun.push("-i");
dockerRun.push("--rm");

dockerRun.push("--name", containerName);
Expand Down Expand Up @@ -217,7 +219,8 @@ pub class Container {

log("starting container from image {this.imageTag}");
log("docker {dockerRun.join(" ")}");
util.exec("docker", dockerRun.copy(), { env: { PATH: pathEnv } });
Util._spawn("docker", dockerRun.copy(), { env: { PATH: pathEnv } });
// util.exec("docker", dockerRun.copy(), { env: { PATH: pathEnv } });

log("containerName={containerName}");

Expand All @@ -233,6 +236,15 @@ pub class Container {
util.waitUntil(inflight () => {
try {
out = Json.parse(util.exec("docker", ["inspect", containerName], { env: { PATH: pathEnv } }).stdout);

if let port = opts.port {
if let network = opts.network {
if network == "host" {
return out?.tryGetAt(0)?.tryGet("Config")?.tryGet("ExposedPorts")?.tryGet("{port}/tcp") != nil;
}
}
return out?.tryGetAt(0)?.tryGet("NetworkSettings")?.tryGet("Ports")?.tryGet("{port}/tcp")?.tryGetAt(0)?.tryGet("HostPort")?.tryAsStr() != nil;
}
return true;
} catch {
log("something went wrong");
Expand Down Expand Up @@ -296,4 +308,4 @@ pub class Container {
this.containerService.start();
this.readinessService.start();
}
}
}
31 changes: 22 additions & 9 deletions python/sim/function.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
const { App } = require("@winglang/sdk/lib/core");
const { Function: SimFunction } = require("@winglang/sdk/lib/target-sim/function.js");

module.exports.handleSimInflight = (inflight, props) => {
for (let e in props.env) {
inflight.inner.service.addEnvironment(e, props.env[e]);
}
module.exports.Function = class Function extends SimFunction {
constructor(
scope,
id,
inflight,
props = {},
pythonInflight,
) {
super(scope, id, inflight, props);

this.pythonInflight = pythonInflight;

for (let e in props.env) {
this.pythonInflight.inner.service.addEnvironment(e, props.env[e]);
}

if (!App.of(inflight).isTestEnvironment) {
for (let key of ["AWS_REGION", "AWS_DEFAULT_REGION", "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY"]) {
let value = process.env[key];
if (value) {
inflight.inner.service.addEnvironment(key, value);
if (!App.of(this).isTestEnvironment) {
for (let key of ["AWS_REGION", "AWS_DEFAULT_REGION", "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY"]) {
let value = process.env[key];
if (value) {
this.pythonInflight.inner.service.addEnvironment(key, value);
}
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions python/sim/inflight.w
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ bring cloud;
bring util;
bring http;
bring math;
bring sim;
bring "./containers.w" as containers;
bring "../types.w" as types;
bring "../util.w" as libutil;

pub class Inflight impl cloud.IFunctionHandler {
pub url: str;
service: cloud.Service;
network: str?;
clients: MutMap<types.LiftedSim>;

new(props: types.InflightProps) {
let homeEnv = util.tryEnv("HOME") ?? "";
let pathEnv = util.tryEnv("PATH") ?? "";

let outdir = libutil.build(
let outdir = libutil.buildSim(
nodePath: nodeof(this).path,
path: props.path,
handler: props.handler,
Expand All @@ -34,17 +36,16 @@ pub class Inflight impl cloud.IFunctionHandler {
"127.0.0.1:{runtimePort}"
];
let flags = MutMap<str>{};
let var network: str? = nil;
let platform = Inflight.os();
if platform != "darwin" && platform != "win32" {
network = "host";
this.network = "host";
}

let runner = new containers.Container(
image: "public.ecr.aws/lambda/python:3.12",
image: outdir,
name: "python-runner",
volumes: {
"/var/task:ro,delegated": outdir,
"/var/task:ro,delegated": props.path,
},
env: {
DOCKER_LAMBDA_STAY_OPEN: "1",
Expand All @@ -54,7 +55,7 @@ pub class Inflight impl cloud.IFunctionHandler {
exposedPort: port,
args: args,
flags: flags.copy(),
network: network,
network: this.network,
entrypoint: "/usr/local/bin/aws-lambda-rie",
);

Expand Down Expand Up @@ -104,7 +105,7 @@ pub class Inflight impl cloud.IFunctionHandler {
};

let var host = "http://host.docker.internal";
if let network = network {
if let network = this.network {
if network == "host" {
host = "http://127.0.0.1";
}
Expand Down Expand Up @@ -154,6 +155,9 @@ pub class Inflight impl cloud.IFunctionHandler {
}

let res = http.post(this.url, { body: body });
if !res.ok {
log("Failed to invoke the function: {Json.stringify(res)}");
}
return res.body;
}

Expand Down
1 change: 1 addition & 0 deletions python/sim/util.extern.d.ts

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

21 changes: 21 additions & 0 deletions python/sim/util.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
const { spawn } = require("node:child_process");

exports.env = () => {
return process.env;
};

exports.os = function() {
return process.platform;
};

exports._spawn = function(command, args, options) {
const child = spawn(command, args, {
stdio: "pipe",
...options,
});

child.stdout.on("data", (data) =>
console.log(data.toString().trim())
);

child.stderr.on("data", (data) =>
console.log(data.toString().trim())
);

child.once("error", (err) => {
console.error(err);
});
}
4 changes: 2 additions & 2 deletions python/test-assets/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ def handler(event, context):
print(event)
print(context)

foo_env = os.getenv("FOO")
payload = from_function_event(event)
foo_env = os.getenv("FOO")

email_client = lifted("email")
email_client.send_email(Source="[email protected]", Destination={'ToAddresses': ['[email protected]',],},Message={'Subject': {'Data': 'Winglang Test Email!',},'Body': {'Text': {'Data': 'Hello from Python!',},}},)
Expand Down Expand Up @@ -77,9 +77,9 @@ def api_handler(event, context):
print(event)
print(context)

req = from_api_event(event)
foo = os.getenv("FOO")

req = from_api_event(event)
client_put = lifted("bucket")
client_put.put(req["path"], json.dumps(req))

Expand Down
2 changes: 1 addition & 1 deletion python/test-assets/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wingsdk == 0.0.2
wingsdk == 0.0.4
requests
boto3
Faker
Expand Down
15 changes: 8 additions & 7 deletions python/tfaws/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { ResourceNames } = require("@winglang/sdk/lib/shared/resource-names");
const { DEFAULT_MEMORY_SIZE } = require("@winglang/sdk/lib/shared/function");
const cdktf = require("cdktf");
const awsProvider = require("@cdktf/provider-aws");
const { build } = require("../util.js");
const { buildAws } = require("../util.js");

const FUNCTION_NAME_OPTS = {
maxLen: 64,
Expand All @@ -24,17 +24,18 @@ module.exports.Function = class Function extends Construct {
id,
inflight,
props = {},
pythonInflight,
) {
super(scope, id);

this.dummy = new TfAwsFunction(this, "Dummy", createInflight(async (ctx) => {}));
const pathEnv = process.env["PATH"] || "";
const homeEnv = process.env["HOME"] || "";

const outdir = build({
const outdir = buildAws({
nodePath: Node.of(handler).path,
path: inflight.inner.props.path,
handler: inflight.inner.props.handler,
path: pythonInflight.inner.props.path,
handler: pythonInflight.inner.props.handler,
homeEnv: homeEnv,
pathEnv: pathEnv,
});
Expand All @@ -57,8 +58,8 @@ module.exports.Function = class Function extends Construct {
const roleName = this.dummy.role.name;

const clients = {};
for (let clientId of Object.keys(inflight.inner.lifts)) {
const { client, options } = inflight.inner.lifts[clientId];
for (let clientId of Object.keys(pythonInflight.inner.lifts)) {
const { client, options } = pythonInflight.inner.lifts[clientId];
const allow = options.allow;

// SDK resources
Expand Down Expand Up @@ -122,7 +123,7 @@ module.exports.Function = class Function extends Construct {
this.lambda = new awsProvider.lambdaFunction.LambdaFunction(this, "PyFunction", {
functionName: this.name,
role: roleArn,
handler: inflight.inner.props.handler,
handler: pythonInflight.inner.props.handler,
runtime: "python3.11",
s3Bucket: bucket.bucket,
s3Key: lambdaArchive.key,
Expand Down
3 changes: 2 additions & 1 deletion python/util.extern.d.ts

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

Loading

0 comments on commit c1591d1

Please sign in to comment.