예제 #1
0
def partitiondict2partition(partition):
  slap_partition = ComputerPartition(app.config['computer_id'],
      partition['reference'])
  slap_partition._requested_state = 'started'
  if partition['software_release']:
    slap_partition._need_modification = 1
  else:
    slap_partition._need_modification = 0
  slap_partition._parameter_dict = xml2dict(partition['xml'])
  address_list = []
  for address in execute_db('partition_network', 'SELECT * FROM %s WHERE partition_reference=?', [partition['reference']]):
    address_list.append((address['reference'], address['address']))
  slap_partition._parameter_dict['ip_list'] = address_list
  slap_partition._parameter_dict['slap_software_type'] = partition['software_type']
  slap_partition._connection_dict = xml2dict(partition['connection_xml'])
  slap_partition._software_release_document = SoftwareRelease(
      software_release=partition['software_release'],
      computer_guid=app.config['computer_id'])
  return slap_partition
예제 #2
0
파일: views.py 프로젝트: SlapOS/slapos.core
def partitiondict2partition(partition):
  slap_partition = ComputerPartition(partition['computer_reference'],
      partition['reference'])
  slap_partition._software_release_document = None
  slap_partition._requested_state = 'destroyed'
  slap_partition._need_modification = 0
  slap_partition._instance_guid = '%s-%s' % (partition['computer_reference'], partition['reference'])

  root_partition = getRootPartition(partition['reference'])

  if partition['software_release']:
    slap_partition._need_modification = 1
    slap_partition._requested_state = partition['requested_state']
    slap_partition._parameter_dict = xml2dict(partition['xml'])
    address_list = []
    full_address_list = []
    for address in execute_db('partition_network',
                              'SELECT * FROM %s WHERE partition_reference=? AND computer_reference=?',
                              [partition['reference'], partition['computer_reference']]):
      address_list.append((address['reference'], address['address']))
    slap_partition._parameter_dict['ip_list'] = address_list
    slap_partition._parameter_dict['full_address_list'] = full_address_list
    slap_partition._parameter_dict['slap_software_type'] = \
        partition['software_type']
    slap_partition._parameter_dict['instance_title'] = \
        partition['partition_reference']
    slap_partition._parameter_dict['root_instance_title'] = \
        root_partition['partition_reference']
    if partition['slave_instance_list'] is not None:
      slap_partition._parameter_dict['slave_instance_list'] = \
          loads(partition['slave_instance_list'].encode('utf-8'))
    else:
      slap_partition._parameter_dict['slave_instance_list'] = []
    slap_partition._connection_dict = xml2dict(partition['connection_xml'])
    slap_partition._software_release_document = SoftwareRelease(
      software_release=partition['software_release'],
      computer_guid=partition['computer_reference'])

  return slap_partition
예제 #3
0
  def _getSlapPartitionByPackingList(self, computer_partition_document):
    computer = computer_partition_document
    portal = self.getPortalObject()
    portal_preferences = portal.portal_preferences
    while computer.getPortalType() != 'Computer':
      computer = computer.getParentValue()
    computer_id = computer.getReference()
    slap_partition = SlapComputerPartition(computer_id,
                                computer_partition_document.getReference())

    slap_partition._software_release_document = None
    slap_partition._requested_state = 'destroyed'
    slap_partition._need_modification = 0

    software_instance = None

    if computer_partition_document.getSlapState() == 'busy':
      software_instance_list = portal.portal_catalog(
          portal_type="Software Instance",
          default_aggregate_uid=computer_partition_document.getUid(),
          validation_state="validated",
          limit=2,
          )
      software_instance_count = len(software_instance_list)
      if software_instance_count == 1:
        software_instance = software_instance_list[0].getObject()
      elif software_instance_count > 1:
        # XXX do not prevent the system to work if one partition is broken
        raise NotImplementedError, "Too many instances %s linked to %s" % \
          ([x.path for x in software_instance_list],
           computer_partition_document.getRelativeUrl())

    if software_instance is not None:
      state = software_instance.getSlapState()
      if state == "stop_requested":
        slap_partition._requested_state = 'stopped'
      if state == "start_requested":
        slap_partition._requested_state = 'started'

      slap_partition._software_release_document = SoftwareRelease(
            software_release=software_instance.getRootSoftwareReleaseUrl(),
            computer_guid=computer_id)

      slap_partition._need_modification = 1

      parameter_dict = self._getSoftwareInstanceAsParameterDict(
                                                       software_instance)
      # software instance has to define an xml parameter
      slap_partition._parameter_dict = self._instanceXmlToDict(
        parameter_dict.pop('xml'))
      slap_partition._connection_dict = self._instanceXmlToDict(
        parameter_dict.pop('connection_xml'))
      for slave_instance_dict in parameter_dict.get("slave_instance_list", []):
        if slave_instance_dict.has_key("connection_xml"):
          slave_instance_dict.update(self._instanceXmlToDict(
            slave_instance_dict.pop("connection_xml")))
        if slave_instance_dict.has_key("xml"):
          slave_instance_dict.update(self._instanceXmlToDict(
            slave_instance_dict.pop("xml")))
      slap_partition._parameter_dict.update(parameter_dict)

    return slap_partition
