##// END OF EJS Templates
piura :: telegram schain restart file [bug] - script
jespinoza -
r494:3b4213a1f18f
parent child
Show More
@@ -1,103 +1,103
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
2 # -*- coding: utf-8 -*-
3
3
4 from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
4 from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
5 import logging, os, json, time, requests, docker
5 import logging, os, json, time, requests, docker
6
6
7 IDS_VALUE={'ids': list(map(int, os.getenv('SOPHY_IDS_TELEGRAM', '9999999999,0000000000').split(',')))}
7 IDS_VALUE={'ids': list(map(int, os.getenv('SOPHY_IDS_TELEGRAM', '9999999999,0000000000').split(',')))}
8
8
9 # Enable logging
9 # Enable logging
10 logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
10 logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
11 logger = logging.getLogger(__name__)
11 logger = logging.getLogger(__name__)
12
12
13 def restart_schain(update, context):
13 def restart_schain(update, context):
14 """Send a message when the command /restart_schain is issued."""
14 """Send a message when the command /restart_schain is issued."""
15
15
16 if update.to_dict()['message']['from']['id'] in IDS_VALUE['ids']:
16 if update.to_dict()['message']['from']['id'] in IDS_VALUE['ids']:
17
17
18 response_status = requests.get('http://sophy-schain/status')
18 response_status = requests.get('http://sophy-schain/status')
19
19
20 if response_status.status_code == 200:
20 if response_status.status_code == 200:
21 update.message.reply_text('Experiment name: %s' % (response_status.json()['name']))
22 time.sleep(1)
23 update.message.reply_text('Parameters: %s' % (response_status.json()['parameters']))
24 time.sleep(1)
25 update.message.reply_text('Range: %s' % (response_status.json()['range']))
26 time.sleep(1)
27
28 if response_status.json()['running']:
21 if response_status.json()['running']:
22 update.message.reply_text('Experiment name: %s' % (response_status.json()['name']))
23 time.sleep(1)
24 update.message.reply_text('Parameters: %s' % (response_status.json()['online']['polarimetric']))
25 time.sleep(1)
26 update.message.reply_text('Range: %s' % (response_status.json()['online']['range']))
27 time.sleep(1)
29 #-------------------#
28 #-------------------#
30 try:
29 try:
31 docker_client = docker.from_env()
30 docker_client = docker.from_env()
32 docker_client.containers.get("sirm-schain").restart()
31 docker_client.containers.get("sirm-schain").restart()
33 update.message.reply_text('sirm-schain container restared!')
32 update.message.reply_text('sirm-schain container restared!')
34 except:
33 except:
35 update.message.reply_text('ERROR: sirm-schain container not restared!')
34 update.message.reply_text('ERROR: sirm-schain container not restared!')
36 time.sleep(1)
35 time.sleep(1)
37 #-------------------#
36 #-------------------#
38 response_schain = requests.post('http://sophy-schain/start', json={'name': response_status.json()['name']})
37 #response_schain = requests.post('http://sophy-schain/start', json={'name': response_status.json()['name']})
38 response_schain = requests.post('http://sophy-schain/start', json=response_status.json())
39 if response_schain.status_code == 200:
39 if response_schain.status_code == 200:
40 update.message.reply_text('Done: schain has restared!')
40 update.message.reply_text('Done: schain has restared!')
41 else:
41 else:
42 update.message.reply_text('Fail: Please, try again!')
42 update.message.reply_text('Fail: Please, try again!')
43 else:
43 else:
44 update.message.reply_text("Experiment isn't running")
44 update.message.reply_text("Experiment isn't running")
45 else:
45 else:
46 update.message.reply_text("ERROR: sirm-schain not running")
46 update.message.reply_text("ERROR: sirm-schain not running")
47 else:
47 else:
48 update.message.reply_text('ERROR: You must be authorized to use this action')
48 update.message.reply_text('ERROR: You must be authorized to use this action')
49
49
50 #def start_schain(update, context):
50 #def start_schain(update, context):
51 # """Send a message when the command /start is issued."""
51 # """Send a message when the command /start is issued."""
52 # update.message.reply_text('Hello, please write /help for to use the options!')
52 # update.message.reply_text('Hello, please write /help for to use the options!')
53
53
54 def help_schain(update, context):
54 def help_schain(update, context):
55 update.message.reply_text('Commands used:')
55 update.message.reply_text('Commands used:')
56 time.sleep(1)
56 time.sleep(1)
57 update.message.reply_text('/restart_schain -> Restart schain')
57 update.message.reply_text('/restart_schain -> Restart schain')
58 #time.sleep(1)
58 #time.sleep(1)
59 #update.message.reply_text('/show_exp -> Show experiment name')
59 #update.message.reply_text('/show_exp -> Show experiment name')
60
60
61
61
62 #def show_exp(update, context):
62 #def show_exp(update, context):
63 #
63 #
64 # if update.message.chat.id in IDS_VALUE[update.message.chat.type]:
64 # if update.message.chat.id in IDS_VALUE[update.message.chat.type]:
65 # response_status = requests.get('http://sophy/status')
65 # response_status = requests.get('http://sophy/status')
66 # if response_status.status_code == 200:
66 # if response_status.status_code == 200:
67 # update.message.reply_text(str(response_status.json()['name']))
67 # update.message.reply_text(str(response_status.json()['name']))
68 # else:
68 # else:
69 # update.message.reply_text("No experiment is running")
69 # update.message.reply_text("No experiment is running")
70 #
70 #
71 # else:
71 # else:
72 # update.message.reply_text('ERROR: You must be authorized to use this action')
72 # update.message.reply_text('ERROR: You must be authorized to use this action')
73
73
74 def error(update, context):
74 def error(update, context):
75 """Log Errors caused by Updates."""
75 """Log Errors caused by Updates."""
76 logger.warning('Update "%s" caused error "%s"', update, context.error)
76 logger.warning('Update "%s" caused error "%s"', update, context.error)
77
77
78
78
79 def main():
79 def main():
80 updater = Updater("6026493035:AAE8vTbZqDWxu82nE8dPSMJKo4CiILIWxbs", use_context=True)
80 updater = Updater("6026493035:AAE8vTbZqDWxu82nE8dPSMJKo4CiILIWxbs", use_context=True)
81
81
82 # Get the dispatcher to register handlers
82 # Get the dispatcher to register handlers
83 dp = updater.dispatcher
83 dp = updater.dispatcher
84
84
85 # on different commands - answer in Telegram
85 # on different commands - answer in Telegram
86 #dp.add_handler(CommandHandler("start_schain", start_schain))
86 #dp.add_handler(CommandHandler("start_schain", start_schain))
87 #dp.add_handler(CommandHandler("show_exp", show_exp))
87 #dp.add_handler(CommandHandler("show_exp", show_exp))
88 dp.add_handler(CommandHandler("help_schain", help_schain))
88 dp.add_handler(CommandHandler("help_schain", help_schain))
89 dp.add_handler(CommandHandler("restart_schain", restart_schain))
89 dp.add_handler(CommandHandler("restart_schain", restart_schain))
90
90
91 # log all errors
91 # log all errors
92 dp.add_error_handler(error)
92 dp.add_error_handler(error)
93
93
94 # Start the Bot
94 # Start the Bot
95 updater.start_polling()
95 updater.start_polling()
96
96
97 # Run the bot until you press Ctrl-C or the process receives SIGINT,
97 # Run the bot until you press Ctrl-C or the process receives SIGINT,
98 # SIGTERM or SIGABRT. This should be used most of the time, since
98 # SIGTERM or SIGABRT. This should be used most of the time, since
99 # start_polling() is non-blocking and will stop the bot gracefully.
99 # start_polling() is non-blocking and will stop the bot gracefully.
100 updater.idle()
100 updater.idle()
101
101
102 if __name__ == '__main__':
102 if __name__ == '__main__':
103 main()
103 main()
General Comments 0
You need to be logged in to leave comments. Login now