Commit f978bdee by tarokkk

store-server: fixed quota

parent aedd2a46
...@@ -229,7 +229,7 @@ def set_quota(neptun): ...@@ -229,7 +229,7 @@ def set_quota(neptun):
quota = request.json['QUOTA'] quota = request.json['QUOTA']
except: except:
abort(400, 'Wrong syntax!') abort(400, 'Wrong syntax!')
result = subprocess.call([ROOT_BIN_FOLDER+'/'+USER_MANAGER, 'setquota', neptun, quota, hard_quota(quota)]) result = subprocess.call([ROOT_BIN_FOLDER+'/'+USER_MANAGER, 'setquota', neptun, str(quota), hard_quota(quota)])
if result == 0: if result == 0:
return return
elif result == 2: elif result == 2:
...@@ -240,25 +240,30 @@ def set_quota(neptun): ...@@ -240,25 +240,30 @@ def set_quota(neptun):
@force_ssl @force_ssl
def new_user(neptun): def new_user(neptun):
key_list = [] key_list = []
smbpasswd='' smbpasswd = ''
quota = ''
try: try:
smbpasswd = request.json['SMBPASSWD'] smbpasswd = request.json['SMBPASSWD']
quota = request.josn['QUOTA'] quota = request.json['QUOTA']
except: except:
print "Invalid syntax"
abort(400, 'Invalid syntax') abort(400, 'Invalid syntax')
# Call user creator script # Call user creator script
result = subprocess.call([ROOT_BIN_FOLDER+'/'+USER_MANAGER, 'add', neptun, smbpasswd, quota, hard_quota(quota)]) print smbpasswd+" "+str(quota)
result = subprocess.call([ROOT_BIN_FOLDER+'/'+USER_MANAGER, 'add', neptun, smbpasswd, str(quota), hard_quota(quota)])
if result == 0: if result == 0:
try: try:
for key in request.json['KEYS']: for key in request.json['KEYS']:
key_list.append(key) key_list.append(key)
updateSSHAuthorizedKeys(neptun, key_list) updateSSHAuthorizedKeys(neptun, key_list)
except: except:
print "SSH error"
abort(400, 'SSH') abort(400, 'SSH')
return return
elif result == 2: elif result == 2:
abort(403, 'User already exist!') abort(403, 'User already exist!')
else: else:
print "Error"
abort(400, 'An error occured!') abort(400, 'An error occured!')
...@@ -326,7 +331,7 @@ def upload(hash_num): ...@@ -326,7 +331,7 @@ def upload(hash_num):
# Return hard quota from quota # Return hard quota from quota
def hard_quota(quota): def hard_quota(quota):
return str(int(quota)*1.25) return str(int(quota*1.25))
# Define filebuffer for big uploads # Define filebuffer for big uploads
def fbuffer(f, chunk_size=4096): def fbuffer(f, chunk_size=4096):
......
...@@ -82,6 +82,10 @@ case $COMMAND in ...@@ -82,6 +82,10 @@ case $COMMAND in
# echo $(quota -w ${USER_NAME} 2>/dev/null | tail -1 | awk '{ print $2" "$3" "$4 }') # echo $(quota -w ${USER_NAME} 2>/dev/null | tail -1 | awk '{ print $2" "$3" "$4 }')
echo "1916 2097152 2621440" echo "1916 2097152 2621440"
;; ;;
'setquota')
echo "$1 $2 $3 $4" > /tmp/asd
exit 0
;;
*) *)
echo "Usage: UserManager.sh COMMAND USER PASSWORD" echo "Usage: UserManager.sh COMMAND USER PASSWORD"
exit 1 exit 1
......
...@@ -13,7 +13,7 @@ from one.util import keygen ...@@ -13,7 +13,7 @@ from one.util import keygen
from school.models import Person, Group from school.models import Person, Group
from datetime import timedelta as td from datetime import timedelta as td
from django.db.models.signals import post_delete, pre_delete from django.db.models.signals import post_delete, pre_delete
from store.api import StoreApi
import subprocess, tempfile, os, stat, re, base64, struct import subprocess, tempfile, os, stat, re, base64, struct
...@@ -68,6 +68,23 @@ class UserCloudDetails(models.Model): ...@@ -68,6 +68,23 @@ class UserCloudDetails(models.Model):
def reset_smb(self): def reset_smb(self):
self.smb_password = pwgen() self.smb_password = pwgen()
def set_quota(sender, instance, created, **kwargs):
if not StoreApi.userexist(instance.user.username):
try:
password = instance.smb_password
quota = instance.disk_quota * 1024
key_list = []
for key in instance.user.sshkey_set.all():
key_list.append(key.key)
except:
pass
#Create user
if not StoreApi.createuser(instance.user.username, password, key_list, quota):
pass
else:
StoreApi.set_quota(instance.user.username, instance.disk_quota)
post_save.connect(set_quota, sender=UserCloudDetails)
def reset_keys(sender, instance, created, **kwargs): def reset_keys(sender, instance, created, **kwargs):
if created: if created:
instance.reset_smb() instance.reset_smb()
......
...@@ -162,7 +162,14 @@ $(function() { ...@@ -162,7 +162,14 @@ $(function() {
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
url: '/vm/' + state + '/' + id + '/', url: '/vm/' + state + '/' + id + '/',
success: function(data) {} success: function(data, b, c) {
if ( state == "resume" ){
window.location.href = '/vm/show/'+id+"/";
}
else {
window.location.reload();
}
}
}) })
} }
$('#new-member').click(function() { $('#new-member').click(function() {
......
...@@ -146,6 +146,15 @@ class StoreApi: ...@@ -146,6 +146,15 @@ class StoreApi:
else: else:
return False return False
@staticmethod @staticmethod
def set_quota(neptun, quota):
url = settings['store_url']+'/quota/'+neptun
payload = json.dumps({ 'QUOTA' : quota })
r = StoreApi.post_request(url, payload)
if r.status_code == requests.codes.ok:
return True
else:
return False
@staticmethod
def userexist(neptun): def userexist(neptun):
url = settings['store_url']+'/'+neptun url = settings['store_url']+'/'+neptun
r = StoreApi.get_request(url) r = StoreApi.get_request(url)
...@@ -154,9 +163,9 @@ class StoreApi: ...@@ -154,9 +163,9 @@ class StoreApi:
else: else:
return False return False
@staticmethod @staticmethod
def createuser(neptun, password, key_list): def createuser(neptun, password, key_list, quota):
url = settings['store_url']+'/new/'+neptun url = settings['store_url']+'/new/'+neptun
payload = json.dumps({ 'SMBPASSWD' : password, 'KEYS' : key_list }) payload = json.dumps({ 'SMBPASSWD' : password, 'KEYS' : key_list, 'QUOTA' : quota })
r = StoreApi.post_request(url, payload) r = StoreApi.post_request(url, payload)
if r.status_code == requests.codes.ok: if r.status_code == requests.codes.ok:
return True return True
......
...@@ -15,13 +15,14 @@ def estabilish_store_user(user): ...@@ -15,13 +15,14 @@ def estabilish_store_user(user):
try: try:
details = request.user.userclouddetails_set.all()[0] details = request.user.userclouddetails_set.all()[0]
password = details.smb_password password = details.smb_password
quota = details.disk_quota
key_list = [] key_list = []
for key in request.user.sshkey_set.all(): for key in request.user.sshkey_set.all():
key_list.append(key.key) key_list.append(key.key)
except: except:
return HttpResponse('Can not acces to django database!', status_code=404) return HttpResponse('Can not acces to django database!', status_code=404)
#Create user #Create user
if not StoreApi.createuser(user,password,key_list): if not StoreApi.createuser(user, password, key_list, str(quota)):
return HttpResponse('User does not exist on store! And could not create!') return HttpResponse('User does not exist on store! And could not create!')
@login_required @login_required
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment