2 # -*- coding: utf-8 -*-
7 Copyright (c) 2013 Frédéric Massart - FMCorz.net
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 http://github.com/FMCorz/mdk
29 from ..command
import Command
30 from ..tools
import yesOrNo
31 from ..exceptions
import CreateException
, InstallException
34 class CreateCommand(Command
):
36 _description
= 'Creates new instances of Moodle'
38 def __init__(self
, *args
, **kwargs
):
39 super(CreateCommand
, self
).__init__(*args
, **kwargs
)
44 'action': 'store_true',
46 'help': 'launch the installation script after creating the instance'
53 'choices': ['mariadb', 'mysqli', 'pgsql'],
54 'default': self
.C
.get('defaultEngine'),
55 'help': 'database engine to install the instance on, use with --install',
63 'choices': ['integration', 'review', 'stable'],
65 'help': 'specify what this instance is for',
73 'help': 'scripts to run after installation',
79 ['-n', '--identifier'],
83 'help': 'use this identifier instead of generating one. The flag --suffix will be used. ' +
84 'Do not use when creating multiple versions at once',
93 'help': 'suffixes for the instance name',
101 'choices': [str(x
) for x
in range(13, int(self
.C
.get('masterBranch')))] + ['master'],
102 'default': ['master'],
103 'help': 'version of Moodle',
104 'metavar': 'version',
113 versions
= args
.version
114 suffixes
= args
.suffix
115 install
= args
.install
117 # Throw an error when --engine is used without --install. The code is currently commented out
118 # because as --engine has a default value, it will always be set, and so it becomes impossible
119 # to create an instance without installing it. I cannot think about a clean fix yet. Removing
120 # the default value will cause --help not to output the default as it should... Let's put more
121 # thoughts into this and perhaps use argument groups.
122 # if engine and not install:
123 # self.argumentError('--engine can only be used with --install.')
125 for version
in versions
:
126 for suffix
in suffixes
:
131 'purpose': args
.purpose
,
132 'identifier': args
.identifier
,
139 logging
.info('Process complete!')
142 """Proceeds to the creation of an instance"""
144 # TODO Remove these ugly lines, but I'm lazy to rewrite the variables in this method...
146 __init__
= lambda self
, **kw
: setattr(self
, '__dict__', kw
)
150 version
= args
.version
151 name
= self
.Wp
.generateInstanceName(version
, engine
=engine
, purpose
=args
.purpose
, suffix
=args
.suffix
, identifier
=args
.identifier
)
154 versionNice
= version
155 if version
== 'master':
156 versionNice
= self
.C
.get('wording.master')
159 if args
.purpose
== 'integration':
160 fullname
= self
.C
.get('wording.integration') + ' ' + versionNice
+ ' ' + self
.C
.get('wording.%s' % engine
)
162 if args
.purpose
== 'stable':
163 fullname
= self
.C
.get('wording.stable') + ' ' + versionNice
+ ' ' + self
.C
.get('wording.%s' % engine
)
165 if args
.purpose
== 'review':
166 fullname
= self
.C
.get('wording.review') + ' ' + versionNice
+ ' ' + self
.C
.get('wording.%s' % engine
)
170 fullname
+= ' ' + args
.suffix
.replace('-', ' ').replace('_', ' ').title()
172 # Create the instance
173 logging
.info('Creating instance %s...' % name
)
177 'purpose': args
.purpose
,
181 M
= self
.Wp
.create(**kwargs
)
182 except CreateException
as e
:
183 logging
.error('Error creating %s:\n %s' %
(name
, e
))
185 except Exception as e
:
186 logging
.exception('Error creating %s:\n %s' %
(name
, e
))
189 # Run the install script
193 dbname
= re
.sub(r
'[^a-zA-Z0-9]', '', name
).lower()
194 prefixDbname
= self
.C
.get('db.namePrefix')
196 dbname
= prefixDbname
+ dbname
198 db
= DB(engine
, self
.C
.get('db.%s' % engine
))
200 if db
.dbexists(dbname
):
201 logging
.info('Database already exists (%s)' % dbname
)
202 dropDb
= yesOrNo('Do you want to remove it?')
209 'fullname': fullname
,
210 'dataDir': self
.Wp
.getPath(name
, 'data'),
211 'wwwroot': self
.Wp
.getUrl(name
)
215 except InstallException
as e
:
216 logging
.warning('Error while installing %s:\n %s' %
(name
, e
))
218 except Exception as e
:
219 logging
.exception('Error while installing %s:\n %s' %
(name
, e
))
223 if M
.isInstalled() and type(args
.run
) == list:
224 for script
in args
.run
:
225 logging
.info('Running script \'%s\'' %
(script
))
228 except Exception as e
:
229 logging
.warning('Error while running the script \'%s\':\ %s' %
(script
, e
))