示例#1
0
            def client_cfg_from_tpl(product):
                homedir = os.path.expanduser("~")
                if product == configure.OPSIM:
                    filename = "opsim-client.conf"
                    cfg_link = os.path.join(homedir, ".lsst", "opsim.conf")
                elif product == configure.MYSQL:
                    filename = "my-client.cnf"
                    cfg_link = os.path.join(homedir, ".my.cnf")
                else:
                    logging.fatal(
                        "Unable to apply configuration template for "
                        "product %s", product)
                    sys.exit(1)

                template_file = os.path.join(run_base_dir, "templates", "etc",
                                             filename)
                cfg_file = os.path.join(run_base_dir, "etc", filename)
                configure.apply_tpl(template_file, cfg_file)
                logging.info(
                    "Client configuration file created : {0}".format(cfg_file))

                if os.path.isfile(cfg_link) and os.lstat(cfg_link):

                    try:
                        is_symlink_correct = os.path.samefile(
                            cfg_link, cfg_file)
                    except os.error:
                        # link is broken
                        is_symlink_correct = False

                    if not is_symlink_correct:
                        if args.force or configure.user_yes_no_query(
                            ("Do you want to update link to {0} user "
                             "configuration file ".format(product) +
                             "(currently pointing to {0}) with {1}?".format(
                                 os.path.realpath(cfg_link), cfg_file))):
                            os.remove(cfg_link)
                            os.symlink(cfg_file, cfg_link)
                        else:
                            logging.info("Client configuration unmodified. "
                                         "Exiting.")
                            sys.exit(1)

                else:

                    if product == configure.OPSIM:
                        # might need to create directory first
                        try:
                            os.makedirs(os.path.join(homedir, ".lsst"))
                            logging.debug("Creating client configuration "
                                          "directory : ~/.lsst")
                        except os.error:
                            pass

                    try:
                        os.remove(cfg_link)
                        logging.debug(
                            "Removing broken symbolic link : {0}".format(
                                cfg_link))
                    except os.error:
                        pass

                    os.symlink(cfg_file, cfg_link)

                logging.info("{0} is now pointing to : {1}".format(
                    cfg_link, cfg_file))
def main():

    args = parseArgs()

    logging.basicConfig(format='%(levelname)s: %(message)s',
                        level=args.verbose_level)

    logging.info("opsim configuration tool\n" +
                 "=======================================" +
                 "================================")

    opsim_dir = os.path.abspath(os.path.join(
                                os.path.dirname(os.path.realpath(__file__)),
                                ".."))

    if configure.PREPARE in args.step_list:

        if os.path.exists(args.opsim_run_dir):
            if args.force or configure.user_yes_no_query(
                    "WARNING : Do you want to erase all configuration" +
                    " data in {0} ?".format(args.opsim_run_dir)):
                shutil.rmtree(args.opsim_run_dir)
            else:
                logging.info("Stopping opsim configuration, please specify an "
                             "other configuration directory")
                sys.exit(1)

        in_config_dir = os.path.join(opsim_dir, "cfg")
        in_template_config_dir = os.path.join(in_config_dir, "templates")
        out_template_config_dir = os.path.join(args.opsim_run_dir, "templates")
        logging.info("Copying template configuration from {0} to {1}"
                     .format(in_template_config_dir, args.opsim_run_dir))
        shutil.copytree(in_template_config_dir, out_template_config_dir)

        in_meta_config_file = os.path.join(in_config_dir, "opsim-meta.conf")
        logging.info("Creating meta-configuration file: {0}"
                     .format(args.meta_config_file))
        params_dict = {
            'RUN_BASE_DIR': args.opsim_run_dir
        }
        configure.apply_tpl(in_meta_config_file, args.meta_config_file,
                            params_dict)

    def intersect(seq1, seq2):
        '''
        returns subset of seq1 which is contained in seq2 keeping original
        ordering of items
        '''
        seq2 = set(seq2)
        return [item for item in seq1 if item in seq2]

    def contains_configuration_step(step_list):
        return bool(intersect(step_list, configure.STEP_RUN_LIST))

    ###################################
    #
    # Running configuration procedure
    #
    ###################################
    if contains_configuration_step(args.step_list):
        try:
            logging.info("Reading meta-configuration file {0}"
                         .format(args.meta_config_file))
            config = commons.read_config(args.meta_config_file)

            # used in templates targets comments
            config['opsim']['meta_config_file'] = args.meta_config_file

        except ConfigParser.NoOptionError, exc:
            logging.fatal("Missing option in meta-configuration file: %s", exc)
            sys.exit(1)

        if configure.DIRTREE in args.step_list:
            logging.info("Defining main directory structure")
            configure.check_root_dirs()
            configure.check_root_symlinks()

        ##########################################
        #
        # Creating opsim services configuration
        # using templates and meta_config_file
        #
        ##########################################
        run_base_dir = config['opsim']['run_base_dir']
        if configure.ETC in args.step_list:
            logging.info("Creating configuration files in {0}"
                         .format(os.path.join(run_base_dir, "etc")) +
                         " and scripts in {0}"
                         .format(os.path.join(run_base_dir, "tmp")))
            template_root = os.path.join(run_base_dir, "templates")
            dest_root = os.path.join(run_base_dir)
            configure.apply_templates(template_root, dest_root)

        components_to_configure = intersect(args.step_list,
                                            configure.COMPONENTS)
        if len(components_to_configure) > 0:
            logging.info("Running configuration scripts")
            configuration_scripts_dir = os.path.join(run_base_dir, 'tmp',
                                                     'configure')

            for comp in components_to_configure:
                cfg_script = os.path.join(configuration_scripts_dir,
                                          comp + ".sh")
                if os.path.isfile(cfg_script):
                    commons.run_command([cfg_script])

            def client_cfg_from_tpl(product):
                homedir = os.path.expanduser("~")
                if product == configure.OPSIM:
                    filename = "opsim-client.conf"
                    cfg_link = os.path.join(homedir, ".lsst", "opsim.conf")
                elif product == configure.MYSQL:
                    filename = "my-client.cnf"
                    cfg_link = os.path.join(homedir, ".my.cnf")
                else:
                    logging.fatal("Unable to apply configuration template for "
                                  "product %s", product)
                    sys.exit(1)

                template_file = os.path.join(
                    run_base_dir, "templates", "etc", filename
                )
                cfg_file = os.path.join(
                    run_base_dir, "etc", filename
                )
                configure.apply_tpl(
                    template_file,
                    cfg_file
                )
                logging.info(
                    "Client configuration file created : {0}".format(cfg_file)
                )

                if os.path.isfile(cfg_link) and os.lstat(cfg_link):

                    try:
                        is_symlink_correct = os.path.samefile(cfg_link,
                                                              cfg_file)
                    except os.error:
                        # link is broken
                        is_symlink_correct = False

                    if not is_symlink_correct:
                        if args.force or configure.user_yes_no_query(
                            ("Do you want to update link to {0} user "
                             "configuration file ".format(product) +
                             "(currently pointing to {0}) with {1}?"
                             .format(os.path.realpath(cfg_link), cfg_file))):
                            os.remove(cfg_link)
                            os.symlink(cfg_file, cfg_link)
                        else:
                            logging.info("Client configuration unmodified. "
                                         "Exiting.")
                            sys.exit(1)

                else:

                    if product == configure.OPSIM:
                        # might need to create directory first
                        try:
                            os.makedirs(os.path.join(homedir, ".lsst"))
                            logging.debug("Creating client configuration "
                                          "directory : ~/.lsst")
                        except os.error:
                            pass

                    try:
                        os.remove(cfg_link)
                        logging.debug("Removing broken symbolic link : {0}"
                                      .format(cfg_link))
                    except os.error:
                        pass

                    os.symlink(cfg_file, cfg_link)

                logging.info(
                    "{0} is now pointing to : {1}".format(cfg_link, cfg_file)
                )

            if configure.MYSQL in args.step_list:
                client_cfg_from_tpl(configure.MYSQL)

            if configure.CLIENT in args.step_list:
                client_cfg_from_tpl(configure.OPSIM)
