-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploystudio_users.py
166 lines (137 loc) · 5.4 KB
/
deploystudio_users.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
#!/bin/python
# -*- coding: UTF-8 -*-
# insert users deploy studio
from os.path import join, isdir, exists
from os import path, listdir
import argparse
import sys
new_user_code = "\t<key>dstudio-users</key>\n\
<array>\n\
\t<dict>\n\
\t\t<key>dstudio-user-admin-status</key>\n\
\t\t<string>YES</string>\n\
\t\t<key>dstudio-user-hidden-status</key>\n\
\t\t<string>NO</string>\n\
\t\t<key>dstudio-user-name</key>\n\
\t\t<string>{0}</string>\n\
\t\t<key>dstudio-user-password</key>\n\
\t\t<string>{2}</string>\n\
\t\t<key>dstudio-user-shortname</key>\n\
\t\t<string>{1}</string>\n\
\t</dict>\n\
</array>\n"
valid_header_options = ["yes", "y", "no", "n"]
print "************************************"
print "* Laptop Mass User Creation Script *"
print "* for DeployStudio *"
print "* *"
print "* Created by: Cian Byrne 2016 (c) *"
print "************************************"
def main():
#
# Files to Import
#
# Change these here to the file you need.
#
parser = argparse.ArgumentParser(description='Insert users into DeployStudio installation.')
parser.add_argument("-r", "--repo", type=str, nargs=1, required=True,
help="The path to the root of your DeployStudio repository location. E.G. /Users/Shared/repository")
parser.add_argument("-f", "--file", type=str, nargs=1, required=True,
help="The full path to the CSV file containing usernames. E.G. ~/Desktop/usernames.csv")
parser.add_argument("-H", "--header", type=str, nargs=1, required=True,
help="Does the provided file contain headers? (y or n)")
args = parser.parse_args()
# file with usernames and serial numbers
# FORMAT: Serial Number, Name, Short Name, Password
#
# NOTE: 'Name' is what is displayed to the user.
# 'Short Name' is what is used for authentication and home directory.
#
if args.file:
user_file_path = args.file[0]
# check that file exists:
if exists(user_file_path):
pass
else:
print "CSV file does not exists. Please try again."
sys.exit()
# change this option if you are running this file other than in the folder
# with the .plist files in it.
if args.repo:
# check that the repo directory exists and is real
deploystudio_path = join(args.repo[0], "Databases", "ByHost")
if isdir(deploystudio_path):
pass
else:
print "Invalid DeployStudio path. Please try again."
sys.exit()
# change this option if your file has headers...
if args.header:
user_file_header = args.header[0]
if user_file_header in valid_header_options:
if user_file_header == "yes" or "y":
user_file_header = True
else:
user_file_header = False
else:
print "Invalid header option. Please try again."
sys.exit()
#
# Openning Required Files
#
print "Getting file...{0}".format(user_file_path)
user_file = open(user_file_path, 'r')
print "Beginning process..."
# initialise variables...
usernames = {}
row = 0
# load all serial numbers and usernames into dict
for line in user_file:
if row == 0 and user_file_header:
# skip top row
row += 1
else:
# parse line of input... FORMAT: Serial Number, Name, Short Name, Password
line = line.split(',')
usernames[line[0]] = {'name' : line[1], 'shortname' : line[2], 'password' : line[3].strip("\n") }
row += 1
row = 0
# grab the file and unpack it.
for sn in usernames:
try:
# open .plist file...
plist_file = open(join(deploystudio_path,'{0}.plist'.format(sn)))
# read .plist file...
contents = plist_file.readlines()
# close .plist file...
plist_file.close()
# find if a user is already added...
search_flag = True
for line in contents:
# then skip / do nothing...
if 'dstudio-users' in line:
search_flag = False
print "{0} already has a user!".format(sn)
# no user is in file, insert one from the imported file.
if search_flag:
# insert settings for users...
contents.insert(-2, new_user_code.format(usernames[sn]['name'], usernames[sn]['shortname'],
usernames[sn]['password']))
# open the .plist file
plist_file = open(join(deploystudio_path,'{0}.plist'.format(sn)),'w')
# insert the string (above) with username...
for line in contents:
plist_file.write(line)
# close the .plist file...
plist_file.close()
# report back that a record has been inserted...
print "RECORD INSERTED!"
row += 1
except IOError:
# triggered because the file does not exist.
print "ERROR: {0}.plist cannot be found".format(sn)
continue
print "There were {0} records modified.".format(row)
print "PROGRAM COMPLETE SUCCESSFULLY!"
if __name__ == '__main__':
main()