diff --git a/sample_ee_vnf/helm-charts/eechart/source/playbook.yaml b/sample_ee_vnf/helm-charts/eechart/source/ansible/playbook.yaml similarity index 100% rename from sample_ee_vnf/helm-charts/eechart/source/playbook.yaml rename to sample_ee_vnf/helm-charts/eechart/source/ansible/playbook.yaml diff --git a/sample_ee_vnf/helm-charts/eechart/source/install.sh b/sample_ee_vnf/helm-charts/eechart/source/install.sh index db8f0538a1c1e508094f2a19bb1ab1e8acbee55e..6fab882d85291e308b4325fc292097ffe8c8a9d8 100755 --- a/sample_ee_vnf/helm-charts/eechart/source/install.sh +++ b/sample_ee_vnf/helm-charts/eechart/source/install.sh @@ -22,15 +22,10 @@ apt-get install -y software-properties-common apt-add-repository --yes --update ppa:ansible/ansible apt install -y ansible -# Install library to execute command remotely by ssh -echo "Installing asynssh" -python3 -m pip install asyncssh - # Install ping system command apt install -y iputils-ping -# Install HTTP python library -python3 -m pip install requests +# Install libraries from requirementsy to execute command remotely by ssh +echo "Installing requirements" +python3 -m pip install -r requirements.txt -# Install MySQL library -python3 -m pip install mysql-connector-python diff --git a/sample_ee_vnf/helm-charts/eechart/source/mysql/__init__.py b/sample_ee_vnf/helm-charts/eechart/source/mysql/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/sample_ee_vnf/helm-charts/eechart/source/mylib.py b/sample_ee_vnf/helm-charts/eechart/source/mysql/mysql_utils.py similarity index 70% rename from sample_ee_vnf/helm-charts/eechart/source/mylib.py rename to sample_ee_vnf/helm-charts/eechart/source/mysql/mysql_utils.py index c0bd3bc8cdebf42c2467d8042a095d94a6664cb3..613f551c578db3838a2f634b33d5064ef04c5362 100644 --- a/sample_ee_vnf/helm-charts/eechart/source/mylib.py +++ b/sample_ee_vnf/helm-charts/eechart/source/mysql/mysql_utils.py @@ -22,35 +22,14 @@ import logging import asyncio -import asyncssh import time - from mysql.connector import connect, Error logger = logging.getLogger("osm_ee.vnf") -async def ssh_exec(host: str, user: str, command: str - ) -> (int, str): - """ - Execute a remote command via SSH. - """ - - try: - async with asyncssh.connect(host, - username=user, - known_hosts=None) as conn: - logger.debug("Executing command '{}'".format(command)) - result = await conn.run(command) - logger.debug("Result: {}".format(result)) - return result.exit_status, result.stderr - 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 +async def mysql_query(host: str, user: str, password: str, retries: int, query: str ) -> (int, str): """ Execute a query to a MySQL database. diff --git a/sample_ee_vnf/helm-charts/eechart/source/requirements-dev.txt b/sample_ee_vnf/helm-charts/eechart/source/requirements-dev.txt new file mode 100644 index 0000000000000000000000000000000000000000..b0998066bbae33e7eaa09006eb9b1e2c398656e8 --- /dev/null +++ b/sample_ee_vnf/helm-charts/eechart/source/requirements-dev.txt @@ -0,0 +1 @@ +osm_ee @ git+https://osm.etsi.org/gitlab/vnf-onboarding/docker-api-fe.git@master#egg=version_subpkg&subdirectory=osm_ee diff --git a/sample_ee_vnf/helm-charts/eechart/source/requirements.txt b/sample_ee_vnf/helm-charts/eechart/source/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..dd4f0033e83b9aa06a9b4a512fe67e687a8e40a7 --- /dev/null +++ b/sample_ee_vnf/helm-charts/eechart/source/requirements.txt @@ -0,0 +1,4 @@ +asyncssh +requests +mysql-connector-python + diff --git a/sample_ee_vnf/helm-charts/eechart/source/run_ssh.sh b/sample_ee_vnf/helm-charts/eechart/source/run_ssh.sh deleted file mode 100755 index 8b58b3cce67868565722c481427d683a14a84fe4..0000000000000000000000000000000000000000 --- a/sample_ee_vnf/helm-charts/eechart/source/run_ssh.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env bash - -date "+%H:%M:%S Starting $0..." -IP=$1 -USERNAME=$2 -SCRIPT=$3 -PARAMS=$4 - -DIR=$(dirname $0) - -date "+%H:%M:%S Waiting for $IP to be ready..." -i=5 -while ! ssh -T -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=10 -o LogLevel=ERROR "$USERNAME"@"$IP" 'exit' ; do - date "+%H:%M:%S Error accessing $IP, retrying..." - sleep 5 - i=$(( $i - 1 )) - [ $i -ge 0 ] || exit 1 -done - -date "+%H:%M:%S SSH server is up, sending script '${DIR}/${SCRIPT}'..." -scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ${DIR}/${SCRIPT} "$USERNAME"@"$IP": -if [ $? -ne 0 ]; then - date "+%H:%M:%S scp error" - exit 1 -fi -date "+%H:%M:%S OK. Setting file permissions" -ssh -T -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=10 -o LogLevel=ERROR "$USERNAME"@"$IP" "chmod a+x $SCRIPT" -if [ $? -ne 0 ]; then - date "+%H:%M:%S ssh error" - exit 1 -fi - -COMMAND="./$SCRIPT" -[ ${#PARAMS} -ge 0 ] || COMMAND="${COMMAND=} $PARAMS" -date "+%H:%M:%S Running '$COMMAND' on $IP..." -ssh -T -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=10 -o LogLevel=ERROR "$USERNAME"@"$IP" "$COMMAND" -if [ $? -ne 0 ]; then - date "+%H:%M:%S ssh error" - exit 1 -fi - -date "+%H:%M:%S End" -exit 0 diff --git a/sample_ee_vnf/helm-charts/eechart/source/ssh/__init__.py b/sample_ee_vnf/helm-charts/eechart/source/ssh/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/sample_ee_vnf/helm-charts/eechart/source/install_nginx.sh b/sample_ee_vnf/helm-charts/eechart/source/ssh/remote/install_nginx.sh similarity index 100% rename from sample_ee_vnf/helm-charts/eechart/source/install_nginx.sh rename to sample_ee_vnf/helm-charts/eechart/source/ssh/remote/install_nginx.sh diff --git a/sample_ee_vnf/helm-charts/eechart/source/ssh/ssh_utils.py b/sample_ee_vnf/helm-charts/eechart/source/ssh/ssh_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..9c574a01a252f2840b308add7e5afdcaa8d81d88 --- /dev/null +++ b/sample_ee_vnf/helm-charts/eechart/source/ssh/ssh_utils.py @@ -0,0 +1,48 @@ +## +# Copyright 2022 Telefonica Investigacion y Desarrollo, S.A.U. +# This file is part of OSM +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# For those usages not covered by the Apache License, Version 2.0 please +# contact with: nfvlabs@tid.es +## + +import logging +import asyncio +import asyncssh +import time +from typing import Tuple + +logger = logging.getLogger("osm_ee.vnf") + + +async def ssh_exec(host: str, user: str, command: str + ) -> Tuple[int, str]: + """ + Execute a remote command via SSH. + """ + try: + async with asyncssh.connect(host, + username=user, + known_hosts=None) as conn: + command=f"ssh/remote/{command}" + logger.debug("Executing command '{}'".format(command)) + result = await conn.run(command) + logger.debug("Result: {}".format(result)) + return result.exit_status, result.stderr + except Exception as e: + logger.error("Error: {}".format(repr(e))) + return -1, str(e) 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 2ed3c61d0a764f3d278ab4fb1e484a8f4cdf72f2..8cd6a1c8e88e971173d347f05c749d215237541d 100644 --- a/sample_ee_vnf/helm-charts/eechart/source/vnf_ee.py +++ b/sample_ee_vnf/helm-charts/eechart/source/vnf_ee.py @@ -16,7 +16,6 @@ ## import asyncio -import asyncssh import requests import logging import os @@ -25,12 +24,15 @@ from osm_ee.exceptions import VnfException import osm_ee.util.util_ee as util_ee import osm_ee.util.util_ansible as util_ansible -import osm_ee.vnf.mylib as mylib +#from osm_ee.vnf import ssh.ssh_utils +import ssh.ssh_utils +#from osm_ee.vnf import mysql.mysql_utils +import mysql.mysql_utils class VnfEE: PLAYBOOK_PATH = "/app/EE/osm_ee/vnf" - SSH_SCRIPT = "/app/EE/osm_ee/vnf/run_ssh.sh" + SSH_SCRIPT = "/app/EE/osm_ee/vnf/ssh/local/run_ssh.sh" def __init__(self, config_params): self.logger = logging.getLogger('osm_ee.vnf') @@ -119,7 +121,7 @@ class VnfEE: # self._check_required_params(params, ["file"]) # command = "touch" + " " + params["file"] - # return_code, description = await mylib.ssh_exec(self.config_params["ssh-hostname"], self.config_params["ssh-username"], command) + # return_code, description = await ssh_utils.ssh_exec(self.config_params["ssh-hostname"], self.config_params["ssh-username"], command) # if return_code != 0: # yield "ERROR", description # else: @@ -135,7 +137,7 @@ class VnfEE: # password = os.getenv('mysql_password') # retries = 3 # query = "SHOW DATABASES" - # return_code, description = mylib.mysql_query(host, user, password, retries, query) + # return_code, description = mysql_utils.mysql_query(host, user, password, retries, query) # if return_code != 0: # yield "ERROR", description # else: