forked from ungleich-public/cdist
		
	Run executable [remote] scripts directly, allowing for transparent shebang support [POLYGLOT]
This commit is contained in:
		
					parent
					
						
							
								cbb9bb165a
							
						
					
				
			
			
				commit
				
					
						ef3f075650
					
				
			
		
					 1 changed files with 33 additions and 5 deletions
				
			
		| 
						 | 
					@ -216,18 +216,46 @@ class Remote:
 | 
				
			||||||
            _wrap_addr(self.target_host[0]), destination)])
 | 
					            _wrap_addr(self.target_host[0]), destination)])
 | 
				
			||||||
        self._run_command(command)
 | 
					        self._run_command(command)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def check_if_executable(self, path):
 | 
				
			||||||
 | 
					        """Check if the given remote resource is "executable",
 | 
				
			||||||
 | 
					        (i.e. has its execute bit set).
 | 
				
			||||||
 | 
					        Return True/False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        self.log.trace("Remote check if executable : %s", path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # XXX: Too bad we can't just check the returned status.
 | 
				
			||||||
 | 
					        # Hence the dance with "echo TRUE/FALSE"
 | 
				
			||||||
 | 
					        chk = " ".join([
 | 
				
			||||||
 | 
					                'if [ -f "{path}" ] && [ -x "{path}" ] ',
 | 
				
			||||||
 | 
					                '; then echo TRUE ; else echo FALSE ; fi']).format(path=path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        out = self.run(['/bin/sh', '-c', "'" + chk + "'"],
 | 
				
			||||||
 | 
					                    env=None, return_output=True) or ""
 | 
				
			||||||
 | 
					        return out.strip() == 'TRUE'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def run_script(self, script, env=None, return_output=False, stdout=None,
 | 
					    def run_script(self, script, env=None, return_output=False, stdout=None,
 | 
				
			||||||
                   stderr=None):
 | 
					                   stderr=None):
 | 
				
			||||||
        """Run the given script with the given environment on the remote side.
 | 
					        """Run the given script with the given environment on the remote side.
 | 
				
			||||||
        Return the output as a string.
 | 
					        Return the output as a string.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					        command = [script]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        command = [
 | 
					        if self.check_if_executable(script):
 | 
				
			||||||
            self.configuration.get('remote_shell', "/bin/sh"),
 | 
					            # Allow transparent shebang support for "executable" scripts
 | 
				
			||||||
            "-e"
 | 
					            self.log.debug(
 | 
				
			||||||
        ]
 | 
					                '%-70s : Remote script is executable, '
 | 
				
			||||||
        command.append(script)
 | 
					                        + 'running it directly', script)
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            # FIXME: Who knows what "-e" means for an arbitrary remote_shell ?
 | 
				
			||||||
 | 
					            # Keeping the old behavior (of always adding "-e") for the moment.
 | 
				
			||||||
 | 
					            shell = [self.configuration.get('remote_shell', "/bin/sh"), "-e"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            command = shell + command
 | 
				
			||||||
 | 
					            self.log.debug(
 | 
				
			||||||
 | 
					                '%-70s : Remote script is NOT executable, '
 | 
				
			||||||
 | 
					                        + 'running it with %s', script, " ".join(shell))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return self.run(command, env=env, return_output=return_output,
 | 
					        return self.run(command, env=env, return_output=return_output,
 | 
				
			||||||
                        stdout=stdout, stderr=stderr)
 | 
					                        stdout=stdout, stderr=stderr)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue