From 276d31d2dd62dab67273755e8dcba703e91c8eb9 Mon Sep 17 00:00:00 2001 From: Clark Boylan Date: Mon, 3 Apr 2023 16:09:03 -0700 Subject: [PATCH] Fix rax reverse DNS setup in launch The launch node script didn't call the method in dns.py to setup rax reverse dns. This means we didn't attempt to set up reverse dns at all even when booting in rax. Fix this by calling the appropriate method. Note we do some refactoring too in order to keep dns.py in the business of dealing only with forward dns. All of the rax reverse dns content is kept in rax_rdns.py. Change-Id: I86091f4e5c56d38bb2d25b983f8b77ec1cd5b7b5 --- launch/src/opendev_launch/dns.py | 29 -------------- launch/src/opendev_launch/launch_node.py | 4 ++ launch/src/opendev_launch/rax_rdns.py | 48 ++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 32 deletions(-) diff --git a/launch/src/opendev_launch/dns.py b/launch/src/opendev_launch/dns.py index 2f6ca58267..453ecb934b 100755 --- a/launch/src/opendev_launch/dns.py +++ b/launch/src/opendev_launch/dns.py @@ -19,19 +19,10 @@ # limitations under the License. import argparse -from . import rax_rdns from .sshfp import sshfp_print_records from .ssh_knownhosts import generate_known_hosts -def get_href(server): - if not hasattr(server, 'links'): - return None - for link in server.links: - if link['rel'] == 'self': - return link['href'] - - def print_dns(cloud, server): ip4 = server.public_v4 ip6 = server.public_v6 @@ -53,23 +44,6 @@ def print_dns_opendev(name, ip4, ip6): sshfp_print_records(name, ip4) -def set_rax_reverse_dns(cloud, server, ip4, ip6): - # Get the server object from the sdk layer so that we can pull the - # href data out of the links dict. - try: - raw_server = cloud.compute.get_server(server.id) - except AttributeError: - print("Please update your version of shade/openstacksdk." - " openstacksdk >= 0.12 is required") - raise - href = get_href(raw_server) - - # Reads default config file /etc/rax-rdns-auth.conf and calls to - # API to set reverse dns for RAX servers. - auth = rax_rdns.get_auth() - rax_rdns.rax_rdns(server.name, href, ip4, ip6, 3600, auth) - - def print_inventory_yaml(server, ip4, ip6): known_hosts = generate_known_hosts(ip4) @@ -106,7 +80,4 @@ def main(): " openstacksdk >= 0.12 is required") raise - if 'rax' in cloud.config.name: - set_rax_reverse_dns(cloud, server, ip4, ip6) - print_dns(cloud, server) diff --git a/launch/src/opendev_launch/launch_node.py b/launch/src/opendev_launch/launch_node.py index eee342b9ef..a411294520 100755 --- a/launch/src/opendev_launch/launch_node.py +++ b/launch/src/opendev_launch/launch_node.py @@ -30,6 +30,7 @@ import time import traceback from . import dns +from . import rax_rdns from . import utils import openstack @@ -430,6 +431,9 @@ def main(): options.environment, options.volume_size, options.timeout, options.ignore_ipv6, options.playbooks) + if 'rax' in cloud.config.name: + rax_rdns.set_rax_reverse_dns(cloud, server, + server.public_v4, server.public_v6) dns.print_dns(cloud, server) print("If this is a server that is expected to send email (ask, review,") print("lists, etc) double check that the server's IPs are not listed on") diff --git a/launch/src/opendev_launch/rax_rdns.py b/launch/src/opendev_launch/rax_rdns.py index 565a133eb7..e6e66fda8f 100644 --- a/launch/src/opendev_launch/rax_rdns.py +++ b/launch/src/opendev_launch/rax_rdns.py @@ -39,6 +39,15 @@ import sys RACKSPACE_IDENTITY_ENDPOINT='https://identity.api.rackspacecloud.com/v2.0/tokens' RACKSPACE_DNS_ENDPOINT="https://dns.api.rackspacecloud.com/v1.0" + +def get_href(server): + if not hasattr(server, 'links'): + return None + for link in server.links: + if link['rel'] == 'self': + return link['href'] + + def _get_auth_token(session, username, api_key): # Get auth token data = {'auth': @@ -113,6 +122,23 @@ def rax_rdns(name, server_href, ip4, ip6, ttl, auth): logging.info("RDNS Done: %s %s" % (r.status_code, r.reason)) +def set_rax_reverse_dns(cloud, server, ip4, ip6): + # Get the server object from the sdk layer so that we can pull the + # href data out of the links dict. + try: + raw_server = cloud.compute.get_server(server.id) + except AttributeError: + print("Please update your version of shade/openstacksdk." + " openstacksdk >= 0.12 is required") + raise + href = get_href(raw_server) + + # Reads default config file /etc/rax-rdns-auth.conf and calls to + # API to set reverse dns for RAX servers. + auth = get_auth() + rax_rdns(server.name, href, ip4, ip6, 3600, auth) + + def main(): parser = argparse.ArgumentParser(description='Update RDNS') parser.add_argument('--debug', dest='debug', action='store_true') @@ -120,7 +146,10 @@ def main(): default='/etc/rax-rdns-auth.conf') parser.add_argument('--ttl', dest='ttl', type=int, default=3600) parser.add_argument('name') - parser.add_argument('server_href') + parser.add_argument('server_href', required=False, + help='If server_href is not supplied then href, ' + 'and ip addresses are fetched from the cloud ' + 'using name.') parser.add_argument('ip4') parser.add_argument('ip6') args = parser.parse_args() @@ -132,5 +161,18 @@ def main(): requests_log.setLevel(logging.DEBUG) requests_log.propogate = True - auth = get_auth(args.config) - rax_rdns(args.name, args.server_href, args.ip4, args.ip6, args.ttl, auth) + if args.server_href: + auth = get_auth(args.config) + rax_rdns(args.name, args.server_href, args.ip4, args.ip6, args.ttl, auth) + else: + import openstack + cloud = openstack.connect() + # Get the server using the shade layer so that we have server.public_v4 + # and server.public_v6 + try: + server = cloud.get_server(args.name) + except AttributeError: + print("Please update your version of shade/openstacksdk." + " openstacksdk >= 0.12 is required") + raise + set_rax_reverse_dns(cloud, server, server.public_v4, server.public_v6)