You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by pq...@apache.org on 2010/03/01 22:23:23 UTC
svn commit: r917724 - /incubator/libcloud/trunk/libcloud/ssh.py
Author: pquerna
Date: Mon Mar 1 21:23:23 2010
New Revision: 917724
URL: http://svn.apache.org/viewvc?rev=917724&view=rev
Log:
Create a plugable SSH Client class, with the Paramiko version mostly working.
Added:
incubator/libcloud/trunk/libcloud/ssh.py (with props)
Added: incubator/libcloud/trunk/libcloud/ssh.py
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/libcloud/ssh.py?rev=917724&view=auto
==============================================================================
--- incubator/libcloud/trunk/libcloud/ssh.py (added)
+++ incubator/libcloud/trunk/libcloud/ssh.py Mon Mar 1 21:23:23 2010
@@ -0,0 +1,117 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# libcloud.org licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""
+Wraps multiple ways to communicate over SSH
+"""
+have_paramiko = False
+
+try:
+ import paramiko
+ import socket
+ have_paramiko = True
+except ImportError:
+ pass
+
+from os.path import split as psplit
+
+class BaseSSHClient(object):
+ def __init__(self, hostname, port=22, username='root', password=None, key=None):
+ self.hostname = hostname
+ self.port = port
+ self.username = username
+ self.password = password
+ self.key = key
+
+ def connect(self):
+ raise NotImplementedError, \
+ 'connect not implemented for this ssh client'
+
+ def put(self, path, contents=None, chmod=None):
+ raise NotImplementedError, \
+ 'put not implemented for this ssh client'
+
+ def delete(self, path):
+ raise NotImplementedError, \
+ 'delete not implemented for this ssh client'
+
+ def run(self, cmd):
+ raise NotImplementedError, \
+ 'run not implemented for this ssh client'
+
+ def close(self):
+ raise NotImplementedError, \
+ 'close not implemented for this ssh client'
+
+class ParamikoSSHClient(BaseSSHClient):
+ def __init__(self, hostname, port=22, username='root', password=None, key=None):
+ super(ParamikoSSHClient, self).__init__(hostname, port, username, password, key)
+ self.client = paramiko.SSHClient()
+ self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
+
+ def connect(self):
+ conninfo = {'hostname': self.hostname,
+ 'port': self.port,
+ 'username': self.username,
+ 'password': self.password,
+ 'allow_agent': False,
+ 'look_for_keys': False}
+ self.client.connect(**conninfo)
+ return True
+
+ def put(self, path, contents=None, chmod=None):
+ sftp = self.client.open_sftp()
+ # less than ideal, but we need to mkdir stuff otherwise file() fails
+ head, tail = psplit(path)
+ if path[0] == "/":
+ sftp.chdir("/")
+ for part in head.split("/"):
+ if part != "":
+ try:
+ sftp.mkdir(part)
+ except IOError, e:
+ # so, there doens't seem to be a way to
+ # catch EEXIST consistently *sigh*
+ pass
+ sftp.chdir(part)
+ ak = sftp.file(tail, mode='w')
+ ak.write(contents)
+ if chmod is not None:
+ ak.chmod(chmod)
+ ak.close()
+ sftp.close()
+
+ def delete(self, path):
+ sftp = self.client.open_sftp()
+ sftp.unlink(path)
+ sftp.close()
+
+ def run(self, cmd):
+ stdin, stdout, stderr = self.client.exec_command(cmd)
+ stdin.close()
+ so = stdout.read()
+ se = stderr.read()
+ return [so, se]
+
+ def close(self):
+ self.client.close()
+
+class ShellOutSSHClient(BaseSSHClient):
+ # TODO: write this one
+ pass
+
+SSHClient = ParamikoSSHClient
+if not have_paramiko:
+ SSHClient = ShellOutSSHClient
Propchange: incubator/libcloud/trunk/libcloud/ssh.py
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/libcloud/trunk/libcloud/ssh.py
------------------------------------------------------------------------------
svn:keywords = Date Revision Author HeadURL Id
Propchange: incubator/libcloud/trunk/libcloud/ssh.py
------------------------------------------------------------------------------
svn:mime-type = text/plain