Permalink
Browse files

Remove ssh module from snapcraft

Moved it to the only consumer, the tests.

LP: #1533400
  • Loading branch information...
1 parent b36c094 commit 143579fb4b68c2d79846c9b6de0a837a8f40bb54 @sergiusens committed Jan 12, 2016
Showing with 37 additions and 59 deletions.
  1. +37 −3 examples_tests/tests.py
  2. +0 −56 snapcraft/ssh.py
View
@@ -1,6 +1,6 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
-# Copyright (C) 2015 Canonical Ltd
+# Copyright (C) 2015, 2016 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
@@ -24,7 +24,6 @@
import time
import testscenarios
-from snapcraft import ssh
logger = logging.getLogger(__name__)
@@ -78,10 +77,45 @@ def _get_ssh_options():
return [
'-o', 'UserKnownHostsFile=/dev/null',
'-o', 'StrictHostKeyChecking=no',
- '-i', ssh.get_latest_private_key()
+ '-i', _get_latest_ssh_private_key()
]
+def _get_latest_ssh_private_key():
+ """Return the latest private key in ~/.ssh.
+
+ :returns:
+ Path of the most-recently-modified private SSH key
+ :raises LookupError:
+ If no such key was found.
+
+ This function tries to mimic the logic found in ``ubuntu-device-flash``. It
+ will look for the most recently modified private key in the users' SSH
+ configuration directory.
+ """
+ candidates = []
+ ssh_dir = os.path.expanduser('~/.ssh/')
+ for filename in os.listdir(ssh_dir):
+ # Skip public keys, we want the private key
+ if filename.endswith('.pub'):
+ continue
+ ssh_key = os.path.join(ssh_dir, filename)
+ # Skip non-files
+ if not os.path.isfile(ssh_key):
+ continue
+ # Ensure that it is a real ssh key
+ with open(ssh_key, 'rb') as stream:
+ if stream.readline() != b'-----BEGIN RSA PRIVATE KEY-----\n':
+ continue
+ candidates.append(ssh_key)
+ # Sort the keys by modification time, pick the most recent key
+ candidates.sort(key=lambda f: os.stat(f).st_mtime, reverse=True)
+ logger.debug('Available ssh public keys: %r', candidates)
+ if not candidates:
+ raise LookupError('Unable to find any private ssh key')
+ return candidates[0]
+
+
def _copy_file_through_ssh(ip, port, user, local_file_path, remote_file_path):
scp_command = ['scp', '-P', port]
scp_command.extend(_get_ssh_options())
View
@@ -1,56 +0,0 @@
-# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
-#
-# Copyright (C) 2015 Canonical Ltd
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License version 3 as
-# published by the Free Software Foundation.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-import logging
-import os
-
-
-logger = logging.getLogger(__name__)
-
-
-def get_latest_private_key():
- """Return the latest private key in ~/.ssh.
-
- :returns:
- Path of the most-recently-modified private SSH key
- :raises LookupError:
- If no such key was found.
-
- This function tries to mimic the logic found in ``ubuntu-device-flash``. It
- will look for the most recently modified private key in the users' SSH
- configuration directory.
- """
- candidates = []
- ssh_dir = os.path.expanduser('~/.ssh/')
- for filename in os.listdir(ssh_dir):
- # Skip public keys, we want the private key
- if filename.endswith('.pub'):
- continue
- ssh_key = os.path.join(ssh_dir, filename)
- # Skip non-files
- if not os.path.isfile(ssh_key):
- continue
- # Ensure that it is a real ssh key
- with open(ssh_key, 'rb') as stream:
- if stream.readline() != b'-----BEGIN RSA PRIVATE KEY-----\n':
- continue
- candidates.append(ssh_key)
- # Sort the keys by modification time, pick the most recent key
- candidates.sort(key=lambda f: os.stat(f).st_mtime, reverse=True)
- logger.debug('Available ssh public keys: %r', candidates)
- if not candidates:
- raise LookupError('Unable to find any private ssh key')
- return candidates[0]

0 comments on commit 143579f

Please sign in to comment.