-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbell.py
More file actions
141 lines (114 loc) · 4.34 KB
/
bell.py
File metadata and controls
141 lines (114 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from socket import timeout
import urllib.request
import subprocess
import logging
import argparse
import json
import sys
import os
logging.basicConfig(
format="%(asctime)s | %(levelname)s | %(funcName)20s:%(lineno)s - %(message)s",
level=os.environ.get("LOGGING_LEVEL", logging.INFO),
)
logger = logging.getLogger(__name__)
def get_status_message():
try:
response = urllib.request.urlopen(
"http://169.254.169.254/latest/meta-data/instance-id", timeout=5
)
instance_id = response.read().decode("utf-8")
response = urllib.request.urlopen(
"http://169.254.169.254/latest/meta-data/public-hostname", timeout=5
)
public_hostname = response.read().decode("utf-8")
except urllib.request.URLError:
logger.debug(
"Metadata request timed out. You are probably not in an AWS instance."
)
return ":bellhop_bell: local mode"
try:
import boto3
except ImportError:
return f":bellhop_bell: {public_hostname} is running"
ec2 = boto3.client("ec2")
response = ec2.describe_tags(
Filters=[
{"Name": "resource-id", "Values": [instance_id]},
]
)
ec2_instance_name = response["Tags"][0]["Value"]
logger.debug(f"ec2 instance name: {ec2_instance_name}")
response = ec2.describe_instance_status(InstanceIds=[instance_id])
status = response["InstanceStatuses"][0]["InstanceState"]["Name"]
logger.debug(f"ec2 instance status: {status}")
return f":bellhop_bell: {ec2_instance_name} is {status}"
def send_slack_message(webhook_url, message):
logger.debug(f"slack message: {message}")
data = json.dumps({"text": message}).encode("utf-8")
request = urllib.request.Request(webhook_url)
request.add_header("Content-Type", "application/json; charset=utf-8")
request.add_header("Content-Length", len(data))
try:
response = urllib.request.urlopen(request, data, timeout=5)
logger.debug(f"slack response code: {response.getcode()}")
except urllib.request.URLError:
logger.error(
"slack message not sent. probably timeout. this is normal if you are testing or running locally."
)
def bell(webhook_url, capture_output=False, *command):
if command:
logger.debug(f"command: {command}")
try:
if not capture_output:
send_slack_message(
webhook_url,
f":bellhop_bell: command `{' '.join(command)}` started :rocket:",
)
# Capture the result of the process so we can use it later
result = subprocess.run(command, check=True, capture_output=capture_output)
if capture_output:
send_slack_message(
webhook_url,
f"```{result.stdout.decode('utf-8')}```",
)
except subprocess.CalledProcessError:
logger.error("command failed")
send_slack_message(
webhook_url, f":bellhop_bell: command `{' '.join(command.stdout)}` failed :x:"
)
raise
except AttributeError:
logger.error("command failed")
send_slack_message(
webhook_url, f":bellhop_bell: command `{' '.join(command.stdout)}` failed :x:"
)
raise
if not capture_output:
send_slack_message(
webhook_url, f":bellhop_bell: command `{' '.join(command)}` finished :boom:"
)
else:
instance_status = get_status_message()
send_slack_message(webhook_url, instance_status)
def cli():
argument_parser = argparse.ArgumentParser()
argument_parser.add_argument(
"--webhook-url",
default=os.environ.get("SLACK_WEBHOOK_URL"),
help="incoming slack webhook url",
)
argument_parser.add_argument(
"--capture-output",
action="store_true",
help="capture output of command",
default=False,
)
args, command_args = argument_parser.parse_known_args()
if not args.webhook_url:
print(
"You need to pass a value for webhook url either through --webhook-url or as an environment variable SLACK_WEBHOOK_URL"
)
sys.exit(1)
bell(args.webhook_url, args.capture_output, *command_args)
if __name__ == "__main__":
cli()