/
system.py
202 lines (162 loc) · 7.03 KB
/
system.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# -*- mode: python; encoding: utf-8 -*-
#
# Copyright 2012 Jens Lindström, Opera Software ASA
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
import pwd
import grp
import subprocess
import argparse
import installation
hostname = None
username = "critic"
email = None
uid = None
groupname = "critic"
gid = None
create_system_user = None
created_system_user = False
create_system_group = None
created_system_group = False
def fetch_uid_gid():
global uid, gid
uid = pwd.getpwnam(username).pw_uid
gid = grp.getgrnam(groupname).gr_gid
def add_arguments(mode, parser):
if mode != "install":
parser.add_argument("--system-recipient", action="append",
dest="system_recipients", help=argparse.SUPPRESS)
return
parser.add_argument("--system-hostname", action="store",
help="FQDN of the system")
parser.add_argument("--system-username", action="store",
help="name of system user to run as")
parser.add_argument("--force-create-system-user", action="store_true",
help=("don't prompt for permission to create a new "
"system user if doesn't exist"))
parser.add_argument("--system-email", action="store",
help="address used as sender of emails")
parser.add_argument("--system-groupname", action="store",
help="name of system group to run as")
parser.add_argument("--force-create-system-group", action="store_true",
help=("don't prompt for permission to create a new "
"system group if it doesn't exist"))
parser.add_argument("--system-recipient", action="append",
dest="system_recipients", metavar="SYSTEM_RECIPIENT",
help=("email recipient of automatic messages from "
"the system"))
def prepare(mode, arguments, data):
global hostname, username, email, create_system_user
global groupname, create_system_group
global uid, gid
if mode == "install":
print """
Critic Installation: System
===========================
"""
if arguments.system_hostname: hostname = arguments.system_hostname
else:
try: hostname = subprocess.check_output(["hostname", "--fqdn"]).strip()
except: pass
hostname = installation.input.string(prompt="What is the machine's FQDN?",
default=hostname)
while True:
if arguments.system_username: username = arguments.system_username
else:
username = installation.input.string(prompt="What system user should Critic run as?",
default=username)
try:
pwd.getpwnam(username)
user_exists = True
except:
user_exists = False
if user_exists:
print """
The system user '%s' already exists.
""" % username
if installation.input.yes_or_no(prompt="Use the existing system user '%s'?" % username,
default=True):
create_system_user = False
break
else:
print """
The system user '%s' doesn't exists.
""" % username
if arguments.force_create_system_user or installation.input.yes_or_no(prompt="Create a system user named '%s'?" % username,
default=True):
create_system_user = True
break
while True:
if arguments.system_groupname: groupname = arguments.system_groupname
else:
groupname = installation.input.string(prompt="What system group should Critic run as?",
default=groupname)
try:
grp.getgrnam(groupname)
group_exists = True
except:
group_exists = False
if group_exists:
print """
The system group '%s' already exists.
""" % groupname
if installation.input.yes_or_no(prompt="Use the existing system group '%s'?" % groupname,
default=True):
create_system_group = False
break
else:
print """
The system group '%s' doesn't exists.
""" % groupname
if arguments.force_create_system_group or installation.input.yes_or_no(prompt="Create a system group named '%s'?" % groupname,
default=True):
create_system_group = True
break
if arguments.system_email: email = arguments.system_email
else:
email = installation.input.string(prompt="What address should be used as the sender of emails from the system?",
default=("%s@%s" % (username, hostname)))
else:
import configuration
hostname = configuration.base.HOSTNAME
username = configuration.base.SYSTEM_USER_NAME
email = configuration.base.SYSTEM_USER_EMAIL
try: groupname = configuration.base.SYSTEM_GROUP_NAME
except AttributeError: groupname = data["installation.system.groupname"]
fetch_uid_gid()
data["installation.system.hostname"] = hostname
data["installation.system.username"] = username
data["installation.system.email"] = email
data["installation.system.groupname"] = groupname
return True
def install(data):
global uid, gid
if create_system_group:
print "Creating group '%s' ..." % groupname
subprocess.check_call(["addgroup", "--quiet", "--system", groupname])
if create_system_user:
print "Creating user '%s' ..." % username
subprocess.check_call(
["adduser", "--quiet", "--system", "--ingroup=%s" % groupname,
"--home=%s" % installation.paths.data_dir, "--disabled-login",
username])
uid = pwd.getpwnam(username).pw_uid
gid = grp.getgrnam(groupname).gr_gid
return True
def undo():
if created_system_user:
print "Deleting user '%s' ..." % username
subprocess.check_call(["deluser", "--system", username])
if created_system_group:
print "Deleting group '%s' ..." % groupname
subprocess.check_call(["delgroup", "--system", groupname])