Skip to content

Commit

Permalink
add initial code for VO-level monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-luna-valero committed Dec 5, 2024
1 parent 49e3720 commit c2badb9
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
95 changes: 95 additions & 0 deletions fedcloud_vm_monitoring/vo_mon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""VO-level testing for a given site in a VO"""

import ipaddress
import subprocess
from collections import defaultdict
from datetime import datetime, timezone

import click
import ldap3
import paramiko
from dateutil.parser import parse
from fedcloudclient.openstack import fedcloud_openstack
from fedcloudclient.sites import find_endpoint_and_project_id
from ldap3.core.exceptions import LDAPException
from paramiko import SSHException

from imclient import IMClient
import time
import os

IM_REST_API = "https://im.egi.eu/im"
AUTH_FILE = "auth.dat"

class VOTestException(Exception):
pass


class VOTest:
"""Helper class to call im-client easily"""

def __init__(self, vo, site, token):
self.vo = vo
self.site = site
self.token = token
self.now = datetime.now(timezone.utc)

def create_auth_file(self, filepath):
with open(filepath, "w") as output_file:
output_file.write("id = im; type = InfrastructureManager; token = \"{}\"\n".format(self.token))
output_file.write("id = egi; type = EGI; host = {}; vo = {}; token = \"{}\"\n".format(self.site, self.vo, self.token))

def delete_auth_file(self, filepath):
if os.path.exists(filepath): os.remove(filepath)

def create_vm_tosca_template(self):
inf_desc = """
tosca_definitions_version: tosca_simple_yaml_1_0
imports:
- grycap_custom_types: https://raw.githubusercontent.com/grycap/tosca/main/custom_types.yaml
topology_template:
node_templates:
simple_node:
type: tosca.nodes.indigo.Compute
capabilities:
endpoint:
properties:
network_name: PUBLIC
host:
properties:
num_cpus: 2
mem_size: 4 GB
os:
properties:
image: appdb://SITE/egi.ubuntu.22.04?VO
outputs:
node_ip:
value: { get_attribute: [ simple_node, public_address, 0 ] }
node_creds:
value: { get_attribute: [ simple_node, endpoint, credential, 0 ] }
"""
return inf_desc.replace("SITE", self.site).replace("VO", self.vo)

def launch_test_vm(self):
self.create_auth_file(AUTH_FILE)
tosca_template = self.create_vm_tosca_template()
auth = IMClient.read_auth_data(AUTH_FILE)
imclient = IMClient.init_client(IM_REST_API, auth)
click.echo(f"[+] Creating test VM...")
success, inf_id = imclient.create(tosca_template, desc_type="yaml")
click.echo(f"[+] IM: {success}, {inf_id}")
state = "pending"
while state != "configured":
click.echo(f"[+] Waiting for test VM to be ready...")
time.sleep(10)
success, state = imclient.getvminfo(inf_id, 0, prop='state')
time.sleep(30)
# click.echo(f"[+] Waiting for test VM to be ready...")
# success, info = imclient._wait()
# click.echo(f"[+] IM: {success}, {inf_id}")
# TODO: need to ssh into VM here
success, err = imclient.destroy(inf_id)
click.echo(f"[+] IM: {success}, {err}")
self.delete_auth_file(AUTH_FILE)
31 changes: 31 additions & 0 deletions fedcloud_vm_monitoring/vo_mon_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""VO-level testing"""

import click
from fedcloud_vm_monitoring.appdb import AppDB
from fedcloud_vm_monitoring.vo_mon import VOTest, VOTestException
from fedcloudclient.decorators import oidc_params
from fedcloudclient.sites import list_sites

@click.command()
@oidc_params
@click.option("--site", help="Restrict the testing to the site provided")
@click.option(
"--vo",
default="vo.access.egi.eu",
help="VO name to test",
show_default=True,
)
def main(site, vo, access_token):
# gather all sites in a given VO
appdb = AppDB(vo)
appdb_sites = appdb.get_sites_for_vo()
fedcloudclient_sites = list_sites(vo)
sites = [site] if site else set(appdb_sites + fedcloudclient_sites)
for s in sites:
click.secho(f"[.] Testing VO {vo} at {s}", fg="blue", bold=True)
try:
vo_test = VOTest(vo, site, access_token)
vo_test.launch_test_vm()

except VOTestException as e:
click.echo(" ".join([click.style("ERROR:", fg="red"), str(e)]), err=True)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ readme = "README.md"

[tool.poetry.scripts]
fedcloud-vo-monitor = "fedcloud_vm_monitoring.cli:main"
fedcloud-vo-testing = "fedcloud_vm_monitoring.vo_mon_cli:main"

[tool.poetry.dependencies]
python = "^3.12"
Expand Down

0 comments on commit c2badb9

Please sign in to comment.