예제 #4
0
  def registerComputerPartition(self, computer_reference,
                                computer_partition_reference):
    """
    Registers connected representation of computer partition and
    returns Computer Partition class object
    """
    # Try to get the computer partition to raise an exception if it doesn't
    # exist
    portal = self.getPortalObject()
    computer_partition_document = self._getComputerPartitionDocument(
          computer_reference, computer_partition_reference)
    slap_partition = SlapComputerPartition(computer_reference,
        computer_partition_reference)
    slap_partition._software_release_document = None
    slap_partition._requested_state = 'destroyed'
    slap_partition._need_modification = 0
    software_instance = None

    if computer_partition_document.getSlapState() == 'busy':
      software_instance_list = portal.portal_catalog(
          portal_type="Software Instance",
          default_aggregate_uid=computer_partition_document.getUid(),
          validation_state="validated",
          limit=2,
          )
      software_instance_count = len(software_instance_list)
      if software_instance_count == 1:
        software_instance = software_instance_list[0].getObject()
      elif software_instance_count > 1:
        # XXX do not prevent the system to work if one partition is broken
        raise NotImplementedError, "Too many instances %s linked to %s" % \
          ([x.path for x in software_instance_list],
           computer_partition_document.getRelativeUrl())

    if software_instance is not None:
      # trick client side, that data has been synchronised already for given
      # document
      slap_partition._synced = True
      state = software_instance.getSlapState()
      if state == "stop_requested":
        slap_partition._requested_state = 'stopped'
      if state == "start_requested":
        slap_partition._requested_state = 'started'

      slap_partition._software_release_document = SoftwareRelease(
            software_release=software_instance.getRootSoftwareReleaseUrl(),
            computer_guid=computer_reference)

      slap_partition._need_modification = 1

      parameter_dict = self._getSoftwareInstanceAsParameterDict(
                                                       software_instance)
      # software instance has to define an xml parameter
      slap_partition._parameter_dict = self._instanceXmlToDict(
        parameter_dict.pop('xml'))
      slap_partition._connection_dict = self._instanceXmlToDict(
        parameter_dict.pop('connection_xml'))
      for slave_instance_dict in parameter_dict.get("slave_instance_list", []):
        if slave_instance_dict.has_key("connection_xml"):
          slave_instance_dict.update(self._instanceXmlToDict(
            slave_instance_dict.pop("connection_xml")))
        if slave_instance_dict.has_key("xml"):
          slave_instance_dict.update(self._instanceXmlToDict(
            slave_instance_dict.pop("xml")))
      slap_partition._parameter_dict.update(parameter_dict)
    return xml_marshaller.xml_marshaller.dumps(slap_partition)
예제 #5
0
  def _getSlapPartitionByPackingList(self, computer_partition_document):
    computer = computer_partition_document
    portal = self.getPortalObject()
    portal_preferences = portal.portal_preferences
    while computer.getPortalType() != 'Computer':
      computer = computer.getParentValue()
    computer_id = computer.getReference()
    slap_partition = SlapComputerPartition(computer_id,
                                computer_partition_document.getReference())

    slap_partition._software_release_document = None
    slap_partition._requested_state = 'destroyed'
    slap_partition._need_modification = 0

    update_movement = self._getSalePackingListLineForComputerPartition(
      computer_partition_document, service_uid_list=[portal.restrictedTraverse(portal_preferences.getPreferredInstanceUpdateResource()).getUid()])
    if update_movement is not None:
      if update_movement.getSimulationState() != 'confirmed':
        # only confirmed update movements are interesting
        update_movement = None
    movement = self._getSalePackingListLineForComputerPartition(
                                           computer_partition_document)
    if movement is not None:
      software_release_document = \
              movement.getAggregateValue(portal_type='Software Release')
      slap_partition._software_release_document =  SoftwareRelease(
            software_release=software_release_document.getUrlString(),
            computer_guid=computer_id)
      parameter_dict = self._getSalePackingListLineAsSoftwareInstance(
                                                       movement)
      # software instance has to define an xml parameter
      slap_partition._parameter_dict = self._instanceXmlToDict(
        parameter_dict.pop('xml'))
      slap_partition._connection_dict = self._instanceXmlToDict(
        parameter_dict.pop('connection_xml'))
      for slave_instance_dict in parameter_dict.get("slave_instance_list", []):
        if slave_instance_dict.has_key("connection_xml"):
          slave_instance_dict.update(self._instanceXmlToDict(
            slave_instance_dict.pop("connection_xml")))
        if slave_instance_dict.has_key("xml"):
          slave_instance_dict.update(self._instanceXmlToDict(
            slave_instance_dict.pop("xml")))
      slap_partition._parameter_dict.update(parameter_dict)

      # Apply state and buildout run conditions
      if movement.getResource() == \
              portal_preferences.getPreferredInstanceSetupResource():

        if movement.getSimulationState() in ('confirmed', 'started',):
          slap_partition._requested_state = 'stopped'
          slap_partition._need_modification = 1
        elif movement.getSimulationState() == 'stopped':
          slap_partition._requested_state = 'stopped'
          if update_movement is not None:
            slap_partition._need_modification = 1
        elif movement.getSimulationState() == 'delivered':
          slap_partition._requested_state = 'destroyed'
        else:
          raise NotImplementedError, "Unexpected state %s" % \
                                     movement.getSimulationState()

      elif movement.getResource() == \
              portal_preferences.getPreferredInstanceHostingResource():
        if movement.getSimulationState() == 'confirmed':
          slap_partition._requested_state = 'started'
          slap_partition._need_modification = 1
        elif movement.getSimulationState() == 'started':
          slap_partition._requested_state = 'started'
          slap_partition._need_modification = \
            self._hasSlaveInstanceNeedModification(computer_partition_document)
          if update_movement is not None:
            slap_partition._need_modification = 1
        elif movement.getSimulationState() == 'stopped':
          slap_partition._requested_state = 'stopped'
          slap_partition._need_modification = 1
        elif movement.getSimulationState() == 'delivered':
          slap_partition._requested_state = 'stopped'
          if update_movement is not None:
            slap_partition._need_modification = 1
        else:
          raise NotImplementedError, "Unexpected state %s" % \
                                     movement.getSimulationState()

      elif movement.getResource() == \
              portal_preferences.getPreferredInstanceCleanupResource():

        if movement.getSimulationState() in ('confirmed', 'started',
            'stopped'):
          slap_partition._need_modification = 1

      else:
        raise NotImplementedError, "Unexpected resource%s" % \
                                   movement.getResource()
    return slap_partition
예제 #6
0
파일: views.py 프로젝트: NBSW/slapos.core
def partitiondict2partition(partition):
  for key, value in partition.iteritems():
    if type(value) is unicode:
      partition[key] = value.encode()
  slap_partition = ComputerPartition(partition['computer_reference'],
      partition['reference'])
  slap_partition._software_release_document = None
  slap_partition._requested_state = 'destroyed'
  slap_partition._need_modification = 0
  slap_partition._instance_guid = '%s-%s' % (partition['computer_reference'], partition['reference'])

  if partition['software_release']:
    slap_partition._need_modification = 1
    slap_partition._requested_state = partition['requested_state']
    slap_partition._parameter_dict = xml2dict(partition['xml'])
    address_list = []
    full_address_list = []
    for address in execute_db('partition_network',
                              'SELECT * FROM %s WHERE partition_reference=? AND computer_reference=?',
                              [partition['reference'], partition['computer_reference']]):
      address_list.append((address['reference'], address['address']))
    slap_partition._parameter_dict['ip_list'] = address_list
    slap_partition._parameter_dict['full_address_list'] = full_address_list
    slap_partition._parameter_dict['slap_software_type'] = \
        partition['software_type']
    if partition['slave_instance_list'] is not None:
      slap_partition._parameter_dict['slave_instance_list'] = \
          xml_marshaller.xml_marshaller.loads(partition['slave_instance_list'])
    else:
      slap_partition._parameter_dict['slave_instance_list'] = []
    slap_partition._connection_dict = xml2dict(partition['connection_xml'])
    slap_partition._software_release_document = SoftwareRelease(
      software_release=partition['software_release'],
      computer_guid=partition['computer_reference'])

  return slap_partition