Fix flake8, black and pylint errors in code 79/14479/1
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Tue, 9 Jul 2024 12:30:44 +0000 (14:30 +0200)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Tue, 9 Jul 2024 12:30:44 +0000 (14:30 +0200)
Change-Id: Ic03f4d31e2ba9801be20d3679bd5749948cab39d
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
osm_ee/base_ee.py
osm_ee/exceptions.py
osm_ee/frontend_client.py
osm_ee/frontend_grpc.py
osm_ee/frontend_server.py
osm_ee/util/util_ansible.py
osm_ee/util/util_ee.py
osm_ee/util/util_grpc.py
osm_ee/util/util_kubernetes.py
osm_ee/vnf/vnf_ee.py

index 6728c49..c7b1bed 100644 (file)
@@ -36,15 +36,16 @@ class BaseEE:
     HEALTH_CHECK_ACTION = "health-check"
 
     def __init__(self):
-        self.logger = logging.getLogger('osm_ee.base')
+        self.logger = logging.getLogger("osm_ee.base")
 
+        self.config_params = {}
         # Check if configuration is stored and load it
         if os.path.exists(self.CONFIG_FILE):
-            with open(self.CONFIG_FILE, 'r') as file:
+            with open(self.CONFIG_FILE, "r") as file:
                 self.config_params = yaml.load(file, Loader=yaml.FullLoader)
-                self.logger.debug("Load existing config from file: {}".format(self.config_params))
-        else:
-            self.config_params = {}
+                self.logger.debug(
+                    "Load existing config from file: {}".format(self.config_params)
+                )
 
         self.vnf_ee = VnfEE(self.config_params)
 
@@ -56,7 +57,9 @@ class BaseEE:
         return ssh_key
 
     async def run_action(self, id, name, params):
-        self.logger.debug("Execute action id: {}, name: {}, params: {}".format(id, name, params))
+        self.logger.debug(
+            "Execute action id: {}, name: {}, params: {}".format(id, name, params)
+        )
 
         try:
             # Health-check
@@ -71,10 +74,13 @@ class BaseEE:
                 action_params = yaml.safe_load(params)
 
                 if name == "config":
-                    self.logger.debug("Store config info in file: {}".format(self.CONFIG_FILE))
+                    self.logger.debug(
+                        "Store config info in file: {}".format(self.CONFIG_FILE)
+                    )
                     self.config_params.update(action_params)
-                    with open(self.CONFIG_FILE, 'w') as file:
+                    with open(self.CONFIG_FILE, "w") as file:
                         config = yaml.dump(self.config_params, file)
+                        self.logger.debug("Config info: {}".format(config))
 
                 async for return_status, detailed_message in method(id, action_params):
                     if return_status not in self.RETURN_STATUS_LIST:
@@ -82,15 +88,20 @@ class BaseEE:
                     else:
                         yield return_status, str(detailed_message)
         except AttributeError as e:
-            error_msg = "Action name: {} not implemented".format(name)
+            error_msg = "Action name: {} not implemented. Exception: {}".format(
+                name, str(e)
+            )
             self.logger.error(error_msg)
             yield "ERROR", error_msg
         except Exception as e:
-            self.logger.error("Error executing action id, name: {},{}: {}".format(id, name, str(e)), exc_info=True)
+            self.logger.error(
+                "Error executing action id, name: {},{}: {}".format(id, name, str(e)),
+                exc_info=True,
+            )
             yield "ERROR", str(e)
 
 
-if __name__ == '__main__':
+if __name__ == "__main__":
     logging.basicConfig(level=logging.DEBUG)
 
     loop = asyncio.get_event_loop()
index 4829197..a99399d 100644 (file)
 # contact with: nfvlabs@tid.es
 ##
 
+
 class ExecEnvException(Exception):
     """Exception thrown by the EE if the actions can't be invoked or there is any generic error"""
 
+
 class VnfException(Exception):
-    """Exception thrown by the Vnf EE code in case of error"""
\ No newline at end of file
+    """Exception thrown by the Vnf EE code in case of error"""
index 0c34580..21203a2 100644 (file)
@@ -24,13 +24,11 @@ import sys
 import yaml
 import asyncio
 import uuid
-import traceback
-import os
 import socket
 
 from grpclib.client import Channel
 
-from osm_ee.frontend_pb2 import PrimitiveRequest, PrimitiveReply
+from osm_ee.frontend_pb2 import PrimitiveRequest
 from osm_ee.frontend_pb2 import SshKeyRequest, SshKeyReply
 from osm_ee.frontend_grpc import FrontendExecutorStub
 
@@ -42,7 +40,7 @@ async def frontend_client(host_name, port, primitive_name, params):
     try:
         stub = FrontendExecutorStub(channel)
 
-        if (primitive_name == "get_ssh_key"):
+        if primitive_name == "get_ssh_key":
             print("Get ssh key")
             reply: SshKeyReply = await stub.GetSshKey(SshKeyRequest())
             print(reply.message)
@@ -51,22 +49,25 @@ async def frontend_client(host_name, port, primitive_name, params):
                 primitive_id = str(uuid.uuid1())
                 print("Execute primitive {}, params: {}".format(primitive_name, params))
                 await stream.send_message(
-                    PrimitiveRequest(id=primitive_id, name=primitive_name, params=yaml.dump(params)), end=True)
+                    PrimitiveRequest(
+                        id=primitive_id, name=primitive_name, params=yaml.dump(params)
+                    ),
+                    end=True,
+                )
                 async for reply in stream:
                     print(reply)
-                #replies = [reply async for reply in stream]
-                #print(replies)
+                # replies = [reply async for reply in stream]
+                # print(replies)
     except Exception as e:
         print("Error executing primitive {}: {}".format(primitive_name, str(e)))
-        #print(traceback.format_exc())
     finally:
         channel.close()
 
 
-if __name__ == '__main__':
+if __name__ == "__main__":
 
     args = sys.argv[1:]
-    if (len(args) < 1):
+    if len(args) < 1:
         print("Usage: host port primitive_name params")
     else:
         host_name = args[0]
@@ -79,7 +80,9 @@ if __name__ == '__main__':
 
         loop = asyncio.get_event_loop()
         try:
-            task = asyncio.ensure_future(frontend_client(host_name, port, primitive_name, params))
+            task = asyncio.ensure_future(
+                frontend_client(host_name, port, primitive_name, params)
+            )
             loop.run_until_complete(task)
         finally:
             loop.close()
index b2b95d3..5bb0380 100644 (file)
@@ -6,6 +6,7 @@ import typing
 
 import grpclib.const
 import grpclib.client
+
 if typing.TYPE_CHECKING:
     import grpclib.server
 
@@ -15,22 +16,28 @@ import osm_ee.frontend_pb2
 class FrontendExecutorBase(abc.ABC):
 
     @abc.abstractmethod
-    async def RunPrimitive(self, stream: 'grpclib.server.Stream[osm_ee.frontend_pb2.PrimitiveRequest, osm_ee.frontend_pb2.PrimitiveReply]') -> None:
+    async def RunPrimitive(
+        self,
+        stream: "grpclib.server.Stream[osm_ee.frontend_pb2.PrimitiveRequest, osm_ee.frontend_pb2.PrimitiveReply]",
+    ) -> None:
         pass
 
     @abc.abstractmethod
