from fabric.contrib.files import exists as fab_exists from fabric.operations import run from fabric.api import env import os import sys import pathlib dir_parent = pathlib.Path(os.path.abspath(__file__)).parents[2] sys.path.append(str(dir_parent)) try: import customfabric.modules.utils as utils except ImportError: raise # import os # import sys # import utils def check_is_conf(section, handle_error=False): """confirms that the configuration section name passed is a legit one Keyword Arguments: section -- the configuration section we are looking at handle_error -- if True, print out an error message and exit """ configuration = env.config if section in configuration.templates.keys(): return True else: if handle_error: print(""" Error. maintenance.exists_dir_sub takes a 'section' parameter value. '%s' is not a valid parameter value. Valid options include:""" % section) for key in configuration.templates.keys(): print(" %s" % key) print(""" Please run the command again, but this time enter a valid section value. """) sys.exit() return False def exists_dir_top(): """ Check if the parent directory for all configuration files exists""" from fabric.contrib.files import exists configuration = env.config # NOTE # all template configuration files are built off the files that are # contained LOCALLY. I don't bother building them off the remotely # located files, since those files get rsync'd anyway. if env.debug: print("maintenance.exists_dir_top -- checking for " "directory:\n\t%s\n" % configuration.paths.conf.local) return exists(configuration.paths.conf.local) def exists_dir_sub(section): """Check if the subdirectory for this configuration type exists in the configuration directory Keyword Arguments: section -- the configuration section we are looking at """ configuration = env.config # NOTE # all template configuration files are built off the files that are # contained LOCALLY. I don't bother building them off the remotely # located files, since those files get rsync'd anyway. check_is_conf(section) _template = getattr(configuration.templates, section) if env.debug: utils.printvar('template.path.local', _template.path.local) path_test = os.path.join(_template.path.local, 'blah') utils.printvar('exists_path_test', fab_exists(path_test)) utils.printvar('exists_local', fab_exists(_template.path.local)) else: return fab_exists(_template.path.local) def exists_file(section): """Check if the template file for this configuration type exists in the configuration directory Keyword Arguments: section -- the configuration type """ configuration = env.config check_is_conf(section) exists_dir_sub(section) utils.print_console("\tNOTE: exists_file ONLY works when run on the" " local branch!\n\tThis is because it is set up to " " only check development template config files", numsep=90) _template = getattr(configuration.templates, section) path_src = os.path.join( _template.path.local, 'files', _template.conf.src) if env.debug: utils.printvar('template.path.local', _template.path.local) utils.printvar('template.src', _template.conf.src) utils.printvar('template.dst', _template.conf.dst) utils.printvar('path_src', path_src) utils.printvar('path_exists', fab_exists(path_src)) return fab_exists(path_src) # TODO # DONE 1. make sure the configuration request is legit for this branch # DONE 2. check to see if conf directory exists # DONE 3. check to see if conf template file exists # 4a. (optional) add switch to check if conf file was built from template # 4b. (optional) check to see if version is up to date def create_dir(): # TODO # 1. make sure the configuration request is legit for this branch # 2. check to see if conf dir already exists # 3. if not create it pass def create_dir_top(): """Creates the top level conf directory if it does not exist""" import utils configuration = env.config if not exists_dir_top(): cmd_mkdir = "mkdir %s" % configuration.paths.conf.remote run(cmd_mkdir) else: msg = "configuration directory already exists, aborting create." \ "Continue? Y/n" utils.prompt_continue(message=msg) def create_file(): pass