Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
James Hale committed Mar 2, 2019
0 parents commit 2184205
Show file tree
Hide file tree
Showing 16 changed files with 1,849 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
23 changes: 23 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"extends": ["prettier", "airbnb-base"],
"rules": {
"no-console": 0,
"import/no-unresolved": 0,
"import/no-dynamic-require": 0,
"global-require": 0
},
"overrides": [
{
"files": ["test/**"],
"rules": {
"import/no-extraneous-dependencies": 0
},
"globals": {
"describe": true,
"it": true,
"test": true,
"expect": true
}
}
]
}
3 changes: 3 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 The Ardent Company

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
107 changes: 107 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# now-python-wsgi
*A Now builder for Python WSGI applications*

## Quickstart

If you have an existing WSGI app, getting this builder to work for you is a
piece of 🍰!


### 1. Add a Now configuration

Add a `now.json` file to the root of your application:

```json
{
"version": 2,
"name": "python-wsgi-app",
"builds": [{
"src": "index.py",
"use": "@ardent-labs/now-python-wsgi",
"config": { "maxLambdaSize": "15mb" }
}]
}
```

This configuration is doing a few things in the `"builds"` part:

1. `"src": "index.py"`
This tells Now that there is one entrypoint to build for. `index.py` is a
file we'll create shortly.
2. `use": "@ardent-labs/now-python-wsgi"`
Tell Now to use this builder when deploying your application
3. `"config": { "maxLambdaSize": "15mb" }`
Bump up the maximum size of the built application to accommodate some larger
python WSGI libraries (like Django or Flask). This may not be necessary for
you.


### 2. Add a Now entrypoint

Add `index.py` to the root of your application. This entrypoint should make
available an object named `application` that is an instance of your WSGI
application. E.g.:

```python
# For a Dango app
from django_app.wsgi import application
# Replace `django_app` with the appropriate name to point towards your project's
# wsgi.py file
```

Look at your framework documentation for help getting access to the WSGI
application.

If the WSGI instance isn't named `application` you'll need adjust the import so
the builder can find it when your project is being deployed. E.g.:

```python
from my_app.wsgi import app as application
```


### 3. Deploy!

That's it, you're ready to go:

```
$ now
> Deploying python-wsgi-app
...
> Success! Deployment ready [57s]
```


## Additional considerations

### Requirements & runtime

At the time of writing, Zeit Now runs on AWS Lambda in the python 3.6 runtime.
This has a number of implications on what libaries will be available to you,
notably:

- PostgreSQL, so psycopg2 won't work out of the box
- MySQL, so MySQL adapters won't work out of the box either
- Sqlite, so the built-in Sqlite adapter won't be available
- Python <3.6, so python 2 code will need an update


## Contributing

### To-dos

- [ ] Add tests for various types of requests


## Attribution

This implementation draws upon work from:

- [@clement](https://github.com/rclement) on
[now-builders/#163](https://github.com/zeit/now-builders/pull/163)
- [serverless](https://github.com/serverless/serverless) and
[serverless-wsgi](https://github.com/logandk/serverless-wsgi)
- [@sisp](https://github.com/sisp) on
[now-builders/#95](https://github.com/zeit/now-builders/pull/95)
- [Zappa](https://github.com/Miserlou/Zappa) by
[@miserlou](https://github.com/Miserlou)
57 changes: 57 additions & 0 deletions download-and-install-pip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const path = require('path');
const fetch = require('node-fetch');
const execa = require('execa');
const { createWriteStream } = require('fs');

const getWritableDirectory = require('@now/build-utils/fs/get-writable-directory.js'); // eslint-disable-line import/no-extraneous-dependencies

const url = 'https://bootstrap.pypa.io/get-pip.py';

// downloads `get-pip.py` and returns its absolute path
async function downloadGetPipScript() {
console.log('downloading "get-pip.py"...');
const res = await fetch(url);

if (!res.ok || res.status !== 200) {
throw new Error(`Could not download "get-pip.py" from "${url}"`);
}

const dir = await getWritableDirectory();
const filePath = path.join(dir, 'get-pip.py');
const writeStream = createWriteStream(filePath);

return new Promise((resolve, reject) => {
res.body
.on('error', reject)
.pipe(writeStream)
.on('finish', () => resolve(filePath));
});
}

// downloads and installs `pip` (respecting
// process.env.PYTHONUSERBASE), and returns
// the absolute path to it
async function downloadAndInstallPip() {
if (!process.env.PYTHONUSERBASE) {
// this is the directory in which `pip` will be
// installed to. `--user` will assume `~` if this
// is not set, and `~` is not writeable on AWS Lambda.
// let's refuse to proceed
throw new Error(
'Could not install "pip": "PYTHONUSERBASE" env var is not set',
);
}
const getPipFilePath = await downloadGetPipScript();

console.log('runing "python get-pip.py"...');
try {
await execa('python3', [getPipFilePath, '--user'], { stdio: 'inherit' });
} catch (err) {
console.log('could not install pip');
throw err;
}

return path.join(process.env.PYTHONUSERBASE, 'bin', 'pip');
}

module.exports = downloadAndInstallPip;
95 changes: 95 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const path = require('path');
const execa = require('execa');
const { readFile, writeFile } = require('fs.promised');
const getWritableDirectory = require('@now/build-utils/fs/get-writable-directory.js'); // eslint-disable-line import/no-extraneous-dependencies
const download = require('@now/build-utils/fs/download.js'); // eslint-disable-line import/no-extraneous-dependencies
const glob = require('@now/build-utils/fs/glob.js'); // eslint-disable-line import/no-extraneous-dependencies
const { createLambda } = require('@now/build-utils/lambda.js'); // eslint-disable-line import/no-extraneous-dependencies

const downloadAndInstallPip = require('./download-and-install-pip');

async function pipInstall(pipPath, srcDir, ...args) {
console.log(`running "pip install -t ${srcDir} ${args.join(' ')}"...`);
try {
await execa(pipPath, ['install', '-t', srcDir, ...args], {
stdio: 'inherit',
});
} catch (err) {
console.log(`failed to run "pip install -t ${srcDir} ${args.join(' ')}"`);
throw err;
}
}

exports.config = {
maxLambdaSize: '5mb',
};

exports.build = async ({ files, entrypoint }) => {
console.log('downloading files...');

const srcDir = await getWritableDirectory();

// eslint-disable-next-line no-param-reassign
files = await download(files, srcDir);

// this is where `pip` will be installed to
// we need it to be under `/tmp`
const pyUserBase = await getWritableDirectory();
process.env.PYTHONUSERBASE = pyUserBase;

const pipPath = await downloadAndInstallPip();

console.log('installing handler requirements');
await pipInstall(pipPath, srcDir, '-r',
path.join(__dirname, 'requirements.txt'));

const entryDirectory = path.dirname(entrypoint);
const requirementsTxt = path.join(entryDirectory, 'requirements.txt');

if (files[requirementsTxt]) {
console.log('found local "requirements.txt"');

const requirementsTxtPath = files[requirementsTxt].fsPath;
await pipInstall(pipPath, srcDir, '-r', requirementsTxtPath);
} else if (files['requirements.txt']) {
console.log('found global "requirements.txt"');

const requirementsTxtPath = files['requirements.txt'].fsPath;
await pipInstall(pipPath, srcDir, '-r', requirementsTxtPath);
}

const originalNowHandlerPyContents = await readFile(
path.join(__dirname, 'now_python_wsgi', 'handler.py'),
'utf8',
);
// will be used on `from $here import handler`
// for example, `from api.users import handler`
console.log('entrypoint is', entrypoint);
const userHandlerFilePath = entrypoint
.replace(/\//g, '.')
.replace(/\.py$/, '');
const nowHandlerPyContents = originalNowHandlerPyContents.replace(
'__NOW_HANDLER_FILENAME',
userHandlerFilePath,
);

// in order to allow the user to have `server.py`, we need our `server.py` to be called
// somethig else
const nowHandlerPyFilename = 'now__handler__python';

await writeFile(
path.join(srcDir, `${nowHandlerPyFilename}.py`),
nowHandlerPyContents,
);

const lambda = await createLambda({
files: await glob('**', srcDir),
handler: `${nowHandlerPyFilename}.now_handler`,
runtime: 'python3.6',
environment: {},
});

return {
[entrypoint]: lambda,
};
};
1 change: 1 addition & 0 deletions now_python_wsgi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .handler import handler # noqa
Loading

0 comments on commit 2184205

Please sign in to comment.