Exemple #1
0
def create_task(constellation_name, data):
    """
    Adds a task to a constellation simulation ask list. The data is a
    dictionary. The keys are:
    - task_title
    - ros_package
    - launch_file
    - launch_args
    - timeout
    - latency
    - uplink_data_cap
    - downllink_data_cap
    - local_start
    - local_stop
    - vrc_id
    - vrc_num
    """
    try:
        log('create_task')
        task_id = "t" + get_unique_short_name()
        data['task_id'] = task_id
        data['task_state'] = "ready"
        data['task_message'] = 'Ready to run'

        cs = ConstellationState(constellation_name)
        tasks = cs.get_value('tasks')
        tasks.append(data)

        # save new task list in db
        cs.set_value('tasks', tasks)
        log('task %s/%s created' % (constellation_name, task_id))
    except Exception, e:
        log("update_task error %s" % e)
        tb = traceback.format_exc()
        log("traceback: %s" % tb)
Exemple #2
0
def monitor(constellation_name):
    """
    Loop that monitors the execution of a constellation
    """
    try:
        proc = multiprocessing.current_process().name
        log("monitoring [%s] from proc '%s'" % (constellation_name, proc))

        constellation = ConstellationState(constellation_name)
        config = constellation.get_value('configuration')
        constellation_plugin = get_plugin(config)

        counter = 0
        done = False
        while not done:
            try:
                log("monitor %s (%s)" % (constellation_name, counter))
                done = constellation_plugin.monitor(constellation_name,
                                                    counter)
                log("monitor [%s] returned %s" % (constellation_name, done))
                counter += 1
            except Exception, e:
                done = False
                log("cloudsimd.py monitor error: %s" % e)
                tb = traceback.format_exc()
                log("traceback:  %s" % tb)

        log("monitor %s from proc %s DONE" % (proc, constellation_name))
Exemple #3
0
def reset_tasks(name=None):
    """
    Resets tasks for constellation name. If name is None,
    tasks are reset for all running constellations.

    After reset, the current task is empty and any task that was
     - starting
     - running
     - stopping
    set to stopped, and it can't be run again.
    Stopped tasks are not affected
    """
    names = []
    if name:
        names = [name]
    else:
        names = get_constellation_names()
    for constellation_name in names:
        cs = ConstellationState(constellation_name)
        cs.set_value('current_task', '')
        tasks = cs.get_value('tasks')
        for task in tasks:
            task_id = task['task_id']
            state = task['task_state']
            if state not in ['ready']:
                cs.update_task_value(task_id, 'task_state', 'ready')
                cs.update_task_value(task_id, 'task_message', 'Ready to run')
Exemple #4
0
def delete_task(constellation_name, task_id):
    """
    Removes a task from the constellation's simulation task list
    """
    log('delete task')
    try:
        log("delete_task %s/%s" % (constellation_name, task_id))
        cs = ConstellationState(constellation_name)
        cs.delete_task(task_id)

    except Exception, e:
        log("delete_task error %s" % e)
        tb = traceback.format_exc()
        log("traceback:  %s" % tb)
Exemple #5
0
def update_task(constellation_name, data):
    """
    Updates a task to a constellation simulation ask list.
    """
    try:
        task_id = data['task_id']
        log("update_task %s/%s" % (constellation_name, task_id))
        cs = ConstellationState(constellation_name)
        cs.update_task(task_id, data)
        log("updated: %s" % task_id)
    except Exception, e:
        log("update_task error %s" % e)
        tb = traceback.format_exc()
        log("traceback:  %s" % tb)
Exemple #6
0
def stop_cloudsim_notebook(constellation_name):
    """
    Stops the gzweb server
    """
    proc = multiprocessing.current_process().name
    log("stop_gzweb '%s' from proc '%s'" % (constellation_name, proc))
    constellation = ConstellationState(constellation_name)
    try:
        config = constellation.get_value('configuration')
        constellation_plugin = get_plugin(config)
        constellation.set_value(CLOUDSIM_NOTEBOOK_KEY, 'stopping')
        constellation_plugin.stop_cloudsim_notebook(constellation_name)
    except:
        tb = traceback.format_exc()
        log("STOP_cloudsim_notebook ERROR traceback:  %s" % tb)
