1
0

Cloud-init, Exception Handling

This commit is contained in:
Philipp Klüter
2020-05-22 16:42:02 +02:00
parent c8248ef7bf
commit 6e194e0c52
5 changed files with 51 additions and 33 deletions

View File

@@ -1,6 +1,5 @@
#!/usr/bin/env python3
from hcloud import Client
from hcloud import APIException
from hcloud import Client, APIException
from hcloud.server_types.domain import ServerType
from hcloud.images.domain import Image
import argparse
@@ -10,6 +9,8 @@ parser = argparse.ArgumentParser()
parser.add_argument("serverName")
parser.add_argument("serverType")
parser.add_argument("serverImage")
parser.add_argument('-ci', '--cloudInit', dest='ciFile', metavar='cloud-init filepath')
parser.add_argument("apiKey")
args = parser.parse_args()
@@ -18,12 +19,15 @@ serverName = args.serverName
serverType = args.serverType
serverImage = args.serverImage
apiKey = args.apiKey
ciFile = args.ciFile
try:
with open('/Users/Philipp/Documents/Server/cloud-init/default.yaml', 'r') as file:
cloud_init = file.read()
if ciFile:
with open(ciFile, 'r') as file:
cloud_init = file.read()
else:
cloud_init=''
# Please paste your API token here between the quotes
client = Client(token=apiKey)
response = client.servers.create(name=serverName, server_type=ServerType(
name=serverType), image=Image(name=serverImage), user_data=cloud_init)