Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add initial Windows support #17

Merged
merged 17 commits into from
May 2, 2019
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
33 changes: 25 additions & 8 deletions src/providers/native/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import * as ms from 'ms';
import * as uuid from 'uuid/v4';
import createDebug from 'debug';
import { AddressInfo } from 'net';
import { basename, join, resolve } from 'path';
import * as listen from 'async-listen';
import { Pool, createPool } from 'generic-pool';
import { delimiter, basename, join, resolve } from 'path';
import { ChildProcess, spawn } from 'child_process';
import { RuntimeServer } from '../../runtime-server';
import {
Expand All @@ -15,6 +15,7 @@ import {
Provider
} from '../../types';

const isWin = process.platform === 'win32';
const debug = createDebug('@zeit/fun:providers/native');

export default class NativeProvider implements Provider {
Expand Down Expand Up @@ -58,7 +59,10 @@ export default class NativeProvider implements Provider {
async createProcess(): Promise<ChildProcess> {
const { runtime, params, region, version, extractedDir } = this.lambda;
const binDir = join(runtime.cacheDir, 'bin');
const bootstrap = join(runtime.cacheDir, 'bootstrap');
const bootstrap = join(
runtime.cacheDir,
isWin ? 'bootstrap.js' : 'bootstrap'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not always use bootstrap.js? Why is Windows different here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so bizarre that it would definitely warrant a comment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically there's two implementations of these "bootstrap" logic. Ideally just temporarily, and we use the .js ones for all platforms, but for now running the .js ones on unix was causing tests to fail. So consider this just an in-progress transition to that while we get Windows initially/incrementally working.

);

const server = new RuntimeServer(this.lambda);
await listen(server, 0, '127.0.0.1');
Expand All @@ -78,7 +82,7 @@ export default class NativeProvider implements Provider {
// https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html
const env = {
// Non-reserved env vars (can overwrite with params)
PATH: `${binDir}:${process.env.PATH}`,
PATH: `${binDir}${delimiter}${process.env.PATH}`,
LANG: 'en_US.UTF-8',

// User env vars
Expand All @@ -100,7 +104,14 @@ export default class NativeProvider implements Provider {
TZ: ':UTC'
};

const proc = spawn(bootstrap, [], {
let bin: string = bootstrap;
const args: string[] = [];
if (isWin) {
args.push(bootstrap);
bin = process.execPath;
}

const proc = spawn(bin, args, {
env,
cwd: taskDir,
stdio: ['ignore', 'inherit', 'inherit']
Expand Down Expand Up @@ -148,13 +159,19 @@ export default class NativeProvider implements Provider {
}

freezeProcess(proc: ChildProcess) {
debug('Freezing process %o', proc.pid);
process.kill(proc.pid, 'SIGSTOP');
// `SIGSTOP` is not supported on Windows
if (!isWin) {
debug('Freezing process %o', proc.pid);
process.kill(proc.pid, 'SIGSTOP');
}
}

unfreezeProcess(proc: ChildProcess) {
debug('Unfreezing process %o', proc.pid);
process.kill(proc.pid, 'SIGCONT');
// `SIGCONT` is not supported on Windows
if (!isWin) {
debug('Unfreezing process %o', proc.pid);
process.kill(proc.pid, 'SIGCONT');
}
}

async invoke(params: InvokeParams): Promise<InvokeResult> {
Expand Down
5 changes: 5 additions & 0 deletions src/runtimes/go1.x/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { join } from 'path';
import { spawn } from 'child_process';

const bootstrap = join(__dirname, 'bootstrap');
spawn(bootstrap, [], { stdio: 'inherit' });
6 changes: 6 additions & 0 deletions src/runtimes/nodejs6.10/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { join } from 'path';
import { spawn } from 'child_process';

const nodeBin = join(__dirname, 'bin', 'node');
const bootstrap = join(__dirname, '..', 'nodejs', 'bootstrap.js');
spawn(nodeBin, [ bootstrap ], { stdio: 'inherit' });
6 changes: 6 additions & 0 deletions src/runtimes/nodejs8.10/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { join } from 'path';
import { spawn } from 'child_process';

const nodeBin = join(__dirname, 'bin', 'node');
const bootstrap = join(__dirname, '..', 'nodejs', 'bootstrap.js');
spawn(nodeBin, [ bootstrap ], { stdio: 'inherit' });
6 changes: 6 additions & 0 deletions src/runtimes/provided/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { join } from 'path';
import { spawn } from 'child_process';

// Delegate out to the provided `bootstrap` file within the lambda
const bootstrap = join(process.env.LAMBDA_TASK_ROOT, 'bootstrap');
spawn(bootstrap, [], { stdio: 'inherit' });
11 changes: 11 additions & 0 deletions src/runtimes/python/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { join } from 'path';
import { spawn } from 'child_process';

// `PYTHONPATH` is *not* a restricted env var, so only set the
// default one if the user did not provide one of their own
if (!process.env.PYTHONPATH) {
process.env.PYTHONPATH = process.env.LAMBDA_RUNTIME_DIR;
}

const bootstrap = join(__dirname, 'bootstrap.py');
spawn('python', [ bootstrap ], { stdio: 'inherit' });
6 changes: 6 additions & 0 deletions src/runtimes/python2.7/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { join } from 'path';
import { spawn } from 'child_process';

const pythonBin = join(__dirname, 'bin', 'python');
const bootstrap = join(__dirname, '..', 'python', 'bootstrap.py');
spawn(pythonBin, [ bootstrap ], { stdio: 'inherit' });
6 changes: 6 additions & 0 deletions src/runtimes/python3.6/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { join } from 'path';
import { spawn } from 'child_process';

const pythonBin = join(__dirname, 'bin', 'python');
const bootstrap = join(__dirname, '..', 'python', 'bootstrap.py');
spawn(pythonBin, [ bootstrap ], { stdio: 'inherit' });
6 changes: 6 additions & 0 deletions src/runtimes/python3.7/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { join } from 'path';
import { spawn } from 'child_process';

const pythonBin = join(__dirname, 'bin', 'python');
const bootstrap = join(__dirname, '..', 'python', 'bootstrap.py');
spawn(pythonBin, [ bootstrap ], { stdio: 'inherit' });