Skip to content
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

Add p-helper commands to make restarting Pulp easier #161

Merged
merged 1 commit into from
Sep 26, 2024
Merged
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
88 changes: 88 additions & 0 deletions base/container_scripts/install_phelpers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# The p-helper commands, back and better than ever!
# This file should be sourced to get the classic commands inside your oci-env


_paction() {
SERVICES=$(s6-rc -d list | grep -E 'pulpcore|nginx' | paste -sd ' ')
if [ $# -gt 1 ] ; then
SERVICES="${@:2}"
fi
echo "`setterm -foreground blue` s6-rc $1 ${SERVICES}"
setterm -default
s6-rc $1 ${SERVICES}
}

pstart() {
_paction start $@
}
_pstart_help="Start all pulp-related services"

pstop() {
_paction stop $@
}
_pstop_help="Stop all pulp-related services"

prestart() {
_paction stop $@
_paction start $@
}
_prestart_help="Restart all pulp-related services"

pstatus() {
echo "`setterm -foreground green`Services that are live/ran successfully"
_paction -a list
echo "`setterm -foreground red`Services that are down"
_paction -da list
}
_pstatus_help="Report the status of all pulp-related services"


pdbreset() {
echo "Resetting the Pulp database"
bash /opt/oci_env/base/container_scripts/database_reset.sh
}
_pdbreset_help="Reset the Pulp database"
# can get away with not resetting terminal settings here since it gets reset in phelp
_pdbreset_help="$_dbreset_help - `setterm -foreground red -bold on`THIS DESTROYS YOUR PULP DATA"

pclean() {
pdbreset
sudo rm -rf /var/pulp/media/*
redis-cli FLUSHALL
pulpcore-manager collectstatic --clear --noinput --link
if which mc ; then
mc rb --force s3/${PULP_AWS_STORAGE_BUCKET_NAME}
mc mb s3/${PULP_AWS_STORAGE_BUCKET_NAME}
fi
}
_pclean_help="Restore pulp to a clean-installed state"
# can get away with not resetting terminal settings here since it gets reset in phelp
_pclean_help="$_pclean_help - `setterm -foreground red -bold on`THIS DESTROYS YOUR PULP DATA"

phelp() {
# get a list of declared functions, filter out ones with leading underscores as "private"
funcs=$(declare -F | awk '{ print $3 }'| grep -v ^_ | grep -v fzf)

# for each func, if a help string is defined, assume it's a pulp function and print its help
# (this is bash introspection via variable variables)
for func in $funcs; do
# get the "help" variable name for this function
help_var="_${func}_help"
# use ${!<varname>} syntax to eval the help_var
help=${!help_var}
# If the help var had a value, echo its value here (the value is function help text)
if [ ! -z "$help" ]; then
# make the function name easy to spot
setterm -foreground yellow -bold on
echo -n "$func"
# reset terminal formatting before printing the help text
# (implicitly format it as normal text)
setterm -default
echo ": $help"
fi
done

# explicitly restore terminal formatting is reset before exiting function
setterm -default
}
_phelp_help="Print this help"
4 changes: 4 additions & 0 deletions base/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ then
then
echo "eval \"\$(LC_ALL=C _PULP_COMPLETE=bash_source pulp)\"" >> /root/.bashrc
fi
if ! grep -q "install_phelpers.sh" /root/.bashrc
then
echo "source /opt/oci_env/base/container_scripts/install_phelpers.sh" >> /root/.bashrc
fi
pulp --refresh-api status
fi

Expand Down
16 changes: 0 additions & 16 deletions base/s6-rc-modifications/pulpcore-api/run

This file was deleted.

32 changes: 32 additions & 0 deletions client/oci_env/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,35 @@ def poll(args, client):

def pulp(args, client):
exit(client.exec(["pulp"] + args.command, interactive=False).returncode)


def phelper(args, client):
if args.action in ("help", "status", "dbreset", "clean"):
proc = client.exec(["bash", "-lc", f"p{args.action}"], shell=True)
exit(proc.returncode)
shortcuts = {"api": "pulpcore-api", "content": "pulpcore-content"}
pcmd = [f"p{args.action}"]
opts = set(args.services)
for val in opts:
if val in shortcuts:
pcmd.append(shortcuts[val])
elif val.startswith("worker"):
# Support notation: 'worker', 'worker3', 'worker1-3'
worker_range = [int(i) for i in val[len("worker"):].split("-") if i.isdigit()]
if len(worker_range) == 1:
worker_range.append(worker_range[0])
elif len(worker_range) == 0:
worker_range = [1, int(client.config.get("PULP_WORKERS", 2))]
worker_range[1] += 1 # Increase outer bounds for range
for i in range(*worker_range[:3]):
pcmd.append(f"pulpcore-worker@{i}")
else:
pcmd.append(val)

if args.action in {"start", "restart"}:
if set(pcmd).intersection({"pulpcore-api", "pulpcore-content"}) and "nginx" not in opts:
pcmd.append("nginx")
cmd_str = " ".join(pcmd)
print(f"{args.action.capitalize()}ing {pcmd[1:] or 'all services'}")
proc = client.exec(["bash", "-lc", f'"{cmd_str}"'], shell=True)
exit(proc.returncode)
18 changes: 17 additions & 1 deletion client/oci_env/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
pulpcore_manager,
profile,
poll,
pulp
pulp,
phelper
)

from oci_env.utils import (
Expand Down Expand Up @@ -55,6 +56,7 @@ def get_parser():
parse_profile_command(subparsers)
parse_poll_command(subparsers)
parse_pulp_cli_command(subparsers)
parse_phelper_commands(subparsers)

return parser

Expand Down Expand Up @@ -154,6 +156,20 @@ def parse_pulp_cli_command(subparsers):
parser.set_defaults(func=pulp)


def parse_phelper_commands(subparsers):
for action in ("start", "stop", "restart"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for action in ("start", "stop", "restart"):
for action in ("start", "stop", "restart", "status", "clean", "dbreset", "help"):

We need to register the rest of the commands:

oci-env phelp
usage: oci-env [-h] [-v] [-e ENV_FILE]
               {compose,exec,db,shell,test,generate-client,pulpcore-manager,profile,poll,pulp,pstart,pstop,prestart}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I really need to? clean and dbreset are already covered in other commands, status doesn't do anything extra than what pulp status does, and help is redundant. Those extra commands are helpful inside the container, but outside I don't think so.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, phelp can be useful for showing what commends are available for the user. If we wanted to completely mirror the old behaviour, it would be nice to have this change included.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I just added the rest of the phelper commands to the list. I guess it isn't much of a big deal.

parser = subparsers.add_parser(f'p{action}', help=f'{action.capitalize()} all/any Pulp service')
parser.add_argument('services', nargs=argparse.REMAINDER, help=f'List of services to {action}, leave blank to {action} all')
parser.set_defaults(func=phelper, action=action)
parser = subparsers.add_parser('phelp', help='Show available pcommands available in the container.')
parser.set_defaults(func=phelper, action="help")
parser = subparsers.add_parser('pstatus', help='Return the status of pulp-related services.')
parser.set_defaults(func=phelper, action="status")
parser = subparsers.add_parser('pclean', help='Restore Pulp to a clean install state.')
parser.set_defaults(func=phelper, action="clean")
parser = subparsers.add_parser('pdbreset', help='Reset the Pulp database.')
parser.set_defaults(func=phelper, action="dbreset")

def main():
parser = get_parser()
args = parser.parse_args()
Expand Down
8 changes: 5 additions & 3 deletions client/oci_env/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ def _service_containers():
except subprocess.CalledProcessError:
_exit_no_container_found()

def exec(self, args, service=None, interactive=False, pipe_output=False, privileged=False):
def exec(self, args, service=None, interactive=False, pipe_output=False, privileged=False, shell=False):
"""
Execute a script in a running container using podman or docker.

Expand All @@ -432,15 +432,17 @@ def exec(self, args, service=None, interactive=False, pipe_output=False, privile

if privileged:
cmd = cmd[:2] + ["--privileged"] + cmd[2:]
if shell:
cmd = " ".join(cmd)

if interactive:
if self.is_verbose:
logger.info(f"Running [interactive] command in container: {' '.join(cmd)}")
proc = subprocess.call(cmd)
proc = subprocess.call(cmd, shell=shell)
else:
if self.is_verbose:
logger.info(f"Running [non-interactive] command in container: {' '.join(cmd)}")
proc = subprocess.run(cmd, capture_output=pipe_output)
proc = subprocess.run(cmd, capture_output=pipe_output, shell=shell)
return proc

def get_dynaconf_variable(self, name):
Expand Down
Loading