Commit 9ec20785 authored by aguilard's avatar aguilard
Browse files

Helm-chart based EE sample packages

parent 72572a68
Pipeline #4860 passed with stage
in 1 minute and 38 seconds
nsd:
nsd:
- description: Single sample_ee VNF
df:
- id: default-df
vnf-profile:
- id: sample_ee
virtual-link-connectivity:
- constituent-cpd-id:
- constituent-base-element-id: sample_ee
constituent-cpd-id: vnf-mgmt-ext
virtual-link-profile-id: mgmtnet
- constituent-cpd-id:
- constituent-base-element-id: sample_ee
constituent-cpd-id: vnf-internal-ext
virtual-link-profile-id: internal
vnfd-id: sample_ee-vnf
id: sample_ee-ns
name: sample_ee-ns
version: '1.0'
virtual-link-desc:
- id: mgmtnet
mgmt-network: true
- id: internal
vnfd-id:
- sample_ee-vnf
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
apiVersion: v1
appVersion: "1.0"
description: OSM EE helm chart
name: eechart
version: 0.1.0
#!/bin/bash
##
# 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.
##
echo "Updating operating system"
apt-get update
# Install ansible libraries
echo "Installing ansible"
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
#!/usr/bin/env bash
set -eux
sudo -s <<EOF
apt update
apt install -y nginx
systemctl status nginx
EOF
##
# 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
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)
---
- hosts: all
become: yes
tasks:
- name: Wait 120 seconds, but only start checking after 10 seconds
wait_for_connection:
delay: 20
timeout: 120
- name: Install packages
apt:
name:
- "{{ app }}"
state: latest
cache_valid_time: 3600
#!/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
##
# 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.
##
import asyncio
import asyncssh
import requests
import logging
import os
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
class VnfEE:
PLAYBOOK_PATH = "/app/EE/osm_ee/vnf"
SSH_SCRIPT = "/app/EE/osm_ee/vnf/run_ssh.sh"
def __init__(self, config_params):
self.logger = logging.getLogger('osm_ee.vnf')
self.config_params = config_params
# config method saves SSH access parameters (host and username) for future use by other methods.
# It is mandatory in any case.
async def config(self, id, params):
self.logger.debug("Execute action config, params: {}".format(params))
# Config action is special, params are merged with previous config calls
self.config_params.update(params)
required_params = ["ssh-hostname"]
self._check_required_params(self.config_params, required_params)
yield "OK", "Configured"
# This method implements the "run_script" primitive. Uncomment and modify it if a primitive requires executing a user
# script in the VDU. It needs "file" parameter (script's file to run, must be in "source" directory)
# and optionally "parameters" (command-line arguments for script).
# async def run_script(self, id, params):
# self.logger.debug("Execute action run_script, params: '{}'".format(params))
# self._check_required_params(params, ["file"])
# command = "bash " + self.SSH_SCRIPT + " " + self.config_params["ssh-hostname"] + " " + self.config_params["ssh-username"] + " " + params["file"]
# if "parameters" in params:
# command += " \"" + params.get("parameters", "") + "\""
# self.logger.debug("Command: '{}'".format(command))
# return_code, stdout, stderr = await util_ee.local_async_exec(command)
# if return_code != 0:
# yield "ERROR", "return code {}: {}".format(return_code, stderr.decode())
# else:
# yield "OK", stdout.decode()
# This method implements the "ansible_playbook" primitive. Uncomment and modify it if a primitive requires
# executing an Ansible Playbook. It needs "playbook-name" parameter (playbook file, must be in "source" directory).
# async def ansible_playbook(self, id, params):
# self.logger.debug("Execute action ansible_playbook, params: '{}'".format(params))
# try:
# self._check_required_params(params, ["playbook-name"])
# params["ansible_user"] = self.config_params["ssh-username"]
# inventory = self.config_params["ssh-hostname"] + ","
# playbook = self.PLAYBOOK_PATH + "/" + params["playbook-name"]
# os.environ["ANSIBLE_HOST_KEY_CHECKING"] = "False"
# return_code, stdout, stderr = await util_ansible.execute_playbook(playbook, inventory, params)
# status = "OK" if return_code == 0 else "ERROR"
# yield status, stdout + stderr
# except Exception as e:
# self.logger.debug("Error executing ansible playbook: {}".format(repr(e)))
# yield "ERROR", str(e)
# This method implements the "ping" primitive. Uncomment and modify it if a primitive requires
# executing a local command (such as ping) on EE. It uses "ssh-hostname" as ping destination.
# async def ping(self, id, params):
# self.logger.debug("Execute action ping, params: '{}'".format(params))
# command = "ping -c 3 " + self.config_params["ssh-hostname"]
# return_code, stdout, stderr = await util_ee.local_async_exec(command)
# if return_code != 0:
# yield "ERROR", "return code {}: {}".format(return_code, stderr.decode())
# else:
# yield "OK", stdout.decode()
# This method implements the "http_check" primitive. Uncomment and modify it if a primitive requires
# executing embedded python code on VfnEE class. It requests "http://<ssh-hostname>/" using a HTTP library.
# async def http_check(self, id, params):
# self.logger.debug("Execute action http_check, params: '{}'".format(params))
# try:
# session = requests.Session()
# url = 'http://' + self.config_params["ssh-hostname"]
# self.logger.debug("HTTP GET {}...".format(url))
# req = session.get(url)
# self.logger.debug("{}".format(req.text))
# if req.status_code == 200:
# yield "OK", req.text
# else:
# yield "ERROR", req.text
# except Exception as e:
# self.logger.error("HTTP error: {}".format(repr(e)))
# yield "ERROR", str(e)
# This method implements the "touch" primitive. Uncomment and modify it if you need a primitive that
# imports a user-defined python library. It only needs "file" parameter for creating it in VDU via SSH.
# async def touch(self, id, params):
# self.logger.debug("Execute action touch, params: '{}'".format(params))
# 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)
# 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):
for required_param in required_params:
if required_param not in params:
raise VnfException("Missing required param: {}".format(required_param))
1. Get the application URL by running these commands:
{{- if .Values.ingress.enabled }}
{{- range $host := .Values.ingress.hosts }}
{{- range .paths }}
http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ . }}
{{- end }}
{{- end }}
{{- else if contains "NodePort" .Values.service.type }}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "eechart.fullname" . }})
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
{{- else if contains "LoadBalancer" .Values.service.type }}
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "eechart.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "eechart.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}")
echo http://$SERVICE_IP:{{ .Values.service.port }}
{{- else if contains "ClusterIP" .Values.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "eechart.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl port-forward $POD_NAME 8080:80
{{- end }}
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "eechart.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "eechart.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "eechart.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{/*
Common labels
*/}}
{{- define "eechart.labels" -}}
app.kubernetes.io/name: {{ include "eechart.name" . }}
helm.sh/chart: {{ include "eechart.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end -}}
{{/*
Create the name of the service account to use
*/}}
{{- define "eechart.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
{{ default (include "eechart.fullname" .) .Values.serviceAccount.name }}
{{- else -}}
{{ default "default" .Values.serviceAccount.name }}
{{- end -}}
{{- end -}}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "eechart.fullname" . }}
data:
{{ (.Files.Glob "source/*").AsConfig | indent 2 }}
apiVersion: v1
kind: ConfigMap
metadata:
name: "vnf-snmp-generator-{{ .Values.global.osm.vnf_id | lower }}"
data:
generator.yml: |-
{{ .Files.Get "snmp/generator.yml" | nindent 4}}
\ No newline at end of file
{{- if .Values.ingress.enabled -}}
{{- $fullName := include "eechart.fullname" . -}}
{{- $svcPort := .Values.service.port -}}
{{- if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1beta1
{{- else -}}
apiVersion: extensions/v1beta1
{{- end }}
kind: Ingress
metadata:
name: {{ $fullName }}
labels:
{{ include "eechart.labels" . | indent 4 }}
{{- with .Values.ingress.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
{{- if .Values.ingress.tls }}
tls:
{{- range .Values.ingress.tls }}
- hosts:
{{- range .hosts }}
- {{ . | quote }}
{{- end }}
secretName: {{ .secretName }}
{{- end }}
{{- end }}
rules:
{{- range .Values.ingress.hosts }}
- host: {{ .host | quote }}
http:
paths:
{{- range .paths }}
- path: {{ . }}
backend:
serviceName: {{ $fullName }}
servicePort: {{ $svcPort }}
{{- end }}
{{- end }}
{{- end }}
apiVersion: v1
kind: ConfigMap
metadata:
name: "vnf-snmp-mibs-{{ .Values.global.osm.vnf_id | lower}}"
data:
{{ (.Files.Glob "snmp/mibs/**").AsConfig | indent 2 }}
\ No newline at end of file
apiVersion: v1
kind: Service
metadata:
name: {{ include "eechart.fullname" . }}
labels:
{{ include "eechart.labels" . | indent 4 }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: grpc
protocol: TCP
name: grpc
selector:
app.kubernetes.io/name: {{ include "eechart.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ template "eechart.serviceAccountName" . }}
labels:
{{ include "eechart.labels" . | indent 4 }}
{{- end -}}
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: {{ include "eechart.fullname" . }}
labels:
{{ include "eechart.labels" . | indent 4 }}
spec:
serviceName: {{ include "eechart.fullname" . }}
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app.kubernetes.io/name: {{ include "eechart.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
template:
metadata:
labels:
app.kubernetes.io/name: {{ include "eechart.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
vnf: {{ .Values.global.osm.vnf_id | lower}}
spec:
imagePullSecrets:
- name: regcred
serviceAccountName: {{ template "eechart.serviceAccountName" . }}
securityContext:
runAsUser: 0
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: grpc
containerPort: 50051
protocol: TCP
resources:
{{- toYaml .Values.resources | nindent 12 }}
volumeMounts:
- name: osm-ee
mountPath: /app/storage
- name: osm-ee-source
mountPath: /app/EE/osm_ee/vnf
- name: snmp-config-volume
mountPath: /etc/snmp_exporter
- name: vnf-mibs
mountPath: /root/.snmp/mibs
- name: vnf-generator
mountPath: /app/vnf/generator
volumes:
- name: osm-ee-source
configMap:
name: {{ include "eechart.fullname" . }}
- name: snmp-config-volume
hostPath:
path: "/var/lib/osm/snmp_exporter/{{ .Values.global.osm.vnf_id | lower }}/"
- name: vnf-mibs
configMap:
name: "vnf-snmp-mibs-{{ .Values.global.osm.vnf_id | lower}}"
- name: vnf-generator
configMap:
name: "vnf-snmp-generator-{{ .Values.global.osm.vnf_id | lower}}"
- name: osm-ee
hostPath:
path: /var/lib/osm/osm/osm_osm_packages/_data
apiVersion: v1
kind: Pod
metadata:
name: "{{ include "eechart.fullname" . }}-test-connection"
labels:
{{ include "eechart.labels" . | indent 4 }}
annotations:
"helm.sh/hook": test-success
spec:
containers:
- name: wget
image: busybox
command: ['wget']
args: ['{{ include "eechart.fullname" . }}:{{ .Values.service.port }}']
restartPolicy: Never
global:
osm:
vnf_id: AVNFId
# Default values for eechart.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1
image:
repository: illoret/grpcee
tag: latest
pullPolicy: IfNotPresent
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""
serviceAccount:
# Specifies whether a service account should be created
create: false
# The name of the service account to use.
# If not set and create is true, a name is generated using the fullname template
name:
podSecurityContext: {}
# fsGroup: 2000
securityContext: {}
# capabilities:
# drop:
# - ALL
# readOnlyRootFilesystem: true
# runAsNonRoot: true
# runAsUser: 1000
service:
type: ClusterIP
port: 50050
ingress:
enabled: false
annotations: {}
# kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true"
hosts:
- host: chart-example.local
paths: []
tls: []
# - secretName: chart-example-tls
# hosts:
# - chart-example.local
resources: {}
# We usually recommend not to specify default resources and to leave this as a conscious
# choice for the user. This also increases chances charts run on environments with little
# resources, such as Minikube. If you do want to specify resources, uncomment the following
# lines, adjust them as necessary, and remove the curly braces after 'resources:'.
# limits:
# cpu: 100m
# memory: 128Mi
# requests:
# cpu: 100m
# memory: 128Mi
nodeSelector: {}
tolerations: []
affinity: {}
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