Skip to content

Commit a2e1430

Browse files
committed
[WIP] ssh backend
1 parent 0392392 commit a2e1430

File tree

10 files changed

+79
-10
lines changed

10 files changed

+79
-10
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,3 @@ coverage.xml
5656
Session.vim
5757
.netrwhist
5858
*~
59-
60-
test.py

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "specinfra"]
2+
path = specinfra
3+
url = [email protected]:libspecinfra/specinfra.git

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: rust
2+
rust:
3+
- stable
4+
- beta
5+
- nightly
6+
matrix:
7+
allow_failures:
8+
- rust: nightly
9+
script:
10+
- cd specinfra && cargo build --release

build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
docker run --rm --user "$(id -u)":"$(id -g)" -v "$PWD/specinfra":/usr/src/specinfra -w /usr/src/specinfra rust cargo build --release
2+
cp specinfra/target/release/libspecinfra.so ./libspecinfra/

libspecinfra/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import sys
1010
import ctypes
11+
import os
1112
import libspecinfra.resource
1213

1314
from libspecinfra.structures import SpecinfraS, BackendWrapperS
@@ -18,10 +19,11 @@ def load():
1819
global library
1920

2021
if library is None:
22+
libdir = os.path.dirname(os.path.abspath(__file__))
2123
prefix = {'win32': ''}.get(sys.platform, 'lib')
2224
extension = {'darwin': '.dylib', 'win32': '.dll'}.get(sys.platform, '.so')
23-
library = ctypes.cdll.LoadLibrary(
24-
'{}specinfra{}'.format(prefix, extension))
25+
libpath = os.path.join(libdir, '{}specinfra{}'.format(prefix, extension))
26+
library = ctypes.cdll.LoadLibrary(libpath)
2527

2628
return library
2729

libspecinfra/backend/__init__.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ctypes
22
import libspecinfra
3-
from libspecinfra.structures import BackendWrapperS
3+
from libspecinfra.structures import BackendWrapperS, BackendWrapperS ,SSHBuilderS
44

55

66
class Direct(object):
@@ -16,12 +16,40 @@ def __exit__(self, exc_type, exc_value, traceback):
1616

1717
class SSH(object):
1818

19-
def __init__(self, host):
19+
def __init__(self, host, user=None, password=None, key_file=None, port=None):
2020
lib = libspecinfra.load()
21-
lib.backend_ssh_new.argtypes = (ctypes.c_char_p,)
22-
lib.backend_ssh_new.restype = ctypes.POINTER(BackendWrapperS)
23-
lib.backend_ssh_free.argtypes = (ctypes.POINTER(BackendWrapperS),)
24-
self.obj = lib.backend_ssh_new(host.encode('utf-8'))
21+
ssh_builder_p = ctypes.POINTER(SSHBuilderS)
22+
lib.backend_ssh_builder_new.argtypes = (ctypes.c_char_p,)
23+
lib.backend_ssh_builder_new.restype = ssh_builder_p
24+
ssh_builder = lib.backend_ssh_builder_new(host.encode('utf-8'))
25+
26+
if user is not None:
27+
user = user.encode('utf-8')
28+
lib.backend_ssh_builder_user.argtypes = (ssh_builder_p, ctypes.c_char_p,)
29+
lib.backend_ssh_builder_user.restype = ssh_builder_p
30+
ssh_builder = lib.backend_ssh_builder_user(ssh_builder, user)
31+
32+
if password is not None:
33+
password = password.encode('utf-8')
34+
lib.backend_ssh_builder_password.argtypes = (ssh_builder_p, ctypes.c_char_p,)
35+
lib.backend_ssh_builder_password.restype = ssh_builder_p
36+
ssh_builder = lib.backend_ssh_builder_password(ssh_builder, password)
37+
38+
if key_file is not None:
39+
key_file = key_file.encode('utf-8')
40+
lib.backend_ssh_builder_key_file.argtypes = (ssh_builder_p, ctypes.c_char_p,)
41+
lib.backend_ssh_builder_key_file.restype = ssh_builder_p
42+
ssh_builder = lib.backend_ssh_builder_key_file(ssh_builder, key_file)
43+
44+
if port is not None:
45+
lib.backend_ssh_builder_port.argtypes = (ssh_builder_p, ctypes.c_uint32,)
46+
lib.backend_ssh_builder_port.restype = ssh_builder_p
47+
ssh_builder = lib.backend_ssh_builder_port(ssh_builder, port)
48+
49+
lib.backend_ssh_builder_finalize.argtypes = (ssh_builder_p,)
50+
lib.backend_ssh_builder_finalize.restype = ctypes.POINTER(BackendWrapperS)
51+
self.obj = lib.backend_ssh_builder_finalize(ssh_builder)
2552

2653
def __exit__(self, exc_type, exc_value, traceback):
54+
libspecinfra.load().backend_ssh_builder_free.argtypes = (ctypes.POINTER(SSHBuilderS),)
2755
self.lib.backend_ssh_free(self.obj)

libspecinfra/structures.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ class SpecinfraS(ctypes.Structure): pass;
55
class BackendWrapperS(ctypes.Structure): pass;
66

77
class FileS(ctypes.Structure): pass;
8+
9+
class SSHBuilderS(ctypes.Structure): pass;

setup.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
import os
33

44
from setuptools import setup, find_packages
5+
from setuptools.dist import Distribution
6+
7+
class BinaryDistribution(Distribution):
8+
def is_pure(self):
9+
return False
510

611
description = 'Python bindings for libspecinfra.'
712

@@ -14,6 +19,9 @@
1419
author_email='[email protected]',
1520
url='https://github.com/libspecinfra/libspecinfra-python',
1621
packages=find_packages(exclude=['tests*', 'specinfra*']),
22+
package_data={'libspecinfra': ['libspecinfra/libspecinfra.so']},
23+
include_package_data=True,
24+
distclass=BinaryDistribution,
1725
# rust extensions are not zip safe, just like C-extensions.
1826
zip_safe=False,
1927
license='MIT License',

specinfra

Submodule specinfra added at 8f177b4

test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import libspecinfra
2+
import libspecinfra.backend
3+
4+
#backend = libspecinfra.backend.Direct()
5+
6+
backend = libspecinfra.backend.SSH(
7+
host='ec2-52-25-30-143.us-west-2.compute.amazonaws.com',
8+
user='ubuntu',
9+
key_file='/Users/marcy/.ssh/orthros.pem',
10+
port=22)
11+
specinfra = libspecinfra.Specinfra(backend)
12+
f = specinfra.file('/etc/passwd')
13+
14+
#print(oct(f.mode())) # => 0o6441
15+
print(oct(f.mode()))

0 commit comments

Comments
 (0)