-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstaller.py
executable file
·344 lines (248 loc) · 8.29 KB
/
installer.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/bin/python3
# Modules import
# My imports
from modules import parse_arguments, loadYamlFromFile
import subprocess
import os
class colors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class STATUS:
PROGRESS = '[.]'
OK = f'[{colors.OKGREEN}\u2714{colors.ENDC}]'
ERROR = f'[{colors.FAIL}\u2714{colors.ENDC}]'
class Executor:
ULTRALOG = False
LOGERROR = False
@staticmethod
def execute(command: str):
if Executor.ULTRALOG:
os.system(command)
return 0
elif Executor.LOGERROR:
"""
process = subprocess.Popen(
command, shell=True, stdout=subprocess.PIPE)
process.wait()
"""
process = subprocess.run(
command, shell=True, stdout=subprocess.PIPE)
return process.returncode
else:
"""
process = subprocess.Popen(
command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
process.wait()
"""
process = subprocess.run(
command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
return process.returncode
def execute(command: str, description: str):
message = f'{STATUS.PROGRESS} {description}'
print(message, end='', flush=True)
returncode = Executor.execute(command)
if returncode == 0:
message = f'{STATUS.OK} {description}'
else:
message = f'{STATUS.ERROR} {description}'
print(f'\r{message}')
def setKeyMap(keymap: str):
description = f'Settings keymap to {keymap}'
command = f'loadkeys {keymap}'
execute(command, description)
def setTimeProtocol(ntp: bool):
description = f'Settings NTP to {ntp}'
command = f'timedatectl set-ntp {ntp}'
execute(command, description)
def mountLinuxPartition(linux_partition: str):
description = f'Mounting Linux Partition from {linux_partition}'
command = f'mount {linux_partition} /mnt'
execute(command, description)
def mountEfiPartition(efi_partition: str, path: str):
description = f'Mounting Efi Partition from {efi_partition} to {path}'
command = f'mkdir -p /mnt{path}; mount {efi_partition} /mnt{path}'
execute(command, description)
def installPackage(package: str):
description = f'Installing {package}'
command = f'pacstrap /mnt {package} --noconfirm'
execute(command, description)
def generateFstab():
description = 'Generating fstab'
command = 'genfstab -U /mnt >> /mnt/etc/fstab'
execute(command, description)
def copyInstallerInMNT():
description = 'Copying installer to /mnt'
command = 'cp -r ../archinstaller /mnt'
execute(command, description)
def installPyaml():
description = 'Installing PyYaml'
command = 'pacstrap /mnt python-yaml'
execute(command, description)
def chrootAndExecute():
description = 'Running chroot'
command = 'arch-chroot /mnt /archinstaller/installer.py /archinstaller/config.yml --chroot' + \
(' --logerror' if Executor.LOGERROR else '')
os.system(command)
def reboot():
print("\n\nInstallation completed!!\n\n")
input("Press enter to reboot")
os.system('reboot')
def archiso(data):
partitions = data['partitions']
boot = data['boot']
settings = data['settings']
packages = data['packages']
# Set keymap
keymap = settings['keymap']
setKeyMap(keymap)
# Set time protocol
ntp = settings['ntp']
setTimeProtocol(ntp)
# Mounting partitions
linux_partition = partitions['linux']
efi_partition = partitions['efi']
efi_directory = boot['efi-directory']
mountLinuxPartition(linux_partition)
mountEfiPartition(efi_partition, efi_directory)
# Installing packages
for package in packages:
installPackage(package)
# Generate fstab
generateFstab()
# Copying installer to /mnt
copyInstallerInMNT()
# Installing PyYaml
installPyaml()
# Chroot and execute
chrootAndExecute()
# Rebooting
reboot()
def linkTime(region: str, location: str):
description = f'Linking time to {location}/{region}'
command = f'ln -sf /usr/share/zoneinfo/{region}/{location} /etc/localtime'
execute(command, description)
def setHwClock():
description = 'Setting hwclock'
command = 'hwclock --systohc'
execute(command, description)
def addLocale(locale: str):
description = f'Adding locale {locale}'
command = f'echo {locale} >> /etc/locale.gen; locale-gen'
execute(command, description)
def addLang(lang: str):
description = f'Adding lang {lang}'
command = f'echo "LANG={lang}" >> /etc/locale.conf'
execute(command, description)
def addKeyMap(keymap: str):
description = f'Adding keymap {keymap}'
command = f'echo "KEYMAP={keymap}" >> /etc/vconsole.conf'
execute(command, description)
def addHostname(hostname: str):
description = f'Adding hostname {hostname}'
command = f'echo "{hostname}" > /etc/hostname'
execute(command, description)
def setupHosts():
description = 'Setting up hosts'
command = 'echo "127.0.0.1 localhost.localdomain localhost" > /etc/hosts'
execute(command, description)
def mkinit():
description = 'mkinit'
command = 'mkinitcpio -P'
execute(command, description)
def grubInstall(target, efi_directory, bootloader_id):
description = f'Installing GRUB to {target}'
command = f'grub-install --target={target} --efi-directory={efi_directory} --bootloader-id={bootloader_id} --recheck'
execute(command, description)
def mkconfig():
description = 'mkconfig'
command = 'grub-mkconfig -o /boot/grub/grub.cfg'
execute(command, description)
def setPasswForRoot(password: str):
description = f'Setting root password to {password}'
command = f'echo -e "{password}\n{password}" | passwd root'
execute(command, description)
def createUser(username: str, password: str):
description = f'Creating user {username} with password {password}'
command = f'useradd -m -G wheel "{username}"; echo -e "{password}\n{password}" | passwd {username}'
execute(command, description)
def addWheelToSudoers():
description = 'Adding wheel group to sudoers'
command = 'echo "%wheel ALL=(ALL) ALL" | EDITOR="tee -a" visudo'
execute(command, description)
def enableSddm():
description = 'Enabling sddm'
command = 'systemctl enable sddm.service'
execute(command, description)
def enableNetworkManager():
description = 'Enabling NetworkManager'
command = 'systemctl enable NetworkManager.service'
execute(command, description)
def chroot(data):
partitions = data['partitions']
boot = data['boot']
settings = data['settings']
packages = data['packages']
accounts = data['accounts']
# Link time
region = settings['region']
location = settings['location']
linkTime(region, location)
# Set hwclock
setHwClock()
# Add locale
locale = settings['locale']
addLocale(locale)
# Add lang
lang = settings['lang']
addLang(lang)
# Add keymap
keymap = settings['keymap']
addKeyMap(keymap)
# Add hostname
hostname = settings['hostname']
addHostname(hostname)
# Setup hosts
setupHosts()
# mkinit
mkinit()
# Grub install
target = boot['target']
efi_directory = boot['efi-directory']
bootloader_id = boot['bootloader-id']
grubInstall(target, efi_directory, bootloader_id)
# mkconfig
mkconfig()
# Set root password
password = accounts['root-password']
setPasswForRoot(password)
# Create user
username = accounts['username']
password = accounts['password']
createUser(username, password)
# Add wheel to sudoers
addWheelToSudoers()
# Enable sddm
enableSddm()
# Enable NetworkManager
enableNetworkManager()
os.system('exit')
exit(0)
if __name__ == "__main__":
args = parse_arguments()
configurationFilePath = args.file
data = loadYamlFromFile(configurationFilePath)
if args.ultralog:
Executor.ULTRALOG = True
if args.logerror:
Executor.LOGERROR = True
if args.chroot:
chroot(data)
else:
archiso(data)