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
|
#!/usr/bin/env python
# Copyright (C) 2012 Peter Hatina <phatina@redhat.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
import os
import sys
from cura.cura_options import CuraBasicOptions
from cura.cura_address import CuraIpv4Addr, CuraIpv4AddrGenerator
from cura.cura_client_user import CuraUserClient
class CuraUserOptions(CuraBasicOptions):
def __init__(self):
script_name = os.path.basename(__file__)
super(self.__class__, self).__init__(
"Available actions:\n"
" list-users, list-groups, group-members\n"
" useradd\n\n"
"Group-members:\n"
" " + script_name + " [options] group-members groupName\n\n"
"Useradd:\n"
" " + script_name + " [options] useradd newUsername\n\n")
self.m_parser.set_usage("Usage: %prog [options] action")
self.m_parser.add_option("-s", "--shell",
action = "store",
dest = "shell",
help = "login shell of the new account")
self.m_parser.add_option("-r", "--system-account",
action = "store_true",
dest = "system_account",
help = "create a system account")
self.m_parser.add_option("-m", "--create-home",
action = "store_true",
dest = "create_home",
help = "create the user's home directory")
self.m_parser.add_option("-M", "--no-create-home",
action = "store_false",
dest = "create_home",
help = "do not create the user's home directory")
self.m_parser.add_option("-N", "--no-user-group",
action = "store_false",
dest = "create_group",
help = "do not create a group with the same name as the user")
self.m_parser.add_option("-g", "--gid",
action = "store",
dest = "gid",
help = "name or ID of the primary group of the new account")
self.m_parser.add_option("", "--gecos",
action = "store",
dest = "gecos",
help = "GECOS information for new user")
self.m_parser.add_option("-d", "--home-dir",
action = "store",
dest = "home_dir",
help = "home directory of the new account")
self.m_parser.add_option("-n", "--new-password",
action = "store",
dest = "new_password",
help = "password for the new account")
self.m_parser.add_option("-i", "--uid",
action = "store",
dest = "uid",
help = "user ID of the new account")
@property
def good(self):
if not len(self.m_pos_options):
return False
elif self.m_pos_options[0] == "group-members":
return len(self.m_pos_options) == 2
elif self.m_pos_options[0] == "useradd":
return len(self.m_pos_options) == 2
return len(self.m_pos_options) == 1
@property
def action(self):
return self.m_pos_options[0] if self.good and self.m_pos_options[0] else ""
@property
def group(self):
return self.m_pos_options[1] if self.good and self.m_pos_options[1] else ""
@property
def newUsername(self):
return self.m_pos_options[1] if self.good and self.m_pos_options[1] else ""
@property
def shell(self):
return self.m_options.shell if self.good and self.m_options.shell else ""
@property
def systemAccount(self):
return self.m_options.system_account if self.good and self.m_options.system_account else False
@property
def createHome(self):
return self.m_options.create_home in (None, True) if self.good else False
@property
def createGroup(self):
return self.m_options.create_group in (None, True) if self.good else True
@property
def gid(self):
return self.m_options.gid if self.good and self.m_options.gid else ""
@property
def gecos(self):
return self.m_options.gecos if self.good and self.m_options.gecos else ""
@property
def homeDir(self):
return self.m_options.home_dir if self.good and self.m_options.home_dir else ""
@property
def newPassword(self):
return self.m_options.new_password if self.good and self.m_options.new_password else ""
@property
def uid(self):
return self.m_options.uid if self.good and self.m_options.uid else ""
if __name__ == "__main__":
options = CuraUserOptions()
options.parse(sys.argv)
if not options.good:
sys.stderr.write(
"Wrong tool usage. Run " + __file__ + " --help for detailed help.\n")
sys.exit(1)
client_failed = False
client_hostnames = CuraIpv4AddrGenerator(options.hostname).enumerate()
client_action = options.action.lower()
for client_hostname in client_hostnames:
if not len(client_hostname):
continue
client = CuraUserClient(client_hostname, options.username, options.password)
actions = {
"list-users" : client.listUsers,
"list-groups" : client.listGroups
}
if client_action == "group-members":
(rval, rparam) = client.listGroupMembers(options.group)
elif client_action == "useradd":
(rval, rparam) = client.userAdd(
options.newUsername,
shell = options.shell,
systemAccount = options.systemAccount,
createHome = options.createHome,
createGroup = options.createGroup,
homeDir = options.homeDir,
uid = options.uid,
gid = options.gid,
password = options.newPassword,
gecos = options.gecos)
elif client_action in actions:
(rval, rparam) = actions[client_action]()
else:
sys.stderr.write("No such action to perform!\n")
sys.exit(1)
if rval:
sys.stdout.write("%s: %s\n" % (client_hostname, rparam if rparam else "ok"))
else:
sys.stderr.write("%s: %s\n" % (client_hostname, rparam))
client_failed = True
sys.exit(client_failed)
|