forked from FMCorz/mdk
/
moodle-init.py
executable file
·184 lines (158 loc) · 6.42 KB
/
moodle-init.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Moodle Development Kit
Copyright (c) 2012 Frédéric Massart - FMCorz.net
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
http://github.com/FMCorz/mdk
"""
import sys
import os
import argparse
import shutil
import grp
import re
import pwd
import subprocess
from lib.tools import debug, question, get_current_user
def resolve_directory(path, user):
if path.startswith('~'):
path = re.sub(r'^~', '~%s' % user, path)
path = os.path.abspath(os.path.realpath(os.path.expanduser(path)))
return path
# Arguments
parser = argparse.ArgumentParser(description='Initialise MDK for the current user.')
parser.add_argument('-f', '--force', action='store_true', help='Force the initialisation')
args = parser.parse_args()
# Check root.
if os.getuid() != 0:
debug('You must execute this as root.')
debug(' sudo mdk init')
sys.exit(1)
# Check what user we want to initialise for.
while True:
username = question('What user are you initialising MDK for?', get_current_user())
try:
user = pwd.getpwnam(username)
except:
debug('Error while getting information for user %s' % (username))
continue
try:
usergroup = grp.getgrgid(user.pw_gid)
except:
debug('Error while getting the group of user %s' % (username))
continue
break
# Default directories.
userdir = resolve_directory('~/.moodle-sdk', username)
scriptdir = os.path.dirname(os.path.realpath(__file__))
# Create the main MDK folder.
if not os.path.isdir(userdir):
debug('Creating directory %s.' % userdir)
os.mkdir(userdir, 0755)
os.chown(userdir, user.pw_uid, usergroup.gr_gid)
# Checking if the config file exists.
userconfigfile = os.path.join(userdir, 'config.json')
if os.path.isfile(userconfigfile):
debug('Config file %s already in place.' % userconfigfile)
if not args.force:
debug('Aborting. Use --force to continue.')
sys.exit(1)
elif not os.path.isfile(userconfigfile):
debug('Creating user config file in %s.' % userconfigfile)
open(userconfigfile, 'w')
os.chown(userconfigfile, user.pw_uid, usergroup.gr_gid)
# If the group moodle-sdk exists, then we want to add the user to it.
try:
group = grp.getgrnam('moodle-sdk')
if not username in group.gr_mem:
debug('Adding user %s to group %s.' % (username, group.gr_name))
# This command does not work for some reason...
# os.initgroups(username, group.gr_gid)
chgrp = subprocess.Popen(['usermod', '-a', '-G', 'moodle-sdk', username])
chgrp.wait()
except KeyError:
# Raised when the group has not been found.
group = None
pass
# Loading the configuration.
from lib.config import Conf as Config
C = Config(userfile=userconfigfile)
# Asks the user what needs to be asked.
while True:
www = question('What is the DocumentRoot of your virtual host?', C.get('dirs.www'))
www = resolve_directory(www, username)
try:
if not os.path.isdir(www):
os.mkdir(www, 0775)
os.chown(www, user.pw_uid, usergroup.gr_gid)
except:
debug('Error while creating directory %s' % www)
continue
else:
C.set('dirs.www', www)
break
while True:
storage = question('Where do you want to store your Moodle instances?', C.get('dirs.storage'))
storage = resolve_directory(storage, username)
try:
if not os.path.isdir(storage):
if storage != www:
os.mkdir(storage, 0775)
os.chown(storage, user.pw_uid, usergroup.gr_gid)
else:
debug('Error! dirs.www and dirs.storage must be different!')
continue
except:
debug('Error while creating directory %s' % storage)
continue
else:
C.set('dirs.storage', storage)
break
# The default configuration file should point to the right directory for dirs.mdk,
# we will just ensure that it exists.
mdkdir = C.get('dirs.mdk')
mdkdir = resolve_directory(mdkdir, username)
if not os.path.isdir(mdkdir):
try:
debug('Creating MDK directory %s' % mdkdir)
os.mkdir(mdkdir, 0775)
os.chown(mdkdir, user.pw_uid, usergroup.gr_gid)
except:
debug('Error while creating %s, please fix manually.' % mdkdir)
# Git repository.
github = question('What is your Github username? (Leave blank if not using Github)')
if github != None:
C.set('remotes.mine', C.get('remotes.mine').replace('YourGitHub', github))
C.set('repositoryUrl', C.get('repositoryUrl').replace('YourGitHub', github))
C.set('diffUrlTemplate', C.get('diffUrlTemplate').replace('YourGitHub', github))
C.set('myRemote', 'github')
C.set('upstreamRemote', 'origin')
else:
C.set('remotes.mine', question('What is your remote?', C.get('remotes.mine')))
C.set('myRemote', question('What to call your remote?', C.get('myRemote')))
C.set('upstreamRemote', question('What to call the upsream remote (official Moodle remote)?', C.get('upstreamRemote')))
# Database settings.
C.set('db.mysqli.user', question('What is your MySQL user?', C.get('db.mysqli.user')))
C.set('db.mysqli.passwd', question('What is your MySQL password?', C.get('db.mysqli.passwd'), password=True))
C.set('db.pgsql.user', question('What is your PostgreSQL user?', C.get('db.pgsql.user')))
C.set('db.pgsql.passwd', question('What is your PostgreSQL password?', C.get('db.pgsql.passwd'), password=True))
debug('')
debug('MDK has been initialised with minimal configuration.')
debug('For more settings, edit your config file: %s.' % userconfigfile)
debug('Use %s as documentation.' % os.path.join(scriptdir, 'config-dist.json'))
debug('')
debug('Type the following command to create your first instance:')
debug(' mdk create')
debug('(This will take some time, but don\'t worry, that\'s because the cache is still empty)')
debug('')
debug('/!\ Please logout/login before to avoid permission issues: sudo su `whoami`')