From f74d02cac57bee2d2cb1f8d096fa807f1325bf4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Mon, 14 Dec 2020 08:39:02 +0100 Subject: [PATCH] added MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .emacs.d/init.el | 21 ++- .local/bin/ansible | 177 ++++++++++++++++++ .local/bin/ansible-config | 1 + .local/bin/ansible-connection | 342 ++++++++++++++++++++++++++++++++++ .local/bin/ansible-console | 1 + .local/bin/ansible-doc | 1 + .local/bin/ansible-galaxy | 1 + .local/bin/ansible-inventory | 1 + .local/bin/ansible-playbook | 1 + .local/bin/ansible-pull | 1 + .local/bin/ansible-test | 28 +++ .local/bin/ansible-vault | 1 + 12 files changed, 571 insertions(+), 5 deletions(-) create mode 100755 .local/bin/ansible create mode 120000 .local/bin/ansible-config create mode 100755 .local/bin/ansible-connection create mode 120000 .local/bin/ansible-console create mode 120000 .local/bin/ansible-doc create mode 120000 .local/bin/ansible-galaxy create mode 120000 .local/bin/ansible-inventory create mode 120000 .local/bin/ansible-playbook create mode 120000 .local/bin/ansible-pull create mode 100755 .local/bin/ansible-test create mode 120000 .local/bin/ansible-vault diff --git a/.emacs.d/init.el b/.emacs.d/init.el index c1618f1..8b53327 100644 --- a/.emacs.d/init.el +++ b/.emacs.d/init.el @@ -13,6 +13,9 @@ ) (package-initialize) +(setq package-check-signature nil) +(setq gnutls-algorithm-priority "NORMAL:-VERS-TLS1.3") + ;; Remove working cl (require 'cl-lib) (setq byte-compile-warnings '(cl-functions)) @@ -100,7 +103,12 @@ (ac-config-default) (global-auto-complete-mode t) -;; Company +;; Company & robe +(use-package robe + :ensure t + :config + ) + (eval-after-load 'company '(push 'company-robe company-backends)) @@ -112,15 +120,15 @@ '(define-key inf-ruby-mode-map (kbd "TAB") 'auto-complete)) ;; Theme -;(use-package gruber-darker-theme +;(use-package dracula-theme ; :ensure t ; :config -; (load-theme 'gruber-darker-theme t)) +; (load-theme 'dracula t)) -(use-package dracula-theme +(use-package nord-theme :ensure t :config - (load-theme 'dracula t)) + (load-theme 'nord t)) ;; Sitebar dirred (use-package dired-sidebar @@ -212,6 +220,9 @@ ;; scrolling: (setq scroll-conservatively 100) +;; Whitespaces +(global-whitespace-mode 1) + ;; no "bell" (audible notification): (setq ring-bell-function 'ignore) diff --git a/.local/bin/ansible b/.local/bin/ansible new file mode 100755 index 0000000..5d48dfe --- /dev/null +++ b/.local/bin/ansible @@ -0,0 +1,177 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +# (c) 2012, Michael DeHaan +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# PYTHON_ARGCOMPLETE_OK + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +__requires__ = ['ansible_core'] + + +import errno +import os +import shutil +import sys +import traceback + +from ansible import context +from ansible.errors import AnsibleError, AnsibleOptionsError, AnsibleParserError +from ansible.module_utils._text import to_text + + +# Used for determining if the system is running a new enough python version +# and should only restrict on our documented minimum versions +_PY38_MIN = sys.version_info[:2] >= (3, 8) +_PY3_MIN = sys.version_info[:2] >= (3, 5) +_PY2_MIN = (2, 6) <= sys.version_info[:2] < (3,) +_PY_MIN = _PY3_MIN or _PY2_MIN +if not _PY_MIN: + raise SystemExit('ERROR: Ansible requires a minimum of Python2 version 2.6 or Python3 version 3.5. Current version: %s' % ''.join(sys.version.splitlines())) + + +class LastResort(object): + # OUTPUT OF LAST RESORT + def display(self, msg, log_only=None): + print(msg, file=sys.stderr) + + def error(self, msg, wrap_text=None): + print(msg, file=sys.stderr) + + +if __name__ == '__main__': + + display = LastResort() + + try: # bad ANSIBLE_CONFIG or config options can force ugly stacktrace + import ansible.constants as C + from ansible.utils.display import Display, initialize_locale + except AnsibleOptionsError as e: + display.error(to_text(e), wrap_text=False) + sys.exit(5) + + initialize_locale() + + cli = None + me = os.path.basename(sys.argv[0]) + + try: + display = Display() + if C.CONTROLLER_PYTHON_WARNING and not _PY38_MIN: + display.deprecated( + ( + 'Ansible will require Python 3.8 or newer on the controller starting with Ansible 2.12. ' + 'Current version: %s' % ''.join(sys.version.splitlines()) + ), + version='2.12', + collection_name='ansible.builtin', + ) + display.debug("starting run") + + sub = None + target = me.split('-') + if target[-1][0].isdigit(): + # Remove any version or python version info as downstreams + # sometimes add that + target = target[:-1] + + if len(target) > 1: + sub = target[1] + myclass = "%sCLI" % sub.capitalize() + elif target[0] == 'ansible': + sub = 'adhoc' + myclass = 'AdHocCLI' + else: + raise AnsibleError("Unknown Ansible alias: %s" % me) + + try: + mycli = getattr(__import__("ansible.cli.%s" % sub, fromlist=[myclass]), myclass) + except ImportError as e: + # ImportError members have changed in py3 + if 'msg' in dir(e): + msg = e.msg + else: + msg = e.message + if msg.endswith(' %s' % sub): + raise AnsibleError("Ansible sub-program not implemented: %s" % me) + else: + raise + + b_ansible_dir = os.path.expanduser(os.path.expandvars(b"~/.ansible")) + try: + os.mkdir(b_ansible_dir, 0o700) + except OSError as exc: + if exc.errno != errno.EEXIST: + display.warning("Failed to create the directory '%s': %s" + % (to_text(b_ansible_dir, errors='surrogate_or_replace'), + to_text(exc, errors='surrogate_or_replace'))) + else: + display.debug("Created the '%s' directory" % to_text(b_ansible_dir, errors='surrogate_or_replace')) + + try: + args = [to_text(a, errors='surrogate_or_strict') for a in sys.argv] + except UnicodeError: + display.error('Command line args are not in utf-8, unable to continue. Ansible currently only understands utf-8') + display.display(u"The full traceback was:\n\n%s" % to_text(traceback.format_exc())) + exit_code = 6 + else: + cli = mycli(args) + exit_code = cli.run() + + except AnsibleOptionsError as e: + cli.parser.print_help() + display.error(to_text(e), wrap_text=False) + exit_code = 5 + except AnsibleParserError as e: + display.error(to_text(e), wrap_text=False) + exit_code = 4 +# TQM takes care of these, but leaving comment to reserve the exit codes +# except AnsibleHostUnreachable as e: +# display.error(str(e)) +# exit_code = 3 +# except AnsibleHostFailed as e: +# display.error(str(e)) +# exit_code = 2 + except AnsibleError as e: + display.error(to_text(e), wrap_text=False) + exit_code = 1 + except KeyboardInterrupt: + display.error("User interrupted execution") + exit_code = 99 + except Exception as e: + if C.DEFAULT_DEBUG: + # Show raw stacktraces in debug mode, It also allow pdb to + # enter post mortem mode. + raise + have_cli_options = bool(context.CLIARGS) + display.error("Unexpected Exception, this is probably a bug: %s" % to_text(e), wrap_text=False) + if not have_cli_options or have_cli_options and context.CLIARGS['verbosity'] > 2: + log_only = False + if hasattr(e, 'orig_exc'): + display.vvv('\nexception type: %s' % to_text(type(e.orig_exc))) + why = to_text(e.orig_exc) + if to_text(e) != why: + display.vvv('\noriginal msg: %s' % why) + else: + display.display("to see the full traceback, use -vvv") + log_only = True + display.display(u"the full traceback was:\n\n%s" % to_text(traceback.format_exc()), log_only=log_only) + exit_code = 250 + + sys.exit(exit_code) diff --git a/.local/bin/ansible-config b/.local/bin/ansible-config new file mode 120000 index 0000000..cabb1f5 --- /dev/null +++ b/.local/bin/ansible-config @@ -0,0 +1 @@ +ansible \ No newline at end of file diff --git a/.local/bin/ansible-connection b/.local/bin/ansible-connection new file mode 100755 index 0000000..2dd431c --- /dev/null +++ b/.local/bin/ansible-connection @@ -0,0 +1,342 @@ +#!/usr/bin/python3 +# Copyright: (c) 2017, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +from __future__ import (absolute_import, division, print_function) + +__metaclass__ = type +__requires__ = ['ansible_core'] + + +import fcntl +import hashlib +import os +import signal +import socket +import sys +import time +import traceback +import errno +import json + +from contextlib import contextmanager + +from ansible import constants as C +from ansible.module_utils._text import to_bytes, to_text +from ansible.module_utils.six import PY3 +from ansible.module_utils.six.moves import cPickle, StringIO +from ansible.module_utils.connection import Connection, ConnectionError, send_data, recv_data +from ansible.module_utils.service import fork_process +from ansible.parsing.ajson import AnsibleJSONEncoder, AnsibleJSONDecoder +from ansible.playbook.play_context import PlayContext +from ansible.plugins.loader import connection_loader +from ansible.utils.path import unfrackpath, makedirs_safe +from ansible.utils.display import Display +from ansible.utils.jsonrpc import JsonRpcServer + + +def read_stream(byte_stream): + size = int(byte_stream.readline().strip()) + + data = byte_stream.read(size) + if len(data) < size: + raise Exception("EOF found before data was complete") + + data_hash = to_text(byte_stream.readline().strip()) + if data_hash != hashlib.sha1(data).hexdigest(): + raise Exception("Read {0} bytes, but data did not match checksum".format(size)) + + # restore escaped loose \r characters + data = data.replace(br'\r', b'\r') + + return data + + +@contextmanager +def file_lock(lock_path): + """ + Uses contextmanager to create and release a file lock based on the + given path. This allows us to create locks using `with file_lock()` + to prevent deadlocks related to failure to unlock properly. + """ + + lock_fd = os.open(lock_path, os.O_RDWR | os.O_CREAT, 0o600) + fcntl.lockf(lock_fd, fcntl.LOCK_EX) + yield + fcntl.lockf(lock_fd, fcntl.LOCK_UN) + os.close(lock_fd) + + +class ConnectionProcess(object): + ''' + The connection process wraps around a Connection object that manages + the connection to a remote device that persists over the playbook + ''' + def __init__(self, fd, play_context, socket_path, original_path, task_uuid=None, ansible_playbook_pid=None): + self.play_context = play_context + self.socket_path = socket_path + self.original_path = original_path + self._task_uuid = task_uuid + + self.fd = fd + self.exception = None + + self.srv = JsonRpcServer() + self.sock = None + + self.connection = None + self._ansible_playbook_pid = ansible_playbook_pid + + def start(self, variables): + try: + messages = list() + result = {} + + messages.append(('vvvv', 'control socket path is %s' % self.socket_path)) + + # If this is a relative path (~ gets expanded later) then plug the + # key's path on to the directory we originally came from, so we can + # find it now that our cwd is / + if self.play_context.private_key_file and self.play_context.private_key_file[0] not in '~/': + self.play_context.private_key_file = os.path.join(self.original_path, self.play_context.private_key_file) + self.connection = connection_loader.get(self.play_context.connection, self.play_context, '/dev/null', + task_uuid=self._task_uuid, ansible_playbook_pid=self._ansible_playbook_pid) + self.connection.set_options(var_options=variables) + + self.connection._socket_path = self.socket_path + self.srv.register(self.connection) + messages.extend([('vvvv', msg) for msg in sys.stdout.getvalue().splitlines()]) + + self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + self.sock.bind(self.socket_path) + self.sock.listen(1) + messages.append(('vvvv', 'local domain socket listeners started successfully')) + except Exception as exc: + messages.extend(self.connection.pop_messages()) + result['error'] = to_text(exc) + result['exception'] = traceback.format_exc() + finally: + result['messages'] = messages + self.fd.write(json.dumps(result, cls=AnsibleJSONEncoder)) + self.fd.close() + + def run(self): + try: + while not self.connection._conn_closed: + signal.signal(signal.SIGALRM, self.connect_timeout) + signal.signal(signal.SIGTERM, self.handler) + signal.alarm(self.connection.get_option('persistent_connect_timeout')) + + self.exception = None + (s, addr) = self.sock.accept() + signal.alarm(0) + signal.signal(signal.SIGALRM, self.command_timeout) + while True: + data = recv_data(s) + if not data: + break + log_messages = self.connection.get_option('persistent_log_messages') + + if log_messages: + display.display("jsonrpc request: %s" % data, log_only=True) + + request = json.loads(to_text(data, errors='surrogate_or_strict')) + if request.get('method') == "exec_command" and not self.connection.connected: + self.connection._connect() + + signal.alarm(self.connection.get_option('persistent_command_timeout')) + + resp = self.srv.handle_request(data) + signal.alarm(0) + + if log_messages: + display.display("jsonrpc response: %s" % resp, log_only=True) + + send_data(s, to_bytes(resp)) + + s.close() + + except Exception as e: + # socket.accept() will raise EINTR if the socket.close() is called + if hasattr(e, 'errno'): + if e.errno != errno.EINTR: + self.exception = traceback.format_exc() + else: + self.exception = traceback.format_exc() + + finally: + # allow time for any exception msg send over socket to receive at other end before shutting down + time.sleep(0.1) + + # when done, close the connection properly and cleanup the socket file so it can be recreated + self.shutdown() + + def connect_timeout(self, signum, frame): + msg = 'persistent connection idle timeout triggered, timeout value is %s secs.\nSee the timeout setting options in the Network Debug and ' \ + 'Troubleshooting Guide.' % self.connection.get_option('persistent_connect_timeout') + display.display(msg, log_only=True) + raise Exception(msg) + + def command_timeout(self, signum, frame): + msg = 'command timeout triggered, timeout value is %s secs.\nSee the timeout setting options in the Network Debug and Troubleshooting Guide.'\ + % self.connection.get_option('persistent_command_timeout') + display.display(msg, log_only=True) + raise Exception(msg) + + def handler(self, signum, frame): + msg = 'signal handler called with signal %s.' % signum + display.display(msg, log_only=True) + raise Exception(msg) + + def shutdown(self): + """ Shuts down the local domain socket + """ + lock_path = unfrackpath("%s/.ansible_pc_lock_%s" % os.path.split(self.socket_path)) + if os.path.exists(self.socket_path): + try: + if self.sock: + self.sock.close() + if self.connection: + self.connection.close() + if self.connection.get_option("persistent_log_messages"): + for _level, message in self.connection.pop_messages(): + display.display(message, log_only=True) + except Exception: + pass + finally: + if os.path.exists(self.socket_path): + os.remove(self.socket_path) + setattr(self.connection, '_socket_path', None) + setattr(self.connection, '_connected', False) + + if os.path.exists(lock_path): + os.remove(lock_path) + + display.display('shutdown complete', log_only=True) + + +def main(): + """ Called to initiate the connect to the remote device + """ + rc = 0 + result = {} + messages = list() + socket_path = None + + # Need stdin as a byte stream + if PY3: + stdin = sys.stdin.buffer + else: + stdin = sys.stdin + + # Note: update the below log capture code after Display.display() is refactored. + saved_stdout = sys.stdout + sys.stdout = StringIO() + + try: + # read the play context data via stdin, which means depickling it + vars_data = read_stream(stdin) + init_data = read_stream(stdin) + + if PY3: + pc_data = cPickle.loads(init_data, encoding='bytes') + variables = cPickle.loads(vars_data, encoding='bytes') + else: + pc_data = cPickle.loads(init_data) + variables = cPickle.loads(vars_data) + + play_context = PlayContext() + play_context.deserialize(pc_data) + display.verbosity = play_context.verbosity + + except Exception as e: + rc = 1 + result.update({ + 'error': to_text(e), + 'exception': traceback.format_exc() + }) + + if rc == 0: + ssh = connection_loader.get('ssh', class_only=True) + ansible_playbook_pid = sys.argv[1] + task_uuid = sys.argv[2] + cp = ssh._create_control_path(play_context.remote_addr, play_context.port, play_context.remote_user, play_context.connection, ansible_playbook_pid) + # create the persistent connection dir if need be and create the paths + # which we will be using later + tmp_path = unfrackpath(C.PERSISTENT_CONTROL_PATH_DIR) + makedirs_safe(tmp_path) + + socket_path = unfrackpath(cp % dict(directory=tmp_path)) + lock_path = unfrackpath("%s/.ansible_pc_lock_%s" % os.path.split(socket_path)) + + with file_lock(lock_path): + if not os.path.exists(socket_path): + messages.append(('vvvv', 'local domain socket does not exist, starting it')) + original_path = os.getcwd() + r, w = os.pipe() + pid = fork_process() + + if pid == 0: + try: + os.close(r) + wfd = os.fdopen(w, 'w') + process = ConnectionProcess(wfd, play_context, socket_path, original_path, task_uuid, ansible_playbook_pid) + process.start(variables) + except Exception: + messages.append(('error', traceback.format_exc())) + rc = 1 + + if rc == 0: + process.run() + else: + process.shutdown() + + sys.exit(rc) + + else: + os.close(w) + rfd = os.fdopen(r, 'r') + data = json.loads(rfd.read(), cls=AnsibleJSONDecoder) + messages.extend(data.pop('messages')) + result.update(data) + + else: + messages.append(('vvvv', 'found existing local domain socket, using it!')) + conn = Connection(socket_path) + conn.set_options(var_options=variables) + pc_data = to_text(init_data) + try: + conn.update_play_context(pc_data) + conn.set_check_prompt(task_uuid) + except Exception as exc: + # Only network_cli has update_play context and set_check_prompt, so missing this is + # not fatal e.g. netconf + if isinstance(exc, ConnectionError) and getattr(exc, 'code', None) == -32601: + pass + else: + result.update({ + 'error': to_text(exc), + 'exception': traceback.format_exc() + }) + + if os.path.exists(socket_path): + messages.extend(Connection(socket_path).pop_messages()) + messages.append(('vvvv', sys.stdout.getvalue())) + result.update({ + 'messages': messages, + 'socket_path': socket_path + }) + + sys.stdout = saved_stdout + if 'exception' in result: + rc = 1 + sys.stderr.write(json.dumps(result, cls=AnsibleJSONEncoder)) + else: + rc = 0 + sys.stdout.write(json.dumps(result, cls=AnsibleJSONEncoder)) + + sys.exit(rc) + + +if __name__ == '__main__': + display = Display() + main() diff --git a/.local/bin/ansible-console b/.local/bin/ansible-console new file mode 120000 index 0000000..cabb1f5 --- /dev/null +++ b/.local/bin/ansible-console @@ -0,0 +1 @@ +ansible \ No newline at end of file diff --git a/.local/bin/ansible-doc b/.local/bin/ansible-doc new file mode 120000 index 0000000..cabb1f5 --- /dev/null +++ b/.local/bin/ansible-doc @@ -0,0 +1 @@ +ansible \ No newline at end of file diff --git a/.local/bin/ansible-galaxy b/.local/bin/ansible-galaxy new file mode 120000 index 0000000..cabb1f5 --- /dev/null +++ b/.local/bin/ansible-galaxy @@ -0,0 +1 @@ +ansible \ No newline at end of file diff --git a/.local/bin/ansible-inventory b/.local/bin/ansible-inventory new file mode 120000 index 0000000..cabb1f5 --- /dev/null +++ b/.local/bin/ansible-inventory @@ -0,0 +1 @@ +ansible \ No newline at end of file diff --git a/.local/bin/ansible-playbook b/.local/bin/ansible-playbook new file mode 120000 index 0000000..cabb1f5 --- /dev/null +++ b/.local/bin/ansible-playbook @@ -0,0 +1 @@ +ansible \ No newline at end of file diff --git a/.local/bin/ansible-pull b/.local/bin/ansible-pull new file mode 120000 index 0000000..cabb1f5 --- /dev/null +++ b/.local/bin/ansible-pull @@ -0,0 +1 @@ +ansible \ No newline at end of file diff --git a/.local/bin/ansible-test b/.local/bin/ansible-test new file mode 100755 index 0000000..b96a777 --- /dev/null +++ b/.local/bin/ansible-test @@ -0,0 +1,28 @@ +#!/usr/bin/python3 +# PYTHON_ARGCOMPLETE_OK +"""Command line entry point for ansible-test.""" + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import os +import sys + + +def main(): + """Main program entry point.""" + ansible_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + source_root = os.path.join(ansible_root, 'test', 'lib') + + if os.path.exists(os.path.join(source_root, 'ansible_test', '_internal', 'cli.py')): + # running from source, use that version of ansible-test instead of any version that may already be installed + sys.path.insert(0, source_root) + + # noinspection PyProtectedMember + from ansible_test._internal.cli import main as cli_main + + cli_main() + + +if __name__ == '__main__': + main() diff --git a/.local/bin/ansible-vault b/.local/bin/ansible-vault new file mode 120000 index 0000000..cabb1f5 --- /dev/null +++ b/.local/bin/ansible-vault @@ -0,0 +1 @@ +ansible \ No newline at end of file