zuul-jobs/roles/use-buildset-registry/library/modify_buildkitd_toml.py
Monty Taylor 70db82198c Write buildkitd.toml in use-buildset-registry
It's a generally useful file for people using buildkitd. It's also
more appropriate to write it in use-buildset-registry and then
just have build-docker-image copy it.

Do the same thing with writing the cert - we don't need to know
which path on the host use-buildset-registry wrote the file to,
we can just write the content from the dir into the container,
and then it's just a consistent command to apply it.

Change-Id: Iaa485c2e8628900dccbed1f4b0773b6d1b5f7983
2020-05-05 14:47:19 -05:00

71 lines
2.2 KiB
Python

# Copyright 2019 Red Hat, Inc
#
# 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.
import os
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils import remarshal
def get_location(prefix, location):
# To support usage with both docker and podman, the buildset
# registry keeps "docker.io" entries un-namespaced.
if prefix == 'docker.io':
return location
else:
return location + '/' + prefix
def ansible_main():
module = AnsibleModule(
argument_spec=dict(
path=dict(required=True, type='path'),
buildset_registry=dict(type='raw'),
buildset_registry_alias=dict(type='str'),
namespaces=dict(type='raw'),
)
)
p = module.params
location = '%s:%s' % (p['buildset_registry_alias'],
p['buildset_registry']['port'])
if os.path.exists(p['path']):
with open(p['path'], 'rb') as f:
input_data = f.read()
data = remarshal.decode('toml', input_data, True)
else:
input_data = None
data = {}
if 'registry' not in data:
data['registry'] = {}
for namespace in set(p['namespaces']):
n_config = data['registry'].setdefault(namespace, {})
mirrors = n_config.setdefault('mirrors', [])
new_loc = get_location(namespace, location)
if not mirrors or new_loc != mirrors[0]:
mirrors.insert(0, new_loc)
output_data = remarshal.encode_toml(data, True)
changed = input_data is None or input_data != output_data
if changed:
with open(p['path'], 'wb') as f:
f.write(output_data.encode('utf8'))
module.exit_json(changed=changed, data=data)
if __name__ == '__main__':
ansible_main()