Ejemplo n.º 1
0
def run_init():
    config = ConfigHelper()

    if config.is_present():
        do_override_input = input(
            'Do you want to override the existing config [y/n]?   ')
        while not (do_override_input == 'y' or do_override_input == 'n'):
            do_override_input = input('Unrecognized input. Try again:   ')

        if do_override_input == 'n':
            sys.exit(0)

    MailService(config).interactively_configure()

    dualis = DualisService(config)
    dualis.interactively_acquire_token()

    schedule = ScheduleService(config)
    is_schedule_activated = schedule.interactively_configure()

    print('Configuration finished and saved!')

    print('Fetching current states as base...')
    dualis.fetch_and_save_unchecked_state()
    if is_schedule_activated:
        schedule.fetch_and_save_unchecked_state()
    print('done!')

    print('  To set a cron-job for this program on your Unix-System:\n' +
          '    1. `crontab -e`\n' +
          '    2. Add `*/15 * * * * cd %s && python3 main.py`\n' %
          (os.path.dirname(os.path.realpath(__file__))) +
          '    3. Save and you\'re done!')

    print('All set and ready to go!')
Ejemplo n.º 2
0
def run_change_notification_mail():
    config = ConfigHelper()
    config.load()

    MailService(config).interactively_configure()

    print('Configuration successfully updated!')
Ejemplo n.º 3
0
def run_new_token():
    config = ConfigHelper()
    config.load()  # because we do not want to override the other settings

    DualisService(config).interactively_acquire_token()

    print('New Token successfully saved!')
Ejemplo n.º 4
0
    def __init__(self, host, host_ip=None, ssh_user=None, ssh_port=None, ssh_options=None):
        config_helper = ConfigHelper(host)
        self._cluster_interface = config_helper.get_cluster_interface()
        self._writer_vip_cidr = config_helper.get_writer_vip_cidr()
        self._writer_vip = config_helper.get_writer_vip()
        self._requires_sudo = config_helper.get_requires_sudo()

        self._ssh_client = SSHHelper(host, host_ip, ssh_user, ssh_port, ssh_options)
Ejemplo n.º 5
0
def run_main():
    logging.basicConfig(
        filename='DualisWatcher.log', level=logging.DEBUG,
        format='%(asctime)s  %(levelname)s  {%(module)s}  %(message)s', datefmt='%Y-%m-%d %H:%M:%S'
    )

    logging.info('--- main started ---------------------')
    if IS_DEBUG:
        logging.info('Debug-Mode detected. Errors will not be logged but instead re-risen.')
        debug_logger = logging.getLogger()
        debug_logger.setLevel(logging.ERROR)
        debug_logger.addHandler(ReRaiseOnError())

    try:
        logging.debug('Loading config...')
        config = ConfigHelper()
        config.load()
    except BaseException as e:
        logging.error('Error while trying to load the Configuration! Exiting...', extra={'exception':e})
        sys.exit(-1)

    mail_service = MailService(config)

    try:
        dualis = DualisService(config)

        logging.debug('Checking for changes for the configured Dualis-Account....')
        results = dualis.fetch_and_check_state()
        changes = results[0]
        course_names = results[1]

        if changes.diff_count > 0:
            logging.info('%s changes found for the configured Dualis-Account.'%(changes.diff_count))
            token = dualis.get_token()
            mail_service.notify_about_changes_in_results(changes, course_names, token)
            dualis.save_state()
        else:
            logging.info('No changes found for the configured Dualis-Account.')

        schedule = ScheduleService(config)
        if schedule.is_activated:
            logging.debug('Checking for changes for the configured Schedule...')
            changes = schedule.fetch_and_check_state()
            if len(changes) > 0:
                logging.info('%s changes found for the configured Schedule.'%(len(changes)))
                mail_service.notify_about_changes_in_schedule(changes, schedule.uid)
                schedule.save_state()
            else:
                logging.info('No changes found for the configured Schedule.')

        logging.debug('All done. Exiting...')
    except BaseException as e:
        error_formatted = traceback.format_exc()
        logging.error(error_formatted, extra={'exception':e})
        mail_service.notify_about_error(str(e))
        logging.debug('Exception-Handling completed. Exiting...', extra={'exception' : e})
        sys.exit(-1)
Ejemplo n.º 6
0
def reset_landscape_server():
    """
    Resets the landscaper server connection back to the one specified in the
    config file.
    """
    global HOST, PORT

    HOST = config.get("LANDSCAPE", "host")
    PORT = config.get("LANDSCAPE", "port")
Ejemplo n.º 7
0
def main():

    cfg_helper = ConfigHelper()
    cfg = cfg_helper.get_config()
    print cfg

    dmp_helper = DumpHelper()
    dump = dmp_helper.dump()
    print dump
Ejemplo n.º 8
0
def run_new_token(email='', password=''):
    config = ConfigHelper()
    config.load()  # because we do not want to override the other settings

    if (not email or not password):
        DualisService(config).interactively_acquire_token()
    else:
        DualisService(config).acquire_token(email, password)

    print('New Token successfully saved!')
Ejemplo n.º 9
0
def main():

    for set_name in ConfigHelper.get_datasets():

        MetricsHelper.reset_metrics()

        data, set_target = IOHelper.read_dataset(set_name)

        feats, labels = DataHelper.extract_feature_labels(data, set_target)
        DataHelper.create_label_mapping(labels)
        max_nb_feats = DataHelper.calculate_max_nb_features(feats)

        for e in range(ConfigHelper.nb_executions):
            start = time.time()
            print("Execution " + str(e))

            train_idxs, test_idxs = DataHelper.split_in_sets(feats, labels)

            train_X = DataHelper.select_rows(feats, train_idxs, copy=False)
            train_y = DataHelper.select_rows(labels, train_idxs, copy=False)
            test_X = DataHelper.select_rows(feats, test_idxs, copy=False)
            test_y = DataHelper.select_rows(labels, test_idxs, copy=False)

            for noise_level in ConfigHelper.noise_levels:

                noisy_idxs, noisy_train_y = DataHelper.insert_noise(
                    train_y, noise_level)

                for name, clf, clean_type in ConfigHelper.get_classifiers():

                    algorithm_data = ConfigHelper.choose_algorithm(
                        clf, clean_type, train_X, noisy_train_y, noisy_idxs,
                        max_nb_feats)

                    chosen_rate = algorithm_data[0]
                    chosen_threshold = algorithm_data[1]
                    chosen_X = algorithm_data[2]
                    chosen_y = algorithm_data[3]
                    chosen_clf = algorithm_data[4]
                    true_filtered = algorithm_data[5]
                    false_filtered = algorithm_data[6]

                    chosen_clf.fit(chosen_X, chosen_y)
                    predictions = chosen_clf.predict(test_X)
                    error = MetricsHelper.calculate_error_score(
                        test_y, predictions)

                    MetricsHelper.metrics.append([
                        set_name, e, noise_level, name, chosen_rate,
                        chosen_threshold, error, true_filtered, false_filtered
                    ])
            print(str(time.time() - start))

        IOHelper.store_results(MetricsHelper.convert_metrics_to_frame(),
                               "final_" + set_name)
