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

"datasette publish cloudrun" command to publish to Google Cloud Run #434

Merged
merged 8 commits into from May 3, 2019
@@ -5,6 +5,7 @@
DEFAULT_PLUGINS = (
"datasette.publish.heroku",
"datasette.publish.now",
"datasette.publish.cloudrun",
)

pm = pluggy.PluginManager("datasette")
@@ -0,0 +1,71 @@
from datasette import hookimpl
import click
import json
from subprocess import check_call, check_output

from .common import (
add_common_publish_arguments_and_options,
fail_if_publish_binary_not_installed,
)
from ..utils import temporary_docker_directory


@hookimpl
def publish_subcommand(publish):
@publish.command()
@add_common_publish_arguments_and_options
@click.option(
"-n",
"--name",
default="datasette",
help="Application name to use when deploying",
)
@click.option("--spatialite", is_flag=True, help="Enable SpatialLite extension")
def cloudrun(
files,
metadata,
extra_options,
branch,
template_dir,
plugins_dir,
static,
install,
version_note,
title,
license,
license_url,
source,
source_url,
about,
about_url,
name,
spatialite,
):
fail_if_publish_binary_not_installed("gcloud", "Google Cloud", "https://cloud.google.com/sdk/")
project = check_output("gcloud config get-value project", shell=True).decode("utf-8").strip()

with temporary_docker_directory(
files,
name,
metadata,
extra_options,
branch,
template_dir,
plugins_dir,
static,
install,
spatialite,
version_note,
{
"title": title,
"license": license,
"license_url": license_url,
"source": source,
"source_url": source_url,
"about": about,
"about_url": about_url,
},
):
image_id = f"gcr.io/{project}/{name}"

This comment has been minimized.

Copy link
@simonw

simonw Apr 18, 2019

Owner

Datasette still supports Python 3.5 so we can't use format strings just yet.

check_call(f"gcloud builds submit --tag {image_id}", shell=True)
check_call(f"gcloud beta run deploy --allow-unauthenticated --image {image_id}", shell=True)
@@ -265,24 +265,25 @@ def escape_sqlite(s):


def make_dockerfile(files, metadata_file, extra_options, branch, template_dir, plugins_dir, static, install, spatialite, version_note):
cmd = ['"datasette"', '"serve"', '"--host"', '"0.0.0.0"']
cmd.append('"' + '", "'.join(files) + '"')
cmd.extend(['"--cors"', '"--port"', '"8001"', '"--inspect-file"', '"inspect-data.json"'])
cmd = ['datasette', 'serve', '--host', '0.0.0.0']
cmd.append('", "'.join(files))
cmd.extend(['--cors', '--port', '$PORT', '--inspect-file', 'inspect-data.json'])
if metadata_file:
cmd.extend(['"--metadata"', '"{}"'.format(metadata_file)])
cmd.extend(['--metadata', '{}'.format(metadata_file)])
if template_dir:
cmd.extend(['"--template-dir"', '"templates/"'])
cmd.extend(['--template-dir', 'templates/'])
if plugins_dir:
cmd.extend(['"--plugins-dir"', '"plugins/"'])
cmd.extend(['--plugins-dir', 'plugins/'])
if version_note:
cmd.extend(['"--version-note"', '"{}"'.format(version_note)])
cmd.extend(['--version-note', '{}'.format(version_note)])
if static:
for mount_point, _ in static:
cmd.extend(['"--static"', '"{}:{}"'.format(mount_point, mount_point)])
cmd.extend(['--static', '{}:{}'.format(mount_point, mount_point)])
if extra_options:
for opt in extra_options.split():
cmd.append('"{}"'.format(opt))

cmd.append('{}'.format(opt))
cmd = ' '.join(cmd)
shell_command = f'"sh", "-c", "{cmd}"'
if branch:
install = ['https://github.com/simonw/datasette/archive/{}.zip'.format(
branch
@@ -297,10 +298,11 @@ def make_dockerfile(files, metadata_file, extra_options, branch, template_dir, p
{spatialite_extras}
RUN pip install -U {install_from}
RUN datasette inspect {files} --inspect-file inspect-data.json
ENV PORT 8001
EXPOSE 8001
CMD [{cmd}]'''.format(
files=' '.join(files),
cmd=', '.join(cmd),
cmd=shell_command,
install_from=' '.join(install),
spatialite_extras=SPATIALITE_DOCKERFILE_EXTRAS if spatialite else '',
).strip()
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.