hbnet/web/gen_script_template.py

44 lines
1.4 KiB
Python

# Function to generate a script for Pi-Star that adds MASTER instances and generated passphrases to DMR_Hosts.txt.
def gen_script(dmr_id, svr_lst):
auth_contents = ''
header = '''
## DMR_hosts.txt, generated by HBNet.
########################################################################################################
### Name DMR-ID IP/Hostname Password Port #
########################################################################################################
\n'''
for i in svr_lst:
## print(i[1])
auth_contents = auth_contents + str(i[0]) + '''\t\t\t\t''' + str(dmr_id) + '''\t''' + i[1] + '''\t\t\t\t''' + i[2] + '''\t\t''' + str(i[3]) + '\n'
## print(header)
## print(auth_contents)
## return header + auth_contents
output = '''
#!/usr/bin/python3
import os
from pathlib import Path
os.chdir('/root')
if Path('/root/DMR_Hosts.txt').is_file():
print('DMR_Hosts.txt exists, adding entries...')
with open('/root/DMR_Hosts.txt', 'a') as dmr_hosts:
dmr_hosts.write("""''' + auth_contents + '''""")
dmr_hosts.close()
else:
Path('/root/DMR_Hosts.txt').touch()
print('DMR_Hosts.txt does not exist, creating...')
with open('/root/DMR_Hosts.txt', 'w') as dmr_hosts:
dmr_hosts.write("""''' + header + auth_contents + '""")' + '''
dmr_hosts.close()
print('DMR Host file updates.')'''
print(output)
return output