Example #1
0
    def get_imgadm_conf(sources):
        conf = PickleDict(
            settings.VMS_IMAGE_IMGADM_CONF
        )  # PickleDict because it has a dump() method (to json)
        system_sources = conf.get('sources', [])
        conf['sources'] = [{
            'type': 'imgapi',
            'url': url
        } for url in sources] + system_sources

        return conf
Example #2
0
    def clean_json(self):
        try:
            data = PickleDict.load(self.cleaned_data['json'])
        except Exception:
            raise forms.ValidationError(_('Invalid JSON format.'))

        return data
Example #3
0
 def manifest_active(self):
     return PickleDict(self.json.get('manifest_active', {}))
Example #4
0
 def manifest(self):
     return PickleDict(self.json.get('manifest', {}))
Example #5
0
 def backup(self):
     return PickleDict(self.json.get('backup', {}))
Example #6
0
 def _decode(xdata):
     """Unpickle data from DB and return a PickleDict object"""
     data = pickle.loads(base64.decodestring(xdata))
     if not isinstance(data, PickleDict):
         data = PickleDict(data)
     return data
Example #7
0
def harvest_vm_cb(result, task_id, node_uuid=None):
    node = Node.objects.get(uuid=node_uuid)
    dc = Dc.objects.get_by_id(dc_id_from_task_id(task_id))
    err = result.pop('stderr', None)
    vms = []
    vms_err = []
    jsons = []

    if result.pop('returncode', None) != 0 or err:
        logger.error(
            'Found nonzero returncode in result from harvest_vm(%s). Error: %s',
            node, err)
        raise TaskException(
            result, 'Got bad return code (%s). Error: %s' %
            (result['returncode'], err))

    for json in result.pop('stdout', '').split('||||'):
        json = json.strip()
        if json:
            try:
                jsons.append(PickleDict.load(json))
            except Exception as e:
                logger.error(
                    'Could not parse json output from harvest_vm(%s). Error: %s',
                    node, e)
                raise TaskException(result, 'Could not parse json output')

    if not jsons:
        raise TaskException(result, 'Missing json output')

    request = get_dummy_request(dc, method='POST', system_user=True)

    for json in jsons:
        vm_uuid = json.get(
            'uuid', None)  # Bad uuid will be stopped later in vm_from_json()
        if vm_uuid:
            if Vm.objects.filter(uuid=vm_uuid).exists():
                logger.warning('Ignoring VM %s found in harvest_vm(%s)',
                               vm_uuid, node)
                continue
        try:
            vm = vm_from_json(request,
                              task_id,
                              json,
                              dc,
                              template=True,
                              save=True,
                              update_ips=True,
                              update_dns=True)
        except Exception as e:
            logger.exception(e)
            logger.error('Could not load VM from json:\n"""%s"""', json)
            err_msg = 'Could not load server %s. Error: %s' % (vm_uuid, e)
            task_log_cb_error({'message': err_msg},
                              task_id,
                              obj=node,
                              **result['meta'])
            vms_err.append(vm_uuid)
        else:
            logger.info('Successfully saved new VM %s after harvest_vm(%s)',
                        vm, node)
            vms.append(vm.hostname)
            vm_deployed.send(task_id,
                             vm=vm)  # Signal!  (will update monitoring)

            if vm.json_changed():
                try:
                    _vm_update(vm)
                except Exception as e:
                    logger.exception(e)

    if vms or not vms_err:
        if vms:
            result['message'] = 'Successfully harvested %s server(s) (%s)' % (
                len(vms), ','.join(vms))
        else:
            result['message'] = 'No new server found'

        task_log_cb_success(result, task_id, obj=node, **result['meta'])
        return result
    else:
        raise TaskException(result, 'Could not find or load any server')