Пример #1
0
def basic_lifecycle():
    """Demonstrate basic set of lifecycle operations with LXD."""
    lxd = pycloudlib.LXDContainer('example-basic')
    inst = lxd.launch(image_id=RELEASE)
    inst.delete()

    name = 'pycloudlib-daily'
    inst = lxd.launch(name=name, image_id=RELEASE)
    inst.console_log()

    result = inst.execute('uptime')
    print(result)
    print(result.return_code)
    print(result.ok)
    print(result.failed)
    print(bool(result))

    inst.shutdown()
    inst.start()
    inst.restart()

    # Custom attributes
    print(inst.ephemeral)
    print(inst.state)

    inst = lxd.get_instance(name)
    inst.delete()
Пример #2
0
    def measure(self, datadir, instances=1, reboots=1):
        """
        Measure LXD containers.
        Returns the measurement metadata as a dictionary
        """
        print("Perforforming measurement on LXD")

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

        tag = "bootspeed-" + self.inst_type.replace(".", "") + "-" + self.release

        if self.is_vm:
            lxd = pycloudlib.LXDVirtualMachine(tag=tag, timestamp_suffix=False)
        else:
            lxd = pycloudlib.LXDContainer(tag=tag, timestamp_suffix=False)

        lxd.key_pair = pycloudlib.key.KeyPair(
            self.ssh_pubkey_path, self.ssh_privkey_path
        )
        image = lxd.daily_image(release=release)
        serial = lxd.image_serial(image)

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

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

            print("Launching instance", ninstance + 1, "of", instances)
            instance = lxd.launch(
                image_id=image, instance_type=self.inst_type, name=tag, ephemeral=True
            )
            print("Instance launched (%s)" % tag)

            try:
                measure_instance(instance, instance_data, reboots)
            finally:
                print("Deleting the instance.")
                instance.delete()

        # On LXD we can consider the machine the measurement is run on as the
        # 'region'; platform.node() returns its hostname.
        region = platform.node()
        metadata = gen_metadata(
            cloud=self.cloud,
            region=region,
            inst_type=self.inst_type,
            release=self.release,
            cloudid=image,
            serial=serial,
        )

        return metadata
Пример #3
0
def launch_options():
    """Demonstrate various launching scenarios.

    First up is launching with a different profile, in this case with
    two profiles.

    Next, is launching an ephemeral instance with a different image
    remote server.

    Then, an instance with custom network, storage, and type settings.
    This is an example of booting an instance without cloud-init so
    wait is set to False.

    Finally, an instance with custom configurations options.
    """
    lxd = pycloudlib.LXDContainer('example-launch')
    kvm_profile = textwrap.dedent("""\
        devices:
          kvm:
            path: /dev/kvm
            type: unix-char
        """)

    lxd.create_profile(profile_name="kvm", profile_config=kvm_profile)

    lxd.launch(name='pycloudlib-kvm',
               image_id=RELEASE,
               profile_list=['default', 'kvm'])
    lxd.delete_instance('pycloudlib-kvm')

    lxd.launch(name='pycloudlib-ephemeral',
               image_id='ubuntu:%s' % RELEASE,
               ephemeral=True)
    lxd.delete_instance('pycloudlib-ephemeral')

    lxd.launch(name='pycloudlib-custom-hw',
               image_id='images:ubuntu/xenial',
               network='lxdbr0',
               storage='default',
               inst_type='t2.micro',
               wait=False)
    lxd.delete_instance('pycloudlib-custom-hw')

    lxd.launch(name='pycloudlib-privileged',
               image_id=RELEASE,
               config_dict={
                   'security.nesting': 'true',
                   'security.privileged': 'true'
               })
    lxd.delete_instance('pycloudlib-privileged')
Пример #4
0
def launch_multiple():
    """Launch multiple instances.

    How to quickly launch multiple instances with LXD. This prevents
    waiting for the instance to start each time. Note that the
    wait_for_delete method is not used, as LXD does not do any waiting.
    """
    lxd = pycloudlib.LXDContainer('example-multiple')

    instances = []
    for num in range(3):
        inst = lxd.launch(name='pycloudlib-%s' % num,
                          image_id=RELEASE,
                          wait=False)
        instances.append(inst)

    for instance in instances:
        instance.wait()

    for instance in instances:
        instance.delete()
Пример #5
0
def modify_instance():
    """Demonstrate how to modify and interact with an instance.

    The inits an instance and before starting it, edits the the
    container configuration.

    Once started the instance demonstrates some interactions with the
    instance.
    """
    lxd = pycloudlib.LXDContainer('example-modify')

    inst = lxd.init('pycloudlib-modify-inst', RELEASE)
    inst.edit('limits.memory', '3GB')
    inst.start()

    inst.execute('uptime > /tmp/uptime')
    inst.pull_file('/tmp/uptime', '/tmp/pulled_file')
    inst.push_file('/tmp/pulled_file', '/tmp/uptime_2')
    inst.execute('cat /tmp/uptime_2')

    inst.delete(wait=False)
Пример #6
0
def snapshot_instance():
    """Demonstrate snapshot functionality.

    This shows the lifecycle of booting an instance and cleaning it
    before creating a snapshot.

    Next, both create the snapshot and immediately restore the original
    instance to the snapshot level.
    Finally, launch another instance from the snapshot of the instance.
    """
    lxd = pycloudlib.LXDContainer('example-snapshot')
    inst = lxd.launch(name='pycloudlib-snapshot-base', image_id=RELEASE)

    snapshot_name = 'snapshot'
    inst.local_snapshot(snapshot_name)
    inst.restore(snapshot_name)

    child = lxd.clone('%s/%s' % (inst.name, snapshot_name),
                      'pycloudlib-snapshot-child')

    child.delete()
    inst.delete_snapshot(snapshot_name)
    inst.delete(wait=False)