2013-06-10 07:43:24 -05:00
|
|
|
# Authors: Tomas Babej <tbabej@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2013 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
|
2015-08-12 06:44:11 -05:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2013-06-10 07:43:24 -05:00
|
|
|
import os
|
2015-08-12 06:44:11 -05:00
|
|
|
from textwrap import wrap
|
|
|
|
|
2013-06-10 07:43:24 -05:00
|
|
|
from ipalib import api
|
2015-06-24 10:14:54 -05:00
|
|
|
from ipalib.plugable import Plugin, API
|
2013-06-10 07:43:24 -05:00
|
|
|
from ipalib.errors import ValidationError
|
2016-12-02 02:10:41 -06:00
|
|
|
from ipaplatform.paths import paths
|
2013-06-10 07:43:24 -05:00
|
|
|
from ipapython import admintool
|
2014-02-05 02:24:22 -06:00
|
|
|
from ipapython.ipa_log_manager import log_mgr
|
2013-06-10 07:43:24 -05:00
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
To add configuration instructions for a new use case, define a new class that
|
|
|
|
inherits from Advice class.
|
|
|
|
|
|
|
|
You should create a plugin file for it in ipaserver/advise/plugins folder.
|
|
|
|
|
|
|
|
The class can run any arbitrary code or IPA command via api.Command['command']()
|
|
|
|
calls. It needs to override get_info() method, which returns the formatted
|
|
|
|
advice string.
|
|
|
|
|
2016-04-25 09:14:05 -05:00
|
|
|
Important! Do not forget to register the class to the API.
|
|
|
|
|
|
|
|
>>> @register()
|
2013-06-10 07:43:24 -05:00
|
|
|
>>> class sample_advice(Advice):
|
|
|
|
>>> description = 'Instructions for machine with SSSD 1.0 setup.'
|
|
|
|
|
|
|
|
Description provided shows itself as a header and in the list of all advices
|
|
|
|
currently available via ipa-advise.
|
|
|
|
|
|
|
|
Optionally, you can require root privileges for your plugin:
|
|
|
|
|
|
|
|
>>> require_root = True
|
|
|
|
|
|
|
|
The following method should be implemented in your plugin:
|
|
|
|
|
2013-08-06 05:27:53 -05:00
|
|
|
>>> def get_info():
|
2013-06-10 07:43:24 -05:00
|
|
|
>>> self.log.debug('Entering execute() method')
|
|
|
|
>>> self.log.comment('Providing useful advice just for you')
|
|
|
|
>>> self.log.command('yum update sssd -y')
|
|
|
|
|
|
|
|
As you can see, Advice's log has 3 different levels. Debug lines are printed
|
|
|
|
out with '# DEBUG:' prefix if --verbose had been used. Comment lines utilize
|
|
|
|
'# ' prefix and command lines are printed raw.
|
|
|
|
|
2013-08-06 05:27:53 -05:00
|
|
|
Please note that comments are automatically wrapped after 70 characters.
|
|
|
|
Use wrapped=False option to force the unwrapped line in the comment.
|
|
|
|
|
|
|
|
>>> self.log.comment("This line should not be wrapped", wrapped=False)
|
|
|
|
|
2013-06-10 07:43:24 -05:00
|
|
|
As a result, you can redirect the advice's output directly to a script file.
|
|
|
|
|
|
|
|
# ipa-advise sample-advice > script.sh
|
|
|
|
# ./script.sh
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2015-02-16 10:34:01 -06:00
|
|
|
class _AdviceOutput(object):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.content = []
|
|
|
|
self.prefix = '# '
|
|
|
|
self.options = None
|
|
|
|
|
|
|
|
def comment(self, line, wrapped=True):
|
|
|
|
if wrapped:
|
|
|
|
for wrapped_line in wrap(line, 70):
|
|
|
|
self.content.append(self.prefix + wrapped_line)
|
|
|
|
else:
|
|
|
|
self.content.append(self.prefix + line)
|
|
|
|
|
|
|
|
def debug(self, line):
|
|
|
|
if self.options.verbose:
|
|
|
|
self.comment('DEBUG: ' + line)
|
|
|
|
|
|
|
|
def command(self, line):
|
|
|
|
self.content.append(line)
|
|
|
|
|
|
|
|
|
|
|
|
class Advice(Plugin):
|
|
|
|
"""
|
|
|
|
Base class for advices, plugins for ipa-advise.
|
|
|
|
"""
|
|
|
|
|
|
|
|
options = None
|
|
|
|
require_root = False
|
|
|
|
description = ''
|
|
|
|
|
2015-06-22 05:58:43 -05:00
|
|
|
def __init__(self, api):
|
|
|
|
super(Advice, self).__init__(api)
|
2015-02-16 10:34:01 -06:00
|
|
|
self.log = _AdviceOutput()
|
|
|
|
|
|
|
|
def set_options(self, options):
|
|
|
|
self.options = options
|
|
|
|
self.log.options = options
|
|
|
|
|
|
|
|
def get_info(self):
|
|
|
|
"""
|
2015-07-03 11:14:42 -05:00
|
|
|
This method should be overridden by child Advices.
|
2015-02-16 10:34:01 -06:00
|
|
|
|
|
|
|
Returns a string with instructions.
|
|
|
|
"""
|
|
|
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2015-06-15 08:36:26 -05:00
|
|
|
|
|
|
|
class AdviseAPI(API):
|
|
|
|
bases = (Advice,)
|
2016-03-16 05:22:00 -05:00
|
|
|
|
|
|
|
@property
|
|
|
|
def packages(self):
|
|
|
|
import ipaserver.advise.plugins
|
|
|
|
return (ipaserver.advise.plugins,)
|
2015-06-15 08:36:26 -05:00
|
|
|
|
|
|
|
advise_api = AdviseAPI()
|
2015-02-16 10:34:01 -06:00
|
|
|
|
|
|
|
|
2013-06-10 07:43:24 -05:00
|
|
|
class IpaAdvise(admintool.AdminTool):
|
|
|
|
"""
|
|
|
|
Admin tool that given systems's configuration provides instructions how to
|
|
|
|
configure the systems for various use cases.
|
|
|
|
"""
|
|
|
|
|
|
|
|
command_name = 'ipa-advise'
|
|
|
|
usage = "%prog ADVICE"
|
|
|
|
description = "Provides configuration advice for various use cases. To "\
|
|
|
|
"see the list of possible ADVICEs, run ipa-advise without "\
|
|
|
|
"any arguments."
|
|
|
|
|
|
|
|
def __init__(self, options, args):
|
|
|
|
super(IpaAdvise, self).__init__(options, args)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def add_options(cls, parser):
|
|
|
|
super(IpaAdvise, cls).add_options(parser)
|
|
|
|
|
|
|
|
def validate_options(self):
|
|
|
|
super(IpaAdvise, self).validate_options(needs_root=False)
|
|
|
|
|
|
|
|
if len(self.args) > 1:
|
|
|
|
raise self.option_parser.error("You can only provide one "
|
|
|
|
"positional argument.")
|
|
|
|
|
|
|
|
def log_success(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def print_config_list(self):
|
|
|
|
self.print_header('List of available advices')
|
|
|
|
|
2016-07-11 07:03:36 -05:00
|
|
|
max_keyword_len = max(
|
2016-07-18 03:44:23 -05:00
|
|
|
(len(advice.name) for advice in advise_api.Advice))
|
2013-06-10 07:43:24 -05:00
|
|
|
|
2016-07-11 07:03:36 -05:00
|
|
|
for advice in advise_api.Advice:
|
2013-06-10 07:43:24 -05:00
|
|
|
description = getattr(advice, 'description', '')
|
2016-07-18 03:44:23 -05:00
|
|
|
keyword = advice.name.replace('_', '-')
|
2013-06-10 07:43:24 -05:00
|
|
|
|
|
|
|
# Compute the number of spaces needed for the table to be aligned
|
|
|
|
offset = max_keyword_len - len(keyword)
|
2013-08-06 05:52:03 -05:00
|
|
|
prefix = " {key} {off}: ".format(key=keyword, off=' ' * offset)
|
|
|
|
wrapped_description = wrap(description, 80 - len(prefix))
|
|
|
|
|
|
|
|
# Print the first line with the prefix (keyword)
|
2015-08-12 06:44:11 -05:00
|
|
|
print(prefix + wrapped_description[0])
|
2013-08-06 05:52:03 -05:00
|
|
|
|
|
|
|
# Print the rest wrapped behind the colon
|
|
|
|
for line in wrapped_description[1:]:
|
2015-08-12 06:44:11 -05:00
|
|
|
print("{off}{line}".format(off=' ' * len(prefix), line=line))
|
2013-06-10 07:43:24 -05:00
|
|
|
|
|
|
|
def print_header(self, header, print_shell=False):
|
|
|
|
header_size = len(header)
|
|
|
|
|
|
|
|
prefix = ''
|
|
|
|
if print_shell:
|
|
|
|
prefix = '# '
|
2015-08-12 06:44:11 -05:00
|
|
|
print('#!/bin/sh')
|
2013-06-10 07:43:24 -05:00
|
|
|
|
|
|
|
# Do not print out empty header
|
|
|
|
if header_size > 0:
|
2015-08-12 06:44:11 -05:00
|
|
|
print((prefix + '-' * 70))
|
2013-08-06 05:52:03 -05:00
|
|
|
for line in wrap(header, 70):
|
2015-08-12 06:44:11 -05:00
|
|
|
print((prefix + line))
|
|
|
|
print((prefix + '-' * 70))
|
2013-06-10 07:43:24 -05:00
|
|
|
|
|
|
|
def print_advice(self, keyword):
|
2015-02-16 10:34:01 -06:00
|
|
|
advice = getattr(advise_api.Advice, keyword, None)
|
2013-06-10 07:43:24 -05:00
|
|
|
|
|
|
|
# Ensure that Configuration class for given --setup option value exists
|
|
|
|
if advice is None:
|
|
|
|
raise ValidationError(
|
|
|
|
name="advice",
|
|
|
|
error="No instructions are available for '{con}'. "
|
|
|
|
"See the list of available configuration "
|
|
|
|
"by invoking the ipa-advise command with no argument."
|
|
|
|
.format(con=keyword.replace('_', '-')))
|
|
|
|
|
|
|
|
# Check whether root privileges are needed
|
|
|
|
if advice.require_root and os.getegid() != 0:
|
|
|
|
raise admintool.ScriptError(
|
|
|
|
'Must be root to get advice for {adv}'
|
|
|
|
.format(adv=keyword.replace('_', '-')), 1)
|
|
|
|
|
|
|
|
# Print out nicely formatted header
|
|
|
|
self.print_header(advice.description, print_shell=True)
|
|
|
|
|
|
|
|
# Set options so that plugin can use verbose/quiet options
|
|
|
|
advice.set_options(self.options)
|
|
|
|
|
|
|
|
# Print out the actual advice
|
2012-12-19 03:25:24 -06:00
|
|
|
api.Backend.rpcclient.connect()
|
2013-06-10 07:43:24 -05:00
|
|
|
advice.get_info()
|
2012-12-19 03:25:24 -06:00
|
|
|
api.Backend.rpcclient.disconnect()
|
2013-06-10 07:43:24 -05:00
|
|
|
for line in advice.log.content:
|
2015-08-12 06:44:11 -05:00
|
|
|
print(line)
|
2013-06-10 07:43:24 -05:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
super(IpaAdvise, self).run()
|
|
|
|
|
2016-12-02 02:10:41 -06:00
|
|
|
api.bootstrap(in_server=False,
|
|
|
|
context='cli',
|
|
|
|
confdir=paths.ETC_IPA)
|
2013-06-10 07:43:24 -05:00
|
|
|
api.finalize()
|
2016-12-02 02:10:41 -06:00
|
|
|
advise_api.bootstrap(in_server=False,
|
|
|
|
context='cli',
|
|
|
|
confdir=paths.ETC_IPA)
|
2015-02-16 10:34:01 -06:00
|
|
|
advise_api.finalize()
|
2014-02-05 02:24:22 -06:00
|
|
|
if not self.options.verbose:
|
|
|
|
# Do not print connection information by default
|
|
|
|
logger_name = r'ipa\.ipalib\.plugins\.rpcclient'
|
|
|
|
log_mgr.configure(dict(logger_regexps=[(logger_name, 'warning')]))
|
2013-06-10 07:43:24 -05:00
|
|
|
|
|
|
|
# With no argument, print the list out and exit
|
|
|
|
if not self.args:
|
|
|
|
self.print_config_list()
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
keyword = self.args[0].replace('-', '_')
|
|
|
|
self.print_advice(keyword)
|