コード例 #1
0
ファイル: host.py プロジェクト: jererc/mist-py
def run():
    settings_ = Settings.get_settings('host')
    hosts = get_hosts(settings_['ip_range'])
    if hosts is None:
        logger.debug('failed to find hosts')
        return

    factory = get_factory()
    for host in hosts:
        Host.update({'host': host},
                    {'$set': {
                        'alive': True,
                        'seen': datetime.utcnow(),
                    }},
                    upsert=True,
                    safe=True)

        factory.add(**get_worker(host))

    Host.update({'host': {
        '$nin': hosts
    }}, {'$set': {
        'alive': False
    }},
                safe=True,
                multi=True)
    delta = timedelta(minutes=settings_['host_timeout'])
    Host.remove({'seen': {'$lt': datetime.utcnow() - delta}}, safe=True)

    for res in Host.find({'alive': False}):
        factory.remove(**get_worker(res['host']))
コード例 #2
0
ファイル: sync.py プロジェクト: jererc/mist-py
def _get_abs_path(host, uuid, path, retries=1):
    '''Get the absolute path for a uuid partition.
    '''
    disks = host.get_disks()
    disk = _get_disk(disks, uuid) or {}
    path_uuid = disk.get('path')

    if not path_uuid or not host.exists(path_uuid):
        automount = Settings.get_settings('sync')['automount']
        if not automount or retries <= 0:
            logger.info('failed to get path for uuid %s on %s', uuid,
                        host.host)
            return
        dev = disk.get('dev')
        if not dev:
            logger.info('failed to get device for uuid %s on %s', uuid,
                        host.host)
            return
        if not host.mount(dev):
            logger.info('failed to mount device %s (uuid %s) on %s', dev, uuid,
                        host.host)
            return
        return _get_abs_path(host, uuid, path, retries=retries - 1)

    return os.path.join(path_uuid, path.lstrip(
        '/'))  # lstrip '/' to avoid getting the filesystem root with join()
コード例 #3
0
ファイル: host.py プロジェクト: jererc/mist-py
def update_host(host):
    settings_ = Settings.get_settings('host')

    res = Host.find_one({'host': host})
    if not res:
        return
    res.setdefault('users', [])

    # Clean host users list
    for user in res['users'][:]:
        if not User.find_one({'_id': user['_id']}):
            res['users'].remove(user)

    # Get a list of users to try
    users = []
    for user in User.find():
        user_ = _get_user(res['users'], user['_id'])
        if user_:
            user2 = deepcopy(user_)
            user2.update(user)
            users.insert(0, user2)
        else:
            users.append(user)

    port_service = None
    ports_timeout = []

    for user in users:
        port = user.get('port', 22)
        if port in ports_timeout:
            continue
        if port_service and port != port_service:
            continue
        if not user.get('logged') and user.get('failed'):
            delta = timedelta(minutes=settings_['failed_user_timeout'])
            if user['failed'] > datetime.utcnow() - delta:
                continue

        try:
            session = SshHost(host,
                              user['username'],
                              user['password'],
                              port=port)
            if user.get('failed'):
                del user['failed']
            user['logged'] = datetime.utcnow()
        except TimeoutError, e:
            ports_timeout.append(port)
            if user.get('logged'):
                logger.info('failed to connect to %s@%s:%s: %s',
                            user['username'], host, port, str(e))
            continue
        except Exception, e:
            if user.get('logged'):
                del user['logged']
                logger.info('failed to connect to %s@%s:%s: %s',
                            user['username'], host, port, str(e))
            user['failed'] = datetime.utcnow()
            continue
コード例 #4
0
ファイル: host.py プロジェクト: jererc/mist-py
def update_host(host):
    settings_ = Settings.get_settings('host')

    res = Host.find_one({'host': host})
    if not res:
        return
    res.setdefault('users', [])

    # Clean host users list
    for user in res['users'][:]:
        if not User.find_one({'_id': user['_id']}):
            res['users'].remove(user)

    # Get a list of users to try
    users = []
    for user in User.find():
        user_ = _get_user(res['users'], user['_id'])
        if user_:
            user2 = deepcopy(user_)
            user2.update(user)
            users.insert(0, user2)
        else:
            users.append(user)

    port_service = None
    ports_timeout = []

    for user in users:
        port = user.get('port', 22)
        if port in ports_timeout:
            continue
        if port_service and port != port_service:
            continue
        if not user.get('logged') and user.get('failed'):
            delta = timedelta(minutes=settings_['failed_user_timeout'])
            if user['failed'] > datetime.utcnow() - delta:
                continue

        try:
            session = SshHost(host, user['username'], user['password'],
                    port=port)
            if user.get('failed'):
                del user['failed']
            user['logged'] = datetime.utcnow()
        except TimeoutError, e:
            ports_timeout.append(port)
            if user.get('logged'):
                logger.info('failed to connect to %s@%s:%s: %s', user['username'], host, port, str(e))
            continue
        except Exception, e:
            if user.get('logged'):
                del user['logged']
                logger.info('failed to connect to %s@%s:%s: %s', user['username'], host, port, str(e))
            user['failed'] = datetime.utcnow()
            continue
コード例 #5
0
ファイル: sync.py プロジェクト: jererc/mist-py
def run():
    sync_timeout = Settings.get_settings('sync')['sync_timeout']
    for sync in Sync.find({'$or': [
            {'reserved': None},
            {'reserved': {'$lt': datetime.utcnow()}},
            ]}):
        if not validate_sync(sync):
            continue
        target = '%s.workers.sync.process_sync' % settings.PACKAGE_NAME
        get_factory().add(target=target, args=(sync['_id'],),
                timeout=sync_timeout)
コード例 #6
0
ファイル: watch.py プロジェクト: jererc/mist-py
def run():
    settings = Settings.get_settings('email')
    if not set(['host', 'username', 'password', 'port']) <= set(
            settings.keys()):
        return

    server = None
    now = datetime.utcnow()
    delta = timedelta(days=settings.get('delta', 7))

    for user in User.find():
        if not user.get('email'):
            continue
        notified = user.get('notified')
        if notified and notified > now - delta:
            continue
        login = get_last_login(user['_id'])
        if login and login > now - delta:
            continue

        subject = '%s is unreachable' % user['name']
        if login:
            body = 'The device "%s" is unreachable for %d days.\n\n' % (
                user['name'], (now - login).days)
        else:
            body = 'The device "%s" is unreachable.\n\n' % user['name']
        body += 'It won\'t be synchronized until your SSH server application (e.g.: QuickSSHD) is up and running on the device.'

        if not server:
            server = Email(settings['host'], settings['username'],
                           settings['password'], settings['port'])
        try:
            server.send('mist', user['email'], subject, body)
        except Exception, e:
            logger.error('failed to send email to %s: %s', user['email'],
                         str(e))
            continue

        User.update({'_id': user['_id']}, {'$set': {
            'notified': now
        }},
                    safe=True)
コード例 #7
0
ファイル: sync.py プロジェクト: jererc/mist-py
def run():
    sync_timeout = Settings.get_settings('sync')['sync_timeout']
    for sync in Sync.find({
            '$or': [
                {
                    'reserved': None
                },
                {
                    'reserved': {
                        '$lt': datetime.utcnow()
                    }
                },
            ]
    }):
        if not validate_sync(sync):
            continue
        target = '%s.workers.sync.process_sync' % settings.PACKAGE_NAME
        get_factory().add(target=target,
                          args=(sync['_id'], ),
                          timeout=sync_timeout)
コード例 #8
0
ファイル: sync.py プロジェクト: jererc/mist-py
def _get_abs_path(host, uuid, path, retries=1):
    '''Get the absolute path for a uuid partition.
    '''
    disks = host.get_disks()
    disk = _get_disk(disks, uuid) or {}
    path_uuid = disk.get('path')

    if not path_uuid or not host.exists(path_uuid):
        automount = Settings.get_settings('sync')['automount']
        if not automount or retries <= 0:
            logger.info('failed to get path for uuid %s on %s', uuid, host.host)
            return
        dev = disk.get('dev')
        if not dev:
            logger.info('failed to get device for uuid %s on %s', uuid, host.host)
            return
        if not host.mount(dev):
            logger.info('failed to mount device %s (uuid %s) on %s', dev, uuid, host.host)
            return
        return _get_abs_path(host, uuid, path, retries=retries-1)

    return os.path.join(path_uuid, path.lstrip('/'))    # lstrip '/' to avoid getting the filesystem root with join()
コード例 #9
0
ファイル: host.py プロジェクト: jererc/mist-py
def run():
    settings_ = Settings.get_settings('host')
    hosts = get_hosts(settings_['ip_range'])
    if hosts is None:
        logger.debug('failed to find hosts')
        return

    factory = get_factory()
    for host in hosts:
        Host.update({'host': host}, {'$set': {
                'alive': True,
                'seen': datetime.utcnow(),
                }}, upsert=True, safe=True)

        factory.add(**get_worker(host))

    Host.update({'host': {'$nin': hosts}},
            {'$set': {'alive': False}}, safe=True, multi=True)
    delta = timedelta(minutes=settings_['host_timeout'])
    Host.remove({'seen': {'$lt': datetime.utcnow() - delta}}, safe=True)

    for res in Host.find({'alive': False}):
        factory.remove(**get_worker(res['host']))
コード例 #10
0
ファイル: watch.py プロジェクト: jererc/mist-py
def run():
    settings = Settings.get_settings('email')
    if not set(['host', 'username', 'password', 'port']) <= set(settings.keys()):
        return

    server = None
    now = datetime.utcnow()
    delta = timedelta(days=settings.get('delta', 7))

    for user in User.find():
        if not user.get('email'):
            continue
        notified = user.get('notified')
        if notified and notified > now - delta:
            continue
        login = get_last_login(user['_id'])
        if login and login > now - delta:
            continue

        subject = '%s is unreachable' % user['name']
        if login:
            body = 'The device "%s" is unreachable for %d days.\n\n' % (user['name'], (now - login).days)
        else:
            body = 'The device "%s" is unreachable.\n\n'  % user['name']
        body += 'It won\'t be synchronized until your SSH server application (e.g.: QuickSSHD) is up and running on the device.'

        if not server:
            server = Email(settings['host'], settings['username'],
                    settings['password'], settings['port'])
        try:
            server.send('mist', user['email'], subject, body)
        except Exception, e:
            logger.error('failed to send email to %s: %s', user['email'], str(e))
            continue

        User.update({'_id': user['_id']},
                {'$set': {'notified': now}}, safe=True)
コード例 #11
0
ファイル: sync.py プロジェクト: jererc/mist-py
def set_retry(sync):
    delta = Settings.get_settings('sync')['sync_retry_delta']
    sync['reserved'] = datetime.utcnow() + timedelta(minutes=delta)
    Sync.save(sync, safe=True)
コード例 #12
0
ファイル: api.py プロジェクト: jererc/mist-py
def update_settings():
    data = request.json
    for section, settings in data.items():
        Settings.set_settings(section, settings, overwrite=True)
    return jsonify(result=True)
コード例 #13
0
ファイル: api.py プロジェクト: jererc/mist-py
def list_settings():
    settings = {}
    for section in ('host', 'sync', 'email'):
        settings[section] = Settings.get_settings(section)
    return serialize({'result': settings})
コード例 #14
0
ファイル: sync.py プロジェクト: jererc/mist-py
def set_retry(sync):
    delta = Settings.get_settings('sync')['sync_retry_delta']
    sync['reserved'] = datetime.utcnow() + timedelta(minutes=delta)
    Sync.save(sync, safe=True)