-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathark_updater.py
143 lines (113 loc) · 4.66 KB
/
ark_updater.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
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
142
143
RESTART_DELAY = 60
CRON_INTERVAL = 10
import sys
print ("PYTHON VERSION:"+sys.version)
import time
from random import choice
import ark_manager as am
from configuration import read_config, write_config
import yaml
from datetime import datetime, timedelta
import os
from debug import get_logger
log = get_logger("ark_updater")
from locks import Lock
def perform_checks(checks=None, auto_update=True):
if not checks:
checks = read_config('updater_config')['updater_checks']
queue = Lock("queue")
if queue.is_locked:
queue_data = yaml.load(queue.message)
first_detection = False
else:
queue_data = {"items": {}}
first_detection = True
notification = ""
if first_detection:
notification += "Restart scheduled ~{} minutes from now.\n".format(RESTART_DELAY)
# Go through all checks and run functions from arm_manager, storing returned dict values
# Return values must be dicts that will later be passed down to notification strings and action functions
detected = False
for check_id, check in enumerate(checks):
if check_id not in queue_data['items']:
check_response = getattr(am, check['check_function'])()
if check_response:
queue_data['items'][check_id] = check
checks[check_id]['check_response'] = check_response
log.debug("Adding to queue: {}; {};".format(check['update_name'], checks[check_id]['check_response']))
notification += "{} detected, adding to queue.\n".format(check['update_name'].format(**checks[check_id]['check_response']))
detected = True
# Send notification if new update was detected regardless of whether it's first update or not.
# If no updates detected it's not first detection, there's no queue and no notification will be sent.
if detected and notification.strip():
queue_data['update_time'] = datetime.now() + timedelta(minutes=RESTART_DELAY)
queue.lock(yaml.dump(queue_data, default_flow_style=False))
am.broadcast(notification, True)
# Now check if datetime for scheduled restart is near. If not warn about upcoming update. If it is then start
# performing restart and required actions.
if queue.is_locked:
if datetime.now() >= queue_data['update_time']:
if auto_update:
update(queue, queue_data)
def background_tasks():
tasks = read_config('updater_config')['background_tasks']
for task in tasks:
try:
getattr(am, task)()
except Exception as e:
am.discord_message(f"Background task failed: {task}; {e}")
def update(queue, queue_data):
general = Lock()
general.lock("Updating...")
am.broadcast("Server restarts in 5 minutes!", True)
time.sleep(60 * 4)
am.broadcast("Server restart in 1 minute! Ready or not, here I come!")
time.sleep(25)
am.broadcast("Seriously, restarting now.")
time.sleep(5)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
am.stop_server()
for item_id, item_data in queue_data['items'].items():
for action in item_data['actions']:
try:
result = getattr(am, action)(**item_data['check_response'])
except Exception as e:
am.broadcast("Unable to run {}: {}".format(action, str(e)))
log.error("Unable to run {}: {}".format(action, str(e)), exc_info=True)
am.start_server()
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
time.sleep(60 * 3)
am.broadcast("Server should be back up and running by now or in a few more minutes.", True)
general.unlock()
queue.unlock()
# def update():
# lock = Lock()
#
# if lock.locked:
# log.debug("Another script already running, exit...")
# sys.exit()
#
# if check_version():
# print ("Update found!")
# log.info("New version found, performing update.")
# lock.lock("Game updater.")
# run_with_delay(update_server, message="Update detected. ")
# update_server()
# time.sleep(10 * 60)
# broadcast("Update finished. Server should be up and running by now.", True)
# time.sleep(20 * 60)
# lock.unlock()
# else:
# log.debug("No new versions found.")
# print ("No new versions.")
if __name__ == "__main__":
import sys
general = Lock()
if general.is_locked:
sys.exit()
background = Lock("background_tasks")
if not background.is_locked:
background.lock()
background_tasks()
background.unlock()
perform_checks()