better patch for unverified context
[bootmanager.git] / source / RunlevelAgent.py
index 012dba1..0d514c3 100755 (executable)
@@ -7,6 +7,8 @@
 #   so that it is immediately visible at myplc (gui or api).
 # 
 
+from __future__ import print_function
+
 import xml, xmlrpclib
 import logging
 import time
@@ -14,6 +16,7 @@ import traceback
 import sys
 import os
 import string
+import ssl
 
 CONFIG_FILE = "/tmp/source/configuration"
 SESSION_FILE = "/etc/planetlab/session"
@@ -32,7 +35,7 @@ def read_config_file(filename):
 
         parts = string.split(line, "=")
         if len(parts) != 2:
-            print "Invalid line in vars file: {}".format(line)
+            print("Invalid line in vars file: {}".format(line))
             validConfFile = False
             break
 
@@ -42,7 +45,7 @@ def read_config_file(filename):
 
     vars_file.close()
     if not validConfFile:
-        print "Unable to read configuration vars."
+        print("Unable to read configuration vars.")
 
     return vars
 
@@ -72,7 +75,12 @@ class PLC:
     def __init__(self, auth, url):
         self.auth = auth
         self.url = url
-        self.api = xmlrpclib.Server(self.url, verbose=False, allow_none=True)
+        # Using a self signed certificate
+        # https://www.python.org/dev/peps/pep-0476/
+        try:    turn_off_server_verify = { 'context' : ssl._create_unverified_context() } 
+        except: turn_off_server_verify = {}
+        self.api = xmlrpclib.Server(self.url, verbose=False, allow_none=True,
+                                    **turn_off_server_verify)
 
     def __getattr__(self, name):
         method = getattr(self.api, name)
@@ -103,7 +111,7 @@ def save_pid():
         f.write("{}\n".format(pid))
         f.close()
     except:
-        print "Uuuhhh.... this should not occur."
+        print("Uuuhhh.... this should not occur.")
         sys.exit(1)
 
 def start_and_run():
@@ -122,8 +130,8 @@ def start_and_run():
             api.AuthCheck()
             break
         except:
-            print "Retry in 30 seconds: ", os.popen("uptime").read().strip()
-            traceback.print_exc()
+            print("Retry in 30 seconds: ", os.popen("uptime").read().strip())
+            traceback.print_exc(limit=5)
             time.sleep(30)
 
     try:
@@ -168,7 +176,7 @@ def start_and_run():
                 api.ReportRunlevel({'run_level' : 'failboot'})
                 
         except:
-            print "reporting error: ", os.popen("uptime").read().strip()
+            print("reporting error: ", os.popen("uptime").read().strip())
             traceback.print_exc()
 
         sys.stdout.flush()