view create.py @ 29:991e5e7fc82a default tip

fix documents
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 14 Dec 2017 22:18:37 +0900
parents c64a640558ba
children
line wrap: on
line source

#!/usr/bin/python


import os, re
import argparse
import portops

base_path = "/home/k138582/docker-hg/"
CONTAINER_NUM_LIMIT = 8

def ie_mkdir(projectname):
    username = os.getlogin()
    m = re.match('^([ek]\d\d[58]\d\d\d)$', username)
    if m is not None:
        dir = base_path + "students/" + username[:3] + os.sep + username
        
        mkdir1(dir)
        os.system("/bin/chown "+os.getlogin()+" "+ dir)

        if (len(os.listdir(dir)) > CONTAINER_NUM_LIMIT):
            print("[!] Too many project.")
            exit()

        dir = dir + os.sep + projectname
        mkdir1(dir)
        os.system("/bin/chown " + os.getlogin() + " " + dir)
        return dir
    print("[!] Permission denied. You are not permitted user.")
    exit()
        
# make necessary sub directory
#   /etc/libvirt/qemu/teachers
#   /var/log/libvirt/qemu/teachers
#   /var/run/libvirt/qemu/teachers

def mkdir1(name):
    if not os.path.exists(name):
        os.makedirs(name);

def change_own(base_path):
    if not os.path.isdir(base_path):
        os.system("/bin/chown " + os.getlogin() + " " + base_path)
        return

    for f in os.listdir(base_path):
        path = os.path.join(base_path, f)
        os.system("/bin/chown " + os.getlogin() + " " + path)
        if os.path.isdir(path):
            change_own(path)


def make_hgrepo(projpath):
    os.chdir(projpath)
    os.system("hg init")

    dockerfile = "Dockerfile" 
    fd = open(dockerfile, "w")
    fd.write("# Using ie-docker, you sould edit this file\n")
    fd.write("FROM fedora\n")
    fd.close()

    os.system("hg add " + dockerfile)
    os.system("hg commit -u " + os.getlogin() + " -m systemcommit")

    change_own(os.getcwd())

#
# main method
#

if __name__ == "__main__":

    parser = argparse.ArgumentParser(description='Create new project for ie-docker.')
    parser.add_argument('projectname', metavar='project name', help='ie-docker project name')
    args = parser.parse_args()

    if not portops.reserve_port(os.getlogin(), args.projectname):
        print("[!] Can't get port to use for project.")
        print("[!] Check your number of projects.")
        sys.exit()

    projpath = ie_mkdir(args.projectname)
    
    make_hgrepo(projpath)

    print("Create your project on " + projpath)
    print("Exec on your client : hg clone ssh://your_account@" + os.uname()[1] + os.sep + projpath)


# end