-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Plugin system for compose #3905
Changes from all commits
adc5922
d0cdd23
3b6f51c
04f2af5
2fe1813
11eb861
d087a6e
2241bff
9d95ac0
a7abcd2
dc45335
c204ed8
9dc68bc
92b5a62
2a27baf
560b69e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.idea | ||
*.egg-info | ||
*.pyc | ||
.coverage* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from __future__ import absolute_import | ||
from __future__ import unicode_literals | ||
|
||
from inspect import cleandoc | ||
from inspect import getdoc | ||
|
||
from docopt import docopt | ||
|
@@ -21,7 +22,12 @@ def __init__(self, command_class, options): | |
self.options = options | ||
|
||
def parse(self, argv): | ||
command_help = getdoc(self.command_class) | ||
# Fix for http://bugs.python.org/issue12773 | ||
if hasattr(self.command_class, '__modified_doc__'): | ||
command_help = cleandoc(self.command_class.__modified_doc__) | ||
else: | ||
command_help = getdoc(self.command_class) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was http://bugs.python.org/issue12773 not fixed before python 3.3 released? https://github.com/python/cpython/blob/3.3/Misc/NEWS#L4652 suggests it was, and testing against python 2.7.12, 3.3.5 and 3.5 locally there doesn't appear to be an issue in mutating the |
||
options = docopt_full_help(command_help, argv, **self.options) | ||
command = options['COMMAND'] | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,11 @@ | |
from ..const import DEFAULT_TIMEOUT | ||
from ..const import IS_WINDOWS_PLATFORM | ||
from ..errors import StreamParseError | ||
from ..plugin import PluginError | ||
from ..plugin_manager import InvalidPluginError | ||
from ..plugin_manager import InvalidPluginFileTypeError | ||
from ..plugin_manager import PluginDoesNotExistError | ||
from ..plugin_manager import PluginManager | ||
from ..progress_stream import StreamOutputError | ||
from ..project import NoSuchService | ||
from ..project import OneOffFilter | ||
|
@@ -44,6 +49,7 @@ | |
from .formatter import Formatter | ||
from .log_printer import build_log_presenters | ||
from .log_printer import LogPrinter | ||
from .utils import get_plugin_dir | ||
from .utils import get_version_info | ||
from .utils import yesno | ||
|
||
|
@@ -56,7 +62,12 @@ | |
|
||
|
||
def main(): | ||
command = dispatch() | ||
try: | ||
command = dispatch() | ||
except PluginError as e: | ||
setup_logging() | ||
log.error("Plugin error: %s", str(e)) | ||
sys.exit(1) | ||
|
||
try: | ||
command() | ||
|
@@ -76,11 +87,15 @@ def main(): | |
except NeedsBuildError as e: | ||
log.error("Service '%s' needs to be built, but --no-build was passed." % e.service.name) | ||
sys.exit(1) | ||
except PluginError as e: | ||
log.error(e.msg) | ||
sys.exit(1) | ||
except (errors.ConnectionError, StreamParseError): | ||
sys.exit(1) | ||
|
||
|
||
def dispatch(): | ||
plugin_manager = PluginManager(get_plugin_dir()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be after setup_logging() in case you need to log something from the PluginManager class setup? |
||
setup_logging() | ||
dispatcher = DocoptDispatcher( | ||
TopLevelCommand, | ||
|
@@ -94,22 +109,35 @@ def dispatch(): | |
sys.exit(1) | ||
|
||
setup_console_handler(console_handler, options.get('--verbose')) | ||
return functools.partial(perform_command, options, handler, command_options) | ||
plugin_manager.load_config('.', options) | ||
return functools.partial(perform_command, options, handler, command_options, plugin_manager) | ||
|
||
|
||
def perform_command(options, handler, command_options): | ||
def perform_command(options, handler, command_options, plugin_manager): | ||
if options['COMMAND'] in ('help', 'version'): | ||
# Skip looking up the compose file. | ||
handler(command_options) | ||
return | ||
|
||
if options['COMMAND'] in ('config', 'bundle'): | ||
command = TopLevelCommand(None) | ||
command = TopLevelCommand(None, plugin_manager) | ||
handler(command, options, command_options) | ||
return | ||
|
||
none_project_commands = ['plugin'] | ||
|
||
for tlc_command in TopLevelCommand.__dict__: | ||
if hasattr(getattr(TopLevelCommand, tlc_command), '__standalone__') and \ | ||
getattr(TopLevelCommand, tlc_command).__standalone__ is True: | ||
none_project_commands.append(tlc_command) | ||
|
||
if options['COMMAND'] in none_project_commands: | ||
command = TopLevelCommand(None, plugin_manager) | ||
handler(command, command_options) | ||
return | ||
|
||
project = project_from_options('.', options) | ||
command = TopLevelCommand(project) | ||
command = TopLevelCommand(project, plugin_manager) | ||
with errors.handle_connection_errors(project.client): | ||
handler(command, command_options) | ||
|
||
|
@@ -179,6 +207,7 @@ class TopLevelCommand(object): | |
kill Kill containers | ||
logs View output from containers | ||
pause Pause services | ||
plugin Manages the plugins | ||
port Print the public port for a port binding | ||
ps List containers | ||
pull Pull service images | ||
|
@@ -194,9 +223,10 @@ class TopLevelCommand(object): | |
version Show the Docker-Compose version information | ||
""" | ||
|
||
def __init__(self, project, project_dir='.'): | ||
def __init__(self, project, plugin_manager, project_dir='.'): | ||
self.project = project | ||
self.project_dir = '.' | ||
self.plugin_manager = plugin_manager | ||
self.project_dir = project_dir | ||
|
||
def build(self, options): | ||
""" | ||
|
@@ -516,6 +546,73 @@ def pause(self, options): | |
containers = self.project.pause(service_names=options['SERVICE']) | ||
exit_if(not containers, 'No containers to pause', 1) | ||
|
||
def plugin(self, options): | ||
""" | ||
Manages docker-compose plugins | ||
|
||
Usage: plugin [list|install|update|config|uninstall] [PLUGIN] | ||
""" | ||
|
||
if options['list']: | ||
plugins = self.plugin_manager.get_plugins() | ||
|
||
if len(plugins) <= 0: | ||
print('No plugins installed.') | ||
else: | ||
headers = [ | ||
'ID', | ||
'Name', | ||
'Description', | ||
'Version' | ||
] | ||
rows = [] | ||
|
||
for plugin_name, plugin in plugins.items(): | ||
rows.append([ | ||
plugin.id, | ||
plugin.name, | ||
plugin.description, | ||
plugin.version | ||
]) | ||
|
||
print(Formatter().table(headers, rows)) | ||
elif options['install']: | ||
plugin_command( | ||
self.plugin_manager, | ||
'install_plugin', | ||
options['PLUGIN'], | ||
"Plugin '{}' successfully installed.", | ||
"An error occurred during the installation of plugin '{}'." | ||
) | ||
elif options['uninstall']: | ||
if self.plugin_manager.is_plugin_installed(options['PLUGIN']): | ||
print("Going to remove plugin '{}'".format(options['PLUGIN'])) | ||
|
||
if options.get('--force') or yesno("Are you sure? [yN] ", default=False): | ||
self.plugin_manager.uninstall_plugin(options['PLUGIN']) | ||
else: | ||
log.error("Plugin '{}' isn't installed".format(options['PLUGIN'])) | ||
sys.exit(1) | ||
elif options['config']: | ||
plugin_command( | ||
self.plugin_manager, | ||
'configure_plugin', | ||
options['PLUGIN'], | ||
"Configuration of plugin '{}' successfully.", | ||
"An error occurred during the configuration of plugin '{}'." | ||
) | ||
elif options['update']: | ||
plugin_command( | ||
self.plugin_manager, | ||
'update_plugin', | ||
options['PLUGIN'], | ||
"Update of plugin '{}' successfully.", | ||
"An error occurred during the update of plugin '{}'." | ||
) | ||
else: | ||
subject = get_handler(self, 'plugin') | ||
print(getdoc(subject)) | ||
|
||
def port(self, options): | ||
""" | ||
Print the public port for a port binding. | ||
|
@@ -1044,3 +1141,19 @@ def exit_if(condition, message, exit_code): | |
if condition: | ||
log.error(message) | ||
raise SystemExit(exit_code) | ||
|
||
|
||
def plugin_command(plugin_manager, command, plugin_name, success_message, error_message): | ||
try: | ||
success = getattr(plugin_manager, command)(plugin_name) | ||
except (PluginDoesNotExistError, InvalidPluginError, InvalidPluginFileTypeError) as e: | ||
log.error(str(e)) | ||
sys.exit(1) | ||
|
||
if success is True: | ||
print(success_message.format(plugin_name)) | ||
elif success is False: | ||
log.error(error_message.format(plugin_name)) | ||
sys.exit(1) | ||
elif success is None: | ||
print("Nothing to do.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be in your global gitignore file. Or edit
$repo_dir/info/exclude