update_limits.py.j2 #1

  • //
  • guest/
  • russell_jackson/
  • ansible-sdp/
  • roles/
  • perforce-sdp-install/
  • templates/
  • update_limits.py.j2
  • View
  • Commits
  • Open Download .zip Download (3 KB)
#!/usr/bin/env python3
#==============================================================================
# Copyright and license info is available in the LICENSE file included with
# the Server Deployment Package (SDP), and also available online:
# https://swarm.workshop.perforce.com/projects/perforce-software-sdp/view/main/LICENSE
#------------------------------------------------------------------------------
# MANAGED BY ANSIBLE (perforce-sdp-install / group_limits.yml) -- do not edit by
# hand. The limits group name is templated from host_vars (p4_baseline_group).
#------------------------------------------------------------------------------

"""
This script makes sure that all Perforce users are in the limits group.
"""

import sys, os, re, subprocess, tempfile

if len(sys.argv) < 2:
    print("Usage: update_limits.py <instance>", file=sys.stderr)
    sys.exit(1)

instance = sys.argv[1]

if not re.match(r'^[A-Za-z0-9_]+$', instance):
    print("Error: instance must be alphanumeric (plus underscore) only.", file=sys.stderr)
    sys.exit(1)

tmpdir = tempfile.mkdtemp(prefix="update_limits_")
users_file = os.path.join(tmpdir, "users.txt")
limits_file = os.path.join(tmpdir, "limits.txt")
newlimits_file = os.path.join(tmpdir, "newlimits.txt")

try:
    subprocess.run(["/p4/common/bin/p4login", instance], check=True)

    with open(limits_file, "w") as fh:
        subprocess.run(
            ["/p4/common/bin/p4master_run", instance,
             "/p4/{0}/bin/p4_{0}".format(instance), "group", "-o", "{{ p4_baseline_group }}"],
            stdout=fh, check=True)

    with open(users_file, "w") as fh:
        subprocess.run(
            ["/p4/common/bin/p4master_run", instance,
             "/p4/{0}/bin/p4_{0}".format(instance), "-ztag", "-F", "%User%", "users"],
            stdout=fh, check=True)

    userlist = []

    try:
        with open(users_file, 'r', errors='replace') as u:
            for user in u.readlines():
                user = user.strip()
                if user != "":
                    userlist.append(user)
    except Exception:
        sys.exit(0)

    # Deduplicate while preserving order
    seen = set()
    unique_userlist = []
    for user in userlist:
        if user not in seen:
            seen.add(user)
            unique_userlist.append(user)
    userlist = unique_userlist

    output = open(newlimits_file, "w")

    try:
        with open(limits_file, 'r', errors='replace') as l:
            for line in l.readlines():
                if line != "\n":
                    output.write(line)
    except Exception:
        output.close()
        sys.exit(0)

    for user in userlist:
        output.write("\t%s\n" % user)

    output.close()

    with open(newlimits_file, "r") as fh:
        subprocess.run(
            ["/p4/common/bin/p4master_run", instance,
             "/p4/{0}/bin/p4_{0}".format(instance), "group", "-i"],
            stdin=fh, stdout=subprocess.DEVNULL, check=True)

finally:
    for f in [users_file, limits_file, newlimits_file]:
        if os.path.exists(f):
            os.remove(f)
    if os.path.isdir(tmpdir):
        os.rmdir(tmpdir)
# Change User Description Committed
#1 32894 Russell C. Jackson (Rusty) Add group resource guardrails to perforce-sdp-install; inventory cleanup

Guardrails (gated on guardrails_enabled, commit server):
- New group_limits phase (thin orchestrator + group_limits/ subdir:
  deploy_scripts, set_limits, populate) included from main.yml. Templates the
  SDP keep_group_unset.py trigger and update_limits.py from inventory so no
  group/user name is hardcoded; sets Max* on each managed group via the new
  idempotent files/manage_p4group.py helper; populates the limits group once
  (marker-guarded). No new cron -- the SDP's hourly master cron maintains
  membership.
- Every group name is its own variable (p4_baseline_group, p4_integrators_group,
  p4_unlimited_group, p4_admins_group, p4_replicas_group); p4_group_limits is a
  list of {name, limits}. Naming convention in group_vars/main; per-environment
  values + enable flag in each commit server's host_vars; safe fallbacks in
  defaults.
- Molecule: install scenario now runs the guardrails phase after
  configure_new_server and verifies the scripts are deployed and the
  limits/p4-unlimited groups carry the expected values. README + docs updated.

Inventory cleanup:
- Removed the 2kgla1tst2 and la1v-rjackson commit hosts (host_vars + inventory).
- Renamed the top-level inventory group central -> main (group_vars dir moved,
  references updated across inventory, molecule, README, CLAUDE.md).

ansible-lint production-clean; playbooks syntax-check; helper/templates compile.