mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
--plugin-secret option for datasette publish
Closes #543 Also added new --show-files option to publish now and publish cloudrun - handy for debugging.
This commit is contained in:
parent
2d04986c44
commit
973f8f139d
13 changed files with 381 additions and 86 deletions
|
|
@ -1,6 +1,7 @@
|
|||
from datasette import hookimpl
|
||||
import click
|
||||
import json
|
||||
import os
|
||||
from subprocess import check_call, check_output
|
||||
|
||||
from .common import (
|
||||
|
|
@ -24,6 +25,11 @@ def publish_subcommand(publish):
|
|||
"--service", default="", help="Cloud Run service to deploy (or over-write)"
|
||||
)
|
||||
@click.option("--spatialite", is_flag=True, help="Enable SpatialLite extension")
|
||||
@click.option(
|
||||
"--show-files",
|
||||
is_flag=True,
|
||||
help="Output the generated Dockerfile and metadata.json",
|
||||
)
|
||||
def cloudrun(
|
||||
files,
|
||||
metadata,
|
||||
|
|
@ -33,6 +39,7 @@ def publish_subcommand(publish):
|
|||
plugins_dir,
|
||||
static,
|
||||
install,
|
||||
plugin_secret,
|
||||
version_note,
|
||||
title,
|
||||
license,
|
||||
|
|
@ -44,6 +51,7 @@ def publish_subcommand(publish):
|
|||
name,
|
||||
service,
|
||||
spatialite,
|
||||
show_files,
|
||||
):
|
||||
fail_if_publish_binary_not_installed(
|
||||
"gcloud", "Google Cloud", "https://cloud.google.com/sdk/"
|
||||
|
|
@ -52,6 +60,30 @@ def publish_subcommand(publish):
|
|||
"gcloud config get-value project", shell=True, universal_newlines=True
|
||||
).strip()
|
||||
|
||||
extra_metadata = {
|
||||
"title": title,
|
||||
"license": license,
|
||||
"license_url": license_url,
|
||||
"source": source,
|
||||
"source_url": source_url,
|
||||
"about": about,
|
||||
"about_url": about_url,
|
||||
}
|
||||
|
||||
environment_variables = {}
|
||||
if plugin_secret:
|
||||
extra_metadata["plugins"] = {}
|
||||
for plugin_name, plugin_setting, setting_value in plugin_secret:
|
||||
environment_variable = (
|
||||
"{}_{}".format(plugin_name, plugin_setting)
|
||||
.upper()
|
||||
.replace("-", "_")
|
||||
)
|
||||
environment_variables[environment_variable] = setting_value
|
||||
extra_metadata["plugins"].setdefault(plugin_name, {})[
|
||||
plugin_setting
|
||||
] = {"$env": environment_variable}
|
||||
|
||||
with temporary_docker_directory(
|
||||
files,
|
||||
name,
|
||||
|
|
@ -64,16 +96,17 @@ def publish_subcommand(publish):
|
|||
install,
|
||||
spatialite,
|
||||
version_note,
|
||||
{
|
||||
"title": title,
|
||||
"license": license,
|
||||
"license_url": license_url,
|
||||
"source": source,
|
||||
"source_url": source_url,
|
||||
"about": about,
|
||||
"about_url": about_url,
|
||||
},
|
||||
extra_metadata,
|
||||
environment_variables,
|
||||
):
|
||||
if show_files:
|
||||
if os.path.exists("metadata.json"):
|
||||
print("=== metadata.json ===\n")
|
||||
print(open("metadata.json").read())
|
||||
print("\n==== Dockerfile ====\n")
|
||||
print(open("Dockerfile").read())
|
||||
print("\n====================\n")
|
||||
|
||||
image_id = "gcr.io/{project}/{name}".format(project=project, name=name)
|
||||
check_call("gcloud builds submit --tag {}".format(image_id), shell=True)
|
||||
check_call(
|
||||
|
|
|
|||
|
|
@ -41,6 +41,14 @@ def add_common_publish_arguments_and_options(subcommand):
|
|||
help="Additional packages (e.g. plugins) to install",
|
||||
multiple=True,
|
||||
),
|
||||
click.option(
|
||||
"--plugin-secret",
|
||||
nargs=3,
|
||||
type=(str, str, str),
|
||||
callback=validate_plugin_secret,
|
||||
multiple=True,
|
||||
help="Secrets to pass to plugins, e.g. --plugin-secret datasette-auth-github client_id xxx",
|
||||
),
|
||||
click.option(
|
||||
"--version-note", help="Additional note to show on /-/versions"
|
||||
),
|
||||
|
|
@ -76,3 +84,10 @@ def fail_if_publish_binary_not_installed(binary, publish_target, install_link):
|
|||
err=True,
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def validate_plugin_secret(ctx, param, value):
|
||||
for plugin_name, plugin_setting, setting_value in value:
|
||||
if "'" in setting_value:
|
||||
raise click.BadParameter("--plugin-secret cannot contain single quotes")
|
||||
return value
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ def publish_subcommand(publish):
|
|||
plugins_dir,
|
||||
static,
|
||||
install,
|
||||
plugin_secret,
|
||||
version_note,
|
||||
title,
|
||||
license,
|
||||
|
|
@ -61,6 +62,30 @@ def publish_subcommand(publish):
|
|||
)
|
||||
call(["heroku", "plugins:install", "heroku-builds"])
|
||||
|
||||
extra_metadata = {
|
||||
"title": title,
|
||||
"license": license,
|
||||
"license_url": license_url,
|
||||
"source": source,
|
||||
"source_url": source_url,
|
||||
"about": about,
|
||||
"about_url": about_url,
|
||||
}
|
||||
|
||||
environment_variables = {}
|
||||
if plugin_secret:
|
||||
extra_metadata["plugins"] = {}
|
||||
for plugin_name, plugin_setting, setting_value in plugin_secret:
|
||||
environment_variable = (
|
||||
"{}_{}".format(plugin_name, plugin_setting)
|
||||
.upper()
|
||||
.replace("-", "_")
|
||||
)
|
||||
environment_variables[environment_variable] = setting_value
|
||||
extra_metadata["plugins"].setdefault(plugin_name, {})[
|
||||
plugin_setting
|
||||
] = {"$env": environment_variable}
|
||||
|
||||
with temporary_heroku_directory(
|
||||
files,
|
||||
name,
|
||||
|
|
@ -72,15 +97,7 @@ def publish_subcommand(publish):
|
|||
static,
|
||||
install,
|
||||
version_note,
|
||||
{
|
||||
"title": title,
|
||||
"license": license,
|
||||
"license_url": license_url,
|
||||
"source": source,
|
||||
"source_url": source_url,
|
||||
"about": about,
|
||||
"about_url": about_url,
|
||||
},
|
||||
extra_metadata,
|
||||
):
|
||||
app_name = None
|
||||
if name:
|
||||
|
|
@ -104,6 +121,11 @@ def publish_subcommand(publish):
|
|||
create_output = check_output(cmd).decode("utf8")
|
||||
app_name = json.loads(create_output)["name"]
|
||||
|
||||
for key, value in environment_variables.items():
|
||||
call(
|
||||
["heroku", "config:set", "-a", app_name, "{}={}".format(key, value)]
|
||||
)
|
||||
|
||||
call(["heroku", "builds:create", "-a", app_name, "--include-vcs-ignore"])
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
from datasette import hookimpl
|
||||
import click
|
||||
import json
|
||||
import os
|
||||
from subprocess import run, PIPE
|
||||
|
||||
from .common import (
|
||||
|
|
@ -24,6 +25,11 @@ def publish_subcommand(publish):
|
|||
@click.option("--token", help="Auth token to use for deploy")
|
||||
@click.option("--alias", multiple=True, help="Desired alias e.g. yoursite.now.sh")
|
||||
@click.option("--spatialite", is_flag=True, help="Enable SpatialLite extension")
|
||||
@click.option(
|
||||
"--show-files",
|
||||
is_flag=True,
|
||||
help="Output the generated Dockerfile and metadata.json",
|
||||
)
|
||||
def nowv1(
|
||||
files,
|
||||
metadata,
|
||||
|
|
@ -33,6 +39,7 @@ def publish_subcommand(publish):
|
|||
plugins_dir,
|
||||
static,
|
||||
install,
|
||||
plugin_secret,
|
||||
version_note,
|
||||
title,
|
||||
license,
|
||||
|
|
@ -46,6 +53,7 @@ def publish_subcommand(publish):
|
|||
token,
|
||||
alias,
|
||||
spatialite,
|
||||
show_files,
|
||||
):
|
||||
fail_if_publish_binary_not_installed("now", "Zeit Now", "https://zeit.co/now")
|
||||
if extra_options:
|
||||
|
|
@ -54,6 +62,30 @@ def publish_subcommand(publish):
|
|||
extra_options = ""
|
||||
extra_options += "--config force_https_urls:on"
|
||||
|
||||
extra_metadata = {
|
||||
"title": title,
|
||||
"license": license,
|
||||
"license_url": license_url,
|
||||
"source": source,
|
||||
"source_url": source_url,
|
||||
"about": about,
|
||||
"about_url": about_url,
|
||||
}
|
||||
|
||||
environment_variables = {}
|
||||
if plugin_secret:
|
||||
extra_metadata["plugins"] = {}
|
||||
for plugin_name, plugin_setting, setting_value in plugin_secret:
|
||||
environment_variable = (
|
||||
"{}_{}".format(plugin_name, plugin_setting)
|
||||
.upper()
|
||||
.replace("-", "_")
|
||||
)
|
||||
environment_variables[environment_variable] = setting_value
|
||||
extra_metadata["plugins"].setdefault(plugin_name, {})[
|
||||
plugin_setting
|
||||
] = {"$env": environment_variable}
|
||||
|
||||
with temporary_docker_directory(
|
||||
files,
|
||||
name,
|
||||
|
|
@ -66,15 +98,8 @@ def publish_subcommand(publish):
|
|||
install,
|
||||
spatialite,
|
||||
version_note,
|
||||
{
|
||||
"title": title,
|
||||
"license": license,
|
||||
"license_url": license_url,
|
||||
"source": source,
|
||||
"source_url": source_url,
|
||||
"about": about,
|
||||
"about_url": about_url,
|
||||
},
|
||||
extra_metadata,
|
||||
environment_variables,
|
||||
):
|
||||
now_json = {"version": 1}
|
||||
open("now.json", "w").write(json.dumps(now_json, indent=4))
|
||||
|
|
@ -88,6 +113,13 @@ def publish_subcommand(publish):
|
|||
else:
|
||||
done = run("now", stdout=PIPE)
|
||||
deployment_url = done.stdout
|
||||
if show_files:
|
||||
if os.path.exists("metadata.json"):
|
||||
print("=== metadata.json ===\n")
|
||||
print(open("metadata.json").read())
|
||||
print("\n==== Dockerfile ====\n")
|
||||
print(open("Dockerfile").read())
|
||||
print("\n====================\n")
|
||||
if alias:
|
||||
# I couldn't get --target=production working, so I call
|
||||
# 'now alias' with arguments directly instead - but that
|
||||
|
|
|
|||
|
|
@ -272,6 +272,7 @@ def make_dockerfile(
|
|||
install,
|
||||
spatialite,
|
||||
version_note,
|
||||
environment_variables=None,
|
||||
):
|
||||
cmd = ["datasette", "serve", "--host", "0.0.0.0"]
|
||||
for filename in files:
|
||||
|
|
@ -307,11 +308,18 @@ FROM python:3.6
|
|||
COPY . /app
|
||||
WORKDIR /app
|
||||
{spatialite_extras}
|
||||
{environment_variables}
|
||||
RUN pip install -U {install_from}
|
||||
RUN datasette inspect {files} --inspect-file inspect-data.json
|
||||
ENV PORT 8001
|
||||
EXPOSE 8001
|
||||
CMD {cmd}""".format(
|
||||
environment_variables="\n".join(
|
||||
[
|
||||
"ENV {} '{}'".format(key, value)
|
||||
for key, value in (environment_variables or {}).items()
|
||||
]
|
||||
),
|
||||
files=" ".join(files),
|
||||
cmd=cmd,
|
||||
install_from=" ".join(install),
|
||||
|
|
@ -333,6 +341,7 @@ def temporary_docker_directory(
|
|||
spatialite,
|
||||
version_note,
|
||||
extra_metadata=None,
|
||||
environment_variables=None,
|
||||
):
|
||||
extra_metadata = extra_metadata or {}
|
||||
tmp = tempfile.TemporaryDirectory()
|
||||
|
|
@ -361,6 +370,7 @@ def temporary_docker_directory(
|
|||
install,
|
||||
spatialite,
|
||||
version_note,
|
||||
environment_variables,
|
||||
)
|
||||
os.chdir(datasette_dir)
|
||||
if metadata_content:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue