|
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +from leapp.actors import Actor |
| 5 | +from leapp.libraries.stdlib import api |
| 6 | +from leapp.libraries.common.cllaunch import run_on_cloudlinux |
| 7 | +from leapp.libraries.common.cln_switch import get_target_userspace_path |
| 8 | +from leapp.tags import DownloadPhaseTag, IPUWorkflowTag |
| 9 | +from leapp.libraries.common.config.version import get_target_major_version |
| 10 | + |
| 11 | + |
| 12 | +class PinClnMirror(Actor): |
| 13 | + """ |
| 14 | + Save CLN mirror that was used last time. |
| 15 | + """ |
| 16 | + |
| 17 | + name = 'pin_cln_mirror' |
| 18 | + consumes = () |
| 19 | + produces = () |
| 20 | + tags = (IPUWorkflowTag, DownloadPhaseTag.Before) |
| 21 | + |
| 22 | + CLN_REPO_ID = "cloudlinux-x86_64-server-%s" |
| 23 | + DEFAULT_CLN_MIRROR = "https://xmlrpc.cln.cloudlinux.com/XMLRPC/" |
| 24 | + |
| 25 | + @run_on_cloudlinux |
| 26 | + def process(self): |
| 27 | + """Pin CLN mirror""" |
| 28 | + target_userspace = get_target_userspace_path() |
| 29 | + api.current_logger().info("Pin CLN mirror: target userspace=%s", target_userspace) |
| 30 | + |
| 31 | + # load last mirror URL from dnf spacewalk plugin cache |
| 32 | + spacewalk_settings = {} |
| 33 | + |
| 34 | + # find the mirror used in the last transaction |
| 35 | + # (expecting to find the one used in dnf_package_download actor) |
| 36 | + spacewalk_json_path = os.path.join(target_userspace, 'var/lib/dnf/_spacewalk.json') |
| 37 | + try: |
| 38 | + with open(spacewalk_json_path) as file: |
| 39 | + spacewalk_settings = json.load(file) |
| 40 | + except (OSError, IOError, ValueError): |
| 41 | + api.current_logger().error( |
| 42 | + "No spacewalk settings found in %s - can't identify the last used CLN mirror", |
| 43 | + spacewalk_json_path, |
| 44 | + ) |
| 45 | + |
| 46 | + mirror_url = spacewalk_settings.get( |
| 47 | + self.CLN_REPO_ID % get_target_major_version(), {} |
| 48 | + ).get("url", [self.DEFAULT_CLN_MIRROR])[0] |
| 49 | + |
| 50 | + # pin mirror |
| 51 | + mirrorlist_path = os.path.join(target_userspace, 'etc/mirrorlist') |
| 52 | + with open(mirrorlist_path, 'w') as file: |
| 53 | + file.write(mirror_url + '\n') |
| 54 | + api.current_logger().info("Pin CLN mirror %s in %s", mirror_url, mirrorlist_path) |
| 55 | + |
| 56 | + up2date_path = os.path.join(target_userspace, 'etc/sysconfig/rhn/up2date') |
| 57 | + with open(up2date_path, 'a+') as file: |
| 58 | + file.write('\nmirrorURL[comment]=Set mirror URL to /etc/mirrorlist\nmirrorURL=file:///etc/mirrorlist\n') |
| 59 | + api.current_logger().info("Updated up2date_path %s", up2date_path) |
0 commit comments