def __init__(self, manager, name, *args, **kwargs): super().__init__(*args, **kwargs) self._pos = ServiceDashPosition() self._manager = manager self._build_options = pdict() self._env = pdict() self._logs = deque(maxlen=1000) self._name = name self._title = name.replace('_', ' ').title() self.clean_status()
def clean_status(self): logger.debug('restoring state', name=self.name) self._meta = pdict() self._app = pdict() self._app_ts = None self._status_override = None self._dock = pdict() self._dock_ts = None self._methods = [] self._managed = False self._protected = False self._persistent = False self._native = False
async def hello_msg(message): user = message.new_chat_member or message['from'] mention_ = mention(user) text = settings.msg.hello.format(mention=mention_) state.joined[str(user.id)] = pdict(time=now(), chat_id=message.chat.id, user_id=user.id, mention=mention_, phase=-1) msg = {'chat_id': message.chat['id'], 'text': text} logger.info('sending', msg=msg) await send_msg(msg)
async def get(self, name, **kwargs): params = kwargs.pop('params', pdict()) positions = [] # Container env envs = [] if params.env: envs.append(params.env) if params.get('pos') and is_valid_pos(params.pos): positions.append(params.pos) if name not in self._state: logger.debug('loading state', name=name) config = await self.load_config(name) meta = await image_navigator.image_meta(name) svc = ServiceState(name=name, manager=self) if config: if config.get('env'): envs.append(config['env']) if config.get('pos') and is_valid_pos(config['pos']): positions.append(config['pos']) if meta: svc.set_meta(meta) if meta.env: envs.append(meta.env) if meta.get('pos') and is_valid_pos(meta['pos']): positions.append(meta['pos']) positions.append(self.grid.default_pos) self._state[name] = svc svc = self._state[name] # env variables if len(envs): svc.set_env(merge_dicts(*envs)) # passed build options if params.build_opts: svc.set_build_opts(**params.build_opts) # position allocation if len(positions): # TODO: pass list of positions pos = positions.pop(0) logger.debug('set_pos', p=pos) await self.set_pos(name, pos, svc=svc) return svc
def handle_location(city=None, country=None, region=None, **kwargs): result = pdict() if country: result.country_en = str(country['name_en']) result.country_ru = str(country['name_ru']) result.country_iso = str(country['iso']) if city: result.city_en = str(city['name_en']) result.city_ru = str(city['name_ru']) if region: result.region_en = str(region['name_en']) result.region_ru = str(region['name_ru']) result.region_iso = str(region['iso']) return result
def format_person(person): # view = pdict(name=person['name'], courses=[], email='', phone='', phone_part='', google=person[google_field], id=str(person['id'])) view = pdict(name=person['name'], courses=set(), email='', phone='', phone_part='', id=str(person['id'])) for field in ['google', 'telegram']: view[field] = person[field] if len(person['phone']) and person['phone'][0]['value']: view.phone = clean_phone(person['phone'][0]['value']) view.phone_part = view.phone[-4:] if len(person['email']) and person['email'][0]['value']: view.email = person['email'][0]['value'].lower() return view
async def update_config(self, name, keysvals): config = (await self.load_config(name)) or pdict() for k, v in keysvals.items(): target = config path = k.split('.') prop = path.pop() for p in path: target = target[p] if v == '': target.pop(prop, None) else: target[prop] = v self.save_config(name, config) if name == SHARED_CONFIG_KEY: self._shared_config = config return config
def handle_location(city=None, country=None, subdivisions=None, **kwargs): result = pdict() if country: result.country_en = country['names']['en'] result.country_ru = country['names']['ru'] result.country_iso = country['iso_code'] if city and 'names' in city: result.city_en = city['names']['en'] result.city_ru = (city['names']['ru'] if 'ru' in city['names'] else en_to_ru(city['names']['en'])) if subdivisions and len(subdivisions) > 0: region = subdivisions[0] result.region_en = region['names']['en'] result.region_ru = (region['names']['ru'] if 'ru' in region['names'] else en_to_ru(region['names']['en'])) result.region_iso = region['iso_code'] return result
async def containers(self, as_dict=False, status=None, fullinfo=False, inband=True): filters = pdict() if inband: filters.label = ['inband'] if status: filters.status = [status] containers = await self.dc.containers.list( all=True, filters=ujson.dumps(filters)) lst = [] for c in containers: bc = await BandContainer.create_with_info(c) lst.append(bc) return lst if not as_dict else {c.name: c for c in lst}
def full_state(self): docker = self.dockstate appdata = self.appstate state = None uptime = None inband = False if docker: state = docker.state inband = docker.inband if appdata and appdata.app_uptime: uptime = appdata.app_uptime else: uptime = docker.uptime elif appdata and appdata.app_state == STATUS_RUNNING: state = STATUS_RUNNING uptime = appdata.app_uptime return pdict( name=self.name, uptime=uptime, state=self._status_override or state, title=self.title, inband=inband, pos=self.pos, # TODO: remove when dashboard updated sla=randint(98, 99), mem=randint(1, 3), cpu=randint(1, 3), stat=dict( sla=randint(98, 99), mem=randint(1, 3), cpu=randint(1, 3), ), meta=dict( native=self._native, managed=self._managed, protected=self._protected, persistent=self._persistent))
async def create_image(self, img, img_options): logger.debug("Building image", n=img.name, io=img_options, path=img.path) async with img.create(img_options) as builder: progress = pdict() struct = builder.struct() last = time() async for chunk in await self.dc.images.build(**struct): if isinstance(chunk, dict): chunk = pdict.from_dict(chunk) if chunk.aux: struct.id = chunk.aux.ID logger.debug('chunk', chunk=chunk) elif chunk.status and chunk.id: progress[chunk.id] = chunk if time() - last > 1: logger.info("\nDocker build progress", progress=progress) last = time() elif chunk.stream: # logger.debug('chunk', chunk=chunk) step = re.search(r'Step\s(\d+)\/(\d+)', chunk.stream) if step: logger.debug('Docker build step ', groups=step.groups()) else: logger.debug('unknown chunk', chunk=chunk) else: logger.debug('unknown chunk type', type=type(chunk), chunk=chunk) if not struct.id: raise Exception('Build process not completed') logger.info('Docker image created', struct_id=struct.id) return img.set_data(await self.dc.images.get(img.name))
from band import expose, logger, settings, worker, response from pysyge.pysyge import GeoLocator, MODE_BATCH, MODE_MEMORY from prodict import Prodict as pdict import subprocess import os from async_lru import alru_cache from aiohttp.web_exceptions import HTTPInternalServerError, HTTPNoContent state = pdict(geodata=None) @worker() async def startup(): """ Load database on startup """ try: if not os.path.isfile(settings.db_file): raise FileNotFoundError("db file not found") state.geodata = GeoLocator(settings.db_file, MODE_BATCH | MODE_MEMORY) logger.info('DB version', dbver=state.geodata.get_db_version(), dbdate=state.geodata.get_db_date()) except Exception: logger.exception('error while opening database file') def handle_location(city=None, country=None, region=None, **kwargs): result = pdict() if country: result.country_en = str(country['name_en'])
import subprocess import os.path import maxminddb import asyncio from band import expose, worker, logger, settings, response from prodict import Prodict as pdict from async_lru import alru_cache from aiohttp.web_exceptions import HTTPServiceUnavailable from itertools import count from transliterate import translit state = pdict(db=None) @expose() async def cache_info(): return dict(enrich.cache_info()._asdict()) @expose.enricher(keys=[settings.key_prefix], props=settings.props) @alru_cache(maxsize=512) async def enrich(**params): try: ip = params.get('ip', None) if not state.db: raise HTTPServiceUnavailable() if ip: location = state.db.get(ip) if location: return handle_location(**location) return {}
def config(self): return pdict( pos=self.pos, build_options=self.build_options, env=self._env)
def __init__(self): self.chats = pdict() self.joined = pdict()
def chat(self, id): if id not in self.chats: self.chats[id] = pdict(**DEF_CHAT) return self.chats[id]
""" Band service skeleton (c) Dmitry Rodin 2018 --------------------- """ import asyncio from itertools import count from prodict import Prodict as pdict from band import expose, cleanup, worker, settings, logger, response state = pdict() @expose.handler() async def main(**params): pass @worker() async def service_worker(): for num in count(): try: if num == 0: pass except asyncio.CancelledError: break except Exception: logger.exception('exc') await asyncio.sleep(30)
class state: persons = pdict() stages = pdict() deals = pdict() person_fields = pdict() deal_fields = pdict()
""" Rockstat calltracking main (c) Dmitry Rodin 2018 """ import asyncio from prodict import Prodict as pdict from band import expose, worker, settings, logger, redis_factory, scheduler from .structs import State from .helpers import rand_item, ms logger.info('p', p=settings.phones) state = State(users=pdict(), **settings.phones) @expose() async def user_by_phone(phone, **params): """ Find user by number """ logger.info('user_by_phone', phone=phone) for uid, user in state.users.items(): if phone == user.phone and user.sess_no: return dict(uid=uid, sess_no=user.sess_no) logger.warn('cant find associated session', uid=uid, phone=phone) @expose.handler() async def phone_request(uid='', **params): """ Get allocated phone """ if uid and uid in state.users: