Commit 69387caa authored by garciadeblas's avatar garciadeblas
Browse files

Update structure of sample_ee_vnf


Signed-off-by: garciadeblas's avatargarciadeblas <gerardo.garciadeblas@telefonica.com>
parent 0cc5a2e1
Pipeline #5115 passed with stage
in 1 minute and 49 seconds
......@@ -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
......@@ -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.
......
osm_ee @ git+https://osm.etsi.org/gitlab/vnf-onboarding/docker-api-fe.git@master#egg=version_subpkg&subdirectory=osm_ee
asyncssh
requests
mysql-connector-python
#!/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
##
# 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)
......@@ -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:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment