Skip to content

Instantly share code, notes, and snippets.

@detorto
Created May 31, 2017 11:46
Show Gist options
  • Save detorto/f7f9436bf37e80629aaf6721290e7599 to your computer and use it in GitHub Desktop.
Save detorto/f7f9436bf37e80629aaf6721290e7599 to your computer and use it in GitHub Desktop.
Simple script for ttech mount procedure
#!/usr/bin/python
import os
import sys
import os.path
from optparse import OptionParser
import glob
from xml.dom.minidom import parse, parseString
from xml.dom.minidom import Node
usage = "usage: %prog [options]"
parser = OptionParser(usage = usage)
parser.add_option("-a","--arch", dest="arch", default="target",
help="arch from releases path")
parser.add_option("-c","--candidates", dest="cand_list", default="candidates.xml",
help="Cadidatefile from path to be used")
parser.add_option("-q","--qid", dest="qid", default="lqd",
help="qid to be mounted")
parser.add_option("-p", "--path", dest="path", default="./",
help="path to project or releases dir")
(options, args) = parser.parse_args()
def create_releases_path(options):
p = os.path.abspath( options.path )
if options.arch in os.listdir(p):
print "Found ARCH=%s in current dir, thinking this is releases dir." % options.arch
p = p + "/" + options.arch
return p
if "releases" in os.listdir(p):
p = p + "/releases"
print "Found releases dir in current folder, using it."
if options.arch in os.listdir(p):
p = p+"/"+options.arch
return p
print "No ARCH=%s forlder in %s!" % (options.arch, p)
return None
def get_http_root_dir():
f = open("/etc/nginx/sites-enabled/default","r")
l = f.readline()
while l:
q = l.split()
if "root" in q:
return q[1][:-1]
l = f.readline()
def create_mount_path(options):
rootdir = get_http_root_dir()
mntpath = rootdir + "/" + options.qid
if not os.path.exists(mntpath):
print "Creating " + mntpath
os.mkdir(mntpath)
os.system("umount %s"%mntpath)
return mntpath
def mount(p1, p2):
if os.system("mount %s %s --bind" % (p1,p2)) != 0:
print "Can't mount. Maybee u not root?"
sys.exit(1)
def update_pospckg (options):
found = False
xml = parse(get_http_root_dir() + "/pos.pckg")
package = xml.getElementsByTagName('Package')[0]
links = package.getElementsByTagName("Link")
for link in links:
if link.getAttribute("qid")==options.qid:
print "Changing existing qid in pos.pckg..."
link.setAttribute("origin","%s/%s"%(options.qid, options.cand_list))
found = True
if not found:
node = xml.createElement("Link")
node.setAttribute("qid",options.qid)
node.setAttribute("origin","%s/%s"%(options.qid, options.cand_list))
print "Appending new link to pos.pckg..."
package.appendChild(node)
f = open(get_http_root_dir() + "/pos.pckg","w")
pretty_print = lambda xml: '\n'.join([line for line in xml.toprettyxml(indent=' '*2).split('\n') if line.strip()])
f.write( pretty_print(xml) )
f.close()
path_to_mount = create_releases_path(options)
if not path_to_mount:
sys.exit(1)
path_where_mount = create_mount_path(options)
if not path_where_mount:
sys.exit(1)
mount(path_to_mount, path_where_mount)
print "Mounted %s to %s with access qid %s " %(path_to_mount, path_where_mount,options.qid)
update_pospckg(options);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment