diff --git a/contrib/fuel-utils/.gitignore b/contrib/fuel-utils/.gitignore new file mode 100644 index 000000000..a96057a11 --- /dev/null +++ b/contrib/fuel-utils/.gitignore @@ -0,0 +1,40 @@ +*.py[cod] + +# C extensions +*.so + +# Packages +*.egg +*.egg-info +dist +build +eggs +parts +bin +var +sdist +develop-eggs +.installed.cfg +lib +lib64 + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox +nosetests.xml + +# Translations +*.mo + +# Mr Developer +.mr.developer.cfg +.project +.pydevproject +.idea + +AUTHORS +ChangeLog +MANIFEST diff --git a/contrib/fuel-utils/LICENSE b/contrib/fuel-utils/LICENSE new file mode 100644 index 000000000..37ec93a14 --- /dev/null +++ b/contrib/fuel-utils/LICENSE @@ -0,0 +1,191 @@ +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, and +distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by the copyright +owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all other entities +that control, are controlled by, or are under common control with that entity. +For the purposes of this definition, "control" means (i) the power, direct or +indirect, to cause the direction or management of such entity, whether by +contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the +outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity exercising +permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, including +but not limited to software source code, documentation source, and configuration +files. + +"Object" form shall mean any form resulting from mechanical transformation or +translation of a Source form, including but not limited to compiled object code, +generated documentation, and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or Object form, made +available under the License, as indicated by a copyright notice that is included +in or attached to the work (an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object form, that +is based on (or derived from) the Work and for which the editorial revisions, +annotations, elaborations, or other modifications represent, as a whole, an +original work of authorship. For the purposes of this License, Derivative Works +shall not include works that remain separable from, or merely link (or bind by +name) to the interfaces of, the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including the original version +of the Work and any modifications or additions to that Work or Derivative Works +thereof, that is intentionally submitted to Licensor for inclusion in the Work +by the copyright owner or by an individual or Legal Entity authorized to submit +on behalf of the copyright owner. For the purposes of this definition, +"submitted" means any form of electronic, verbal, or written communication sent +to the Licensor or its representatives, including but not limited to +communication on electronic mailing lists, source code control systems, and +issue tracking systems that are managed by, or on behalf of, the Licensor for +the purpose of discussing and improving the Work, but excluding communication +that is conspicuously marked or otherwise designated in writing by the copyright +owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf +of whom a Contribution has been received by Licensor and subsequently +incorporated within the Work. + +2. Grant of Copyright License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable copyright license to reproduce, prepare Derivative Works of, +publicly display, publicly perform, sublicense, and distribute the Work and such +Derivative Works in Source or Object form. + +3. Grant of Patent License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable (except as stated in this section) patent license to make, have +made, use, offer to sell, sell, import, and otherwise transfer the Work, where +such license applies only to those patent claims licensable by such Contributor +that are necessarily infringed by their Contribution(s) alone or by combination +of their Contribution(s) with the Work to which such Contribution(s) was +submitted. If You institute patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Work or a +Contribution incorporated within the Work constitutes direct or contributory +patent infringement, then any patent licenses granted to You under this License +for that Work shall terminate as of the date such litigation is filed. + +4. Redistribution. + +You may reproduce and distribute copies of the Work or Derivative Works thereof +in any medium, with or without modifications, and in Source or Object form, +provided that You meet the following conditions: + +You must give any other recipients of the Work or Derivative Works a copy of +this License; and +You must cause any modified files to carry prominent notices stating that You +changed the files; and +You must retain, in the Source form of any Derivative Works that You distribute, +all copyright, patent, trademark, and attribution notices from the Source form +of the Work, excluding those notices that do not pertain to any part of the +Derivative Works; and +If the Work includes a "NOTICE" text file as part of its distribution, then any +Derivative Works that You distribute must include a readable copy of the +attribution notices contained within such NOTICE file, excluding those notices +that do not pertain to any part of the Derivative Works, in at least one of the +following places: within a NOTICE text file distributed as part of the +Derivative Works; within the Source form or documentation, if provided along +with the Derivative Works; or, within a display generated by the Derivative +Works, if and wherever such third-party notices normally appear. The contents of +the NOTICE file are for informational purposes only and do not modify the +License. You may add Your own attribution notices within Derivative Works that +You distribute, alongside or as an addendum to the NOTICE text from the Work, +provided that such additional attribution notices cannot be construed as +modifying the License. +You may add Your own copyright statement to Your modifications and may provide +additional or different license terms and conditions for use, reproduction, or +distribution of Your modifications, or for any such Derivative Works as a whole, +provided Your use, reproduction, and distribution of the Work otherwise complies +with the conditions stated in this License. + +5. Submission of Contributions. + +Unless You explicitly state otherwise, any Contribution intentionally submitted +for inclusion in the Work by You to the Licensor shall be under the terms and +conditions of this License, without any additional terms or conditions. +Notwithstanding the above, nothing herein shall supersede or modify the terms of +any separate license agreement you may have executed with Licensor regarding +such Contributions. + +6. Trademarks. + +This License does not grant permission to use the trade names, trademarks, +service marks, or product names of the Licensor, except as required for +reasonable and customary use in describing the origin of the Work and +reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. + +Unless required by applicable law or agreed to in writing, Licensor provides the +Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, +including, without limitation, any warranties or conditions of TITLE, +NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are +solely responsible for determining the appropriateness of using or +redistributing the Work and assume any risks associated with Your exercise of +permissions under this License. + +8. Limitation of Liability. + +In no event and under no legal theory, whether in tort (including negligence), +contract, or otherwise, unless required by applicable law (such as deliberate +and grossly negligent acts) or agreed to in writing, shall any Contributor be +liable to You for damages, including any direct, indirect, special, incidental, +or consequential damages of any character arising as a result of this License or +out of the use or inability to use the Work (including but not limited to +damages for loss of goodwill, work stoppage, computer failure or malfunction, or +any and all other commercial damages or losses), even if such Contributor has +been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. + +While redistributing the Work or Derivative Works thereof, You may choose to +offer, and charge a fee for, acceptance of support, warranty, indemnity, or +other liability obligations and/or rights consistent with this License. However, +in accepting such obligations, You may act only on Your own behalf and on Your +sole responsibility, not on behalf of any other Contributor, and only if You +agree to indemnify, defend, and hold each Contributor harmless for any liability +incurred by, or claims asserted against, such Contributor by reason of your +accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work + +To apply the Apache License to your work, attach the following boilerplate +notice, with the fields enclosed by brackets "[]" replaced with your own +identifying information. (Don't include the brackets!) The text should be +enclosed in the appropriate comment syntax for the file format. We also +recommend that a file or class name and description of purpose be included on +the same "printed page" as the copyright notice for easier identification within +third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/contrib/fuel-utils/MANIFEST.in b/contrib/fuel-utils/MANIFEST.in new file mode 100644 index 000000000..9fa77e244 --- /dev/null +++ b/contrib/fuel-utils/MANIFEST.in @@ -0,0 +1,2 @@ +recursive-include fdb_cleaner *.html *.min.js *.css *.png *.jpg *.gif *.json *.yaml *.ini +recursive-exclude fdb_cleaner *.orig *.pyc *.pyo *.sqlite diff --git a/contrib/fuel-utils/README.md b/contrib/fuel-utils/README.md new file mode 100644 index 000000000..dd149dae1 --- /dev/null +++ b/contrib/fuel-utils/README.md @@ -0,0 +1,8 @@ +FUEL-utils +=========== + +Set of tool for maintance installed Openstack cluster + +* **fuel-fdb-cleaner** -- utility for clean fdb table on compute and router + nodes while active l3-agent migrate from one node to another. + diff --git a/contrib/fuel-utils/etc/fdb_cleaner.conf b/contrib/fuel-utils/etc/fdb_cleaner.conf new file mode 100644 index 000000000..41be1ca53 --- /dev/null +++ b/contrib/fuel-utils/etc/fdb_cleaner.conf @@ -0,0 +1,3 @@ +[DEFAULT] +API_VER=2.0 +LOG_NAME=neutron-fdb-cleaner diff --git a/contrib/fuel-utils/fuel_utils/__init__.py b/contrib/fuel-utils/fuel_utils/__init__.py new file mode 100644 index 000000000..71a2a0e3b --- /dev/null +++ b/contrib/fuel-utils/fuel_utils/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +import eventlet + +eventlet.monkey_patch() diff --git a/contrib/fuel-utils/fuel_utils/core/__init__.py b/contrib/fuel-utils/fuel_utils/core/__init__.py new file mode 100644 index 000000000..71a2a0e3b --- /dev/null +++ b/contrib/fuel-utils/fuel_utils/core/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +import eventlet + +eventlet.monkey_patch() diff --git a/contrib/fuel-utils/fuel_utils/core/daemonize_green.py b/contrib/fuel-utils/fuel_utils/core/daemonize_green.py new file mode 100644 index 000000000..0248d935b --- /dev/null +++ b/contrib/fuel-utils/fuel_utils/core/daemonize_green.py @@ -0,0 +1,234 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +import eventlet + +eventlet.monkey_patch() +import os +import sys +import errno +import signal +import logging + + +RunningGreenDaemons = set() + + +class StreamToLogger(object): + """ + Fake file-like stream object that redirects writes to a logger instance. + """ + + def __init__(self, logger, log_level=logging.INFO): + self.logger = logger + self.log_level = log_level + self.linebuf = '' + + def write(self, buf): + for line in buf.rstrip().splitlines(): + self.logger.log(self.log_level, line.rstrip()) + + +def sigterm_handler(signum, frame): + """ + Call actions will be done after SIGTERM. + """ + for daemon in RunningGreenDaemons: + daemon.sigterm() + sys.exit(0) + + +def sighup_handler(signum, frame): + """ + Call actions will be done after SIGHUP. + """ + for daemon in RunningGreenDaemons: + daemon.sighup() + + +class Daemonize(object): + """ Daemonize object + Object constructor expects three arguments: + - app: contains the application name which will be sent to logger. + - pid: path to the pidfile. + """ + + def __init__(self, pidfile, logger=None, green_pool_size=1024): + self.pidfile = pidfile + self.debug = getattr(self, 'debug', False) + # Initialize logging. + self.logger = logger or logging.getLogger(__package__) + if self.debug: + self.loglevel = logging.DEBUG + elif hasattr(self, 'loglevel'): + pass + else: + self.loglevel = logging.ERROR + self.logger.setLevel(self.loglevel) + # Display log messages only on defined handlers. + self.logger.propagate = False + ## It will work on OS X and Linux. No FreeBSD support, guys, + ## I don't want to import re here + ## to parse your peculiar platform string. + #if sys.platform == "darwin": + # syslog_address = "/var/run/syslog" + #else: + # syslog_address = "/dev/log" + # Try to mimic to normal syslog messages. + self.green_pool = eventlet.greenpool.GreenPool(size=green_pool_size) + + def run(self): + """ + Method representing the thread’s activity. + You may override this method in a subclass. + """ + import time + + time.sleep(25) + self.logger.warn("green-daemon body. You must redefine run() method ") + + def sighup(self): + """ + Method, that will be call while SIGHUP received + """ + self.logger.warn("Caught signal HUP. Reloading.") + + def sigterm(self): + """ + Method, that will be call while SIGTERM received + """ + self.logger.warn("Caught signal TERM. Stopping daemon.") + self.remove_pidfile() + + def create_pidfile(self, recurse=10): + """ + Create a locked PID-file so that only one + instance of this daemon is running at any time. + """ + try: + fd = os.open(self.pidfile, os.O_WRONLY | os.O_CREAT | os.O_EXCL) + except OSError as e: + self.logger.debug("errno='{0}'".format(e.errno)) + if e.errno in [errno.EACCES, errno.EAGAIN, errno.EEXIST]: + self.logger.warn( + "Can't create PID-file. " + "Pidfile '{file}' already exists.".format( + file=self.pidfile)) + #find process with PID of it file + try: + with open(self.pidfile, 'r') as f: + pid = f.readline() + try: + pid = int(pid) + except ValueError: + pid = 0 + if pid > 0: + try: + os.kill(pid, 0) + self.logger.error( + "Process with PID " + "{0} found, exiting...".format(pid) + ) + sys.exit(2) + except OSError as e: + if e.errno == errno.ESRCH and recurse > 0: + self.logger.debug( + "Found PID-file, but process with " + "PID={0} not found".format(pid) + ) + os.unlink(self.pidfile) + return self.create_pidfile( + recurse=recurse - 1) + elif recurse <= 0: + self.logger.error( + "Can't start daemon, " + "due can't remove existing " + "PID-file '{0}' from another.".format( + self.pidfile) + ) + sys.exit(2) + else: + self.logger.error( + "Process with PID " + "{0} found, exiting...".format(pid) + ) + sys.exit(2) + else: + self.logger.debug( + "Found PID-file, that contains no PID.") + os.unlink(self.pidfile) + return self.create_pidfile(recurse=recurse - 1) + except IOError as e: + self.logger.error( + "Can't read PID from file " + "'{file}'\n{err}".format(file=self.pidfile, err=e)) + sys.exit(2) + else: + self.logger.error("Can't create PID-file.\n{0}".format(e)) + sys.exit(1) + pid = os.getpid() + os.write(fd, "{pid}".format(pid=pid)) + os.fsync(fd) + self.pidfile_fd = fd + return pid + + def remove_pidfile(self): + try: + os.close(self.pidfile_fd) + except OSError as e: + self.logger.debug("errno='{0}'".format(e.errno)) + pass + try: + os.unlink(self.pidfile) + except OSError as e: + self.logger.debug("errno='{0}'".format(e.errno)) + if e.errno != errno.ENOENT: + self.logger.error(e) + + def start(self): + """ start method + Main daemonization process. + """ + RunningGreenDaemons.add(self) + + try: + if os.fork() > 0: + sys.exit(0) # kill off parent + except OSError as e: + self.logger.error( + "fork #1 failed: " + "{errno} {errmsg}".format( + errno=e.errno, errmsg=e.strerror)) + sys.exit(1) + self.logger.debug("fork #1 successful.") + os.setsid() + os.chdir('/') + os.umask(0o022) + + # Second fork + try: + if os.fork() > 0: + sys.exit(0) + except OSError as e: + self.logger.error( + "fork #2 failed: " + "{errno} {errmsg}".format( + errno=e.errno, errmsg=e.strerror)) + sys.exit(1) + self.logger.debug("fork #2 succeful.") + + devnull = os.devnull if hasattr(os, "devnull") else "/dev/null" + si = os.open(devnull, os.O_RDWR) + os.dup2(si, sys.stdin.fileno()) + sys.stdout = StreamToLogger(self.logger, logging.INFO) + sys.stderr = StreamToLogger(self.logger, logging.ERROR) + + # Set custom action on SIGTERM. + signal.signal(signal.SIGTERM, sigterm_handler) + signal.signal(signal.SIGHUP, sighup_handler) + + pid = self.create_pidfile() + self.logger.info("Daemonized successfully, PID={pid}".format(pid=pid)) + + self.run() + +# vim: tabstop=4 shiftwidth=4 softtabstop=4 diff --git a/contrib/fuel-utils/fuel_utils/fdb_cleaner/__init__.py b/contrib/fuel-utils/fuel_utils/fdb_cleaner/__init__.py new file mode 100644 index 000000000..e4e279d62 --- /dev/null +++ b/contrib/fuel-utils/fuel_utils/fdb_cleaner/__init__.py @@ -0,0 +1,92 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +import eventlet + +eventlet.monkey_patch() +import os +import sys +import argparse +import logging.handlers +from fuel_utils.fdb_cleaner.settings import LOG_NAME +from fuel_utils.fdb_cleaner.daemon import Daemon + + +def main(): + parser = argparse.ArgumentParser( + description='Quantum network node cleaning tool.') + parser.add_argument( + "-c", "--auth-config", dest="authconf", default="/root/openrc", + help="Authenticating config FILE", metavar="FILE") + parser.add_argument( + "-l", "--log", dest="log", action="store", default=None, + help="log file or logging.conf location") + parser.add_argument( + "-p", "--pid", dest="pid", action="store", + help="PID file", default="/tmp/{0}.pid".format(LOG_NAME)) + parser.add_argument( + "--retries", dest="retries", type=int, default=50, + help="try NN retries for OpenStack API call", metavar="NN") + parser.add_argument( + "--sleep", dest="sleep", type=int, default=2, + help="sleep seconds between retries", metavar="SEC") + parser.add_argument( + "--endpoint-type", dest="endpoint_type", action="store", + default="adminURL", + help="Endpoint type ('admin' or 'public') for use.", metavar="TYPE") + parser.add_argument( + "--ssh-username", dest="ssh_username", action="store", default='root', + help="Username for ssh connect", metavar="UNAME") + parser.add_argument( + "--ssh-password", dest="ssh_password", action="store", default=None, + help="Password for ssh connect", metavar="PASSWD") + parser.add_argument( + "--ssh-keyfile", dest="ssh_keyfile", action="append", + help="SSH key file", metavar="FILE") + parser.add_argument( + "--ssh-port", dest="ssh_port", type=int, default=22, + help="Port for SSH connection", metavar="NN") + parser.add_argument( + "--ssh-timeout", dest="ssh_timeout", type=int, default=120, + help="Connection timeout for SSH session", metavar="SEC") + parser.add_argument( + "--debug", dest="debug", action="store_true", default=False, + help="debug") + args = parser.parse_args() + + # setup logging + import logging # must be here due Py.logging design + + _log_level = logging.DEBUG if args.debug else logging.INFO + LOG = logging.getLogger(LOG_NAME) # do not move to UP of file + if not args.log: + # log config or file not given -- log to console + _log_handler = logging.StreamHandler(sys.stdout) + _log_handler.setFormatter(logging.Formatter( + "%(asctime)s - %(levelname)s - %(message)s")) + LOG.addHandler(_log_handler) + LOG.setLevel(_log_level) + elif args.log.split(os.sep)[-1] == 'logging.conf': + # setup logging by external file + import logging.config + + logging.config.fileConfig(args.log) + else: + # log to given file + _log_handler = logging.handlers.WatchedFileHandler(args.log) + _log_handler.setFormatter(logging.Formatter( + "%(asctime)s - %(levelname)s - %(message)s")) + LOG.addHandler(_log_handler) + LOG.setLevel(_log_level) + + LOG.info("Try to start daemon: {0}".format(' '.join(sys.argv))) + cfg = vars(args) + cfg['loglevel'] = _log_level + fdb_daemon = Daemon(cfg, logger=LOG) + fdb_daemon.start() + sys.exit(0) + + +if __name__ == '__main__': + main() + +# vim: tabstop=4 shiftwidth=4 softtabstop=4 diff --git a/contrib/fuel-utils/fuel_utils/fdb_cleaner/config.py b/contrib/fuel-utils/fuel_utils/fdb_cleaner/config.py new file mode 100644 index 000000000..51134aa59 --- /dev/null +++ b/contrib/fuel-utils/fuel_utils/fdb_cleaner/config.py @@ -0,0 +1,80 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +import eventlet + +eventlet.monkey_patch() +import re +import sys +import errno +import logging +from settings import LOG_NAME + + +class BaseAuthConfig(object): + """ + read auth config and store it. + Try be a singletone + """ + + def __init__(self): + self._configs = {} + + @staticmethod + def _read_config(cfg_file): + """ + Read OS auth config file + cfg_file -- the path to config file + """ + auth_conf_errors = { + 'OS_TENANT_NAME': 'Missing tenant name.', + 'OS_USERNAME': 'Missing username.', + 'OS_PASSWORD': 'Missing password.', + 'OS_AUTH_URL': 'Missing API url.', + } + rv = {} + stripchars = " \'\"" + LOG = logging.getLogger(LOG_NAME) + try: + with open(cfg_file) as f: + for line in f: + rg = re.match(r'\s*export\s+(\w+)\s*=\s*(.*)', line) + if rg: + rv[rg.group(1).strip(stripchars)] = \ + rg.group(2).strip(stripchars) + except IOError: + LOG.error("Can't open file '{path}'".format(path=cfg_file)) + sys.exit(errno.ENOENT) + + # error detection + exit_msg = [] + for i, e in auth_conf_errors.iteritems(): + if rv.get(i) is None: + exit_msg.append(e) + if len(exit_msg) > 0: + for msg in exit_msg: + LOG.error("AUTH-config error: '{msg}'".format(msg=msg)) + sys.exit(errno.EPROTO) + return rv + + def read(self, cfg_filename='/root/openrc'): + """ + Read or get from cache OS auth config file + + Args: + cfg_filename (str) -- the path to config file + Returns: + Dict of auth params. + Raises: + IOError: if file can't readable or not wound. + """ + rv = self._configs.get(cfg_filename) + if rv: + return rv + rv = self._read_config(cfg_filename) + self._configs[cfg_filename] = rv + return self._configs.get(cfg_filename) + + +AuthConfig = BaseAuthConfig() + +# vim: tabstop=4 shiftwidth=4 softtabstop=4 diff --git a/contrib/fuel-utils/fuel_utils/fdb_cleaner/daemon.py b/contrib/fuel-utils/fuel_utils/fdb_cleaner/daemon.py new file mode 100644 index 000000000..5031be10e --- /dev/null +++ b/contrib/fuel-utils/fuel_utils/fdb_cleaner/daemon.py @@ -0,0 +1,210 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +import eventlet + +eventlet.monkey_patch() +from keystoneclient.v2_0 import client as ks_client + +try: + from neutronclient.neutron import client as n_client +except ImportError: + from quantumclient.quantum import client as n_client +from fuel_utils.core.daemonize_green import Daemonize +from fuel_utils.fdb_cleaner.config import AuthConfig +from fuel_utils.fdb_cleaner.settings import API_VER +import sys +import os +import re +import random +import time +import paramiko + + +class Daemon(Daemonize): + """ + Main FDB-cleaner daemon class + """ + + def __init__(self, cfg, logger=None, green_pool_size=2000): + self.options = cfg + self.auth_config = AuthConfig.read(cfg.get('authconf')) + self.debug = cfg.get('debug') + self.loglevel = cfg.get('loglevel') + self.os_credentials = None + self.keystone = None + self.neutron = None + super(Daemon, self).__init__( + cfg['pid'], logger, green_pool_size=green_pool_size) + + def _get_keystone(self): + if not (self.os_credentials is None): + return self.os_credentials + ret_count = self.options.get('retries', 50) + while True: + if ret_count <= 0: + self.logger.error( + ">>> Keystone error: " + "no more retries for connect to keystone server.") + sys.exit(1) + try: + self.keystone = ks_client.Client( + username=self.auth_config.get('OS_USERNAME'), + password=self.auth_config.get('OS_PASSWORD'), + tenant_name=self.auth_config.get('OS_TENANT_NAME'), + auth_url=self.auth_config.get('OS_AUTH_URL') + ) + break + except Exception as e: + errmsg = e.message.strip() + # COMMENT: Very bad idea to think we can + # catch all known ways here + if re.search(r"Connection\s+refused$", errmsg, re.I) or \ + re.search(r"Connection\s+timed\s+out$", errmsg, re.I) or \ + re.search(r"Service\s+Unavailable$", errmsg, re.I) or \ + re.search( + r"'*NoneType'.*attribute\s+'*__getitem__'*$", + errmsg, re.I) or \ + re.search( + r"No\s+route\s+to\s+host$", errmsg, re.I): + self.logger.info( + ">>> Can't connect to " + "{0}, wait for server ready...".format( + self.auth_config.get('OS_AUTH_URL'))) + time.sleep(self.options.sleep) + # COMMENT: I'm not sure the way how keystone client work's + # So 'else' branch can't be always a keystone error + else: + self.logger.error(">>> Keystone error:\n" + "{0}".format(e.message)) + sys.exit(1) + ret_count -= 1 + self.os_credentials = { + 'net_endpoint': self.keystone.service_catalog.url_for( + service_type='network', + endpoint_type=self.options.get('endpoint_type') + ), + 'nova_endpoint': self.keystone.service_catalog.url_for( + service_type='compute', + endpoint_type=self.options.get('endpoint_type') + ), + 'token': self.keystone.auth_token + } + + def _get_neutron(self): + if (self.os_credentials is None) or \ + (self.os_credentials.get('net_endpoint') is None): + self.logger.error("Neutron: credentials not given.") + sys.exit(1) + self.neutron = n_client.Client( + API_VER, + endpoint_url=self.os_credentials.get('net_endpoint'), + token=self.os_credentials.get('token') + ) + + def _get_another_agents_list(self): + #todo: catch some exceptions for retry + return self.neutron.list_agents() + + def __run(self): + time.sleep(60) + self.remove_pidfile() + + def run(self): + # get credentials + self._get_keystone() + # get neutron interface object + self._get_neutron() + # ask neutron-api for list nodes with have ovs-agent + agents = self._get_another_agents_list() + if type(agents) != dict or type(agents.get('agents')) != list: + return None + nodes = [ + i for i in agents.get('agents') + if i.get('agent_type') == 'Open vSwitch agent' + and i.get('alive') + and i.get('host') != os.getenv('HOSTNAME') + ] + # process nodes + for node in nodes: + self.logger.debug("+spawning: {0}".format(node.get('host'))) + self.green_pool.spawn_n(self.worker, node) + self.logger.debug("+spawned: {0}".format(node.get('host'))) + self.green_pool.waitall() + self.logger.info("*** end of work") + time.sleep(10) + self.remove_pidfile() + + def worker(self, node_hash): + # ssh to node + self.logger.info( + " ssh to '{node}:{port}'".format( + node=node_hash.get('host'), + port=self.options.get('ssh_port', '22'))) + try: + ssh = paramiko.SSHClient() + ssh.load_system_host_keys() + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + ssh.connect( + node_hash.get('host'), + port=self.options.get('ssh_port') or None, + username=self.options.get('ssh_username'), + password=self.options.get('ssh_password') or None, + timeout=self.options.get('ssh_timeout') or None, + key_filename=self.options.get('ssh_keyfile') or None, + #compress=False, + ) + except paramiko.SSHException as e: + self.logger.error( + "Can't connect to '{node}:{port}'\n{error}".format( + node=node_hash.get('host'), + port=self.options.get('ssh_port', '22'), + error=e)) + return None + except: + self.logger.error( + "Can't connect to '{node}:{port}'\nUnrecognized error".format( + node=node_hash.get('host'), + port=self.options.get('ssh_port', '22'))) + return None + w = int(random.random() * 120) + rcommands = [ + "ovs-appctl fdb/flush", + ] + for rcmd in rcommands: + try: + stdin, stdout, stderr = ssh.exec_command( + rcmd, + # COMMENT: What about default timeout for a command? + # timeout=0.0, + # for non-blocking mode (False -- for blocking mode) + # get_pty=False + ) + #for line in stdout: + # # pass + # self.logger.debug("remote say: '{0}'".format(line.rstrip("\n"))) + # waiting execute command and get return-code. + rc = stdout.channel.recv_exit_status() + err_msg = "{node}: '{cmd}', rc={rc}".format( + node=node_hash.get('host'), + cmd=rcmd, + rc=rc + ) + if rc == 0: + self.logger.debug(err_msg) + else: + self.logger.error(err_msg) + except paramiko.SSHException as e: + self.logger.error( + "{node}: '{cmd}', exception:\n{error}".format( + node=node_hash.get('host'), + cmd=rcmd, + error=e)) + except: + self.logger.error( + "{node}: '{cmd}', exception:\nUnrecognized error".format( + node=node_hash.get('host'), + cmd=rcmd)) + self.logger.debug( + "session to '{node}' done.".format(node=node_hash.get('host'))) + +# vim: tabstop=4 shiftwidth=4 softtabstop=4 diff --git a/contrib/fuel-utils/fuel_utils/fdb_cleaner/settings.py b/contrib/fuel-utils/fuel_utils/fdb_cleaner/settings.py new file mode 100644 index 000000000..43ff77f20 --- /dev/null +++ b/contrib/fuel-utils/fuel_utils/fdb_cleaner/settings.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +import eventlet + +eventlet.monkey_patch() + +LOG_NAME = 'neutron-fdb-cleaner' +API_VER = '2.0' + +# vim: tabstop=4 shiftwidth=4 softtabstop=4 diff --git a/contrib/fuel-utils/requirements.txt b/contrib/fuel-utils/requirements.txt new file mode 100644 index 000000000..10d9e9967 --- /dev/null +++ b/contrib/fuel-utils/requirements.txt @@ -0,0 +1,5 @@ +paramiko>=1.12 +eventlet>=0.12 +greenlet>=0.3.1 +python-neutronclient +python-keystoneclient diff --git a/contrib/fuel-utils/setup.cfg b/contrib/fuel-utils/setup.cfg new file mode 100644 index 000000000..4910e6d1f --- /dev/null +++ b/contrib/fuel-utils/setup.cfg @@ -0,0 +1,38 @@ +[metadata] +name = fuel_utils +version = 20131119-01 +summary = CLI utilities for FUEL project +description-file = + README.md +author = Mirantis +author_email = fuel-dev@mirantis.com +maintainer = Sergey Vasilenko +maintainer_email = svasilenko@mirantis.com +home-page = http://www.mirantis.com/FUEL/ +classifier = + Environment :: OpenStack + Intended Audience :: Information Technology + Intended Audience :: System Administrators + License :: OSI Approved :: Apache Software License + Operating System :: POSIX :: Linux + Programming Language :: Python + Programming Language :: Python :: 2 + Programming Language :: Python :: 2.7 + Programming Language :: Python :: 2.6 + +[files] +packages = + fuel_utils +data_files = + /etc/fuel = + etc/fdb_cleaner.conf +;scripts = +; bin/fuel-fdb-cleaner + +[entry_points] +console_scripts = + fuel-fdb-cleaner = fuel_utils.fdb_cleaner:main + +[global] +setup-hooks = + pbr.hooks.setup_hook diff --git a/contrib/fuel-utils/setup.py b/contrib/fuel-utils/setup.py new file mode 100644 index 000000000..eccaa6732 --- /dev/null +++ b/contrib/fuel-utils/setup.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +## +## To deploy use +## $ python ./setup.py sdist upload +## +## Your need increase version before it !!! +## + +from __future__ import unicode_literals +import setuptools + +setuptools.setup( + setup_requires=[ + 'pbr', + 'setuptools' + ], + pbr=True +) + +# vim: tabstop=4 shiftwidth=4 softtabstop=4