mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
--memory option for publish cloudrun, refs #694
This commit is contained in:
parent
be20e6991e
commit
ddd11b3ddd
2 changed files with 53 additions and 2 deletions
|
|
@ -2,6 +2,7 @@ from datasette import hookimpl
|
||||||
import click
|
import click
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
from subprocess import check_call, check_output
|
from subprocess import check_call, check_output
|
||||||
|
|
||||||
from .common import (
|
from .common import (
|
||||||
|
|
@ -30,6 +31,11 @@ def publish_subcommand(publish):
|
||||||
is_flag=True,
|
is_flag=True,
|
||||||
help="Output the generated Dockerfile and metadata.json",
|
help="Output the generated Dockerfile and metadata.json",
|
||||||
)
|
)
|
||||||
|
@click.option(
|
||||||
|
"--memory",
|
||||||
|
callback=_validate_memory,
|
||||||
|
help="Memory to allocate in Cloud Run, e.g. 1Gi",
|
||||||
|
)
|
||||||
def cloudrun(
|
def cloudrun(
|
||||||
files,
|
files,
|
||||||
metadata,
|
metadata,
|
||||||
|
|
@ -52,6 +58,7 @@ def publish_subcommand(publish):
|
||||||
service,
|
service,
|
||||||
spatialite,
|
spatialite,
|
||||||
show_files,
|
show_files,
|
||||||
|
memory,
|
||||||
):
|
):
|
||||||
fail_if_publish_binary_not_installed(
|
fail_if_publish_binary_not_installed(
|
||||||
"gcloud", "Google Cloud", "https://cloud.google.com/sdk/"
|
"gcloud", "Google Cloud", "https://cloud.google.com/sdk/"
|
||||||
|
|
@ -127,8 +134,8 @@ def publish_subcommand(publish):
|
||||||
image_id = "gcr.io/{project}/{name}".format(project=project, name=name)
|
image_id = "gcr.io/{project}/{name}".format(project=project, name=name)
|
||||||
check_call("gcloud builds submit --tag {}".format(image_id), shell=True)
|
check_call("gcloud builds submit --tag {}".format(image_id), shell=True)
|
||||||
check_call(
|
check_call(
|
||||||
"gcloud run deploy --allow-unauthenticated --platform=managed --image {} {}".format(
|
"gcloud run deploy --allow-unauthenticated --platform=managed --image {} {}{}".format(
|
||||||
image_id, service,
|
image_id, service, " --memory {}".format(memory) if memory else ""
|
||||||
),
|
),
|
||||||
shell=True,
|
shell=True,
|
||||||
)
|
)
|
||||||
|
|
@ -150,3 +157,9 @@ def get_existing_services():
|
||||||
}
|
}
|
||||||
for service in services
|
for service in services
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_memory(ctx, param, value):
|
||||||
|
if re.match(r"^\d+(Gi|G|Mi|M)$", value) is None:
|
||||||
|
raise click.BadParameter("--memory should be a number then Gi/G/Mi/M e.g 1Gi")
|
||||||
|
return value
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
from click.testing import CliRunner
|
from click.testing import CliRunner
|
||||||
from datasette import cli
|
from datasette import cli
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
import pytest
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -98,6 +99,43 @@ def test_publish_cloudrun(mock_call, mock_output, mock_which):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch("shutil.which")
|
||||||
|
@mock.patch("datasette.publish.cloudrun.check_output")
|
||||||
|
@mock.patch("datasette.publish.cloudrun.check_call")
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"memory,should_fail",
|
||||||
|
[["1Gi", False], ["2G", False], ["256Mi", False], ["4", True], ["GB", True],],
|
||||||
|
)
|
||||||
|
def test_publish_cloudrun_memory(
|
||||||
|
mock_call, mock_output, mock_which, memory, should_fail
|
||||||
|
):
|
||||||
|
mock_output.return_value = "myproject"
|
||||||
|
mock_which.return_value = True
|
||||||
|
runner = CliRunner()
|
||||||
|
with runner.isolated_filesystem():
|
||||||
|
open("test.db", "w").write("data")
|
||||||
|
result = runner.invoke(
|
||||||
|
cli.cli,
|
||||||
|
["publish", "cloudrun", "test.db", "--service", "test", "--memory", memory],
|
||||||
|
)
|
||||||
|
if should_fail:
|
||||||
|
assert 2 == result.exit_code
|
||||||
|
return
|
||||||
|
assert 0 == result.exit_code
|
||||||
|
tag = "gcr.io/{}/datasette".format(mock_output.return_value)
|
||||||
|
mock_call.assert_has_calls(
|
||||||
|
[
|
||||||
|
mock.call("gcloud builds submit --tag {}".format(tag), shell=True),
|
||||||
|
mock.call(
|
||||||
|
"gcloud run deploy --allow-unauthenticated --platform=managed --image {} test --memory {}".format(
|
||||||
|
tag, memory
|
||||||
|
),
|
||||||
|
shell=True,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@mock.patch("shutil.which")
|
@mock.patch("shutil.which")
|
||||||
@mock.patch("datasette.publish.cloudrun.check_output")
|
@mock.patch("datasette.publish.cloudrun.check_output")
|
||||||
@mock.patch("datasette.publish.cloudrun.check_call")
|
@mock.patch("datasette.publish.cloudrun.check_call")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue