Skip to content

Commit

Permalink
Magic commands available everywhere in the project
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonioSanch3z committed Aug 28, 2024
1 parent e7bbd26 commit dac8559
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 21 deletions.
1 change: 1 addition & 0 deletions apricot_magics/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .apricot_magics import load_ipython_extension
98 changes: 80 additions & 18 deletions src/apricot_magics.py → apricot_magics/apricot_magics.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
from tabulate import tabulate
from IPython.core.magic import Magics, line_magic, line_cell_magic, magics_class
from subprocess import run, PIPE, CalledProcessError
from subprocess import run, PIPE, CalledProcessError, check_output

import os
import json

@magics_class
class Apricot_Magics(Magics):

def __init__(self, shell):
super().__init__(shell)
self.im_client_path = None

########################
# Auxiliar functions #
########################

def find_im_client_path(self) -> str:
try:
im_client_path = check_output(['which', 'im_client.py'], text=True).strip()
if not im_client_path:
raise FileNotFoundError("im_client.py not found in the system PATH.")
return im_client_path
except CalledProcessError:
raise FileNotFoundError("Failed to find im_client.py in the system PATH.")

def get_im_client_path(self) -> str:
if self.im_client_path is None:
self.find_im_client_path()
return self.im_client_path

def create_auth_pipe(self, infrastructure_id):
# Get the absolute path to the infrastructuresList.json file
base_dir = os.path.dirname(__file__)
file_path = os.path.abspath(os.path.join(base_dir, '..', 'infrastructuresList.json'))

# Read the JSON data from the file
with open('../infrastructuresList.json') as f:
data = json.load(f)
try:
with open(file_path) as f:
data = json.load(f)
except FileNotFoundError:
raise FileNotFoundError(f"File not found: {file_path}")
except json.JSONDecodeError:
raise ValueError(f"Error decoding JSON from file: {file_path}")

# Find the infrastructure with the specified ID
found_infrastructure = None
for infrastructure in data['infrastructures']:
if infrastructure['infrastructureID'] == infrastructure_id:
for infrastructure in data.get('infrastructures', []):
if infrastructure.get('infrastructureID') == infrastructure_id:
found_infrastructure = infrastructure
break

Expand All @@ -38,6 +65,7 @@ def create_auth_pipe(self, infrastructure_id):
auth_content += f"id = {found_infrastructure['id']}; type = {found_infrastructure['type']}; username = {found_infrastructure['user']}; password = {found_infrastructure['pass']}"
elif found_infrastructure['type'] == "EGI":
auth_content += f"id = {found_infrastructure['id']}; type = {found_infrastructure['type']}; host = {found_infrastructure['host']}; vo = {found_infrastructure['vo']}; token = {found_infrastructure['EGIToken']}"

# Write auth-pipe content to a file
with open('auth-pipe', 'w') as auth_file:
auth_file.write(auth_content)
Expand All @@ -50,10 +78,11 @@ def generate_key(self, infrastructure_id, vm_id):
##########################################
private_key_content = None
host_ip = None
im_client_path = self.get_im_client_path()

