-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_ovh_ids.py
54 lines (45 loc) · 1.97 KB
/
get_ovh_ids.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
import sys
import ovh
def get_flavor_id(client, region, flavor_name):
flavors = client.get(f'/cloud/project/{os.environ["OVH_SERVICE_NAME"]}/flavor', region=region)
for flavor in flavors:
if flavor['name'] == flavor_name:
return flavor['id']
raise ValueError(f"Flavor '{flavor_name}' not found in region '{region}'")
def get_image_id(client, region, image_name, flavor_id):
images = client.get(f'/cloud/project/{os.environ["OVH_SERVICE_NAME"]}/image',
flavorType=flavor_id,
osType='linux',
region=region)
for image in images:
if image['name'] == image_name:
return image['id']
raise ValueError(f"Image '{image_name}' not found for flavor '{flavor_id}' in region '{region}'")
def main():
client = ovh.Client(
endpoint=os.environ['OVH_ENDPOINT'],
application_key=os.environ['OVH_APPLICATION_KEY'],
application_secret=os.environ['OVH_APPLICATION_SECRET'],
consumer_key=os.environ['OVH_CONSUMER_KEY']
)
region = os.environ['OVH_REGION']
flavor_name = os.environ['OVH_INSTANCE_FLAVOR_NAME']
image_name = os.environ['OVH_INSTANCE_IMAGE_NAME']
try:
flavor_id = get_flavor_id(client, region, flavor_name)
image_id = get_image_id(client, region, image_name, flavor_id)
# Write to GitHub Actions output file
with open(os.environ['GITHUB_OUTPUT'], 'a') as fh:
print(f"OVH_INSTANCE_FLAVOR_ID={flavor_id}", file=fh)
print(f"OVH_INSTANCE_IMAGE_ID={image_id}", file=fh)
# Also set environment variables for the current process
os.environ['OVH_INSTANCE_FLAVOR_ID'] = flavor_id
os.environ['OVH_INSTANCE_IMAGE_ID'] = image_id
print(f"Flavor ID: {flavor_id}")
print(f"Image ID: {image_id}")
except ValueError as e:
print(f"Error: {str(e)}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()