Browse Source

Use username from node information if available

Nodepool knows the username that you should ssh with at image build time
and includes this information in the node data. Zuul should use this
username for the executor target.

Change-Id: I1e677061b9fd495b192d25a5825362c81e40d0c6
Depends-On: Ife0daa79f319aea04ed32513f99c73c460156941
changes/83/453983/8
Jamie Lennox 5 years ago committed by Tobias Henkel
parent
commit
d4006d6927
  1. 2
      tests/base.py
  2. 2
      tests/fixtures/config/inventory/git/common-config/playbooks/hostvars-inventory.yaml
  3. 13
      tests/fixtures/config/inventory/git/common-config/zuul.yaml
  4. 1
      tests/fixtures/config/inventory/git/org_project/.zuul.yaml
  5. 21
      tests/unit/test_inventory.py
  6. 4
      zuul/executor/server.py
  7. 1
      zuul/model.py

2
tests/base.py

@ -1713,6 +1713,8 @@ class FakeNodepool(object):
image_id=None,
host_keys=["fake-key1", "fake-key2"],
executor='fake-nodepool')
if 'fakeuser' in node_type:
data['username'] = 'fakeuser'
data = json.dumps(data).encode('utf8')
path = self.client.create(path, data,
makepath=True,

2
tests/fixtures/config/inventory/git/common-config/playbooks/hostvars-inventory.yaml vendored

@ -0,0 +1,2 @@
- hosts: all
tasks: []

13
tests/fixtures/config/inventory/git/common-config/zuul.yaml vendored

@ -31,6 +31,14 @@
- compute1
- compute2
- nodeset:
name: nodeset2
nodes:
- name: default
label: default-label
- name: fakeuser
label: fakeuser-label
- job:
name: base
parent: null
@ -47,3 +55,8 @@
name: group-inventory
nodeset: nodeset1
run: playbooks/group-inventory.yaml
- job:
name: hostvars-inventory
run: playbooks/hostvars-inventory.yaml
nodeset: nodeset2

1
tests/fixtures/config/inventory/git/org_project/.zuul.yaml vendored

@ -4,3 +4,4 @@
jobs:
- single-inventory
- group-inventory
- hostvars-inventory

21
tests/unit/test_inventory.py

@ -80,3 +80,24 @@ class TestInventory(ZuulTestCase):
self.executor_server.release()
self.waitUntilSettled()
def test_hostvars_inventory(self):
inventory = self._get_build_inventory('hostvars-inventory')
all_nodes = ('default', 'fakeuser')
self.assertIn('all', inventory)
self.assertIn('hosts', inventory['all'])
self.assertIn('vars', inventory['all'])
for node_name in all_nodes:
self.assertIn(node_name, inventory['all']['hosts'])
# check if the nodes use the correct username
if node_name == 'fakeuser':
username = 'fakeuser'
else:
username = 'zuul'
self.assertEqual(
inventory['all']['hosts'][node_name]['ansible_user'], username)
self.executor_server.release()
self.waitUntilSettled()

4
zuul/executor/server.py

@ -920,6 +920,10 @@ class AnsibleJob(object):
private_ipv4=node.get('private_ipv4'),
public_ipv6=node.get('public_ipv6')))
username = node.get('username')
if username:
host_vars['ansible_user'] = username
host_keys = []
for key in node.get('host_keys'):
if port != 22:

1
zuul/model.py

@ -388,6 +388,7 @@ class Node(object):
self.az = None
self.provider = None
self.region = None
self.username = None
@property
def state(self):

Loading…
Cancel
Save