История изменений
Исправление constin, (текущая версия) :
Полная версия ( я знаю, что это никому не интересно и не нужно)
#!/usr/bin/env python3
import sys
import subprocess
from subprocess import Popen, PIPE
import re
# tools
def run_command(cmd, echo=True, exit_on_error=False):
p = Popen(cmd, stdout=subprocess.PIPE,stderr=PIPE,shell=True,
universal_newlines=True)
o, e = p.communicate()
if echo:
print('{}{}'.format(o, e))
if p.returncode != 0 and exit_on_error:
quit('Error: {}{} Exit script'.format(o,e))
return ('{}{}'.format(o, e), p.returncode)
def printhelp():
pass
def delete_from_export(dataset):
run_command(
'grep -v {} /etc/exports > /tmp/exports && mv /tmp/exports \
/etc/exports'.format(dataset), echo=False)
run_command('exportfs -ra', exit_on_error=True)
def pxe_boot_config(ip, dataset, label):
config_list = [
"DEFAULT vesamenu.c32",
"PROMPT 0",
"MENU TITLE PXE BOOT " + label + " ZFS " + ip,
"LABEL pxe-ro",
"MENU LABEL PXE",
"KERNEL vmlinuz",
"APPEND root=/dev/nfs initrd=initrd.img nfsroot="+ip+":/"+dataset+" ip=dhcp ro quiet splash",
"IPAPPEND 3",
"LABEL pxe-rw",
"MENU LABEL PXE Administrator",
"KERNEL vmlinuz",
"MENU PASSWD XXXX",
"APPEND root=/dev/nfs initrd=initrd.img nfsroot=" +ip + ":/" + dataset + " ip=dhcp rw quiet",
"IPAPPEND 3",
"ONTIMEOUT pxe-ro",
"TIMEOUT 50",
"MENU COLOR border 30;44 #40ffffff #a0000000 std",
"MENU COLOR title 1;36;44 #9033ccff #a0000000 std",
"MENU COLOR sel 7;37;40 #e0000000 #20ffffff all",
"MENU COLOR unsel 37;44 #50ffffff #a0000000 std",
"MENU COLOR help 37;40 #c0ffffff #a0000000 std",
"MENU COLOR timeout_msg 37;40 #80ffffff #00000000 std",
"MENU COLOR timeout 1;37;40 #c0ffffff #00000000 std",
"MENU COLOR msg07 37;40 #90ffffff #a0000000 std",
"MENU COLOR tabmsg 37;40 #e0ffffff #a0000000 std",
"MENU COLOR disabled 37;44 #50ffffff #a0000000 std",
"MENU COLOR hotkey 1;30;47 #ffff0000 #a0000000 std",
"MENU COLOR hotsel 1;7;30;47 #ffff0000 #20ffffff all",
"MENU COLOR scrollbar 30;47 #ffff0000 #00000000 std",
"MENU COLOR cmdmark 1;36;47 #e0ff0000 #00000000 std",
"MENU COLOR cmdline 30;47 #ff000000 #00000000 none"
]
f = open('/srv/tftp/pxelinux.cfg/{}.cfg'.format(label), "a")
for line in config_list:
f.write(line + "\n")
f.close()
# main fuctions
def clone(snapname, dataset):
run_command('zfs clone {} {}'.format(snapname,dataset), exit_on_error=True)
if dataset in open('/etc/exports').read():
print('{} already exist in /etc/exports'.format(dataset))
else:
f = open('/etc/exports', "a")
f.write('/{} 192.168.0.0/24(rw,no_root_squash,async,insecure,no_subtree_check)\n'.format(dataset))
f.flush()
f.close()
run, error= run_command('exportfs -ra')
if error != 0:
print("Rolling changes back.Exiting script")
print("clean /etc/export")
delete_from_export(dataset)
print("destroy zfs clone")
run_command('zfs destroy {}'.format(dataset))
print("done")
quit()
# # TODO write reroll
label = dataset.replace("/", "-")
list = ["LABEL " + label, "MENU LABEL " + label, "KERNEL vesamenu.c32",
"MENU PASSWD XXX", "APPEND pxelinux.cfg/" + label + ".cfg"]
f = open('/srv/tftp/default.cfg', "r+")
for line in f:
if 'SEPARATOR' in line:
for item in list:
f.write(item + "\n")
break
f.close()
pxe_ip, er =run_command("hostname -I| cut -d' ' -f1",echo=False,exit_on_error=True)
pxe_boot_config(pxe_ip.strip(),dataset, label)
def destroy(dataset):
run_command('zfs destroy {}'.format(dataset))
delete_from_export(dataset)
label = dataset.replace("/", "-")
run_command('sed -i "/LABEL {}/,/{}.cfg/d" /srv/tftp/default.cfg'.format(label,label))
run_command('rm /srv/tftp/pxelinux.cfg/{}.cfg'.format(label))
if sys.argv[1] == "clone":
clone(sys.argv[2],sys.argv[3])
elif sys.argv[1] == "destroy":
destroy(sys.argv[2])
else:
printhelp()
Исходная версия constin, :
Полная версия ( я знаю, что это никому не интересно и не нужно)
#!/usr/bin/env python3
import sys
import os
import subprocess
from subprocess import Popen, PIPE
import re
# tools
def run_command(cmd, echo=True, exit_on_error=False):
p = Popen(cmd, stdout=subprocess.PIPE,stderr=PIPE,shell=True,
universal_newlines=True)
o, e = p.communicate()
if echo:
print('{}{}'.format(o, e))
if p.returncode != 0 and exit_on_error:
quit('Error: {}{} Exit script'.format(o,e))
return ('{}{}'.format(o, e), p.returncode)
def printhelp():
pass
def delete_from_export(dataset):
run_command(
'grep -v {} /etc/exports > /tmp/exports && mv /tmp/exports \
/etc/exports'.format(dataset), echo=False)
run_command('exportfs -ra', exit_on_error=True)
def pxe_boot_config(ip, dataset, label):
config_list = [
"DEFAULT vesamenu.c32",
"PROMPT 0",
"MENU TITLE PXE BOOT " + label + " ZFS " + ip,
"LABEL pxe-ro",
"MENU LABEL PXE",
"KERNEL vmlinuz",
"APPEND root=/dev/nfs initrd=initrd.img nfsroot="+ip+":/"+dataset+" ip=dhcp ro quiet splash",
"IPAPPEND 3",
"LABEL pxe-rw",
"MENU LABEL PXE Administrator",
"KERNEL vmlinuz",
"MENU PASSWD XXXX",
"APPEND root=/dev/nfs initrd=initrd.img nfsroot=" +ip + ":/" + dataset + " ip=dhcp rw quiet",
"IPAPPEND 3",
"ONTIMEOUT pxe-ro",
"TIMEOUT 50",
"MENU COLOR border 30;44 #40ffffff #a0000000 std",
"MENU COLOR title 1;36;44 #9033ccff #a0000000 std",
"MENU COLOR sel 7;37;40 #e0000000 #20ffffff all",
"MENU COLOR unsel 37;44 #50ffffff #a0000000 std",
"MENU COLOR help 37;40 #c0ffffff #a0000000 std",
"MENU COLOR timeout_msg 37;40 #80ffffff #00000000 std",
"MENU COLOR timeout 1;37;40 #c0ffffff #00000000 std",
"MENU COLOR msg07 37;40 #90ffffff #a0000000 std",
"MENU COLOR tabmsg 37;40 #e0ffffff #a0000000 std",
"MENU COLOR disabled 37;44 #50ffffff #a0000000 std",
"MENU COLOR hotkey 1;30;47 #ffff0000 #a0000000 std",
"MENU COLOR hotsel 1;7;30;47 #ffff0000 #20ffffff all",
"MENU COLOR scrollbar 30;47 #ffff0000 #00000000 std",
"MENU COLOR cmdmark 1;36;47 #e0ff0000 #00000000 std",
"MENU COLOR cmdline 30;47 #ff000000 #00000000 none"
]
f = open('/srv/tftp/pxelinux.cfg/{}.cfg'.format(label), "a")
for line in config_list:
f.write(line + "\n")
f.close()
# main fuctions
def clone(snapname, dataset):
run_command('zfs clone {} {}'.format(snapname,dataset), exit_on_error=True)
if dataset in open('/etc/exports').read():
print('{} already exist in /etc/exports'.format(dataset))
else:
f = open('/etc/exports', "a")
f.write('/{} 192.168.0.0/24(rw,no_root_squash,async,insecure,no_subtree_check)\n'.format(dataset))
f.flush()
f.close()
run, error= run_command('exportfs -ra')
if error != 0:
print("Rolling changes back.Exiting script")
print("clean /etc/export")
delete_from_export(dataset)
print("destroy zfs clone")
run_command('zfs destroy {}'.format(dataset))
print("done")
quit()
# # TODO write reroll
label = dataset.replace("/", "-")
list = ["LABEL " + label, "MENU LABEL " + label, "KERNEL vesamenu.c32",
"MENU PASSWD XXX", "APPEND pxelinux.cfg/" + label + ".cfg"]
f = open('/srv/tftp/default.cfg', "r+")
for line in f:
if 'SEPARATOR' in line:
for item in list:
f.write(item + "\n")
break
f.close()
pxe_ip, er =run_command("hostname -I| cut -d' ' -f1",echo=False,exit_on_error=True)
pxe_boot_config(pxe_ip.strip(),dataset, label)
def destroy(dataset):
run_command('zfs destroy {}'.format(dataset))
delete_from_export(dataset)
label = dataset.replace("/", "-")
run_command('sed -i "/LABEL {}/,/{}.cfg/d" /srv/tftp/default.cfg'.format(label,label))
run_command('rm /srv/tftp/pxelinux.cfg/{}.cfg'.format(label))
if sys.argv[1] == "clone":
clone(sys.argv[2],sys.argv[3])
elif sys.argv[1] == "destroy":
destroy(sys.argv[2])
else:
printhelp()