mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Add publish to heroku support (merge pull request #104 from jacobian)
datasette publish heroku mydb.db
This commit is contained in:
commit
e47117ce1d
2 changed files with 96 additions and 23 deletions
|
|
@ -2,11 +2,11 @@ import click
|
||||||
from click_default_group import DefaultGroup
|
from click_default_group import DefaultGroup
|
||||||
import json
|
import json
|
||||||
import shutil
|
import shutil
|
||||||
from subprocess import call
|
from subprocess import call, check_output
|
||||||
import sys
|
import sys
|
||||||
from .app import Datasette
|
from .app import Datasette
|
||||||
from .utils import (
|
from .utils import (
|
||||||
temporary_docker_directory,
|
temporary_docker_directory, temporary_heroku_directory
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -27,11 +27,11 @@ def build(files, inspect_file):
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@click.argument('publisher', type=click.Choice(['now']))
|
@click.argument('publisher', type=click.Choice(['now', 'heroku']))
|
||||||
@click.argument('files', type=click.Path(exists=True), nargs=-1)
|
@click.argument('files', type=click.Path(exists=True), nargs=-1)
|
||||||
@click.option(
|
@click.option(
|
||||||
'-n', '--name', default='datasette',
|
'-n', '--name', default='datasette',
|
||||||
help='Application name to use when deploying to Now'
|
help='Application name to use when deploying to Now (ignored for Heroku)'
|
||||||
)
|
)
|
||||||
@click.option(
|
@click.option(
|
||||||
'-m', '--metadata', type=click.File(mode='r'),
|
'-m', '--metadata', type=click.File(mode='r'),
|
||||||
|
|
@ -49,28 +49,47 @@ def publish(publisher, files, name, metadata, extra_options, force, branch, **ex
|
||||||
"""
|
"""
|
||||||
Publish specified SQLite database files to the internet along with a datasette API.
|
Publish specified SQLite database files to the internet along with a datasette API.
|
||||||
|
|
||||||
Only current option for PUBLISHER is 'now'. You must have Zeit Now installed:
|
Options for PUBLISHER:
|
||||||
https://zeit.co/now
|
* 'now' - You must have Zeit Now installed: https://zeit.co/now
|
||||||
|
* 'heroku' - You must have Heroku installed: https://cli.heroku.com/
|
||||||
|
|
||||||
Example usage: datasette publish now my-database.db
|
Example usage: datasette publish now my-database.db
|
||||||
"""
|
"""
|
||||||
if not shutil.which('now'):
|
def _fail_if_publish_binary_not_installed(binary, publish_target, install_link):
|
||||||
click.secho(
|
"""Exit (with error message) if ``binary` isn't installed"""
|
||||||
' The publish command requires "now" to be installed and configured ',
|
if not shutil.which(binary):
|
||||||
bg='red',
|
click.secho(
|
||||||
fg='white',
|
f" Publishing to {publish_target} requires {binary} to be installed and configured ",
|
||||||
bold=True,
|
bg='red',
|
||||||
err=True,
|
fg='white',
|
||||||
)
|
bold=True,
|
||||||
click.echo('Follow the instructions at https://zeit.co/now#whats-now', err=True)
|
err=True
|
||||||
sys.exit(1)
|
)
|
||||||
|
click.echo(f"Follow the instructions at {install_link}", err=True)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
with temporary_docker_directory(files, name, metadata, extra_options, branch, extra_metadata):
|
if publisher == 'now':
|
||||||
if force:
|
_fail_if_publish_binary_not_installed('now', 'Zeit Now', 'https://zeit.co/now')
|
||||||
call(['now', '--force'])
|
with temporary_docker_directory(files, name, metadata, extra_options, branch, extra_metadata):
|
||||||
else:
|
if force:
|
||||||
call('now')
|
call(['now', '--force'])
|
||||||
|
else:
|
||||||
|
call('now')
|
||||||
|
|
||||||
|
elif publisher == 'heroku':
|
||||||
|
_fail_if_publish_binary_not_installed('heroku', 'Heroku', 'https://cli.heroku.com')
|
||||||
|
|
||||||
|
# Check for heroku-builds plugin
|
||||||
|
plugins = [line.split()[0] for line in check_output(['heroku', 'plugins']).splitlines()]
|
||||||
|
if b'heroku-builds' not in plugins:
|
||||||
|
click.echo('Publishing to Heroku requires the heroku-builds plugin to be installed.')
|
||||||
|
click.confirm('Install it? (this will run `heroku plugins:install heroku-builds`)', abort=True)
|
||||||
|
call(["heroku", "plugins:install", "heroku-builds"])
|
||||||
|
|
||||||
|
with temporary_heroku_directory(files, name, metadata, extra_options, branch, extra_metadata):
|
||||||
|
create_output = check_output(['heroku', 'apps:create', '--json'])
|
||||||
|
app_name = json.loads(create_output)["name"]
|
||||||
|
call(["heroku", "builds:create", "-a", app_name])
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@click.argument('files', type=click.Path(exists=True), nargs=-1, required=True)
|
@click.argument('files', type=click.Path(exists=True), nargs=-1, required=True)
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ import base64
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import shlex
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import tempfile
|
import tempfile
|
||||||
import time
|
import time
|
||||||
|
|
@ -175,6 +176,59 @@ def temporary_docker_directory(files, name, metadata, extra_options, branch=None
|
||||||
os.chdir(saved_cwd)
|
os.chdir(saved_cwd)
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def temporary_heroku_directory(files, name, metadata, extra_options, branch=None, extra_metadata=None):
|
||||||
|
# FIXME: lots of duplicated code from above
|
||||||
|
|
||||||
|
extra_metadata = extra_metadata or {}
|
||||||
|
tmp = tempfile.TemporaryDirectory()
|
||||||
|
saved_cwd = os.getcwd()
|
||||||
|
|
||||||
|
file_paths = [
|
||||||
|
os.path.join(saved_cwd, name)
|
||||||
|
for name in files
|
||||||
|
]
|
||||||
|
file_names = [os.path.split(f)[-1] for f in files]
|
||||||
|
|
||||||
|
if metadata:
|
||||||
|
metadata_content = json.load(metadata)
|
||||||
|
else:
|
||||||
|
metadata_content = {}
|
||||||
|
for key, value in extra_metadata.items():
|
||||||
|
if value:
|
||||||
|
metadata_content[key] = value
|
||||||
|
|
||||||
|
try:
|
||||||
|
os.chdir(tmp.name)
|
||||||
|
|
||||||
|
if metadata_content:
|
||||||
|
open('metadata.json', 'w').write(json.dumps(metadata_content, indent=2))
|
||||||
|
|
||||||
|
open('runtime.txt', 'w').write('python-3.6.2')
|
||||||
|
|
||||||
|
if branch:
|
||||||
|
install_from = f'https://github.com/simonw/datasette/archive/{branch}.zip'
|
||||||
|
else:
|
||||||
|
install_from = 'datasette'
|
||||||
|
|
||||||
|
open('requirements.txt', 'w').write(install_from)
|
||||||
|
os.mkdir('bin')
|
||||||
|
open('bin/post_compile', 'w').write('datasette build --inspect-file inspect-data.json')
|
||||||
|
|
||||||
|
quoted_files = " ".join(map(shlex.quote, files))
|
||||||
|
procfile_cmd = f'web: datasette serve --host 0.0.0.0 {quoted_files} --cors --port $PORT --inspect-file inspect-data.json'
|
||||||
|
open('Procfile', 'w').write(procfile_cmd)
|
||||||
|
|
||||||
|
for path, filename in zip(file_paths, file_names):
|
||||||
|
os.link(path, os.path.join(tmp.name, filename))
|
||||||
|
|
||||||
|
yield
|
||||||
|
|
||||||
|
finally:
|
||||||
|
tmp.cleanup()
|
||||||
|
os.chdir(saved_cwd)
|
||||||
|
|
||||||
|
|
||||||
def get_all_foreign_keys(conn):
|
def get_all_foreign_keys(conn):
|
||||||
tables = [r[0] for r in conn.execute('select name from sqlite_master where type="table"')]
|
tables = [r[0] for r in conn.execute('select name from sqlite_master where type="table"')]
|
||||||
table_to_foreign_keys = {}
|
table_to_foreign_keys = {}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue