kopia lustrzana https://github.com/simonw/datasette
Improved UI for publish cloudrun, closes #608
rodzic
f9c146b893
commit
83fc5165ac
|
@ -60,6 +60,23 @@ def publish_subcommand(publish):
|
||||||
"gcloud config get-value project", shell=True, universal_newlines=True
|
"gcloud config get-value project", shell=True, universal_newlines=True
|
||||||
).strip()
|
).strip()
|
||||||
|
|
||||||
|
if not service:
|
||||||
|
# Show the user their current services, then prompt for one
|
||||||
|
click.echo("Please provide a service name for this deployment\n")
|
||||||
|
click.echo("Using an existing service name will over-write it")
|
||||||
|
click.echo("")
|
||||||
|
existing_services = get_existing_services()
|
||||||
|
if existing_services:
|
||||||
|
click.echo("Your existing services:\n")
|
||||||
|
for existing_service in existing_services:
|
||||||
|
click.echo(
|
||||||
|
" {name} - created {created} - {url}".format(
|
||||||
|
**existing_service
|
||||||
|
)
|
||||||
|
)
|
||||||
|
click.echo("")
|
||||||
|
service = click.prompt("Service name", type=str)
|
||||||
|
|
||||||
extra_metadata = {
|
extra_metadata = {
|
||||||
"title": title,
|
"title": title,
|
||||||
"license": license,
|
"license": license,
|
||||||
|
@ -110,8 +127,26 @@ 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 beta run deploy --allow-unauthenticated --platform=managed --image {}{}".format(
|
"gcloud beta run deploy --allow-unauthenticated --platform=managed --image {} {}".format(
|
||||||
image_id, " {}".format(service) if service else ""
|
image_id, service,
|
||||||
),
|
),
|
||||||
shell=True,
|
shell=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_existing_services():
|
||||||
|
services = json.loads(
|
||||||
|
check_output(
|
||||||
|
"gcloud beta run services list --platform=managed --format json",
|
||||||
|
shell=True,
|
||||||
|
universal_newlines=True,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
"name": service["metadata"]["name"],
|
||||||
|
"created": service["metadata"]["creationTimestamp"],
|
||||||
|
"url": service["status"]["address"]["url"],
|
||||||
|
}
|
||||||
|
for service in services
|
||||||
|
]
|
||||||
|
|
|
@ -24,6 +24,53 @@ def test_publish_cloudrun_invalid_database(mock_which):
|
||||||
assert 'Path "woop.db" does not exist' in result.output
|
assert 'Path "woop.db" does not exist' in result.output
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch("shutil.which")
|
||||||
|
@mock.patch("datasette.publish.cloudrun.check_output")
|
||||||
|
@mock.patch("datasette.publish.cloudrun.check_call")
|
||||||
|
@mock.patch("datasette.publish.cloudrun.get_existing_services")
|
||||||
|
def test_publish_cloudrun_prompts_for_service(
|
||||||
|
mock_get_existing_services, mock_call, mock_output, mock_which
|
||||||
|
):
|
||||||
|
mock_get_existing_services.return_value = [
|
||||||
|
{"name": "existing", "created": "2019-01-01", "url": "http://www.example.com/"}
|
||||||
|
]
|
||||||
|
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"], input="input-service"
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
"""
|
||||||
|
Please provide a service name for this deployment
|
||||||
|
|
||||||
|
Using an existing service name will over-write it
|
||||||
|
|
||||||
|
Your existing services:
|
||||||
|
|
||||||
|
existing - created 2019-01-01 - http://www.example.com/
|
||||||
|
|
||||||
|
Service name: input-service
|
||||||
|
""".strip()
|
||||||
|
== result.output.strip()
|
||||||
|
)
|
||||||
|
assert 0 == result.exit_code
|
||||||
|
tag = "gcr.io/myproject/datasette"
|
||||||
|
mock_call.assert_has_calls(
|
||||||
|
[
|
||||||
|
mock.call("gcloud builds submit --tag {}".format(tag), shell=True),
|
||||||
|
mock.call(
|
||||||
|
"gcloud beta run deploy --allow-unauthenticated --platform=managed --image {} input-service".format(
|
||||||
|
tag
|
||||||
|
),
|
||||||
|
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")
|
||||||
|
@ -33,14 +80,16 @@ def test_publish_cloudrun(mock_call, mock_output, mock_which):
|
||||||
runner = CliRunner()
|
runner = CliRunner()
|
||||||
with runner.isolated_filesystem():
|
with runner.isolated_filesystem():
|
||||||
open("test.db", "w").write("data")
|
open("test.db", "w").write("data")
|
||||||
result = runner.invoke(cli.cli, ["publish", "cloudrun", "test.db"])
|
result = runner.invoke(
|
||||||
|
cli.cli, ["publish", "cloudrun", "test.db", "--service", "test"]
|
||||||
|
)
|
||||||
assert 0 == result.exit_code
|
assert 0 == result.exit_code
|
||||||
tag = "gcr.io/{}/datasette".format(mock_output.return_value)
|
tag = "gcr.io/{}/datasette".format(mock_output.return_value)
|
||||||
mock_call.assert_has_calls(
|
mock_call.assert_has_calls(
|
||||||
[
|
[
|
||||||
mock.call("gcloud builds submit --tag {}".format(tag), shell=True),
|
mock.call("gcloud builds submit --tag {}".format(tag), shell=True),
|
||||||
mock.call(
|
mock.call(
|
||||||
"gcloud beta run deploy --allow-unauthenticated --platform=managed --image {}".format(
|
"gcloud beta run deploy --allow-unauthenticated --platform=managed --image {} test".format(
|
||||||
tag
|
tag
|
||||||
),
|
),
|
||||||
shell=True,
|
shell=True,
|
||||||
|
@ -65,6 +114,8 @@ def test_publish_cloudrun_plugin_secrets(mock_call, mock_output, mock_which):
|
||||||
"publish",
|
"publish",
|
||||||
"cloudrun",
|
"cloudrun",
|
||||||
"test.db",
|
"test.db",
|
||||||
|
"--service",
|
||||||
|
"datasette",
|
||||||
"--plugin-secret",
|
"--plugin-secret",
|
||||||
"datasette-auth-github",
|
"datasette-auth-github",
|
||||||
"client_id",
|
"client_id",
|
||||||
|
|
Ładowanie…
Reference in New Issue