--min-instances and --max-instances Cloud Run publish options, closes #1779

This commit is contained in:
Simon Willison 2022-08-14 10:07:30 -07:00
commit 82167105ee
3 changed files with 56 additions and 15 deletions

View file

@ -52,6 +52,16 @@ def publish_subcommand(publish):
multiple=True,
help="Additional packages to apt-get install",
)
@click.option(
"--max-instances",
type=int,
help="Maximum Cloud Run instances",
)
@click.option(
"--min-instances",
type=int,
help="Minimum Cloud Run instances",
)
def cloudrun(
files,
metadata,
@ -79,6 +89,8 @@ def publish_subcommand(publish):
cpu,
timeout,
apt_get_extras,
max_instances,
min_instances,
):
"Publish databases to Datasette running on Cloud Run"
fail_if_publish_binary_not_installed(
@ -168,12 +180,20 @@ def publish_subcommand(publish):
),
shell=True,
)
extra_deploy_options = []
for option, value in (
("--memory", memory),
("--cpu", cpu),
("--max-instances", max_instances),
("--min-instances", min_instances),
):
if value:
extra_deploy_options.append("{} {}".format(option, value))
check_call(
"gcloud run deploy --allow-unauthenticated --platform=managed --image {} {}{}{}".format(
"gcloud run deploy --allow-unauthenticated --platform=managed --image {} {}{}".format(
image_id,
service,
" --memory {}".format(memory) if memory else "",
" --cpu {}".format(cpu) if cpu else "",
" " + " ".join(extra_deploy_options) if extra_deploy_options else "",
),
shell=True,
)

View file

@ -251,6 +251,8 @@ datasette publish cloudrun --help
--cpu [1|2|4] Number of vCPUs to allocate in Cloud Run
--timeout INTEGER Build timeout in seconds
--apt-get-install TEXT Additional packages to apt-get install
--max-instances INTEGER Maximum Cloud Run instances
--min-instances INTEGER Minimum Cloud Run instances
--help Show this message and exit.

View file

@ -105,19 +105,36 @@ def test_publish_cloudrun(mock_call, mock_output, mock_which, tmp_path_factory):
@mock.patch("datasette.publish.cloudrun.check_output")
@mock.patch("datasette.publish.cloudrun.check_call")
@pytest.mark.parametrize(
"memory,cpu,timeout,expected_gcloud_args",
"memory,cpu,timeout,min_instances,max_instances,expected_gcloud_args",
[
["1Gi", None, None, "--memory 1Gi"],
["2G", None, None, "--memory 2G"],
["256Mi", None, None, "--memory 256Mi"],
["4", None, None, None],
["GB", None, None, None],
[None, 1, None, "--cpu 1"],
[None, 2, None, "--cpu 2"],
[None, 3, None, None],
[None, 4, None, "--cpu 4"],
["2G", 4, None, "--memory 2G --cpu 4"],
[None, None, 1800, "--timeout 1800"],
["1Gi", None, None, None, None, "--memory 1Gi"],
["2G", None, None, None, None, "--memory 2G"],
["256Mi", None, None, None, None, "--memory 256Mi"],
[
"4",
None,
None,
None,
None,
None,
],
[
"GB",
None,
None,
None,
None,
None,
],
[None, 1, None, None, None, "--cpu 1"],
[None, 2, None, None, None, "--cpu 2"],
[None, 3, None, None, None, None],
[None, 4, None, None, None, "--cpu 4"],
["2G", 4, None, None, None, "--memory 2G --cpu 4"],
[None, None, 1800, None, None, "--timeout 1800"],
[None, None, None, 2, None, "--min-instances 2"],
[None, None, None, 2, 4, "--min-instances 2 --max-instances 4"],
[None, 2, None, None, 4, "--cpu 2 --max-instances 4"],
],
)
def test_publish_cloudrun_memory_cpu(
@ -127,6 +144,8 @@ def test_publish_cloudrun_memory_cpu(
memory,
cpu,
timeout,
min_instances,
max_instances,
expected_gcloud_args,
tmp_path_factory,
):