Ejemplo n.º 10
0
def main():
    actions = {
        SteamvrUtils.Action.ON: ['on', '1'],
        SteamvrUtils.Action.OFF: ['off', '0'],
        SteamvrUtils.Action.DAEMON: ['daemon', 'd'],
        SteamvrUtils.Action.CONFIG_HELP: ['config-help', 'c']
    }

    parser = argparse.ArgumentParser()
    parser.add_argument('action',
                        choices=[
                            keyword for _, keywords in actions.items()
                            for keyword in keywords
                        ],
                        help='action to perform on the Base Stations')
    parser.add_argument(
        '--dry-run',
        help=
        'Do not modify anything (bluetooth connections are still made, but never used to write).',
        action='store_true')
    parser.add_argument('--config',
                        default=None,
                        help='Path to a config file.')
    parser.add_argument(
        '--version',
        action='version',
        version='steamvr_utils {version}'.format(version=__version__))
    args = parser.parse_args()

    config = Config(config_path=args.config, dry_run_overwrite=args.dry_run)

    log.initialise(config)

    # noinspection PyBroadException
    try:

        selected_action = None
        for action in SteamvrUtils.Action:
            if args.action in actions[action]:
                selected_action = action

        if selected_action == SteamvrUtils.Action.CONFIG_HELP:
            config_helper = ConfigHelper(config)
            config_helper.print_help()
            return

        log.i('steamvr_utils version: {}'.format(__version__))
        log.d('dry_run: {}'.format(config.dry_run()))

        steamvr_utils = SteamvrUtils(config=config)

        steamvr_utils.action(selected_action)
    except Exception:
        log.e('', exc_info=True)
        exit(1)
Ejemplo n.º 11
0
def run_change_schedule_watcher():
    config = ConfigHelper()
    config.load()

    schedule = ScheduleService(config)

    schedule.interactively_configure()
    print('Configuration successfully updated!')

    print('Fetching current states as base...')
    schedule.fetch_and_save_unchecked_state()
    print('done!')
Ejemplo n.º 12
0
def get_telemetry(telemetry=None, host=None, port=None):
    """
    Factory function which returns a telemetry class either using the arguments
    provided or from a configuration file.
    :param telemetry: The string name of the telemetry class to return.
    :param host: IP of the telemetry server.
    :param port: Port for the telemetry server
    :return: A telemetry class derived from the Telemetry abstract class.
    """
    if not telemetry:
        telemetry = ConfigHelper.get("DEFAULT", "telemetry")

    if telemetry == "snap":
        LOG.debug('snap telemetry')
        host = ConfigHelper.get('SNAP', 'host')
        port = ConfigHelper.get('SNAP', 'port')
        user = ConfigHelper.get('SNAP', 'user')
        password = ConfigHelper.get('SNAP', 'password')
        dbname = ConfigHelper.get('SNAP', 'dbname')
        return Snap(host, port, user, password, dbname)
    elif telemetry == "prometheus":
        LOG.debug('prometheus telemetry')
        host = ConfigHelper.get('PROMETHEUS', 'PROMETHEUS_HOST')
        port = ConfigHelper.get('PROMETHEUS', 'PROMETHEUS_PORT')
        return (host, port)
    else:
        msg = "No telemetry class for the type: {}".format(telemetry)
        raise AttributeError(msg)
Ejemplo n.º 13
0
    def __stop_ssh_command(self):
        try:
            self.orig_master_host = getattr(self, "orig_master_host")
            self.orig_master_config = ConfigHelper(self.orig_master_host)
        except Exception as e:
            print("Failed to read configuration for original master: %s" % str(e))
            return False

        # Original master
        try:
            orig_master_ip = getattr(self, "orig_master_ip", self.orig_master_host)
            orig_master_ssh_ip = getattr(self, "orig_master_ssh_ip", orig_master_ip)
            orig_master_ssh_port = getattr(self, "orig_master_ssh_port", None)
            orig_master_ssh_user = getattr(self, "ssh_user", None)
            orig_master_ssh_options = getattr(self, "ssh_options", None)
        except AttributeError as e:
            print("Failed to read one or more required original master parameter(s): %s" % str(e))
            return False

        try:
            if self.orig_master_config.get_manage_vip():
                vip_type = self.orig_master_config.get_vip_type()
                print("Removing the vip using the '%s' provider from the original master '%s'" %
                      (vip_type, self.orig_master_host))

                if not self.__remove_vip_from_host(vip_type, self.orig_master_host, orig_master_ssh_ip,
                                                   orig_master_ssh_user, orig_master_ssh_port, orig_master_ssh_options,
                                                   self.FAILOVER_TYPE_ONLINE):
                    return False
        except Exception as e:
            print("Unexpected error: %s" % str(e))
            return False

        return True
Ejemplo n.º 14
0
    def __status_command(self):
        try:
            self.orig_master_host = getattr(self, "orig_master_host")
            self.orig_master_config = ConfigHelper(self.orig_master_host)
        except Exception as e:
            print("Failed to read configuration for original master: %s" % str(e))
            return False

        # Original master
        try:
            orig_master_ip = getattr(self, "orig_master_ip", self.orig_master_host)
            orig_master_ssh_ip = getattr(self, "orig_master_ssh_ip", orig_master_ip)
            orig_master_ssh_port = getattr(self, "orig_master_ssh_port", None)
            orig_master_ssh_user = getattr(self, "ssh_user", None)
            orig_master_ssh_options = getattr(self, "ssh_options", None)
        except AttributeError as e:
            print("Failed to read one or more required original master parameter(s): %s" % str(e))
            return False

        try:
            if self.orig_master_config.get_manage_vip():
                vip_type = self.orig_master_config.get_vip_type()
                print("Checking the vip using the '%s' provider on the original master '%s'" %
                      (vip_type, self.orig_master_host))

                if not self.__check_vip_on_host(vip_type, self.orig_master_host, orig_master_ssh_ip,
                                                orig_master_ssh_user, orig_master_ssh_port, orig_master_ssh_options):
                    print("The VIP was not found on host %s" % self.orig_master_host)
                    return False
        except Exception as e:
            print("Unexpected error: %s" % str(e))
            return False

        return True
