mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
--cpu option for datasette publish cloudrun, closes #1420
This commit is contained in:
parent
cd8b7bee8f
commit
a1f3830356
4 changed files with 48 additions and 19 deletions
|
|
@ -36,6 +36,11 @@ def publish_subcommand(publish):
|
||||||
callback=_validate_memory,
|
callback=_validate_memory,
|
||||||
help="Memory to allocate in Cloud Run, e.g. 1Gi",
|
help="Memory to allocate in Cloud Run, e.g. 1Gi",
|
||||||
)
|
)
|
||||||
|
@click.option(
|
||||||
|
"--cpu",
|
||||||
|
type=click.Choice(["1", "2", "4"]),
|
||||||
|
help="Number of vCPUs to allocate in Cloud Run",
|
||||||
|
)
|
||||||
@click.option(
|
@click.option(
|
||||||
"--apt-get-install",
|
"--apt-get-install",
|
||||||
"apt_get_extras",
|
"apt_get_extras",
|
||||||
|
|
@ -66,6 +71,7 @@ def publish_subcommand(publish):
|
||||||
spatialite,
|
spatialite,
|
||||||
show_files,
|
show_files,
|
||||||
memory,
|
memory,
|
||||||
|
cpu,
|
||||||
apt_get_extras,
|
apt_get_extras,
|
||||||
):
|
):
|
||||||
fail_if_publish_binary_not_installed(
|
fail_if_publish_binary_not_installed(
|
||||||
|
|
@ -151,8 +157,11 @@ def publish_subcommand(publish):
|
||||||
image_id = f"gcr.io/{project}/{name}"
|
image_id = f"gcr.io/{project}/{name}"
|
||||||
check_call(f"gcloud builds submit --tag {image_id}", shell=True)
|
check_call(f"gcloud builds submit --tag {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, " --memory {}".format(memory) if memory else ""
|
image_id,
|
||||||
|
service,
|
||||||
|
" --memory {}".format(memory) if memory else "",
|
||||||
|
" --cpu {}".format(cpu) if cpu else "",
|
||||||
),
|
),
|
||||||
shell=True,
|
shell=True,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -28,5 +28,6 @@ Options:
|
||||||
--spatialite Enable SpatialLite extension
|
--spatialite Enable SpatialLite extension
|
||||||
--show-files Output the generated Dockerfile and metadata.json
|
--show-files Output the generated Dockerfile and metadata.json
|
||||||
--memory TEXT Memory to allocate in Cloud Run, e.g. 1Gi
|
--memory TEXT Memory to allocate in Cloud Run, e.g. 1Gi
|
||||||
|
--cpu [1|2|4] Number of vCPUs to allocate in Cloud Run
|
||||||
--apt-get-install TEXT Additional packages to apt-get install
|
--apt-get-install TEXT Additional packages to apt-get install
|
||||||
--help Show this message and exit.
|
--help Show this message and exit.
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ def test_help_includes(name, filename):
|
||||||
# actual has "Usage: cli package [OPTIONS] FILES"
|
# actual has "Usage: cli package [OPTIONS] FILES"
|
||||||
# because it doesn't know that cli will be aliased to datasette
|
# because it doesn't know that cli will be aliased to datasette
|
||||||
expected = expected.replace("Usage: datasette", "Usage: cli")
|
expected = expected.replace("Usage: datasette", "Usage: cli")
|
||||||
assert expected == actual
|
assert expected == actual, "Run python update-docs-help.py to fix this"
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
|
|
|
||||||
|
|
@ -105,17 +105,28 @@ 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_output")
|
||||||
@mock.patch("datasette.publish.cloudrun.check_call")
|
@mock.patch("datasette.publish.cloudrun.check_call")
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"memory,should_fail",
|
"memory,cpu,expected_gcloud_args",
|
||||||
[
|
[
|
||||||
["1Gi", False],
|
["1Gi", None, "--memory 1Gi"],
|
||||||
["2G", False],
|
["2G", None, "--memory 2G"],
|
||||||
["256Mi", False],
|
["256Mi", None, "--memory 256Mi"],
|
||||||
["4", True],
|
["4", None, None],
|
||||||
["GB", True],
|
["GB", None, None],
|
||||||
|
[None, 1, "--cpu 1"],
|
||||||
|
[None, 2, "--cpu 2"],
|
||||||
|
[None, 3, None],
|
||||||
|
[None, 4, "--cpu 4"],
|
||||||
|
["2G", 4, "--memory 2G --cpu 4"],
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_publish_cloudrun_memory(
|
def test_publish_cloudrun_memory_cpu(
|
||||||
mock_call, mock_output, mock_which, memory, should_fail, tmp_path_factory
|
mock_call,
|
||||||
|
mock_output,
|
||||||
|
mock_which,
|
||||||
|
memory,
|
||||||
|
cpu,
|
||||||
|
expected_gcloud_args,
|
||||||
|
tmp_path_factory,
|
||||||
):
|
):
|
||||||
mock_output.return_value = "myproject"
|
mock_output.return_value = "myproject"
|
||||||
mock_which.return_value = True
|
mock_which.return_value = True
|
||||||
|
|
@ -123,22 +134,30 @@ def test_publish_cloudrun_memory(
|
||||||
os.chdir(tmp_path_factory.mktemp("runner"))
|
os.chdir(tmp_path_factory.mktemp("runner"))
|
||||||
with open("test.db", "w") as fp:
|
with open("test.db", "w") as fp:
|
||||||
fp.write("data")
|
fp.write("data")
|
||||||
result = runner.invoke(
|
args = ["publish", "cloudrun", "test.db", "--service", "test"]
|
||||||
cli.cli,
|
if memory:
|
||||||
["publish", "cloudrun", "test.db", "--service", "test", "--memory", memory],
|
args.extend(["--memory", memory])
|
||||||
)
|
if cpu:
|
||||||
if should_fail:
|
args.extend(["--cpu", str(cpu)])
|
||||||
|
result = runner.invoke(cli.cli, args)
|
||||||
|
if expected_gcloud_args is None:
|
||||||
assert 2 == result.exit_code
|
assert 2 == result.exit_code
|
||||||
return
|
return
|
||||||
assert 0 == result.exit_code
|
assert 0 == result.exit_code
|
||||||
tag = f"gcr.io/{mock_output.return_value}/datasette"
|
tag = f"gcr.io/{mock_output.return_value}/datasette"
|
||||||
|
expected_call = (
|
||||||
|
"gcloud run deploy --allow-unauthenticated --platform=managed"
|
||||||
|
" --image {} test".format(tag)
|
||||||
|
)
|
||||||
|
if memory:
|
||||||
|
expected_call += " --memory {}".format(memory)
|
||||||
|
if cpu:
|
||||||
|
expected_call += " --cpu {}".format(cpu)
|
||||||
mock_call.assert_has_calls(
|
mock_call.assert_has_calls(
|
||||||
[
|
[
|
||||||
mock.call(f"gcloud builds submit --tag {tag}", shell=True),
|
mock.call(f"gcloud builds submit --tag {tag}", shell=True),
|
||||||
mock.call(
|
mock.call(
|
||||||
"gcloud run deploy --allow-unauthenticated --platform=managed --image {} test --memory {}".format(
|
expected_call,
|
||||||
tag, memory
|
|
||||||
),
|
|
||||||
shell=True,
|
shell=True,
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue