示例#1
0
    def get_vps_spec_data(self,
                          db_session: Session,
                          isp_id: int,
                          reload: bool = False) -> dict:
        vps_spec_data = {}
        vps_isp_obj = crud_isp.get(db_session=db_session, id=isp_id)

        if vps_isp_obj:
            vps_spec_data_key = self.VPS_SPEC_DATA_KEY_PATTERN.format(
                name=vps_isp_obj.provider_name)
            if reload:
                vps_spec_raw_data = None
            else:
                vps_spec_raw_data = self.conn.get(vps_spec_data_key)

            if vps_spec_raw_data:
                vps_spec_data = json.loads(vps_spec_raw_data)
            else:
                vps_spec_data = crud_vps.get_specs(db_session=db_session,
                                                   isp_id=isp_id)
                self.set_data_cache(name=vps_spec_data_key,
                                    value=json.dumps(vps_spec_data),
                                    ex=self.VPS_SPEC_CACHE_EXPIRE_TIME)

        return vps_spec_data
示例#2
0
    def run(self, isp_id: int, domain: str):
        with session_manager() as db_session:
            isp = crud_isp.get(db_session=db_session, id=isp_id)
            isp_instance = isp.isp_instance
        if not isp:
            result = {
                'status': 'pass',
                'msg': f"empty isp via isp_id: {isp_id}"
            }
            return self.set_result(result)

        formatted_domain = self.format_domain(domain)
        domain_detect_result = isp_instance.check_domain_raw(formatted_domain)
        domain_detect_replay_data = domain_detect_result.get('reply', {})

        available_domain_data = domain_detect_replay_data.get("available", {})
        unavailable_domain_data = domain_detect_replay_data.get(
            "unavailable", {})
        domain_list = []

        for available_domain_data in self.extract_domain_data(
                available_domain_data):
            domain_list.append(dict(purchasable=True, **available_domain_data))
        for unavailable_domain_data in self.extract_domain_data(
                unavailable_domain_data):
            domain_list.append(
                dict(purchasable=False, **unavailable_domain_data))

        return domain_list
示例#3
0
文件: vps.py 项目: vinceyxl/LuWu
def create_vps_server(
        db: Session = Depends(get_db), *, vps_profile: VpsCreateSchema):
    # validate
    vps_isp_obj = crud_isp.get(db_session=db, id=vps_profile.isp_id)
    if not vps_isp_obj:
        raise ValidationError(
            [
                ErrorWrapper(Exception('provider_name is not matched'),
                             loc="isp_id")
            ],
            model=VpsCreateSchema,
        )

    # create base vps data
    rp = RedisPool()
    vps_spec_data = rp.get_vps_spec_value(db_session=db,
                                          isp_id=vps_profile.isp_id,
                                          os_code=vps_profile.os_code,
                                          plan_code=vps_profile.plan_code,
                                          region_code=vps_profile.region_code)
    vps_config = dict(hostname=vps_profile.hostname,
                      isp_id=vps_profile.isp_id,
                      ssh_keys=vps_profile.ssh_keys,
                      remark=vps_profile.remark,
                      status=vps_profile.status,
                      **vps_spec_data)

    vps_obj = crud_vps.create(db_session=db,
                              obj_in=vps_config,
                              serializer=None)
    task = celery_app.send_task("create_vps",
                                args=[vps_profile.dict(), vps_obj.id])
    return dict(result=task)
示例#4
0
文件: vps.py 项目: vinceyxl/LuWu
def get_vps_ssh_keys(
    db: Session = Depends(get_db), *, isp_id: int = Query(..., alias='ispId')):
    ssh_key_list = []
    vps_isp_obj = crud_isp.get(db_session=db, id=isp_id)
    if vps_isp_obj:
        ssh_key_list = vps_isp_obj.isp_instance.get_ssh_key_list(isp_id)

    return dict(result=ssh_key_list)
示例#5
0
文件: vps.py 项目: vinceyxl/LuWu
 def run(self, vps_profile: VpsCreateSchema, vps_id: int, *args, **kwargs) -> dict:
     with session_manager() as db_session:
         vps_isp_obj = crud_isp.get(db_session=db_session, id=vps_profile['isp_id'])
         ssh_key_obj = crud_ssh_config.get_config(db_session)
         extra_server_data = vps_isp_obj.isp_instance.create_server(vps_profile, ssh_key_obj.public_key)
         vps_data = dict(
             status=VpsStatus.running,
             **extra_server_data
         )
         crud_vps.update(db_session=db_session, obj_id=vps_id, obj_in=vps_data)
     return self.set_result()
示例#6
0
 def run(self, isp_id: int, domain: str, **kwargs: dict):
     with session_manager() as db_session:
         isp = crud_isp.get(db_session=db_session, id=isp_id)
         try:
             register_success = isp.isp_instance.register_domain(domain)
         except Exception as e:
             self.log_exception(e)
             register_success = False
         if register_success:
             domain_profile = DomainCreate(isp_id=isp_id, domain=domain)
             crud_domain.create(db_session=db_session,
                                obj_in=domain_profile,
                                serializer=None)
     return register_success
示例#7
0
文件: crud_vps.py 项目: vinceyxl/LuWu
    def get_specs(cls, db_session: Session, isp_id: int) -> dict:
        vps_isp_obj = crud_isp.get(db_session=db_session, id=isp_id)

        if vps_isp_obj and vps_isp_obj.is_vps_isp:
            region_list = vps_isp_obj.isp_instance.get_available_regions_list()
            os_list = vps_isp_obj.isp_instance.get_available_os_list()
            plan_list = vps_isp_obj.isp_instance.get_available_plans_list()
        else:
            os_list = []
            plan_list = []
            region_list = []

        vps_spec_data = {
            'region': region_list,
            'os': os_list,
            'plan': plan_list,
        }
        return vps_spec_data
示例#8
0
文件: domains.py 项目: vinceyxl/LuWu
def purchase_domain(
        db: Session = Depends(get_db), *, domain_profile: PurchaseDomainSchema
):
    domain_isp = crud_isp.get(
        db_session=db,
        id=domain_profile.isp_id,
    )
    if domain_isp and domain_isp.provider_name != domain_profile.provider_name:
        raise ValidationError(
            [
                ErrorWrapper(Exception('provider_name is not matched'),
                             loc="provider_name")
            ],
            model=PurchaseDomainSchema,
        )

    purchase_task = celery_app.send_task("purchase_domain",
                                         kwargs=domain_profile.dict())
    purchase_result = purchase_task.get()
    return dict(result=purchase_result)