def create_uhost_batch(image_id, count):
    resp = client.uhost().create_uhost_instance({
        "Name":
        "sdk-python3-example-two-tier",
        "Zone":
        "cn-bj2-05",
        "ImageId":
        image_id,
        "LoginMode":
        "Password",
        "Password":
        utils.gen_password(20),
        "CPU":
        1,
        "Memory":
        1024,
        "MaxCount":
        count,
    })
    uhost_ids = resp.get("UHostIds", [])
    wait.wait_for_state(
        target=["running"],
        pending=["pending"],
        timeout=300,
        refresh=lambda:
        ("running"
         if all([state == "Running"
                 for state in mget_uhost_states(uhost_ids)]) else "pending"),
    )
    return uhost_ids
Beispiel #2
0
def create_uhost_batch(image_id, count):
    resp = client.uhost().create_uhost_instance({
        'Name':
        'sdk-python2-example-two-tier',
        'Zone':
        'cn-bj2-05',
        'ImageId':
        image_id,
        'LoginMode':
        "Password",
        'Password':
        utils.gen_password(20),
        'CPU':
        1,
        'Memory':
        1024,
        'MaxCount':
        count,
    })
    uhost_ids = resp.get('UHostIds', [])
    wait.wait_for_state(
        target=['running'],
        pending=['pending'],
        timeout=300,
        refresh=lambda:
        ('running'
         if all([state == 'Running'
                 for state in mget_uhost_states(uhost_ids)]) else 'pending'),
    )
    return uhost_ids
def test_wait():
    with pytest.raises(wait.WaitTimeoutException):
        wait.wait_for_state(
            pending=["pending"],
            target=["running"],
            refresh=lambda: "pending",
            timeout=0.5,
        )
    wait.wait_for_state(pending=["pending"],
                        target=["running"],
                        refresh=lambda: "running",
                        timeout=0.5)
Beispiel #4
0
def delete_uhost_batch(uhost_ids):
    for uhost_id in uhost_ids:
        client.uhost().stop_uhost_instance({"UHostId": uhost_id})
    wait.wait_for_state(
        target=["stopped"],
        pending=["pending"],
        timeout=300,
        refresh=lambda: "stopped"
        if all([(state == "Stopped") for state in mget_uhost_states(uhost_ids)])
        else "pending",
    )
    for uhost_id in uhost_ids:
        client.uhost().terminate_uhost_instance({"UHostId": uhost_id})
Beispiel #5
0
def delete_uhost_batch(uhost_ids):
    for uhost_id in uhost_ids:
        client.uhost().stop_uhost_instance({'UHostId': uhost_id})

    wait.wait_for_state(
        target=['stopped'],
        pending=['pending'],
        timeout=300,
        refresh=lambda:
        ('stopped'
         if all([state == 'Stopped'
                 for state in mget_uhost_states(uhost_ids)]) else 'pending'),
    )

    for uhost_id in uhost_ids:
        client.uhost().terminate_uhost_instance({'UHostId': uhost_id})
Beispiel #6
0
def main():
    client = Client({
        "region": "cn-bj2",
        "project_id": os.getenv("UCLOUD_PROJECT_ID"),
        "public_key": os.getenv("UCLOUD_PUBLIC_KEY"),
        "private_key": os.getenv("UCLOUD_PRIVATE_KEY"),
    })
    logger.info("finding image, random choice one")
    images = (client.uhost().describe_image({
        "ImageType": "Base",
        "OsType": "Linux"
    }).get("ImageSet", []))
    assert len(images) > 0
    logger.info("creating uhost instance ...")
    image = random.choice(images)
    resp = client.uhost().create_uhost_instance({
        "Name":
        "sdk-python-example",
        "Zone":
        image["Zone"],
        "ImageId":
        image["ImageId"],
        "LoginMode":
        "Password",
        "Password":
        utils.gen_password(20),
        "CPU":
        1,
        "Memory":
        1024,
        "Disks": [{
            "Size": image["ImageSize"],
            "Type": "LOCAL_NORMAL",
            "IsBoot": "True",
        }],
    })
    uhost_id = utils.first(resp["UHostIds"])
    logger.info("uhost instance is created")

    def refresh_state():
        uhost = utils.first(client.uhost().describe_uhost_instance({
            "UHostIds": [uhost_id]
        }).get("UHostSet", []))
        if uhost.get("State") in ["Running", "Stopped"]:
            return uhost["State"].lower()
        return "pending"

    logger.info("wait uhost state is running ...")
    try:
        wait.wait_for_state(
            pending=["pending"],
            target=["running"],
            timeout=300,
            refresh=refresh_state,
        )
    except wait.WaitTimeoutException as e:
        logger.error(e)
    logger.info("uhost instance is running")
    logger.info("stopping uhost for clean up resources ...")
    client.uhost().stop_uhost_instance({"UHostId": uhost_id})
    try:
        wait.wait_for_state(
            pending=["pending"],
            target=["stopped"],
            timeout=180,
            refresh=refresh_state,
        )
    except wait.WaitTimeoutException as e:
        logger.error(e)
    else:
        logger.info("uhost instance is stopped")
    logger.info("remove uhost instance from cloud")
    client.uhost().terminate_uhost_instance({"UHostId": uhost_id})
Beispiel #7
0
def main():
    client = Client({
        "region": "cn-bj2",
        "project_id": os.getenv("UCLOUD_PROJECT_ID"),
        "public_key": os.getenv("UCLOUD_PUBLIC_KEY"),
        "private_key": os.getenv("UCLOUD_PRIVATE_KEY"),
    })

    logger.info("finding image, random choice one")
    images = client.uhost().describe_image({
        'ImageType': 'Base',
        'OsType': 'Linux'
    }).get('ImageSet', [])

    assert len(images) > 0

    logger.info("creating uhost instance ...")
    image = random.choice(images)

    resp = client.uhost().create_uhost_instance({
        'Name':
        'sdk-python-example',
        'Zone':
        image["Zone"],
        'ImageId':
        image["ImageId"],
        'LoginMode':
        "Password",
        'Password':
        utils.gen_password(20),
        'CPU':
        1,
        'Memory':
        1024,
        'Disks': [{
            'Size': image["ImageSize"],
            'Type': 'CLOUD_NORMAL',
            'IsBoot': 'True',
        }],
    })
    uhost_id = utils.first(resp["UHostIds"])
    logger.info("uhost instance is created")

    def refresh_state():
        uhost = utils.first(client.uhost().describe_uhost_instance({
            'UHostIds': [uhost_id]
        }).get('UHostSet', []))
        if uhost.get('State') in ['Running', 'Stopped']:
            return uhost['State'].lower()
        return 'pending'

    logger.info("wait uhost state is running ...")
    try:
        wait.wait_for_state(pending=['pending'],
                            target=['running'],
                            timeout=300,
                            refresh=refresh_state)
    except wait.WaitTimeoutException as e:
        logger.error(e)
    logger.info("uhost instance is running")

    logger.info("stopping uhost for clean up resources ...")
    client.uhost().stop_uhost_instance({'UHostId': uhost_id})

    try:
        wait.wait_for_state(pending=['pending'],
                            target=['stopped'],
                            timeout=180,
                            refresh=refresh_state)
    except wait.WaitTimeoutException as e:
        logger.error(e)
    else:
        logger.info("uhost instance is stopped")

    logger.info("remove uhost instance from cloud")
    client.uhost().terminate_uhost_instance({'UHostId': uhost_id})