Skip to content

Commit 0d8691e

Browse files
committed
add files
1 parent 941a793 commit 0d8691e

File tree

8 files changed

+115
-48
lines changed

8 files changed

+115
-48
lines changed

.vscode/launch.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
6+
"version": "0.2.0",
7+
"configurations": [
8+
{
9+
"name": "Python: Current File",
10+
"type": "python",
11+
"request": "launch",
12+
"program": "${file}",
13+
"console": "integratedTerminal",
14+
"justMyCode": true,
15+
"env":{"BACON":"SOGOOD"} //not working in vscode 08/2022
16+
}
17+
]
18+
}

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.envFile": "${workspaceFolder}/.env" //semi-broken. works in debug mode
3+
}

boto_automations/snapshots_restore_volume.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
print(vol.state)
9797
if vol.state == 'available':
9898
ec2_resource.Instance(instance_id).attach_volume(
99-
Device='/dev/xvdb',
99+
Device='/dev/xvda',
100100
VolumeId=new_volume['VolumeId']
101101
)
102102
print('\nVolume attached!')

play_test.py

-47
Original file line numberDiff line numberDiff line change
@@ -1,47 +0,0 @@
1-
from operator import itemgetter
2-
3-
l = ['one', 'two', 'three']
4-
d = {'pizza': 'good', 'pasta': 'also good', 'burger': 'very good'}
5-
6-
g_dict = {
7-
'choco': 'late',
8-
'van': 'illa',
9-
'g_list1':
10-
[
11-
{
12-
'fdict1a': 'fdictval1a',
13-
'fdict1b': 'fdictval1b',
14-
'StartTime': 333
15-
},
16-
{
17-
'fdict1a': 'fdictval2a',
18-
'fdict1b': 'fdictval2b',
19-
'StartTime': 111
20-
},
21-
{
22-
'fdict1a': 'fdictval2a-copy',
23-
'fdict1b': 'fdictval2b-copy',
24-
'StartTime': 222
25-
}
26-
],
27-
'espresso': 'freddo',
28-
'latte': 'machiatto',
29-
'g_list2':
30-
[
31-
{
32-
'fdict1a': 'fdictval3a',
33-
'fdict1b': 'fdictval3b'
34-
},
35-
{
36-
'fdict1a': 'fdictval4a',
37-
'fdict1b': 'fdictval4b'
38-
}
39-
],
40-
}
41-
42-
43-
# sort the list of dictionaries by 'StartTime' and reverse
44-
da_sorted = sorted(g_dict['g_list1'], key=itemgetter('StartTime'), reverse=True)
45-
46-
for item in da_sorted:
47-
print(item)

python_tips/env_vars.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
3+
# dotenv package
4+
# Mainly for development environments.
5+
# reads env vars from .env file, if exists.
6+
7+
# from dotenv import load_dotenv
8+
# load_dotenv()
9+
10+
11+
# python os package.
12+
# read env vars
13+
print(os.getenv('EMAIL_ADDRESS'))
14+
print(os.getenv('EMAIL_PASSWORD'))
15+
16+
print(os.environ.get('EMAIL_ADDRESS'))
17+
print(os.environ.get('EMAIL_PASSWORD'))
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from fabric import Connection
2+
from invoke import Responder
3+
4+
# set host 'ec2-monitor' in .ssh/config to be read automatically
5+
# else must specify connection details here
6+
result = Connection('ec2-monitor').run('docker ps', hide=True)
7+
msg = "Ran {0.command!r} on {0.connection.host}, got stdout:\n{0.stdout}"
8+
print(msg.format(result))
9+
10+
11+
# other syntax
12+
c = Connection('ec2-monitor')
13+
r = c.run('sudo whoami', hide=True)
14+
msg = "Ran {0.command!r} on {0.connection.host}, got stdout:\n{0.stdout}"
15+
print(msg.format(result))
16+
17+
18+
# sudo password prompt
19+
""" c = Connection('host')
20+
sudopass = Responder(
21+
pattern=r'\[sudo\] password:',
22+
response='mypassword\n',
23+
)
24+
c.run('sudo whoami', pty=True, watchers=[sudopass]) """
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# execute remote commands via shh with paramiko
2+
3+
from sys import stderr, stdin, stdout
4+
import paramiko
5+
6+
7+
ssh = paramiko.SSHClient()
8+
9+
# known_hosts prompt
10+
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
11+
12+
# connect as ec2-user with .pem | root connection might require priv-key pub-key.pub
13+
ssh.connect(hostname='18.184.246.158',
14+
username="ec2-user",
15+
key_filename='/home/mltamd/.ssh/ec2-key-pair-frankfurt.pem')
16+
stdin, stdout, stderr = ssh.exec_command('docker ps')
17+
print(stdout.readlines())
18+
ssh.close()

website_monitor/website-monitor.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import requests
2+
import smtplib
3+
import os
4+
# from dotenv import load_dotenv
5+
# load_dotenv()
6+
7+
EMAIL_ADDRESS = os.getenv('EMAIL_ADDRESS')
8+
EMAIL_PASSWORD = os.getenv('EMAIL_PASSWORD')
9+
10+
11+
def send_notification(email_content):
12+
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
13+
smtp.ehlo()
14+
smtp.starttls()
15+
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
16+
#smtp.sendmail(sender, receiver, message)
17+
email_content = f"Subject:WEB MONITOR ALERT\n{email_content}"
18+
smtp.sendmail(EMAIL_ADDRESS, EMAIL_ADDRESS, email_content)
19+
20+
21+
try:
22+
r = requests.get(
23+
'http://ec2-18-184-246-158.eu-central-1.compute.amazonaws.com:8080/')
24+
if r.status_code == 200:
25+
print("Website is up!")
26+
else:
27+
print("Website is down!")
28+
# send email
29+
message = "Website is down!"
30+
send_notification(message)
31+
except Exception as e:
32+
print(f"Connection error: {e}")
33+
message = "Connection error!"
34+
send_notification(message)

0 commit comments

Comments
 (0)