|
| 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 OCI instance.""" |
| 4 | + |
| 5 | +import logging |
| 6 | +import sys |
| 7 | + |
| 8 | +import pycloudlib |
| 9 | + |
| 10 | +def demo_cluster( |
| 11 | + availability_domain: str = None, |
| 12 | + compartment_id: str = None, |
| 13 | + vcn_name: str = None, |
| 14 | +): |
| 15 | + """Show example of using the OCI library to launch a cluster instance and ping between them.""" |
| 16 | + |
| 17 | + with pycloudlib.OCI( |
| 18 | + "pycl-oracle-cluster-demo", |
| 19 | + availability_domain=availability_domain, |
| 20 | + compartment_id=compartment_id, |
| 21 | + vcn_name=vcn_name, |
| 22 | + ) as client: |
| 23 | + instances = client.create_compute_cluster( |
| 24 | + image_id=client.released_image("noble"), |
| 25 | + instance_count=2, |
| 26 | + ) |
| 27 | + # get the private ips of the instances |
| 28 | + private_ips = [instance.private_ip for instance in instances] |
| 29 | + # try to ping each instance from each other instance at their private ip |
| 30 | + for instance in instances: |
| 31 | + for private_ip in private_ips: |
| 32 | + if private_ip != instance.private_ip: |
| 33 | + print(f"Pinging {private_ip} from {instance.private_ip}") |
| 34 | + r = instance.execute(f"ping -c 1 -W 5 {private_ip}") |
| 35 | + if not r.ok: |
| 36 | + print(f"Failed to ping {private_ip} from {instance.private_ip}") |
| 37 | + else: |
| 38 | + print(f"Successfully pinged {private_ip} from {instance.private_ip}") |
| 39 | + |
| 40 | +if __name__ == "__main__": |
| 41 | + logging.basicConfig(level=logging.DEBUG) |
| 42 | + if len(sys.argv) != 3: |
| 43 | + print( |
| 44 | + "No arguments passed via command line. " |
| 45 | + "Assuming values are set in pycloudlib configuration file." |
| 46 | + ) |
| 47 | + demo_cluster() |
| 48 | + else: |
| 49 | + passed_availability_domain = sys.argv[1] |
| 50 | + passed_compartment_id = sys.argv[2] |
| 51 | + passed_vcn_name = sys.argv[3] if len(sys.argv) == 4 else None |
| 52 | + demo_cluster(passed_availability_domain, passed_compartment_id, passed_vcn_name) |
0 commit comments