2626from os import makedirs
2727from os .path import exists , join
2828
29- template = '''DEFAULT default
29+ restore_template = '''DEFAULT default
3030PROMPT 1
3131TIMEOUT 26
3232DISPLAY boot.msg
3535APPEND vga=normal devfs=nomount pxe ramdisk_size=66000 load_ramdisk=1 init=/linuxrc prompt_ramdisk=0 initrd=initrd.gz root=/dev/ram0 rw noapic nolapic lba combined_mode=libata ide0=noprobe nomce pci=nomsi irqpoll quiet Server="%s" Share="%s" Directory="%s" Image_To_Restore="%s" After_Completion="Reboot" CIFS_Preferred="Y" Zsplit_Preferred="Y" AUTO="Y" User="%s" Passwd="%s" Extend_Parts_Whenever_Possible="N" Replace_BIOS="N" IP="%s" Netmask="%s" Gateway="%s"
3636'''
3737
38+ backup_template = '''DEFAULT default
39+ PROMPT 1
40+ TIMEOUT 26
41+ DISPLAY boot.msg
42+ LABEL default
43+ KERNEL kernel
44+ APPEND vga=normal devfs=nomount pxe ramdisk_size=66000 load_ramdisk=1 init=/linuxrc prompt_ramdisk=0 initrd=initrd.gz root=/dev/ram0 rw noapic nolapic lba combined_mode=libata ide0=noprobe nomce pci=nomsi irqpoll quiet Server="%s" Share="%s" Directory="%s" Image_To_Restore="Create_New_Image" New_Image_Name="%s" Already_Existing_Image="Replace" Store_MD5="N" Compression_Type="gzip" After_Completion="Reboot" Minimize_Before_Storing="N" Repart="N" CIFS_Preferred="Y" AUTO="Y" User="%s" Passwd="%s" Extend_Parts_Whenever_Possible="N" Replace_BIOS="N" IP="%s" Netmask="%s" Gateway="%s"
45+ '''
46+
47+
48+ cmd = ''
3849tftp_dir = ''
3950mac = ''
4051cifs_server = ''
4152share = ''
4253directory = ''
43- image_to_restore = ''
54+ template_dir = ''
4455cifs_username = ''
4556cifs_password = ''
4657ip = ''
4758netmask = ''
4859gateway = ''
4960
50- def prepare_boot_file ( ):
61+ def prepare ( is_restore ):
5162 try :
5263 pxelinux = join (tftp_dir , "pxelinux.cfg" )
5364 if exists (pxelinux ) == False :
@@ -56,31 +67,32 @@ def prepare_boot_file():
5667 cfg_name = "01-" + mac .replace (':' ,'-' ).lower ()
5768 cfg_path = join (pxelinux , cfg_name )
5869 f = open (cfg_path , "w" )
59- stuff = template % (cifs_server , share , directory , image_to_restore , cifs_username , cifs_password , ip , netmask , gateway )
70+ if is_restore :
71+ fmt = restore_template
72+ else :
73+ fmt = backup_template
74+ stuff = fmt % (cifs_server , share , directory , template_dir , cifs_username , cifs_password , ip , netmask , gateway )
6075 f .write (stuff )
6176 f .close ()
6277 return 0
63- except IOError , e :
78+ except Exception , e :
6479 print e
6580 return 1
6681
82+
6783if __name__ == "__main__" :
6884 if len (sys .argv ) < 12 :
6985 print "Usage: prepare_tftp_bootfile.py tftp_dir mac cifs_server share directory image_to_restor cifs_username cifs_password ip netmask gateway"
7086 exit (1 )
7187
72- tftp_dir = sys .argv [1 ]
73- mac = sys .argv [2 ]
74- cifs_server = sys .argv [3 ]
75- share = sys .argv [4 ]
76- directory = sys .argv [5 ]
77- image_to_restore = sys .argv [6 ]
78- cifs_username = sys .argv [7 ]
79- cifs_password = sys .argv [8 ]
80- ip = sys .argv [9 ]
81- netmask = sys .argv [10 ]
82- gateway = sys .argv [11 ]
88+ (cmd , tftp_dir , mac , cifs_server , share , directory , template_dir , cifs_username , cifs_password , ip , netmask , gateway ) = sys .argv [1 :]
8389
84-
85- ret = prepare_boot_file ()
90+ if cmd == "restore" :
91+ ret = prepare (True )
92+ elif cmd == "backup" :
93+ ret = prepare (False )
94+ else :
95+ print "Unknown cmd: %s" % cmd
96+ ret = 1
97+
8698 exit (ret )
0 commit comments