Changed the JSON structure to be able to pass args via the network

This commit is contained in:
Lord Anton Hvornum 2018-04-08 12:21:46 +02:00
parent ad8a4fd65e
commit 456db37a90
2 changed files with 78 additions and 53 deletions

View File

@ -6,6 +6,7 @@ from glob import glob
from socket import socket, inet_ntoa, AF_INET, AF_INET6, AF_PACKET
from collections import OrderedDict as oDict
from subprocess import Popen, STDOUT, PIPE
from time import sleep
rootdir_pattern = re.compile('^.*?/devices')
harddrives = oDict()
@ -149,20 +150,46 @@ if __name__ == '__main__':
if not 'country' in args: args['country'] = 'SE' #all
if not 'packages' in args: args['packages'] = ''
if not 'post' in args: args['post'] = 'reboot'
if not 'password' in args: args['password'] = '0000'
## == If we got networking,
# Try fetching instructions for this box and execute them.
if get_default_gateway_linux():
locmac = get_local_MACs()
for mac in locmac:
try:
instructions = grab_url_data('https://raw.githubusercontent.com/Torxed/archinstall/net-deploy/deployments/{}.json'.format(mac))
except urllib.error.HTTPError:
print('[N] No instructions for this box on this mac: {}'.format(mac))
continue
#print('Decoding:', instructions)
try:
instructions = json.loads(instructions.decode('UTF-8'), object_pairs_hook=oDict)
except:
print('[E] JSON instructions failed to load for {}'.format(mac))
instructions = {}
sleep(5)
continue
if 'args' in instructions:
for key, val in instructions['args'].items():
args[key] = val
print(args)
if not os.path.isfile(args['pwfile']):
PIN = '0000'
#PIN = '0000'
with open(args['pwfile'], 'w') as pw:
pw.write(PIN)
else:
## TODO: Convert to `rb` instead.
# We shouldn't discriminate \xfu from being a passwd phrase.
with open(args['pwfile'], 'r') as pw:
PIN = pw.read().strip()
pw.write(args['password'])
#else:
# ## TODO: Convert to `rb` instead.
# # We shouldn't discriminate \xfu from being a passwd phrase.
# with open(args['pwfile'], 'r') as pw:
# PIN = pw.read().strip()
print()
print('[!] Disk PASSWORD is: {}'.format(PIN))
print('[!] Disk PASSWORD is: {}'.format(args['password']))
print()
print('[N] Setting up {drive}.'.format(**args))
# dd if=/dev/random of=args['drive'] bs=4096 status=progress
@ -220,12 +247,12 @@ if __name__ == '__main__':
o = run('arch-chroot /mnt chmod 700 /root')
## == Passwords
# o = run('arch-chroot /mnt usermod --password {} root'.format(PIN))
# o = run("arch-chroot /mnt sh -c 'echo {pin} | passwd --stdin root'".format(pin='"{pin}"'.format(**args, pin=PIN)), echo=True)
o = run("arch-chroot /mnt sh -c \"echo 'root:{pin}' | chpasswd\"".format(**args, pin=PIN))
# o = run('arch-chroot /mnt usermod --password {} root'.format(args['password']))
# o = run("arch-chroot /mnt sh -c 'echo {pin} | passwd --stdin root'".format(pin='"{pin}"'.format(**args, pin=args['password'])), echo=True)
o = run("arch-chroot /mnt sh -c \"echo 'root:{pin}' | chpasswd\"".format(**args, pin=args['password']))
if 'user' in args:
o = run('arch-chroot /mnt useradd -m -G wheel {user}'.format(**args))
o = run("arch-chroot /mnt sh -c \"echo '{user}:{pin}' | chpasswd\"".format(**args, pin=PIN))
o = run("arch-chroot /mnt sh -c \"echo '{user}:{pin}' | chpasswd\"".format(**args, pin=args['password']))
with open('/mnt/etc/mkinitcpio.conf', 'w') as mkinit:
## TODO: Don't replace it, in case some update in the future actually adds something.
@ -250,30 +277,22 @@ if __name__ == '__main__':
entry.write('initrd /initramfs-linux.img\n')
entry.write('options cryptdevice=UUID={UUID}:luksdev root=/dev/mapper/luksdev rw intel_pstate=no_hwp\n'.format(UUID=UUID))
## == If we got networking,
# Try fetching instructions for this box and execute them.
if get_default_gateway_linux():
locmac = get_local_MACs()
for mac in locmac:
try:
instructions = grab_url_data('https://raw.githubusercontent.com/Torxed/archinstall/net-deploy/deployments/{}.json'.format(mac))
except urllib.error.HTTPError:
print('[N] No instructions for this box on this mac: {}'.format(mac))
continue
#print('Decoding:', instructions)
instructions = json.loads(instructions.decode('UTF-8'), object_pairs_hook=oDict)
for title in instructions:
print('[N] Network Deploy: {}'.format(title))
for command in instructions[title]:
opts = instructions[title][command] if type(instructions[title][command]) in (dict, oDict) else {}
conf = {}
if 'post' in instructions:
conf = instructions['post']
elif not 'args' in instructions and len(instructions):
conf = instructions
#print('[N] Command: {} ({})'.format(command, opts))
o = run('arch-chroot /mnt {c}'.format(c=command), **opts)
if type(instructions[title][command]) == bytes and len(instructions[title][command]) and not instructions[title][command] in o:
print('[W] Post install command failed: {}'.format(o.decode('UTF-8')))
#print(o)
for title in conf:
print('[N] Network Deploy: {}'.format(title))
for command in conf[title]:
opts = conf[title][command] if type(conf[title][command]) in (dict, oDict) else {}
#print('[N] Command: {} ({})'.format(command, opts))
o = run('arch-chroot /mnt {c}'.format(c=command), **opts)
if type(conf[title][command]) == bytes and len(conf[title][command]) and not conf[title][command] in o:
print('[W] Post install command failed: {}'.format(o.decode('UTF-8')))
#print(o)
o = run('umount -R /mnt')
if args['post'] == 'reboot':

View File

@ -1,22 +1,28 @@
{
"Setup temp build env": {
"pacman -Syy --noconfirm git" : null,
"useradd -m -G wheel builder" : null,
"sed -i 's/# %wheel ALL=(ALL) NO/%wheel ALL=(ALL) NO/' /etc/sudoers" : null
"args" : {
"post" : "stay",
"password" : "0001"
},
"install lighttpd2-git": {
"git clone https://aur.archlinux.org/lighttpd2-git.git /home/builder/lighttpd2" : null,
"chown -R builder.builder /home/builder/lighttpd2" : null,
"su - builder -c \"(cd /home/builder/lighttpd2/; /usr/bin/makepkg -s --noconfirm)\"" : null,
"sh -c 'pacman -U --noconfirm /home/builder/lighttpd2/*.xz'" : null
},
"Remove temp build env": {
"rm -rf /home/builder/lighttpd2" : null,
"sed -i 's/%wheel ALL=(ALL) NO/# %wheel ALL=(ALL) NO/' /etc/sudoers" : null
},
"Create mirror": {
"mkdir -p /srv/http/archlinux/arch_offline/os/x86_64
"pacman --dbpath /tmp/ -Syu -w --cachedir /srv/http/archlinux/arch_offline/os/x86_64 base base-devel git python python-systemd awesome xorg-xinit xorg-server xorg-server-utils xterm nano screen sudo iptables mesa-libgl dhclient dnsmasq darkhttpd openssh sshfs openssl openvpn gcc" : null,
"repo-add /srv/http/archlinux/arch_offline/os/x86_64/arch_offline.db.tar.gz /srv/http/archlinux/arch_offline/os/x86_64/*.pkg.tar.xz" : null,
"post" : {
"Setup temp build env": {
"pacman -Syy --noconfirm git" : null,
"useradd -m -G wheel builder" : null,
"sed -i 's/# %wheel ALL=(ALL) NO/%wheel ALL=(ALL) NO/' /etc/sudoers" : null
},
"install lighttpd2-git": {
"git clone https://aur.archlinux.org/lighttpd2-git.git /home/builder/lighttpd2" : null,
"chown -R builder.builder /home/builder/lighttpd2" : null,
"su - builder -c \"(cd /home/builder/lighttpd2/; /usr/bin/makepkg -s --noconfirm)\"" : null,
"sh -c 'pacman -U --noconfirm /home/builder/lighttpd2/*.xz'" : null
},
"Remove temp build env": {
"rm -rf /home/builder/lighttpd2" : null,
"sed -i 's/%wheel ALL=(ALL) NO/# %wheel ALL=(ALL) NO/' /etc/sudoers" : null
},
"Create mirror": {
"mkdir -p /srv/http/archlinux/arch_offline/os/x86_64" : null,
"pacman --dbpath /tmp/ -Syu -w --cachedir /srv/http/archlinux/arch_offline/os/x86_64 base base-devel git python python-systemd awesome xorg-xinit xorg-server xorg-server-utils xterm nano screen sudo iptables mesa-libgl dhclient dnsmasq darkhttpd openssh sshfs openssl openvpn gcc" : null,
"repo-add /srv/http/archlinux/arch_offline/os/x86_64/arch_offline.db.tar.gz /srv/http/archlinux/arch_offline/os/x86_64/*.pkg.tar.xz" : null,
}
}
}