Exemple #7
0
def start_gzweb(constellation_name):
    """
    Starts the gzweb server
    """
    proc = multiprocessing.current_process().name
    log("start_gzweb '%s' from proc '%s'" % (constellation_name, proc))
    constellation = ConstellationState(constellation_name)
    try:
        config = constellation.get_value('configuration')
        constellation_plugin = get_plugin(config)
        constellation.set_value("gzweb", 'starting')
        constellation_plugin.start_gzweb(constellation_name)
    except:
        tb = traceback.format_exc()
        log("START_GZWEB ERROR traceback:  %s" % tb)
Exemple #8
0
def update_tasks(new_task_data, name=None):
    """
    Update all the tasks for a given constellation or all of them (if None)
    @param new_task_data Dictionary with the new task fields
    """
    names = []
    if name:
        names = [name]
    else:
        names = get_constellation_names()
    for constellation_name in names:
        cs = ConstellationState(constellation_name)
        tasks = cs.get_value('tasks')
        for task in tasks:
            task_id = task['task_id']
            for k, v in new_task_data.iteritems():
                cs.update_task_value(task_id, k, v)
Exemple #9
0
def launch_cmd(root_dir, data):
    constellation_name = "c" + get_unique_short_name()
    constellation = ConstellationState(constellation_name)
    # put the minimum information in Redis so that the monitoring can work
    constellation.set_value('constellation_state', 'launching')
    constellation.set_value('configuration', data['configuration'])

    async_launch(constellation_name, data)
    async_monitor(constellation_name)
Exemple #10
0
def launch(constellation_name, data):
    """
    Deploys a constellation. The configuration determines what type of
    constellation will be launched. the constellation directory is where all
    data should be saved (ssh keys, downloadable scripts, etc.)
    """
    proc = multiprocessing.current_process().name
    log("LAUNCH [%s] from proc %s" % (constellation_name, proc))
    constellation = ConstellationState(constellation_name)
    try:
        configs = _get_all_configurations()
        provider = data['cloud_provider']
        region_name = data['region']
        config_name = data['configuration']

        cloudsim_config = get_cloudsim_config()
        log("region: %s" % region_name)

        confs = configs[provider]['regions'][region_name]['configurations']

        cfg = None
        try:
            cfg = [x for x in confs if x['name'] == config_name][0]
        except:
            raise Exception(config_name + " is not a invalid configuration",
                            "for this provider/region")
        log("configuration: %s" % cfg)
        log("preparing REDIS and filesystem %s" % constellation_name)
        init_constellation_data(constellation_name, data, cloudsim_config)
        constellation_plugin = get_plugin(config_name)
        constellation.set_value('constellation_state', 'launching')
        log("calling the plugin's launch function")
        constellation_plugin.launch(cfg, constellation_name, data)
        constellation.set_value('constellation_state', 'running')

        log("Launch of constellation %s done" % constellation_name)
    except Exception, e:
        tb = traceback.format_exc()
        constellation.set_value(
            'error', 'Launch aborted with exception: '
            '%s<pre>%s</pre>' % (e, tb))
        log("LAUNCH ERROR traceback:  %s" % tb)
Exemple #11
0
def remove_tasks(name=None):
    """
    Removes tasks for constellation name. If name is None,
    tasks are removed for all running constellations.
    """
    names = []
    if name:
        names = [name]
    else:
        names = get_constellation_names()
    for constellation_name in names:
        cs = ConstellationState(constellation_name)
        cs.set_value('tasks', [])
        cs.set_value('current_task', '')
