Beispiel #1
0
    def test_connect_does_not_prompt_password_when_ssh_raises_channel_exception(self):
        def raise_channel_exception_once(*args, **kwargs):
            if raise_channel_exception_once.should_raise_channel_exception:
                raise_channel_exception_once.should_raise_channel_exception = False
                raise ssh.ChannelException(2, 'Connect failed')
        raise_channel_exception_once.should_raise_channel_exception = True

        def generate_fake_client():
            fake_client = Fake('SSHClient', allows_any_call=True, expect_call=True)
            fake_client.provides('connect').calls(raise_channel_exception_once)
            return fake_client

        fake_ssh = Fake('ssh', allows_any_call=True)
        fake_ssh.provides('SSHClient').calls(generate_fake_client)
        # We need the real exceptions here to preserve the inheritence structure
        fake_ssh.SSHException = ssh.SSHException
        fake_ssh.ChannelException = ssh.ChannelException
        patched_connect = patch_object('fabric.network', 'ssh', fake_ssh)
        patched_password = patch_object('fabric.network', 'prompt_for_password', Fake('prompt_for_password', callable = True).times_called(0))
        try:
            connect('user', 'localhost', 22, HostConnectionCache())
        finally:
            # Restore ssh
            patched_connect.restore()
            patched_password.restore()
Beispiel #2
0
    def test_connect_does_not_prompt_password_when_ssh_raises_channel_exception(
            self):
        def raise_channel_exception_once(*args, **kwargs):
            if raise_channel_exception_once.should_raise_channel_exception:
                raise_channel_exception_once.should_raise_channel_exception = False
                raise ssh.ChannelException(2, 'Connect failed')

        raise_channel_exception_once.should_raise_channel_exception = True

        def generate_fake_client():
            fake_client = Fake('SSHClient',
                               allows_any_call=True,
                               expect_call=True)
            fake_client.provides('connect').calls(raise_channel_exception_once)
            return fake_client

        fake_ssh = Fake('ssh', allows_any_call=True)
        fake_ssh.provides('SSHClient').calls(generate_fake_client)
        # We need the real exceptions here to preserve the inheritence structure
        fake_ssh.SSHException = ssh.SSHException
        fake_ssh.ChannelException = ssh.ChannelException
        patched_connect = patch_object('fabric.network', 'ssh', fake_ssh)
        patched_password = patch_object(
            'fabric.network', 'prompt_for_password',
            Fake('prompt_for_password', callable=True).times_called(0))
        try:
            connect('user', 'localhost', 22, HostConnectionCache())
        finally:
            # Restore ssh
            patched_connect.restore()
            patched_password.restore()
Beispiel #3
0
def disconnect():
    yield
    host = env.host_string
    if host and host in connections:
        normalized_host = normalize(host)
        connections[host].get_transport().close()
        connect(normalized_host[0], normalized_host[1], normalized_host[2],
                HostConnectionCache())
def fetch_files(file_list):
    fetch_successes = list()
    fetch_file_list = list()

    # determine what's already here
    for remote_filename in file_list:
        local_filename = lcu.remote_to_local_filename(remote_filename)
        if is_file(local_filename):
            fetch_successes.append(True)
        else:
            fetch_file_list.append((remote_filename, local_filename))

    if fetch_file_list:
        fabric_env['key_filename'] = cl_conf.SCP_SECRET_KEYFILE

        # fetch what isn't already here
        with closing(fn.connect(cl_conf.SCP_USER, cl_conf.SCP_HOST, cl_conf.SCP_PORT, None)) as ssh:
            with closing(ssh.open_sftp()) as sftp:
                for remote_filename, local_filename in fetch_file_list:
                    with closing(sftp.open(remote_filename)) as r_file:
                        with open(local_filename, 'wb') as l_file:
                            logger("Fetching file: {} to: {}".format(remote_filename, local_filename))
                            try:
                                l_file.write(r_file.read())
                                fetch_successes.append(True)
                            except IOError:
                                fetch_successes.append(False)

    return fetch_successes
Beispiel #5
0
 def wcl(self):
     with closing(
             connect(self.user, self.host, self.port,
                     HostConnectionCache())) as ssh:
         stdin, stdout, stderr = ssh.exec_command('wc -l %s' %
                                                  self.filename)
         return int(
             stdout.readlines()[0].split()[0]) - (1 if self.header else 0)