Ejemplo n.º 15
0
    def __stop_command(self):
        try:
            self.orig_master_host = getattr(self, "orig_master_host")
            self.orig_master_config = ConfigHelper(self.orig_master_host)
        except Exception as e:
            print("Failed to read configuration for original master: %s" % str(e))
            return False

        # Original master
        try:
            orig_master_ip = getattr(self, "orig_master_ip", self.orig_master_host)
            orig_master_mysql_port = getattr(self, "orig_master_port", None)
            orig_master_ssh_ip = getattr(self, "orig_master_ssh_ip", orig_master_ip)
            orig_master_ssh_port = getattr(self, "orig_master_ssh_port", None)
            orig_master_ssh_user = getattr(self, "orig_master_ssh_user", None)
            orig_master_mysql_user = self.__unescape_from_shell(getattr(self, "orig_master_user"))
            orig_master_mysql_pass = self.__unescape_from_shell(getattr(self, "orig_master_password"))
        except AttributeError as e:
            print("Failed to read one or more required original master parameter(s): %s" % str(e))
            return False

        # Setup MySQL connections
        mysql_orig_master = MySQLHelper(orig_master_ip, orig_master_mysql_port, orig_master_mysql_user,
                                        orig_master_mysql_pass)

        try:
            print("Connecting to mysql on the original master '%s'" % self.orig_master_host)
            if not mysql_orig_master.connect():
                return False

            if self.orig_master_config.get_manage_vip():
                vip_type = self.orig_master_config.get_vip_type()
                print("Removing the vip using the '%s' provider from the original master '%s'" %
                      (vip_type, self.orig_master_host))

                if not self.__remove_vip_from_host(vip_type, self.orig_master_host, orig_master_ssh_ip,
                                                   orig_master_ssh_user, orig_master_ssh_port,
                                                   self.FAILOVER_TYPE_ONLINE):
                    return False

            if self.orig_master_config.get_super_read_only() and mysql_orig_master.super_read_only_supported():
                print("Setting super_read_only to '1' on the original master '%s'" % self.orig_master_host)
                if not mysql_orig_master.set_super_read_only() or not mysql_orig_master.is_super_read_only():
                    return False
            else:
                print("Setting read_only to '1' on the original master '%s'" % self.orig_master_host)
                if not mysql_orig_master.set_read_only() or not mysql_orig_master.is_read_only():
                    return False

            if not self.__mysql_kill_threads(self.orig_master_host, mysql_orig_master):
                return False
        except Exception as e:
            print("Unexpected error: %s" % str(e))
            return False
        finally:
            print("Disconnecting from mysql on the original master '%s'" % self.orig_master_host)
            mysql_orig_master.disconnect()

        return True
Ejemplo n.º 16
0
 def __init__(self):
     PATH = lambda p: os.path.abspath(
         os.path.join(os.path.dirname(__file__), p))
     DB_PASS = ConfigHelper().get('DB_PASS')
     self.db = PooledMySQLDatabase('PersonalHomepage',
                                   user='******',
                                   password=DB_PASS,
                                   host='localhost',
                                   port=3306)
Ejemplo n.º 17
0
def run_new_token(email='', password=''):
    config = ConfigHelper()
    config.load()  # because we do not want to override the other settings

    logging.basicConfig(
        filename='DualisWatcher.log',
        level=logging.DEBUG,
        format='%(asctime)s  %(levelname)s  {%(module)s}  %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S')

    logging.info('--- run_new_token started ---------------------')

    if (not email or not password):
        DualisService(config).interactively_acquire_token()
    else:
        DualisService(config).acquire_token(email, password)

    print('New Token successfully saved!')
Ejemplo n.º 18
0
    def __status_command(self):
        try:
            self.orig_master_host = getattr(self, "orig_master_host")
            self.orig_master_config = ConfigHelper(self.orig_master_host)
        except Exception as e:
            print("Failed to read configuration for original master: %s" %
                  str(e))
            return False

        # Original master
        try:
            orig_master_ip = getattr(self, "orig_master_ip",
                                     self.orig_master_host)
            orig_master_ssh_ip = getattr(self, "orig_master_ssh_ip",
                                         orig_master_ip)
            orig_master_ssh_port = getattr(self, "orig_master_ssh_port", None)
            orig_master_ssh_user = getattr(self, "ssh_user", None)
            orig_master_ssh_options = getattr(self, "ssh_options", None)
        except AttributeError as e:
            print(
                "Failed to read one or more required original master parameter(s): %s"
                % str(e))
            return False

        try:
            if self.orig_master_config.get_manage_vip():
                vip_type = self.orig_master_config.get_vip_type()
                print(
                    "Checking the vip using the '%s' provider on the original master '%s'"
                    % (vip_type, self.orig_master_host))

                if not self.__check_vip_on_host(
                        vip_type, self.orig_master_host, orig_master_ssh_ip,
                        orig_master_ssh_user, orig_master_ssh_port,
                        orig_master_ssh_options):
                    print("The VIP was not found on host %s" %
                          self.orig_master_host)
                    return False
        except Exception as e:
            print("Unexpected error: %s" % str(e))
            return False

        return True
Ejemplo n.º 19
0
    def __rollback_stop_command(self):
        try:
            self.orig_master_host = getattr(self, "orig_master_host")
            self.orig_master_config = ConfigHelper(self.orig_master_host)
        except Exception as e:
            print("Failed to read configuration for original master: %s" % str(e))
            return False

        # Original master
        try:
            orig_master_ip = getattr(self, "orig_master_ip", self.orig_master_host)
            orig_master_mysql_port = getattr(self, "orig_master_port", None)
            orig_master_mysql_user = self.__unescape_from_shell(getattr(self, "orig_master_user"))
            orig_master_mysql_pass = self.__unescape_from_shell(getattr(self, "orig_master_password"))
            orig_master_ssh_ip = getattr(self, "orig_master_ssh_ip", orig_master_ip)
            orig_master_ssh_port = getattr(self, "orig_master_ssh_port", None)
            orig_master_ssh_user = getattr(self, "orig_master_ssh_user", None)
            orig_master_ssh_options = getattr(self, "ssh_options", None)
        except AttributeError as e:
            print("Failed to read one or more required original master parameter(s): %s" % str(e))
            return False

        # Setup MySQL connections
        mysql_orig_master = MySQLHelper(orig_master_ip, orig_master_mysql_port, orig_master_mysql_user,
                                        orig_master_mysql_pass)

        print("Rolling back the failover changes on the original master '%s'" % self.orig_master_host)
        try:
            if not mysql_orig_master.connect():
                print("Failed to connect to mysql on the original master '%s'" % self.orig_master_host)
                return False

            if not mysql_orig_master.unset_read_only() or mysql_orig_master.is_read_only():
                print("Failed to reset read_only to '0' on the original master '%s'" % self.orig_master_host)
                return False

            print("Set read_only back to '0' on the original master '%s'" % self.orig_master_host)

            if self.orig_master_config.get_manage_vip():
                vip_type = self.orig_master_config.get_vip_type()
                if not self.__add_vip_to_host(vip_type, self.orig_master_host, orig_master_ssh_ip, orig_master_ssh_user,
                                              orig_master_ssh_port, orig_master_ssh_options):
                    print("Failed to add back the vip using the '%s' provider to the original master '%s'" %
                          (vip_type, self.orig_master_host))
                    return False

                print("Added back the vip to the original master '%s'" % self.orig_master_host)
        except Exception as e:
            print("Unexpected error: %s" % str(e))
            return False
        finally:
            mysql_orig_master.disconnect()

        return True
Ejemplo n.º 20
0
 def __init__(self, key_id, secret_key, args=None):
     self.utility = Utility(args)
     self.args = args or self.utility.updated_hash()
     self.event_id_exist = True
     self.key_id = key_id
     self.secret_key = secret_key
     self.data_dir = os.path.join(os.path.dirname(__file__), os.pardir,
                                  'data')
     self.has_configdir = False
     self.first_batch = True
     self.config = ConfigHelper()