Exemple #12
0
def update_constellation(constellation_name):
    """
    Updates the constellation via the cloud interface.
    This is an operation applied to a running constellation to ugrade the
    software
    """
    proc = multiprocessing.current_process().name
    log("update '%s' from proc '%s'" % (constellation_name, proc))
    constellation = ConstellationState(constellation_name)
    try:
        config = constellation.get_value('configuration')
        constellation_plugin = get_plugin(config)
        constellation.set_value('constellation_state', 'updating')
        constellation_plugin.update(constellation_name)
        constellation.set_value('constellation_state', 'running')
    except:
        tb = traceback.format_exc()
        log("UPDATE ERROR traceback:  %s" % tb)
Exemple #13
0
def stop_task(constellation_name):
    """
    Stops the current running tasks on a constellation. If no simulation task
    is running.
    """
    try:
        log("stop_task %s" % (constellation_name))
        cs = ConstellationState(constellation_name)
        config = cs.get_value('configuration')
        constellation_plugin = get_plugin(config)

        task_id = cs.get_value('current_task')
        if task_id != '':
            task = cs.get_task(task_id)

            # you can only stop a running task
            if task['task_state'] == 'running':

                log('task_state stopping')
                cs.update_task_value(task_id, 'task_state', 'stopping')
                cs.update_task_value(task_id, 'stop_time',
                                     datetime.datetime.utcnow().isoformat())
                try:
                    log('calling stop task')
                    constellation_plugin.stop_task(constellation_name, task)
                except Exception, e:
                    tb = traceback.format_exc()
                    log('task error during stop')
                    log("traceback:  %s" % tb)
                else:
                    log('task stopped successfully')
                finally:
                    cs.update_task_value(task_id, 'task_state', 'stopped')
                    cs.set_value('current_task', '')
Exemple #14
0
def start_task(constellation_name, task_id):
    """
    Starts a simulation task on a constellation.
    Only one task can run at a time.
    """
    try:
        log("start_task %s/%s" % (constellation_name, task_id))
        cs = ConstellationState(constellation_name)
        config = cs.get_value('configuration')
        constellation_plugin = get_plugin(config)
        current_task = cs.get_value('current_task')
        if current_task == '':
            task = cs.get_task(task_id)
            task_state = task['task_state']
            if task_state == 'ready':
                cs.set_value('current_task', task_id)
                log('task_state starting')
                cs.update_task_value(task_id, 'task_message', '')
                cs.update_task_value(task_id, 'task_state', 'starting')
                cs.update_task_value(task_id, 'start_time',
                                     datetime.datetime.utcnow().isoformat())
                # no other task running, and task is ready
                try:
                    constellation_plugin.start_task(constellation_name, task)
                except Exception, e:
                    log("Start task error %s" % e)
                    tb = traceback.format_exc()
                    log("traceback:  %s" % tb)
                    cs.update_task_value(task_id, 'task_message',
                                         'Task failed to start: %s' % (e))
                    task = cs.get_task(task_id)
                    constellation_plugin.stop_task(constellation_name, task)
                    cs.update_task_value(task_id, 'task_state', 'stopped')
                    cs.set_value('current_task', '')
                    raise
                cs.update_task_value(task_id, 'task_state', 'running')
                log('task_state running')
            else:
                log("Task is not ready (%s)" % task_state)
        else:
Exemple #15
0
def terminate(constellation_name):
    """
    Terminates the constellation via the cloud interface.
    This could give the resources back to the cloud provider (AWS),
    or wipe data.
    """
    proc = multiprocessing.current_process().name
    log("terminate '%s' from proc '%s'" % (constellation_name, proc))

    constellation = ConstellationState(constellation_name)
    try:
        # clear the error message
        constellation.set_value('error', '')
        config = constellation.get_value('configuration')
        log("    configuration is '%s'" % (config))
        constellation_plugin = get_plugin(config)
        constellation.set_value('constellation_state', 'terminating')
        constellation_plugin.terminate(constellation_name)
        constellation.set_value('constellation_state', 'terminated')
    except Exception, e:
        tb = traceback.format_exc()
        constellation.set_value(
            'error', 'Terminate aborted with exception: '
            '%s<pre>%s</pre>' % (e, tb))
        log("TERMINATE ERROR traceback:  %s" % tb)