def _replace_str_in_remote_file(self, filename, origStr, newStr): s = '' client = self._ssh.open_sftp() #check that file exists try: client.stat(filename) client.close() except IOError: LOG.debug( "File to edit doesn't exist: {0}".format(filename)) return # copy the file locally data = remote.read_file_from(self._ssh.open_sftp(), filename) # read thru lines and replace origStr with newStr for line in six.StringIO(data): if origStr in line: if newStr != '': s += line.replace(origStr, newStr) else: s += line # write the file back out remote.write_file_to(self._ssh.open_sftp(), filename, s)
def _configure_ambari_server_api_port(self, port): # do nothing if port is not specified or is default if port is None or port == 8080: return ambari_config_file = '/etc/ambari-server/conf/ambari.properties' LOG.debug('Configuring Ambari Server API port: {0}'.format(port)) # read the current contents data = remote.read_file_from(self._ssh.open_sftp(), ambari_config_file) data = '{0}\nclient.api.port={1}\n'.format(data, port) # write the file back remote.write_file_to(self._ssh.open_sftp(), ambari_config_file, data)
def update_hosts_file(self, hosts): # read the hosts file data = remote.read_file_from(self._ssh.open_sftp(), '/etc/hosts') output = six.StringIO() for host in hosts: output.write('{0} {1}\n'.format(host.instance.internal_ip, host.instance.fqdn)) output.write('\n') # add the previous file contents output.write(data) # write the file back LOG.debug("updating hosts file with the following:") LOG.debug(output.getvalue()) remote.write_file_to(self._ssh.open_sftp(), '/etc/hosts', output.getvalue())
def write_file_to(self, host, remote_file, data): with contextlib.closing(self.ssh_connection(host)) as ssh: return remote.write_file_to(ssh.open_sftp(), remote_file, data)
def write_file_to(self, host, remote_file, data, node_username): with contextlib.closing(self.ssh_connection(host, node_username)) as ssh: return remote.write_file_to(ssh.open_sftp(), remote_file, data)