Ejemplo n.º 21
0
    def __stop_ssh_command(self):
        try:
            self.orig_master_host = getattr(self, "orig_master_host")
            self.orig_master_config = ConfigHelper(self.orig_master_host)
        except Exception as e:
            print("Failed to read configuration for original master: %s" %
                  str(e))
            return False

        # Original master
        try:
            orig_master_ip = getattr(self, "orig_master_ip",
                                     self.orig_master_host)
            orig_master_ssh_ip = getattr(self, "orig_master_ssh_ip",
                                         orig_master_ip)
            orig_master_ssh_port = getattr(self, "orig_master_ssh_port", None)
            orig_master_ssh_user = getattr(self, "ssh_user", None)
            orig_master_ssh_options = getattr(self, "ssh_options", None)
        except AttributeError as e:
            print(
                "Failed to read one or more required original master parameter(s): %s"
                % str(e))
            return False

        try:
            if self.orig_master_config.get_manage_vip():
                vip_type = self.orig_master_config.get_vip_type()
                print(
                    "Removing the vip using the '%s' provider from the original master '%s'"
                    % (vip_type, self.orig_master_host))

                if not self.__remove_vip_from_host(
                        vip_type, self.orig_master_host, orig_master_ssh_ip,
                        orig_master_ssh_user, orig_master_ssh_port,
                        orig_master_ssh_options, self.FAILOVER_TYPE_ONLINE):
                    return False
        except Exception as e:
            print("Unexpected error: %s" % str(e))
            return False

        return True
Ejemplo n.º 22
0
def main():
    helper = Helper()
    config_helper = ConfigHelper()
    config_helper.create_config()

    # REPLACE TOKENS IN FILES
    tokens = {
        "SUPER_SECRET_KEY": config_helper.get_encoded_secret(),
        "OPERATING_USER": getpass.getuser(),
        "HOME_DIRECTORY": parentdir
    }
    helper.prepare_file(snoopy_filename, tokens)

    tokens = {"HOME_DIRECTORY": parentdir}
    helper.prepare_file(sidecar_filename, tokens)

    tokens = {"HOME_DIRECTORY": '{}/src'.format(parentdir)}
    helper.prepare_file(snoopy_spec_filename, tokens)

    tokens = {"HOME_DIRECTORY": '{}/src'.format(parentdir)}
    helper.prepare_file(sidecar_spec_filename, tokens)

    # INSTALL PYTHON DEPENDENCIES

    # COMPILE
    helper.compile(snoopy_spec_filename)
    helper.compile(sidecar_spec_filename)

    # CLEANUP
    helper.replace_original(snoopy_filename)
    helper.replace_original(sidecar_filename)
    helper.replace_original(snoopy_spec_filename)
    helper.replace_original(sidecar_spec_filename)

    # INSTART
    print("starting daemon...")
    subprocess.check_output([start, snoopy_systemd_file, snoopy_systemd_name])
    subprocess.check_output(
        [start, sidecar_systemd_file, sidecar_systemd_name])
    print("finished!")
Ejemplo n.º 23
0
    def __init__(self, failover_type):
        self.failover_type = failover_type

        if not self.__validate_failover_type():
            raise ValueError

        # Setup configuration
        if not ConfigHelper.load_config():
            raise ValueError

        self.orig_master_host = None
        self.new_master_host = None

        self.orig_master_config = None
        self.new_master_config = None
Ejemplo n.º 24
0
def main():
    helper = Helper()
    config_helper = ConfigHelper()
    config_helper.create_config()
    # shutil.copy('mail.config', 'src/mail.config')

    # REPLACE TOKENS IN FILES
    tokens = {
        "SUPER_SECRET_KEY": config_helper.get_encoded_secret(),
        "HOME_DIRECTORY": parentdir
    }
    helper.prepare_file(snoopy_filename, tokens)
    tokens = {"HOME_DIRECTORY": '{}/src'.format(parentdir)}
    helper.prepare_file(snoopy_spec_filename, tokens)

    # INSTALL PYTHON DEPENDENCIES

    # COMPILE RELOADER -- needs to happen before we install snoopy
    print("compiling reloader...")
    subprocess.check_output(['gcc', 'src/darwin/create_launchd.c'])
    shutil.copy('a.out', 'src/a.out')

    # COMPILE PYTHON
    helper.compile(snoopy_spec_filename)

    # CLEANUP
    helper.replace_original(snoopy_filename)
    helper.replace_original(snoopy_spec_filename)

    # INSTART
    local_config_file = '{}/{}'.format(config_dir, plist_filename)
    runtime_config_file = '{}/{}'.format(launchd_path, plist_filename)
    print("starting daemon...")
    copy_plist(local_config_file, runtime_config_file, parentdir)
    load_plist(runtime_config_file)
    print("finished!")
Ejemplo n.º 25
0
    def __init__(self, failover_type):
        self.failover_type = failover_type

        if not self.__validate_failover_type():
            raise ValueError

        # Setup configuration
        if not ConfigHelper.load_config():
            raise ValueError

        self.orig_master_host = None
        self.new_master_host = None

        self.orig_master_config = None
        self.new_master_config = None
Ejemplo n.º 26
0
    def __init__(self,
                 host,
                 host_ip=None,
                 ssh_user=None,
                 ssh_port=None,
                 ssh_options=None):
        config_helper = ConfigHelper(host)
        self._cluster_interface = config_helper.get_cluster_interface()
        self._writer_vip_cidr = config_helper.get_writer_vip_cidr()
        self._writer_vip = config_helper.get_writer_vip()
        self._requires_sudo = config_helper.get_requires_sudo()
        self._requires_arping = config_helper.get_requires_arping()

        self._ssh_client = SSHHelper(host, host_ip, ssh_user, ssh_port,
                                     ssh_options)
Ejemplo n.º 27
0
def get_info_graph(info_graph=None, landscape=None):
    """
    Factory function which returns an info graph class either using the
    arguments provided or from a configuration file.
    :param info_graph: The name of the info graph to use.
    :param landscape: A graph of the landscape.
    :return: Returns info graph based on the infograph type supplied either
    directly or from the INI file.
    """
    if not info_graph:
        info_graph = ConfigHelper.get("DEFAULT", "iaas")

    if info_graph == "openstack":
        return OpenStackInfoGraph(landscape)
    else:
        msg = "No infograph subclass for type {}".format(info_graph)
        raise AttributeError(msg)
Ejemplo n.º 28
0
def main():
    cleanup(snoopy_service)
    cleanup(sidecar_service)
    home_directory = rootdir.replace('\\', '\\\\')

    helper = Helper()
    config_helper = ConfigHelper()
    config_helper.create_config()

    # REPLACE TOKENS

    tokens = {
        "SUPER_SECRET_KEY": config_helper.get_encoded_secret(),
        "HOME_DIRECTORY": home_directory
    }
    helper.prepare_file(snoopy_filename, tokens)

    tokens = {
        "SUPER_SECRET_KEY": config_helper.get_encoded_secret(),
        "HOME_DIRECTORY": home_directory
    }
    helper.prepare_file(sidecar_filename, tokens)

    tokens = {"HOME_DIRECTORY": r'{}\src'.format(home_directory)}
    helper.prepare_file(snoopy_spec_filename, tokens)

    tokens = {"HOME_DIRECTORY": r'{}\src'.format(home_directory)}
    helper.prepare_file(sidecar_spec_filename, tokens)

    # INSTALL PYTHON DEPENDENCIES

    # COMPILE PROGRAMS

    helper.compile(snoopy_spec_filename)
    helper.compile(sidecar_spec_filename)

    # CLEANUP
    helper.replace_original(snoopy_filename)
    helper.replace_original(sidecar_filename)
    helper.replace_original(snoopy_spec_filename)
    helper.replace_original(sidecar_spec_filename)

    # INSTART
    install(snoopy_service, snoopy)
    install(sidecar_service, sidecar)
    time.sleep(2)
    start(snoopy_service)
    start(sidecar_service)

    print("finished!")
