Merge "Fix rax reverse DNS setup in launch"

This commit is contained in:
Zuul 2023-04-13 00:22:46 +00:00 committed by Gerrit Code Review
commit a5a65012c2
3 changed files with 49 additions and 32 deletions

View File

@ -19,19 +19,10 @@
# limitations under the License. # limitations under the License.
import argparse import argparse
from . import rax_rdns
from .sshfp import sshfp_print_records from .sshfp import sshfp_print_records
from .ssh_knownhosts import generate_known_hosts 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): def print_dns(cloud, server):
ip4 = server.public_v4 ip4 = server.public_v4
ip6 = server.public_v6 ip6 = server.public_v6
@ -53,23 +44,6 @@ def print_dns_opendev(name, ip4, ip6):
sshfp_print_records(name, ip4) 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): def print_inventory_yaml(server, ip4, ip6):
known_hosts = generate_known_hosts(ip4) known_hosts = generate_known_hosts(ip4)
@ -106,7 +80,4 @@ def main():
" openstacksdk >= 0.12 is required") " openstacksdk >= 0.12 is required")
raise raise
if 'rax' in cloud.config.name:
set_rax_reverse_dns(cloud, server, ip4, ip6)
print_dns(cloud, server) print_dns(cloud, server)

View File

@ -30,6 +30,7 @@ import time
import traceback import traceback
from . import dns from . import dns
from . import rax_rdns
from . import utils from . import utils
import openstack import openstack
@ -430,6 +431,9 @@ def main():
options.environment, options.volume_size, options.environment, options.volume_size,
options.timeout, options.ignore_ipv6, options.timeout, options.ignore_ipv6,
options.playbooks) 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) dns.print_dns(cloud, server)
print("If this is a server that is expected to send email (ask, review,") 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") print("lists, etc) double check that the server's IPs are not listed on")

View File

@ -39,6 +39,15 @@ import sys
RACKSPACE_IDENTITY_ENDPOINT='https://identity.api.rackspacecloud.com/v2.0/tokens' RACKSPACE_IDENTITY_ENDPOINT='https://identity.api.rackspacecloud.com/v2.0/tokens'
RACKSPACE_DNS_ENDPOINT="https://dns.api.rackspacecloud.com/v1.0" 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): def _get_auth_token(session, username, api_key):
# Get auth token # Get auth token
data = {'auth': 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)) 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(): def main():
parser = argparse.ArgumentParser(description='Update RDNS') parser = argparse.ArgumentParser(description='Update RDNS')
parser.add_argument('--debug', dest='debug', action='store_true') parser.add_argument('--debug', dest='debug', action='store_true')
@ -120,7 +146,10 @@ def main():
default='/etc/rax-rdns-auth.conf') default='/etc/rax-rdns-auth.conf')
parser.add_argument('--ttl', dest='ttl', type=int, default=3600) parser.add_argument('--ttl', dest='ttl', type=int, default=3600)
parser.add_argument('name') 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('ip4')
parser.add_argument('ip6') parser.add_argument('ip6')
args = parser.parse_args() args = parser.parse_args()
@ -132,5 +161,18 @@ def main():
requests_log.setLevel(logging.DEBUG) requests_log.setLevel(logging.DEBUG)
requests_log.propogate = True requests_log.propogate = True
auth = get_auth(args.config) if args.server_href:
rax_rdns(args.name, args.server_href, args.ip4, args.ip6, args.ttl, auth) 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)