"Fossies" - the Fresh Open Source Software Archive

Member "glusterfs-8.2/tools/glusterfind/src/nodeagent.py" (16 Sep 2020, 5079 Bytes) of package /linux/misc/glusterfs-8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "nodeagent.py" see the Fossies "Dox" file reference documentation.

    1 #!/usr/bin/python3
    2 # -*- coding: utf-8 -*-
    3 
    4 # Copyright (c) 2015 Red Hat, Inc. <http://www.redhat.com/>
    5 # This file is part of GlusterFS.
    6 #
    7 # This file is licensed to you under your choice of the GNU Lesser
    8 # General Public License, version 3 or any later version (LGPLv3 or
    9 # later), or the GNU General Public License, version 2 (GPLv2), in all
   10 # cases as published by the Free Software Foundation.
   11 
   12 import shutil
   13 import sys
   14 import os
   15 import logging
   16 from argparse import ArgumentParser, RawDescriptionHelpFormatter
   17 try:
   18     import urllib.parse as urllib
   19 except ImportError:
   20     import urllib
   21 from errno import ENOTEMPTY
   22 
   23 from utils import setup_logger, mkdirp, handle_rm_error
   24 import conf
   25 
   26 logger = logging.getLogger()
   27 
   28 
   29 def mode_cleanup(args):
   30     working_dir = os.path.join(conf.get_opt("working_dir"),
   31                                args.session,
   32                                args.volume,
   33                                args.tmpfilename)
   34 
   35     mkdirp(os.path.join(conf.get_opt("log_dir"), args.session, args.volume),
   36            exit_on_err=True)
   37     log_file = os.path.join(conf.get_opt("log_dir"),
   38                             args.session,
   39                             args.volume,
   40                             "changelog.log")
   41 
   42     setup_logger(logger, log_file)
   43 
   44     try:
   45         shutil.rmtree(working_dir, onerror=handle_rm_error)
   46     except (OSError, IOError) as e:
   47         logger.error("Failed to delete working directory: %s" % e)
   48         sys.exit(1)
   49 
   50 
   51 def mode_create(args):
   52     session_dir = os.path.join(conf.get_opt("session_dir"),
   53                                args.session)
   54     status_file = os.path.join(session_dir, args.volume,
   55                      "%s.status" % urllib.quote_plus(args.brick))
   56 
   57     mkdirp(os.path.join(session_dir, args.volume), exit_on_err=True,
   58            logger=logger)
   59 
   60     if not os.path.exists(status_file) or args.reset_session_time:
   61         with open(status_file, "w") as f:
   62             f.write(args.time_to_update)
   63 
   64     sys.exit(0)
   65 
   66 
   67 def mode_post(args):
   68     session_dir = os.path.join(conf.get_opt("session_dir"), args.session)
   69     status_file = os.path.join(session_dir, args.volume,
   70                      "%s.status" % urllib.quote_plus(args.brick))
   71 
   72     mkdirp(os.path.join(session_dir, args.volume), exit_on_err=True,
   73            logger=logger)
   74     status_file_pre = status_file + ".pre"
   75 
   76     if os.path.exists(status_file_pre):
   77         os.rename(status_file_pre, status_file)
   78         sys.exit(0)
   79 
   80 
   81 def mode_delete(args):
   82     session_dir = os.path.join(conf.get_opt("session_dir"),
   83                                args.session)
   84     shutil.rmtree(os.path.join(session_dir, args.volume),
   85                   onerror=handle_rm_error)
   86 
   87     # If the session contains only this volume, then cleanup the
   88     # session directory. If a session contains multiple volumes
   89     # then os.rmdir will fail with ENOTEMPTY
   90     try:
   91         os.rmdir(session_dir)
   92     except OSError as e:
   93         if not e.errno == ENOTEMPTY:
   94             logger.warn("Failed to delete session directory: %s" % e)
   95 
   96 
   97 def _get_args():
   98     parser = ArgumentParser(formatter_class=RawDescriptionHelpFormatter,
   99                             description="Node Agent")
  100     subparsers = parser.add_subparsers(dest="mode")
  101 
  102     parser_cleanup = subparsers.add_parser('cleanup')
  103     parser_cleanup.add_argument("session", help="Session Name")
  104     parser_cleanup.add_argument("volume", help="Volume Name")
  105     parser_cleanup.add_argument("tmpfilename", help="Temporary File Name")
  106     parser_cleanup.add_argument("--debug", help="Debug", action="store_true")
  107 
  108     parser_session_create = subparsers.add_parser('create')
  109     parser_session_create.add_argument("session", help="Session Name")
  110     parser_session_create.add_argument("volume", help="Volume Name")
  111     parser_session_create.add_argument("brick", help="Brick Path")
  112     parser_session_create.add_argument("time_to_update", help="Time to Update")
  113     parser_session_create.add_argument("--reset-session-time",
  114                                        help="Reset Session Time",
  115                                        action="store_true")
  116     parser_session_create.add_argument("--debug", help="Debug",
  117                                        action="store_true")
  118 
  119     parser_post = subparsers.add_parser('post')
  120     parser_post.add_argument("session", help="Session Name")
  121     parser_post.add_argument("volume", help="Volume Name")
  122     parser_post.add_argument("brick", help="Brick Path")
  123     parser_post.add_argument("--debug", help="Debug",
  124                              action="store_true")
  125 
  126     parser_delete = subparsers.add_parser('delete')
  127     parser_delete.add_argument("session", help="Session Name")
  128     parser_delete.add_argument("volume", help="Volume Name")
  129     parser_delete.add_argument("--debug", help="Debug",
  130                                action="store_true")
  131     return parser.parse_args()
  132 
  133 
  134 if __name__ == "__main__":
  135     args = _get_args()
  136 
  137     # globals() will have all the functions already defined.
  138     # mode_<args.mode> will be the function name to be called
  139     globals()["mode_" + args.mode](args)