Ejemplo n.º 29
0
    def init_app_conf(app_root_path):
        """
            get config from config file and load some item to memory

        Returns:
            False: load failed
            True: load success
        """
        logger_main = logging.getLogger(GlobalInfo.logger_main)
        try:
            app_config_file_path = app_root_path + '/config/app_config.conf'
            # trim Bom
            trim_file_bom(app_config_file_path)
            ConfigHelper().init(app_config_file_path)

            return True
        except:
            logger_main.error('init app config failed')
            logger_main.error(traceback.format_exc())
            return False
Ejemplo n.º 30
0
 def __init__(self, options=None):
     self.options = options or Options()
     self.product_version = ConfigHelper.get_product_version()
     self.event_reference = self.load_yaml_file(self.event_reference_file)
Ejemplo n.º 31
0
 def __init__(self, host):
     config_helper = ConfigHelper(host)
     self._smtp_host = config_helper.get_smtp_host()
     self._sender = "mha_helper@%s" % socket.getfqdn()
     self._receiver = config_helper.get_report_email()
Ejemplo n.º 32
0
"""
Graph Database Base class and factory.
"""
import time
import requests
from networkx.readwrite import json_graph
from config_helper import ConfigHelper as config
import infograph
import json
import os
import analytics_engine.common as common

LOG = common.LOG
# HOST = '10.1.24.14'
# PORT = 9001
config.get("LANDSCAPE", "host")
config.get("LANDSCAPE", "port")

def get_graph():
    """
    Retrieves the entire landscape graph.
    :return: Landscape as a networkx graph.
    """
    landscape = _get("/graph")
    landscape.raise_for_status()  # Raise an exception if we get an error.

    nx_graph = json_graph.node_link_graph(landscape.json())
    return infograph.get_info_graph(landscape=nx_graph)


def get_subgraph(node_id, timestamp=None, timeframe=0):
Ejemplo n.º 33
0
    def __start_command(self):
        try:
            self.orig_master_host = getattr(self, "orig_master_host")
            self.orig_master_config = ConfigHelper(self.orig_master_host)
        except Exception as e:
            print("Failed to read configuration for original master: %s" % str(e))
            return False

        try:
            self.new_master_host = getattr(self, "new_master_host")
            self.new_master_config = ConfigHelper(self.new_master_host)
        except Exception as e:
            print("Failed to read configuration for new master: %s" % str(e))
            return False

        # New master
        try:
            new_master_ip = getattr(self, "new_master_ip", self.new_master_host)
            new_master_mysql_port = getattr(self, "new_master_port", None)
            new_master_mysql_user = self.__unescape_from_shell(getattr(self, "new_master_user"))
            new_master_mysql_pass = self.__unescape_from_shell(getattr(self, "new_master_password"))
            new_master_ssh_ip = getattr(self, "new_master_ssh_ip", new_master_ip)
            new_master_ssh_port = getattr(self, "new_master_ssh_port", None)
            new_master_ssh_options = getattr(self, "ssh_options", None)

            if self.failover_type == self.FAILOVER_TYPE_HARD:
                new_master_ssh_user = getattr(self, "ssh_user", None)
            else:
                new_master_ssh_user = getattr(self, "new_master_ssh_user", None)
        except AttributeError as e:
            print("Failed to read one or more required new master parameter(s): %s" % str(e))
            return False

        # Setup MySQL connection
        mysql_new_master = MySQLHelper(new_master_ip, new_master_mysql_port, new_master_mysql_user,
                                       new_master_mysql_pass)

        try:
            print("Connecting to mysql on the new master '%s'" % self.new_master_host)
            if not mysql_new_master.connect():
                return False

            print("Setting read_only to '0' on the new master '%s'" % self.new_master_host)
            if not mysql_new_master.unset_read_only() or mysql_new_master.is_read_only():
                return False

            if self.new_master_config.get_manage_vip():
                vip_type = self.new_master_config.get_vip_type()
                print("Adding the vip using the '%s' provider to the new master '%s'" %
                      (vip_type, self.new_master_host))

                if not self.__add_vip_to_host(vip_type, self.new_master_host, new_master_ssh_ip, new_master_ssh_user,
                                              new_master_ssh_port, new_master_ssh_options):
                    return False
        except Exception as e:
            print("Unexpected error: %s" % str(e))
            return False
        finally:
            print("Disconnecting from mysql on the new master '%s'" % self.new_master_host)
            mysql_new_master.disconnect()

        return True
Ejemplo n.º 34
0
 def __init__(self, host, host_ip=None, ssh_user=None, ssh_port=None, ssh_options=None):
     config_helper = ConfigHelper(host)
     self._read_only_config = config_helper.get_read_only_config_file()
     self._super_read_only = config_helper.get_super_read_only()
     self._ssh_client = SSHHelper(host, host_ip, ssh_user, ssh_port, ssh_options)
