From 632d165eae47b9986d5467a9fb6d608b9ec8f48f Mon Sep 17 00:00:00 2001 From: Marek Isalski Date: Sun, 16 Dec 2018 14:43:50 +0000 Subject: [PATCH] output of deployment needs to be private messages for debugging --- fih.py | 80 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/fih.py b/fih.py index 278bbf1..4b685bd 100644 --- a/fih.py +++ b/fih.py @@ -53,7 +53,7 @@ def channel_name_to_raw_incident_number( channel_name ): return channel_name[ len( app.config[ "MATTERMOST_INCIDENT_CHANNEL_PREFIX" ] ): ] -def mattermost_incident_command( command, args, channel_id, raw_incident_number, user_name ): +def mattermost_incident_command( command, args, channel_id, raw_incident_number, user_name, user_id ): channel = app.mm.channels.get_channel( channel_id ) slug = slugify.slugify( channel[ 'purpose' ] ) index = event.Index( path = app.config[ "STATUS_EVENT_INDEX" ] ) @@ -69,10 +69,13 @@ def mattermost_incident_command( command, args, channel_id, raw_incident_number, 'message': "### `PUBLISH` requested by @%s!" % user_name, } ) + private_channel = app.mm.channels.create_direct_message_channel( [ app.config[ "MATTERMOST_USER_ID" ], user_id ] ) + private_channel_id = private_channel[ 'id' ] + # update local repository cmdline = app.config[ "MERCURIAL_BIN" ] + " update" message = exec_to_message( cmdline ) - app.mm.posts.create_post( options = { 'channel_id': channel_id, + app.mm.posts.create_post( options = { 'channel_id': private_channel_id, 'message': message, } ) @@ -84,21 +87,21 @@ def mattermost_incident_command( command, args, channel_id, raw_incident_number, ev.set_published( datetime.datetime.utcnow().strftime( "%Y-%m-%d %H:%M" ) ) written = ev.write() - app.mm.posts.create_post( options = { 'channel_id': channel_id, + app.mm.posts.create_post( options = { 'channel_id': private_channel_id, 'message': "#### Written `%s`:\n\n```\n%s```" % ( ev.path_short(), written ), } ) cmdline = app.config[ "MERCURIAL_BIN" ] + " add " + ev.path_short() message = exec_to_message( cmdline ) if message: - app.mm.posts.create_post( options = { 'channel_id': channel_id, + app.mm.posts.create_post( options = { 'channel_id': private_channel_id, 'message': message, } ) cmdline = app.config[ "MERCURIAL_BIN" ] + " commit -m 'event for incident " + raw_incident_number + " via FIH' " + ev.path_short() message = exec_to_message( cmdline ) if message: - app.mm.posts.create_post( options = { 'channel_id': channel_id, + app.mm.posts.create_post( options = { 'channel_id': private_channel_id, 'message': message, } ) @@ -107,21 +110,21 @@ def mattermost_incident_command( command, args, channel_id, raw_incident_number, cmdline = app.config[ "MERCURIAL_BIN" ] + " pull " + pull_from message = exec_to_message( cmdline ) if message: - app.mm.posts.create_post( options = { 'channel_id': channel_id, + app.mm.posts.create_post( options = { 'channel_id': private_channel_id, 'message': message, } ) cmdline = app.config[ "MERCURIAL_BIN" ] + " merge" message = exec_to_message( cmdline ) if message: - app.mm.posts.create_post( options = { 'channel_id': channel_id, + app.mm.posts.create_post( options = { 'channel_id': private_channel_id, 'message': message, } ) cmdline = app.config[ "MERCURIAL_BIN" ] + " commit -m 'merge from " + pull_from + "'" message = exec_to_message( cmdline ) if message: - app.mm.posts.create_post( options = { 'channel_id': channel_id, + app.mm.posts.create_post( options = { 'channel_id': private_channel_id, 'message': message, } ) @@ -130,7 +133,7 @@ def mattermost_incident_command( command, args, channel_id, raw_incident_number, cmdline = app.config[ "MERCURIAL_BIN" ] + " push " + pull_from message = exec_to_message( cmdline ) if message: - app.mm.posts.create_post( options = { 'channel_id': channel_id, + app.mm.posts.create_post( options = { 'channel_id': private_channel_id, 'message': message, } ) @@ -138,11 +141,11 @@ def mattermost_incident_command( command, args, channel_id, raw_incident_number, cmdline = app.config[ "GROW_BIN" ] + " deploy -f " + deploy_to message = exec_to_message( cmdline ) if message: - app.mm.posts.create_post( options = { 'channel_id': channel_id, + app.mm.posts.create_post( options = { 'channel_id': private_channel_id, 'message': message, } ) - app.mm.posts.create_post( options = { 'channel_id': request.form[ 'channel_id' ], + app.mm.posts.create_post( options = { 'channel_id': channel_id, 'message': "### `PUBLISH` completed!", } ) @@ -246,30 +249,11 @@ def mattermost_incident_command( command, args, channel_id, raw_incident_number, 'message': "Flags set by @%s to: `%s`" % ( user_name, flags ), } ) else: - return "WTF command?" + post_help( channel, raw_incident_number, user_name ) return "" -def mattermost_incident_start( description, team_id, user_name, user_id, channel_id ): - raw_incident_number = generate_raw_incident_number() - fi_number = raw_incident_number_to_public_incident_number( raw_incident_number ) - channel_name = raw_incident_number_to_channel_name( raw_incident_number ) - - channel = app.mm.channels.create_channel( options = { "team_id": team_id, - "name": channel_name, - "display_name": fi_number, - "purpose": description, - "header": "", - "type": "O", - } ) - - # add user to the channel we just created - app.mm.client.make_request( 'post', '/channels/' + channel[ 'id' ] + '/members', options = { 'user_id': user_id, - } ) - app.mm.posts.create_post( options = { 'channel_id': channel[ 'id' ], - 'message': "Incident discussion channel for [%s](%s) created by @%s." % ( fi_number, raw_incident_number_to_url( raw_incident_number ), user_name ), - } ) - +def post_help( channel, raw_incident_number, user_name ): now = datetime.datetime.utcnow().strftime( "%Y-%m-%d %H:%M" ) app.mm.posts.create_post( options = { 'channel_id': channel[ 'id' ], @@ -297,11 +281,7 @@ Again, don't forget to **/incident PUBLISH** once making changes. ## Front Page Status -* **/incident INCIDENT** -* **/incident DEGRADED** -* **/incident MAINTENANCE** -* **/incident NOTICE** -* **/incident OK** +* **/incident [ INCIDENT | DEGRADED | MAINTENANCE | NOTICE | OK ]** Again, after setting the headline status you must: **/incident PUBLISH** @@ -311,8 +291,31 @@ Again, after setting the headline status you must: **/incident PUBLISH** }, } ) + +def mattermost_incident_start( description, team_id, user_name, user_id, channel_id ): + raw_incident_number = generate_raw_incident_number() + fi_number = raw_incident_number_to_public_incident_number( raw_incident_number ) + channel_name = raw_incident_number_to_channel_name( raw_incident_number ) + + channel = app.mm.channels.create_channel( options = { "team_id": team_id, + "name": channel_name, + "display_name": fi_number, + "purpose": description, + "header": "", + "type": "O", + } ) + + # add user to the channel we just created + app.mm.client.make_request( 'post', '/channels/' + channel[ 'id' ] + '/members', options = { 'user_id': user_id, + } ) + app.mm.posts.create_post( options = { 'channel_id': channel[ 'id' ], + 'message': "Incident discussion channel for [%s](%s) created by @%s." % ( fi_number, raw_incident_number_to_url( raw_incident_number ), user_name ), + } ) + + post_help( channel, raw_incident_number, user_name ) + app.mm.posts.create_post( options = { 'channel_id': channel_id, - 'message': 'On behalf of @%s I have started incident ~%s.' % ( request.form[ 'user_name' ], channel_name, ), + 'message': 'On behalf of @%s I have started incident ~%s.' % ( user_name, channel_name, ), } ) return "" @@ -338,6 +341,7 @@ def mattermost_incident(): args = " ".join( text[ 1: ] ), channel_id = request.form[ 'channel_id' ], user_name = request.form[ 'user_name' ], + user_id = request.form[ 'user_id' ], raw_incident_number = raw_incident_number, ) else: