def _transfer_data(self, remote_path, data): """ Copies the module data out to the temporary module path. """ if isinstance(data, dict): data = jsonify(data) afd, afile = tempfile.mkstemp() afo = os.fdopen(afd, "wb") try: data = to_bytes(data, errors="strict") afo.write(data) except Exception as e: raise AnsibleError("failure writing module data to temporary file for transfer: %s" % str(e)) afo.flush() afo.close() try: self._transfer_file(afile, remote_path) finally: os.unlink(afile) return remote_path
def _transfer_data(self, remote_path, data): ''' Copies the module data out to the temporary module path. ''' if isinstance(data, dict): data = jsonify(data) afd, afile = tempfile.mkstemp() afo = os.fdopen(afd, 'w') try: data = to_bytes(data, errors='strict') afo.write(data) except Exception as e: #raise AnsibleError("failure encoding into utf-8: %s" % str(e)) raise AnsibleError("failure writing module data to temporary file for transfer: %s" % str(e)) afo.flush() afo.close() try: self._connection.put_file(afile, remote_path) finally: os.unlink(afile) return remote_path
def runtest(modfile, argspath, modname, module_style): """Test run a module, piping it's output for reporting.""" if module_style == 'ziploader': modfile, argspath = ziploader_setup(modfile, modname) os.system("chmod +x %s" % modfile) invoke = "%s" % (modfile) if argspath is not None: invoke = "%s %s" % (modfile, argspath) cmd = subprocess.Popen(invoke, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (out, err) = cmd.communicate() try: results = json.loads(out) except: print("*" * 35) print("INVALID OUTPUT FORMAT") print(out) traceback.print_exc() sys.exit(1) print("*" * 35) print("PARSED OUTPUT") print(jsonify(results, format=True)) return results
def _transfer_data(self, remote_path, data): ''' Copies the module data out to the temporary module path. ''' if isinstance(data, dict): data = jsonify(data) afd, afile = tempfile.mkstemp(dir=C.DEFAULT_LOCAL_TMP) afo = os.fdopen(afd, 'wb') try: data = to_bytes(data, errors='surrogate_or_strict') afo.write(data) except Exception as e: raise AnsibleError("failure writing module data to temporary file for transfer: %s" % to_native(e)) afo.flush() afo.close() try: self._transfer_file(afile, remote_path) finally: os.unlink(afile) return remote_path
def write_argsfile(argstring, json=False): """ Write args to a file for old-style module's use. """ argspath = os.path.expanduser("~/.ansible_test_module_arguments") argsfile = open(argspath, 'w') if json: args = parse_kv(argstring) argstring = jsonify(args) argsfile.write(argstring) argsfile.close() return argspath
def fetch_file(self, in_path, out_path): ''' save a remote file to the specified path ''' display.vvv("FETCH %s TO %s" % (in_path, out_path), host=self._play_context.remote_addr) data = dict(mode='fetch', in_path=in_path) data = jsonify(data) data = keyczar_encrypt(self.key, data) if self.send_data(data): raise AnsibleError("failed to initiate the file fetch with %s" % self._play_context.remote_addr) fh = open(to_bytes(out_path, errors='strict'), "w") try: bytes = 0 while True: response = self.recv_data() if not response: raise AnsibleError("Failed to get a response from %s" % self._play_context.remote_addr) response = keyczar_decrypt(self.key, response) response = json.loads(response) if response.get('failed', False): raise AnsibleError("Error during file fetch, aborting") out = base64.b64decode(response['data']) fh.write(out) bytes += len(out) # send an empty response back to signify we # received the last chunk without errors data = jsonify(dict()) data = keyczar_encrypt(self.key, data) if self.send_data(data): raise AnsibleError("failed to send ack during file fetch") if response.get('last', False): break finally: # we don't currently care about this final response, # we just receive it and drop it. It may be used at some # point in the future or we may just have the put/fetch # operations not send back a final response at all response = self.recv_data() display.vvv("FETCH wrote %d bytes to %s" % (bytes, out_path), host=self._play_context.remote_addr) fh.close()
def set(self, key, value): self._cache[key] = value cachefile = "%s/%s" % (self._cache_dir, key) try: f = codecs.open(cachefile, 'w', encoding='utf-8') except (OSError,IOError) as e: self._display.warning("error while trying to write to %s : %s" % (cachefile, to_bytes(e))) pass else: f.write(jsonify(value)) finally: f.close()
def _transfer_data(self, remote_path, data): """ Used by the base _execute_module(), and in <2.4 also by the template action module, and probably others. """ if isinstance(data, dict): data = jsonify(data) if not isinstance(data, bytes): data = to_bytes(data, errors='surrogate_or_strict') LOG.debug('_transfer_data(%r, %s ..%d bytes)', remote_path, type(data), len(data)) self._connection.put_data(remote_path, data) return remote_path
def put_file(self, in_path, out_path): ''' transfer a file from local to remote ''' display.vvv("PUT %s TO %s" % (in_path, out_path), host=self._play_context.remote_addr) in_path = to_bytes(in_path, errors='surrogate_or_strict') if not os.path.exists(in_path): raise AnsibleFileNotFound("file or module does not exist: %s" % in_path) fd = open(in_path, 'rb') fstat = os.stat(in_path) try: display.vvv("PUT file is %d bytes" % fstat.st_size, host=self._play_context.remote_addr) last = False while fd.tell() <= fstat.st_size and not last: display.vvvv("file position currently %ld, file size is %ld" % (fd.tell(), fstat.st_size), host=self._play_context.remote_addr) data = fd.read(CHUNK_SIZE) if fd.tell() >= fstat.st_size: last = True data = dict(mode='put', data=base64.b64encode(data), out_path=out_path, last=last) if self._play_context.become: data['user'] = self._play_context.become_user data = jsonify(data) data = keyczar_encrypt(self.key, data) if self.send_data(data): raise AnsibleError("failed to send the file to %s" % self._play_context.remote_addr) response = self.recv_data() if not response: raise AnsibleError("Failed to get a response from %s" % self._play_context.remote_addr) response = keyczar_decrypt(self.key, response) response = json.loads(response) if response.get('failed', False): raise AnsibleError("failed to put the file in the requested location") finally: fd.close() display.vvvv("waiting for final response after PUT", host=self._play_context.remote_addr) response = self.recv_data() if not response: raise AnsibleError("Failed to get a response from %s" % self._play_context.remote_addr) response = keyczar_decrypt(self.key, response) response = json.loads(response) if response.get('failed', False): raise AnsibleError("failed to put the file in the requested location")
def exec_command(self, cmd, in_data=None, sudoable=True): ''' run a command on the remote host ''' super(Connection, self).exec_command(cmd, in_data=in_data, sudoable=sudoable) if in_data: raise AnsibleError( "Internal Error: this module does not support optimized module pipelining" ) display.vvv("EXEC COMMAND %s" % cmd, host=self._play_context.remote_addr) data = dict( mode='command', cmd=cmd, executable=C.DEFAULT_EXECUTABLE, ) data = jsonify(data) data = keyczar_encrypt(self.key, data) if self.send_data(data): raise AnsibleError("Failed to send command to %s" % self._play_context.remote_addr) while True: # we loop here while waiting for the response, because a # long running command may cause us to receive keepalive packets # ({"pong":"true"}) rather than the response we want. response = self.recv_data() if not response: raise AnsibleError("Failed to get a response from %s" % self._play_context.remote_addr) response = keyczar_decrypt(self.key, response) response = json.loads(response) if "pong" in response: # it's a keepalive, go back to waiting display.vvvv("received a keepalive packet", host=self._play_context.remote_addr) continue else: display.vvvv("received the response", host=self._play_context.remote_addr) break return (response.get('rc', None), response.get('stdout', ''), response.get('stderr', ''))
def put_file(self, in_path, out_path): ''' transfer a file from local to remote ''' display.vvv("PUT %s TO %s" % (in_path, out_path), host=self._play_context.remote_addr) in_path = to_bytes(in_path, errors='strict') if not os.path.exists(in_path): raise AnsibleFileNotFound("file or module does not exist: %s" % in_path) fd = file(in_path, 'rb') fstat = os.stat(in_path) try: display.vvv("PUT file is %d bytes" % fstat.st_size, host=self._play_context.remote_addr) last = False while fd.tell() <= fstat.st_size and not last: display.vvvv("file position currently %ld, file size is %ld" % (fd.tell(), fstat.st_size), host=self._play_context.remote_addr) data = fd.read(CHUNK_SIZE) if fd.tell() >= fstat.st_size: last = True data = dict(mode='put', data=base64.b64encode(data), out_path=out_path, last=last) if self._play_context.become: data['user'] = self._play_context.become_user data = jsonify(data) data = keyczar_encrypt(self.key, data) if self.send_data(data): raise AnsibleError("failed to send the file to %s" % self._play_context.remote_addr) response = self.recv_data() if not response: raise AnsibleError("Failed to get a response from %s" % self._play_context.remote_addr) response = keyczar_decrypt(self.key, response) response = json.loads(response) if response.get('failed',False): raise AnsibleError("failed to put the file in the requested location") finally: fd.close() display.vvvv("waiting for final response after PUT", host=self._play_context.remote_addr) response = self.recv_data() if not response: raise AnsibleError("Failed to get a response from %s" % self._play_context.remote_addr) response = keyczar_decrypt(self.key, response) response = json.loads(response) if response.get('failed',False): raise AnsibleError("failed to put the file in the requested location")
def validate_user(self): ''' Checks the remote uid of the accelerated daemon vs. the one specified for this play and will cause the accel daemon to exit if they don't match ''' display.vvvv("sending request for validate_user", host=self._play_context.remote_addr) data = dict( mode='validate_user', username=self._play_context.remote_user, ) data = jsonify(data) data = keyczar_encrypt(self.key, data) if self.send_data(data): raise AnsibleError("Failed to send command to %s" % self._play_context.remote_addr) display.vvvv("waiting for validate_user response", host=self._play_context.remote_addr) while True: # we loop here while waiting for the response, because a # long running command may cause us to receive keepalive packets # ({"pong":"true"}) rather than the response we want. response = self.recv_data() if not response: raise AnsibleError("Failed to get a response from %s" % self._play_context.remote_addr) response = keyczar_decrypt(self.key, response) response = json.loads(response) if "pong" in response: # it's a keepalive, go back to waiting display.vvvv("received a keepalive packet", host=self._play_context.remote_addr) continue else: display.vvvv("received the validate_user response: %s" % (response), host=self._play_context.remote_addr) break if response.get('failed'): return False else: return response.get('rc') == 0
def set(self, key, value): self._cache[key] = value cachefile = "%s/%s" % (self._cache_dir, key) try: f = codecs.open(cachefile, 'w', encoding='utf-8') except (OSError, IOError) as e: display.warning("error while trying to write to %s : %s" % (cachefile, to_bytes(e))) pass else: f.write(jsonify(value, format=True)) finally: try: f.close() except UnboundLocalError: pass
def _transfer_data(self, remote_path, data): ''' Copies the module data out to the temporary module path. ''' if type(data) == dict: data = jsonify(data) afd, afile = tempfile.mkstemp() afo = os.fdopen(afd, 'w') try: if not isinstance(data, unicode): #ensure the data is valid UTF-8 data = data.decode('utf-8') else: data = data.encode('utf-8') afo.write(data) except Exception, e: raise AnsibleError("failure encoding into utf-8: %s" % str(e))
def set(self, key, value): self._cache[key] = value cachefile = "%s/%s" % (self._cache_dir, key) try: f = codecs.open(cachefile, "w", encoding="utf-8") except (OSError, IOError) as e: display.warning( "error in 'jsonfile' cache plugin while trying to write to %s : %s" % (cachefile, to_bytes(e)) ) pass else: f.write(jsonify(value, format=True)) finally: try: f.close() except UnboundLocalError: pass
def exec_command(self, cmd, in_data=None, sudoable=True): ''' run a command on the remote host ''' super(Connection, self).exec_command(cmd, in_data=in_data, sudoable=sudoable) # FIXME: #if sudoable and self..become and self.runner.become_method not in self.become_methods_supported: # raise AnsibleError("Internal Error: this module does not support running commands via %s" % self.runner.become_method) if in_data: raise AnsibleError("Internal Error: this module does not support optimized module pipelining") self._display.vvv("EXEC COMMAND %s" % cmd) data = dict( mode='command', cmd=cmd, executable=C.DEFAULT_EXECUTABLE, ) data = jsonify(data) data = keyczar_encrypt(self.key, data) if self.send_data(data): raise AnsibleError("Failed to send command to %s" % self._play_context.remote_addr) while True: # we loop here while waiting for the response, because a # long running command may cause us to receive keepalive packets # ({"pong":"true"}) rather than the response we want. response = self.recv_data() if not response: raise AnsibleError("Failed to get a response from %s" % self._play_context.remote_addr) response = keyczar_decrypt(self.key, response) response = json.loads(response) if "pong" in response: # it's a keepalive, go back to waiting self._display.vvvv("%s: received a keepalive packet" % self._play_context.remote_addr) continue else: self._display.vvvv("%s: received the response" % self._play_context.remote_addr) break return (response.get('rc', None), response.get('stdout', ''), response.get('stderr', ''))
def _transfer_data(self, remote_path, data): ''' Copies the module data out to the temporary module path. ''' if type(data) == dict: data = jsonify(data) afd, afile = tempfile.mkstemp() afo = os.fdopen(afd, 'w') try: # FIXME: is this still necessary? #if not isinstance(data, unicode): # #ensure the data is valid UTF-8 # data = data.decode('utf-8') #else: # data = data.encode('utf-8') afo.write(data) except Exception, e: #raise AnsibleError("failure encoding into utf-8: %s" % str(e)) raise AnsibleError("failure writing module data to temporary file for transfer: %s" % str(e))
def runtest(modfile, argspath, modname, module_style, interpreters): """Test run a module, piping it's output for reporting.""" invoke = "" if module_style == 'ansiballz': modfile, argspath = ansiballz_setup(modfile, modname, interpreters) if 'ansible_python_interpreter' in interpreters: invoke = "%s " % interpreters['ansible_python_interpreter'] os.system("chmod +x %s" % modfile) invoke = "%s%s" % (invoke, modfile) if argspath is not None: invoke = "%s %s" % (invoke, argspath) cmd = subprocess.Popen(invoke, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (out, err) = cmd.communicate() out, err = to_text(out), to_text(err) try: print("*" * 35) print("RAW OUTPUT") print(out) print(err) results = json.loads(out) except Exception: print("*" * 35) print("INVALID OUTPUT FORMAT") print(out) traceback.print_exc() sys.exit(1) print("*" * 35) print("PARSED OUTPUT") print(jsonify(results, format=True))
def _dump(self, value, filepath): with codecs.open(filepath, 'w', encoding='utf-8') as f: f.write(jsonify(value, format=True))
raise KeyError finally: f.close() def set(self, key, value): self._cache[key] = value cachefile = "%s/%s" % (self._cache_dir, key) try: f = codecs.open(cachefile, 'w', encoding='utf-8') except (OSError,IOError), e: self._display.warning("error while trying to write to %s : %s" % (cachefile, str(e))) pass else: f.write(jsonify(value)) finally: f.close() def has_expired(self, key): cachefile = "%s/%s" % (self._cache_dir, key) try: st = os.stat(cachefile) except (OSError,IOError), e: if e.errno == errno.ENOENT: return False else: self._display.warning("error while trying to stat %s : %s" % (cachefile, str(e))) pass
def test_jsonify_empty(self): self.assertEqual(jsonify(None), '{}')
def test_jsonify_unicode(self): self.assertEqual(jsonify(dict(toshio=u'くらとみ')), u'{"toshio": "くらとみ"}')
def test_jsonify_simple_format(self): res = jsonify(dict(a=1, b=2, c=3), format=True) cleaned = "".join([x.strip() for x in res.splitlines()]) self.assertEqual(cleaned, '{"a": 1,"b": 2,"c": 3}')
def test_jsonify_simple(self): self.assertEqual(jsonify(dict(a=1, b=2, c=3)), '{"a": 1, "b": 2, "c": 3}')