Browse Source

enough: dotenough must save the port to hosts.yml

keep-around/7f2bc0d22ec09ff17e66fcb005b9263daa8dd796
Loïc Dachary 1 year ago
committed by Loic Dachary
parent
commit
a252f3261c
Signed by: dachary GPG Key ID: 992D23B392F9E4F2
  1. 7
      enough/common/dotenough.py
  2. 6
      enough/common/openstack.py
  3. 8
      tests/enough/common/test_dotenough.py

7
enough/common/dotenough.py

@ -23,6 +23,9 @@ class Hosts(object):
def get_ip(self, host):
return self.hosts.get(host, {}).get('ansible_host')
def get_port(self, host):
return self.hosts.get(host, {}).get('ansible_port', '22')
def save(self):
if not os.path.exists(self.d):
os.makedirs(self.d)
@ -38,9 +41,9 @@ class Hosts(object):
def missings(self, names):
return [name for name in names if name not in self.hosts]
def create_or_update(self, name, ipv4):
def create_or_update(self, name, ipv4, port):
if self.get_ip(name) != ipv4:
self.hosts[name] = {'ansible_host': ipv4}
self.hosts[name] = {'ansible_host': ipv4, 'ansible_port': port}
self.save()
return True
else:

6
enough/common/openstack.py

@ -81,8 +81,9 @@ class Stack(OpenStackBase):
self.o.stack.list('--format=value', '-c', 'Stack Name', _iter=True)
]
@staticmethod
@retry((socket.timeout, ConnectionRefusedError), 9)
def wait_for_ssh(self, ip, port):
def wait_for_ssh(ip, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(5)
s.connect((ip, int(port)))
@ -94,7 +95,8 @@ class Stack(OpenStackBase):
self.create()
info = self.update()
self.wait_for_ssh(info['ipv4'], info['port'])
Hosts(self.config_dir).create_or_update(self.definition['name'], info['ipv4'])
Hosts(self.config_dir).create_or_update(
self.definition['name'], info['ipv4'], info['port'])
return info
def create_internal_network(self):

8
tests/enough/common/test_dotenough.py

@ -12,12 +12,14 @@ def test_hosts_create_delete(tmpdir):
h = Hosts(config_dir)
host = 'HOST'
ip = '1.2.3.4'
assert h.create_or_update(host, ip) is True
assert h.create_or_update(host, ip) is False
port = '22'
assert h.create_or_update(host, ip, port) is True
assert h.create_or_update(host, ip, port) is False
assert os.path.exists(f'{config_dir}/inventory/hosts.yml')
h = Hosts(config_dir)
assert h.create_or_update(host, ip) is False
assert h.create_or_update(host, ip, port) is False
assert h.hosts[host]['ansible_host'] == ip
assert h.hosts[host]['ansible_port'] == port
assert h.missings([host, 'MISSING']) == ['MISSING']

Loading…
Cancel
Save