Esempio n. 1
0
    def process_update(self, ch, method, properties, body):
        routing_key = method.routing_key
        try:
            result = json.loads(body)
        except:
            result = body
        log.info("Got %s", routing_key)
        if routing_key in set([
                'notify', 'probe', 'list_sizes', 'list_images',
                'list_networks', 'list_machines', 'list_locations',
                'list_projects', 'ping'
        ]):
            self.send(routing_key, result)
            if routing_key == 'probe':
                log.warn('send probe')

            if routing_key == 'list_networks':
                cloud_id = result['cloud_id']
                log.warn('Got networks from %s',
                         self.user.clouds[cloud_id].title)
            if routing_key == 'list_machines':
                # probe newly discovered running machines
                machines = result['machines']
                cloud_id = result['cloud_id']
                # update cloud machine count in multi-user setups
                try:
                    mcount = self.user.clouds[cloud_id].machine_count
                    if multi_user and len(machines) != mcount:
                        tasks.update_machine_count.delay(
                            self.user.email, cloud_id, len(machines))
                except Exception as exc:
                    log.warning("Error while update_machine_count.delay: %r",
                                exc)
                for machine in machines:
                    bmid = (cloud_id, machine['id'])
                    if bmid in self.running_machines:
                        # machine was running
                        if machine['state'] != 'running':
                            # machine no longer running
                            self.running_machines.remove(bmid)
                        continue
                    if machine['state'] != 'running':
                        # machine not running
                        continue
                    # machine just started running
                    self.running_machines.add(bmid)
                    ips = filter(lambda ip: ':' not in ip,
                                 machine.get('public_ips', []))
                    if not ips:
                        continue

                    has_key = False
                    for k in self.user.keypairs.values():
                        for m in k.machines:
                            if m[:2] == [cloud_id, machine['id']]:
                                has_key = True
                                break
                        if has_key:
                            break

                    if has_key:
                        cached = tasks.ProbeSSH().smart_delay(
                            self.user.email, cloud_id, machine['id'], ips[0])
                        if cached is not None:
                            self.send('probe', cached)

                    cached = tasks.Ping().smart_delay(self.user.email,
                                                      cloud_id, machine['id'],
                                                      ips[0])
                    if cached is not None:
                        self.send('ping', cached)

        elif routing_key == 'update':
            self.user.refresh()
            sections = result
            if 'clouds' in sections:
                self.list_clouds()
            if 'keys' in sections:
                self.list_keys()
            if 'monitoring' in sections:
                self.check_monitoring()
Esempio n. 2
0
    def process_update(self, msg):
        routing_key = msg.delivery_info.get('routing_key')
        log.info("Got %s", routing_key)
        if routing_key in set([
                'notify', 'probe', 'list_sizes', 'list_images',
                'list_machines', 'list_locations', 'ping'
        ]):
            self.emit(routing_key, msg.body)
            if routing_key == 'probe':
                log.warn('send probe')

            if routing_key == 'list_machines':
                # probe newly discovered running machines
                machines = msg.body['machines']
                backend_id = msg.body['backend_id']
                # update backend machine count in multi-user setups
                try:
                    if multi_user and len(machines) != self.user.backends[
                            backend_id].machine_count:
                        tasks.update_machine_count.delay(
                            self.user.email, backend_id, len(machines))
                        log.info('Updated machine count for user %s' %
                                 self.user.email)
                except Exception as e:
                    log.error('Cannot update machine count for user %s: %r' %
                              (self.user.email, e))
                for machine in machines:
                    bmid = (backend_id, machine['id'])
                    if bmid in self.running_machines:
                        # machine was running
                        if machine['state'] != 'running':
                            # machine no longer running
                            self.running_machines.remove(bmid)
                        continue
                    if machine['state'] != 'running':
                        # machine not running
                        continue
                    # machine just started running
                    self.running_machines.add(bmid)
                    ips = filter(lambda ip: ':' not in ip,
                                 machine.get('public_ips', []))
                    if not ips:
                        continue
                    cached = tasks.ProbeSSH().smart_delay(
                        self.user.email, backend_id, machine['id'], ips[0])
                    if cached is not None:
                        self.emit('probe', cached)
                    cached = tasks.Ping().smart_delay(self.user.email,
                                                      backend_id,
                                                      machine['id'], ips[0])
                    if cached is not None:
                        self.emit('ping', cached)
        elif routing_key == 'update':
            self.user.refresh()
            sections = msg.body
            if 'backends' in sections:
                self.backends_greenlet.kill()
                self.backends_greenlet = self.spawn(list_backends_from_socket,
                                                    self)
            if 'keys' in sections:
                self.keys_greenlet.kill()
                self.keys_greenlet = self.spawn(list_keys_from_socket, self)
            if 'monitoring' in sections:
                self.monitoring_greenlet.kill()
                self.monitoring_greenlet = self.spawn(
                    check_monitoring_from_socket, self)