#! /usr/bin/python """ Setup SpotCloud broker account. """ import os, sys from subprocess import call import logging import commands import shutil import socket container_dir = os.path.dirname(os.path.realpath(__file__)) if container_dir not in sys.path: sys.path.append(container_dir) from setup_utils import ask, ask_yesno, get_ip, is_root, is_xen, is_vt_enabled, is_kvm import sqlobject def get_ecp_config_path(): ECP_DIR_PATH = "/opt/enomalism2/" ECP_CONFIG_PATH=os.path.join(ECP_DIR_PATH, 'config', '%s.cfg' % socket.gethostname()) if not os.path.isfile(ECP_CONFIG_PATH): raise RuntimeError( "could not find ECP config at %s" % ECP_CONFIG_PATH) return ECP_CONFIG_PATH def init_ecp(): "ECP need to be initialized before any use" import turbogears ECP_CONFIG_PATH = get_ecp_config_path() turbogears.config.update( turbogears.config.config_obj( ECP_CONFIG_PATH, "enomalism2.config")) def install_quota(name, vms, cpus, memory, storage): from elastic_hosting.model import Quota, uuid as elastic_hosting_uuid if len(list(Quota.select(Quota.q.name==name))): # is already installed return return Quota(name=name, max_vms=vms, max_cpus=cpus, max_memory=memory, max_storage=storage) def create_broker_user(user, email, password, quota, network=None): from enomalism2 import identity from enomalism2.model import Package from elastic_hosting.model import HardwareProfile, GroupQuota, \ uuid as elastic_hosting_uuid # Handle older versions of ECP gracefully... try: from elastic_hosting.model import SpotcloudMock except: class SpotcloudMock: uuid = "d14e6e30-6445-41fd-874a-6c6e6556525a" try: group = identity.EnomalismGroup.by_group_name('broker') except sqlobject.SQLObjectNotFound: group = identity.EnomalismGroup(group_name='broker', display_name='SpotCloud Broker') try: user = identity.EnomalismUser.by_user_name(user) except sqlobject.SQLObjectNotFound: user = identity.EnomalismUser(user_name=user, display_name="SpotCloud", password=password, email_address=email) if not group.has_permission(elastic_hosting_uuid, 'crud'): group.set_permission(elastic_hosting_uuid, 'crud') if network and not group.has_permission(network.uuid, 'r'): group.set_permission(network.uuid, 'r') if not group.has_permission(SpotcloudMock.uuid, 'r'): group.set_permission(SpotcloudMock.uuid, 'r') if not len(list(GroupQuota.select(GroupQuota.q.group==group))): GroupQuota(group=group, quota=quota) if group not in user.groups: user.addTG_Group(group) return user def find_networks(): from elastic_hosting.model import VirtualNetwork return list(VirtualNetwork.select()) def ask_object(msg, list, intro='', formatter=lambda x:unicode(x)): my_msg ="%s [enter 'None' to skip network configuration]: " % msg while True: print intro for idx, obj in enumerate(list): print " %d: %s" % (idx+1, formatter(obj)) try: val = raw_input(my_msg).strip() if val.lower() == 'none': return val = int(val) except ValueError, TypeError: continue if val<1 or val>len(list): continue return list[val-1] def go(install_dir=None, is_spotcloud=False): #Only ask about spotcloud if we don't have a spotcloud key if not is_spotcloud and ask_yesno('Do you want to enable SpotCloud?') == 'no': return {'broker_enabled': 'yes', 'broker_user_id': 'dummy'} if install_dir is None: install_dir = os.path.normpath(os.path.join(container_dir, '..')) #if ask_yesno('Do you want to enable SpotCloud?') == 'no': # return {'broker_enabled': 'yes', 'broker_user_id': 'dummy'} broker_user = ask('Please provide broker username', 'spotcloud') broker_email = ask('Please provide broker user email address') broker_passwd = ask('Please provide broker user password') max_vms = int(ask('Please provide maximum number of VMs for broker user', 100, pattern='\d+')) max_cpus = int(ask('Please provide maximum number of CPUs for broker user', 10, pattern='\d+')) max_mem = int(ask('Please provide maximum memory usage for broker user (in MB)', 10*1024, pattern='\d+')) max_storage = int(ask('Please provide maximum storage usage for broker user (in MB)', 100*1024, pattern='\d+')) available_networks = find_networks() network = None if available_networks: network = ask_object('Please select which network SpotCloud should use', available_networks, 'Available networks:', lambda x: x.name) # default_network = find_default_network() # if default_network: # if ask_yesno('SpotCloud uses the default network for all created machines. Is this OK?') == 'no': # default_network = None print "Installing SpotCloud quota" quota = install_quota('SpotCloud Quota', max_vms, max_cpus, max_mem, max_storage) print "Installing SpotCloud user" user = create_broker_user(broker_user, broker_email, broker_passwd, quota, network=network) print '''Finished. You'll need to update %(configpath)s with the following: spotcloud.enabled = True spotcloud.broker_user = '%(user_uuid)s' You can now create new hardware profiles or use existing profiles. Any profiles that you want displayed on SpotCloud need read permission for '%(user_uuid)s'. Please restart ECP: /etc/init.d/ecp restart /etc/init.d/ecpmanager restart ''' % dict(user_uuid=user.uuid, configpath=get_ecp_config_path()) return {'broker_enabled': 'yes', 'broker_user_id': user.id, 'broker_user_uuid': user.uuid} if __name__ == '__main__': init_ecp() result = go()