Commit 682c53d2 by Őry Máté

try to figure out DISPLAY

(agent will run as an upstart process, so it wont have the DISPLAY envvar)
parent 8d3d3713
...@@ -14,7 +14,7 @@ import urllib2 ...@@ -14,7 +14,7 @@ import urllib2
import webbrowser import webbrowser
logger = logging.getLogger() logger = logging.getLogger()
file_name = "vm_renewal.p" file_name = "vm_renewal.json"
def parse_arguments(): def parse_arguments():
...@@ -72,17 +72,51 @@ def accept(): ...@@ -72,17 +72,51 @@ def accept():
def notify(url): def notify(url):
if os.getenv("DISPLAY") and platform.system() in ('Linux', 'Windows'): olddisplay = os.environ.get("DISPLAY")
webbrowser.open(url, new=2, autoraise=True) try:
elif not os.getenv("DISPLAY"): file_path = os.path.join(get_temp_dir(), file_name)
if os.path.isfile("%s/%s" % (get_temp_dir(), file_name)): with open(file_path, "w") as f:
logger.info("There is on old request already saved") json.dump(url, f)
json.dump(url, open("%s/%s" % (get_temp_dir(), file_name), "wb"))
if platform.system() != "Windows":
display = search_display()
if display:
os.environ['DISPLAY'] = display
wall("This virtual machine is going to expire! Please type \n" wall("This virtual machine is going to expire! Please type \n"
" vm_renewal\n" " vm_renewal\n"
"command to keep it running.") "command to keep it running.")
else: else:
raise Exception('Not supported system type') display = True
webbrowser.open(url, new=2, autoraise=True)
finally:
os.environ["DISPLAY"] = olddisplay
def search_display():
"""Search a valid DISPLAY env var in processes
"""
env = os.getenv("DISPLAY")
if env:
return env
for pid in os.listdir("/proc"):
if not pid.isdigit():
continue
env = os.path.join("/proc", pid, "environ")
try:
with open(env, "r") as f:
envs = dict(line.split("=", 1)
for line in f.read().split("\0") if "=" in line)
except:
continue
else:
if "DISPLAY" in envs and ":" in envs["DISPLAY"]:
return envs["DISPLAY"]
return None
def main(): def main():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment