Compare commits
10 commits
56b9ede990
...
1639c613ab
| Author | SHA1 | Date | |
|---|---|---|---|
| 1639c613ab | |||
| 8cb53e725e | |||
| b9f89db78f | |||
| 8e641c0f97 | |||
| 92cab96c85 | |||
| d4009d8f09 | |||
| 6318ece7e1 | |||
| 023ae57225 | |||
| ad6fb7e28f | |||
| 63e7ff18cd |
4 changed files with 196 additions and 13 deletions
98
pastafariutils/linux.py
Normal file
98
pastafariutils/linux.py
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
#!/usr/bin/epython3
|
||||
|
||||
import os
|
||||
import distro
|
||||
from subprocess import call, DEVNULL
|
||||
import re
|
||||
import json
|
||||
|
||||
def shell_command(command):
|
||||
|
||||
if call(command, shell=True) > 0:
|
||||
print('Error: cannot execute command')
|
||||
return False
|
||||
|
||||
def check_distro(arr_command):
|
||||
|
||||
distro_id=distro.id()
|
||||
|
||||
if not distro_id in arr_command:
|
||||
|
||||
print("Sorry, you don't have a patch for this distro\n\n")
|
||||
|
||||
return False
|
||||
else:
|
||||
return distro_id
|
||||
|
||||
def install_package(package):
|
||||
|
||||
distro_id=distro.id()
|
||||
|
||||
if distro_id=='debian' or distro_id=='ubuntu':
|
||||
|
||||
return shell_command('sudo DEBIAN_FRONTEND="noninteractive" apt-get install -y {}'.format(package[distro]))
|
||||
|
||||
elif distro_id=='fedora' or distro_id=='almalinux' or distro_id=='rocky':
|
||||
|
||||
return shell_command('sudo dnf install -y {}'.format(package[distro]))
|
||||
|
||||
elif distro_id=='arch':
|
||||
|
||||
return shell_command('sudo pacman -S --noconfirm {}'.format(package[distro]))
|
||||
|
||||
|
||||
def patch_file(original_file, patch_file):
|
||||
|
||||
distro_id=check_distro(original_file)
|
||||
|
||||
if not distro_id:
|
||||
|
||||
return False
|
||||
|
||||
return shell_command("sudo patch {} < {}".format(original_file[distro], patch_file[distro]))
|
||||
|
||||
|
||||
def systemd_service(action, service):
|
||||
|
||||
distro_id=check_distro(service)
|
||||
|
||||
if not distro_id:
|
||||
|
||||
return False
|
||||
|
||||
return shell_command('sudo systemctl {} {}'.format(action, service[distro]))
|
||||
|
||||
|
||||
def exec(command):
|
||||
|
||||
distro_id=check_distro(command)
|
||||
|
||||
if not distro_id:
|
||||
|
||||
return False
|
||||
|
||||
return shell_command(command[distro])
|
||||
|
||||
|
||||
def sed(arr_sed):
|
||||
|
||||
distro_id=check_distro(arr_sed)
|
||||
|
||||
if not distro_id:
|
||||
|
||||
return False
|
||||
|
||||
return shell_command("sudo sed -i s/{}/{}/g".format(arr_sed[distro][0], arr_sed[distro][1]))
|
||||
|
||||
|
||||
def json_log(message, error=0, status=0, progress=0, no_progress=0, return_message=0):
|
||||
|
||||
log={'error': error, 'status': status, 'progress': progress, 'no_progress': no_progress, 'message': message}
|
||||
|
||||
if not return_message:
|
||||
|
||||
print(json.dumps(log))
|
||||
|
||||
else:
|
||||
return json.dumps(log)
|
||||
|
||||
73
pastafariutils/packages.py
Normal file
73
pastafariutils/packages.py
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import distro
|
||||
from subprocess import call, DEVNULL
|
||||
import re
|
||||
|
||||
def install_package(package: dict, extra_configurations={}):
|
||||
|
||||
"""A function for install packages for different distros. Now support, debian, ubuntu, rocky, almalinux, fedora, archlinux.
|
||||
|
||||
Args:
|
||||
package (dict): A dict with all packages names for every distro.
|
||||
|
||||
Returns:
|
||||
|
||||
result_package_manager (bool): return false if install fail, if install is finished, return true.
|
||||
"""
|
||||
|
||||
linux_distro=distro.id()
|
||||
|
||||
if not linux_distro in package:
|
||||
print('Sorry, not package in {}'.format(linux_distro))
|
||||
return False
|
||||
|
||||
if linux_distro=='debian' or linux_distro=='ubuntu':
|
||||
|
||||
if call('sudo DEBIAN_FRONTEND="noninteractive" apt-get install -y {}'.format(package[linux_distro]), shell=True) > 0:
|
||||
print('Error, cannot install {}...'.format(package[linux_distro]))
|
||||
return False
|
||||
|
||||
elif linux_distro=='rocky' or linux_distro=='fedora' or linux_distro=='almalinux':
|
||||
|
||||
if call("sudo dnf install -y {}".format(package[linux_distro]), shell=True) > 0:
|
||||
print('Error, cannot install {}...'.format(package[linux_distro]))
|
||||
return False
|
||||
if linux_distro=='arch':
|
||||
|
||||
if call("sudo pacman -S --noconfirm {}".format(package[linux_distro]), shell=True) > 0:
|
||||
print('Error, cannot install {}...'.format(package[linux_distro]))
|
||||
return False
|
||||
|
||||
# Method for patch files using patch utility
|
||||
|
||||
def apply_patch(original_file: dict, patch_file: dict):
|
||||
|
||||
linux_distro=distro.id()
|
||||
|
||||
# patch originalAmigo.sh < parche.patch
|
||||
|
||||
if not linux_distro in original_file or not linux_distro in patch_file):
|
||||
print('Error, not exists original file and patch files for this distro'))
|
||||
return False
|
||||
|
||||
if call("sudo patch {} < {}".format(original_file[linux_distro], patch_file[linux_distro]), shell=True) > 0:
|
||||
print('Error, cannot patch {}...'.format(original_file[linux_distro]))
|
||||
return False
|
||||
# A simple function for fill an array for every distros with the same package name.
|
||||
|
||||
def fill_all_distros_str(name):
|
||||
|
||||
fill_str={}
|
||||
|
||||
for distro in ['debian', 'ubuntu', 'rocky', 'almalinux', 'fedora', 'arch']:
|
||||
fill_str[distro]=name
|
||||
|
||||
return fill_str
|
||||
|
||||
# A simple function for get a json return value
|
||||
|
||||
def return_json_value():
|
||||
|
||||
return {}
|
||||
|
|
@ -13,7 +13,7 @@ from subprocess import call, DEVNULL
|
|||
def add_user(user, password='', group='', user_directory='', shell='/usr/sbin/nologin'):
|
||||
|
||||
if user_directory=='':
|
||||
user_directory=user
|
||||
user_directory='/home/'+user
|
||||
|
||||
try:
|
||||
user_pwd=pwd.getpwnam(user)
|
||||
|
|
@ -32,6 +32,8 @@ def add_user(user, password='', group='', user_directory='', shell='/usr/sbin/no
|
|||
|
||||
if group!='':
|
||||
|
||||
# Buggy, need fix.
|
||||
|
||||
stat_group=os.stat('/home/%s' % user_directory)
|
||||
gid=stat_group.st_gid
|
||||
|
||||
|
|
@ -43,10 +45,12 @@ def add_user(user, password='', group='', user_directory='', shell='/usr/sbin/no
|
|||
|
||||
if call(func_user, shell=True, stdout=DEVNULL) > 0:
|
||||
|
||||
return (True, '')
|
||||
return (False, 'Error executing useradd command')
|
||||
|
||||
else:
|
||||
return (False, 'Error executing useradd command')
|
||||
|
||||
return (True, '')
|
||||
|
||||
|
||||
|
||||
def change_password(user, new_password):
|
||||
|
|
@ -54,10 +58,14 @@ def change_password(user, new_password):
|
|||
try:
|
||||
user_pwd=pwd.getpwnam(user)
|
||||
|
||||
if call("sudo echo \"%s:%s\" | chpasswd" % (user, new_password), shell=True, stdout=DEVNULL) > 0:
|
||||
if call("sudo echo \"%s:%s\" | sudo chpasswd" % (user, new_password), shell=True, stdout=DEVNULL) > 0:
|
||||
|
||||
return (False, 'I cannot change password, permissions?')
|
||||
|
||||
else:
|
||||
return (True, 'Change password successfully')
|
||||
|
||||
|
||||
except KeyError:
|
||||
|
||||
return (False, 'I cannot change password, user exists?')
|
||||
|
|
@ -65,10 +73,14 @@ def change_password(user, new_password):
|
|||
def del_user(user):
|
||||
|
||||
if call("sudo userdel -r %s" % user, shell=True, stdout=DEVNULL, stderr=DEVNULL) > 0:
|
||||
|
||||
return (False, '')
|
||||
else:
|
||||
return (True, 'Deleted user successfully')
|
||||
|
||||
else:
|
||||
def mkdir_sh(directory):
|
||||
|
||||
if call("sudo mkdir -p %s" % directory, shell=True) > 0:
|
||||
return (False, '')
|
||||
else:
|
||||
return (True, 'Created directory successfully %s' % directory)
|
||||
|
||||
|
|
|
|||
8
setup.py
8
setup.py
|
|
@ -5,15 +5,15 @@ import os
|
|||
from setuptools import setup, find_packages
|
||||
|
||||
|
||||
if sys.version_info < (3, 3):
|
||||
raise NotImplementedError("Sorry, you need at least Python 3.3 for use pastafariutils.")
|
||||
if sys.version_info < (3, 6):
|
||||
raise NotImplementedError("Sorry, you need at least Python 3.6 for use pastafariutils.")
|
||||
|
||||
#import paramecio
|
||||
# Pillow should be installed after if you need ImageField
|
||||
# If you install passlib and bcrypt, the password system will use bcrypt by default, if not, will use native crypt libc
|
||||
|
||||
setup(name='paramecio',
|
||||
version='0.5.0',
|
||||
setup(name='pastafariutils',
|
||||
version='0.6.3',
|
||||
description='Utils for make *nix scripts.',
|
||||
long_description='Utils for make *nix scripts.',
|
||||
author='Antonio de la Rosa Caballero',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue