Cloud-init, Exception Handling
This commit is contained in:
12
create.py
12
create.py
@@ -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:
|
||||
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)
|
||||
|
||||
23
delete.py
23
delete.py
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
from hcloud import Client
|
||||
from hcloud import Client, APIException
|
||||
import argparse
|
||||
|
||||
# Get Servername
|
||||
@@ -12,14 +12,19 @@ args = parser.parse_args()
|
||||
serverName = args.serverName
|
||||
apiKey = args.apiKey
|
||||
|
||||
# Please paste your API token here between the quotes
|
||||
client = Client(token=apiKey)
|
||||
|
||||
server = client.servers.get_by_name(name=serverName)
|
||||
print("Deleting: ", server.name)
|
||||
response = client.servers.delete(server)
|
||||
try:
|
||||
client = Client(token=apiKey)
|
||||
server = client.servers.get_by_name(name=serverName)
|
||||
|
||||
print("Remaining servers: ")
|
||||
servers = client.servers.get_all()
|
||||
for server in servers:
|
||||
print("Deleting: ", server.name)
|
||||
response = client.servers.delete(server)
|
||||
|
||||
print("Remaining servers: ")
|
||||
servers = client.servers.get_all()
|
||||
for server in servers:
|
||||
print(server.name)
|
||||
|
||||
|
||||
except APIException as e:
|
||||
print("API Error ", e)
|
||||
|
||||
17
details.py
17
details.py
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
from hcloud import Client
|
||||
from hcloud import Client, APIException
|
||||
import argparse
|
||||
|
||||
# Get Servername
|
||||
@@ -12,9 +12,12 @@ args = parser.parse_args()
|
||||
serverName = args.serverName
|
||||
apiKey = args.apiKey
|
||||
|
||||
# Please paste your API token here between the quotes
|
||||
client = Client(token=apiKey)
|
||||
server = client.servers.get_by_name(serverName)
|
||||
print("Name: ", server.name)
|
||||
print("Status: ", server.status)
|
||||
print("Type: ", server.server_type.name)
|
||||
try:
|
||||
client = Client(token=apiKey)
|
||||
server = client.servers.get_by_name(serverName)
|
||||
print("Name: ", server.name)
|
||||
print("Status: ", server.status)
|
||||
print("Type: ", server.server_type.name)
|
||||
|
||||
except APIException as e:
|
||||
print("API Error ", e)
|
||||
|
||||
14
list.py
14
list.py
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
from hcloud import Client
|
||||
from hcloud import Client, APIException
|
||||
import argparse
|
||||
|
||||
# Get Servername
|
||||
@@ -8,10 +8,14 @@ parser.add_argument("apiKey")
|
||||
args = parser.parse_args()
|
||||
apiKey = args.apiKey
|
||||
|
||||
# Please paste your API token here between the quotes
|
||||
client = Client(token=apiKey)
|
||||
|
||||
servers = client.servers.get_all()
|
||||
for server in servers:
|
||||
try:
|
||||
client = Client(token=apiKey)
|
||||
|
||||
servers = client.servers.get_all()
|
||||
for server in servers:
|
||||
print("Servername:", server.name)
|
||||
print("Servername:", server.status)
|
||||
|
||||
except APIException as e:
|
||||
print("API Error ", e)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#!/usr/bin/env python3
|
||||
from hcloud import Client
|
||||
from hcloud import Client, APIException
|
||||
from hcloud.server_types.domain import ServerType
|
||||
import time
|
||||
import argparse
|
||||
|
||||
import sys
|
||||
|
||||
# Get Servername
|
||||
parser = argparse.ArgumentParser()
|
||||
@@ -18,10 +18,12 @@ serverType = args.serverType
|
||||
apiKey = args.apiKey
|
||||
|
||||
try:
|
||||
# Please paste your API token here between the quotes
|
||||
client = Client(token=apiKey)
|
||||
|
||||
server = client.servers.get_by_name(name=serverName)
|
||||
|
||||
if server.server_type.name == serverType:
|
||||
sys.exit('Server type is already: %s' % serverType)
|
||||
|
||||
if server.status != "off":
|
||||
print("Stopping: ", server.name)
|
||||
response = client.servers.power_off(server)
|
||||
|
||||
Reference in New Issue
Block a user