Esempio n. 1
0
 def insertSite(self, site_id):
     site = model.Site(parent=self.db_root.key())
     site.site_id = site_id
     # Metro is always a 2-tuple where the first is the metro (first three
     # letters of the site ID) and the second is the site ID itself.
     site.metro = [site_id[:3], site_id]
     site.put()
Esempio n. 2
0
    def update_site(self, site):
        """Add a new site or update an existing site.

        Args:
            site: A json representing the site info.

        Returns:
            True if the registration succeeds, False otherwise.
        """
        try:
            lat_long = float(site[self.LAT_FIELD])
            lon_long = float(site[self.LON_FIELD])
        except ValueError:
            logging.error('Geo coordinates are not float (%s, %s)',
                          site[self.LAT_FIELD], site[self.LON_FIELD])
            return False
        site = model.Site(site_id=site[self.SITE_FIELD],
                          city=site[self.CITY_FIELD],
                          country=site[self.COUNTRY_FIELD],
                          latitude=lat_long,
                          longitude=lon_long,
                          metro=site[self.METRO_FIELD],
                          registration_timestamp=long(time.time()),
                          key_name=site[self.SITE_FIELD],
                          roundrobin=site[self.ROUNDROBIN_FIELD])

        try:
            site.put()
        except db.TransactionFailedError:
            # TODO(claudiu) Trigger an event/notification.
            logging.error('Failed to write site %s to datastore.', site.site_id)
            return False
        logging.info('Succeeded to write site %s to db', site.site_id)

        return True
Esempio n. 3
0
    def register_site(self, nagios_site):
        """Registers a new site.

        Args:
            nagios_site: A json representing the site info as provided by Nagios.

        Returns:
            True if the registration succeeds, False otherwise.
        """
        try:
            lat_long = float(nagios_site[self.LAT_FIELD])
            lon_long = float(nagios_site[self.LON_FIELD])
        except ValueError:
            logging.error('Geo coordinates are not float (%s, %s)',
                           nagios_site[self.LAT_FIELD],
                           nagios_site[self.LON_FIELD])
            return False
        site = model.Site(
            site_id = nagios_site[self.SITE_FIELD],
            city = nagios_site[self.CITY_FIELD],
            country = nagios_site[self.COUNTRY_FIELD],
            latitude = lat_long,
            longitude = lon_long,
            metro = nagios_site[self.METRO_FIELD],
            registration_timestamp=long(time.time()),
            key_name=nagios_site[self.SITE_FIELD])

        try:
            site.put()
        except db.TransactionFailedError:
            # TODO(claudiu) Trigger an event/notification.
            logging.error('Failed to write site %s to datastore.', site.site_id)
            return False
        logging.info('Succeeded to write site %s to db', site.site_id)

        tools = model.Tool.all()
        for tool in tools:
            for server_id in ['mlab1', 'mlab2', 'mlab3']:
                fqdn = model.get_fqdn(tool.slice_id, server_id, site.site_id)
                if fqdn is None:
                    logging.error(
                        'Cannot compute fqdn for slice %s.', tool.slice_id)
                    continue

                sliver_tool = IPUpdateHandler().initialize_sliver_tool(
                    tool, site, server_id, fqdn)
                try:
                    sliver_tool.put()
                    logging.info(
                        'Succeeded to write sliver %s to datastore.', fqdn)
                except db.TransactionFailedError:
                    logging.error(
                        'Failed to write sliver %s to datastore.', fqdn)
                    continue

        return True
Esempio n. 4
0
    def testGetCandidatesNoSites(self):
        root = TestEntityGroupRoot(key_name='root')
        st1 = model.Site(parent=root.key())
        st1.site_id = 's1'
        st1.metro = ['metro2']
        st1.put()

        metro_resolver = resolver.MetroResolver()
        mock_query = mock.Mock(metro='metro1')
        self.assertEqual(
            0, len(metro_resolver._get_candidates(mock_query, 'unused_arg')))
Esempio n. 5
0
    def testGetCandidatesYesSites(self):
        class MetroResolverMockup(resolver.MetroResolver):
            def _get_candidates_from_sites(self, unused_arg1, unused_arg2,
                                           site_list):
                return site_list

        root = TestEntityGroupRoot(key_name='root')
        st1 = model.Site(parent=root.key())
        st1.site_id = 's1'
        st1.metro = ['metro1']
        st1.put()
        st2 = model.Site(parent=root.key())
        st2.site_id = 's2'
        st2.metro = ['metro1', 'site1']
        st2.put()
        st3 = model.Site(parent=root.key())
        st3.site_id = 's3'
        st3.metro = ['metro2', 'site2']
        st3.put()

        metro_resolver = MetroResolverMockup()
        mock_query = mock.Mock(metro='metro1')
        self.assertEqual(
            2, len(metro_resolver._get_candidates(mock_query, 'unused_arg')))