Bug 1596479: teach `run-task` to get worker location from `TASKCLUSTER_WORKER_LOCATION` environment var r=tomprince

Previously we inspected the `TASKCLUSTER_WORKER_GROUP` environment variable,
which now only returns the cloud provider of the worker. This commit teaches
`run-task` to instead use the `TASKCLUSTER_WORKER_LOCATION` to gather
information about the location of the worker. We also use the extra data
about the cloud provider for the worker to construct a key for use in the
config, in the form `cloudprovider/region`, so GCP hgweb mirrors can be
amended to the `hgmointernal` config when they are ready.

While we're here we make the error handling for a missing environment
variable slightly nicer.

Differential Revision: https://phabricator.services.mozilla.com/D53209

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Connor Sheehan 2019-11-20 00:10:53 +00:00
Родитель 250d8b8e9d
Коммит 3adff2160f
1 изменённых файлов: 23 добавлений и 12 удалений

Просмотреть файл

@ -538,14 +538,28 @@ def resolve_checkout_url(base_repo, head_repo):
The config will be of the form:
{
"us-west-2": { # value of `TASKCLUSTER_WORKER_GROUP`
"aws/us-west-2": { # key built from `TASKCLUSTER_WORKER_LOCATION` variable
"rate": 0.5,
"domain": "us-west-2.hgmointernal.net"
},
"us-east-1": {...}
"google/us-central1": {...}
}
"""
region = os.getenv('TASKCLUSTER_WORKER_GROUP')
worker_location = os.getenv('TASKCLUSTER_WORKER_LOCATION')
if not worker_location:
print_line(b'vcs', b'TASKCLUSTER_WORKER_LOCATION environment variable not set; '
b'using public hg.mozilla.org service\n')
return base_repo, head_repo
try:
worker_location = json.loads(worker_location)
except json.JSONDecodeError:
print_line(b'vcs', b'Could not decode TASKCLUSTER_WORKER_LOCATION environment variable '
b'as JSON. Content: %s\n' % worker_location)
print_line(b'vcs', b'using public hg.mozilla.org service\n')
return base_repo, head_repo
config_key = '%(cloud)s/%(region)s' % worker_location
try:
print_line(b'vcs', b'fetching hgmointernal config from %s\n' %
@ -556,17 +570,14 @@ def resolve_checkout_url(base_repo, head_repo):
hgmointernal_config = json.loads(res.read().decode('utf-8'))['secret']
# Use public hg service if region not yet supported
if region not in hgmointernal_config:
if region:
print_line(b'vcs', b'region %s not yet supported; using public '
b'hg.mozilla.org service\n' % region.encode('utf-8'))
else:
print_line(b'vcs', b'unspecified region; using public '
b'hg.mozilla.org service\n')
if config_key not in hgmointernal_config:
print_line(b'vcs', b'region %s not yet supported; using public '
b'hg.mozilla.org service\n' % config_key.encode('utf-8'))
return base_repo, head_repo
# Only send a percentage of traffic to the internal mirror
rate = float(hgmointernal_config[region]['rate'])
rate = float(hgmointernal_config[config_key]['rate'])
if random.random() > rate:
print_line(b'vcs', b'hgmointernal rate miss; using '
@ -576,7 +587,7 @@ def resolve_checkout_url(base_repo, head_repo):
print_line(b'vcs', b'hgmointernal rate hit; cloning from '
b'private hgweb mirror\n')
mirror_domain = hgmointernal_config[region]['domain']
mirror_domain = hgmointernal_config[config_key]['domain']
if base_repo and base_repo.startswith('https://hg.mozilla.org'):
base_repo = base_repo.replace('hg.mozilla.org', mirror_domain, 1)