File: genscript.py

package info (click to toggle)
ganeti 2.16.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 16,660 kB
  • sloc: python: 105,115; haskell: 43,768; sh: 3,896; makefile: 2,763
file content (61 lines) | stat: -rwxr-xr-x 1,894 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python

# Generate postinst/postrm for ganeti using doc/users/*

import os
import sys


def read_list(fname):
    with open(fname, "r") as f:
        return [ l.strip() for l in f ]

def read_pairs(fname):
    with open(fname, "r") as f:
        return [ l.strip().split(None, 1) for l in f ]

if len(sys.argv) != 3 or sys.argv[1] not in ('postinst', 'postrm'):
    sys.stderr.write("Usage: %s postinst|postrm DIR\n" % sys.argv[0])
    sys.exit(1)

root = os.path.join(sys.argv[2], 'doc/users')
debian_root = os.path.dirname(__file__)

out = ""
if sys.argv[1] == "postinst":
    out += "\t# Groups\n"
    for group in read_list(os.path.join(root, "groups")):
        out += "\taddgroup --quiet --system \"%s\"\n" % group
    out += "\n"

    out += "\t# Users\n"
    for user, group in read_pairs(os.path.join(root, "users")):
        out += "\tadduser --quiet --system --ingroup \"%s\" --no-create-home" \
               " --disabled-password --disabled-login" \
               " --home /var/lib/ganeti \"%s\"\n" % (group, user)
    out += "\n"

    out += "\t# Group memberships\n"
    for user, group in read_pairs(os.path.join(root, "groupmemberships")):
        out += "\tadduser --quiet \"%s\" \"%s\"\n" % (user, group)

elif sys.argv[1] == "postrm":
    out += "\t# Users\n"
    for user, _ in read_pairs(os.path.join(root, "users")):
        out += "\tdeluser --quiet --system \"%s\" || true\n" % user
    out += "\n"

    out += "\t# Groups\n"
    for group in read_list(os.path.join(root, "groups")):
        out += "\tdelgroup --quiet --system \"%s\" || true\n" % group
    out += "\n"


with open(os.path.join(debian_root, "ganeti.%s.in" % sys.argv[1])) as script:
    done = False
    for line in script:
        if line.strip() == "#GANETI_USERS#" and not done:
            sys.stdout.write(out)
            done = True
        else:
            sys.stdout.write(line)