3 # Copyright (C) 2014 Simo Sorce <simo@redhat.com>
5 # see file 'COPYING' for use and warranty information
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from ipsilon.login.common import LoginMgrsInstall
21 from ipsilon.providers.common import ProvidersInstall
30 TEMPLATES = '/usr/share/ipsilon/templates/install'
31 CONFDIR = '/etc/ipsilon'
32 HTTPDCONFD = '/etc/httpd/conf.d'
35 class ConfigurationError(Exception):
37 def __init__(self, message):
38 super(ConfigurationError, self).__init__(message)
39 self.message = message
42 return repr(self.message)
45 LOGFILE = '/var/log/ipsilon-install.log'
46 logger = logging.getLogger()
50 global logger # pylint: disable=W0603
51 if os.path.isfile(LOGFILE):
53 created = '%s' % time.ctime(os.path.getctime(LOGFILE))
54 shutil.move(LOGFILE, '%s.%s' % (LOGFILE, created))
57 logger = logging.getLogger()
59 lh = logging.FileHandler(LOGFILE)
61 print >> sys.stderr, 'Unable to open %s (%s)' % (LOGFILE, str(e))
62 lh = logging.StreamHandler(sys.stderr)
63 formatter = logging.Formatter('[%(asctime)s] %(message)s')
64 lh.setFormatter(formatter)
68 def install(plugins, args):
69 logger.info('Installation initiated')
70 now = time.strftime("%Y%m%d%H%M%S", time.gmtime())
72 logger.info('Installing default config files')
73 ipsilon_conf = os.path.join(CONFDIR, 'ipsilon.conf')
74 idp_conf = os.path.join(CONFDIR, 'idp.conf')
75 httpd_conf = os.path.join(HTTPDCONFD, 'idp.conf')
76 if os.path.exists(ipsilon_conf):
77 shutil.move(ipsilon_conf, '%s.bakcup.%s' % (ipsilon_conf, now))
78 if os.path.exists(idp_conf):
79 shutil.move(idp_conf, '%s.backup.%s' % (idp_conf, now))
80 shutil.copy(os.path.join(TEMPLATES, 'ipsilon.conf'), CONFDIR)
81 shutil.copy(os.path.join(TEMPLATES, 'idp.conf'), CONFDIR)
82 if not os.path.exists(httpd_conf):
83 os.symlink(idp_conf, httpd_conf)
84 # Load the cherrypy config from the newly installed file so
85 # that db paths and all is properly set before configuring
87 cherrypy.config.update(ipsilon_conf)
89 # Move pre-existing admin db away
90 admin_db = cherrypy.config['admin.config.db']
91 if os.path.exists(admin_db):
92 shutil.move(admin_db, '%s.backup.%s' % (admin_db, now))
94 logger.info('Configuring login managers')
95 for plugin_name in args['lm_order']:
96 plugin = plugins['Login Managers'][plugin_name]
97 plugin.configure(args)
99 logger.info('Configuring Authentication Providers')
100 for plugin_name in plugins['Auth Providers']:
101 plugin = plugins['Auth Providers'][plugin_name]
102 plugin.configure(args)
105 def uninstall(plugins, args):
106 logger.info('Uninstallation initiated')
107 raise Exception('Not Implemented')
112 'Login Managers': LoginMgrsInstall().plugins,
113 'Auth Providers': ProvidersInstall().plugins
118 def parse_args(plugins):
119 parser = argparse.ArgumentParser(description='Ipsilon Install Options')
120 parser.add_argument('--version',
121 action='version', version='%(prog)s 0.1')
122 parser.add_argument('-o', '--login-managers-order', dest='lm_order',
123 help='Comma separated list of login managers')
124 parser.add_argument('--ipa', choices=['yes', 'no'], default='yes',
125 help='Detect and use an IPA server for authentication')
126 parser.add_argument('--uninstall', action='store_true',
127 help="Uninstall the server and all data")
131 for plugin_group in plugins:
132 group = parser.add_argument_group(plugin_group)
133 for plugin_name in plugins[plugin_group]:
134 plugin = plugins[plugin_group][plugin_name]
135 if plugin.ptype == 'login':
136 lms.append(plugin.name)
137 plugin.install_args(group)
139 args = vars(parser.parse_args())
141 if args['lm_order'] is None:
142 args['lm_order'] = []
144 if args[name] == 'yes':
145 args['lm_order'].append(name)
147 args['lm_order'] = args['lm_order'].split(',')
149 if len(args['lm_order']) == 0:
150 #force the basic pam provider if nothing else is selected
151 if 'pam' not in args:
154 args['lm_order'] = ['pam']
159 if __name__ == '__main__':
164 fplugins = find_plugins()
165 opts = parse_args(fplugins)
167 logger.setLevel(logging.DEBUG)
169 logger.info('Intallation arguments:')
170 for k in sorted(opts.iterkeys()):
171 logger.info('%s: %s', k, opts[k])
173 if 'uninstall' in opts and opts['uninstall'] is True:
174 uninstall(fplugins, opts)
176 install(fplugins, opts)
177 except Exception, e: # pylint: disable=broad-except
179 if 'uninstall' in opts and opts['uninstall'] is True:
180 print 'Uninstallation aborted.'
182 print 'Installation aborted.'
183 print 'See log file %s for details' % LOGFILE
187 if 'uninstall' in opts and opts['uninstall'] is True:
188 print 'Uninstallation complete.'
190 print 'Installation complete.'