コード例 #1
0
def create_crontab_basic(basic_minutes):
    basic_cron = CronTab(user=True)
    basic_command = const.CRONTAB_BASIC_COMMAND
    basic_comment = const.CRONTAB_BASIC_COMMENT
    # need to find command first
    basic_iter = basic_cron.find_command(basic_command)
    # if exist, remove first
    for basic_exist_job in basic_iter:
        basic_cron.remove(basic_exist_job)
    basic_job = basic_cron.new(command=basic_command, comment=basic_comment)
    basic_job.minute.every(basic_minutes)
    logger.info(
        "add natrix basic keep alive crontab successfully, please use \"crontab -l\" to check"
    )
    basic_job.enable()
    basic_cron.write()
コード例 #2
0
def create_crontab_advance(advance_minutes):
    advance_cron = CronTab(user=True)
    advance_command = const.CRONTAB_ADVANCE_COMMAND
    advance_comment = const.CRONTAB_ADVANCE_COMMENT
    # need to find command first
    advance_iter = advance_cron.find_command(advance_command)
    # if exist, remove first
    for advance_exist_job in advance_iter:
        advance_cron.remove(advance_exist_job)
    advance_job = advance_cron.new(command=advance_command,
                                   comment=advance_comment)
    advance_job.minute.every(advance_minutes)
    logger.info(
        "add natrix advance keep alive crontab successfully, please use \"crontab -l\" to check"
    )
    advance_job.enable()
    advance_cron.write()
コード例 #3
0
def create_crontab_reboot(reboot_hours, reboot_minutes):
    reboot_cron = CronTab(user=True)
    reboot_command = const.CRONTAB_REBOOT_COMMAND
    reboot_comment = const.CRONTAB_REBOOT_COMMENT
    # need to find comment first
    reboot_iter = reboot_cron.find_comment(reboot_comment)
    # if exist, remove first
    for reboot_exist_job in reboot_iter:
        reboot_cron.remove(reboot_exist_job)
    reboot_job = reboot_cron.new(command=reboot_command,
                                 comment=reboot_comment)
    reboot_job.hour.on(reboot_hours)
    reboot_job.minute.on(reboot_minutes)
    logger.info(
        "add natrix reboot crontab successfully, please use \"crontab -l\" to check"
    )
    reboot_job.enable()
    reboot_cron.write()
コード例 #4
0
def parse_crontab_clean(args):
    clean_cron = CronTab(user=True)
    # basic
    basic_command = const.CRONTAB_BASIC_COMMAND
    basic_iter = clean_cron.find_command(basic_command)
    for basic_exist_job in basic_iter:
        clean_cron.remove(basic_exist_job)
    # advance
    advance_command = const.CRONTAB_ADVANCE_COMMAND
    advance_iter = clean_cron.find_command(advance_command)
    for advance_exist_job in advance_iter:
        clean_cron.remove(advance_exist_job)
    # reboot
    reboot_comment = const.CRONTAB_REBOOT_COMMENT
    reboot_iter = clean_cron.find_comment(reboot_comment)
    for reboot_exist_job in reboot_iter:
        clean_cron.remove(reboot_exist_job)
    logger.info(
        "clean natrix crontab successfully, please use \"crontab -l\" to check"
    )
    clean_cron.write()
コード例 #5
0
def parse_service_init(args):
    logger.info("=" * SEPARATOR_COUNT)
    logger.info("initializing natrixclient services ......")
    # 提升到root权限
    if os.geteuid():
        logger.error("must be root or have sudo authorization")
        exit(101)

    init_service_etc()
    init_service_log()
    init_service_systemctl()

    logger.info("\nsuccessfully initialized natrix client services")
    logger.info("=" * SEPARATOR_COUNT)
コード例 #6
0
def init_service_systemctl():
    systemd_dir = "/etc/systemd/system/"
    logger.debug("\n4. copying sample systemd files to {}".format(systemd_dir))
    sample_systemd_dir = os.path.dirname(os.path.realpath(__file__)) + "/../conf/etc/systemd/system/"
    for sample_systemd_file in os.listdir(sample_systemd_dir):
        systemd_path = systemd_dir + sample_systemd_file
        if os.path.isfile(systemd_path):
            logger.info("systemd file {} already exists in {}".format(sample_systemd_file, systemd_dir))
            bak_time = time.strftime("%Y%m%d%H%M%S", time.localtime())
            bak_systemd_path = systemd_path + "." + str(bak_time)
            logger.info("backup original systemd file from {} to {}".format(systemd_path, bak_systemd_path))
            os.rename(systemd_path, bak_systemd_path)
        sample_systemd_path = sample_systemd_dir + sample_systemd_file
        logger.info("copying systemd sample file from {} to {} ......".format(sample_systemd_path, systemd_path))
        shutil.copyfile(sample_systemd_path, systemd_path)

    logger.debug("\n5. reloading systemd daemon service")
    # must add shell=True
    daemon_reload_command = "systemctl daemon-reload"
    daemon_reload_process = subprocess.Popen(daemon_reload_command,
                                             stdin=subprocess.PIPE,
                                             stdout=subprocess.PIPE,
                                             stderr=subprocess.PIPE,
                                             shell=True)
    # communicate() returns a tuple (stdout, stderr)
    daemon_reload_result = daemon_reload_process.communicate()
    logger.debug("daemon reload command \"{}\" result: {}".format(daemon_reload_command,
                                                                  daemon_reload_result))
コード例 #7
0
def init_service_log():
    log_dir = "/var/log/natrixclient/"
    logger.info("\n3. creating log directory {} ......".format(log_dir))
    if os.path.isdir(log_dir):
        logger.info("log directory {} exist".format(log_dir))
    else:
        logger.info("creating log directory {}".format(log_dir))
        os.makedirs(log_dir)
コード例 #8
0
def parse_service_start(args):
    logger.info("=" * SEPARATOR_COUNT)
    logger.info("starting natrixclient services ......")
    # systemd service files
    logger.info("\n1. checking natrixclient systemd service files ......")
    systemd_paths = ["/etc/systemd/system/natrixclient.service"]
    for systemd_path in systemd_paths:
        logger.info("checking natrixclient systemd file {}".format(systemd_path))
        if os.path.isfile(systemd_path):
            logger.debug("natrixclient systemd file {} exist".format(systemd_path))
        else:
            logger.error("natrixclient systemd file {} not exist, please execute \"natrixclient service init\" first!!!")
            exit(201)
    # systemd daemon files
    logger.info("\n2. checking natrixclient systemd daemon files ......")
    # TODO: CHANGE
    daemon_paths = ["/etc/natrixclient/natrixclient.daemon"]
    for daemon_path in daemon_paths:
        logger.info("checking natrixclient daemon service file {}".format(daemon_path))
        if os.path.isfile(daemon_path):
            logger.debug("natrixclient daemon service file {} exist".format(daemon_path))
        else:
            logger.error("natrixclient daemon service file {} not exist, "
                         "please execute \"natrixclient service init\" first!!!".format(daemon_path))
            exit(202)
    # start systemd service
    logger.info("\n3. starting natrixclient services ......")
    services = ["natrixclient.service"]
    for service in services:
        logger.debug("starting systemd service {}".format(service))
        # must add shell=True
        service_start_command = "systemctl start " + service
        service_start_process = subprocess.Popen(service_start_command,
                                                 stdin=subprocess.PIPE,
                                                 stdout=subprocess.PIPE,
                                                 stderr=subprocess.PIPE,
                                                 shell=True)
        # communicate() returns a tuple (stdout, stderr)
        service_start_result = service_start_process.communicate()
        logger.debug("service start command: \"{}\" \nresult: \"{}\"".format(service_start_command,
                                                                             service_start_result))
    # # add crontab job
    # # add keep alive basic crontab job
    # logger.info("\n4. adding keep alive basic crontab job ......")
    # basic_minutes = const.CRONTAB_BASIC_MINUTES
    # create_crontab_basic(basic_minutes)
    # # add keep alive advance crontab job
    # logger.info("\n5. adding keep alive advance crontab job ......")
    # advance_minutes = const.CRONTAB_ADVANCE_MINUTES
    # create_crontab_advance(advance_minutes)

    # add reboot at midnight crontab job
    logger.info("\n4. adding reboot at midnight crontab job ......")
    reboot_hours = const.CRONTAB_REBOOT_HOURS
    reboot_minutes = const.CRONTAB_REBOOT_MINUTES
    create_crontab_reboot(reboot_hours, reboot_minutes)

    logger.info("\nsuccessfully started natrix client services")
    logger.info("=" * SEPARATOR_COUNT)
コード例 #9
0
def parse_service_status(args):
    logger.info("=" * SEPARATOR_COUNT)
    logger.info("checking natrixclient services status......")
    # systemd service
    logger.info("\n1. checking systemd services status ......")
    services = ["natrixclient.service"]
    for service in services:
        logger.debug("checking service {} status".format(service))
        service_status_command = "systemctl status " + service
        service_status_process = subprocess.Popen(service_status_command,
                                                  stdin=subprocess.PIPE,
                                                  stdout=subprocess.PIPE,
                                                  stderr=subprocess.PIPE, shell=True)
        service_status_result = service_status_process.communicate()
        # logger.debug("service status command \"{}\" result: {}".format(service_status_command, service_status_result))
        service_status_string = service_status_result[0].decode()
        logger.info(service_status_string)
    # crontab -l
    logger.info("\n2. checking crontab jobs status")
    crontab_status_command = "crontab -l"
    crontab_status_process = subprocess.Popen(crontab_status_command,
                                              stdin=subprocess.PIPE,
                                              stdout=subprocess.PIPE,
                                              stderr=subprocess.PIPE,
                                              shell=True)
    crontab_status_result = crontab_status_process.communicate()
    # logger.debug("crontab status command \"{}\" result: {}".format(crontab_status_command, crontab_status_result))
    crontab_status_string = crontab_status_result[0].decode()
    logger.info(crontab_status_string)
    logger.info("\nsuccessfully checking natrixclient status")
    logger.info("=" * SEPARATOR_COUNT)
コード例 #10
0
def parse_service_disable(args):
    logger.info("=" * SEPARATOR_COUNT)
    logger.info("disabling natrixclient services ......")
    # stop service
    # start systemd service
    logger.info("\n1. disabling systemd services ......")
    services = ["natrixclient.service"]
    for service in services:
        logger.debug("disabling systemd service {}".format(service))
        # must add shell=True
        service_disable_command = "systemctl disable " + service
        service_disable_process = subprocess.Popen(service_disable_command,
                                                   stdin=subprocess.PIPE,
                                                   stdout=subprocess.PIPE,
                                                   stderr=subprocess.PIPE,
                                                   shell=True)
        # communicate() returns a tuple (stdout, stderr)
        service_disable_result = service_disable_process.communicate()
        logger.debug("service disable command: \"{}\" \nresult: \"{}\"".format(service_disable_command, service_disable_result))
    # clean crontab
    logger.info("\n2. clean all natrixclient crontab jobs ......")
    parse_crontab_clean(None)
    logger.info("\nsuccessfully disabled natrixclient services")
    logger.info("=" * SEPARATOR_COUNT)
コード例 #11
0
def init_service_etc():
    conf_dir = const.CONFIG_DIR
    logger.info("\n1. creating configuration directory {} ......".format(conf_dir))
    if os.path.isdir(conf_dir):
        logger.info("configuration directory {} exist".format(conf_dir))
    else:
        logger.info("creating configuration directory {}".format(conf_dir))
        os.makedirs(conf_dir)

    # copy configuration files to /etc/natrixclient
    logger.info("")
    logger.info("\n2. copying sample configuration files to {}......".format(conf_dir))
    sample_dir = os.path.dirname(os.path.realpath(__file__)) + "/../conf/etc/natrixclient/"
    for sample_file in os.listdir(sample_dir):
        conf_path = conf_dir + sample_file
        if os.path.isfile(conf_path):
            logger.info("configuration file {} already exists in {}".format(sample_file, conf_dir))
            bak_time = time.strftime("%Y%m%d%H%M%S", time.localtime())
            bak_conf_path = conf_path + "." + str(bak_time)
            logger.info("backup original configuration file from {} to {}".format(conf_path, bak_conf_path))
            os.rename(conf_path, bak_conf_path)
        sample_path = sample_dir + sample_file
        logger.info("copying configuration sample file from {} to {} ......".format(sample_path, conf_path))
        shutil.copyfile(sample_path, conf_path)