Reputation: 327
Sorry for misunderstanding title, but I couldn't explained my issue in a small title. It's not a big problem but confusing anyway.
Current output:
1) Deployment
2) Service Status
3) List Servers
4) Restart Service
5) Restart Server
6) Execute Commands on Servers
Make your choice: 4
1) Group
2) Single Machine
Group or Single Machine?: 1
1) The Perfect Life
Which group?: 1
1) Tomcat
1) JBoss
2) Tomcat
1) JBoss
2) Tomcat
1) JBoss
Select a service: --????--*
XML:
<Group id="1" name="The Perfect Life"
username="root" password="mypasswd123" state="ok">
<Server id="1" name="Machine (250)" ip="192.168.1.250" username="" password="" state="ok">
<App id="1" type="Tomcat" state="ok" />
</Server>
<Server id="2" name="Machine (251)" ip="192.168.1.251" username="" password="" state="ok">
<App id="1" type="JBoss" path="" state="ok" />
<App id="2" type="Tomcat" path="" state="ok" />
</Server>
<Server id="3" name="Machine (252)" ip="192.168.1.252" username="" password="" state="ok">
<App id="1" type="JBoss" path="" state="ok" />
<App id="2" type="Tomcat" path="" state="ok" />
</Server>
<Server id="4" name="Machine (253)" ip="192.168.1.253" username="" password="" state="ok">
<App id="1" type="JBoss" path="" state="ok" />
</Server>
</Group>
Code to retrieve the unfunctional "Select a service" part:
if GroupList:
for App in Group.getElementsByTagName('App'):
print App.getAttribute('id')+") "+App.getAttribute('type')
sList = raw_input("Select a service: ")
App = Group.getElementsByTagName('App')[int(sList)-1]
print (' ')
if sList:
x = App.getAttribute('state')
stype = App.getAttribute('type')
trigger = 0
while trigger < 1:
if x == 'ok':
try:
getDeployPath = App.getAttribute('path')
sIP = Server.getAttribute('ip')
sUSER = Server.getAttribute('username')
sPASS = Server.getAttribute('password')
if not sUSER:
sUSER = Group.getAttribute('username')
if not sPASS:
sPASS = Group.getAttribute('password')
dssh = paramiko.SSHClient()
dssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
dssh.connect(sIP, username=sUSER, password=sPASS)
And it goes...
...
...
So, what I want to do?
1) I want to, print Tomcat and JBoss (or when I add a new App to XML, print it) briefly printing different types.
2) After a selection made, select all App Tags for the Selected Service Type under all Group Servers. Because I will add more groups.
I tryed everything by myself. Tryed grouping them with key, but after selection I'm stuck.
NOTE: I'm using Python's default minidom library. Not lxml or something else.
Thanks
Upvotes: 0
Views: 120
Reputation: 38265
I'm a bit confused about what you're asking. I'm also confused by your XML schema.
However, I would recommend that you familiarise yourself with XPATH. My preferred XML lib is lxml
.
You can use xpath expressions
to query your XML data. The biggest caveat is that your XML may not comply with your desired data model. You ought to enforce compliance with an XML Schema and/or do a lot of manual error checking.
Here's a simple (no input checking) example of using XPATH expressions to query your XML blob. I'm selecting the group by id, but you could leave out predicate after group if you wanted to select across all groups:
from lxml import etree
doc = etree.XML("""
<Group id="1" name="The Perfect Life"
username="root" password="mypasswd123" state="ok">
<Server id="1" name="Machine (250)" ip="192.168.1.250" username="" password="" state="ok">
<App id="1" type="Tomcat" state="ok" />
</Server>
<Server id="2" name="Machine (251)" ip="192.168.1.251" username="" password="" state="ok">
<App id="1" type="JBoss" path="" state="ok" />
<App id="2" type="Tomcat" path="" state="ok" />
</Server>
<Server id="3" name="Machine (252)" ip="192.168.1.252" username="" password="" state="ok">
<App id="1" type="JBoss" path="" state="ok" />
<App id="2" type="Tomcat" path="" state="ok" />
</Server>
<Server id="4" name="Machine (253)" ip="192.168.1.253" username="" password="" state="ok">
<App id="1" type="JBoss" path="" state="ok" />
</Server>
</Group>
""")
# group id from user
group_id = "1"
# unique apps under group_id
app_types = set(doc.xpath('//Group[@id=$group_id]/descendant::App/@type',group_id=group_id))
app_dict = dict((x+1,y) for (x,y) in enumerate(app_types))
print "Apps:\n\t" + "\n\t".join("%d) %s" % x for x in sorted(app_dict.items()))
app_selection = input("Select a service: ")
app_type = app_dict.get(int(app_selection))
# Servers in group_id with app_type
for server in doc.xpath('//Group[@id=$group_id]/Server[./App[@type=$app_type]]',group_id=group_id,app_type=app_type):
print etree.tostring(server) # Don't know what you want to do per-server...
Yields:
Apps:
1) JBoss
2) Tomcat
Select a service: 1
<Server id="2" name="Machine (251)" ip="192.168.1.251" username="" password="" state="ok">
<App id="1" type="JBoss" path="" state="ok"/>
<App id="2" type="Tomcat" path="" state="ok"/>
</Server>
<Server id="3" name="Machine (252)" ip="192.168.1.252" username="" password="" state="ok">
<App id="1" type="JBoss" path="" state="ok"/>
<App id="2" type="Tomcat" path="" state="ok"/>
</Server>
<Server id="4" name="Machine (253)" ip="192.168.1.253" username="" password="" state="ok">
<App id="1" type="JBoss" path="" state="ok"/>
</Server>
Upvotes: 1