Skip to content

Commit

Permalink
Merge pull request #1 from grycap/antonio-dev
Browse files Browse the repository at this point in the history
Added dynamic path to im_client.py
  • Loading branch information
AntonioSanch3z authored Jul 25, 2024
2 parents b1f7c87 + 925cbc8 commit cdb438c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 33 deletions.
Empty file added deployed-templates/.gitkeep
Empty file.
40 changes: 13 additions & 27 deletions src/deploymentMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ContentsManager } from '@jupyterlab/services';
import { KernelManager } from '@jupyterlab/services';
import { Widget } from '@lumino/widgets';
import { Dialog } from '@jupyterlab/apputils';
import { executeKernelCommand, getIMClientPath } from './utils';

interface IDeployInfo {
IMuser: string;
Expand Down Expand Up @@ -179,27 +180,6 @@ function getInputValue(inputId: string): string {
return input.value;
}

async function executeKernelCommand(
command: string,
callback: (output: string) => void
): Promise<void> {
try {
const kernelManager = new KernelManager();
const kernel = await kernelManager.startNew();
const future = kernel.requestExecute({ code: command });

future.onIOPub = msg => {
const content = msg.content as any;
const outputText =
content.text || (content.data && content.data['text/plain']);
callback(outputText);
};
} catch (error) {
console.error('Error executing kernel command:', error);
deploying = false;
}
}

async function computeHash(input: string): Promise<string> {
const msgUint8 = new TextEncoder().encode(input);
const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8);
Expand Down Expand Up @@ -445,8 +425,9 @@ async function createChildsForm(
//* Bash commands *//
//*********************//

function selectImage(obj: IDeployInfo): string {
async function selectImage(obj: IDeployInfo): Promise<string> {
const pipeAuth = `${obj.infName}-auth-pipe`;
const imClientPath = await getIMClientPath();

let cmd = `%%bash
PWD=$(pwd)
Expand All @@ -473,7 +454,7 @@ function selectImage(obj: IDeployInfo): string {

cmd += `echo -e "${authContent}" > $PWD/${pipeAuth} &
# Create final command where the output is stored in "imageOut"
imageOut=$(python3 /usr/local/bin/im_client.py -a $PWD/${pipeAuth} -r https://im.egi.eu/im cloudimages ${obj.id})
imageOut=$(python3 ${imClientPath} -a $PWD/${pipeAuth} -r https://im.egi.eu/im cloudimages ${obj.id})
# Remove pipe
rm -f $PWD/${pipeAuth} &> /dev/null
# Print IM output on stderr or stdout
Expand Down Expand Up @@ -508,10 +489,15 @@ const getEGIToken = async () => {
});
};

function deployIMCommand(obj: IDeployInfo, mergedTemplate: string): string {
async function deployIMCommand(
obj: IDeployInfo,
mergedTemplate: string
): Promise<string> {
const pipeAuth = `${obj.infName}-auth-pipe`;
const imClientPath = await getIMClientPath();
const imageRADL = obj.infName;
const templatePath = `$PWD/deployed-templates/${imageRADL}.yaml`;

let cmd = `%%bash
PWD=$(pwd)
# Remove pipes if they exist
Expand Down Expand Up @@ -539,7 +525,7 @@ function deployIMCommand(obj: IDeployInfo, mergedTemplate: string): string {

cmd += `echo -e "${authContent}" > $PWD/${pipeAuth} &
# Create final command where the output is stored in "imageOut"
imageOut=$(python3 /usr/local/bin/im_client.py -a $PWD/${pipeAuth} create ${templatePath} -r https://im.egi.eu/im)
imageOut=$(python3 ${imClientPath} -a $PWD/${pipeAuth} create ${templatePath} -r https://im.egi.eu/im)
# Remove pipe
rm -f $PWD/${pipeAuth} &> /dev/null
# Print IM output on stderr or stdout
Expand Down Expand Up @@ -921,7 +907,7 @@ async function deployInfraConfiguration(

if (deployInfo.deploymentType !== 'EC2') {
// Create select image command
const cmdImageNames = selectImage(deployInfo);
const cmdImageNames = await selectImage(deployInfo);

try {
// Execute the deployment command
Expand Down Expand Up @@ -1106,7 +1092,7 @@ async function deployFinalRecipe(
const mergedYamlContent = jsyaml.dump(mergedTemplate);

// Create deploy command
const cmdDeploy = deployIMCommand(deployInfo, mergedYamlContent);
const cmdDeploy = await deployIMCommand(deployInfo, mergedYamlContent);

// Show loading spinner
dialogBody.innerHTML =
Expand Down
19 changes: 13 additions & 6 deletions src/listDeployments.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { KernelManager } from '@jupyterlab/services';
import { Dialog } from '@jupyterlab/apputils';
import { Widget } from '@lumino/widgets';
import { getIMClientPath } from './utils';

interface IInfrastructure {
IMuser: string;
Expand Down Expand Up @@ -113,7 +114,7 @@ async function populateTable(table: HTMLTableElement): Promise<void> {
const stateCell = row.insertCell();

try {
const cmdState = infrastructureState(infrastructure);
const cmdState = await infrastructureState(infrastructure);
// Execute kernel to get output
const futureState = kernel.requestExecute({ code: cmdState });

Expand Down Expand Up @@ -149,7 +150,7 @@ async function populateTable(table: HTMLTableElement): Promise<void> {
}

try {
const cmdIP = infrastructureIP(infrastructure);
const cmdIP = await infrastructureIP(infrastructure);
// Execute kernel to get output
const futureIP = kernel.requestExecute({ code: cmdIP });

Expand Down Expand Up @@ -185,7 +186,9 @@ async function populateTable(table: HTMLTableElement): Promise<void> {
await kernel.shutdown();
}

function infrastructureState(infrastructure: IInfrastructure): string {
async function infrastructureState(
infrastructure: IInfrastructure
): Promise<string> {
const {
IMuser,
IMpass,
Expand All @@ -202,6 +205,7 @@ function infrastructureState(infrastructure: IInfrastructure): string {
} = infrastructure;

const pipeAuth = 'auth-pipe';
const imClientPath = await getIMClientPath();

let authContent = `id=im; type=InfrastructureManager; username=${IMuser}; password=${IMpass};\n`;
authContent += `id=${id}; type=${type}; host=${host};`;
Expand Down Expand Up @@ -232,7 +236,7 @@ function infrastructureState(infrastructure: IInfrastructure): string {
# Command to create the infrastructure manager client credentials
echo -e "${authContent}" > $PWD/${pipeAuth} &
stateOut=$(python3 /usr/local/bin/im_client.py getstate ${infrastructureID} -r https://im.egi.eu/im -a $PWD/${pipeAuth})
stateOut=$(python3 ${imClientPath} getstate ${infrastructureID} -r https://im.egi.eu/im -a $PWD/${pipeAuth})
# Remove pipe
rm -f $PWD/${pipeAuth} &> /dev/null
# Print state output on stderr or stdout
Expand All @@ -248,7 +252,9 @@ function infrastructureState(infrastructure: IInfrastructure): string {
return cmd;
}

function infrastructureIP(infrastructure: IInfrastructure): string {
async function infrastructureIP(
infrastructure: IInfrastructure
): Promise<string> {
const {
IMuser,
IMpass,
Expand All @@ -265,6 +271,7 @@ function infrastructureIP(infrastructure: IInfrastructure): string {
} = infrastructure;

const pipeAuth = 'auth-pipe';
const imClientPath = await getIMClientPath();

let authContent = `id=im; type=InfrastructureManager; username=${IMuser}; password=${IMpass};\n`;
authContent += `id=${id}; type=${type}; host=${host};`;
Expand Down Expand Up @@ -295,7 +302,7 @@ function infrastructureIP(infrastructure: IInfrastructure): string {
# Command to create the infrastructure manager client credentials
echo -e "${authContent}" > $PWD/${pipeAuth} &
stateOut=$(python3 /usr/local/bin/im_client.py getvminfo ${infrastructureID} 0 net_interface.1.ip -r https://im.egi.eu/im -a $PWD/${pipeAuth})
stateOut=$(python3 ${imClientPath} getvminfo ${infrastructureID} 0 net_interface.1.ip -r https://im.egi.eu/im -a $PWD/${pipeAuth})
# Remove pipe
rm -f $PWD/${pipeAuth} &> /dev/null
# Print state output on stderr or stdout
Expand Down
39 changes: 39 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { KernelManager } from '@jupyterlab/services';

export async function executeKernelCommand(
command: string,
callback: (output: string) => void
): Promise<void> {
try {
const kernelManager = new KernelManager();
const kernel = await kernelManager.startNew();
const future = kernel.requestExecute({ code: command });

future.onIOPub = msg => {
const content = msg.content as any;
const outputText =
content.text || (content.data && content.data['text/plain']);
callback(outputText);
};
} catch (error) {
console.error('Error executing kernel command:', error);
}
}

export async function getIMClientPath(): Promise<string> {
return new Promise((resolve, reject) => {
const cmdIMClientPath = '%%bash\n' + 'which im_client.py';

executeKernelCommand(cmdIMClientPath, output => {
if (output.trim()) {
resolve(output.trim());
} else {
reject(
new Error(
'Failed to find im_client.py path. Maybe IM-client is not installed.'
)
);
}
}).catch(reject);
});
}

0 comments on commit cdb438c

Please sign in to comment.