-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathorka_inventory.py
128 lines (103 loc) · 4.14 KB
/
orka_inventory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/python3
import argparse
import json
import os
import subprocess
class OrkaAnsibleInventory:
def __init__(self):
self.vm_data = None
self.filtered_data = None
self.inventory = {
'group': {'hosts': []},
'vars': [],
'_meta': {
'hostvars': {}
}
}
def get_current_vm_data(self):
"""Get current VM data related to the current CLI user.
Note
----
The user must be logged in to the Orka CLI.
"""
completed_process = subprocess.run(
['orka', 'vm', 'list', '--json'],
capture_output=True)
dict_string = completed_process.stdout.decode('utf-8')
data = json.loads(dict_string)
self.vm_data = data['virtual_machine_resources']
def get_deployed_vms(self):
"""Filter current VM data to isolate deployed VMs."""
self.filtered_data = \
[i for i in self.vm_data if i['vm_deployment_status'] == 'Deployed']
def get_vm_by_host_name(self, host_name):
"""Filter current VM data to isolate named VM.
Args:
host_name: string: the VM name to match.
"""
self.filtered_data = \
[i for i in self.vm_data if host_name == i['status'][0]['virtual_machine_name']]
def get_name_contains_vms(self, name_contains):
"""Filter current VM data to isolate VMs by partial name match.
Args:
name_contains: string: partial match sort key for deployed VMs.
"""
nc = name_contains.lower()
self.filtered_data = \
[i for i in self.filtered_data if nc in i['status'][0]['virtual_machine_name'].lower()]
# def _build_vars(self):
# """Build the vars dict to pass to Ansible"""
# ansible_ssh_user = os.environ.get('ANSIBLE_SSH_USER')
# ansible_ssh_pass = os.environ.get('ANSIBLE_SSH_PASS')
# ansible_connection = 'ssh'
# return {
# 'ansible_connection': ansible_connection,
# 'ansible_ssh_user': ansible_ssh_user,
# 'ansible_ssh_pass': ansible_ssh_pass
# }
def create_inventory(self):
"""Create the inventory object to return to Ansible."""
hosts = []
ansible_ssh_user = os.environ.get('ANSIBLE_SSH_USER')
ansible_ssh_pass = os.environ.get('ANSIBLE_SSH_PASS')
for i in self.filtered_data:
ip_address = i['status'][0]['virtual_machine_ip']
hosts.append(ip_address)
self.inventory['_meta']['hostvars'][ip_address] = \
{'ansible_ssh_port': i['status'][0]['ssh_port'],
'ansible_ssh_user': ansible_ssh_user,
'ansible_ssh_pass': ansible_ssh_pass,
'ansible_connection': 'ssh'}
self.inventory['group']['hosts'] = hosts
# varss = self._build_vars()
# self.inventory['vars'] = varss
print(json.dumps(self.inventory))
return json.dumps(self.inventory)
def parse_args():
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument('--list', help='list deployed VMs',
action='store_true')
group.add_argument('--host', help='get host by name', action='store',
dest='host_name')
return parser.parse_args()
def main(args, name_contains):
if args.host_name:
host_name = args.host_name
inventory_creator = OrkaAnsibleInventory()
inventory_creator.get_current_vm_data()
inventory_creator.get_vm_by_host_name(host_name)
inventory_creator.create_inventory()
elif args.list:
inventory_creator = OrkaAnsibleInventory()
inventory_creator.get_current_vm_data()
inventory_creator.get_deployed_vms()
if name_contains:
inventory_creator.get_name_contains_vms(name_contains)
inventory_creator.create_inventory()
else:
print('Warning: you must pass either `--list` or `--host <hostname>` argument.')
if __name__ == '__main__':
args = parse_args()
name_contains = os.environ.get('ANSIBLE_NAME_CONTAINS')
main(args, name_contains)