def api(self) -> pycloudlib.cloud.BaseCloud:
        """Return the api used to interact with the cloud provider."""
        if self._api is None:
            self._api = pycloudlib.EC2(
                tag=self.tag,
                access_key_id=self.aws_access_key_id,
                secret_access_key=self.aws_secret_access_key,
                region=self.region,
            )

        return self._api
Beispiel #2
0
def demo():
    """Show example of using the EC2 library.

    Connects to EC2 and finds the latest daily image. Then runs
    through a number of examples.
    """
    ec2 = pycloudlib.EC2(tag='examples')
    daily = ec2.daily_image(release='bionic')

    launch_basic(ec2, daily)
    custom_vpc(ec2, daily)
    snapshot(ec2, daily)
    launch_multiple(ec2, daily)
    hot_add(ec2, daily)
Beispiel #3
0
    def __init__(self, _, instance_type, release, iterations=1, log_dir=''):
        """Initialize base test."""
        self.log_dir = os.path.join(log_dir, instance_type, release,
                                    self.test_name)
        self.setup_logging()
        self._log = logging.getLogger(__name__)

        self.cloud = pycloudlib.EC2(tag='perfkit')
        self.instance_type = instance_type
        self.release = release
        self.iterations = iterations

        self.csv_result = ''
        self.instance = None
Beispiel #4
0
    def measure(self, datadir, instances=1, reboots=1):
        """
        Measure Amazon AWS EC2.
        Returns the measurement metadata as a dictionary
        """
        print("Perforforming measurement on Amazon EC2")

        if self.release in distro_metanames:
            release = metaname2release(self.release)
            print("Resolved %s to %s" % (self.release, release))
        else:
            release = self.release

        ec2 = pycloudlib.EC2(tag="bootspeed", region=self.region)

        if not self.ssh_pubkey_path:
            self.ssh_pubkey_path = ec2.key_pair.public_key_path
        if not self.ssh_privkey_path:
            self.ssh_privkey_path = ec2.key_pair.private_key_path
        if not self.ssh_keypair_name:
            self.ssh_keypair_name = ec2.key_pair.name
        ec2.use_key(self.ssh_pubkey_path, self.ssh_privkey_path, self.ssh_keypair_name)

        # Will also catch unknown inst types raising a meaningful exception
        inst_specs = ec2.client.describe_instance_types(InstanceTypes=[self.inst_type])[
            "InstanceTypes"
        ][0]

        arch = "amd64"
        if "arm64" in inst_specs["ProcessorInfo"]["SupportedArchitectures"]:
            arch = "arm64"

        image_username = "******"

        if release.startswith("debian-"):
            image_username = "******"
            debrelease = re.search(r"debian-(.*)", release).group(1)
            if debrelease == "sid":
                daily, serial = self.debian_sid_daily_image(arch)
            else:
                raise NotImplementedError
        else:
            daily = ec2.daily_image(release=release, arch=arch)
            serial = ec2.image_serial(daily)

        print("Instance architecture:", arch)
        print("Daily image for", release, "is", daily)
        print("Image serial:", serial)
        print("Instance username:"******"instance_" + str(ninstance))
            instance_data.mkdir()

            # This tag name will be inherited by the launched instance.
            # We want it to be unique and to contain an easily parsable
            # timestamp (UTC seconds since epoch), which we will use to
            # detemine if an instance is stale and should be terminated.
            tag = "bootspeed-" + str(int(dt.datetime.utcnow().timestamp()))
            ec2.tag = tag

            print("Launching instance", ninstance + 1, "of", instances, "tag:", ec2.tag)
            instance = ec2.launch(
                image_id=daily,
                instance_type=self.inst_type,
                SubnetId=self.subnetid,
                SecurityGroupIds=self.sgid,
                Placement={"AvailabilityZone": self.availability_zone},
                wait=False,
            )
            instance.username = image_username

            try:
                measure_instance(instance, instance_data, reboots)

                # If the availability zone is not specified a random one is
                # assigned. We want to make sure the next instances (if any)
                # will use the same zone, so we save it.
                if not self.availability_zone:
                    self.availability_zone = instance.availability_zone
            finally:
                print("Deleting the instance.")
                instance.delete(wait=False)

        metadata = gen_metadata(
            cloud=self.cloud,
            region=self.region,
            availability_zone=self.availability_zone,
            inst_type=self.inst_type,
            release=self.release,
            cloudid=daily,
            serial=serial,
        )

        return metadata
    def measure(self, datadir, instances=1, reboots=1):
        """
        Measure Amazon AWS EC2.
        Returns the measurement metadata as a dictionary
        """
        print("Perforforming measurement on Amazon EC2")

        if self.release in distro_metanames:
            release = metaname2release(self.release)
            print("Resolved %s to %s" % (self.release, release))
        else:
            release = self.release

        ec2 = pycloudlib.EC2(tag="bootspeed", region=self.region)

        if not self.ssh_pubkey_path:
            self.ssh_pubkey_path = ec2.key_pair.public_key_path
        if not self.ssh_privkey_path:
            self.ssh_privkey_path = ec2.key_pair.private_key_path
        if not self.ssh_keypair_name:
            self.ssh_keypair_name = ec2.key_pair.name
        ec2.use_key(self.ssh_pubkey_path, self.ssh_privkey_path,
                    self.ssh_keypair_name)

        if self.inst_type.split(".")[0] == "a1":
            if release == "xenial" or release == "bionic":
                # Workaround for LP: #1832386
                daily = ec2.released_image(release=release, arch="arm64")
            else:
                daily = ec2.daily_image(release=release, arch="arm64")
        else:
            daily = ec2.daily_image(release=release)

        serial = ec2.image_serial(daily)

        print("Daily image for", release, "is", daily)
        print("Image serial:", serial)

        for ninstance in range(instances):
            instance_data = Path(datadir, "instance_" + str(ninstance))
            instance_data.mkdir()

            # This tag name will be inherited by the launched instance.
            # We want it to be unique and to contain an easily parsable
            # timestamp (UTC seconds since epoch), which we will use to
            # detemine if an instance is stale and should be terminated.
            tag = "bootspeed-" + str(int(dt.datetime.utcnow().timestamp()))
            ec2.tag = tag

            print("Launching instance", ninstance + 1, "of", instances, "tag:",
                  ec2.tag)
            instance = ec2.launch(
                daily,
                instance_type=self.inst_type,
                SubnetId=self.subnetid,
                SecurityGroupIds=self.sgid,
                Placement={"AvailabilityZone": self.availability_zone},
                wait=False,
            )

            try:
                # Hammer the instance SSH daemon to log the first login time.
                ssh_hammer(instance, self.ssh_privkey_path)

                # If the availability zone is not specified a random one is
                # assigned. We want to make sure the next instances (if any)
                # will use the same zone, so we save it.
                if not self.availability_zone:
                    self.availability_zone = instance.availability_zone

                measure_instance(instance, instance_data, reboots)
            finally:
                print("Deleting the instance.")
                instance.delete(wait=False)

        metadata = gen_metadata(
            cloud=self.cloud,
            region=self.region,
            availability_zone=self.availability_zone,
            inst_type=self.inst_type,
            release=self.release,
            cloudid=daily,
            serial=serial,
        )

        return metadata