Skip to content

terminate_cluster

Terminate a cluster.

Parameters:

Name Type Description Default
quokka_cluster EC2Cluster

Cluster to terminate.

required
Return

None

Examples:

>>> from pyquokka.utils import *
>>> manager = QuokkaClusterManager()
>>> cluster = manager.create_cluster(aws_access_key, aws_access_id, num_instances = 2, instance_type = "i3.2xlarge", ami="ami-0530ca8899fac469f", requirements = ["numpy", "pandas"])
>>> manager.terminate_cluster(cluster)
Source code in pyquokka/utils.py
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
def terminate_cluster(cluster_json):

    """
    Terminate a cluster.

    Args:
        quokka_cluster (EC2Cluster): Cluster to terminate.

    Return:
        None

    Examples:

        >>> from pyquokka.utils import *
        >>> manager = QuokkaClusterManager()
        >>> cluster = manager.create_cluster(aws_access_key, aws_access_id, num_instances = 2, instance_type = "i3.2xlarge", ami="ami-0530ca8899fac469f", requirements = ["numpy", "pandas"])
        >>> manager.terminate_cluster(cluster)

    """

    ec2 = boto3.client("ec2")
    with open(cluster_json, "r") as f:
        cluster_info = json.load(f)
    instance_ids = list(cluster_info['instance_ids'].values())
    ec2.terminate_instances(InstanceIds = instance_ids)
    ec2.terminate_instances(InstanceIds = instance_ids)
    while True:
        time.sleep(0.1)
        a = ec2.describe_instances(InstanceIds = instance_ids)
        states = [a['Reservations'][0]['Instances'][i]['State']['Name'] for i in range(len(instance_ids))]
        if "running" in states:
            continue
        else:
            break
    os.remove(cluster_json)