cmd_getvminfo = [
'python3',
'/usr/local/bin/im_client.py',
im_client_path,
'getvminfo',
infrastructure_id,
vm_id,
Expand Down Expand Up @@ -111,6 +140,8 @@ def apricot_log(self, line):
print("Usage: apricot_log infrastructure-id\n")
return "Fail"

im_client_path = self.get_im_client_path()

# Split the input line to extract the infrastructure ID
inf_id = line.split()[0]

Expand All @@ -124,7 +155,7 @@ def apricot_log(self, line):
# Construct the command to retrieve log messages
cmd_getcontmsg = [
"python3",
"/usr/local/bin/im_client.py",
im_client_path,
"getcontmsg",
inf_id,
"-a",
Expand All @@ -151,9 +182,17 @@ def apricot_log(self, line):
@line_magic
def apricot_ls(self, line):
infrastructures_list = []
im_client_path = self.get_im_client_path()

with open('../infrastructuresList.json') as f:
data = json.load(f)
base_dir = os.path.dirname(__file__)
file_path = os.path.abspath(os.path.join(base_dir, '..', 'infrastructuresList.json'))

try:
with open(file_path) as f:
data = json.load(f)
except FileNotFoundError:
print(f"File not found: {file_path}")
return

# Iterate through each infrastructure
for infrastructure in data.get('infrastructures', []):
Expand All @@ -172,7 +211,7 @@ def apricot_ls(self, line):

cmd_getstate = [
'python3',
'/usr/local/bin/im_client.py',
im_client_path,
'getstate',
infrastructure_info['InfrastructureID'],
'-r',
Expand All @@ -199,7 +238,7 @@ def apricot_ls(self, line):

cmd_getvminfo = [
'python3',
'/usr/local/bin/im_client.py',
im_client_path,
'getvminfo',
infrastructure_info['InfrastructureID'],
'0',
Expand Down Expand Up @@ -242,6 +281,8 @@ def apricot_info(self, line):
print("Usage: apricot_info infrastructure-id\n")
return "Fail"

im_client_path = self.get_im_client_path()

# Split the input line to extract the infrastructure ID
inf_id = line.split()[0]

Expand All @@ -255,7 +296,7 @@ def apricot_info(self, line):
# Construct the command to retrieve log messages
cmd_getinfo = [
"python3",
"/usr/local/bin/im_client.py",
im_client_path,
"getinfo",
inf_id,
"-a",
Expand Down Expand Up @@ -285,6 +326,8 @@ def apricot_vmls(self, line):
print("Usage: apricot_vmls infrastructure-id\n")
return "Fail"

im_client_path = self.get_im_client_path()

# Split the input line to extract the infrastructure ID
inf_id = line.split()[0]

Expand All @@ -298,7 +341,7 @@ def apricot_vmls(self, line):

cmd_getinfo = [
'python3',
'/usr/local/bin/im_client.py',
im_client_path,
'getinfo',
inf_id,
'-r',
Expand Down Expand Up @@ -532,6 +575,7 @@ def apricot(self, code, cell=None):
print("Usage: destroy infrastructure-id")
return "Fail"
else:
im_client_path = self.get_im_client_path()
inf_id = words[1]

try:
Expand All @@ -542,7 +586,7 @@ def apricot(self, code, cell=None):

cmd_destroy = [
'python3',
'/usr/local/bin/im_client.py',
im_client_path,
'destroy',
inf_id,
'-r',
Expand Down Expand Up @@ -570,18 +614,36 @@ def apricot(self, code, cell=None):
return "Fail"

# Load infrastructure list from JSON file
with open('../infrastructuresList.json', 'r') as f:
data = json.load(f)
base_dir = os.path.dirname(__file__)
file_path = os.path.abspath(os.path.join(base_dir, '..', 'infrastructuresList.json'))

# Load infrastructure list from JSON file
try:
with open(file_path, 'r') as f:
data = json.load(f)
except FileNotFoundError:
print(f"File not found: {file_path}")
return "Failed"
except json.JSONDecodeError:
print(f"Error decoding JSON from file: {file_path}")
return "Failed"

# Find and remove the infrastructure with the specified ID
for infrastructure in data['infrastructures']:
if infrastructure['infrastructureID'] == inf_id:
data['infrastructures'].remove(infrastructure)
break

base_dir = os.path.dirname(__file__)
file_path = os.path.abspath(os.path.join(base_dir, '..', 'infrastructuresList.json'))

# Write the updated infrastructure list back to the JSON file
with open('../infrastructuresList.json', 'w') as f:
json.dump(data, f, indent=4)
try:
with open(file_path, 'w') as f:
json.dump(data, f, indent=4)
except IOError as e:
print(f"Error writing to file {file_path}: {e}")
return "Failed"

except CalledProcessError as e:
print(f"Error: {e}")
Expand Down
21 changes: 20 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
__import__("setuptools").setup()
from setuptools import setup, find_packages

setup(
name='apricot_magics',
version='0.1.0',
packages=find_packages(), # Automatically find the `apricot_magics` package
install_requires=[
'ipython',
],
entry_points={
'ipython.plugins': [
'apricot_magics = apricot_magics.apricot_magics'
]
},
include_package_data=True,
package_data={
'': ['*.py'],
},
zip_safe=False
)
5 changes: 3 additions & 2 deletions src/deploymentMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { KernelManager } from '@jupyterlab/services';
import { Widget } from '@lumino/widgets';
import { Dialog } from '@jupyterlab/apputils';
import { executeKernelCommand, getIMClientPath } from './utils';
import * as path from 'path';

type UserInput = {
name: string;
Expand Down Expand Up @@ -510,7 +511,7 @@ async function deployIMCommand(
): Promise<string> {
const pipeAuth = `${obj.infName}-auth-pipe`;
const imClientPath = await getIMClientPath();
const templatePath = '$PWD/deployed-template.yaml';
const templatePath = path.resolve(__dirname, '..', 'deployed-template.yaml');

let cmd = `%%bash
PWD=$(pwd)
Expand Down Expand Up @@ -556,7 +557,7 @@ async function deployIMCommand(
}

async function saveToInfrastructureList(obj: IInfrastructureData) {
const filePath = '$PWD/infrastructuresList.json';
const filePath = path.resolve(__dirname, '..', 'infrastructuresList.json');

// Construct the bash command
const cmd = `
Expand Down

0 comments on commit dac8559

Please sign in to comment.