feat: Add new gcloud commands, API clients, and third-party libraries across various services.

This commit is contained in:
2026-01-01 20:26:35 +01:00
parent 5e23cbece0
commit a19e592eb7
25221 changed files with 8324611 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*- #
# Copyright 2015 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""The gcloud app versions group."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
@base.ReleaseTracks(base.ReleaseTrack.GA, base.ReleaseTrack.BETA)
class Versions(base.Group):
"""View and manage your App Engine versions.
This set of commands can be used to view and manage your existing App Engine
versions.
To create new deployments, use `{parent_command} deploy`.
For more information on App Engine versions, see:
https://cloud.google.com/appengine/docs/python/an-overview-of-app-engine
## EXAMPLES
To list your deployed versions, run:
$ {command} list
"""
category = base.APP_ENGINE_CATEGORY

View File

@@ -0,0 +1,83 @@
# -*- coding: utf-8 -*- #
# Copyright 2015 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""The Browse command."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.calliope import base
from googlecloudsdk.command_lib.app import browser_dispatcher
from googlecloudsdk.command_lib.app import flags
from googlecloudsdk.core import log
from googlecloudsdk.core import properties
class Browse(base.Command):
"""Open the specified versions in a browser."""
detailed_help = {
'EXAMPLES': """\
To show version `v1` for the default service in the browser, run:
$ {command} v1
To show version `v1` of a specific service in the browser, run:
$ {command} v1 --service="myService"
To show multiple versions side-by-side, run:
$ {command} v1 v2 --service="myService"
""",
}
@staticmethod
def Args(parser):
parser.display_info.AddFormat("""
table(
version:label=VERSION,
url:label=URL
)
""")
flags.LAUNCH_BROWSER.AddToParser(parser)
parser.add_argument(
'versions',
nargs='+',
help="""\
The versions to open (optionally filtered by the --service flag).""")
parser.add_argument('--service', '-s',
help=('If specified, only open versions from the '
'given service. If not specified, use the '
'default service.'))
def Run(self, args):
"""Launch a browser, or return a table of URLs to print."""
project = properties.VALUES.core.project.Get(required=True)
outputs = []
for version in args.versions:
outputs.append(browser_dispatcher.BrowseApp(project,
args.service,
version,
args.launch_browser))
if any(outputs):
if args.launch_browser:
# We couldn't find a browser to launch
log.status.Print(
'Did not detect your browser. Go to these links to view your app:')
return outputs
return None

View File

@@ -0,0 +1,139 @@
# -*- coding: utf-8 -*- #
# Copyright 2015 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""The Delete command."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import copy
from googlecloudsdk.api_lib.app import appengine_api_client
from googlecloudsdk.api_lib.app import service_util
from googlecloudsdk.api_lib.app import version_util
from googlecloudsdk.calliope import base
from googlecloudsdk.core import exceptions
from googlecloudsdk.core import log
from googlecloudsdk.core.console import console_io
from googlecloudsdk.core.resource import resource_printer
from googlecloudsdk.core.util import text
class VersionsDeleteError(exceptions.Error):
"""Errors occurring when deleting versions."""
pass
class Delete(base.DeleteCommand):
"""Delete a specified version.
You cannot delete a version of a service that is currently receiving traffic.
"""
detailed_help = {
'EXAMPLES': """\
To delete a specific version of a specific service, run:
$ {command} --service=myService v1
To delete a named version across all services, run:
$ {command} v1
To delete multiple versions of a specific service, run:
$ {command} --service=myService v1 v2
To delete multiple named versions across all services, run:
$ {command} v1 v2
""",
}
@staticmethod
def Args(parser):
parser.add_argument('versions', nargs='+', help=(
'The versions to delete (optionally filtered by the --service flag).'))
parser.add_argument('--service', '-s',
help=('If specified, only delete versions from the '
'given service.'))
def Run(self, args):
client = appengine_api_client.GetApiClientForTrack(self.ReleaseTrack())
services = client.ListServices()
# If a service is supplied, only list versions for that service
if args.service:
services = [s for s in services if s.id == args.service]
all_versions = client.ListVersions(services)
# Sort versions to make behavior deterministic enough for unit testing.
versions = sorted(version_util.GetMatchingVersions(all_versions,
args.versions,
args.service), key=str)
services_to_delete = []
for service in sorted(services):
service_versions = len(
[v for v in all_versions if v.service == service.id])
versions_to_delete = len([v for v in versions if v.service == service.id])
if service_versions == versions_to_delete and service_versions > 0:
if service.id == 'default':
raise VersionsDeleteError(
'The default service (module) may not be deleted, and must '
'comprise at least one version.'
)
else:
services_to_delete.append(service)
for version in copy.copy(versions):
if version.service == service.id:
versions.remove(version)
for version in versions:
if version.traffic_split:
# TODO(b/32869800): collect info on all versions before raising.
raise VersionsDeleteError(
'Version [{version}] is currently serving {allocation:.2f}% of '
'traffic for service [{service}].\n\n'
'Please move all traffic away via one of the following methods:\n'
' - deploying a new version with the `--promote` argument\n'
' - running `gcloud app services set-traffic`\n'
' - running `gcloud app versions migrate`'.format(
version=version.id,
allocation=version.traffic_split * 100,
service=version.service))
if services_to_delete:
word = text.Pluralize(len(services_to_delete), 'service')
log.warning(
'Requested deletion of all existing versions for the following {0}:'
.format(word))
resource_printer.Print(services_to_delete, 'list', out=log.status)
console_io.PromptContinue(prompt_string=(
'\nYou cannot delete all versions of a service. Would you like to '
'delete the entire {0} instead?').format(word), cancel_on_no=True)
service_util.DeleteServices(client, services_to_delete)
if versions:
fmt = 'list[title="Deleting the following versions:"]'
resource_printer.Print(versions, fmt, out=log.status)
console_io.PromptContinue(cancel_on_no=True)
else:
if not services_to_delete:
log.warning('No matching versions found.')
version_util.DeleteVersions(client, versions)

View File

@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*- #
# Copyright 2015 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""`gcloud app versions describe` command."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.app import appengine_api_client
from googlecloudsdk.calliope import base
class Describe(base.DescribeCommand):
"""Display all data about an existing version."""
@staticmethod
def Args(parser):
parser.add_argument(
'--service', '-s', required=True,
help='The service corresponding to the version to show.')
parser.add_argument('version', help='The ID of the version to show.')
def Run(self, args):
api_client = appengine_api_client.GetApiClientForTrack(self.ReleaseTrack())
return api_client.GetVersionResource(service=args.service,
version=args.version)

View File

@@ -0,0 +1,103 @@
# -*- coding: utf-8 -*- #
# Copyright 2015 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""`gcloud app versions list` command."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.app import appengine_api_client
from googlecloudsdk.api_lib.app import version_util
from googlecloudsdk.calliope import base
from googlecloudsdk.core import exceptions
from googlecloudsdk.core import log
class ServiceNotFoundError(exceptions.Error):
pass
class List(base.ListCommand):
"""List your existing versions.
This command lists all the versions of all services that are currently
deployed to the App Engine server.
"""
detailed_help = {
'EXAMPLES': """\
To list all services and versions, run:
$ {command}
To list all versions for a specific service, run:
$ {command} --service=service1
To list only versions that are receiving traffic, run:
$ {command} --hide-no-traffic
To list all version information in JSON, run:
$ {command} --format="json"
To list versions created after a specific date, run:
$ {command} --filter="version.createTime.date('%Y-%m-%d', Z)>'2017-11-03'"
""",
}
@staticmethod
def Args(parser):
parser.add_argument('--service', '-s',
help='Only show versions from this service.')
parser.add_argument('--hide-no-traffic', action='store_true',
help='Only show versions that are receiving traffic.')
parser.display_info.AddFormat("""
table(
service,
version.id:label=VERSION.ID,
traffic_split.format("{0:.2f}", .),
last_deployed_time.date("%Y-%m-%dT%H:%M:%S%Oz", undefined="-")
:label=LAST_DEPLOYED,
version.servingStatus:label=SERVING_STATUS
)
""")
parser.display_info.AddUriFunc(version_util.GetUri)
def Run(self, args):
api_client = appengine_api_client.GetApiClientForTrack(self.ReleaseTrack())
services = api_client.ListServices()
service_ids = [s.id for s in services]
log.debug('All services: {0}'.format(service_ids))
if args.service and args.service not in service_ids:
raise ServiceNotFoundError(
'Service [{0}] not found.'.format(args.service))
# Filter the services list to make fewer ListVersions calls.
if args.service:
services = [s for s in services if s.id == args.service]
versions = api_client.ListVersions(services)
# Filter for service.
if args.service:
versions = [v for v in versions if v.service == args.service]
# Filter for traffic.
if args.hide_no_traffic:
versions = [v for v in versions if v.traffic_split]
return sorted(versions, key=str)

View File

@@ -0,0 +1,115 @@
# -*- coding: utf-8 -*- #
# Copyright 2016 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""The Migrate command."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.app import appengine_api_client
from googlecloudsdk.api_lib.app import operations_util
from googlecloudsdk.api_lib.app import service_util
from googlecloudsdk.calliope import base
from googlecloudsdk.core import exceptions
from googlecloudsdk.core import log
from googlecloudsdk.core.console import console_io
import six
import six.moves
class VersionsMigrateError(exceptions.Error):
"""Errors when migrating versions."""
@base.ReleaseTracks(base.ReleaseTrack.BETA, base.ReleaseTrack.GA)
class Migrate(base.Command):
"""Migrate traffic from one version to another for a set of services."""
detailed_help = {
'EXAMPLES': """\
This only works for automatically scaled Standard versions.
To migrate from one version to another for all services where there
is a version v2 and shut down the previous version, run:
$ {command} v2
To migrate from one version to another for a specific service, run:
$ {command} v2 --service="s1"
""",
}
@staticmethod
def Args(parser):
parser.add_argument('version', help='The version to migrate to.')
parser.add_argument('--service', '-s',
help='If specified, only migrate versions from the '
'given service.')
def Run(self, args):
client = appengine_api_client.GetApiClientForTrack(self.ReleaseTrack())
if args.service:
service = client.GetServiceResource(args.service)
traffic_split = {}
if service.split:
for split in service.split.allocations.additionalProperties:
traffic_split[split.key] = split.value
services = [service_util.Service(client.project, service.id,
traffic_split)]
else:
services = client.ListServices()
all_versions = client.ListVersions(services)
if args.version not in {v.id for v in all_versions}:
if args.service:
raise VersionsMigrateError('Version [{0}/{1}] does not exist.'.format(
args.service, args.version))
else:
raise VersionsMigrateError('Version [{0}] does not exist.'.format(
args.version))
service_names = {v.service for v in all_versions if v.id == args.version}
def WillBeMigrated(v):
return (v.service in service_names and v.traffic_split and
v.traffic_split > 0 and v.id != args.version)
# All versions that will stop receiving traffic.
versions_to_migrate = list(six.moves.filter(WillBeMigrated, all_versions))
for version in versions_to_migrate:
short_name = '{0}/{1}'.format(version.service, version.id)
promoted_name = '{0}/{1}'.format(version.service, args.version)
log.status.Print('Migrating all traffic from version '
'[{0}] to [{1}]'.format(
short_name, promoted_name))
console_io.PromptContinue(cancel_on_no=True)
errors = {}
for service in sorted(set([v.service for v in versions_to_migrate])):
allocations = {args.version: 1.0}
try:
operations_util.CallAndCollectOpErrors(
client.SetTrafficSplit, service, allocations, shard_by='ip',
migrate=True)
except (operations_util.MiscOperationError) as err:
errors[service] = six.text_type(err)
if errors:
error_string = ('Issues migrating all traffic of '
'service(s): [{0}]\n\n{1}'.format(
', '.join(list(errors.keys())),
'\n\n'.join(list(errors.values()))))
raise VersionsMigrateError(error_string)

View File

@@ -0,0 +1,109 @@
# -*- coding: utf-8 -*- #
# Copyright 2015 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""The Start command."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.app import appengine_api_client
from googlecloudsdk.api_lib.app import operations_util
from googlecloudsdk.api_lib.app import version_util
from googlecloudsdk.calliope import base
from googlecloudsdk.core import exceptions
from googlecloudsdk.core import log
from googlecloudsdk.core.console import console_io
from googlecloudsdk.core.console import progress_tracker
from googlecloudsdk.core.resource import resource_printer
import six
class VersionsStartError(exceptions.Error):
"""Errors occurring when starting versions."""
pass
class Start(base.Command):
"""Start serving specified versions.
This command starts serving the specified versions. It may only be used if the
scaling module for your service has been set to manual.
"""
detailed_help = {
'EXAMPLES': """\
To start a specific version across all services, run:
$ {command} v1
To start multiple named versions across all services, run:
$ {command} v1 v2
To start a single version on a single service, run:
$ {command} --service=servicename v1
To start multiple versions in a single service, run:
$ {command} --service=servicename v1 v2
""",
}
@staticmethod
def Args(parser):
parser.add_argument('versions', nargs='+', help=(
'The versions to start. (optionally filtered by the --service flag).'))
parser.add_argument('--service', '-s',
help=('If specified, only start versions from the '
'given service.'))
def Run(self, args):
# TODO(b/36052475): This fails with "module/version does not exist" even
# when it exists if the scaling mode is set to auto. It would be good
# to improve that error message.
api_client = appengine_api_client.GetApiClientForTrack(self.ReleaseTrack())
services = api_client.ListServices()
versions = version_util.GetMatchingVersions(
api_client.ListVersions(services),
args.versions, args.service)
if not versions:
log.warning('No matching versions found.')
return
fmt = 'list[title="Starting the following versions:"]'
resource_printer.Print(versions, fmt, out=log.status)
console_io.PromptContinue(cancel_on_no=True)
errors = {}
# Sort versions to make behavior deterministic enough for unit testing.
for version in sorted(versions, key=str):
try:
with progress_tracker.ProgressTracker('Starting [{0}]'.format(version)):
operations_util.CallAndCollectOpErrors(
api_client.StartVersion, version.service, version.id)
except operations_util.MiscOperationError as err:
errors[version] = six.text_type(err)
if errors:
printable_errors = {}
for version, error_msg in errors.items():
short_name = '[{0}/{1}]'.format(version.service, version.id)
printable_errors[short_name] = '{0}: {1}'.format(short_name, error_msg)
raise VersionsStartError(
'Issues starting version(s): {0}\n\n'.format(
', '.join(list(printable_errors.keys()))) +
'\n\n'.join(list(printable_errors.values())))

View File

@@ -0,0 +1,103 @@
# -*- coding: utf-8 -*- #
# Copyright 2015 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""The Stop command."""
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
from googlecloudsdk.api_lib.app import appengine_api_client
from googlecloudsdk.api_lib.app import operations_util
from googlecloudsdk.api_lib.app import version_util
from googlecloudsdk.calliope import base
from googlecloudsdk.core import exceptions
from googlecloudsdk.core import log
from googlecloudsdk.core.console import console_io
from googlecloudsdk.core.console import progress_tracker
from googlecloudsdk.core.resource import resource_printer
import six
class VersionsStopError(exceptions.Error):
"""Errors occurring when stopping versions."""
pass
class Stop(base.Command):
"""Stop serving specified versions.
This command stops serving the specified versions. It may only be used if the
scaling module for your service has been set to manual.
"""
detailed_help = {
'EXAMPLES': """\
To stop a specific version across all services, run:
$ {command} v1
To stop multiple named versions across all services, run:
$ {command} v1 v2
To stop a single version on a single service, run:
$ {command} --service=servicename v1
To stop multiple versions in a single service, run:
$ {command} --service=servicename v1 v2
Note that that last example may be more simply written using the
`services stop` command (see its documentation for details).
""",
}
@staticmethod
def Args(parser):
parser.add_argument('versions', nargs='+', help=(
'The versions to stop (optionally filtered by the --service flag).'))
parser.add_argument('--service', '-s',
help=('If specified, only stop versions from the '
'given service.'))
def Run(self, args):
# TODO(b/36057452): This fails with "module/version does not exist" even
# when it exists if the scaling mode is set to auto. It would be good
# to improve that error message.
api_client = appengine_api_client.GetApiClientForTrack(self.ReleaseTrack())
services = api_client.ListServices()
versions = version_util.GetMatchingVersions(
api_client.ListVersions(services),
args.versions, args.service)
if versions:
fmt = 'list[title="Stopping the following versions:"]'
resource_printer.Print(versions, fmt, out=log.status)
console_io.PromptContinue(cancel_on_no=True)
else:
log.warning('No matching versions found.')
errors = []
for version in sorted(versions, key=str):
try:
with progress_tracker.ProgressTracker('Stopping [{0}]'.format(version)):
operations_util.CallAndCollectOpErrors(
api_client.StopVersion, version.service, version.id, block=True)
except operations_util.MiscOperationError as err:
errors.append(six.text_type(err))
if errors:
raise VersionsStopError('\n\n'.join(errors))