Skip to content

Prototype tuxctl #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions samples/python/second/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

*.py text eol=lf
57 changes: 57 additions & 0 deletions samples/python/second/src/commands/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import click

def common(function):
"""Add common options to click commands"""
function = click.option(
"--input",
"input_json",
help="JSON input data with settings",
required=False,
type=str,
)(function)

function = click.option(
"--username",
help="Username for authentication",
type=str,
required=False,
)(function)

function = click.option(
"--password",
help="Password for authentication",
type=str,
required=False,
hide_input=True,
)(function)

# Add additional user properties
function = click.option(
"--uid",
help="User ID number",
type=int,
required=False,
)(function)

function = click.option(
"--gid",
help="Primary group ID",
type=int,
required=False,
)(function)

function = click.option(
"--home",
help="Home directory path",
type=str,
required=False,
)(function)

function = click.option(
"--shell",
help="Login shell path",
type=str,
required=False,
)(function)

return function
40 changes: 40 additions & 0 deletions samples/python/second/src/commands/delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import click
import sys
from typing import Optional
from utils.utils import delete_user, collect_input_data
from utils.logger import dfl_logger as Logger


@click.command()
@click.option("--username", "-u", help="Username of the user to delete", type=str)
@click.option("--input", "input_json", help="JSON input with username", type=str)
@click.option(
"-w",
"--what-if",
is_flag=True,
help="Show what would happen without making changes",
)
def delete(username: Optional[str], input_json: Optional[str], what_if: bool):
"""
Delete a Linux user.

This command deletes a specified Linux user from the system.
"""
try:
data = collect_input_data(username=username, input_json=input_json)

username_to_delete = data.get("username")

if not username_to_delete:
Logger.error("Username is required to delete a user.", target="delete")
sys.exit(1)

Logger.info(
f"Processing delete request for user: {username_to_delete}", target="delete"
)

delete_user(username=username_to_delete, what_if=what_if)

except Exception as e:
Logger.error(f"Failed to process delete command: {str(e)}", target="delete")
sys.exit(1)
13 changes: 13 additions & 0 deletions samples/python/second/src/commands/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import click
from models.dsc_user import User

@click.command()
def export():
"""
Export Linux user information.

This command exports information about Linux users in JSON format.
"""

user = User()
user.export()
28 changes: 28 additions & 0 deletions samples/python/second/src/commands/get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import click
from utils.utils import get_input, get_requested_properties
from commands.common import common



@click.command()
@common
def get(username, password, input_json, uid, gid, home, shell):
"""
Get Linux user information.

This command takes either command line parameters or a JSON input to retrieve
information about a Linux user.
"""
user = get_input(
username=username,
password=password,
input_json=input_json,
uid=uid,
gid=gid,
home=home,
shell=shell
)

requested_properties = get_requested_properties(user)

user.get_current_state(requested_properties)
27 changes: 27 additions & 0 deletions samples/python/second/src/commands/root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import click
import sys
import os
from commands.get import get
from commands.set import set
from commands.delete import delete
from commands.export import export

@click.group(invoke_without_command=True)
@click.pass_context
def root_command(ctx):
"""Linux User Management CLI."""

ctx.ensure_object(dict)

if os.name != 'nt' and hasattr(os, 'geteuid') and os.geteuid() != 0:
click.echo("This program requires root privileges to manage users. Please run with sudo.")
sys.exit(1)

if ctx.invoked_subcommand is None:
click.echo(ctx.get_help())

# Register all available commands
root_command.add_command(get, name="get")
root_command.add_command(set, name="set")
root_command.add_command(delete, name="delete")
root_command.add_command(export, name="export")
37 changes: 37 additions & 0 deletions samples/python/second/src/commands/set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import click
from typing import Optional
from commands.common import common
from utils.utils import get_input

@click.command()
@common
@click.option('-w', '--what-if', is_flag=True, help='Show what would happen without making changes')
def set(
username: Optional[str],
password: Optional[str],
input_json: Optional[str],
uid: Optional[int],
gid: Optional[int],
home: Optional[str],
shell: Optional[str],
what_if: bool
):
"""
Create or update a Linux user.

This command takes either command line parameters or a JSON input to create or
modify a Linux user. If the user already exists, it will be updated.

Note: Group membership is read-only and cannot be modified with this command.
"""
user = get_input(
username=username,
password=password,
input_json=input_json,
uid=uid,
gid=gid,
home=home,
shell=shell
)

user.modify(what_if=what_if)
5 changes: 5 additions & 0 deletions samples/python/second/src/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python3
from commands.root import root_command

if __name__ == "__main__":
root_command()
Loading