diff --git a/sample_ee_vnf/helm-charts/eechart/charts.sample/mysql-8.8.26.tgz b/sample_ee_vnf/helm-charts/eechart/charts.sample/mysql-8.8.26.tgz new file mode 100644 index 0000000000000000000000000000000000000000..2cfb9b29533b6f6837de93a2022eec32c11b0b4b Binary files /dev/null and b/sample_ee_vnf/helm-charts/eechart/charts.sample/mysql-8.8.26.tgz differ diff --git a/sample_ee_vnf/helm-charts/eechart/charts/.gitkeep b/sample_ee_vnf/helm-charts/eechart/charts/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/sample_ee_vnf/helm-charts/eechart/source/install.sh b/sample_ee_vnf/helm-charts/eechart/source/install.sh index ffec14d6a3dc0f95158c797bd4dfbb88a041f425..db8f0538a1c1e508094f2a19bb1ab1e8acbee55e 100755 --- a/sample_ee_vnf/helm-charts/eechart/source/install.sh +++ b/sample_ee_vnf/helm-charts/eechart/source/install.sh @@ -31,3 +31,6 @@ apt install -y iputils-ping # Install HTTP python library python3 -m pip install requests + +# Install MySQL library +python3 -m pip install mysql-connector-python diff --git a/sample_ee_vnf/helm-charts/eechart/source/mylib.py b/sample_ee_vnf/helm-charts/eechart/source/mylib.py index 2efea2138653aa7255a15a7ea13906d3cc99875d..c0bd3bc8cdebf42c2467d8042a095d94a6664cb3 100644 --- a/sample_ee_vnf/helm-charts/eechart/source/mylib.py +++ b/sample_ee_vnf/helm-charts/eechart/source/mylib.py @@ -23,6 +23,10 @@ import logging import asyncio import asyncssh +import time + + +from mysql.connector import connect, Error logger = logging.getLogger("osm_ee.vnf") @@ -44,3 +48,33 @@ async def ssh_exec(host: str, user: str, command: str except Exception as e: logger.error("Error: {}".format(repr(e))) return -1, str(e) + + +def mysql_query(host: str, user: str, password: str, retries: int, query: str + ) -> (int, str): + """ + Execute a query to a MySQL database. + """ + + text = "" + logger.debug("Host: '{}', user: '{}', password: '{}', query: '{}'".format(host, user, password, query)) + for i in range(0, retries): + try: + with connect( + host=host, + user=user, + password=password, + ) as connection: + with connection.cursor() as cursor: + cursor.execute(query) + for (db) in cursor: + logger.debug(db) + text = text + str(db[0]) + ", " + return 0, text[0:len(text)-2] + except Error as e: + text = str(e) + logger.debug("Error: {}".format(e)) + time.sleep(3) + continue + + return -1, text diff --git a/sample_ee_vnf/helm-charts/eechart/source/vnf_ee.py b/sample_ee_vnf/helm-charts/eechart/source/vnf_ee.py index 4c0f1cb7065ec0ebe5976431b2937084252f5d25..2ed3c61d0a764f3d278ab4fb1e484a8f4cdf72f2 100644 --- a/sample_ee_vnf/helm-charts/eechart/source/vnf_ee.py +++ b/sample_ee_vnf/helm-charts/eechart/source/vnf_ee.py @@ -125,6 +125,23 @@ class VnfEE: # else: # yield "OK", description + # This method implements the "check_database" primitive. Uncomment and modify it if a primitive requires + # accessing to a service provided by a helm subchart. Access parameters are read from environment variables + # async def check_database(self, id, params): + # self.logger.debug("Execute action check_database, params: '{}'".format(params)) + + # host = os.getenv('mysql_host') + # user = os.getenv('mysql_user') + # password = os.getenv('mysql_password') + # retries = 3 + # query = "SHOW DATABASES" + # return_code, description = mylib.mysql_query(host, user, password, retries, query) + # if return_code != 0: + # yield "ERROR", description + # else: + # yield "OK", description + + # Static method that verifies whether a parameter exists in the map @staticmethod def _check_required_params(params, required_params): diff --git a/sample_ee_vnf/helm-charts/eechart/templates/secret.yaml b/sample_ee_vnf/helm-charts/eechart/templates/secret.yaml new file mode 100644 index 0000000000000000000000000000000000000000..62d4b267e8cae8df9dcdee471dcbb6a4151b040b --- /dev/null +++ b/sample_ee_vnf/helm-charts/eechart/templates/secret.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: Secret +metadata: + name: {{ include "eechart.fullname" . }} +type: Opaque +data: + mysql_host: {{ .Values.mysql.fullnameOverride | b64enc | quote }} + mysql_user: {{ "root" | b64enc | quote }} + mysql_password: {{ .Values.mysql.auth.rootPassword | b64enc | quote }} diff --git a/sample_ee_vnf/helm-charts/eechart/templates/statefulset.yaml b/sample_ee_vnf/helm-charts/eechart/templates/statefulset.yaml index 8b124995057b996bbf43b867ffb908da1c254f31..a13cfa6e7a767ad96ac697fe2ae406c4b9ec6f53 100755 --- a/sample_ee_vnf/helm-charts/eechart/templates/statefulset.yaml +++ b/sample_ee_vnf/helm-charts/eechart/templates/statefulset.yaml @@ -27,6 +27,9 @@ spec: - name: {{ .Chart.Name }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" imagePullPolicy: {{ .Values.image.pullPolicy }} + envFrom: + - secretRef: + name: {{ include "eechart.fullname" . }} ports: - name: grpc containerPort: 50051 diff --git a/sample_ee_vnf/helm-charts/eechart/values.yaml b/sample_ee_vnf/helm-charts/eechart/values.yaml index 7006b5df1ac6ad4972e7f9a535f26ebff21176ae..fa824d7572b0162e0c667fc0d6547a11636b30fe 100644 --- a/sample_ee_vnf/helm-charts/eechart/values.yaml +++ b/sample_ee_vnf/helm-charts/eechart/values.yaml @@ -70,3 +70,8 @@ nodeSelector: {} tolerations: [] affinity: {} + +mysql: + auth: + rootPassword: "123456" + fullnameOverride: "eechart-mysql" diff --git a/sample_ee_vnf/sample_ee_vnfd.yaml b/sample_ee_vnf/sample_ee_vnfd.yaml index 87d6a41562ca48f744388b8ee6a2a91cd3e017d0..d528a5c6b0b2acc3112458052db24b9c29ff6fb1 100644 --- a/sample_ee_vnf/sample_ee_vnfd.yaml +++ b/sample_ee_vnf/sample_ee_vnfd.yaml @@ -42,6 +42,9 @@ vnfd: # parameter: # - data-type: STRING # name: file + # - execution-environment-primitive: check_database + # execution-environment-ref: sample_ee + # name: check_database config-access: ssh-access: default-user: ubuntu @@ -86,6 +89,9 @@ vnfd: # - name: file # value: myfile.txt # seq: 6 + # - execution-environment-ref: sample_ee + # name: check_database + # seq: 7 ext-cpd: - id: vnf-mgmt-ext int-cpd: