1 | # |
---|
2 | # Copyright (C) 2022 Soleta Networks <info@soleta.eu> |
---|
3 | # |
---|
4 | # This program is free software: you can redistribute it and/or modify it under |
---|
5 | # the terms of the GNU Affero General Public License as published by the |
---|
6 | # Free Software Foundation; either version 3 of the License, or |
---|
7 | # (at your option) any later version. |
---|
8 | |
---|
9 | import logging |
---|
10 | import os |
---|
11 | import subprocess |
---|
12 | |
---|
13 | from subprocess import DEVNULL, PIPE |
---|
14 | |
---|
15 | import psutil |
---|
16 | |
---|
17 | |
---|
18 | def find_mountpoint(path): |
---|
19 | """ |
---|
20 | Returns mountpoint of a given path |
---|
21 | """ |
---|
22 | path = os.path.abspath(path) |
---|
23 | while not os.path.ismount(path): |
---|
24 | path = os.path.dirname(path) |
---|
25 | return path |
---|
26 | |
---|
27 | |
---|
28 | def mount_mkdir(source, target): |
---|
29 | """ |
---|
30 | Mounts and creates the mountpoint directory if it's not present. |
---|
31 | |
---|
32 | Return True if mount is sucessful or if target is already a mountpoint. |
---|
33 | """ |
---|
34 | if not os.path.exists(target): |
---|
35 | os.mkdir(target) |
---|
36 | |
---|
37 | if not os.path.ismount(target): |
---|
38 | return mount(source, target) |
---|
39 | else: |
---|
40 | return True |
---|
41 | |
---|
42 | return False |
---|
43 | |
---|
44 | |
---|
45 | def mount(source, target): |
---|
46 | """ |
---|
47 | Mounts source into target directoru using mount(8). |
---|
48 | |
---|
49 | Return true if exit code is 0. False otherwise. |
---|
50 | """ |
---|
51 | cmd = f'mount {source} {target}' |
---|
52 | proc = subprocess.run(cmd.split(), stderr=DEVNULL) |
---|
53 | |
---|
54 | return not proc.returncode |
---|
55 | |
---|
56 | |
---|
57 | def umount(target): |
---|
58 | """ |
---|
59 | Umounts target using umount(8). |
---|
60 | |
---|
61 | Return true if exit code is 0. False otherwise. |
---|
62 | """ |
---|
63 | cmd = f'umount {target}' |
---|
64 | proc = subprocess.run(cmd.split(), stderr=DEVNULL) |
---|
65 | |
---|
66 | return not proc.returncode |
---|
67 | |
---|
68 | |
---|
69 | def get_usedperc(mountpoint): |
---|
70 | """ |
---|
71 | Returns percetage of used filesystem as decimal number. |
---|
72 | """ |
---|
73 | try: |
---|
74 | total, used, free, perc = psutil.disk_usage(mountpoint) |
---|
75 | except FileNotFoundError: |
---|
76 | return '0' |
---|
77 | return str(perc) |
---|
78 | |
---|
79 | |
---|
80 | def ogReduceFs(disk, part): |
---|
81 | """ |
---|
82 | Bash function 'ogReduceFs' wrapper |
---|
83 | """ |
---|
84 | proc = subprocess.run(f'ogReduceFs {disk} {part}', |
---|
85 | shell=True, stdout=PIPE, |
---|
86 | encoding='utf-8') |
---|
87 | if proc.returncode != 0: |
---|
88 | logging.warn(f'ogReduceFS exited with non zero code: {proc.returncode}') |
---|
89 | subprocess.run(f'ogUnmount {disk} {part}', |
---|
90 | shell=True) |
---|
91 | |
---|
92 | |
---|
93 | def ogExtendFs(disk, part): |
---|
94 | """ |
---|
95 | Bash function 'ogExtendFs' wrapper |
---|
96 | """ |
---|
97 | subprocess.run(f'ogMount {disk} {part}', |
---|
98 | shell=True) |
---|
99 | proc = subprocess.run(f'ogExtendFs {disk} {part}', |
---|
100 | shell=True) |
---|
101 | if proc.returncode != 0: |
---|
102 | logging.warn(f'ogExtendFs exited with non zero code: {proc.returncode}') |
---|