|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import random, string, subprocess, os, sys
|
|
|
|
from datetime import time, date, datetime
|
|
|
|
from time import strftime
|
|
|
|
|
|
|
|
try:
|
|
|
|
command = sys.argv[1]
|
|
|
|
except IndexError, e:
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
rand = ''.join(random.choice(string.letters + string.digits) for i in xrange(8))
|
|
|
|
rand = "%s_%s" % (strftime("%Y%m%d_%H%M%S"), rand)
|
|
|
|
|
|
|
|
logfile = open("/etc/cvm/log/log.%s" % rand, "w")
|
|
|
|
logfile.write("# NON-INTERACTIVE COMMAND EXECUTION\n")
|
|
|
|
logfile.write("$ " + command + "\n")
|
|
|
|
|
|
|
|
prc = subprocess.Popen(["su", "-s", "/bin/bash", "-c", command, "cvm"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
|
|
|
while prc.poll() is None:
|
|
|
|
stdout = prc.stdout.read()
|
|
|
|
stderr = prc.stderr.read()
|
|
|
|
|
|
|
|
if stdout.strip() != "":
|
|
|
|
sys.stdout.write(stdout)
|
|
|
|
logfile.write(stdout + "\n")
|
|
|
|
|
|
|
|
if stderr.strip() != "":
|
|
|
|
sys.stderr.write(stderr)
|
|
|
|
logfile.write(stderr + "\n")
|
|
|
|
|
|
|
|
stdout = prc.stdout.read()
|
|
|
|
stderr = prc.stderr.read()
|
|
|
|
|
|
|
|
logfile.write(stdout + "\n" + stderr + "\n")
|
|
|
|
sys.stdout.write(stdout)
|
|
|
|
sys.stderr.write(stderr)
|
|
|
|
|
|
|
|
logfile.close()
|
|
|
|
exit(prc.returncode)
|