Forked from
hpc-team / HPCasCode
1936 commits behind the upstream repository.
-
Jupiter Hu authoredJupiter Hu authored
kg_init.j2 3.01 KiB
#!/usr/bin/python
import os, sys, time
os.environ['DJANGO_SETTINGS_MODULE'] = "karaage.conf.settings"
from django.conf import settings
from karaage.projects.models import Project
from karaage.institutes.models import Institute
from karaage.machines.models import MachineCategory
from karaage.people.models import Person, Group
CONSOLE_DEBUG = False
class HpcIdInit():
import django
django.setup()
def __init__(self, configfile, password, debug = True):
import json
self.path = configfile
self.password = password
self.debug = debug
self.logfile = None
if not debug:
self.logfile = open("/tmp/kg_init.log", "w")
if self.path and os.path.exists(self.path):
with open(self.path) as data:
config_data = json.load(data)
self.user = config_data["superuser"]
else:
log("Invalid input data")
def __del__(self):
if self.logfile:
if not self.logfile.closed():
self.logfile.close()
def log(self, message):
if self.debug:
print message
else:
now = time.strftime("%c")
self.logfile.write(now + ": " + message + "\n")
def getUser(self, username):
person = None
try:
person = Person.objects.get(username__exact=username)
except Person.DoesNotExist:
log("Person %s not found" %(username))
finally:
return person
def createSuperUser(self, user):
person = None
try:
institute = Institute.objects.get(name = user["institute_name"])
if institute:
person = Person.objects.create(username = user["username"], email = user["email"], password = self.password, short_name = user["short_name"], full_name = user["full_name"], is_admin = True, is_active = True, institute = institute)
if person:
person.set_password(self.password)
person.save()
person.full_clean()
except:
log("Create super user exception: %s" %(traceback.format_exc()))
finally:
return person
def setup(self):
su = self.getUser(self.user["username"])
if su:
self.log("Find super user %s" %(su.username))
else:
su = self.createSuperUser(self.user)
if su:
self.log("Create super user %s OK" %(su.username))
else:
self.log("Create super user %s failed" %(self.user["username"]))
def main(argv):
config_path = None
if len(sys.argv) > 2:
config_path = argv[0]
password = argv[1]
debug = CONSOLE_DEBUG
if len(sys.argv) > 3:
debug = argv[2]
init = HpcIdInit(config_path, password, debug)
init.setup()
else:
print "Usage: kg_init <config file> <superuser password> <option: debug True | False>"
if __name__ == '__main__':
main(sys.argv[1:])