Skip to content

Instantly share code, notes, and snippets.

@maatthc
Last active May 6, 2016 03:54
Show Gist options
  • Save maatthc/d71eee6c3abb66bb714bbff494c9b15f to your computer and use it in GitHub Desktop.
Save maatthc/d71eee6c3abb66bb714bbff494c9b15f to your computer and use it in GitHub Desktop.
It cancels Baremetal servers running at SoftLayer.It also power off the server and muted the monitoring at Datadog.
"""
Cancel the server with SoftLayer, turn it off and mute the Datadog alerts for it.
The host will be available until the next 16th day of the month.
"""
import SoftLayer
import sys
from CC_SoftLayer import get_credentials as sl_get_cred
from CC_Datadog import dd_init
# Fix Python 2.x.
try:
input = raw_input
except NameError:
pass
# Init APIs
username, api_key = sl_get_cred()
dd_api = dd_init()
client = SoftLayer.Client(username=username, api_key=api_key)
hardware_manager = SoftLayer.HardwareManager(client)
try :
hardware = hardware_manager.list_hardware(hostname='demo')
print("Authenticated!")
except Exception as e:
print("Error connecting to SoftLayer: %s" %str(e))
sys.exit(234)
hostnames = input("Enter the Server name list (separated by space): ").split()
print('\n Server List: ' + str(hostnames))
for hostname in hostnames:
if ".com" in hostname:
hostname = hostname.split(".")[0]
hardware = hardware_manager.list_hardware(hostname=hostname)
print(hardware)
for server in hardware:
print("Canceling Hostname: %s - ID: %s: " % (server['id'], server['hostname']))
answer = input(" --> Continue? (Y/N): ")
if answer.lower() is "y" or "yes":
try:
# Cancel and power off at SL
hardware_manager.cancel_hardware(server['id'], reason='unneeded', comment='', immediate=False)
except Exception as e:
if not "ticket already exists" in str(e):
print('Error: %s..' % e)
sys.exit(191)
else:
print("The host %s was already cancelled : %s" %(server['fullyQualifiedDomainName'], str(e)) )
try:
client['Hardware_Server'].powerOff(id=server['id'])
# Mute a hosts on Datadog
hosts = dd_api.Infrastructure.search(q="hosts:" + server['fullyQualifiedDomainName'])
if len(hosts['results']['hosts']) > 0:
dd_api.Host.mute(hosts['results']['hosts'][0])
print("Server cancelled, powered off and muted: " + server['fullyQualifiedDomainName'])
else:
print("Server is not present on Datadog. Was it renamed?")
except Exception as e:
print('Error: %s..' % e)
sys.exit(192)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment