Skip to content

Instantly share code, notes, and snippets.

@02strich
Created March 5, 2014 15:11
Show Gist options
  • Save 02strich/9369108 to your computer and use it in GitHub Desktop.
Save 02strich/9369108 to your computer and use it in GitHub Desktop.
Wiki Migration Scripts
import os
import os.path
import sys
import shutil
IN_DIR = "servers/"
PROCESSED_DIR = "servers-processed/"
OUT_DIR = "sysadmin/Servers/"
PREFIX_MAP = {
"prd": "Production",
"off": "Office",
"alpha": "Alpha"
}
def dict_get(dictionary, key, def_value):
if key in dictionary:
val = dictionary[key]
if val:
return val
else:
return def_value
else:
return def_value
def write_metadata(prefix,dictionary, output):
output.write("{details:label=server-%s}" % prefix)
output.write("""
Server Name (FQDN):{fqdn}
external IP:{external_ip}
internal IP:{internal_ip}
external MAC:{external_mac}
internal MAC:{internal_mac}
Root Password:{root_pw}
RSA fingerprint:{rsa_fp}
Datacenter:{dc_location}
Rack:{rack_location}
Server is a VM host:{is_vm_host}
VM host:{vm_host}
""".format(
fqdn=dict_get(dictionary, 'FQDN',' '),
external_ip=dict_get(dictionary, 'EXTIP', ' '),
external_mac=dict_get(dictionary, 'EXTMAC', ' '),
internal_ip=dict_get(dictionary, 'INTIP', ' '),
internal_mac=dict_get(dictionary, 'INTMAC', ' '),
root_pw=dict_get(dictionary, 'ROOTPW', ' '),
rsa_fp=dict_get(dictionary, 'RSAFINGPRINT', ' '),
dc_location=dict_get(dictionary, 'DATACENTER', ' '),
rack_location=dict_get(dictionary, 'RACKLOCATION', ' '),
is_vm_host=dict_get(dictionary, 'ISVM', 'false'),
vm_host=dict_get(dictionary, 'VMHOST', ' ')
))
output.write("{details}\n")
def main():
for name in os.listdir(IN_DIR):
with open(os.path.join(IN_DIR, name), "rt") as i:
prefix = name.split("-", 1)[0].lower()
if prefix in PREFIX_MAP:
base_path = os.path.join(OUT_DIR, PREFIX_MAP[prefix])
if not os.path.exists(base_path):
os.makedirs(base_path)
else:
base_path = OUT_DIR
with open(os.path.join(base_path, name), "wt") as o:
server_properties = {}
server_properties_finished = False
for line in i.readlines():
if line.strip() in ("{{ServerDetailPage", "{{ServerDetailPageVHost"):
continue
elif line.strip() == "}}":
server_properties_finished = True
write_metadata(prefix, server_properties, o)
elif server_properties_finished:
o.write(line)
else:
try:
key, value = line[1:].split('=', 1)
server_properties[key] = value.strip()
except:
pass
shutil.move(os.path.join(IN_DIR, name), os.path.join(PROCESSED_DIR, name))
main()
import os
import os.path
import sys
import shutil
STORY_TRIGGER = "#WEITERLEITUNG"
TARGET_DIR = "sysadmin-forwards/"
for name in os.listdir('.'):
move_it = False
with open(name, "rt") as f:
data = f.readlines()
if data and STORY_TRIGGER in data[0]:
move_it = True
if move_it:
shutil.move(name, os.path.join(TARGET_DIR, name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment