1
|
|
|
import os |
2
|
|
|
from helpers.telegramhelper import sendalert, sendphoto |
3
|
|
|
|
4
|
|
|
class ServiceName: |
5
|
|
|
'''names of infrastructure services''' |
6
|
|
|
messagebus = 'rabbit' |
7
|
|
|
cache = 'redis' |
8
|
|
|
database = 'mysql' |
9
|
|
|
email = 'gmail' |
10
|
|
|
telegram = 'telegram' |
11
|
|
|
|
12
|
|
|
class InfrastructureService: |
13
|
|
|
'''configuration for a dependency''' |
14
|
|
|
def __init__(self, name, connection, host, port, user, password): |
15
|
|
|
self.name = name |
16
|
|
|
self.connection = connection |
17
|
|
|
self.host = host |
18
|
|
|
self.port = port |
19
|
|
|
self.user = user |
20
|
|
|
self.password = password |
21
|
|
|
|
22
|
|
|
class Configuration(object): |
23
|
|
|
def __init__(self, config): |
24
|
|
|
self.__config = config |
25
|
|
|
|
26
|
|
|
def get(self, key): |
27
|
|
|
if not key in self.__config: |
28
|
|
|
return None |
29
|
|
|
return self.__config[key] |
30
|
|
|
|
31
|
|
|
def is_enabled(self, key): |
32
|
|
|
lookupkey = '{0}.enabled'.format(key) |
33
|
|
|
if not lookupkey in self.__config: |
34
|
|
|
return False |
35
|
|
|
value = self.__config[lookupkey] |
36
|
|
|
if isinstance(value, str): |
37
|
|
|
return value == 'true' or value == 'True' |
38
|
|
|
return value |
39
|
|
|
|
40
|
|
|
class Telegram(object): |
41
|
|
|
def __init__(self, config, service): |
42
|
|
|
self.configuration = config |
43
|
|
|
self.service = service |
44
|
|
|
|
45
|
|
|
def sendmessage(self, message): |
46
|
|
|
if self.configuration.is_enabled('telegram'): |
47
|
|
|
sendalert(message, self.service) |
48
|
|
|
|
49
|
|
|
def sendphoto(self, file_name): |
50
|
|
|
if os.path.isfile(file_name) and os.path.getsize(file_name) > 0: |
51
|
|
|
if self.configuration.is_enabled('telegram'): |
52
|
|
|
sendphoto(file_name, self.service) |
53
|
|
|
|
54
|
|
|
|