def apply_settings(file='fab.cfg', group='default'): if not os.path.exists(file): _handle_failure(message='Configuration file %s does not exist' % file) return config = ConfigParser.ConfigParser() config.readfp(open(file)) user_settings = {} os.environ['DJANGO_SETTINGS_MODULE'] = config.get(group, 'django.settings') for name, value in config.items(group): user_settings[name] = value for key in env: if env[key] and isinstance(env[key], str): env[key] = env[key] % user_settings
def apply_settings(file='fab.cfg', group='default'): if not os.path.exists(file): _handle_failure(message='Configuration file %s does not exist' % file) return config = ConfigParser.ConfigParser() config.readfp(open(file)) user_settings = {} os.environ['DJANGO_SETTINGS_MODULE'] = config.get(group,'django.settings') for name,value in config.items(group): user_settings[name] = value for key in env: if env[key] and isinstance(env[key],str): env[key] = env[key] % user_settings
def mysql_create_user(db_user=None, db_password=None): """ Creates mysql user. """ _, db_user, db_password = _credentials(None, db_user, db_password) if db_user == 'root': # do we really need this? _handle_failure('MySQL root user can not be created') return if _mysql_user_exists(db_user): puts('User %s already exists' % db_user) return sql = MYSQL_CREATE_USER % dict(db_user=db_user, db_password=db_password) mysql_execute(sql, 'root')
def upload_config_template(name, to=None, skip_unexistent=False, **kwargs): if to is None: base_dir = env.conf['ENV_DIR'] + "/etc/" run('mkdir -p ' + base_dir) to = base_dir + name config_template = _config_template_path(name) if config_template is None: if skip_unexistent: return operations._handle_failure('Config template "%s" is not found' % name) files.upload_template(config_template, to, env.conf, use_jinja=True, **kwargs)
def get_db_settings(): try: from fabfile import settings except ImportError: msg = 'Please import django settings in your fabfile.py \nfrom django.conf import settings' _handle_failure(message=msg) if not 'DJANGO_SETTINGS_MODULE' in os.environ: msg = 'DJANGO_SETTINGS_MODULE not set \nYou must call a settings function that sets the os.environ[DJANGO_SETTINGS_MODULE] first' _handle_failure(message=msg) if not hasattr(settings, 'DATABASE_USER'): # global settings is not the django settings msg = 'Please import django settings in your fabfile.py \nfrom django.conf import settings' _handle_failure(message=msg) return {'user': settings.DATABASE_USER, 'pass':settings.DATABASE_PASSWORD, \ 'name':settings.DATABASE_NAME,'engine':settings.DATABASE_ENGINE}
def _run_command_local(command, shell=True, combine_stderr=True, sudo=False, user=None): ''' Local implementation of fabric.operations._run_command that uses subprocess to execute. ''' # Conditionally import error handling function, since different fabric # versions handle this differently try: from fabric.utils import error except ImportError: from fabric.operations import _handle_failure error = lambda msg=None, **kwargs: _handle_failure(msg) # Set up new var so original argument can be displayed verbatim later. given_command = command # Pick up cuisine sudo mode and password as appropriate if sudo and cuisine.sudo_password(): sudo_prefix = ('echo "%s" | %s -S -p ""' % (cuisine.sudo_password, env.sudo_prefix)) else: sudo_prefix = env.sudo_prefix # Handle context manager modifications, and shell wrapping with settings(sudo_prefix=sudo_prefix): wrapped_command = _shell_wrap( _prefix_commands(_prefix_env_vars(command), 'remote'), shell, _sudo_prefix(user) if sudo else None ) # Execute info line which = 'sudo' if sudo else 'run' if output.debug: print("[%s] %s: %s" % ('local', which, wrapped_command)) elif output.running: print("[%s] %s: %s" % ('local', which, given_command)) # Actual execution, stdin/stdout/stderr handling, and termination stdout, stderr, status = _execute_local(wrapped_command, shell=shell, combine_stderr=combine_stderr) # Assemble output string out = _AttributeString(stdout) err = _AttributeString(stderr) # Error handling out.failed = False if status != 0: out.failed = True msg = "%s() received nonzero return code %s while executing" % ( which, status ) if env.warn_only: msg += " '%s'!" % given_command else: msg += "!\n\nRequested: %s\nExecuted: %s" % ( given_command, wrapped_command ) error(message=msg, stdout=out, stderr=err) # Attach return code to output string so users who have set things to # warn only, can inspect the error code. out.return_code = status # Convenience mirror of .failed out.succeeded = not out.failed # Attach stderr for anyone interested in that. out.stderr = err return out
def _run_command_local(command, shell=True, combine_stderr=True, sudo=False, user=None): ''' Local implementation of fabric.operations._run_command that uses subprocess to execute. ''' # Conditionally import error handling function, since different fabric # versions handle this differently try: from fabric.utils import error except ImportError: from fabric.operations import _handle_failure error = lambda msg=None, **kwargs: _handle_failure(msg) # Set up new var so original argument can be displayed verbatim later. given_command = command # Pick up cuisine sudo mode and password as appropriate if sudo and cuisine.sudo_password(): sudo_prefix = ('echo "%s" | %s -S -p ""' % (cuisine.sudo_password, env.sudo_prefix)) else: sudo_prefix = env.sudo_prefix # Handle context manager modifications, and shell wrapping with settings(sudo_prefix=sudo_prefix): wrapped_command = _shell_wrap( _prefix_commands(_prefix_env_vars(command), 'remote'), shell, _sudo_prefix(user) if sudo else None) # Execute info line which = 'sudo' if sudo else 'run' if output.debug: print("[%s] %s: %s" % ('local', which, wrapped_command)) elif output.running: print("[%s] %s: %s" % ('local', which, given_command)) # Actual execution, stdin/stdout/stderr handling, and termination stdout, stderr, status = _execute_local(wrapped_command, shell=shell, combine_stderr=combine_stderr) # Assemble output string out = _AttributeString(stdout) err = _AttributeString(stderr) # Error handling out.failed = False if status != 0: out.failed = True msg = "%s() received nonzero return code %s while executing" % (which, status) if env.warn_only: msg += " '%s'!" % given_command else: msg += "!\n\nRequested: %s\nExecuted: %s" % (given_command, wrapped_command) error(message=msg, stdout=out, stderr=err) # Attach return code to output string so users who have set things to # warn only, can inspect the error code. out.return_code = status # Convenience mirror of .failed out.succeeded = not out.failed # Attach stderr for anyone interested in that. out.stderr = err return out
def error(message=None, **kwargs): return _handle_failure(message)