-
Notifications
You must be signed in to change notification settings - Fork 0
/
apt-upgrade.py
56 lines (46 loc) · 1.75 KB
/
apt-upgrade.py
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
import subprocess
import datetime
import requests
import os
import pytz
from pytz import timezone
webhook_url = 'https://discord.com/api/webhooks/<embed>'
hostname = os.uname()[1]
start_time = datetime.datetime.now(pytz.timezone('US/Eastern'))
subprocess.run(["sudo", "apt", "update", "-y"])
# Count the number of upgradable packages
output = subprocess.Popen(["apt", "list", "--upgradable"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = output.communicate()
output = stdout.decode('utf-8')
upgraded_packages = len([line for line in output.split('\n') if "upgradable" in line])
os.environ["DEBIAN_FRONTEND"] = "noninteractive"
subprocess.run(["sudo", "-E", "apt-get", "-o", "Dpkg::Options::=--force-confold", "-o", "Dpkg::Options::=--force-confdef", "dist-upgrade", "-q", "-y", "--allow-downgrades", "--allow-remove-essential", "--allow-change-held-packages"])
subprocess.run(["sudo", "apt-get", "autoclean", "-y"])
end_time = datetime.datetime.now(pytz.timezone('US/Eastern'))
duration = end_time - start_time
duration_string = '{:02d}:{:02d}:{:02d}'.format(duration.seconds // 3600, (duration.seconds % 3600) // 60, duration.seconds % 60)
embed = {
"title": hostname,
"color": 14501908,
"thumbnail": {
"url": "https://i.imgur.com/4ygriPa.png"
},
"fields": [
{
'name': 'Package Updates',
'value': upgraded_packages,
'inline': True
},
{
'name': 'Duration',
'value': duration_string,
'inline': True
},
{
'name': 'Date and Time',
'value': start_time.strftime('%Y-%m-%d %I:%M %p %Z'),
},
],
}
response = requests.post(webhook_url, json={'embeds': [embed]})
print(response.status_code)