forked from uncloud/uncloud
		
	begin to switch to configparser
To not have unwanted environment influence
This commit is contained in:
		
					parent
					
						
							
								e459434b91
							
						
					
				
			
			
				commit
				
					
						6d0ce65f5c
					
				
			
		
					 2 changed files with 46 additions and 11 deletions
				
			
		| 
						 | 
					@ -3,10 +3,8 @@
 | 
				
			||||||
import argparse
 | 
					import argparse
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
import importlib
 | 
					import importlib
 | 
				
			||||||
 | 
					 | 
				
			||||||
# For the exception
 | 
					 | 
				
			||||||
import decouple
 | 
					 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
 | 
					import os
 | 
				
			||||||
 | 
					
 | 
				
			||||||
COMMANDS = ['api', 'scheduler', 'host', 'filescanner', 'imagescanner', 'metadata']
 | 
					COMMANDS = ['api', 'scheduler', 'host', 'filescanner', 'imagescanner', 'metadata']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -15,20 +13,25 @@ if __name__ == "__main__":
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    arg_parser = argparse.ArgumentParser(prog='ucloud',
 | 
					    arg_parser = argparse.ArgumentParser(prog='ucloud',
 | 
				
			||||||
                                         description='Open Source Cloud Management Software')
 | 
					                                         description='Open Source Cloud Management Software')
 | 
				
			||||||
 | 
					    arg_parser.add_argument('-c', '--conf-dir', help="Configuration directory")
 | 
				
			||||||
    arg_parser.add_argument('component', choices=COMMANDS)
 | 
					    arg_parser.add_argument('component', choices=COMMANDS)
 | 
				
			||||||
    arg_parser.add_argument('component_args', nargs='*')
 | 
					    arg_parser.add_argument('component_args', nargs='*')
 | 
				
			||||||
    args = arg_parser.parse_args()
 | 
					    args = arg_parser.parse_args()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        name = args.component
 | 
					        name = args.component
 | 
				
			||||||
 | 
					 | 
				
			||||||
        mod = importlib.import_module("ucloud.{}.main".format(name))
 | 
					        mod = importlib.import_module("ucloud.{}.main".format(name))
 | 
				
			||||||
        main = getattr(mod, "main")
 | 
					        main = getattr(mod, "main")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if args.conf_dir:
 | 
				
			||||||
 | 
					            print("setting conf")
 | 
				
			||||||
 | 
					            os.environ['UCLOUD_CONF_DIR'] = args.conf_dir
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        main()
 | 
					        main()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    except decouple.UndefinedValueError as e:
 | 
					    # except decouple.UndefinedValueError as e:
 | 
				
			||||||
        print(e)
 | 
					    #     print(e)
 | 
				
			||||||
        sys.exit(1)
 | 
					    #     sys.exit(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    except Exception as e:
 | 
					    except Exception as e:
 | 
				
			||||||
        logging.exception(e)
 | 
					        logging.exception(e)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,14 +4,46 @@ from ucloud.common.host import HostPool
 | 
				
			||||||
from ucloud.common.request import RequestPool
 | 
					from ucloud.common.request import RequestPool
 | 
				
			||||||
from ucloud.common.vm import VmPool
 | 
					from ucloud.common.vm import VmPool
 | 
				
			||||||
from ucloud.common.storage_handlers import FileSystemBasedImageStorageHandler, CEPHBasedImageStorageHandler
 | 
					from ucloud.common.storage_handlers import FileSystemBasedImageStorageHandler, CEPHBasedImageStorageHandler
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from decouple import Config, RepositoryEnv, RepositoryEmpty
 | 
					from decouple import Config, RepositoryEnv, RepositoryEmpty
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Replacing decouple inline
 | 
				
			||||||
 | 
					import configparser
 | 
				
			||||||
 | 
					import os
 | 
				
			||||||
 | 
					import os.path
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import logging
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					log = logging.getLogger("ucloud.config")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					conf_name = "ucloud.conf"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					try:
 | 
				
			||||||
 | 
					    conf_dir = os.environ["UCLOUD_CONF_DIR"]
 | 
				
			||||||
 | 
					except KeyError:
 | 
				
			||||||
 | 
					    conf_dir = "/etc/ucloud"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					config_file = os.path.join(conf_dir, conf_name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					config = configparser.ConfigParser()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					try:
 | 
				
			||||||
 | 
					    with open(config_file, "r") as conf_fd:
 | 
				
			||||||
 | 
					        conf.read(conf_fd)
 | 
				
			||||||
 | 
					except FileNotFoundError:
 | 
				
			||||||
 | 
					    log.warn("Configuration file not found - using defaults")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Compatibility to old code
 | 
				
			||||||
 | 
					env_vars = config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Try importing config, but don't fail if it does not exist
 | 
					# Try importing config, but don't fail if it does not exist
 | 
				
			||||||
try:
 | 
					# try:
 | 
				
			||||||
    env_vars = Config(RepositoryEnv('/etc/ucloud/ucloud.conf'))
 | 
					#     env_vars = Config(RepositoryEnv('/etc/ucloud/ucloud.conf'))
 | 
				
			||||||
except FileNotFoundError:
 | 
					# except FileNotFoundError:
 | 
				
			||||||
    env_vars = Config(RepositoryEmpty())
 | 
					#     env_vars = Config(RepositoryEmpty())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
etcd_wrapper_args = ()
 | 
					etcd_wrapper_args = ()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue