IBM

  1#!/usr/bin/env python3
  2# This file is part of pycloudlib. See LICENSE file for license information.
  3"""Basic examples of various lifecycle with an IBM instance."""
  4
  5import logging
  6import os
  7
  8import pycloudlib
  9import pycloudlib.cloud
 10from pycloudlib.ibm.cloud import IBM
 11
 12
 13def snapshot(ibm: IBM, daily):
 14    """Create a snapshot from a customized image and launch it."""
 15    with ibm.launch(daily) as instance:
 16        instance.wait()
 17        instance.execute("touch custom_config_file")
 18
 19        image = ibm.snapshot(instance)
 20        with ibm.launch(image, name="example-snapshot") as new_instance:
 21            new_instance.execute("ls")
 22
 23    ibm.delete_image(image)
 24
 25
 26def custom_vpc(ibm: IBM, daily):
 27    """Launch instances using a custom VPC."""
 28    vpc = ibm.get_or_create_vpc(name="test-vpc")
 29    with ibm.launch(daily, vpc=vpc) as instance:
 30        instance.wait()
 31        instance.execute("whoami")
 32
 33    # vpc.delete will also delete any associated instances in that VPC
 34    vpc.delete()
 35
 36
 37def launch_basic(ibm: IBM, daily, instance_type):
 38    """Show basic functionality on instances.
 39
 40    Simple launching of an instance, run a command, and delete.
 41    """
 42    with ibm.launch(
 43        daily,
 44        instance_type=instance_type,
 45        floating_ip_substring="default-floating-ip",
 46    ) as instance:
 47        instance.wait()
 48        print(instance.execute("lsb_release -a"))
 49
 50        instance.shutdown()
 51        instance.start()
 52        instance.restart()
 53
 54        # Various Attributes
 55        print(instance.ip)
 56        print(instance.id)
 57
 58
 59def manage_ssh_key(ibm: pycloudlib.IBM, key_name):
 60    """Manage ssh keys for ibm instances."""
 61    if key_name in ibm.list_keys():
 62        ibm.delete_key(key_name)
 63
 64    pub_key_path = "ibm-pubkey"
 65    priv_key_path = "ibm-privkey"
 66    pub_key, priv_key = ibm.create_key_pair()
 67
 68    with open(pub_key_path, "w", encoding="utf-8") as f:
 69        f.write(pub_key)
 70
 71    with open(priv_key_path, "w", encoding="utf-8") as f:
 72        f.write(priv_key)
 73
 74    os.chmod(pub_key_path, 0o600)
 75    os.chmod(priv_key_path, 0o600)
 76
 77    ibm.use_key(
 78        public_key_path=pub_key_path,
 79        private_key_path=priv_key_path,
 80        name=key_name,
 81    )
 82
 83
 84def demo():
 85    """Show example of using the IBM library.
 86
 87    Connects to IBM and finds the latest daily image. Then runs
 88    through a number of examples.
 89    """
 90    with pycloudlib.IBM(tag="pycloudlib-example") as ibm:
 91        manage_ssh_key(ibm, key_name="pycloudlib-example-key")
 92
 93        daily = ibm.daily_image(release="jammy")
 94
 95        # "bx2-metal-96x384" for a bare-metal instance
 96        launch_basic(ibm, daily, "bx2-2x8")
 97
 98
 99if __name__ == "__main__":
100    logging.basicConfig(level=logging.DEBUG)
101    demo()