"Fossies" - the Fresh Open Source Software Archive

Member "storlets-7.0.0/devstack/swift_config.py" (14 Apr 2021, 8070 Bytes) of package /linux/misc/openstack/storlets-7.0.0.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 "swift_config.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 6.0.0_vs_7.0.0.

    1 # Copyright IBM Corp. 2015, 2015 All Rights Reserved
    2 # Copyright (c) 2010-2016 OpenStack Foundation
    3 #
    4 # Licensed under the Apache License, Version 2.0 (the "License");
    5 # you may not use this file except in compliance with the License.
    6 # You may obtain a copy of the License at
    7 #
    8 #    http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 # Unless required by applicable law or agreed to in writing, software
   11 # distributed under the License is distributed on an "AS IS" BASIS,
   12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
   13 # implied.
   14 # See the License for the specific language governing permissions and
   15 # limitations under the License.
   16 
   17 from six.moves import configparser as ConfigParser
   18 import fileinput
   19 import os
   20 import pwd
   21 import shutil
   22 import sys
   23 
   24 swift_run_time_user = None
   25 
   26 
   27 def _chown_to_swift(path):
   28     global swift_run_time_user
   29     uc = pwd.getpwnam(swift_run_time_user)
   30     os.chown(path, uc.pw_uid, uc.pw_gid)
   31 
   32 
   33 def _unpatch_pipeline_line(orig_line, storlet_middleware):
   34     mds = list()
   35     for md in orig_line.split():
   36         if md == 'pipeline' or md == '=':
   37             continue
   38         mds.append(md)
   39 
   40     if storlet_middleware in mds:
   41         mds.remove(storlet_middleware)
   42 
   43     new_line = 'pipeline ='
   44     for md in mds:
   45         new_line += ' ' + md
   46 
   47     return new_line + '\n'
   48 
   49 
   50 def _patch_proxy_pipeline_line(orig_line, storlet_middleware):
   51     mds = list()
   52     for md in orig_line.split():
   53         if md == 'pipeline' or md == '=':
   54             continue
   55         mds.append(md)
   56 
   57     if storlet_middleware in mds:
   58         return orig_line
   59 
   60     # If there is 'copy' middleware, storlet_hander is placed
   61     # in the left of 'copy' middleware.
   62     try:
   63         copy_index = mds.index('copy')
   64     except Exception:
   65         copy_index = -1
   66 
   67     if copy_index != -1:
   68         mds.insert(copy_index, storlet_middleware)
   69     else:
   70         # If there is slo middleware, storlet_hander is placed
   71         # in the left of slo middleware.
   72         try:
   73             slo_index = mds.index('slo')
   74         except Exception:
   75             slo_index = -1
   76 
   77         if slo_index != -1:
   78             mds.insert(slo_index, storlet_middleware)
   79         else:
   80             # Otherwise, storlet_hander is placed in the left of proxy-sever.
   81             proxy_index = mds.index('proxy-server')
   82             mds.insert(proxy_index, storlet_middleware)
   83 
   84     new_line = 'pipeline ='
   85     for md in mds:
   86         new_line += ' ' + md
   87 
   88     return new_line + '\n'
   89 
   90 
   91 def _patch_object_pipeline_line(orig_line, storlet_middleware):
   92     mds = list()
   93     for md in orig_line.split():
   94         if md == 'pipeline' or md == '=':
   95             continue
   96         mds.append(md)
   97 
   98     if storlet_middleware in mds:
   99         return orig_line
  100 
  101     object_index = mds.index('object-server')
  102     mds.insert(object_index, storlet_middleware)
  103 
  104     new_line = 'pipeline ='
  105     for md in mds:
  106         new_line += ' ' + md
  107 
  108     return new_line + '\n'
  109 
  110 
  111 def unpatch_swift_config_file(conf, conf_file):
  112     storlet_middleware = conf.get('common-confs', 'storlet_middleware')
  113 
  114     for line in fileinput.input(conf_file, inplace=1):
  115         if line.startswith('pipeline'):
  116             new_line = _unpatch_pipeline_line(line, storlet_middleware)
  117             line = new_line
  118         sys.stdout.write(line)
  119 
  120     _chown_to_swift(conf_file)
  121 
  122 
  123 def patch_swift_config_file(conf, conf_file, service):
  124     storlet_middleware = conf.get('common-confs', 'storlet_middleware')
  125     filter_block_first_line = '[filter:%s]\n' % storlet_middleware
  126 
  127     filter_in_file = False
  128     for line in fileinput.input(conf_file, inplace=1):
  129         if line.startswith('pipeline'):
  130             if service == 'proxy':
  131                 new_line = _patch_proxy_pipeline_line(line, storlet_middleware)
  132             else:
  133                 new_line = _patch_object_pipeline_line(line,
  134                                                        storlet_middleware)
  135             line = new_line
  136         if filter_block_first_line in line:
  137             filter_in_file = True
  138         sys.stdout.write(line)
  139 
  140     if filter_in_file is False:
  141         with open(conf_file, 'a') as f:
  142             f.write('\n')
  143             f.write(filter_block_first_line)
  144             f.write('use = egg:storlets#%s\n' % storlet_middleware)
  145             f.write('storlet_container = %s\n' %
  146                     conf.get('common-confs', 'storlet_container'))
  147             f.write('storlet_dependency = %s\n' %
  148                     conf.get('common-confs', 'storlet_dependency'))
  149             f.write('storlet_gateway_module = %s\n' %
  150                     conf.get('common-confs', 'storlet_gateway_module'))
  151             f.write('storlet_gateway_conf = %s\n' %
  152                     conf.get('common-confs', 'storlet_gateway_conf'))
  153             f.write('storlet_execute_on_proxy_only = %s\n' % conf.get(
  154                 'common-confs', 'storlet_proxy_execution'))
  155             f.write('execution_server = %s\n' % service)
  156 
  157     _chown_to_swift(conf_file)
  158 
  159 
  160 def unpatch_swift_storlet_proxy_file(conf):
  161     storlet_proxy_server_conf_file = conf.get('proxy-confs',
  162                                               'storlet_proxy_server_conf_file')
  163     if os.path.exists(storlet_proxy_server_conf_file):
  164         os.remove(storlet_proxy_server_conf_file)
  165 
  166 
  167 def patch_swift_storlet_proxy_file(conf):
  168     storlet_proxy_server_conf_file = conf.get('proxy-confs',
  169                                               'storlet_proxy_server_conf_file')
  170     proxy_server_conf_file = conf.get('proxy-confs', 'proxy_server_conf_file')
  171 
  172     source_file = proxy_server_conf_file
  173     target_file = storlet_proxy_server_conf_file
  174     shutil.copyfile(source_file, target_file)
  175 
  176     for line in fileinput.input(storlet_proxy_server_conf_file, inplace=1):
  177         if line.startswith('pipeline'):
  178             # If there is no proxy-logging in the configuration file, we don't
  179             # want to add it to the pipeline. This may cause invalid internal
  180             # client configuration (we encountered this problem in a fuel swift
  181             # cluster).
  182             if 'proxy-logging' in line:
  183                 line = ('pipeline = proxy-logging cache storlet_handler slo '
  184                         'proxy-logging proxy-server\n')
  185             else:
  186                 line = 'pipeline = cache storlet_handler slo proxy-server\n'
  187         sys.stdout.write(line)
  188 
  189     _chown_to_swift(storlet_proxy_server_conf_file)
  190 
  191 
  192 def remove_gateway_conf_file(conf):
  193     gateway_conf_file = conf.get('common-confs', 'storlet_gateway_conf')
  194     if os.path.exists(gateway_conf_file):
  195         os.remove(gateway_conf_file)
  196 
  197 
  198 def remove(conf):
  199     object_server_conf_files = conf.get('object-confs',
  200                                         'object_server_conf_files').split(',')
  201     for f in object_server_conf_files:
  202         if os.path.exists(f):
  203             unpatch_swift_config_file(conf, f)
  204 
  205     proxy_server_conf_file = conf.get('proxy-confs', 'proxy_server_conf_file')
  206     unpatch_swift_config_file(conf, proxy_server_conf_file)
  207 
  208     unpatch_swift_storlet_proxy_file(conf)
  209     remove_gateway_conf_file(conf)
  210 
  211 
  212 def install(conf):
  213     object_server_conf_files = conf.get('object-confs',
  214                                         'object_server_conf_files').split(',')
  215     for f in object_server_conf_files:
  216         if os.path.exists(f):
  217             patch_swift_config_file(conf, f, 'object')
  218 
  219     proxy_server_conf_file = conf.get('proxy-confs', 'proxy_server_conf_file')
  220     patch_swift_config_file(conf, proxy_server_conf_file, 'proxy')
  221 
  222     patch_swift_storlet_proxy_file(conf)
  223 
  224 
  225 def usage(argv):
  226     print("Usage: %s %s %s" % (argv[0],
  227                                "install/remove conf_file",
  228                                "swift_run_time_user"))
  229 
  230 
  231 def main(argv):
  232     if len(argv) != 4:
  233         usage(argv)
  234         exit(-1)
  235 
  236     conf = ConfigParser.ConfigParser()
  237     conf.read(argv[2])
  238     global swift_run_time_user
  239     swift_run_time_user = argv[3]
  240 
  241     if argv[1] == 'install':
  242         install(conf)
  243     elif argv[1] == 'remove':
  244         remove(conf)
  245     else:
  246         usage(argv)
  247 
  248 
  249 if __name__ == "__main__":
  250     main(sys.argv)