This repository has been archived by the owner on Nov 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
maintain_quota.py
109 lines (88 loc) · 4.08 KB
/
maintain_quota.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
#!/usr/bin/python3
import click
import os
import random
import requests
import subprocess
import string
import socket
import yaml
from hs.admin.api import API
def convert_to_mb(value):
if not value:
return None
if value.endswith('M'):
return int(value[:-1])
elif value.endswith('G'):
quota = int(value[:-1])
# for bigger quota, give some more space
if quota > 20:
quota += 3
return quota*1000
def maintain_quota(config, url, admin_token, host_name, product_slug, pac_user, pac_pwd):
params = dict(format='json', hostname=host_name, product=product_slug, action="quota")
url += '/api/v1/instances/'
resp = requests.get(url=url, params=params, headers={'Authorization': f'Token {admin_token}'})
if resp.status_code != 200:
print(resp)
print(resp.text)
return
data = resp.json()
if 'detail' in data:
raise Exception(data['detail'])
# print(data)
api = API(cas=dict(uri='https://login.hostsharing.net/cas/v1/tickets',
service='https://config.hostsharing.net:443/hsar/backend'),
credentials=dict(username=pac_user,
password=pac_pwd),
backends=['https://config.hostsharing.net:443/hsar/xmlrpc/hsadmin',
'https://config2.hostsharing.net:443/hsar/xmlrpc/hsadmin'])
return_code = None
for instance in data:
if return_code:
continue
print(instance['prefix'] + instance['identifier'] + ' ' + instance['status'])
#print(instance)
# compare current quota with quota from saasadmin
new_quota_softlimit = convert_to_mb(instance['quota_app'])
new_storage_softlimit = convert_to_mb(instance['quota_storage'])
if not new_quota_softlimit or not new_storage_softlimit:
continue
username = pac_user + '-' + instance['prefix'] + instance['identifier']
current_hsuser = api.user.search(where={'name': username})[0]
need_change = False
# if we change the hard limit, we must change the softlimit too
new_quota_hardlimit = new_quota_softlimit + 2000
new_storage_hardlimit = new_storage_softlimit + 25000
if int(current_hsuser['quota_softlimit']) != new_quota_softlimit:
need_change = True
if int(current_hsuser['storage_softlimit']) != new_storage_softlimit:
need_change = True
# update quota if different
if need_change:
print(f"previously: {current_hsuser['quota_softlimit']} {current_hsuser['storage_softlimit']}")
print(f"new: {new_quota_softlimit} {new_storage_softlimit}")
api.user.update(where={'name': username}, set={'quota_softlimit': str(new_quota_softlimit),
'quota_hardlimit': str(new_quota_hardlimit),
'storage_softlimit': str(new_storage_softlimit),
'storage_hardlimit': str(new_storage_hardlimit),
})
# TODO: check current usage
# TODO: tell the admin or the customer if quota is full
@click.command()
@click.option('--product', default='randomapp', help='The shortname/slug of the product', prompt=True)
@click.option('--hostname', default='host0001.example.org', help='The hostname of the instances', prompt=True)
@click.option('--configfile', default='config.yaml', help='The config file to use')
def main(product, hostname, configfile):
"""check and maintain the quota for all specified instances"""
# load from config.yml file
with open(configfile, "r") as stream:
try:
config = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
admintoken=config['saasadmin']['admin_token']
url=config['saasadmin']['url']
maintain_quota(config, url, admintoken, hostname, product, config['saasadmin']['pacuser'], config['saasadmin']['pacpwd'])
if __name__ == '__main__':
main()