def _load_ctl_config(self, config): if isinstance(config, CtlConfig): return config else: config = CtlConfig() try: config.load_config('/etc/lnst-ctl.conf') except: pass usr_cfg = os.path.expanduser('~/.lnst/lnst-ctl.conf') if os.path.isfile(usr_cfg): config.load_config(usr_cfg) else: usr_cfg_dir = os.path.dirname(usr_cfg) pool_dir = usr_cfg_dir + "/pool" mkdir_p(pool_dir) global_pools = config.get_section("pools") if (len(global_pools) == 0): config.add_pool("default", pool_dir, usr_cfg) with open(usr_cfg, 'w') as f: f.write(config.dump_config()) dirname = os.path.dirname(sys.argv[0]) gitcfg = os.path.join(dirname, "lnst-ctl.conf") if os.path.isfile(gitcfg): config.load_config(gitcfg) return config
def _write_to_xml(self, msg, hostname, port, mode): """ Used for writing desired output into .xml file. In interactive mode, user is prompted for every interface, in noninteractive mode all interfaces are automatically added to the .xml file. """ if mode == "interactive": output_file = raw_input("Enter the name of the output .xml file "\ "(without .xml, default is hostname.xml): ") if mode == "noninteractive" or output_file == "": output_file = hostname impl = getDOMImplementation() doc = impl.createDocument(None, "slavemachine", None) top_el = doc.documentElement params_el = doc.createElement("params") top_el.appendChild(params_el) param_el = doc.createElement("param") param_el.setAttribute("name", "hostname") param_el.setAttribute("value", hostname) params_el.appendChild(param_el) interfaces_el = doc.createElement("interfaces") top_el.appendChild(interfaces_el) devices_added = 0 for interface in msg.itervalues(): if mode == 'interactive': answer = raw_input("Do you want to add interface '%s' (%s) " "to the recipe? [Y/n]" % (interface['name'], interface['hwaddr'])) if mode == "noninteractive" or answer.lower() == 'y'\ or answer == "": devices_added += 1 eth_el = doc.createElement("eth") eth_el.setAttribute("id", interface['name']) eth_el.setAttribute("label", "default_network") interfaces_el.appendChild(eth_el) params_el = doc.createElement("params") eth_el.appendChild(params_el) param_el = doc.createElement("param") param_el.setAttribute("name", "hwaddr") param_el.setAttribute("value", interface['hwaddr']) params_el.appendChild(param_el) if devices_added == 0: sys.stderr.write("You didn't add any interface, no file '%s.xml' "\ "will be created!\n" % output_file) return mkdir_p(self._pool_dir) try: f = open(self._pool_dir + "/" + output_file + ".xml", 'w') f.write(doc.toprettyxml()) f.close() except: sys.stderr.write("File '%s.xml' could not be opened "\ "or data written." % output_file+"\n") raise WizardException() print "File '%s.xml' successfuly created." % output_file
def _create_dir(self, pool_dir): """ Creates specified directory @param pool_dir Directory to be created @return Path to dir which was created, None if no directory was created """ try: mkdir_p(pool_dir) print("Dir '%s' has been created" % pool_dir) return pool_dir except: sys.stderr.write("Failed creating dir\n") return None