main.py 3.65 KB
Newer Older
root's avatar
root committed
1 2 3 4 5 6 7
#!/usr/bin/env python3
from configparser import ConfigParser
import socket

from re2oapi import Re2oAPIClient

import os
Gabriel Detraz's avatar
Gabriel Detraz committed
8
import shutil
root's avatar
root committed
9
import grp
grisel-davy's avatar
grisel-davy committed
10 11
import sys

Benjamin Graillot's avatar
Benjamin Graillot committed
12
path = (os.path.dirname(os.path.abspath(__file__)))
root's avatar
root committed
13 14

config = ConfigParser()
Benjamin Graillot's avatar
Benjamin Graillot committed
15
config.read(path+'/config.ini')
root's avatar
root committed
16 17 18 19 20

api_hostname = config.get('Re2o', 'hostname')
api_password = config.get('Re2o', 'password')
api_username = config.get('Re2o', 'username')

Charlie Jacomme's avatar
Charlie Jacomme committed
21
api_client = Re2oAPIClient(api_hostname,api_username,api_password, use_tls=False)
root's avatar
root committed
22 23 24 25 26 27 28 29

client_hostname = socket.gethostname().split('.',1)[0]

def reconfigure(api_client):

    users = api_client.list("users/homecreation")

    error = False
30
    os.chdir("/home-adh")
root's avatar
root committed
31
    for user in users:
32
        home = '/home-adh/{}/{}'.format(user['pseudo'][0].lower(),user['pseudo'])
root's avatar
root committed
33 34 35 36
        uid = user['uid']
        gid = user['gid']

        if not os.path.exists(home):  # Home dosen't exist, create it
Gabriel Detraz's avatar
Gabriel Detraz committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
            users_dirs = []
            for dirname in os.listdir("/home-adh"):
                if os.stat("/home-adh/" + dirname).st_uid == uid and os.path.islink("/home-adh/" + dirname):
                    users_dirs.append(dirname)

            if len(users_dirs) == 1:
            # This user had already a home directory before, renaming it
                # Renaming home directory
                shutil.move("/home-adh/" + users_dirs[0][0].lower() + '/' + users_dirs[0], home)
                os.chown(home,int(uid),int(gid))
                os.chown(home + '/Mail', int(uid), int(gid))
                # Deleting the symlink, will be recreated later
                os.remove("/home-adh/" + users_dirs[0])
                # Renaming the maildir
                os.rename("/home-adh/mail/" + users_dirs[0], '/home-adh/mail/' + user['pseudo']) 
                os.chown('/home-adh/mail/' + user['pseudo'], int(uid), 8)

            elif len(users_dirs) == 0:
                os.makedirs(home,0o701)
                os.chown(home,int(uid),int(gid))
root's avatar
root committed
57 58
        
        # Mail
Charlie Jacomme's avatar
Charlie Jacomme committed
59
        if not(os.path.exists(home + '/Mail')) and not(os.path.islink(home + '/Mail')):
root's avatar
root committed
60
            os.makedirs(home + '/Mail', 0o700)
Charlie Jacomme's avatar
Charlie Jacomme committed
61 62
            os.chown(home + '/Mail', int(uid), int(gid))
        if not(os.path.exists('/home-adh/mail/' + user['pseudo'])) and not(os.path.islink(home + '/Mail')):
root's avatar
root committed
63
            os.makedirs('/home-adh/mail/' + user['pseudo'], 0o700)
Charlie Jacomme's avatar
Charlie Jacomme committed
64
            os.chown('/home-adh/mail/' + user['pseudo'], int(uid), 8)
root's avatar
root committed
65 66

        # Owncloud dans le home
Charlie Jacomme's avatar
Charlie Jacomme committed
67
        if not(os.path.exists(home + '/OwnCloud')) and not(os.path.islink(home + '/Owncloud')):
root's avatar
root committed
68
            os.makedirs(home + '/OwnCloud')
Charlie Jacomme's avatar
Charlie Jacomme committed
69 70
            os.chown(home + '/OwnCloud',int(uid),grp.getgrnam('www-data').gr_gid)
            os.chmod(home + '/OwnCloud', 0o770)
root's avatar
root committed
71 72

        # Simlink
73
        link = '/home-adh/{}'.format(user['pseudo'])
root's avatar
root committed
74
        if not os.path.islink(link):
75 76
            link2 = '{}'.format(user['pseudo'])
            home2 = '{}/{}'.format(user['pseudo'][0].lower(),user['pseudo'])
77
            os.symlink(home2, link2)
root's avatar
root committed
78

Charlie Jacomme's avatar
Charlie Jacomme committed
79
        if not ((os.path.exists(home+'/Mail') or os.path.islink(home + '/Mail')) and (os.path.exists(home+'/OwnCloud') or os.path.islink(home + '/OwnCloud')) and os.path.islink(link)):
Charlie Jacomme's avatar
Charlie Jacomme committed
80 81
            print('creation du home de {}'.format(user['pseudo']))
            print("error: {}".format(error))
root's avatar
root committed
82 83
            error = True

Charlie Jacomme's avatar
Charlie Jacomme committed
84
    return error
root's avatar
root committed
85

Charlie Jacomme's avatar
Charlie Jacomme committed
86 87 88
for arg in sys.argv:
    if arg=="--force":
        reconfigure(api_client)
grisel-davy's avatar
grisel-davy committed
89 90 91

for service in api_client.list("services/regen/"):
    if service['hostname'] == client_hostname and \
Charlie Jacomme's avatar
Charlie Jacomme committed
92
        service['service_name'] == 'home' and \
grisel-davy's avatar
grisel-davy committed
93
        service['need_regen']:
Charlie Jacomme's avatar
Charlie Jacomme committed
94 95
        error = reconfigure(api_client)
        api_client.patch(service['api_url'], data={'need_regen': error})  # Regen is there is an error