示例#1
0
 def remove(self, svc_opts: UnixServiceOpts, force: bool = False):
     service_fqn = self.to_service_fqn(svc_opts.service_dir,
                                       svc_opts.service_name)
     self.stop(svc_opts.service_name)
     self.disable(svc_opts.service_name)
     if force and FileHelper.is_exists(service_fqn):
         logger.info(f'Remove System service [{svc_opts.service_name}]...')
         FileHelper.rm(service_fqn)
     SystemHelper.exec_command("systemctl daemon-reload",
                               silent=True,
                               log_lvl=logger.INFO)
示例#2
0
 def lease_ip(self,
              vpn_acc: str,
              vpn_nic: str,
              daemon=False,
              is_execute=True):
     logger.log(self.log_lvl, 'Lease a new VPN IP...')
     command = f'{self.ip_tool} {self._lease_ip_opt(vpn_acc, vpn_nic, daemon)}'
     if is_execute:
         SystemHelper.exec_command(command,
                                   silent=self.silent,
                                   log_lvl=logger.down_lvl(self.log_lvl))
     return command
示例#3
0
 def pre_exec(self, silent=False, log_lvl=logger.DEBUG, **kwargs):
     logger.log(log_lvl, 'Start VPN Client if not yet running...')
     if not self.is_installed(silent, log_lvl):
         return
     if self.pid_handler.is_running():
         self._prev_is_run = True
         return
     SystemHelper.exec_command(f'{self.opts.vpnclient} start',
                               log_lvl=logger.down_lvl(log_lvl))
     time.sleep(1)
     if not self.pid_handler.is_running(log_lvl=logger.down_lvl(log_lvl)):
         logger.error('Unable start VPN Client')
         sys.exit(ErrorCode.VPN_START_FAILED)
示例#4
0
 def post_exec(self, silent=False, log_lvl=logger.DEBUG, **kwargs):
     logger.log(log_lvl, 'Stop VPN Client if applicable...')
     if not self.is_installed(True, log_lvl):
         return
     if (self._prev_is_run or
             not self.adhoc_task) and not kwargs.get('_force_stop', False):
         return
     lvl = logger.down_lvl(log_lvl)
     if self.pid_handler.is_running(log_lvl=lvl):
         SystemHelper.exec_command(f'{self.opts.vpnclient} stop',
                                   silent=silent,
                                   log_lvl=lvl)
         self._cleanup_zombie_vpn(1, log_lvl=lvl)
         self.pid_handler.cleanup()
示例#5
0
 def status(self, service_name: str) -> ServiceStatus:
     status = SystemHelper.exec_command(
         f"systemctl status {service_name} | grep Active | awk '{{print $2$3}}'",
         shell=True,
         silent=True,
         log_lvl=logger.TRACE)
     return ServiceStatus.parse(status)
示例#6
0
 def create(self,
            svc_opts: UnixServiceOpts,
            replacements: dict,
            auto_startup: bool = False):
     service_fqn = self.to_service_fqn(svc_opts.service_dir,
                                       svc_opts.service_name)
     logger.info(
         f'Add new service [{svc_opts.service_name}] in [{service_fqn}]...')
     FileHelper.copy(self.resource_dir.joinpath(Systemd.SERVICE_FILE_TMPL),
                     service_fqn,
                     force=True)
     FileHelper.replace_in_file(service_fqn, replacements, backup='')
     FileHelper.chmod(service_fqn, mode=0o0644)
     SystemHelper.exec_command("systemctl daemon-reload",
                               silent=True,
                               log_lvl=logger.INFO)
     if auto_startup:
         self.enable(svc_opts.service_name)
示例#7
0
 def restart(self, service_name, delay: int = 1):
     logger.info(f"Restart System service [{service_name}]...")
     SystemHelper.exec_command(f"systemctl restart {service_name}",
                               log_lvl=logger.INFO)
     time.sleep(delay)
示例#8
0
 def stop(self, service_name):
     logger.info(f"Stop System service [{service_name}]...")
     SystemHelper.exec_command(f"systemctl stop {service_name}",
                               silent=True,
                               log_lvl=logger.INFO)
示例#9
0
 def disable(self, service_name: str):
     logger.info(f'Disable System service [{service_name}]...', )
     SystemHelper.exec_command(f"systemctl disable {service_name}",
                               silent=True,
                               log_lvl=logger.INFO)
示例#10
0
 def enable(self, service_name: str):
     logger.info(f'Enable System service [{service_name}]...', )
     SystemHelper.exec_command(f"systemctl enable {service_name}",
                               log_lvl=logger.INFO)
示例#11
0
 def renew_all_ip(self, delay=1, silent=False):
     logger.log(self.log_lvl, 'Refresh all IPs...')
     time.sleep(delay)
     SystemHelper.exec_command(f'{self._refresh_all_ip_opt()}',
                               silent=silent or self.silent,
                               log_lvl=logger.down_lvl(self.log_lvl))
示例#12
0
 def release_ip(self, vpn_acc: str, vpn_nic: str):
     logger.log(self.log_lvl, 'Release the current VPN IP...')
     SystemHelper.exec_command(
         f'{self.ip_tool} {self._release_ip_opt(vpn_acc, vpn_nic)}',
         silent=self.silent,
         log_lvl=logger.down_lvl(self.log_lvl))
示例#13
0
 def install(self, package):
     SystemHelper.exec_command(f'{self.tool} install {package} -y',
                               log_lvl=logger.INFO,
                               silent=True)