-    async def GetSshKey(self, stream: 'grpclib.server.Stream[osm_ee.frontend_pb2.SshKeyRequest, osm_ee.frontend_pb2.SshKeyReply]') -> None:
+    async def GetSshKey(
+        self,
+        stream: "grpclib.server.Stream[osm_ee.frontend_pb2.SshKeyRequest, osm_ee.frontend_pb2.SshKeyReply]",
+    ) -> None:
         pass
 
     def __mapping__(self) -> typing.Dict[str, grpclib.const.Handler]:
         return {
-            '/osm_ee.FrontendExecutor/RunPrimitive': grpclib.const.Handler(
+            "/osm_ee.FrontendExecutor/RunPrimitive": grpclib.const.Handler(
                 self.RunPrimitive,
                 grpclib.const.Cardinality.UNARY_STREAM,
                 osm_ee.frontend_pb2.PrimitiveRequest,
                 osm_ee.frontend_pb2.PrimitiveReply,
             ),
-            '/osm_ee.FrontendExecutor/GetSshKey': grpclib.const.Handler(
+            "/osm_ee.FrontendExecutor/GetSshKey": grpclib.const.Handler(
                 self.GetSshKey,
                 grpclib.const.Cardinality.UNARY_UNARY,
                 osm_ee.frontend_pb2.SshKeyRequest,
@@ -44,13 +51,13 @@ class FrontendExecutorStub:
     def __init__(self, channel: grpclib.client.Channel) -> None:
         self.RunPrimitive = grpclib.client.UnaryStreamMethod(
             channel,
-            '/osm_ee.FrontendExecutor/RunPrimitive',
+            "/osm_ee.FrontendExecutor/RunPrimitive",
             osm_ee.frontend_pb2.PrimitiveRequest,
             osm_ee.frontend_pb2.PrimitiveReply,
         )
         self.GetSshKey = grpclib.client.UnaryUnaryMethod(
             channel,
-            '/osm_ee.FrontendExecutor/GetSshKey',
+            "/osm_ee.FrontendExecutor/GetSshKey",
             osm_ee.frontend_pb2.SshKeyRequest,
             osm_ee.frontend_pb2.SshKeyReply,
         )
index 32b3afe..a36594f 100644 (file)
@@ -39,21 +39,31 @@ import osm_ee.util.util_grpc as util_grpc
 class FrontendExecutor(FrontendExecutorBase):
 
     def __init__(self):
-        self.logger = logging.getLogger('osm_ee.frontend_server')
+        self.logger = logging.getLogger("osm_ee.frontend_server")
         self.base_ee = BaseEE()
 
-    async def RunPrimitive(self, stream: Stream[PrimitiveRequest, PrimitiveReply]) -> None:
+    async def RunPrimitive(
+        self, stream: Stream[PrimitiveRequest, PrimitiveReply]
+    ) -> None:
         request = await stream.recv_message()
         try:
-            self.logger.debug(f'Run primitive: id {request.id}, name: {request.name}, params: {request.params}')
-            async for status, detailed_message in self.base_ee.run_action(request.id, request.name, request.params):
-                self.logger.debug(f'Send response {status}, {detailed_message}')
+            self.logger.debug(
+                f"Run primitive: id {request.id}, name: {request.name}, params: {request.params}"
+            )
+            async for status, detailed_message in self.base_ee.run_action(
+                request.id, request.name, request.params
+            ):
+                self.logger.debug(f"Send response {status}, {detailed_message}")
                 await stream.send_message(
-                    PrimitiveReply(status=status, detailed_message=detailed_message))
+                    PrimitiveReply(status=status, detailed_message=detailed_message)
+                )
         except Exception as e:
-            self.logger.debug(f'Error executing primitive: id {request.id}, name: {request.name}, error_msg: {str(e)}')
+            self.logger.debug(
+                f"Error executing primitive: id {request.id}, name: {request.name}, error_msg: {str(e)}"
+            )
             await stream.send_message(
-                PrimitiveReply(status="ERROR", detailed_message=str(e)))
+                PrimitiveReply(status="ERROR", detailed_message=str(e))
+            )
 
     async def GetSshKey(self, stream: Stream[SshKeyRequest, SshKeyReply]) -> None:
         request = await stream.recv_message()
@@ -62,9 +72,9 @@ class FrontendExecutor(FrontendExecutorBase):
         await stream.send_message(SshKeyReply(message=message))
 
 
-async def main(*, host: str = '0.0.0.0', port: int = 50051) -> None:
+async def main(*, host: str = "0.0.0.0", port: int = 50051) -> None:
     logging.basicConfig()
-    logger = logging.getLogger('osm_ee')
+    logger = logging.getLogger("osm_ee")
     logger.setLevel(logging.DEBUG)
 
     # Generate ssh key
@@ -77,11 +87,11 @@ async def main(*, host: str = '0.0.0.0', port: int = 50051) -> None:
     server = Server([FrontendExecutor()])
     with graceful_exit([server]):
         await server.start(host, port, ssl=util_grpc.create_secure_context())
-        logging.getLogger('osm_ee.frontend_server').debug(f'Serving on {host}:{port}')
+        logging.getLogger("osm_ee.frontend_server").debug(f"Serving on {host}:{port}")
         await server.wait_closed()
 
 
-if __name__ == '__main__':
+if __name__ == "__main__":
     loop = asyncio.get_event_loop()
     try:
         main_task = asyncio.ensure_future(main())
index 8f27875..37ed18b 100644 (file)
@@ -29,12 +29,15 @@ import osm_ee.util.util_ee as util_ee
 logger = logging.getLogger("osm_ee.util_ansible")
 
 
-async def execute_playbook(playbook_name: str, inventory: str, extra_vars: dict,
-                           ) -> (int, str):
-
-    command = 'ansible-playbook --inventory={} --extra-vars {} {}'.format(quote(inventory),
-                                                                          quote(json.dumps(extra_vars)),
-                                                                          quote(playbook_name))
+async def execute_playbook(
+    playbook_name: str,
+    inventory: str,
+    extra_vars: dict,
+) -> (int, str):
+
+    command = "ansible-playbook --inventory={} --extra-vars {} {}".format(
+        quote(inventory), quote(json.dumps(extra_vars)), quote(playbook_name)
+    )
 
     logger.debug("Command to be executed: {}".format(command))
 
index 1d0a232..915e97f 100644 (file)
@@ -27,19 +27,16 @@ from shlex import split
 logger = logging.getLogger("osm_ee.util")
 
 
-async def local_async_exec(command: str
-                           ) -> (int, str, str):
+async def local_async_exec(command: str) -> (int, str, str):
     """
-        Executed a local command using asyncio.
-        TODO - do not know yet if return error code, and stdout and strerr or just one of them
+    Executed a local command using asyncio.
+    TODO - do not know yet if return error code, and stdout and strerr or just one of them
     """
     scommand = split(command)
 
     logger.debug("Execute local command: {}".format(command))
     process = await asyncio.create_subprocess_exec(
-        *scommand,
-        stdout=asyncio.subprocess.PIPE,
-        stderr=asyncio.subprocess.PIPE
+        *scommand, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
     )
 
     # wait for command terminate
index 5fe8fc5..2dc68fa 100644 (file)
@@ -14,17 +14,19 @@ CLIENT_CA_FILE = "/etc/ssl/osm-ca.crt"
 def create_secure_context() -> ssl.SSLContext:
     # retrieve certificates from secrets
     if not _retrieve_certs():
-        logger.warning("TLS Certificates not found, starting gRPC server in unsecure mode")
+        logger.warning(
+            "TLS Certificates not found, starting gRPC server in unsecure mode"
+        )
         return None
     # create SSL context
     ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
     ctx.verify_mode = ssl.CERT_REQUIRED
     ctx.load_cert_chain(SERVER_CERT_FILE, SERVER_KEY_FILE)
     ctx.load_verify_locations(CLIENT_CA_FILE)
-    ctx.set_ciphers('ECDHE+AESGCM:ECDHE+CHACHA20:DHE+AESGCM:DHE+CHACHA20')
-    ctx.set_alpn_protocols(['h2'])
+    ctx.set_ciphers("ECDHE+AESGCM:ECDHE+CHACHA20:DHE+AESGCM:DHE+CHACHA20")
+    ctx.set_alpn_protocols(["h2"])
     try:
-        ctx.set_npn_protocols(['h2'])
+        ctx.set_npn_protocols(["h2"])
     except NotImplementedError:
         pass
     return ctx
index a02334e..96ad587 100644 (file)
@@ -10,7 +10,9 @@ def get_secret_data(name) -> dict:
         # we are not running in kubernetes
         return {}
     # Read the namespace from the service account
-    current_namespace = open("/var/run/secrets/kubernetes.io/serviceaccount/namespace").read()
+    current_namespace = open(
+        "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
+    ).read()
 
     v1 = client.CoreV1Api()
     try:
index 10c2e33..34247d9 100644 (file)
@@ -29,7 +29,7 @@ from osm_ee.exceptions import VnfException
 class VnfEE:
 
     def __init__(self, config_params):
-        self.logger = logging.getLogger('osm_ee.vnf')
+        self.logger = logging.getLogger("osm_ee.vnf")
         self.config_params = config_params
 
     async def config(self, id, params):