def playbook(install_path, playbook='playbook', ask_sudo_pass=False, ask_vault_pass=False, extras=None): common.get_dotenv() check_roles_dir(install_path) os.environ["ANSIBLE_CONFIG"] = os.path.join(install_path, 'ansible.cfg') command_string = 'ansible-playbook' command_string += ' -i "%s"' % os.path.join(install_path, 'inventory') if ask_sudo_pass or os.environ.get("OSXSTRAP_ASK_SUDO_PASS") == '1': command_string += ' --ask-sudo-pass' if ask_vault_pass or os.environ.get("OSXSTRAP_ASK_VAULT_PASS") == '1': command_string += ' --ask-vault-pass' if extras: command_string += ' ' + extras default_playbook_path = os.path.join(install_path, playbook) + '.yml' if os.path.exists(default_playbook_path): command_string += ' "%s"' % default_playbook_path else: custom_playbook_path = os.path.join(custom_playbooks_path, playbook) + '.yml' if os.path.exists(custom_playbook_path): command_string += ' "%s"' % custom_playbook_path else: output.abort( "Cannot find playbook %s.yml, looked for it at\n%s\n%s" % (playbook, default_playbook_path, custom_playbook_path)) common.run(command_string)
def edit(editor): if editor == 'webapp': config_file_path = os.path.join(config_path, config_filename) if os.path.exists(config_file_path): f = open(config_file_path) config_string = f.read() f.close() encoded_config_string = base64.b64encode(config_string) print encoded_config_string common.run('open https://osxstrap.github.io#%s' % encoded_config_string) else: output.abort("%s does not exist." % config_file_path) else: common.run('%s %s' % (editor, os.path.join(config_path, config_filename)))
def run(command, capture=False, shell=None): """ Run a command on the local system. Based on https://github.com/fabric/fabric/blob/master/fabric/operations.py. """ print("running local command: %s" % (command)) # Tie in to global output controls as best we can; our capture argument # takes precedence over the output settings. dev_null = None if capture: out_stream = subprocess.PIPE err_stream = subprocess.PIPE else: dev_null = open(os.devnull, 'w+') # Non-captured, hidden streams are discarded. out_stream = None err_stream = None try: cmd_arg = [command] p = subprocess.Popen(cmd_arg, shell=True, stdout=out_stream, stderr=err_stream, executable=shell) (stdout, stderr) = p.communicate() finally: if dev_null is not None: dev_null.close() # Handle error condition (deal with stdout being None, too) out = _AttributeString(stdout.strip() if stdout else "") err = _AttributeString(stderr.strip() if stderr else "") out.command = command out.real_command = command out.failed = False out.return_code = p.returncode out.stderr = err if p.returncode not in [0]: out.failed = True msg = "local command encountered an error (return code %s) while executing '%s'" % ( p.returncode, command) output.abort(msg) out.succeeded = not out.failed # If we were capturing, this will be a string; otherwise it will be None. return out
def copy_config(source_path): if not os.path.isabs(source_path): source_path = os.path.join(os.getcwd(), source_path) destination_path = os.path.join(config_path, config_filename) if source_path and source_path != destination_path: if os.path.exists(source_path): if not os.path.exists(destination_path): common.mkdir(config_path) copyfile(source_path, destination_path) else: output.warning("Destination file %s already exists." % destination_path) if click.confirm('Do you want to overwrite it?'): os.remove(destination_path) copyfile(source_path, destination_path) else: output.abort("To run osxstrap without copying config, use the osxstrap command.") else: output.abort("Input file %s does not exist." % source_path)
def copy_config(source_path): if not os.path.isabs(source_path): source_path = os.path.join(os.getcwd(), source_path) destination_path = os.path.join(config_path, config_filename) if source_path and source_path != destination_path: if os.path.exists(source_path): if not os.path.exists(destination_path): common.mkdir(config_path) copyfile(source_path, destination_path) else: output.warning("Destination file %s already exists." % destination_path) if click.confirm('Do you want to overwrite it?'): os.remove(destination_path) copyfile(source_path, destination_path) else: output.abort( "To run osxstrap without copying config, use the osxstrap command." ) else: output.abort("Input file %s does not exist." % source_path)
def run(command, capture=False, shell=None): """ Run a command on the local system. Based on https://github.com/fabric/fabric/blob/master/fabric/operations.py. """ print("running local command: %s" % (command)) # Tie in to global output controls as best we can; our capture argument # takes precedence over the output settings. dev_null = None if capture: out_stream = subprocess.PIPE err_stream = subprocess.PIPE else: dev_null = open(os.devnull, "w+") # Non-captured, hidden streams are discarded. out_stream = None err_stream = None try: cmd_arg = [command] p = subprocess.Popen(cmd_arg, shell=True, stdout=out_stream, stderr=err_stream, executable=shell) (stdout, stderr) = p.communicate() finally: if dev_null is not None: dev_null.close() # Handle error condition (deal with stdout being None, too) out = _AttributeString(stdout.strip() if stdout else "") err = _AttributeString(stderr.strip() if stderr else "") out.command = command out.real_command = command out.failed = False out.return_code = p.returncode out.stderr = err if p.returncode not in [0]: out.failed = True msg = "local command encountered an error (return code %s) while executing '%s'" % (p.returncode, command) output.abort(msg) out.succeeded = not out.failed # If we were capturing, this will be a string; otherwise it will be None. return out
def playbook(install_path, playbook='playbook', ask_sudo_pass=False, ask_vault_pass=False, extras=None): common.get_dotenv() check_roles_dir(install_path) os.environ["ANSIBLE_CONFIG"] = os.path.join(install_path, 'ansible.cfg') command_string = 'ansible-playbook' command_string += ' -i "%s"' % os.path.join(install_path, 'inventory') if ask_sudo_pass or os.environ.get("OSXSTRAP_ASK_SUDO_PASS") == '1': command_string += ' --ask-sudo-pass' if ask_vault_pass or os.environ.get("OSXSTRAP_ASK_VAULT_PASS") == '1': command_string += ' --ask-vault-pass' if extras: command_string += ' ' + extras default_playbook_path = os.path.join(install_path, playbook) + '.yml' if os.path.exists(default_playbook_path): command_string += ' "%s"' % default_playbook_path else: custom_playbook_path = os.path.join(custom_playbooks_path, playbook) + '.yml' if os.path.exists(custom_playbook_path): command_string += ' "%s"' % custom_playbook_path else: output.abort("Cannot find playbook %s.yml, looked for it at\n%s\n%s" % (playbook, default_playbook_path, custom_playbook_path)) common.run(command_string)