def user_entering_idtenant(message): if opstack.normalize_tenent_name(message.text) is not None: get_name = opstack.normalize_tenent_name(message.text) answer = opstack.bot_get_tenant_id(get_name) bot.send_message(message.chat.id, f"Tenant ID: {answer}") state.set_state(message.from_user.id, config.States.S_start.value) logger.info("response id tenant")
def user_entering_idtenant(message): if opstack.normalize_tenent_name(message.text) is not None: get_name = opstack.normalize_tenent_name(message.text) answer = opstack.bot_server_list(get_name) send_large_msg(message.chat.id, "".join(answer)) state.set_state(message.from_user.id, config.States.S_start.value) logger.info("response server list")
def cmd_show_vm(message): logger.info("command show VM/server info", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}}) st = state.get_state(message.from_user.id) # it's block a code, need to for show status VM after action with VM try: # after migrate (for exemple) we set state in dict (id_vm = {}) # and command show will be return us state many times srv_id = state.get_id_vm(message.from_user.id) if len(srv_id) == 36: recording = 1 else: recording = 0 except: recording = 0 if recording == 1: answer = opstack.bot_show_vm(state.get_id_vm(message.from_user.id)) bot.send_message(message.chat.id, f"I remember you chose: <code>{srv_id}</code>", parse_mode="HTML") send_large_msg(message.chat.id, "".join(answer)) bot.send_message(message.chat.id, f"if you have to choose a new server /cancel or repeat /show", parse_mode="HTML") elif st == config.States.S_start.value or st is False: bot.send_message(message.chat.id, "Ok, i show it, give my ID VM") state.set_state(message.from_user.id, config.States.S_cmd_show_vm.value) elif not st == config.States.S_start.value: bot.send_message(message.chat.id, "Complete previous action or reset active command /cancel") else: logger.error("error in cmd_show_vm")
def user_entering_idvm(message): if opstack.normalize_tenent_name(message.text) is not None: get_name = opstack.normalize_tenent_name(message.text) result = opstack.bot_get_type_env(get_name) tenant_id = opstack.bot_get_tenant_id(get_name) send_large_msg(message.chat.id, f"{result}\nTenant ID: <code>{tenant_id}</code>") state.set_state(message.from_user.id, config.States.S_start.value) logger.info("response info tenant")
def user_entering_idvm(message): if opstack.normalize_server_id(message.text) is not None: srv_id = opstack.normalize_server_id(message.text) state.set_id_vm(message.from_user.id, srv_id) answer = opstack.bot_diagnostic(state.get_id_vm(message.from_user.id)) send_large_msg(message.chat.id, "".join(answer)) state.set_state(message.from_user.id, config.States.S_start.value) logger.info("response diag server")
def user_entering_idvm(message): if opstack.normalize_server_id(message.text) is not None: srv_id = opstack.normalize_server_id(message.text) state.set_id_vm(message.from_user.id, srv_id) rez = opstack.bot_reboot_srv(state.get_id_vm(message.from_user.id)) send_large_msg(message.chat.id, rez) state.set_state(message.from_user.id, config.States.S_start.value) logger.info("response reboot vm")
def flavar_param(message): if opstack.normalize_flavor_param(message.text) is not None: # send request for create requst = opstack.normalize_flavor_param(message.text) answer = opstack.flavor_create(requst[0],requst[1],requst[2]) # send answer in function for division into parts send_large_msg(message.chat.id, answer) state.set_state(message.from_user.id, config.States.S_start.value) logger.info(f"create flavor{answer}")
def server_list(message): logger.info("command list all server in tenant/projectd", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}}) st = state.get_state(message.from_user.id) if st == config.States.S_start.value or st is False: bot.send_message(message.chat.id, "Ok, send my <code>tenant/project name</code>", parse_mode="HTML") state.set_state(message.from_user.id, config.States.S_server_list.value) elif not st == config.States.S_start.value: bot.send_message(message.chat.id, "Complete previous action or reset active state /cancel") else: logger.error("error in cmd_show_vm")
def quota_show(message): logger.info("command show quota", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}}) st = state.get_state(message.from_user.id) if st == config.States.S_start.value or st is False: bot.send_message(message.chat.id, "Ok, send my <code>tenant name</code>", parse_mode="HTML") state.set_state(message.from_user.id, config.States.S_quota_show.value) elif not st == config.States.S_start.value: bot.send_message(message.chat.id, "Complete previous action or reset active state /cancel") else: logger.error("error in server_diagnostic")
def cmd_tv(message): logger.info("command get console", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}}) # get state for check st = state.get_state(message.from_user.id) if st == config.States.S_start.value or st is False: bot.send_message(message.chat.id, "Send my *ID VM*", parse_mode="Markdown") # if state ok, push state in dict and start wrapper state.set_state(message.from_user.id, config.States.S_cmd_tv.value) elif not st == config.States.S_start.value: bot.send_message(message.chat.id, "Complete previous action or reset active command /cancel") else: # Exit to error logger.error("err in cmd_tv")
def flavor_list(message): logger.info("command flavor list", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}}) keyboard = types.InlineKeyboardMarkup(row_width=3) callback_button1 = types.InlineKeyboardButton(text="i01", callback_data=f"i01 {message.from_user.id}") callback_button2 = types.InlineKeyboardButton(text="i02", callback_data=f"i02 {message.from_user.id}") callback_button3 = types.InlineKeyboardButton(text="i03", callback_data=f"i03 {message.from_user.id}") callback_button4 = types.InlineKeyboardButton(text="i04", callback_data=f"i04 {message.from_user.id}") callback_button5 = types.InlineKeyboardButton(text="i04", callback_data=f"i05 {message.from_user.id}") callback_button6 = types.InlineKeyboardButton(text="i05", callback_data=f"i06 {message.from_user.id}") keyboard.add(callback_button1, callback_button2, callback_button3, callback_button4, callback_button5, callback_button6) bot.send_message(message.chat.id, "choose action:", reply_markup=keyboard) state.set_state(message.from_user.id, config.States.S_start.value) logger.info("response keybord action server")
def user_entering_idvm(message): # send messages in normalazer if opstack.normalize_server_id(message.text) is not None: # get normalized param srv_id = opstack.normalize_server_id(message.text) # set state id VM for show command state.set_id_vm(message.from_user.id, srv_id) # send request in openstack rez = opstack.bot_ops_url(state.get_id_vm(message.from_user.id)) # send answer in function for division into parts send_large_msg(message.chat.id, rez) # reset user state in start position state.set_state(message.from_user.id, config.States.S_start.value) # send log about request logger.info("response console")
def cmd_server(message): logger.info("command server action", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}}) if state.check_admins_id(message.from_user.id): # Check access st = state.get_state(message.from_user.id) if st == config.States.S_start.value or st not in locals(): bot.send_message(message.chat.id, "Send me server ID") state.set_state(message.from_user.id, config.States.S_server.value) elif not st == config.States.S_start.value: bot.send_message(message.chat.id, "Complete previous action or reset active command /cancel") else: logger.error("error in cmd_server") else: bot.send_message(message.chat.id, "Access denied, if you need access /access") logger.warning(f"Access denied {message.from_user.id}", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}})
def cmd_hard_reboot_vm(message): logger.info("command reboot server", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}}) if state.check_admins_id(message.from_user.id): # Check access st = state.get_state(message.from_user.id) if st == config.States.S_start.value or st is False: bot.send_message(message.chat.id, "Ok, i do it, give my server ID") state.set_state(message.from_user.id, config.States.S_server_retart.value) elif not st == config.States.S_start.value: bot.send_message(message.chat.id, "Complete previous action or reset active command /cancel") else: logger.error("error in cmd_show_vm") else: bot.send_message(message.chat.id, "Access denied, if you need access /access") logger.warning(f"Access denied {message.from_user.id}", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}})
def cmd_live_migrate(message): if state.check_admins_id(message.from_user.id): # Check access logger.info("command live migrate", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}}) st = state.get_state(message.from_user.id) if st == config.States.S_start.value or st is False: bot.send_message(message.chat.id, "Ok, i show it, give my ID VM") state.set_state(message.from_user.id, config.States.S_live_migrate.value) elif not st == config.States.S_start.value: bot.send_message(message.chat.id, "Complete previous action or reset active command /cancel") else: logger.error("error in cmd_live_migrate") else: bot.send_message(message.chat.id, "Access denied, if you need access /access") logger.warning(f"Access denied {message.from_user.id}", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}})
def get_id(message): logger.info("command create flavor", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}}) if state.check_admins_id(message.from_user.id): st = state.get_state(message.from_user.id) if st == config.States.S_start.value or st is False: send_large_msg(message.chat.id, f"get info about flavor, for exemple: i43,2,16") state.set_state(message.from_user.id, config.States.S_flavor_create.value) elif not st == config.States.S_start.value: bot.send_message(message.chat.id, "Complete previous action or reset active state /cancel") else: logger.error("error in get_id") else: bot.send_message(message.chat.id, "Access denied, if you need access /access") logger.warning(f"Access denied {message.from_user.id}", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}})
def get_id(message): logger.info("command show tenant id", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}}) if state.check_admins_id(message.from_user.id): st = state.get_state(message.from_user.id) if st == config.States.S_start.value or st is False: bot.send_message(message.chat.id, "Ok, send my `tenant/project name`", parse_mode="Markdown") state.set_state(message.from_user.id, config.States.S_get_tenant_id.value) elif not st == config.States.S_start.value: bot.send_message(message.chat.id, "Complete previous action or reset active state /cancel") else: logger.error("error in get_id") else: bot.send_message(message.chat.id, "Access denied, if you need access /access") logger.warning(f"Access denied {message.from_user.id}", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}})
def user_entering_action(message): if opstack.normalize_server_id(message.text) is not None: srv_id = opstack.normalize_server_id(message.text) state.set_id_vm(message.from_user.id, srv_id) keyboard = types.InlineKeyboardMarkup(row_width=3) callback_button1 = types.InlineKeyboardButton(text="show", callback_data=f"show {message.from_user.id}") callback_button2 = types.InlineKeyboardButton(text="console", callback_data=f"console {message.from_user.id}") callback_button3 = types.InlineKeyboardButton(text="start", callback_data=f"start {message.from_user.id}") callback_button4 = types.InlineKeyboardButton(text="stop", callback_data=f"stop {message.from_user.id}") callback_button5 = types.InlineKeyboardButton(text="reboot", callback_data=f"reboot {message.from_user.id}") callback_button6 = types.InlineKeyboardButton(text="live migrate", callback_data=f"live_migrate {message.from_user.id}") keyboard.add(callback_button1, callback_button2, callback_button3, callback_button4, callback_button5, callback_button6) bot.send_message(message.chat.id, "choose action:", reply_markup=keyboard) state.set_state(message.from_user.id, config.States.S_start.value) logger.info("response keybord action server") else: bot.send_message(message.chat.id, "Complete previous action or reset active command /cancel")
def host_list(message): if state.check_admins_id(message.from_user.id): logger.info("command host list", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}}) keyboard = types.InlineKeyboardMarkup(row_width=3) callback_button1 = types.InlineKeyboardButton(text="i01", callback_data=f"i01_host {message.from_user.id}") callback_button2 = types.InlineKeyboardButton(text="i02", callback_data=f"i02_host {message.from_user.id}") callback_button3 = types.InlineKeyboardButton(text="i03", callback_data=f"i03_host {message.from_user.id}") callback_button4 = types.InlineKeyboardButton(text="i04", callback_data=f"i04_host {message.from_user.id}") callback_button5 = types.InlineKeyboardButton(text="i05", callback_data=f"i05_host {message.from_user.id}") callback_button6 = types.InlineKeyboardButton(text="i06", callback_data=f"i06_host {message.from_user.id}") keyboard.add(callback_button1, callback_button2, callback_button3, callback_button4, callback_button5, callback_button6) bot.send_message(message.chat.id, "choose action:", reply_markup=keyboard) state.set_state(message.from_user.id, config.States.S_start.value) logger.info("response keybord action server") else: bot.send_message(message.chat.id, "Access denied, if you need access /access") logger.warning(f"Access denied {message.from_user.id}", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}})
def lambda_handler(event, context): account = sts.get_caller_identity()['Account'] if (Config.get('enable_mock_response') == 'true'): logger.info('Using mock data for describe_vpn_connections()') path = os.path.dirname(os.path.abspath(__file__)) with open(path + '/data/mock_vpn_connections.json') as mock_vpn_connections: response = json.load(mock_vpn_connections) else: response = ec2.describe_vpn_connections() link_down_tolerance = Config.get('link_down_tolerance') subject_line = '[{0}] VPN state change detected!' for vpnConnection in response['VpnConnections']: links_down = [] links_up = [] message_details = [] tunnel_state = [] message_description = '' state_changed = False tags = parse_tags(vpnConnection['Tags']) logger.info('Processing vpn connection with id {0}'.format( vpnConnection['VpnConnectionId'])) logger.debug( 'This vpn has {0} tunnels and has a link down tolerance of {1}'. format(len(vpnConnection['VgwTelemetry']), link_down_tolerance)) for tunnel_telemetry in vpnConnection['VgwTelemetry']: last_state = get_state(vpnConnection['VpnConnectionId'], tunnel_telemetry['OutsideIpAddress']) current_state = tunnel_telemetry['Status'] logger.debug('Tunnel {0} has a current status of {1}'.format( tunnel_telemetry['OutsideIpAddress'], current_state)) if current_state == 'UP': links_up.append(tunnel_telemetry) if current_state == 'DOWN': links_down.append(tunnel_telemetry) if (last_state is not None) and (last_state != current_state): if current_state != 'UP': state_changed = True logger.warn( 'Connection change detected on tunnel {tunnel}: {original_status} -> {new_status}' .format(tunnel=tunnel_telemetry['OutsideIpAddress'], original_status=last_state, new_status=current_state)) tunnel_state.append({ 'id': tunnel_telemetry['OutsideIpAddress'], 'last_state': last_state, 'current_state': current_state }) set_state(tags['Name'], vpnConnection['VpnConnectionId'], tunnel_telemetry['OutsideIpAddress'], current_state) # If there has been a state change, then.. if state_changed is True: # --- Info message if the number of active links is equal to the number of connections if len(links_up) == len(vpnConnection['VgwTelemetry']): subject = subject_line.format('INFO') message_details.append('Tunnel connections are within bounds.') # --- Alert if the number of dead is equal to the number of connections elif len(links_down) == len(vpnConnection['VgwTelemetry']): subject = subject_line.format( Config.get('tolerance_breach_priority')) message_details.append('All tunnel connections are down.') message_description = format_snow_message( tags['Name'], Config.get('tolerance_breach_priority')) # --- Alert if there is a loss of redundancy elif (len(links_down) > 0) and (len(links_down) < len( vpnConnection['VgwTelemetry'])): subject = subject_line.format( Config.get('redundancy_breach_priority')) message_details.append('Loss of redundancy has occured.') message_description = format_snow_message( tags['Name'], Config.get('redundancy_breach_priority')) logger.info('Publishing message..') message_parameters = { 'details': '\n'.join(message_details), 'vpn_name': tags['Name'], 'description': message_description, 'tunnels': tunnel_state, 'requestId': context.aws_request_id } # --- Send an email alert send_sns_message( Config.get('sns_email_topic_name'), subject, get_message_body('email_message_template.txt', message_parameters)) # --- Send a slack alert send_sns_message( Config.get('sns_slack_topic_name'), subject, get_message_body('slack_message_template.txt', message_parameters)) return { 'statusCode': 202, }
def cmd_reset(message): logger.info("command reset state conversation", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}}) bot.send_message(message.chat.id, "Active command to cancel") state.set_state(message.from_user.id, config.States.S_start.value) state.del_id_vm(message.from_user.id)
else: logger.error("error in cmd_live_migrate") else: bot.send_message(message.chat.id, "Access denied, if you need access /access") logger.warning(f"Access denied {message.from_user.id}", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}}) # return information about the project @bot.message_handler(commands=["project", "tenant"]) def cmd_get_type_cod(message): logger.info("command show info about tenant", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}}) st = state.get_state(message.from_user.id) if st == config.States.S_start.value or st is False: bot.send_message(message.chat.id, "Ok, send my tenant/project name") state.set_state(message.from_user.id, config.States.S_get_type_cod.value) elif not st == config.States.S_start.value: bot.send_message(message.chat.id, "Complete previous action or reset active state /cancel") else: logger.error("error in get_type_cod") # /reset state conversation @bot.message_handler(commands=["reset", "cancel"]) def cmd_reset(message): logger.info("command reset state conversation", extra={'bot': {'chat_id': message.chat.id, 'user_id': message.from_user.id}}) bot.send_message(message.chat.id, "Active command to cancel") state.set_state(message.from_user.id, config.States.S_start.value) state.del_id_vm(message.from_user.id)