Пример #1
0
def delete_user_sshkey(id):
    try:
        db_session.query(GnSshKeys).filter(GnSshKeys.id == id).delete()
    except:
        db_session.rollback()
    finally:
        db_session.commit()
Пример #2
0
def server_create_snapshot(id, image_id, user_id, team_code, sql_session):
    snap_info = db_session.query(GnVmImages).filter(
        GnVmImages.id == image_id).one()
    vm_info = sql_session.query(GnVmMachines).filter(
        GnVmMachines.id == id).one()
    try:
        # 네이밍
        new_image_name = vm_info.internal_name + "_" + datetime.datetime.now(
        ).strftime('%Y%m%d%H%M%S')

        # 디스크 복사
        s = pxssh.pxssh(timeout=1200)
        s.login(vm_info.gnHostMachines.ip, USER)
        s.sendline("cp " + config.LIVERT_IMAGE_LOCAL_PATH +
                   vm_info.internal_name + ".img" + " " +
                   config.LIVERT_IMAGE_SNAPSHOT_PATH + new_image_name + ".img")
        s.logout()

        snap_info.filename = new_image_name + '.img'
        snap_info.status = "Running"
        snap_info.host_id = vm_info.gnHostMachines.id
        sql_session.commit()
    except Exception as e:
        print(e.message)
        error_hist = GnErrorHist(
            type=vm_info.type,
            action="Snap-create",
            team_code=vm_info.team_code,
            author_id=vm_info.author_id,
            vm_id=vm_info.id,
            vm_name=vm_info.name,
            cause=e.message,
            action_time=datetime.datetime.now().strftime('%Y%m%d%H%M%S'))
        sql_session.add(error_hist)
        snap_info.status = "Error"
        sql_session.commit()
Пример #3
0
def vm_detail_info(id):
    db_session.query(GnVmMachines).filter(GnVmMachines.id == id).all()
Пример #4
0
def getsshkey_info(id):
    return db_session.query(GnSshKeys).filter(GnSshKeys.id == id).one()
Пример #5
0
def server_create(team_code, user_id, user_name, id, sql_session):
    try:
        #vm 조회
        vm_info = sql_session.query(GnVmMachines).filter(
            GnVmMachines.id == id).one()

        #host 조회
        host_info = sql_session.query(GnHostMachines).filter(
            GnHostMachines.id == vm_info.host_id).one()

        # base image 조회
        image_info = db_session.query(GnVmImages).filter(
            GnVmImages.id == vm_info.image_id).one()

        # ssh 조회
        ssh_info = db_session.query(GnSshKeys).filter(
            GnSshKeys.id == vm_info.ssh_key_id).one()

        # vm 생성
        internal_name = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        intern_id = kvm_create(internal_name, vm_info.cpu, vm_info.memory,
                               vm_info.disk, image_info.filename,
                               image_info.sub_type, host_info.ip)
        print("complete init vm!!!")
        #ip 세팅
        ip = ""
        while len(ip) == 0:
            print(id + ":processing init ip!!!")
            ip = getIpAddress(internal_name, host_info.ip)
        print("complete get ip=" + ip)
        #ip 고정
        # if len(ip) != 0:
        #     print(id+":set init ip!!!")
        #     setStaticIpAddress(ip, host_info.ip, image_info.ssh_id)
        #     print(id+":complete set ip!!!")

        # 기존 저장된 ssh key 등록
        setSsh(host_info.ip, ssh_info.pub, ssh_info.org, ssh_info.name, ip,
               image_info.ssh_id)
        print(id + ":processing modify data!!!")
        vm_info.internal_name = internal_name
        vm_info.internal_id = intern_id
        vm_info.ip = ip
        vm_info.status = "Running"
        vm_info.os = image_info.os
        vm_info.os_ver = image_info.os_ver
        vm_info.os_sub_ver = image_info.os_subver
        vm_info.os_bit = image_info.os_bit

        print(id + ":insert payment info")
        vm_size = sql_session.query(GnVmSize).filter(
            GnVmSize.id == vm_info.size_id).first()
        system_setting = sql_session.query(GnSystemSetting).first()
        instance_status_price = None
        if system_setting.billing_type == 'D':
            instance_status_price = vm_size.day_price
        elif system_setting.billing_type == 'H':
            instance_status_price = vm_size.hour_price

        insert_instance_status = GnInstanceStatus(
            vm_id=vm_info.id,
            vm_name=vm_info.name,
            author_id=vm_info.author_id,
            author_name=user_name,
            team_code=vm_info.team_code,
            price=instance_status_price,
            price_type=system_setting.billing_type,
            cpu=vm_info.cpu,
            memory=vm_info.memory,
            disk=vm_info.disk)
        sql_session.add(insert_instance_status)

        sql_session.commit()
        print(id + ":complete modify data!!!")
        return True
    except Exception as e:
        print(id + ":init vm error!!!")
        print("error:" + e.message)
        error_hist = GnErrorHist(
            type=vm_info.type,
            action="Create",
            team_code=vm_info.team_code,
            author_id=vm_info.author_id,
            vm_id=vm_info.id,
            vm_name=vm_info.name,
            cause=e.message,
            action_time=datetime.datetime.now().strftime('%Y%m%d%H%M%S'))
        sql_session.add(error_hist)
        vm_info.status = "Error"
        sql_session.commit()
        return False