migrate ansible server python scripts to python3 67/97867/3
authorTaka Cho <takamune.cho@att.com>
Fri, 1 Nov 2019 19:52:15 +0000 (15:52 -0400)
committerTaka Cho <takamune.cho@att.com>
Fri, 1 Nov 2019 20:16:51 +0000 (16:16 -0400)
python3 is required for R6 release

Change-Id: I94decf6ead79119f90f5fa70578b4cea8a3e58b7
Issue-ID: CCSDK-1893
Signed-off-by: Taka Cho <takamune.cho@att.com>
ansible-server/src/main/Dockerfile
ansible-server/src/main/ansible-server/AnsibleModule.py
ansible-server/src/main/ansible-server/AnsibleSql.py
ansible-server/src/main/ansible-server/LoadAnsibleMySql.py
ansible-server/src/main/ansible-server/RestServer.py
ansible-server/src/main/ansible-server/UsersRestServer.py
ansible-server/src/main/ansible-server/startAnsibleServer.sh [changed mode: 0644->0755]
ansible-server/src/main/docker/Dockerfile [deleted file]

index d44f5c3..5a1c394 100644 (file)
@@ -8,23 +8,24 @@ WORKDIR /opt/
 COPY ansible-server/requirements.txt ansible-server/requirements.txt
 
 RUN apk add --no-cache curl \
 COPY ansible-server/requirements.txt ansible-server/requirements.txt
 
 RUN apk add --no-cache curl \
+                       iputils \
                        bash \
                        bash \
-                       py2-pip \
+                       py3-pip \
                        openssh-client \
                        openssh-client \
-                       python2 &&\
+                       python3 &&\
     apk add --no-cache --virtual .build-deps build-base \
                                              libffi-dev \
                                              openssl-dev \
     apk add --no-cache --virtual .build-deps build-base \
                                              libffi-dev \
                                              openssl-dev \
-                                             python2-dev &&\
-    pip install --no-cache-dir --upgrade pip==$PIP_TAG && \
-    pip install --no-cache-dir -r ansible-server/requirements.txt &&\
+                                             python3-dev &&\
+    pip3 install --no-cache-dir --upgrade pip==$PIP_TAG && \
+    pip3 install --no-cache-dir -r ansible-server/requirements.txt &&\
     apk del .build-deps
 
 RUN addgroup -S ansible && adduser -S ansible -G ansible
     apk del .build-deps
 
 RUN addgroup -S ansible && adduser -S ansible -G ansible
+
 COPY --chown=ansible:ansible ansible-server ansible-server
 COPY --chown=ansible:ansible configuration/ansible.cfg /etc/ansible/ansible.cfg
 
 COPY --chown=ansible:ansible ansible-server ansible-server
 COPY --chown=ansible:ansible configuration/ansible.cfg /etc/ansible/ansible.cfg
 
-
 WORKDIR /opt/ansible-server
 
 RUN mkdir /opt/onap ; ln -s /opt/ansible-server /opt/onap/ccsdk
 WORKDIR /opt/ansible-server
 
 RUN mkdir /opt/onap ; ln -s /opt/ansible-server /opt/onap/ccsdk
index 71d0c05..80e59e2 100755 (executable)
@@ -80,7 +80,7 @@ def ansibleSysCall (inventory_path, playbook_path, nodelist, mandatory, envparam
             log.append (line)
     else:
         for line in stdout_value: # p.stdout.readlines():
             log.append (line)
     else:
         for line in stdout_value: # p.stdout.readlines():
-            print line # line,
+            print(line) # line,
             if ParseFlag and len(line.strip())>0:
                 ip_address = line.split(':')[0].strip()
                 ok_flag = line.split(':')[1].strip().split('=')[1].split('changed')[0].strip()
             if ParseFlag and len(line.strip())>0:
                 ip_address = line.split(':')[0].strip()
                 ok_flag = line.split(':')[1].strip().split('=')[1].split('changed')[0].strip()
@@ -120,7 +120,7 @@ if __name__ == '__main__':
     p = Process(nodelist=ansible_call, args=('ansible_module_config', playbook_file, nodelist,d, ))
     p.start()
 
     p = Process(nodelist=ansible_call, args=('ansible_module_config', playbook_file, nodelist,d, ))
     p.start()
 
-    print "Process running"
-    print d
+    print("Process running")
+    print(d)
     p.join()
     p.join()
-    print d
+    print(d)
index c910777..4fe7115 100755 (executable)
@@ -52,7 +52,7 @@ class mySql():
                 self.cur.execute(myquery)
             self.db.commit()
             results = self.cur.fetchall()
                 self.cur.execute(myquery)
             self.db.commit()
             results = self.cur.fetchall()
-        except Exception, e:
+        except Exception as e:
             results = repr(e)
         return results
     
             results = repr(e)
         return results
     
@@ -139,24 +139,24 @@ def readPlaybook (sqlintf, value, version=None):
     errorCode = 0
     diag = ''
 
     errorCode = 0
     diag = ''
 
-    print "***> in AnsibleSql.readPlaybook"
+    print("***> in AnsibleSql.readPlaybook")
     
     if not version:
         query = "SELECT MAX(version) FROM playbook WHERE name like'" + value + "%'"
     
     if not version:
         query = "SELECT MAX(version) FROM playbook WHERE name like'" + value + "%'"
-        print "   Query:", query
+        print("   Query:", query)
         results = sqlintf.Query (query)
         version = results[0][0]
 
         results = sqlintf.Query (query)
         version = results[0][0]
 
-    print "   Provided playbook name:", value 
-    print "   Used version:", version
+    print("   Provided playbook name:", value) 
+    print("   Used version:", version)
 
     results = []
     if version:
         query = "SELECT value,type FROM playbook WHERE name='" + value + "@" + version + "'"
         results = sqlintf.Query (query)
 
 
     results = []
     if version:
         query = "SELECT value,type FROM playbook WHERE name='" + value + "@" + version + "'"
         results = sqlintf.Query (query)
 
-        print "Query:", query
-        print "Results:", results
+        print("Query:", query)
+        print("Results:", results)
     
     if len(results) == 0:
         errorCode = 1
     
     if len(results) == 0:
         errorCode = 1
@@ -173,7 +173,7 @@ def readCredentials (sqlintf, tag):
     errorCode = []
     diag = []
 
     errorCode = []
     diag = []
 
-    print "***> in AnsibleSql.readCredential"
+    print("***> in AnsibleSql.readCredential")
     
     # Load credentials
 
     
     # Load credentials
 
@@ -198,8 +198,8 @@ def readCredentials (sqlintf, tag):
                     rec +"'"
             results = sqlintf.Query (query)
 
                     rec +"'"
             results = sqlintf.Query (query)
 
-        print "   Query:", query
-        print "   Results:", len(results), results
+        print("   Query:", query)
+        print("   Results:", len(results), results)
 
         if len(results) == 0:
             errorCode = 1
 
         if len(results) == 0:
             errorCode = 1
@@ -240,20 +240,20 @@ if __name__ == '__main__':
 
     # Load playbooks
 
 
     # Load playbooks
 
-    print "Loading playbooks"
+    print("Loading playbooks")
     for file in onlyfiles:
         if "yml" in file:
 
             name = file.split (".yml")[0]
     for file in onlyfiles:
         if "yml" in file:
 
             name = file.split (".yml")[0]
-            print "  Loading:", name
+            print("  Loading:", name)
             version = name.split("@")[1]
             errorCode, diag = loadPlaybook (sqlintf, name, version, '.yml')
             if errorCode:
             version = name.split("@")[1]
             errorCode, diag = loadPlaybook (sqlintf, name, version, '.yml')
             if errorCode:
-                print "  Results: Failed - ", diag
+                print("  Results: Failed - ", diag)
             else:
             else:
-                print "  Results: Success"
+                print("  Results: Success")
 
 
-    print "\nLoading inventory"
+    print("\nLoading inventory")
     
     # Load inventory
 
     
     # Load inventory
 
@@ -274,48 +274,48 @@ if __name__ == '__main__':
     file.close()
 
     for hostgroup in inv:
     file.close()
 
     for hostgroup in inv:
-        print "  Loading:", hostgroup
+        print("  Loading:", hostgroup)
         hostfqdn = ''
         cred = ''
         for hostname in inv[hostgroup]:
             cred = inv[hostgroup][hostname]
             errorCode, diag = loadCredentials (sqlintf, hostgroup, hostname, cred)
             if errorCode:
         hostfqdn = ''
         cred = ''
         for hostname in inv[hostgroup]:
             cred = inv[hostgroup][hostname]
             errorCode, diag = loadCredentials (sqlintf, hostgroup, hostname, cred)
             if errorCode:
-                print "  Results: Failed - ", diag
+                print("  Results: Failed - ", diag)
             else:
             else:
-                print "  Results: Success"
+                print("  Results: Success")
                 
                 
-    print "\nReading playbook"
+    print("\nReading playbook")
     
     # Read playbook
 
     if not sqlintf.con:
     
     # Read playbook
 
     if not sqlintf.con:
-        print "Cannot connect to MySql:", sqlintf.error
+        print("Cannot connect to MySql:", sqlintf.error)
         sys.exit()
         
     name = "ansible_sleep"
         sys.exit()
         
     name = "ansible_sleep"
-    print "Reading playbook:", name
+    print("Reading playbook:", name)
     value, version, errorCode, diag = readPlaybook (sqlintf, name)
     if errorCode:
     value, version, errorCode, diag = readPlaybook (sqlintf, name)
     if errorCode:
-        print "Results: Failed - ", diag
+        print("Results: Failed - ", diag)
     else:
     else:
-        print "Results: Success"
-        print value
-        print version
-        print diag
+        print("Results: Success")
+        print(value)
+        print(version)
+        print(diag)
 
 
-    print "\nReading inventory"
+    print("\nReading inventory")
 
     # Read inventory
 
     tag = ["your_inventory_test_group_name"]
 
     # Read inventory
 
     tag = ["your_inventory_test_group_name"]
-    print "Reading inventory tag:", tag
+    print("Reading inventory tag:", tag)
     errorCode, diag = readCredentials (sqlintf, tag)
     if errorCode:
     errorCode, diag = readCredentials (sqlintf, tag)
     if errorCode:
-        print "Results: Failed - ", diag
+        print("Results: Failed - ", diag)
     else:
     else:
-        print "Results: Success"
-        print diag
+        print("Results: Success")
+        print(diag)
                 
     sqlintf.Close()
 
                 
     sqlintf.Close()
 
index d3c0ece..4bea8d1 100755 (executable)
@@ -47,7 +47,7 @@ class mySql():
                 self.cur.execute(myquery)
             self.db.commit()
             results = self.cur.fetchall()
                 self.cur.execute(myquery)
             self.db.commit()
             results = self.cur.fetchall()
-        except Exception, e:
+        except Exception as e:
             error = str (e)
         return results, error
 
             error = str (e)
         return results, error
 
@@ -100,7 +100,7 @@ def loadCredentials (hostgroup, hostname, cred):
     query = "SELECT hostname,hostgroup FROM inventory WHERE hostname='" + hostname +"'"
     results = sqlintf.Query (query)
 
     query = "SELECT hostname,hostgroup FROM inventory WHERE hostname='" + hostname +"'"
     results = sqlintf.Query (query)
 
-    print '==>', results
+    print('==>', results)
     
     if hostname in str(results):
 
     
     if hostname in str(results):
 
@@ -155,24 +155,24 @@ if __name__ == '__main__':
     sqlintf = mySql (host, user, passwd, db)
 
     # Load playbooks
     sqlintf = mySql (host, user, passwd, db)
 
     # Load playbooks
-    print "Loading playbooks"
+    print("Loading playbooks")
     for file in onlyfiles:
         if "yml" in file:
             name = file.split (".yml")[0]
     for file in onlyfiles:
         if "yml" in file:
             name = file.split (".yml")[0]
-            print "  Loading:", name
+            print("  Loading:", name)
             version = name.split("@")[1]
             errorCode, diag = loadPlaybook (name, version)
             if errorCode:
             version = name.split("@")[1]
             errorCode, diag = loadPlaybook (name, version)
             if errorCode:
-                print "  Results: Failed - ", diag
+                print("  Results: Failed - ", diag)
             else:
             else:
-                print "  Results: Success"
+                print("  Results: Success")
         if "tar.gz" in file:
             name = file.split (".tar.gz")[0]
         if "tar.gz" in file:
             name = file.split (".tar.gz")[0]
-            print "  Loading:", name
+            print("  Loading:", name)
             version = name.split("@")[1]
             errorCode, diag = loadPlaybook (name, version, ".tar.gz")
 
             version = name.split("@")[1]
             errorCode, diag = loadPlaybook (name, version, ".tar.gz")
 
-    print "\nLoading inventory"
+    print("\nLoading inventory")
     
     # Load inventory
     hostgroup = None
     
     # Load inventory
     hostgroup = None
@@ -192,15 +192,15 @@ if __name__ == '__main__':
     file.close()
 
     for hostgroup in inv:
     file.close()
 
     for hostgroup in inv:
-        print "  Loading:", hostgroup
+        print("  Loading:", hostgroup)
         hostfqdn = ''
         cred = ''
         for hostname in inv[hostgroup]:
             cred = inv[hostgroup][hostname]
             errorCode, diag = loadCredentials (hostgroup, hostname, cred)
             if errorCode:
         hostfqdn = ''
         cred = ''
         for hostname in inv[hostgroup]:
             cred = inv[hostgroup][hostname]
             errorCode, diag = loadCredentials (hostgroup, hostname, cred)
             if errorCode:
-                print "  Results: Failed - ", diag
+                print("  Results: Failed - ", diag)
             else:
             else:
-                print "  Results: Success"
+                print("  Results: Success")
                 
     sqlintf.Close()
                 
     sqlintf.Close()
index 2b4da33..483b832 100755 (executable)
@@ -295,7 +295,7 @@ def store_local_vars(playbook_path, Id):
     cherrypy.log("LocalParameters: " + str(local_parms))
 
     f = open(playbook_path + "/vars/defaults.yml", "a")
     cherrypy.log("LocalParameters: " + str(local_parms))
 
     f = open(playbook_path + "/vars/defaults.yml", "a")
-    for key, value in local_parms.items():
+    for key, value in list(local_parms.items()):
         f.write(key + "=" + value + "\n")
     f.close()
     # ##################################################
         f.write(key + "=" + value + "\n")
     f.close()
     # ##################################################
@@ -718,8 +718,8 @@ def handle_get_method(input_data):
     # Just a debug to dump any records
     if 'GetStatus' in input_data['Type']:
         cherrypy.log("******** Dump Records **********")
     # Just a debug to dump any records
     if 'GetStatus' in input_data['Type']:
         cherrypy.log("******** Dump Records **********")
-        if TestRecord.items():
-            for id, record in TestRecord.items():
+        if list(TestRecord.items()):
+            for id, record in list(TestRecord.items()):
                 cherrypy.log("    Id: " + id)
                 cherrypy.log("Record: " + str(record))
         else:
                 cherrypy.log("    Id: " + id)
                 cherrypy.log("Record: " + str(record))
         else:
@@ -778,7 +778,7 @@ def handle_get_method(input_data):
                 return out_obj
             elif 'GetStatus' in input_data['Type']:
                 cherrypy.log(" Dump Records")
                 return out_obj
             elif 'GetStatus' in input_data['Type']:
                 cherrypy.log(" Dump Records")
-                for id, record in TestRecord.items():
+                for id, record in list(TestRecord.items()):
                     cherrypy.log(" id: " + id)
                     cherrypy.log("   Record:" + str(record))
             elif 'GetTheOutput' in input_data['Type'] or 'GetOutput' in input_data['Type']:
                     cherrypy.log(" id: " + id)
                     cherrypy.log("   Record:" + str(record))
             elif 'GetTheOutput' in input_data['Type'] or 'GetOutput' in input_data['Type']:
@@ -1027,7 +1027,7 @@ if __name__ == '__main__':
             if 'ansible_inv' in config_line:
                 ANSIBLE_INV = config_line.split(':')[1].strip()
                 if not os.path.exists(ANSIBLE_PATH + "/" + ANSIBLE_INV):
             if 'ansible_inv' in config_line:
                 ANSIBLE_INV = config_line.split(':')[1].strip()
                 if not os.path.exists(ANSIBLE_PATH + "/" + ANSIBLE_INV):
-                    print '[INFO] The ansible_inv file does not exist'
+                    print('[INFO] The ansible_inv file does not exist')
                     sys.exit(0)
             if 'ansible_temp' in config_line:
                 ANSIBLE_TEMP = config_line.split(':')[1].strip()
                     sys.exit(0)
             if 'ansible_temp' in config_line:
                 ANSIBLE_TEMP = config_line.split(':')[1].strip()
index b899297..5b664e4 100755 (executable)
@@ -64,7 +64,7 @@ def sys_call (cmd):
 
 def callback (Id, Result, Output, Log, returncode):
     
 
 def callback (Id, Result, Output, Log, returncode):
     
-    print "***> in RestServer.callback"
+    print("***> in RestServer.callback")
 
     if Id in TestRecord:
         time_now = datetime.datetime.utcnow()
 
     if Id in TestRecord:
         time_now = datetime.datetime.utcnow()
@@ -116,17 +116,17 @@ def callback (Id, Result, Output, Log, returncode):
                     if key in TestRecord[Id]['Output']['Output']:
                         data["Results"][key]["Output"] = TestRecord[Id]['Output']['Output'][key]
 
                     if key in TestRecord[Id]['Output']['Output']:
                         data["Results"][key]["Output"] = TestRecord[Id]['Output']['Output'][key]
 
-            print "     Posting to", TestRecord[Id]['CallBack']
+            print("     Posting to", TestRecord[Id]['CallBack'])
             
             s = requests.Session()
             r = s.post(TestRecord[Id]['CallBack'], data = json.dumps(data),
                        headers = {'content-type': 'application/json'})
             
             s = requests.Session()
             r = s.post(TestRecord[Id]['CallBack'], data = json.dumps(data),
                        headers = {'content-type': 'application/json'})
-            print  "     Response", r.status_code, r.text
+            print("     Response", r.status_code, r.text)
 
 def RunAnsible_Playbook (callback, Id, Inventory, Playbook, NodeList, TestRecord,
                          Path, ArchiveFlag):
 
 
 def RunAnsible_Playbook (callback, Id, Inventory, Playbook, NodeList, TestRecord,
                          Path, ArchiveFlag):
 
-    print "***> in RestServer.RunAnsible_Playbook"
+    print("***> in RestServer.RunAnsible_Playbook")
 
     # Run test in playbook for given target
     Result = ''
 
     # Run test in playbook for given target
     Result = ''
@@ -627,9 +627,9 @@ class TestManager (object):
                             # PAP
                             f = open(playbook_path + "/vars/defaults.yml","a")
                             #for id, record in TestRecord.items():
                             # PAP
                             f = open(playbook_path + "/vars/defaults.yml","a")
                             #for id, record in TestRecord.items():
-                            print TestRecord[Id]['LocalParameters']
+                            print(TestRecord[Id]['LocalParameters'])
                             local_parms = TestRecord[Id]['LocalParameters']
                             local_parms = TestRecord[Id]['LocalParameters']
-                            for key, value in local_parms.items():
+                            for key, value in list(local_parms.items()):
                                 f.write(key +"=" + value + "\n");
                             f.close()
                             ###################################################
                                 f.write(key +"=" + value + "\n");
                             f.close()
                             ###################################################
@@ -664,7 +664,7 @@ class TestManager (object):
                             f.close()
                             f = open(playbook_path + "/JsonRequest.txt", "w")
                             #f.write(str(input_json))
                             f.close()
                             f = open(playbook_path + "/JsonRequest.txt", "w")
                             #f.write(str(input_json))
-                            print( json.dumps(input_json, indent=4, sort_keys=True))
+                            print(( json.dumps(input_json, indent=4, sort_keys=True)))
                             f.write( json.dumps(input_json, indent=4, sort_keys=True))
                             f.close()
 
                             f.write( json.dumps(input_json, indent=4, sort_keys=True))
                             f.close()
 
@@ -719,8 +719,8 @@ class TestManager (object):
             # Just a debug to dump any records
             if 'GetStatus' in input_data['Type']:
                 cherrypy.log( "******** Dump Records **********")
             # Just a debug to dump any records
             if 'GetStatus' in input_data['Type']:
                 cherrypy.log( "******** Dump Records **********")
-                if TestRecord.items():
-                  for id, record in TestRecord.items():
+                if list(TestRecord.items()):
+                  for id, record in list(TestRecord.items()):
                     cherrypy.log( "    Id: " + id)
                     cherrypy.log( "Record: " + str(record))
                 else:
                     cherrypy.log( "    Id: " + id)
                     cherrypy.log( "Record: " + str(record))
                 else:
@@ -784,10 +784,10 @@ class TestManager (object):
                         return out_obj
 
                     elif 'GetStatus' in input_data['Type']:
                         return out_obj
 
                     elif 'GetStatus' in input_data['Type']:
-                        print " Dump Records"
+                        print(" Dump Records")
                         for id, record in TestRecord,items():
                         for id, record in TestRecord,items():
-                           print " id: " + id
-                           print "   Record:" + str(reecord)
+                           print(" id: " + id)
+                           print("   Record:" + str(reecord))
 
                     elif 'GetOutput' in input_data['Type']:
 
 
                     elif 'GetOutput' in input_data['Type']:
 
@@ -944,7 +944,7 @@ if __name__ == '__main__':
     config_file_path = "RestServer_config"
 
     if not os.path.exists(config_file_path):
     config_file_path = "RestServer_config"
 
     if not os.path.exists(config_file_path):
-        print '[INFO] The config file does not exist'
+        print('[INFO] The config file does not exist')
         sys.exit(0)
 
     ip = 'na'
         sys.exit(0)
 
     ip = 'na'
@@ -1018,15 +1018,15 @@ if __name__ == '__main__':
         # Point to certificate files
 
         if not os.path.exists(pub):
         # Point to certificate files
 
         if not os.path.exists(pub):
-            print '[INFO] The public certificate does not exist'
+            print('[INFO] The public certificate does not exist')
             sys.exit(0)
 
         if not os.path.exists(priv):
             sys.exit(0)
 
         if not os.path.exists(priv):
-            print '[INFO] The private key does not exist'
+            print('[INFO] The private key does not exist')
             sys.exit(0)            
 
         if not os.path.exists(intermediate):
             sys.exit(0)            
 
         if not os.path.exists(intermediate):
-            print '[INFO] The intermediate certificate does not exist'
+            print('[INFO] The intermediate certificate does not exist')
             sys.exit(0)
 
         
             sys.exit(0)
 
         
@@ -1037,7 +1037,7 @@ if __name__ == '__main__':
     if auth:
         # Read in and build user dictionary
         if not os.path.exists(users):
     if auth:
         # Read in and build user dictionary
         if not os.path.exists(users):
-          print '[INFO] The users file does not exist: ' + users
+          print('[INFO] The users file does not exist: ' + users)
           sys.exit(0)
         userpassdict = {}
         user_file = open(users, 'r')
           sys.exit(0)
         userpassdict = {}
         user_file = open(users, 'r')
old mode 100644 (file)
new mode 100755 (executable)
index 193fe77..2853aca
@@ -2,5 +2,5 @@
 exec &> >(tee -a "/var/log/ansible-server.log")
 
 cd /opt/onap/ccsdk
 exec &> >(tee -a "/var/log/ansible-server.log")
 
 cd /opt/onap/ccsdk
-exec /usr/bin/python RestServer.py
+exec /usr/bin/python3 RestServer.py
 
 
diff --git a/ansible-server/src/main/docker/Dockerfile b/ansible-server/src/main/docker/Dockerfile
deleted file mode 100644 (file)
index fd2037c..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-# Base ubuntu with added packages needed for open ecomp
-FROM onap/ccsdk-alpine-image:${ccsdk.distribution.version}
-
-LABEL maintainer="SDN-C Team (sdnc@lists.openecomp.org)"
-
-#ENV http_proxy=http://anonymous:password@one.proxy.att.com:8080
-#ENV https_proxy=http://anonymous:password@one.proxy.att.com:8080
-
-## add more pkgs
-RUN apk update
-RUN apk add vim
-RUN apk add curl
-RUN apk add ansible
-
-
-# copy files needed
-COPY opt /opt/
-
-WORKDIR /opt/onap/ccsdk
-
-#ENTRYPOINT exec startAnsibleServer.sh
-#CMD ["/bin/bash"]
-EXPOSE 8000
-