Beispiel #6
0
    def wrapper( self, *args, **kwargs ):

        print 'wrapper'
        print kwargs

        with closing( connect( 'cloudmgr', '127.0.0.1', 22 ) ) as ssh:
            with closing( ssh.open_sftp() ) as sftp:
               return fct( self, os = sftp, *args, **kwargs )
Beispiel #7
0
 def rowns(self):
     with closing(
             connect(self.user, self.host, self.port,
                     HostConnectionCache())) as ssh:
         stdin, stdout, stderr = ssh.exec_command(
             'awk \'BEGIN{FS="%s"}{print $1}\' %s%s' %
             (self.sep, self.filename,
              '' if not self.header else ' | tail -n +2'))
         return [x.strip() for x in stdout.readlines()]
	def __enter__( self ):
		if self._identified_value.is_locale:
			modified_os = os
			modified_os.open = open
			return modified_os
		else:
		       	self._ssh  = connect( 'cloudmgr', self._identified_value.run_on_server, 22 )
               		self._sftp = self._ssh.open_sftp()
			return self._sftp
Beispiel #9
0
 def open(self, return_header=True):
     with closing(
             connect(self.user, self.host, self.port,
                     HostConnectionCache())) as ssh:
         with closing(ssh.open_sftp()) as sftp:
             with closing(sftp.open(self.filename)) as f:
                 if not return_header:
                     line = f.readline()
                 for line in f:
                     yield line
Beispiel #10
0
 def __enter__(self):
     if self._identified_value.is_locale:
         modified_os = os
         modified_os.open = open
         return modified_os
     else:
         self._ssh = connect('cloudmgr',
                             self._identified_value.run_on_server, 22)
         self._sftp = self._ssh.open_sftp()
         return self._sftp
	def get_intraparis_descriptor(cls, user, host, port, filenames_descriptor, d_applis ):
		"""get_intraparis_descriptor(cls, user, host, port, filename_descriptor, callbackprocess )
			user : username to remote connection
			host : hostname for remote connection
			port : remote port remote connection
			filenames_descriptor : remote fully qualified filename or list of remote fully qualified filenames
		"""
		with closing(connect(user, host, port)) as ssh:
	    		with closing(ssh.open_sftp()) as sftp:
				if filenames_descriptor.__class__==str: filenames_descriptor = [ filenames_descriptor ]
				for filename_descriptor in filenames_descriptor:
	         			with closing(sftp.open( filename_descriptor )) as f:
						for line in f:
							IntraparisDescriptorToDict.convert_intraparis_descriptor_feed_dict_from_line( line, d_applis )
Beispiel #12
0
    def connect(self, key):
        """
        Force a new connection to host string.
        """
        from fabric.state import env

        user, host, port = normalize(key)
        key = normalize_to_string(key)
        seek_gateway = True
        # break the loop when the host is gateway itself
        if env.gateway:
            seek_gateway = normalize_to_string(env.gateway) != key
            key = normalize_to_string(env.gateway)+'_'+key
        self[key] = connect(
            user, host, port, cache=self, seek_gateway=seek_gateway)
 def __getitem__(self, key):
     gw = s.env.get('gateway')
     if gw is None:
         return super(GatewayConnectionCache, self).__getitem__(key)
     
     gw_user, gw_host, gw_port = network.normalize(gw)
     if self._gw is None:
         # Normalize given key (i.e. obtain username and port, if not given)
         self._gw = network.connect(gw_user, gw_host, gw_port, None, False)
     
     # Normalize given key (i.e. obtain username and port, if not given)
     user, host, port = network.normalize(key)
     # Recombine for use as a key.
     real_key = network.join_host_strings(user, host, port)
     
     # If not found, create new connection and store it
     if real_key not in self:
         self[real_key] = connect_forward(self._gw, host, port, user)
     
     # Return the value either way
     return dict.__getitem__(self, real_key)
Beispiel #14
0
    def __getitem__(self, key):
        gw = s.env.get('gateway')
        if gw is None:
            return super(GatewayConnectionCache, self).__getitem__(key)

        gw_user, gw_host, gw_port = network.normalize(gw)
        if self._gw is None:
            # Normalize given key (i.e. obtain username and port, if not given)
            self._gw = network.connect(gw_user, gw_host, gw_port)

        # Normalize given key (i.e. obtain username and port, if not given)
        user, host, port = network.normalize(key)
        # Recombine for use as a key.
        real_key = network.join_host_strings(user, host, port)

        # If not found, create new connection and store it
        if real_key not in self:
            self[real_key] = connect_forward(self._gw, host, port, user)

        # Return the value either way
        return dict.__getitem__(self, real_key)