def service_stop() -> None: check_root() subprocess.check_call([ "systemctl", "stop", SERVICE_NAME, ])
def service_install() -> None: check_root() assert os.path.isdir(SYSTEMD_FOLDER), "systemd folder does not exist. What kind of linux is this?" assert not os.path.isfile(UNIT_FILE), "you already have the service installed" abs_path_to_program = sys.argv[0] if not os.path.isabs(abs_path_to_program): abs_path_to_program = os.path.join(os.getcwd(), abs_path_to_program) prev_mask = os.umask(0o000) with os.fdopen(os.open(UNIT_FILE, os.O_WRONLY | os.O_CREAT, 0o644), 'wt') as f: f.write(CONTENT.format(abs_path_to_program)) os.umask(prev_mask) subprocess.check_call([ "systemctl", "daemon-reload", ]) subprocess.check_call([ "systemctl", "enable", SERVICE_NAME, ]) if ConfigControl.install_does_run: subprocess.check_call([ "systemctl", "start", SERVICE_NAME, ]) write_config_file_json_system()
def service_start() -> None: """ Start the service """ check_root() subprocess.check_call([ "systemctl", "start", SERVICE_NAME, ])
def run(): if ConfigControl.configure_logging_syslog: pylogconf.core.remove_all_root_handlers() pylogconf.core.setup_syslog( name=pyflexebs.LOGGER_NAME, level=ConfigControl.loglevel, ) logger = get_logger() logger.info("starting") if ConfigControl.configure_proxy: configure_proxy() if ConfigControl.check_tools: check_tools() if ConfigControl.check_root: check_root() metadata = ec2_metadata.ec2_metadata instance_id = metadata.instance_id ec2_resource = boto3.resource('ec2', region_name=metadata.region) instance = ec2_resource.Instance(instance_id) # tags = instance.tags ec2_client = boto3.client('ec2', region_name=metadata.region) volumes = instance.volumes.all() device_to_volume = {} for volume in volumes: for a in volume.attachments: device = normalize_device(a["Device"]) device_to_volume[device] = volume while True: logger.info("checking disk utilization") for p in psutil.disk_partitions(): if p.mountpoint in ConfigAlgo.disregard: logger.info(f"disregard: {p.mountpoint} in {ConfigAlgo.disregard}") continue if p.fstype not in ConfigAlgo.file_systems: logger.info(f"disregard: {p.fstype} not in {ConfigAlgo.file_systems}") continue # if TAG_DONT_RESIZE in tags: # logger.info(f"disregard: {TAG_DONT_RESIZE} in {tags}") # continue logger.info(f"checking {p.device} {p.mountpoint} {p.fstype}") if ConfigAlgo.watermark_max is not None: if psutil.disk_usage(p.mountpoint).percent >= ConfigAlgo.watermark_max: logger.info(f"max watermark detected at disk {p.device} mountpoint {p.mountpoint}") logger.info(f"percent is {psutil.disk_usage(p.mountpoint).percent}") logger.info(f"total is {psutil.disk_usage(p.mountpoint).total}") logger.info(f"used is {psutil.disk_usage(p.mountpoint).used}") enlarge_volume(p, device_to_volume, ec2_client) time.sleep(ConfigAlgo.interval)
def service_uninstall() -> None: check_root() assert os.path.isdir(SYSTEMD_FOLDER), "systemd folder does not exist. What kind of linux is this?" assert os.path.isfile(UNIT_FILE), "you dont have the service installed" if ConfigControl.uninstall_does_kill: subprocess.check_call([ "systemctl", "stop", SERVICE_NAME, ]) subprocess.check_call([ "systemctl", "disable", SERVICE_NAME, ]) os.unlink(UNIT_FILE) subprocess.check_call([ "systemctl", "daemon-reload", ]) rm_config_file_json_system()