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 Azure instance."""
4
5import logging
6
7import pycloudlib
8from pycloudlib.cloud import ImageType
9
10cloud_config = """#cloud-config
11runcmd:
12 - echo 'hello' > /home/ubuntu/example.txt
13"""
14
15
16def save_keys(key_name: str, pub_key: str, priv_key: str):
17 """Save keys generated through Azure."""
18 pub_path = "pub_{}.pem".format(key_name)
19 priv_path = "priv_{}.pem".format(key_name)
20
21 with open(pub_path, "w", encoding="utf-8") as f:
22 f.write(pub_key)
23
24 with open(priv_path, "w", encoding="utf-8") as f:
25 f.write(priv_key)
26
27 return pub_path, priv_path
28
29
30def demo():
31 """Show example of using the Azure library.
32
33 Connects to Azure and launches released image. Then runs
34 through a number of examples.
35
36 PS: we assume in this example that you are logged into
37 you Azure account
38 """
39 with pycloudlib.Azure(tag="azure") as client:
40 image_id = client.daily_image(release="jammy", image_type=ImageType.MINIMAL)
41
42 pub_key, priv_key = client.create_key_pair(key_name="test_integration")
43 pub_path, priv_path = save_keys(
44 key_name="test",
45 pub_key=pub_key,
46 priv_key=priv_key,
47 )
48 client.use_key(pub_path, priv_path)
49
50 with client.launch(
51 image_id=image_id,
52 instance_type="Standard_DS2_v2", # default is Standard_DS1_v2
53 user_data=cloud_config,
54 ) as instance:
55 instance.wait()
56 print(instance.ip)
57 print(instance.execute("cat /home/ubuntu/example.txt"))
58
59 snapshotted_image_id = client.snapshot(instance)
60
61 with client.launch(image_id=snapshotted_image_id) as new_instance:
62 new_instance.wait()
63 new_instance.execute("whoami")
64
65
66def demo_pro():
67 """Show example of launchig a Ubuntu PRO image through Azure."""
68 with pycloudlib.Azure(tag="azure") as client:
69 image_id = client.daily_image(release="focal", image_type=ImageType.PRO)
70
71 pub_key, priv_key = client.create_key_pair(key_name="test_pro")
72 pub_path, priv_path = save_keys(
73 key_name="test_pro",
74 pub_key=pub_key,
75 priv_key=priv_key,
76 )
77 client.use_key(pub_path, priv_path)
78
79 print("Launching Focal Pro instance.")
80 with client.launch(
81 image_id=image_id,
82 instance_type="Standard_DS2_v2", # default is Standard_DS1_v2
83 ) as instance:
84 instance.wait()
85 print(instance.ip)
86 print(instance.execute("sudo ua status --wait"))
87
88
89def demo_pro_fips():
90 """Show example of launchig a Ubuntu PRO FIPS image through Azure."""
91 with pycloudlib.Azure(tag="azure") as client:
92 image_id = client.daily_image(release="focal", image_type=ImageType.PRO_FIPS)
93
94 pub_key, priv_key = client.create_key_pair(key_name="test_pro_fips")
95 pub_path, priv_path = save_keys(
96 key_name="test_pro_fips",
97 pub_key=pub_key,
98 priv_key=priv_key,
99 )
100 client.use_key(pub_path, priv_path)
101
102 print("Launching Focal Pro FIPS instance.")
103 with client.launch(
104 image_id=image_id,
105 instance_type="Standard_DS2_v2", # default is Standard_DS1_v2
106 ) as instance:
107 instance.wait()
108 print(instance.ip)
109 print(instance.execute("sudo ua status --wait"))
110
111
112def demo_pro_fips_updates():
113 """Show example of launchig a Ubuntu PRO FIPS image through Azure."""
114 with pycloudlib.Azure(tag="azure") as client:
115 image_id = client.daily_image(release="jammy", image_type=ImageType.PRO_FIPS_UPDATES)
116
117 pub_key, priv_key = client.create_key_pair(key_name="test_pro_fips")
118 pub_path, priv_path = save_keys(
119 key_name="test_pro_fips",
120 pub_key=pub_key,
121 priv_key=priv_key,
122 )
123 client.use_key(pub_path, priv_path)
124
125 print("Launching Focal Pro FIPS Updates instance.")
126 with client.launch(
127 image_id=image_id,
128 instance_type="Standard_DS2_v2", # default is Standard_DS1_v2
129 ) as instance:
130 instance.wait()
131 print(instance.ip)
132 print(instance.execute("sudo ua status --wait"))
133
134
135if __name__ == "__main__":
136 # Avoid polluting the log with azure info
137 logging.getLogger("adal-python").setLevel(logging.WARNING)
138 logging.getLogger("cli.azure.cli.core").setLevel(logging.WARNING)
139 logging.basicConfig(level=logging.DEBUG)
140
141 demo()
142 demo_pro()
143 demo_pro_fips()
144 demo_pro_fips_updates()