Skip to content

Instantly share code, notes, and snippets.

@valentinbud
Created May 7, 2015 20:00
Show Gist options
  • Save valentinbud/c7a21d32ed76669b0fc2 to your computer and use it in GitHub Desktop.
Save valentinbud/c7a21d32ed76669b0fc2 to your computer and use it in GitHub Desktop.
cloudgist#0
#!py
import salt.utils.smtp as smtp
import salt.cloud.clouds.opennebula as ONE
import logging
import xmlrpclib
import xml.etree.ElementTree as ET
import binascii
import os
# Logging
log = logging.getLogger(__name__)
# SMTP OPTIONS
SMTP_OPTS = {
'smtp.host': 'localhost',
'smtp.from': 'cortexCloud <[email protected]>',
'smtp.to': '[email protected]'
}
# Helper functions
def _get_xml_rpc():
xml_rpc = __opts__['ccc_xml_rpc']
oneuser = __opts__['ccc_user']
onepassword = __opts__['ccc_password']
server = xmlrpclib.ServerProxy(xml_rpc)
return server, oneuser, onepassword
# RUN
def run():
'''
Incoming API request data.
'''
credentials = data['post']
print __opts__
'''
Connect to OpenNebula and retrieve users.
'''
server, oneuser, onepassword = _get_xml_rpc()
print server, oneuser, onepassword
userpool = server.one.userpool.info(oneuser+':'+onepassword)[1]
users = {}
for user in ET.fromstring(userpool).findall('USER'):
users[user.find('NAME').text] = ONE._xmltodict(user)
for key, user in users.items():
if 'cortexup_token' in user['template']:
if user['template']['cortexup_token'] == credentials['cortexup_token']:
new_cortexup_token = binascii.b2a_hex(os.urandom(20))
reset_cortexup_token = server.one.user.update(
oneuser+':'+onepassword,
int(user['id']), 'CORTEXUP_TOKEN=' + new_cortexup_token, 1)
vm_ = __opts__.get('cortexcloud.instance', {})
vm_['reactor'] = True
vm_['instances'] = credentials['ciu']
minion = {}
minion['smtp.to'] = user['name']
minion['smtp.subject'] = 'cortexCloud Instance `' + credentials['ciu'] + '` Ready'
minion['smtp.content'] = 'Hello there Mighty Developer,\n\nI am Your Personal Cortex in the Cloud.\nI have a demonstration ready for you at:\n\n http://'+credentials['ciu']+'cloud.ctxag.com/demo\n\nUse admin to Enter :).\n\n---\nCrafted with Care for You,\nThe cortexCloud Team'
# vm_overrides = {}
# vm_overrides['minion'] = minion
kwargs = {}
kwargs['minion'] = minion
vm_['kwargs'] = kwargs
vm_list = []
print vm_
for key, value in vm_.iteritems():
print key + ':' + str(value)
if not key.startswith('__'):
vm_list.append({key: value})
print vm_list
ret = {
'cortexcloud/instance/start': {
'runner.cloud.profile': vm_list
}
}
print ret
return ret
return {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment