Skip to content

Commit

Permalink
Merge pull request #570 from deNBI/feat/metadata_server
Browse files Browse the repository at this point in the history
Feat/metadata server
  • Loading branch information
dweinholz authored Sep 11, 2024
2 parents 3a6fcf3 + db39074 commit 5b631b4
Show file tree
Hide file tree
Showing 19 changed files with 11,690 additions and 3,759 deletions.
22 changes: 22 additions & 0 deletions portal_client.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ typedef i32 int
/** The Version of the Portal-Client*/
const string VERSION= '1.0.0'


struct VirtualMachineServerMetadata {
1: list<string> public_keys
2: string hashed_auth_token
3: string ip
}
struct Backend {
1: i64 id,
2: string owner,
Expand Down Expand Up @@ -210,6 +216,13 @@ struct PlaybookResult {
3: required string stderr
}

exception MetadataServerNotAvailableException {
1: string message
}
exception MetadataServerNotAllowedException{
1: string message
}

exception ResourceNotFoundException {
/** Name already used. */
1: string message
Expand Down Expand Up @@ -452,6 +465,7 @@ service VirtualMachineService {
10:optional string research_environment
11:optional list<string> additional_security_group_ids,
12:optional string slurm_version,
13:optional string metadata_token,

)

Expand Down Expand Up @@ -485,6 +499,8 @@ service VirtualMachineService {
7:list<map<string,string>> volume_ids_path_new,
8:list<map<string,string>> volume_ids_path_attach,
9:optional list<string> additional_security_group_ids,
10:optional string metadata_token,

) throws (1:NameAlreadyUsedException e,2:ResourceNotAvailableException r,3: ImageNotFoundException i,4: FlavorNotFoundException f,5:DefaultException d)

/** Check if there is an instance with name */
Expand Down Expand Up @@ -542,6 +558,9 @@ service VirtualMachineService {
) throws (1:BackendNotFoundException b,2:DefaultException d)


void set_metadata_server_data(1:string ip,2:VirtualMachineServerMetadata metadata) throws (1:MetadataServerNotAvailableException m,2:MetadataServerNotAllowedException b)
void remove_metadata_server_data(1:string ip) throws (1:MetadataServerNotAvailableException m,2:MetadataServerNotAllowedException b)
void is_metadata_server_available() throws (1:MetadataServerNotAvailableException m,2:MetadataServerNotAllowedException b)

/** Delete a backend*/
void delete_backend(
Expand Down Expand Up @@ -825,4 +844,7 @@ service VirtualMachineService {

throws (1:ServerNotFoundException e, 2: OpenStackConflictException c)




}
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ colorama==0.4.6
gevent==24.2.1
pytest==8.3.3
types-redis==4.6.0.20240903
oslo.utils==7.3.0
45 changes: 45 additions & 0 deletions scripts/get_keys_from_metadata_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

# Load the auth token from .metadata_config.env
source ~/.metadata_config.env

# Define the URL and machine IP
URL="http://192.168.2.122:8000/metadata/192.168.2.74"
AUTH_HEADER="auth_token: ${METADATA_ACCESS_TOKEN}"

# Fetch the JSON response from the URL
response=$(curl -s -X GET "$URL" -H "$AUTH_HEADER")

# Extract the public_keys array from the JSON response
public_keys=$(echo "$response" | jq -r '.public_keys[]')

# Check if public_keys is empty
if [ -z "$public_keys" ]; then
echo "No public keys found. authorized_keys file not updated."
exit 0
fi

# Ensure the .ssh directory and authorized_keys file exist
mkdir -p ~/.ssh
touch ~/.ssh/authorized_keys

# Function to check if a key already exists in the authorized_keys file
key_exists() {
grep -Fqx "$1" ~/.ssh/authorized_keys
}

# Add keys to authorized_keys if they don't already exist
added_keys=0

while IFS= read -r key; do
if ! key_exists "$key"; then
echo "$key" >> ~/.ssh/authorized_keys
((added_keys++))
fi
done <<< "$public_keys"

if [ $added_keys -gt 0 ]; then
echo "$added_keys new public key(s) have been added to the authorized_keys file."
else
echo "All public keys were already present. No changes made to authorized_keys file."
fi
16 changes: 16 additions & 0 deletions simple_vm_client/VirtualMachineHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from simple_vm_client.util import thrift_converter
from simple_vm_client.util.logger import setup_custom_logger

from .metadata_connector.metadata_connector import MetadataConnector
from .ttypes import (
VM,
Backend,
Expand All @@ -23,6 +24,7 @@
PlaybookResult,
ResearchEnvironmentTemplate,
Snapshot,
VirtualMachineServerMetadata,
Volume,
)
from .VirtualMachineService import Iface
Expand All @@ -37,6 +39,7 @@ def __init__(self, config_file: str):
self.openstack_connector = OpenStackConnector(config_file=config_file)
self.bibigrid_connector = BibigridConnector(config_file=config_file)
self.forc_connector = ForcConnector(config_file=config_file)
self.metadata_connetor = MetadataConnector(config_file=config_file)

def keyboard_interrupt_handler_playbooks(self) -> None:
for k, v in self.forc_connector._active_playbooks.items():
Expand All @@ -50,6 +53,15 @@ def keyboard_interrupt_handler_playbooks(self) -> None:
self.openstack_connector.delete_server(openstack_id=k)
raise SystemExit(0)

def is_metadata_server_available(self):
return self.metadata_connetor.is_metadata_server_available()

def set_metadata_server_data(self, ip: str, metadata: VirtualMachineServerMetadata):
return self.metadata_connetor.set_metadata(ip=ip, metadata=metadata)

def remove_metadata_server_data(self, ip: str):
return self.metadata_connetor.remove_metadata(ip=ip)

def get_images(self) -> list[Image]:
images: list[Image] = thrift_converter.os_to_thrift_images(
openstack_images=self.openstack_connector.get_images()
Expand Down Expand Up @@ -370,6 +382,7 @@ def start_server(
research_environment: str,
additional_security_group_ids: list[str],
slurm_version: str = None,
metadata_token: str = None,
) -> str:
if research_environment:
research_environment_metadata = (
Expand All @@ -391,6 +404,7 @@ def start_server(
research_environment_metadata=research_environment_metadata,
additional_security_group_ids=additional_security_group_ids,
slurm_version=slurm_version,
metadata_token=metadata_token,
)

def start_server_with_custom_key(
Expand All @@ -403,6 +417,7 @@ def start_server_with_custom_key(
volume_ids_path_new: list[dict[str, str]],
volume_ids_path_attach: list[dict[str, str]],
additional_security_group_ids: list[str],
metadata_token: str = None,
) -> str:
if research_environment:
research_environment_metadata = (
Expand All @@ -421,6 +436,7 @@ def start_server_with_custom_key(
volume_ids_path_new=volume_ids_path_new,
volume_ids_path_attach=volume_ids_path_attach,
additional_security_group_ids=additional_security_group_ids,
metadata_token=metadata_token,
)
self.forc_connector.set_vm_wait_for_playbook(
openstack_id=openstack_id, private_key=private_key, name=servername
Expand Down
Loading

0 comments on commit 5b631b4

Please sign in to comment.