Skip to content

Commit

Permalink
Fixes #7906: Create a migration script for inventory files from Rudde…
Browse files Browse the repository at this point in the history
…r 2.11 to Rudder 3.2
  • Loading branch information
fanf committed Feb 12, 2016
1 parent 20271cb commit 06a36cc
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions scripts/rudder-migrate-inventory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# need python lxml library. On most distribution, it comes with
# with the default Python installation as it is quite common.

# Centos7: the package needed is:
# yum install python-lxml

# Arch
# pacman -Sy python-lxml

# Debian
# sudo apt-get install -y python-lxml
# sudo pip3 install lxml

# You can also use pip to install the package.
# On some case, it seems broken and need devel package,
# for example in Centos: # see: http://stackoverflow.com/questions/20886069/cant-install-lxml-on-centos
# yum install yum install libxslt-devel libxml2-devel
# pip install lxml


from lxml import etree
import sys
from shutil import copyfile

def help():
print("""
A script that allows to transform inventory file for Rudder 2.x branch
into inventory file for Rudder 3.2+ branch.
Usage:
rudder-migrate-inventory source-inventory-2.x.ocs destination-name.ocs
""")


def migrate(source_file, dest_file):
try:
tree = etree.parse(source_file)

# if <RUDDER> tag is here, just abort
if tree.xpath("//RUDDER"):
print("<RUDDER> tag already present, not need to migrate " + source_file + " (simple copy to "+ dest_file + ")")
copyfile(source_file, dest_file)
exit(0)

content = tree.xpath("/REQUEST/CONTENT")[0]

# adding a <RUDDER> tag

rudder = etree.SubElement(content, "RUDDER")
rudder.append(tree.xpath("/REQUEST/UUID")[0])
rudder.append(tree.xpath("/REQUEST/HOSTNAME")[0])
# processors is not mandatory
processors = tree.xpath("/REQUEST/PROCESSORS")
if processors:
rudder.append(processors[0])

# Adding <RUDDER><AGENT>, now mandatory

agent = etree.SubElement(rudder, "AGENT")
agent_name = etree.SubElement(agent, "AGENT_NAME")
agent_name.text = tree.xpath("/REQUEST/AGENTSNAME/AGENTNAME")[0].text
policy_server = etree.SubElement(agent, "POLICY_SERVER_UUID")
policy_server.text = tree.xpath("/REQUEST/POLICY_SERVER")[0].text
owner = etree.SubElement(agent, "OWNER")
owner.text = tree.xpath("/REQUEST/USER")[0].text

# cfkey not mandatory
keys = tree.xpath("/REQUEST/CFKEY")
if keys:
cfkey = etree.SubElement(agent, "CFENGINE_KEY")
cfkey.text = keys[0].text

tree.write(dest_file, pretty_print=True)

except etree.XMLSyntaxError as e:
log = e.error_log.filter_from_level(etree.ErrorLevels.FATAL)
print("Can not parse file" + source_file + ", exception was:")
print(log)

except Exception as e:
print("Exception when processing file " + source_file + ":")
raise

# Main loop
if __name__ == "__main__":
if len(sys.argv) != 3:
help()
else:
migrate(sys.argv[1], sys.argv[2])

0 comments on commit 06a36cc

Please sign in to comment.