示例#1
0
文件: shell.py 项目: junxu/bcectool
def do_export_ovf(args):
    '''Export the server to an ovf'''
    # 1. get server info  
    # 2. snapshot and download image
    # 3. convert image 
    # 4. write ovf file
    
    #step1 
    env = {'OS_USERNAME': args.os_username,
           'OS_PASSWORD': args.os_password,
           'OS_TENANT_NAME': args.os_tenant_name,
           'OS_AUTH_URL': args.os_auth_url}
    logger.info("Get server's info, please waiting ...")
    server_info = bcec_utils.get_server_info(env, args.server)
    logger.info("Get server's info done")
    
    if args.path is None:
        work_dir = os.getcwd()
    else:
        work_dir = args.path
    
    if os.path.exists(work_dir) is False:
       os.makedirs(work_dir)
    
    src_image = os.path.join(work_dir, ''.join([args.name, ".qcow2" ]))
    #step2:
    snap_name = ''.join([args.name, '-ovf', '-snap']) 
    logger.info("Make snapshot, this will take sometimes, please waiting ...")
    _, image_size, image_id = bcec_utils.server_snapshot(env, args.server, snap_name)
    logger.info("Make snapshot done")

    logger.info("Download image, this will take sometimes, please waiting ...")
    src_image = os.path.join(work_dir, ''.join([args.name, '-ovf-disk1' ".qcow2"]))
    bcec_utils.image_download(env, image_id, src_image)
    logger.info("Download image done")
     
    #step3   
    #image_size = bcec_utils.get_image_size(env, snap_name)
    dest_image = os.path.join(work_dir, ''.join([args.name, '-ovf-disk1' ".vmdk"]))
    logger.info("Convert image to vmdk format, please waiting ...")
    bcec_utils.image_convert("qcow2", src_image, "vmdk" , dest_image)
    logger.info("Convert image done")
    
    server_info['disk'] = ''.join([args.name, '-ovf-disk1' ".vmdk"]) 
    server_info['disk_capacity'] = image_size

    #step4
    ovf_path = os.path.join(work_dir, ''.join([args.name, ".ovf" ]))
    bcec_utils.create_ovf(server_info, ovf_path)
    logger.info("Create ovf done")
示例#2
0
文件: shell.py 项目: junxu/bcectool
def do_import_ovf(args):
    '''Import the ovf'''
    # 1. read ovf file to get ram and vcpu info
    # 2. convert image
    # 3. upload image
    # 4. create vm

    # step1
    vm =  bcec_utils.parse_ovf_import(args.file)
    print vm
    
    # step2
    logger.info("Convert image to qcow2 format, please wait ...")
    images = vm['disks']
    for image_name in images:
        dir_path = os.path.dirname(os.path.abspath(args.file))
        src_image = os.path.join(dir_path, images[image_name]['href']) 
        dest_image = os.path.join(dir_path, ''.join([image_name.split(".")[0], ".qcow2" ]))
        bcec_utils.image_convert("vmdk", src_image, "qcow2" , dest_image)
    logger.info("Convert image done")

    # step3
    image_name = ''.join([args.name, '-ovf'])
    env = {'OS_USERNAME': args.os_username,
           'OS_PASSWORD': args.os_password,
           'OS_TENANT_NAME': args.os_tenant_name,
           'OS_AUTH_URL': args.os_auth_url}
    logger.info("Create image, this will take sometimes, please waiting ...")
    _, image_id = bcec_utils.image_create(env, image_name, dest_image)
    vm['image'] = image_id
    logger.info("Create image done")

    logger.info("Create server, please waiting ...")
    vm['name'] = args.name
    bcec_utils.create_server(env, vm)
    logger.info("import server done")