Ejemplo n.º 35
0
class MHAHelper(object):
    FAILOVER_TYPE_ONLINE = 'online_failover'
    FAILOVER_TYPE_HARD = 'hard_failover'
    FAILOVER_STOP_CMD = 'stop'
    FAILOVER_STOPSSH_CMD = 'stopssh'
    FAILOVER_START_CMD = 'start'
    FAILOVER_STATUS_CMD = 'status'

    def __init__(self, failover_type):
        self.failover_type = failover_type

        if not self.__validate_failover_type():
            raise ValueError

        # Setup configuration
        if not ConfigHelper.load_config():
            raise ValueError

        self.orig_master_host = None
        self.new_master_host = None

        self.orig_master_config = None
        self.new_master_config = None

    def execute_command(self, **kwargs):
        for key, value in kwargs.iteritems():
            setattr(self, key, value)

        try:
            command = getattr(self, "command")
        except Exception as e:
            print("No command supplied: %s" % str(e))
            return False

        # Delegate the work to other functions
        if command == self.FAILOVER_STOP_CMD:
            if self.failover_type == self.FAILOVER_TYPE_ONLINE:
                if not self.__stop_command():
                    self.__rollback_stop_command()
                    return False
            elif self.failover_type == self.FAILOVER_TYPE_HARD:
                return self.__stop_hard_command()

            return True
        elif command == self.FAILOVER_STOPSSH_CMD:
            return self.__stop_ssh_command()
        elif command == self.FAILOVER_START_CMD:
            return self.__start_command()
        elif command == self.FAILOVER_STATUS_CMD:
            return self.__status_command()

        # If we reach here that means no valid command was provided so we return an error here
        return False

    def __validate_failover_type(self):
        return (self.failover_type == MHAHelper.FAILOVER_TYPE_ONLINE or
                self.failover_type == MHAHelper.FAILOVER_TYPE_HARD)

    def __stop_command(self):
        try:
            self.orig_master_host = getattr(self, "orig_master_host")
            self.orig_master_config = ConfigHelper(self.orig_master_host)
        except Exception as e:
            print("Failed to read configuration for original master: %s" % str(e))
            return False

        # Original master
        try:
            orig_master_ip = getattr(self, "orig_master_ip", self.orig_master_host)
            orig_master_mysql_port = getattr(self, "orig_master_port", None)
            orig_master_ssh_ip = getattr(self, "orig_master_ssh_ip", orig_master_ip)
            orig_master_ssh_port = getattr(self, "orig_master_ssh_port", None)
            orig_master_ssh_user = getattr(self, "orig_master_ssh_user", None)
            orig_master_ssh_options = getattr(self, "ssh_options", None)
            orig_master_mysql_user = self.__unescape_from_shell(getattr(self, "orig_master_user"))
            orig_master_mysql_pass = self.__unescape_from_shell(getattr(self, "orig_master_password"))
        except AttributeError as e:
            print("Failed to read one or more required original master parameter(s): %s" % str(e))
            return False

        # Setup MySQL connections
        mysql_orig_master = MySQLHelper(orig_master_ip, orig_master_mysql_port, orig_master_mysql_user,
                                        orig_master_mysql_pass)

        try:
            print("Connecting to mysql on the original master '%s'" % self.orig_master_host)
            if not mysql_orig_master.connect():
                return False

            if self.orig_master_config.get_manage_vip():
                vip_type = self.orig_master_config.get_vip_type()
                print("Removing the vip using the '%s' provider from the original master '%s'" %
                      (vip_type, self.orig_master_host))

                if not self.__remove_vip_from_host(vip_type, self.orig_master_host, orig_master_ssh_ip,
                                                   orig_master_ssh_user, orig_master_ssh_port, orig_master_ssh_options,
                                                   self.FAILOVER_TYPE_ONLINE):
                    return False

            if self.orig_master_config.get_super_read_only() and mysql_orig_master.super_read_only_supported():
                print("Setting super_read_only to '1' on the original master '%s'" % self.orig_master_host)
                if not mysql_orig_master.set_super_read_only() or not mysql_orig_master.is_super_read_only():
                    return False
            else:
                print("Setting read_only to '1' on the original master '%s'" % self.orig_master_host)
                if not mysql_orig_master.set_read_only() or not mysql_orig_master.is_read_only():
                    return False

            if not self.__mysql_kill_threads(self.orig_master_host, mysql_orig_master,
                                             self.orig_master_config.get_kill_after_timeout()):
                return False
        except Exception as e:
            print("Unexpected error: %s" % str(e))
            return False
        finally:
            print("Disconnecting from mysql on the original master '%s'" % self.orig_master_host)
            mysql_orig_master.disconnect()

        return True

    def __stop_hard_command(self):
        try:
            self.orig_master_host = getattr(self, "orig_master_host")
            self.orig_master_config = ConfigHelper(self.orig_master_host)
        except Exception as e:
            print("Failed to read configuration for original master: %s" % str(e))
            return False

        # Original master
        try:
            orig_master_ip = getattr(self, "orig_master_ip", self.orig_master_host)
            orig_master_ssh_ip = getattr(self, "orig_master_ssh_ip", orig_master_ip)
            orig_master_ssh_port = getattr(self, "orig_master_ssh_port", None)
            orig_master_ssh_options = getattr(self, "ssh_options", None)
        except AttributeError as e:
            print("Failed to read one or more required original master parameter(s): %s" % str(e))
            return False

        try:
            if self.orig_master_config.get_manage_vip():
                vip_type = self.orig_master_config.get_vip_type()
                print("Removing the vip using the '%s' provider from the original master '%s'" %
                      (vip_type, self.orig_master_host))

                if not self.__remove_vip_from_host(vip_type, self.orig_master_host, orig_master_ssh_ip, None,
                                                   orig_master_ssh_port, orig_master_ssh_options,
                                                   self.FAILOVER_TYPE_HARD):
                    return False
        except Exception as e:
            print("Unexpected error: %s" % str(e))
            return False

        return True

    def __stop_ssh_command(self):
        try:
            self.orig_master_host = getattr(self, "orig_master_host")
            self.orig_master_config = ConfigHelper(self.orig_master_host)
        except Exception as e:
            print("Failed to read configuration for original master: %s" % str(e))
            return False

        # Original master
        try:
            orig_master_ip = getattr(self, "orig_master_ip", self.orig_master_host)
            orig_master_ssh_ip = getattr(self, "orig_master_ssh_ip", orig_master_ip)
            orig_master_ssh_port = getattr(self, "orig_master_ssh_port", None)
            orig_master_ssh_user = getattr(self, "ssh_user", None)
            orig_master_ssh_options = getattr(self, "ssh_options", None)
        except AttributeError as e:
            print("Failed to read one or more required original master parameter(s): %s" % str(e))
            return False

        try:
            if self.orig_master_config.get_manage_vip():
                vip_type = self.orig_master_config.get_vip_type()
                print("Removing the vip using the '%s' provider from the original master '%s'" %
                      (vip_type, self.orig_master_host))

                if not self.__remove_vip_from_host(vip_type, self.orig_master_host, orig_master_ssh_ip,
                                                   orig_master_ssh_user, orig_master_ssh_port, orig_master_ssh_options,
                                                   self.FAILOVER_TYPE_ONLINE):
                    return False
        except Exception as e:
            print("Unexpected error: %s" % str(e))
            return False

        return True

    def __start_command(self):
        try:
            self.orig_master_host = getattr(self, "orig_master_host")
            self.orig_master_config = ConfigHelper(self.orig_master_host)
        except Exception as e:
            print("Failed to read configuration for original master: %s" % str(e))
            return False

        try:
            self.new_master_host = getattr(self, "new_master_host")
            self.new_master_config = ConfigHelper(self.new_master_host)
        except Exception as e:
            print("Failed to read configuration for new master: %s" % str(e))
            return False

        # New master
        try:
            new_master_ip = getattr(self, "new_master_ip", self.new_master_host)
            new_master_mysql_port = getattr(self, "new_master_port", None)
            new_master_mysql_user = self.__unescape_from_shell(getattr(self, "new_master_user"))
            new_master_mysql_pass = self.__unescape_from_shell(getattr(self, "new_master_password"))
            new_master_ssh_ip = getattr(self, "new_master_ssh_ip", new_master_ip)
            new_master_ssh_port = getattr(self, "new_master_ssh_port", None)
            new_master_ssh_options = getattr(self, "ssh_options", None)

            if self.failover_type == self.FAILOVER_TYPE_HARD:
                new_master_ssh_user = getattr(self, "ssh_user", None)
            else:
                new_master_ssh_user = getattr(self, "new_master_ssh_user", None)
        except AttributeError as e:
            print("Failed to read one or more required new master parameter(s): %s" % str(e))
            return False

        # Setup MySQL connection
        mysql_new_master = MySQLHelper(new_master_ip, new_master_mysql_port, new_master_mysql_user,
                                       new_master_mysql_pass)

        try:
            print("Connecting to mysql on the new master '%s'" % self.new_master_host)
            if not mysql_new_master.connect():
                return False

            print("Setting read_only to '0' on the new master '%s'" % self.new_master_host)
            if not mysql_new_master.unset_read_only() or mysql_new_master.is_read_only():
                return False

            if self.new_master_config.get_manage_vip():
                vip_type = self.new_master_config.get_vip_type()
                print("Adding the vip using the '%s' provider to the new master '%s'" %
                      (vip_type, self.new_master_host))

                if not self.__add_vip_to_host(vip_type, self.new_master_host, new_master_ssh_ip, new_master_ssh_user,
                                              new_master_ssh_port, new_master_ssh_options):
                    return False
        except Exception as e:
            print("Unexpected error: %s" % str(e))
            return False
        finally:
            print("Disconnecting from mysql on the new master '%s'" % self.new_master_host)
            mysql_new_master.disconnect()

        return True

    def __status_command(self):
        try:
            self.orig_master_host = getattr(self, "orig_master_host")
            self.orig_master_config = ConfigHelper(self.orig_master_host)
        except Exception as e:
            print("Failed to read configuration for original master: %s" % str(e))
            return False

        # Original master
        try:
            orig_master_ip = getattr(self, "orig_master_ip", self.orig_master_host)
            orig_master_ssh_ip = getattr(self, "orig_master_ssh_ip", orig_master_ip)
            orig_master_ssh_port = getattr(self, "orig_master_ssh_port", None)
            orig_master_ssh_user = getattr(self, "ssh_user", None)
            orig_master_ssh_options = getattr(self, "ssh_options", None)
        except AttributeError as e:
            print("Failed to read one or more required original master parameter(s): %s" % str(e))
            return False

        try:
            if self.orig_master_config.get_manage_vip():
                vip_type = self.orig_master_config.get_vip_type()
                print("Checking the vip using the '%s' provider on the original master '%s'" %
                      (vip_type, self.orig_master_host))

                if not self.__check_vip_on_host(vip_type, self.orig_master_host, orig_master_ssh_ip,
                                                orig_master_ssh_user, orig_master_ssh_port, orig_master_ssh_options):
                    print("The VIP was not found on host %s" % self.orig_master_host)
                    return False
        except Exception as e:
            print("Unexpected error: %s" % str(e))
            return False

        return True

    def __rollback_stop_command(self):
        try:
            self.orig_master_host = getattr(self, "orig_master_host")
            self.orig_master_config = ConfigHelper(self.orig_master_host)
        except Exception as e:
            print("Failed to read configuration for original master: %s" % str(e))
            return False

        # Original master
        try:
            orig_master_ip = getattr(self, "orig_master_ip", self.orig_master_host)
            orig_master_mysql_port = getattr(self, "orig_master_port", None)
            orig_master_mysql_user = self.__unescape_from_shell(getattr(self, "orig_master_user"))
            orig_master_mysql_pass = self.__unescape_from_shell(getattr(self, "orig_master_password"))
            orig_master_ssh_ip = getattr(self, "orig_master_ssh_ip", orig_master_ip)
            orig_master_ssh_port = getattr(self, "orig_master_ssh_port", None)
            orig_master_ssh_user = getattr(self, "orig_master_ssh_user", None)
            orig_master_ssh_options = getattr(self, "ssh_options", None)
        except AttributeError as e:
            print("Failed to read one or more required original master parameter(s): %s" % str(e))
            return False

        # Setup MySQL connections
        mysql_orig_master = MySQLHelper(orig_master_ip, orig_master_mysql_port, orig_master_mysql_user,
                                        orig_master_mysql_pass)

        print("Rolling back the failover changes on the original master '%s'" % self.orig_master_host)
        try:
            if not mysql_orig_master.connect():
                print("Failed to connect to mysql on the original master '%s'" % self.orig_master_host)
                return False

            if not mysql_orig_master.unset_read_only() or mysql_orig_master.is_read_only():
                print("Failed to reset read_only to '0' on the original master '%s'" % self.orig_master_host)
                return False

            print("Set read_only back to '0' on the original master '%s'" % self.orig_master_host)

            if self.orig_master_config.get_manage_vip():
                vip_type = self.orig_master_config.get_vip_type()
                if not self.__add_vip_to_host(vip_type, self.orig_master_host, orig_master_ssh_ip, orig_master_ssh_user,
                                              orig_master_ssh_port, orig_master_ssh_options):
                    print("Failed to add back the vip using the '%s' provider to the original master '%s'" %
                          (vip_type, self.orig_master_host))
                    return False

                print("Added back the vip to the original master '%s'" % self.orig_master_host)
        except Exception as e:
            print("Unexpected error: %s" % str(e))
            return False
        finally:
            mysql_orig_master.disconnect()

        return True

    @classmethod
    def __remove_vip_from_host(cls, vip_type, host, host_ip, ssh_user, ssh_port, ssh_options, failover_type):
        if vip_type == ConfigHelper.VIP_PROVIDER_TYPE_METAL:
            vip_helper = VIPMetalHelper(host, host_ip, ssh_user, ssh_port, ssh_options)

            # If this is a hard failover and we cannot connect to the original master over SSH then we cannot really do
            # anything here at the moment.
            # TODO: At the moment we are not doing anything here but we would probably want to do something here
            if failover_type == cls.FAILOVER_TYPE_HARD:
                return True

            if not vip_helper.remove_vip():
                return False
        elif vip_type == ConfigHelper.VIP_PROVIDER_TYPE_AWS:
            pass
        elif vip_type == ConfigHelper.VIP_PROVIDER_TYPE_OS:
            pass
        else:
            # There are no other vip providers apart from what we are testing for above. Hence we throw an
            # error here
            return False

        return True

    @classmethod
    def __add_vip_to_host(cls, vip_type, host, host_ip, ssh_user, ssh_port, ssh_options):
        if vip_type == ConfigHelper.VIP_PROVIDER_TYPE_METAL:
            vip_helper = VIPMetalHelper(host, host_ip, ssh_user, ssh_port, ssh_options)
            if not vip_helper.assign_vip():
                return False
        elif vip_type == ConfigHelper.VIP_PROVIDER_TYPE_AWS:
            pass
        elif vip_type == ConfigHelper.VIP_PROVIDER_TYPE_OS:
            pass
        else:
            # There are no other vip providers apart from what we are testing for above. Hence we throw an
            # error here
            return False

        return True

    @classmethod
    def __check_vip_on_host(cls, vip_type, host, host_ip, ssh_user, ssh_port, ssh_options):
        if vip_type == ConfigHelper.VIP_PROVIDER_TYPE_METAL:
            return VIPMetalHelper(host, host_ip, ssh_user, ssh_port, ssh_options).has_vip()
        elif vip_type == ConfigHelper.VIP_PROVIDER_TYPE_AWS:
            pass
        elif vip_type == ConfigHelper.VIP_PROVIDER_TYPE_OS:
            pass
        else:
            # There are no other vip providers apart from what we are testing for above. Hence we throw an
            # error here
            return False

        return True

    @classmethod
    def __mysql_kill_threads(cls, host, mysql_connection, timeout):
        sleep_interval = 0.1
        start = datetime.datetime.now()

        print("Waiting %d seconds for application threads to disconnect from the MySQL server '%s'" % (timeout, host))
        while True:
            try:
                mysql_threads = cls.__get_mysql_threads_list(mysql_connection)
                if len(mysql_threads) < 1:
                    break
            except Exception as e:
                print("Unexpected error: %s" % str(e))
                return False

            time.sleep(sleep_interval)
            now = datetime.datetime.now()
            if (now - start).seconds > timeout:
                break

        print("Terminating all application threads connected to the MySQL server '%s'" % host)
        try:
            for thread in iter(cls.__get_mysql_threads_list(mysql_connection)):
                print("Terminating thread Id => %s, User => %s, Host => %s" %
                      (thread['Id'], thread['User'], thread['Host']))
                mysql_connection.kill_connection(thread['Id'])
        except Exception as e:
            print("Unexpected error: %s" % str(e))
            return False

        return True

    @classmethod
    def __get_mysql_threads_list(cls, mysql_connection):
        threads = list()
        try:
            for row in mysql_connection.get_processlist():
                if (mysql_connection.get_connection_id() == row['Id'] or row['Command'] == "Binlog Dump" or
                        row['Command'] == "Binlog Dump GTID" or row['User'] == "system user"):
                    continue
                threads.append(row)
        except Exception as e:
            print("Failed to get list of processes from MySQL: %s" % str(e))
            return False

        return threads

    @classmethod
    def __unescape_from_shell(cls, escaped):
        # This matches with mha4mysql-node::NodeUtil.pm::@shell_escape_chars
        # username and password provided by MHA are escaped like this
        unescaped = re.sub(r'\\(?!\\)', '', escaped)

        return unescaped
