Advertisement
Guest User

Untitled

a guest
Mar 25th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.75 KB | None | 0 0
  1. from xml.etree import ElementTree
  2. import sys
  3. import time
  4. import os
  5. import urllib
  6. import zipfile
  7. import shutil
  8. import socket
  9. import platform
  10. import py_compile
  11.  
  12.  
  13. # --Constants
  14.  
  15. MANAGER_FOLDER = "C:\\_RocketManager\\"
  16.  
  17. ROCKET_EXTRACT_FOLDER = MANAGER_FOLDER + "last_rocket_download"
  18. BACKUP_BUNDLES_FOLDER = MANAGER_FOLDER + "cpy_bundles"
  19.  
  20. # For Win
  21. URL_ROCKET_BETA = "http://api.rocket.foundation/download/unturned/latest/"
  22. URL_STEAM_WIN = "http://media.steampowered.com/installer/steamcmd.zip"
  23.  
  24. OUTPUT_ZIP_STEAM_WIN = MANAGER_FOLDER + "steam_temp.zip"
  25. OUTPUT_ZIP_ROCKET = MANAGER_FOLDER + "rocket_temp.zip"
  26.  
  27. #For Linux
  28.  
  29. URL_ROCKET_LINUX = "http://api.rocket.foundation/download/unturned-linux/latest/"
  30. URL_STEAM_LINUX = "https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz"
  31.  
  32. PROCNAME_WIN = "Unturned.exe"
  33.  
  34. #--Functions
  35. def write_config(name):
  36. f = open(name, "w")
  37. f.write('''<?xml version="1.0" encoding="UTF-8"?>
  38. <config>
  39. <rebootEvery seconds="3600" />
  40. <unturnedFolder path=".\unturned" recoveryBundlesAfterUpdates="false" />
  41. <rocket useRocket="true" apikey=""/>
  42. <steam username="" password="" />
  43. <steamUpdates validate="true" />
  44. <servers rconEnabled="false">
  45. <server name="server1" rconPort="27013" rconPassword="pass" />
  46. <server name="server2" rconPort="27014" rconPassword="pass" />
  47. </servers>
  48. <notifyBefore seconds="60" />
  49. </config>''')
  50. f.close()
  51.  
  52. def load_config(name):
  53. global REBOOT_TIME
  54. global NOTIFY_TIME
  55. global SERVERS_TO_LAUNCH
  56. global RCON_ENABLED
  57. global RCON_PORT
  58. global RCON_PASSWORD
  59. global UNTURNED_PATH
  60. global VALIDATE_AT_BOOT
  61. global STEAM_USER
  62. global STEAM_PASS
  63. global APIKEY
  64. global BACKUP_BUNDLES
  65. global ROCKET_ENABLED
  66.  
  67. if (not os.path.isfile(name)):
  68. write_config(name)
  69. return True
  70. try:
  71. with open(name, 'rt') as f:
  72. tree = ElementTree.parse(f)
  73.  
  74. node = tree.find("rebootEvery")
  75. REBOOT_TIME = int(node.attrib.get("seconds"))
  76.  
  77. node = tree.find("steamUpdates")
  78. VALIDATE_AT_BOOT = node.attrib.get("validate")
  79.  
  80. node = tree.find("unturnedFolder")
  81. UNTURNED_PATH = node.attrib.get("path")
  82. BACKUP_BUNDLES = node.attrib.get("recoveryBundlesAfterUpdates")
  83. if(VALIDATE_AT_BOOT != "true"):
  84. BACKUP_BUNDLES = "false"
  85.  
  86. node = tree.find("rocket")
  87. APIKEY = node.attrib.get("apikey")
  88. ROCKET_ENABLED = node.attrib.get("useRocket")
  89.  
  90. node = tree.find("steam")
  91. STEAM_USER = node.attrib.get("username")
  92. STEAM_PASS = node.attrib.get("password")
  93.  
  94. SERVERS_TO_LAUNCH = []
  95. RCON_PASSWORD = []
  96. RCON_PORT = []
  97. for node in tree.iter("server"):
  98. SERVERS_TO_LAUNCH.append(node.attrib.get("name"))
  99. RCON_PORT.append(int(node.attrib.get("rconPort")))
  100. RCON_PASSWORD.append(node.attrib.get("rconPassword"))
  101.  
  102.  
  103. node = tree.find("servers")
  104. RCON_ENABLED = node.attrib.get("rconEnabled")
  105.  
  106. node = tree.find("notifyBefore")
  107. NOTIFY_TIME = int(node.attrib.get("seconds"))
  108. if (NOTIFY_TIME > REBOOT_TIME):
  109. NOTIFY_TIME = REBOOT_TIME
  110. return False
  111.  
  112. except:
  113. write_config(name)
  114. return True
  115.  
  116. def downloader(i):
  117. err = False
  118. if (i == "steam"):
  119. try:
  120. if (platform.system() == "Windows"):
  121. urllib.urlretrieve(URL_STEAM_WIN, OUTPUT_ZIP_STEAM_WIN)
  122. else:
  123. urllib.urlretrieve(URL_STEAM_LINUX, OUTPUT_ZIP_STEAM_WIN)
  124.  
  125. except:
  126. err = True
  127.  
  128. if (i == "rocket"):
  129. try:
  130. if (platform.system() == "Windows"):
  131. urllib.urlretrieve(URL_ROCKET_BETA + APIKEY, OUTPUT_ZIP_ROCKET)
  132. else:
  133. urllib.urlretrieve(URL_ROCKET_LINUX + APIKEY, OUTPUT_ZIP_ROCKET)
  134. except:
  135. err = True
  136. return err
  137.  
  138. def extractor(namezip, folder):
  139. zfile = zipfile.ZipFile(namezip)
  140. if not os.path.exists(folder):
  141. os.makedirs(folder)
  142. zfile.extractall(folder)
  143. zfile.close()
  144.  
  145. def clean_up():
  146. try:
  147. os.remove(OUTPUT_ZIP_STEAM_WIN)
  148. except:
  149. pass
  150. try:
  151. os.remove(OUTPUT_ZIP_ROCKET)
  152. except:
  153. pass
  154.  
  155. def installer(folder):
  156. try:
  157. for f in os.listdir(folder):
  158. if (not os.path.isdir(os.path.join(folder, f))):
  159. src_file = os.path.join(folder, f)
  160. dst_file = os.path.join(UNTURNED_PATH + "\\Unturned_Data\\Managed\\", f)
  161. shutil.copyfile(src_file, dst_file)
  162. return False
  163. except IOError:
  164. return True
  165.  
  166. def merge_files(root_src_dir, root_dst_dir):
  167. try:
  168. for src_dir, dirs, files in os.walk(root_src_dir):
  169. dst_dir = src_dir.replace(root_src_dir, root_dst_dir)
  170. if not os.path.exists(dst_dir):
  171. os.mkdir(dst_dir)
  172. for file_ in files:
  173. src_file = os.path.join(src_dir, file_)
  174. dst_file = os.path.join(dst_dir, file_)
  175. if os.path.exists(dst_file):
  176. os.remove(dst_file)
  177. shutil.move(src_file, dst_dir)
  178. return False
  179. except:
  180. return True
  181.  
  182. def rcon_notify(port, passw):
  183. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  184. s.connect(("127.0.0.1", port))
  185. s.settimeout(5)
  186. s.send("login " + passw + "\n")
  187. s.send("broadcast [Manager] This server will restart in " + str(NOTIFY_TIME) + " seconds\n")
  188. s.send("quit\n")
  189. s.recv(1024)
  190.  
  191. s.close()
  192.  
  193. def rcon_shutdown(port, passw):
  194. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  195. s.connect(("127.0.0.1", port))
  196. s.settimeout(5)
  197. s.send("login " + passw + "\n")
  198. s.send("broadcast Rebooting...\n")
  199. s.send("save\n")
  200. s.send("shutdown\n")
  201. s.recv(1024)
  202.  
  203. s.close()
  204.  
  205.  
  206. def bootstrap():
  207. print("--------------------------------------------------------------------------------")
  208. print(" SergiX44's Rocket Manager 1.8.3 ")
  209. print("--------------------------------------------------------------------------------\n\n")
  210.  
  211. print("> Creating folders...")
  212. if not os.path.exists(MANAGER_FOLDER):
  213. os.makedirs(MANAGER_FOLDER)
  214.  
  215. print("> Loading config...")
  216. if load_config(MANAGER_FOLDER + "config_RocketManager.xml"):
  217. print("Close and edit config_RocketManager.xml, then restart me!")
  218. raw_input("Press any key to continue...")
  219. sys.exit(1)
  220.  
  221. if not os.path.isfile("steamcmd.exe"):
  222. ex = True
  223. while ex:
  224. sel = raw_input("> SteamCMD not found! Would you like download it? (y/n) ")
  225. if sel == "y":
  226. print("> Downloading steamcmd...")
  227. if downloader("steam"):
  228. print("ERROR: Unable to download steam! Please check your internet settings!")
  229. raw_input("Press any key to continue...")
  230. sys.exit(3)
  231.  
  232. zfile = zipfile.ZipFile(OUTPUT_ZIP_STEAM_WIN)
  233. zfile.extractall()
  234. zfile.close()
  235. ex = False
  236. if sel == "n":
  237. ex = False
  238. print("Closing...")
  239. time.sleep(1)
  240. sys.exit(1)
  241.  
  242.  
  243. def main():
  244.  
  245. bootstrap()
  246.  
  247. while 1:
  248. #reloading config
  249. print("> Reloading config...")
  250. if load_config(MANAGER_FOLDER + "config_RocketManager.xml"):
  251. print("> Failed loading config! :( \n"
  252. "Config file regenerated, edit config_RocketManager.xml, then restart me!")
  253. raw_input("Press any key to continue...")
  254. sys.exit(2)
  255.  
  256. #saving bundles
  257. if BACKUP_BUNDLES == "true":
  258. print("> Saving Bundles...")
  259. try:
  260. if not os.path.exists(BACKUP_BUNDLES_FOLDER):
  261. os.makedirs(BACKUP_BUNDLES_FOLDER)
  262. if os.path.exists(BACKUP_BUNDLES_FOLDER + "\\Bundles"):
  263. shutil.rmtree(BACKUP_BUNDLES_FOLDER + "\\Bundles")
  264. shutil.copytree(UNTURNED_PATH + "\\Bundles", BACKUP_BUNDLES_FOLDER + "\\Bundles")
  265. except:
  266. print("ERROR: Cannot saving Bundles, aborting...")
  267.  
  268. #launch steam cmd
  269. if (not os.path.isdir(UNTURNED_PATH)) or (VALIDATE_AT_BOOT == "true"):
  270. print("> Launching SteamCMD...")
  271. print ("------------------------------------SteamCMD------------------------------------\n")
  272. os.system("steamcmd.exe +login " + STEAM_USER + " " + STEAM_PASS + " +force_install_dir " + UNTURNED_PATH + " +app_update 304930 validate +exit")
  273. print ("\n------------------------------------END-----------------------------------------\n\n")
  274.  
  275. #recovering bundles
  276. if BACKUP_BUNDLES == "true":
  277. print("> Recovering Bundles...")
  278. if merge_files(BACKUP_BUNDLES_FOLDER + "\\Bundles", UNTURNED_PATH + "\\Bundles"):
  279. print("ERROR: Cannot recovering Bundles, aborting...")
  280.  
  281. if ROCKET_ENABLED == "true":
  282. #download
  283. print("> Downloading rocket...")
  284. while downloader("rocket"):
  285. print("ERROR: Unable to download rocket! Please check your internet settings!\n"
  286. "> Retrying in 5 seconds..")
  287. time.sleep(5)
  288.  
  289. #extract
  290. print("> Extracting rocket...")
  291. rocket_downloaded = True
  292. correct_opened = True
  293. checkzip = None
  294. try:
  295. zzip = zipfile.ZipFile(OUTPUT_ZIP_ROCKET)
  296. checkzip = zzip.testzip()
  297. zzip.close()
  298. except zipfile.BadZipfile:
  299. correct_opened = False
  300. if (checkzip is None) and correct_opened:
  301. if os.path.exists(ROCKET_EXTRACT_FOLDER):
  302. shutil.rmtree(ROCKET_EXTRACT_FOLDER)
  303. extractor(OUTPUT_ZIP_ROCKET, ROCKET_EXTRACT_FOLDER)
  304. else:
  305. print("> Failed to extract Rocket zip (maybe a malformed zip?)")
  306. if os.listdir(ROCKET_EXTRACT_FOLDER):
  307. print("> Using the lastest correct download...")
  308. else:
  309. print("> Not failover found, launching servers...")
  310. rocket_downloaded = False
  311.  
  312. #Moving files
  313. if rocket_downloaded:
  314. print("> Installing rocket...")
  315. if installer(ROCKET_EXTRACT_FOLDER):
  316. print("> Error installing rocket, looking for opened game instances...")
  317. os.system("taskkill /f /im " + PROCNAME_WIN)
  318. time.sleep(1)
  319. if installer(ROCKET_EXTRACT_FOLDER):
  320. print("> Unable to install rocket! try to revalidate the installation!")
  321. clean_up()
  322. raw_input("Press any key to continue...")
  323. sys.exit(4)
  324.  
  325. #clean up zips and extracted files
  326. print("> Cleaning up...")
  327. clean_up()
  328.  
  329. #launching servers
  330. print("> Launching servers...")
  331. for i in range(0, len(SERVERS_TO_LAUNCH)):
  332. print(" - Launching " + SERVERS_TO_LAUNCH[i])
  333. os.system("cd " + UNTURNED_PATH + "\ & start Unturned.exe -nographics -batchmode -silent-crashes +secureserver/" +
  334. SERVERS_TO_LAUNCH[i])
  335. time.sleep(1)
  336.  
  337. #timer
  338. counter = REBOOT_TIME
  339. while counter >= 0:
  340. sys.stdout.write('> Waiting %s ...\r' % str(counter))
  341. sys.stdout.flush()
  342. time.sleep(1)
  343. counter -= 1
  344. if (RCON_ENABLED == "true") and (counter == NOTIFY_TIME) and (ROCKET_ENABLED == "true"):
  345. for i in range(0, len(RCON_PORT)):
  346. try:
  347. rcon_notify(RCON_PORT[i], RCON_PASSWORD[i])
  348. print(" -Reboot Notified on port " + str(RCON_PORT[i]))
  349. except:
  350. print(" -Unable to notify the reboot on port " + str(RCON_PORT[i]) + "! Check your config!")
  351.  
  352. if (RCON_ENABLED == "true") and (ROCKET_ENABLED == "true"):
  353. try:
  354. for i in range(0, len(RCON_PORT)):
  355. rcon_shutdown(RCON_PORT[i], RCON_PASSWORD[i])
  356. except:
  357. print("> Unable to stopping the server using rcon, using the classic method...")
  358. os.system("taskkill /f /im " + PROCNAME_WIN)
  359. else:
  360. os.system("taskkill /f /im " + PROCNAME_WIN)
  361.  
  362.  
  363. if __name__ == '__main__':
  364. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement