Skip to content

Commit

Permalink
--cpu option for datasette publish cloudrun, closes #1420
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Aug 4, 2021
1 parent cd8b7be commit a1f3830
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
13 changes: 11 additions & 2 deletions datasette/publish/cloudrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ def publish_subcommand(publish):
callback=_validate_memory,
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(
"--apt-get-install",
"apt_get_extras",
Expand Down Expand Up @@ -66,6 +71,7 @@ def cloudrun(
spatialite,
show_files,
memory,
cpu,
apt_get_extras,
):
fail_if_publish_binary_not_installed(
Expand Down Expand Up @@ -151,8 +157,11 @@ def cloudrun(
image_id = f"gcr.io/{project}/{name}"
check_call(f"gcloud builds submit --tag {image_id}", shell=True)
check_call(
"gcloud run deploy --allow-unauthenticated --platform=managed --image {} {}{}".format(
image_id, service, " --memory {}".format(memory) if memory else ""
"gcloud run deploy --allow-unauthenticated --platform=managed --image {} {}{}{}".format(
image_id,
service,
" --memory {}".format(memory) if memory else "",
" --cpu {}".format(cpu) if cpu else "",
),
shell=True,
)
Expand Down
1 change: 1 addition & 0 deletions docs/datasette-publish-cloudrun-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ Options:
--spatialite Enable SpatialLite extension
--show-files Output the generated Dockerfile and metadata.json
--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
--help Show this message and exit.
2 changes: 1 addition & 1 deletion tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_help_includes(name, filename):
# actual has "Usage: cli package [OPTIONS] FILES"
# because it doesn't know that cli will be aliased to datasette
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")
Expand Down
51 changes: 35 additions & 16 deletions tests/test_publish_cloudrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,40 +105,59 @@ 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,should_fail",
"memory,cpu,expected_gcloud_args",
[
["1Gi", False],
["2G", False],
["256Mi", False],
["4", True],
["GB", True],
["1Gi", None, "--memory 1Gi"],
["2G", None, "--memory 2G"],
["256Mi", None, "--memory 256Mi"],
["4", None, None],
["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(
mock_call, mock_output, mock_which, memory, should_fail, tmp_path_factory
def test_publish_cloudrun_memory_cpu(
mock_call,
mock_output,
mock_which,
memory,
cpu,
expected_gcloud_args,
tmp_path_factory,
):
mock_output.return_value = "myproject"
mock_which.return_value = True
runner = CliRunner()
os.chdir(tmp_path_factory.mktemp("runner"))
with open("test.db", "w") as fp:
fp.write("data")
result = runner.invoke(
cli.cli,
["publish", "cloudrun", "test.db", "--service", "test", "--memory", memory],
)
if should_fail:
args = ["publish", "cloudrun", "test.db", "--service", "test"]
if memory:
args.extend(["--memory", memory])
if cpu:
args.extend(["--cpu", str(cpu)])
result = runner.invoke(cli.cli, args)
if expected_gcloud_args is None:
assert 2 == result.exit_code
return
assert 0 == result.exit_code
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(f"gcloud builds submit --tag {tag}", shell=True),
mock.call(
"gcloud run deploy --allow-unauthenticated --platform=managed --image {} test --memory {}".format(
tag, memory
),
expected_call,
shell=True,
),
]
Expand Down

0 comments on commit a1f3830

Please sign in to comment.