Skip to content

Commit

Permalink
Add IPv6 support (#845)
Browse files Browse the repository at this point in the history
* Introduce RQD_USE_IPV6_AS_HOSTNAME to get IPv6 address

* Increase hostname length to 45

* Fix IPv4-mapped IPv6 ddress handling in getHostNameFromFQDN

* Add tests for IPv6 address

* Bump version to 0.5 for the database schema change
  • Loading branch information
splhack committed Jan 23, 2021
1 parent dff882c commit 92a8670
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,10 @@ private String getHostNameFromFQDN(String fqdn, Boolean useLongNames) {
if (ipMatcher.matches()){
hostName = fqdn;
}
else if (fqdn.contains(":")) {
// looks like IPv6 address.
hostName = fqdn;
}
else if (useLongNames) {
hostName = fqdn;
Pattern domainPattern = Pattern.compile(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Increase the length of hostnames for IPv6 address.

ALTER TABLE "host" ALTER COLUMN "str_name" TYPE VARCHAR(45);
ALTER TABLE "host_tag" ALTER COLUMN "str_tag" TYPE VARCHAR(45);
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,45 @@ public void testInsertHostFQDN4() {
assertEquals(TEST_HOST_NEW, hostDetail.name);
}

@Test
@Transactional
@Rollback(true)
public void testInsertHostIPv61() {
String TEST_HOST_NEW = "::1";
hostDao.insertRenderHost(buildRenderHost(TEST_HOST_NEW),
hostManager.getDefaultAllocationDetail(),
false);

HostEntity hostDetail = hostDao.findHostDetail(TEST_HOST_NEW);
assertEquals(TEST_HOST_NEW, hostDetail.name);
}

@Test
@Transactional
@Rollback(true)
public void testInsertHostIPv62() {
String TEST_HOST_NEW = "ABCD:ABCD:ABCD:ABCD:ABCD:ABCD:ABCD:ABCD";
hostDao.insertRenderHost(buildRenderHost(TEST_HOST_NEW),
hostManager.getDefaultAllocationDetail(),
false);

HostEntity hostDetail = hostDao.findHostDetail(TEST_HOST_NEW);
assertEquals(TEST_HOST_NEW, hostDetail.name);
}

@Test
@Transactional
@Rollback(true)
public void testInsertHostIPv63() {
String TEST_HOST_NEW = "ABCD:ABCD:ABCD:ABCD:ABCD:ABCD:192.168.100.180";
hostDao.insertRenderHost(buildRenderHost(TEST_HOST_NEW),
hostManager.getDefaultAllocationDetail(),
false);

HostEntity hostDetail = hostDao.findHostDetail(TEST_HOST_NEW);
assertEquals(TEST_HOST_NEW, hostDetail.name);
}

@Test
@Transactional
@Rollback(true)
Expand Down
3 changes: 3 additions & 0 deletions rqd/rqd/rqconstants.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
RQD_RETRY_STARTUP_CONNECT_DELAY = 30
RQD_RETRY_CRITICAL_REPORT_DELAY = 30
RQD_USE_IP_AS_HOSTNAME = True
RQD_USE_IPV6_AS_HOSTNAME = False
RQD_BECOME_JOB_USER = True
RQD_CREATE_USER_IF_NOT_EXISTS = True
RQD_TAGS = ''
Expand Down Expand Up @@ -187,6 +188,8 @@
LOAD_MODIFIER = config.getint(__section, "LOAD_MODIFIER")
if config.has_option(__section, "RQD_USE_IP_AS_HOSTNAME"):
RQD_USE_IP_AS_HOSTNAME = config.getboolean(__section, "RQD_USE_IP_AS_HOSTNAME")
if config.has_option(__section, "RQD_USE_IPV6_AS_HOSTNAME"):
RQD_USE_IPV6_AS_HOSTNAME = config.getboolean(__section, "RQD_USE_IPV6_AS_HOSTNAME")
if config.has_option(__section, "RQD_BECOME_JOB_USER"):
RQD_BECOME_JOB_USER = config.getboolean(__section, "RQD_BECOME_JOB_USER")
if config.has_option(__section, "RQD_TAGS"):
Expand Down
8 changes: 6 additions & 2 deletions rqd/rqd/rqutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,19 @@ def checkAndCreateUser(username):

def getHostIp():
"""Returns the machine's local ip address"""
return socket.gethostbyname(socket.gethostname())
if rqd.rqconstants.RQD_USE_IPV6_AS_HOSTNAME:
return socket.getaddrinfo(socket.gethostname(), None, socket.AF_INET6)[0][4][0]
else:
return socket.gethostbyname(socket.gethostname())


def getHostname():
"""Returns the machine's fully qualified domain name"""
try:
if rqd.rqconstants.OVERRIDE_HOSTNAME:
return rqd.rqconstants.OVERRIDE_HOSTNAME
elif rqd.rqconstants.RQD_USE_IP_AS_HOSTNAME:
elif rqd.rqconstants.RQD_USE_IP_AS_HOSTNAME or \
rqd.rqconstants.RQD_USE_IPV6_AS_HOSTNAME:
return getHostIp()
else:
return socket.gethostbyaddr(socket.gethostname())[0].split('.')[0]
Expand Down

0 comments on commit 92a8670

Please sign in to comment.