示例#3
0
def main():

    args = parseArgs()

    logging.basicConfig(format='%(levelname)s: %(message)s',
                        level=args.verbose_level)

    logging.info("opsim configuration tool\n" +
                 "=======================================" +
                 "================================")

    opsim_dir = os.path.abspath(
        os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))

    if configure.PREPARE in args.step_list:

        if os.path.exists(args.opsim_run_dir):
            if args.force or configure.user_yes_no_query(
                    "WARNING : Do you want to erase all configuration" +
                    " data in {0} ?".format(args.opsim_run_dir)):
                shutil.rmtree(args.opsim_run_dir)
            else:
                logging.info("Stopping opsim configuration, please specify an "
                             "other configuration directory")
                sys.exit(1)

        in_config_dir = os.path.join(opsim_dir, "cfg")
        in_template_config_dir = os.path.join(in_config_dir, "templates")
        out_template_config_dir = os.path.join(args.opsim_run_dir, "templates")
        logging.info("Copying template configuration from {0} to {1}".format(
            in_template_config_dir, args.opsim_run_dir))
        shutil.copytree(in_template_config_dir, out_template_config_dir)

        in_meta_config_file = os.path.join(in_config_dir, "opsim-meta.conf")
        logging.info("Creating meta-configuration file: {0}".format(
            args.meta_config_file))
        params_dict = {'RUN_BASE_DIR': args.opsim_run_dir}
        configure.apply_tpl(in_meta_config_file, args.meta_config_file,
                            params_dict)

    def intersect(seq1, seq2):
        '''
        returns subset of seq1 which is contained in seq2 keeping original
        ordering of items
        '''
        seq2 = set(seq2)
        return [item for item in seq1 if item in seq2]

    def contains_configuration_step(step_list):
        return bool(intersect(step_list, configure.STEP_RUN_LIST))

    ###################################
    #
    # Running configuration procedure
    #
    ###################################
    if contains_configuration_step(args.step_list):
        try:
            logging.info("Reading meta-configuration file {0}".format(
                args.meta_config_file))
            config = commons.read_config(args.meta_config_file)

            # used in templates targets comments
            config['opsim']['meta_config_file'] = args.meta_config_file

        except ConfigParser.NoOptionError, exc:
            logging.fatal("Missing option in meta-configuration file: %s", exc)
            sys.exit(1)

        if configure.DIRTREE in args.step_list:
            logging.info("Defining main directory structure")
            configure.check_root_dirs()
            configure.check_root_symlinks()

        ##########################################
        #
        # Creating opsim services configuration
        # using templates and meta_config_file
        #
        ##########################################
        run_base_dir = config['opsim']['run_base_dir']
        if configure.ETC in args.step_list:
            logging.info("Creating configuration files in {0}".format(
                os.path.join(run_base_dir, "etc")) +
                         " and scripts in {0}".format(
                             os.path.join(run_base_dir, "tmp")))
            template_root = os.path.join(run_base_dir, "templates")
            dest_root = os.path.join(run_base_dir)
            configure.apply_templates(template_root, dest_root)

        components_to_configure = intersect(args.step_list,
                                            configure.COMPONENTS)
        if len(components_to_configure) > 0:
            logging.info("Running configuration scripts")
            configuration_scripts_dir = os.path.join(run_base_dir, 'tmp',
                                                     'configure')

            for comp in components_to_configure:
                cfg_script = os.path.join(configuration_scripts_dir,
                                          comp + ".sh")
                if os.path.isfile(cfg_script):
                    commons.run_command([cfg_script])

            def client_cfg_from_tpl(product):
                homedir = os.path.expanduser("~")
                if product == configure.OPSIM:
                    filename = "opsim-client.conf"
                    cfg_link = os.path.join(homedir, ".lsst", "opsim.conf")
                elif product == configure.MYSQL:
                    filename = "my-client.cnf"
                    cfg_link = os.path.join(homedir, ".my.cnf")
                else:
                    logging.fatal(
                        "Unable to apply configuration template for "
                        "product %s", product)
                    sys.exit(1)

                template_file = os.path.join(run_base_dir, "templates", "etc",
                                             filename)
                cfg_file = os.path.join(run_base_dir, "etc", filename)
                configure.apply_tpl(template_file, cfg_file)
                logging.info(
                    "Client configuration file created : {0}".format(cfg_file))

                if os.path.isfile(cfg_link) and os.lstat(cfg_link):

                    try:
                        is_symlink_correct = os.path.samefile(
                            cfg_link, cfg_file)
                    except os.error:
                        # link is broken
                        is_symlink_correct = False

                    if not is_symlink_correct:
                        if args.force or configure.user_yes_no_query(
                            ("Do you want to update link to {0} user "
                             "configuration file ".format(product) +
                             "(currently pointing to {0}) with {1}?".format(
                                 os.path.realpath(cfg_link), cfg_file))):
                            os.remove(cfg_link)
                            os.symlink(cfg_file, cfg_link)
                        else:
                            logging.info("Client configuration unmodified. "
                                         "Exiting.")
                            sys.exit(1)

                else:

                    if product == configure.OPSIM:
                        # might need to create directory first
                        try:
                            os.makedirs(os.path.join(homedir, ".lsst"))
                            logging.debug("Creating client configuration "
                                          "directory : ~/.lsst")
                        except os.error:
                            pass

                    try:
                        os.remove(cfg_link)
                        logging.debug(
                            "Removing broken symbolic link : {0}".format(
                                cfg_link))
                    except os.error:
                        pass

                    os.symlink(cfg_file, cfg_link)

                logging.info("{0} is now pointing to : {1}".format(
                    cfg_link, cfg_file))

            if configure.MYSQL in args.step_list:
                client_cfg_from_tpl(configure.MYSQL)

            if configure.CLIENT in args.step_list:
                client_cfg_from_tpl(configure.OPSIM)
            def client_cfg_from_tpl(product):
                homedir = os.path.expanduser("~")
                if product == configure.OPSIM:
                    filename = "opsim-client.conf"
                    cfg_link = os.path.join(homedir, ".lsst", "opsim.conf")
                elif product == configure.MYSQL:
                    filename = "my-client.cnf"
                    cfg_link = os.path.join(homedir, ".my.cnf")
                else:
                    logging.fatal("Unable to apply configuration template for "
                                  "product %s", product)
                    sys.exit(1)

                template_file = os.path.join(
                    run_base_dir, "templates", "etc", filename
                )
                cfg_file = os.path.join(
                    run_base_dir, "etc", filename
                )
                configure.apply_tpl(
                    template_file,
                    cfg_file
                )
                logging.info(
                    "Client configuration file created : {0}".format(cfg_file)
                )

                if os.path.isfile(cfg_link) and os.lstat(cfg_link):

                    try:
                        is_symlink_correct = os.path.samefile(cfg_link,
                                                              cfg_file)
                    except os.error:
                        # link is broken
                        is_symlink_correct = False

                    if not is_symlink_correct:
                        if args.force or configure.user_yes_no_query(
                            ("Do you want to update link to {0} user "
                             "configuration file ".format(product) +
                             "(currently pointing to {0}) with {1}?"
                             .format(os.path.realpath(cfg_link), cfg_file))):
                            os.remove(cfg_link)
                            os.symlink(cfg_file, cfg_link)
                        else:
                            logging.info("Client configuration unmodified. "
                                         "Exiting.")
                            sys.exit(1)

                else:

                    if product == configure.OPSIM:
                        # might need to create directory first
                        try:
                            os.makedirs(os.path.join(homedir, ".lsst"))
                            logging.debug("Creating client configuration "
                                          "directory : ~/.lsst")
                        except os.error:
                            pass

                    try:
                        os.remove(cfg_link)
                        logging.debug("Removing broken symbolic link : {0}"
                                      .format(cfg_link))
                    except os.error:
                        pass

                    os.symlink(cfg_file, cfg_link)

                logging.info(
                    "{0} is now pointing to : {1}".format(cfg_link, cfg_file)
                )