FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Adding Remote Example. Add force Flag To get_poolset() · JavaScriptDude/zfslib@4d790b1 · GitHub

Repository navigation

Commit 4d790b1

Browse files
Adding Remote Example. Add force Flag To get_poolset()
1 parent 58485d4 commit 4d790b1

4 files changed

Lines changed: 138 additions & 93 deletions

File tree

‎examples/ex_remote.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Basic example for accessing ZFS on remote machine
2+
3+
import sys, magic, subprocess
4+
from datetime import datetime, timedelta
5+
import zfslib as zfs
6+
from zfslib_ex_common import *
7+
8+
9+
def main(argv):
10+
# Log into remote computer 'freenas' as user 'root'
11+
# This assumes you configured ssh for user 'root' on computer 'freenas' to allow
12+
# local computer to authenticitate with its public key.
13+
# In real world, its suggested to create a user specific for zfs operations like 'zfs'
14+
conn = zfs.Connection(host='root@freenas', properties=["creation"])
15+
16+
# Load pool
17+
poolset = conn.get_poolset()
18+
19+
20+
# Print all datasets test
21+
print("Pools and Datasets:")
22+
for p in poolset:
23+
print_all_datasets(p)
24+
25+
26+
27+
if __name__ == '__main__':
28+
main(sys.argv[1:])
29+
30+

‎examples/examples.py‎

Lines changed: 2 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# See: https://github.com/JavaScriptDude/zfslib
22

33
import sys, magic, subprocess
4-
import zfslib as zfs
54
from datetime import datetime, timedelta
6-
5+
import zfslib as zfs
6+
from zfslib_ex_common import *
77

88
def main(argv):
99
pool_name = 'dpool'
@@ -80,94 +80,6 @@ def main(argv):
8080
print(f"{ds_name_full}@{snap.name} usedsnap: {snap.get_property('usedsnap')}")
8181

8282

83-
def print_all_datasets(pool: zfs.Pool):
84-
allds = pool.get_all_datasets(with_depth=True)
85-
for (depth, ds) in allds:
86-
print("{}: {} {} ({}) - [{}] - {}".format(pool.name, ' .'*depth, ds.name, ds.name, ds.get_property('mountpoint'), ds.get_property('used')))
87-
88-
89-
# This can be very slow for large datasets. Its actually `zfs diff` thats the slow part
90-
def print_diffs_test(ds: zfs.Dataset, snapshots: list):
91-
snap_last = None
92-
for i, snap in enumerate(snapshots):
93-
94-
if i > 0:
95-
diffs = ds.get_diffs(snap_last, snap, file_type='F', chg_type='M', include=['*.vb', '*.py', '*.js', '*.aspx'], exclude=['*.vscod*', '*_pycache_*', '*/_other/db/*'])
96-
for diff in diffs:
97-
try:
98-
if file_is_text(diff.snap_path_left): # Get diff of any text files
99-
print('{} - {}'.format(snap.name, diff))
100-
p_left = diff.snap_path_left
101-
p_right = diff.snap_path_right
102-
103-
# print('. path left: {}'.format(p_left))
104-
# print('. path right: {}'.format(p_right))
105-
(adds, rems, err) = get_file_diff(diff)
106-
if not err is None:
107-
print("Had Error: {}".format(err))
108-
else:
109-
if adds == 0 and rems == 0:
110-
print(" . (No changes)")
111-
else:
112-
print(" . file changed. Lines changed: -{} +{}".format(rems, adds))
113-
print(''' . meld diff: % meld "{}" "{}"'''.format(p_left, p_right))
114-
except PermissionError as ex:
115-
print("Had error: {}".format(ex))
116-
117-
snap_last = snap
118-
119-
120-
121-
def get_file_diff(diff:zfs.Diff):
122-
if not diff.file_type == 'F':
123-
raise Exception('get_file_diff() is only available for files (file_type = F).')
124-
if not diff.chg_type == 'M':
125-
raise Exception('get_file_diff() is only available for modify changes (chg_type = M).')
126-
p_left = diff.snap_path_left
127-
p_right = diff.snap_path_right
128-
129-
cmd = ['diff', '-ubwB', p_left, p_right]
130-
# print('''Running diff cmd: % diff -ubwB "{}" "{}"'''.format(p_left, p_right))
131-
132-
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
133-
stdout, stderr = p.communicate()
134-
135-
if p.returncode > 1:
136-
print("!!!!!! Warning. Return code !=0 `{}`: {}. Code: {}".format(cmd, stderr, p.returncode))
137-
return (0,0, stderr)
138-
139-
elif len(stderr) > 0:
140-
# return code is > 1 (no error), but there is a message in stderr
141-
print("`{}` return code is 0 but had stderr msg: {}".format(cmd, stderr))
142-
143-
if p.returncode == 0:
144-
return (0, 0, None)
145-
146-
if stdout == None:
147-
print('WARNING - stdout is None!')
148-
return (0, 0, "stdout is None")
149-
150-
elif len(stdout) == 0:
151-
print('WARNING - len(stdout) = 0!')
152-
return (0, 0, "stdout len is 0")
153-
154-
stdout = stdout.decode('utf-8')
155-
add = 0
156-
rem = 0
157-
for line in stdout.splitlines():
158-
c = line[0]
159-
if c == '+':
160-
add = add + 1
161-
elif c == '-':
162-
rem = rem + 1
163-
164-
return (add, rem, None)
165-
166-
167-
def file_is_text(path):
168-
f = magic.Magic(mime=True, uncompress=False)
169-
mime = f.from_file(path)
170-
return (mime == 'text/plain')
17183

17284
if __name__ == '__main__':
17385
main(sys.argv[1:])

‎examples/zfslib_ex_common.py‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import sys, magic, subprocess
2+
import zfslib as zfs
3+
4+
from datetime import datetime, timedelta
5+
6+
def print_all_datasets(pool: zfs.Pool):
7+
allds = pool.get_all_datasets(with_depth=True)
8+
for (depth, ds) in allds:
9+
print("{}: {} {} ({}) - [{}] - {}".format(pool.name, ' .'*depth, ds.name, ds.name, ds.get_property('mountpoint'), ds.get_property('used')))
10+
11+
12+
13+
def print_all_datasets(pool: zfs.Pool):
14+
allds = pool.get_all_datasets(with_depth=True)
15+
for (depth, ds) in allds:
16+
print("{}: {} {} ({}) - [{}] - {}".format(pool.name, ' .'*depth, ds.name, ds.name, ds.get_property('mountpoint'), ds.get_property('used')))
17+
18+
19+
# This can be very slow for large datasets. Its actually `zfs diff` thats the slow part
20+
def print_diffs_test(ds: zfs.Dataset, snapshots: list):
21+
snap_last = None
22+
for i, snap in enumerate(snapshots):
23+
24+
if i > 0:
25+
diffs = ds.get_diffs(snap_last, snap, file_type='F', chg_type='M', include=['*.vb', '*.py', '*.js', '*.aspx'], exclude=['*.vscod*', '*_pycache_*', '*/_other/db/*'])
26+
for diff in diffs:
27+
try:
28+
if file_is_text(diff.snap_path_left): # Get diff of any text files
29+
print('{} - {}'.format(snap.name, diff))
30+
p_left = diff.snap_path_left
31+
p_right = diff.snap_path_right
32+
33+
# print('. path left: {}'.format(p_left))
34+
# print('. path right: {}'.format(p_right))
35+
(adds, rems, err) = get_file_diff(diff)
36+
if not err is None:
37+
print("Had Error: {}".format(err))
38+
else:
39+
if adds == 0 and rems == 0:
40+
print(" . (No changes)")
41+
else:
42+
print(" . file changed. Lines changed: -{} +{}".format(rems, adds))
43+
print(''' . meld diff: % meld "{}" "{}"'''.format(p_left, p_right))
44+
except PermissionError as ex:
45+
print("Had error: {}".format(ex))
46+
47+
snap_last = snap
48+
49+
50+
51+
def get_file_diff(diff:zfs.Diff):
52+
if not diff.file_type == 'F':
53+
raise Exception('get_file_diff() is only available for files (file_type = F).')
54+
if not diff.chg_type == 'M':
55+
raise Exception('get_file_diff() is only available for modify changes (chg_type = M).')
56+
p_left = diff.snap_path_left
57+
p_right = diff.snap_path_right
58+
59+
cmd = ['diff', '-ubwB', p_left, p_right]
60+
# print('''Running diff cmd: % diff -ubwB "{}" "{}"'''.format(p_left, p_right))
61+
62+
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
63+
stdout, stderr = p.communicate()
64+
65+
if p.returncode > 1:
66+
print("!!!!!! Warning. Return code !=0 `{}`: {}. Code: {}".format(cmd, stderr, p.returncode))
67+
return (0,0, stderr)
68+
69+
elif len(stderr) > 0:
70+
# return code is > 1 (no error), but there is a message in stderr
71+
print("`{}` return code is 0 but had stderr msg: {}".format(cmd, stderr))
72+
73+
if p.returncode == 0:
74+
return (0, 0, None)
75+
76+
if stdout == None:
77+
print('WARNING - stdout is None!')
78+
return (0, 0, "stdout is None")
79+
80+
elif len(stdout) == 0:
81+
print('WARNING - len(stdout) = 0!')
82+
return (0, 0, "stdout len is 0")
83+
84+
stdout = stdout.decode('utf-8')
85+
add = 0
86+
rem = 0
87+
for line in stdout.splitlines():
88+
c = line[0]
89+
if c == '+':
90+
add = add + 1
91+
elif c == '-':
92+
rem = rem + 1
93+
94+
return (add, rem, None)
95+
96+
97+
def file_is_text(path):
98+
f = magic.Magic(mime=True, uncompress=False)
99+
mime = f.from_file(path)
100+
return (mime == 'text/plain')

‎setup.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
# Deploy to PyPi
2+
# % python3 setup.py sdist bdist_wheel
3+
# % python3 -m twine upload --skip-existing dist/*
14
from setuptools import setup
25
import os
36

4-
VERSION = "0.2"
7+
VERSION = "0.3"
58

69

710
def get_long_description():
@@ -15,7 +18,7 @@ def get_long_description():
1518
'Development Status :: 3 - Alpha',
1619
'Environment :: Console',
1720
'Intended Audience :: System Administrators',
18-
'License :: OSI Approved :: GNU General Public License (GPL)',
21+
'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
1922
'Operating System :: POSIX :: Linux',
2023
'Programming Language :: Python :: 3 :: Only',
2124
'Programming Language :: Python :: 3.7',
@@ -35,7 +38,7 @@ def get_long_description():
3538
"CI": "https://github.com/JavaScriptDude/zfslib/actions",
3639
"Changelog": "https://github.com/JavaScriptDude/zfslib/releases",
3740
},
38-
license="Apache License, Version 2.0",
41+
license="GNU General Public License v3 or later (GPLv3+)",
3942
version=VERSION,
4043
packages=["zfslib"],
4144
install_requires=[],

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL