-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpire.py
36 lines (27 loc) · 1 KB
/
expire.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
import boto3
import lib
def expireHosts(session=None, max_age=None):
'''
Expire all hostnames that have not been updated
more recently than max_age seconds ago, and that
are not marked hold.
Active hostnames are those with files having a
PING extension. Hostnames being held have files
with a HOLD extension. To expire a host, we
delete the PING file and replace it with an
EXPIRED file.
'''
bucket = lib.S3Bucket(session)
holds = set(f.host for f in bucket.iterStateFiles(ext=bucket.HOLD_EXT))
expiry_time = lib.getExpiryTime(max_age)
expired = []
for f in (x for x in bucket.iterStateFiles(ext=bucket.PING_EXT)
if x.file.last_modified < expiry_time and x.host not in holds):
bucket.setHostIP(f.host, None)
bucket.writeStateFile(
f.host, bucket.EXPIRED_EXT, f.file.get()['Body'].read())
f.file.delete()
expired.append(f.host)
if expired:
lib.kickManager(bucket.session)
return expired