Ejemplo n.º 36
0
    headers = get_standard_headers(auth_token)

    building_id = args['buildingID']

    r = requests.get(f'{host}v1/locations/{building_id}', headers=headers)
    r.raise_for_status()

    data = r.json()

    location = data['data']

    return location


def fetch_locations(host, auth_token, args):
    if args['buildingID'] is not None:
        return fetch_single_location(host, auth_token, args)
    else:
        return fetch_locations_batch(host, auth_token, args)


args = parse_arguments()

cred_helper = ConfigHelper(args['config_file'])

host = cred_helper.get_host_url()
auth_token = cred_helper.get_access_token()

locations = fetch_locations(host, auth_token, args)
print(json.dumps(locations))
Ejemplo n.º 37
0
#coding=utf-8
import os
import sys
import time
import smtplib
from email.utils import formataddr
from email.mime.text import MIMEText
try:
    from ..config_helper import ConfigHelper
except:
    sys.path.append('../')
    sys.path.append('../../')
    from config_helper import ConfigHelper

SENDER = ConfigHelper().get('SENDER')
PASSWORD = ConfigHelper().get('PASSWORD')


class Mail(object):
    global SENDER
    global PASSWORD

    def __init__(self, sender_name, subject, content, my_receiver, my_sender=SENDER, my_pass=PASSWORD):
        self.my_sender = my_sender
        self.my_pass = my_pass  #口令,不是密码,通常为16位字符串
        self.sender_name = sender_name
        self.receiver_addr = my_receiver
        self.subject = subject
        self.content = content

    def send(self):
                                              train_y,
                                              ConfigHelper.max_nb_features,
                                              is_train=True)

    test_X = IOHelper.read_dataset("test")

    DataHelper.add_nan_indication_cols(test_X)
    DataHelper.remove_high_nan_rate_cols(test_X, True)
    DataHelper.remove_small_variance_cols(test_X, True)

    DataHelper.fill_missing_data(test_X, is_train=False)
    test_X = DataHelper.split_categorical_cols(test_X, is_train=False)
    DataHelper.scale_continuous_cols(test_X, is_train=False)
    DataHelper.select_best_features(test_X,
                                    train_X,
                                    None,
                                    ConfigHelper.max_nb_features,
                                    is_train=False)
    DataHelper.reset_scaler()

    for name, model in ConfigHelper.get_submission_models():

        print "Training"
        model.fit(train_X, train_y)

        print "Predicting"
        probs = model.predict_proba(test_X)
        submission = MetricsHelper.get_submission(test_X.index, probs)

        print "Saving submission"
        IOHelper.store_submission(submission, name)
Ejemplo n.º 39
0
def run_main():
    logging.basicConfig(
        filename='DualisWatcher.log',
        level=logging.DEBUG,
        format='%(asctime)s  %(levelname)s  {%(module)s}  %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S')

    logging.info('--- main started ---------------------')
    if IS_DEBUG:
        logging.info(
            'Debug-Mode detected. Errors will not be logged but instead re-risen.'
        )
        debug_logger = logging.getLogger()
        debug_logger.setLevel(logging.ERROR)
        debug_logger.addHandler(ReRaiseOnError())

    try:
        logging.debug('Loading config...')
        config = ConfigHelper()
        config.load()
    except BaseException as e:
        logging.error(
            'Error while trying to load the Configuration! Exiting...',
            extra={'exception': e})
        sys.exit(-1)

    r_client = None
    try:
        sentry_dsn = config.get_property('sentry_dsn')
        if sentry_dsn:
            r_client = RavenClient(sentry_dsn,
                                   auto_log_stacks=True,
                                   release=fetch_git_sha(
                                       os.path.dirname(__file__)))
    except BaseException:
        pass

    mail_service = MailService(config)

    try:
        dualis = DualisService(config)

        logging.debug(
            'Checking for changes for the configured Dualis-Account....')
        results = dualis.fetch_and_check_state()
        changes = results[0]
        course_names = results[1]

        if changes.diff_count > 0:
            logging.info(
                '%s changes found for the configured Dualis-Account.' %
                (changes.diff_count))
            mail_service.notify_about_changes_in_results(changes, course_names)
            dualis.save_state()
        else:
            logging.info('No changes found for the configured Dualis-Account.')

        schedule = ScheduleService(config)
        if schedule.is_activated:
            logging.debug(
                'Checking for changes for the configured Schedule...')
            changes = schedule.fetch_and_check_state()
            if len(changes) > 0:
                logging.info('%s changes found for the configured Schedule.' %
                             (len(changes) - 1))
                mail_service.notify_about_changes_in_schedule(
                    changes, schedule.uid)
                schedule.save_state()
            else:
                logging.info('No changes found for the configured Schedule.')

        logging.debug('All done. Exiting...')
    except DualisSleepingError:
        logging.info('Dualis is sleeping, exiting and soon trying again.')
        sys.exit(-1)
    except BaseException as e:
        error_formatted = traceback.format_exc()
        logging.error(error_formatted, extra={'exception': e})

        if r_client:
            r_client.captureException(exec_info=True)

        mail_service.notify_about_error(str(e))

        logging.debug('Exception-Handling completed. Exiting...',
                      extra={'exception': e})
        sys.exit(-1)
Ejemplo n.º 40
0
# -*- coding:utf-8 -*-
import os
from logger_helper import LoggerHelper
from config_helper import ConfigHelper
from register_service import RegisterCenter

if __name__ == "__main__":
    host = ConfigHelper().getConfig("zookeeper", "host")
    port = ConfigHelper().getConfig("zookeeper", "port")
    rs = RegisterCenter("{}:{}".format(host, port))
    rs.connect()
    try:
        rs.service_register("hello_provider", "provider", {
            "host": "127.0.0.1",
            "port": 8000
        })
        rs.service_register("hello_provider", "provider", {
            "host": "127.0.0.1",
            "port": 8001
        })
        rs.service_register("hello_provider", "provider", {
            "host": "127.0.0.1",
            "port": 8002
        })
        LoggerHelper().info(rs.get_register("hello_provider", "provider"))
        LoggerHelper().info("****")
        LoggerHelper().info(rs.get("/hello_provider/provider/ID0000000002"))
        rs.set("/hello_provider/provider/ID0000000002", b"123")
        LoggerHelper().info(rs.get("/hello_provider/provider/ID0000000002"))
        LoggerHelper().info(rs.get_children("/hello_provider/provider"))
        os.system("pause")