def process_args(self, argv, pw_database=None): usage = "usage: %prog [options] deployment_home resource_id" parser = OptionParser(usage=usage) parser.add_option("--dry-run", dest="dry_run", default=False, action="store_true", help="If specified, do a dry run of the resource.") parser.add_option("--install-script-file", dest="install_script_file", default=None, help="Name of install script file (defaults to <dh>/config/install.script)") (self.options, args) = parser.parse_args(args=argv) if len(args) != 2: parser.error("Expecting two arguments: deployment_home resource_id") self.deployment_home = os.path.abspath(os.path.expanduser(args[0])) if not os.path.exists(self.deployment_home): parser.error("Deployment home %s does not exist" % self.deployment_home) if self.options.install_script_file: self.install_script_file = self.options.install_script_file else: self.install_script_file = os.path.join(self.deployment_home, "config/install.script") if not os.path.exists(self.install_script_file): parser.error("Install script file %s does not exist" % self.install_script_file) self.resource_id = args[1] self.file_layout = get_engine_layout_mgr() if pw_database: self.pw_database = pw_database else: pw_file = os.path.join(self.deployment_home, "config/pw_repository") self.has_pw_file = os.path.exists(pw_file) if self.has_pw_file: self.logger.info("Found password repository at %s" % pw_file) else: self.logger.info("No password repository at %s" % pw_file)
def main(argv, installer_supplied_pw_key_list=None, file_layout=None): subproc = False # this will be overwritten after call to parse_command_args, if successful logger = FakeLogger() try: if file_layout==None: file_layout = get_engine_layout_mgr() engine = InstallEngine(file_layout, installer_supplied_pw_key_list) try: engine.parse_command_args(argv) subproc = engine.options.subproc logger = engine.logger engine.run() return 0 except CmdLineError, e: if subproc: raise # we don't print any help in subprocess mode sys.stderr.write(e.__str__() + "\n") if e.option_parser: e.option_parser.print_help() return 1 except UserError, e: if __name__ != "__main__": raise # if called from another script, let that one handle it logger.exception("Aborting install due to error.") e.write_error_to_log(logger) if subproc and file_layout: e.write_error_to_file(os.path.join(file_layout.get_log_directory(), "error.json")) return 1 else: raise # if running directly, let exception bubble to top
def main(argv): parser = create_opt_parser() (options, args) = parser.parse_args(argv) if options.verbose: setup_logging(logging.DEBUG) else: setup_logging(logging.INFO) resource_keys = \ engage.utils.rdef.parse_resource_keys_option(options.resource, parser) if len(args)==0: rdef_file = NamedTemporaryFile(delete=False) rdef_file.close() filename = rdef_file.name using_temp_file = True elif len(args)==1: filename = os.path.abspath(os.path.expanduser(args[0])) using_temp_file = False else: parser.error("Too many arguments") try: layout_mgr = engage_file_layout.get_engine_layout_mgr() main_rdef_file = os.path.abspath(os.path.expanduser(layout_mgr.get_resource_def_file())) all_input_files = layout_mgr.get_extension_resource_files() + [main_rdef_file,] if filename in all_input_files: # make sure user doesn't overwrite main file # should only get this problem if user specified the file assert not using_temp_file parser.error("Resource definition file parameter is for generated file, not the input file!") logger.debug("Preprocessing resource files, output is %s" % filename) preprocess_resource_file(main_rdef_file, layout_mgr.get_extension_resource_files(), filename, logger) with open(filename, "rb") as f: rg = engage.utils.rdef.create_resource_graph(json.load(f)) if resource_keys: rg = rg.filter(resource_keys) if not using_temp_file: rg.write_to_file(filename) (errors, warnings) = rg.validate() if errors==0 and warnings==0: print "Validation of resource definitions ok" return 0 else: print "Validation of resource definitions found %d errors, %d warnings" \ % (errors, warnings) return 1 finally: if using_temp_file: os.remove(filename)
def process_standard_options(options, parser, precreated_file_layout=None, installer_name=None, allow_overrides_of_dh=False, rotate_logfiles=True): if precreated_file_layout: file_layout = precreated_file_layout else: file_layout = get_engine_layout_mgr(installer_name) dh = get_deployment_home(options, parser, file_layout, allow_overrides=allow_overrides_of_dh) log_setup.parse_log_options(options, file_layout.get_log_directory(), rotate_logfiles=rotate_logfiles) if hasattr(options, "master_password_file") and \ options.master_password_file and \ (not os.path.exists(options.master_password_file)): parser.error("Master password file %s does not exist" % options.master_password_file) return (file_layout, dh)
def generate_port_checks(res_name, res_version): key = {u"name":unicode(res_name), u"version":unicode(res_version)} with NamedTempFile() as f: layout_mgr = efl.get_engine_layout_mgr() main_rdef_file = os.path.abspath(os.path.expanduser(layout_mgr.get_resource_def_file())) ppr.preprocess_resource_file(main_rdef_file, layout_mgr.get_extension_resource_files(), f.name, logger) with open(f.name, "rb") as rf: g = rdef.create_resource_graph(json.load(rf)) if not g.has_resource(key): raise Exception("Resource %s %s not found in resource definitions" % (res_name, res_version)) r = g.get_resource(key) if len(r.config_port.properties)>0: checks = resource_port_to_port_check(r.config_port) else: checks = "" for p in r.input_ports.values(): checks += resource_port_to_port_check(p) for p in r.output_ports.values(): checks += resource_port_to_port_check(p) return checks
def main(): """This provides a command line interface to the password database, with awareness of a deployment home's file layout """ usage = "\n %prog [options] view [key]\n %prog [options] create input_filename\n %prog [options] update key" parser = OptionParser(usage=usage) parser.add_option( "--deployment-home", "-d", dest="deployment_home", default=None, help="Location of deployed application - can figure this out automatically unless installing from source", ) (options, args) = parser.parse_args() # check the command line valid_commands = ["view", "view-json", "update", "create"] if len(args) == 0: command = "view" else: command = args[0] if command not in valid_commands: parser.error("Invalid command %s" % command) if len(args) > 2: parser.error("Too many arguments") if command == "update" and len(args) != 2: paser.error("Need to specify key to be updated") if command == "create": if len(args) != 2: parser.error("Need to specify input filename") create_input_filename = abspath(args[1]) if not os.path.exists(create_input_filename): parser.error("Input file %s does not exist" % create_input_filename) if command == "view-json" and len(args) > 1: parser.error("view-json does not accept any additional arguments") # setup the file layout efl = engage_file_layout.get_engine_layout_mgr() if options.deployment_home: dh = abspath(options.deployment_home) if not os.path.exists(dh): parser.error("Deployment home %s not found" % dh) elif efl.has_deployment_home(): dh = efl.get_deployment_home() else: parser.error("Not running from a deployment home, and -d was not specified") # read the existing password database, if present pw_dir = os.path.join(dh, "config") pw_file = os.path.join(pw_dir, pw_repository.REPOSITORY_FILE_NAME) salt_file = os.path.join(pw_dir, pw_repository.SALT_FILE_NAME) if os.path.exists(pw_file) and command != "create": pw_db = pw_repository.PasswordRepository.load_from_file( pw_file, salt_file, _get_master_password(ask_only_once=True) ) else: pw_db = pw_repository.PasswordRepository(_get_master_password()) # run the commands if command == "view": if len(args) == 2: key = args[1] if not pw_db.has_key(key): print "Password database does not contain key '%s'" % key return -1 print "'%s' password is '%s'" % (key, pw_db.get_value(key)) return 0 else: for (k, v) in pw_db.items(): print "'%s' password is '%s'" % (k, v) print "%d entries found." % len(pw_db.items()) return 0 elif command == "view-json": print json.dumps(pw_db.data, indent=2) return 0 elif command == "update": key = args[1] pw_db.update_key(key, _prompt_for_password("Enter password for key '%s'" % key, key)) pw_db.save_to_file(pw_file, salt_file) print "Updated password database with key '%s'" % key return 0 else: assert command == "create" try: with open(create_input_filename, "rb") as f: data = json.load(f) except e: parser.error("Unable to parse input JSON file %s: %s" % (input_filename, string(e))) if not isinstance(data, dict): parser.error("Input file %s does not contain an object/dictionary" % input_filename) for (k, v) in data.items(): pw_db.add_key(k, v) pw_db.save_to_file(pw_file, salt_file) print "Created password database" return 0
def main(argv): global sphinx_mode parser = create_opt_parser() (options, args) = parser.parse_args(argv) if options.header and \ not os.path.exists(options.header): parser.error("Header text file %s does not exist" % options.header) if options.use_sphinx_references: sphinx_mode = True setup_logging(logging.INFO) resource_keys = \ engage.utils.rdef.parse_resource_keys_option(options.resource, parser) if len(args) > 1: parser.error("Too many arguments") def warning(msg): if not options.quiet: sys.stderr.write(msg + "\n") # preprocess resources and build the graph layout_mgr = engage_file_layout.get_engine_layout_mgr() main_rdef_file = os.path.abspath( os.path.expanduser(layout_mgr.get_resource_def_file())) all_input_files = layout_mgr.get_extension_resource_files() + [ main_rdef_file, ] with NamedTempFile() as temp: preprocess_resource_file(main_rdef_file, layout_mgr.get_extension_resource_files(), temp.name, logger) with open(temp.name, "rb") as f: rg = engage.utils.rdef.create_resource_graph(json.load(f)) if resource_keys: rg = rg.filter(resource_keys) if len(args) == 0: of = sys.stdout need_to_close = False else: of = open(args[0], "wb") need_to_close = True if options.header: with open(options.header, "rb") as f: for line in f: of.write(line) # get the resource library library = engage.engine.library.parse_library_files( layout_mgr, use_temporary_file=True) resource_drivers = {} # group the resources and build the map of drivers def is_host(r): return r.inside_constraint==None and r.env_constraint==None and \ r.peer_constraint==None keys = get_sorted_resource_keys(rg) num = len(keys) primary_keys = rg.find_resources_not_referenced_as_dependencies() primary = [] hosts = [] other = [] for k in keys: r = rg.get_resource(k) kstr = engage.utils.rdef.hash_key_for_res_key(k) if kstr in primary_keys: if is_host(r): warning("Skipping resource %s %s - it is an orphan" % (r.key["name"], r.key["version"])) else: primary.append(r) elif is_host(r): hosts.append(r) else: other.append(r) md = ResourceMD("test", k) library_entry = library.get_entry(md) if library_entry: resource_drivers[kstr] = library_entry.mgr_module_name else: resource_drivers[kstr] = None warning("Resource %s %s does not have a resource library entry" % (k["name"], k["value"])) def write_section(title, description, resources): of.write(title + "\n") of.write((RDEF_GROUP_HEADER * len(title)) + "\n") of.write(description + "\n\n") for r in resources: write_resource_as_rst(r, of, rg, resource_drivers) of.write("\n") write_section( "Primary Resources", 'The resources in this section are not referenced as dependencies by other resources. In most cases, these resources correspond to the applicaiton at the "top" of an application stack.', primary) write_section( "Host Resources", 'The resources in this section have no dependencies. In most cases, these resources correspond to the machine at the "bottom" of an application stack.', hosts) write_section( "Interior Resources", 'The resources in this section have dependencies and are dependencies for other resources. In most cases, these resources correspond to the "middle" of an application stack.', other) if need_to_close: of.close() print "Processed %d resources." % num