From cfa78fdf1115bf1503f3c0dbbb30e2c3b45f6fb2 Mon Sep 17 00:00:00 2001 From: Nicholas Archuletta Date: Wed, 8 Jan 2025 05:12:55 +0000 Subject: [PATCH 01/12] wip --- command-handler/src/commands/vm.js | 119 +++++++----------- .../src/util/libvirt/libvirt-server.js | 87 ++++++++++--- 2 files changed, 116 insertions(+), 90 deletions(-) diff --git a/command-handler/src/commands/vm.js b/command-handler/src/commands/vm.js index f222be2..1499641 100644 --- a/command-handler/src/commands/vm.js +++ b/command-handler/src/commands/vm.js @@ -52,97 +52,64 @@ export default { } } else if (actionId === 'button_create_vm') { const buttonsArray = [ - { text: "aws", actionId: "button_create_vm_aws" }, - { text: "hetzner", actionId: "button_create_vm_hetzner" }, - { text: "libvirt", actionId: "button_create_vm_libvirt" }, + { text: "aws", actionId: "button_create_vm_aws" }, + { text: "hetzner", actionId: "button_create_vm_hetzner" }, + { text: "libvirt", actionId: "button_create_vm_libvirt" }, ]; const buttons = buttonBuilder({ buttonsArray, headerText: "choose your platform", fallbackText: "device unsupported" }); app.client.chat.postEphemeral({ - channel: `${body.channel.id}`, - user: `${body.user.id}`, - text: 'select a platform', - ...buttons + channel: `${body.channel.id}`, + user: `${body.user.id}`, + text: 'select a platform', + ...buttons }); } else if (actionId === 'button_start_aws') { - const { instanceId, region } = JSON.parse(body.actions[0].value); - - aws.startServer({ app, body, instanceId, region }); + aws.startServer({ app, body, instanceId: data.instanceId, region: data.region }); } else if (actionId === 'button_stop_aws') { - const { instanceId, region } = JSON.parse(body.actions[0].value); - - aws.stopServer({ app, body, instanceId, region }); + aws.stopServer({ app, body, instanceId: data.instanceId, region: data.region }); } else if (actionId === 'button_delete_aws') { - const { instanceId, serverName, region } = JSON.parse(body.actions[0].value); - - //delete the server - aws.deleteServer({ app, body, instanceId, serverName, region }); - } else if (actionId === 'button_start_hetzner') { - const { vmID } = JSON.parse(body.actions[0].value); - - //start a hetzner server - hetzner.startServer({ app, body, vmID }); - } else if (actionId === 'button_stop_hetzner') { - const { vmID } = JSON.parse(body.actions[0].value); - - //stop a hetzner server - hetzner.stopServer({ app, body, vmID }); - } else if (actionId === 'button_delete_hetzner') { - const { serverName } = JSON.parse(body.actions[0].value); - - //delete the server - hetzner.deleteServer({ app, body, serverName }); - } else if (actionId === 'button_start_libvirt') { - const { serverName } = JSON.parse(body.actions[0].value); - - libvirt.startServer({ app, body, serverName }); - } else if (actionId === 'button_stop_libvirt') { - const { serverName } = JSON.parse(body.actions[0].value); - - libvirt.stopServer({ app, body, serverName }); - } else if (actionId === 'button_delete_libvirt') { - const { serverName } = JSON.parse(body.actions[0].value); - - //delete the server - libvirt.deleteServer({ app, body, serverName }); - } else if (actionId.startsWith('button_create_image_aws')) { - const { imageName, ami, region, instanceType } = JSON.parse(body.actions[0].value); - aws.createServer({ app, body, imageName, ami, region, instanceType }); - } else if (actionId.startsWith('button_create_image_hetzner')) { - const { imageID, imageName, region, serverType } = JSON.parse(body.actions[0].value); - hetzner.createServer({ app, body, imageID, imageName, region, serverType }); - } else if (actionId.startsWith('button_create_image_libvirt')) { - const { imageName } = JSON.parse(body.actions[0].value); - libvirt.createServer({ app, body, imageName }); - } else if (actionId === 'button_create_vm_hetzner') { - //select the hetzner server to create before calling the create server + aws.deleteServer({ app, body, instanceId: data.instanceId, serverName: data.serverName, region: data.region }); + } else if (actionId === 'button_start_hetzner') { + hetzner.startServer({ app, body, vmID: data.vmID }); + } else if (actionId === 'button_stop_hetzner') { + hetzner.stopServer({ app, body, vmID: data.vmID }); + } else if (actionId === 'button_delete_hetzner') { + hetzner.deleteServer({ app, body, serverName: data.serverName }); + } else if (actionId === 'button_start_libvirt') { + libvirt.startServer({ app, body, serverName: data.serverName, region: data.region }); + } else if (actionId === 'button_stop_libvirt') { + libvirt.stopServer({ app, body, serverName: data.serverName, region: data.region }); + } else if (actionId === 'button_delete_libvirt') { + libvirt.deleteServer({ app, body, serverName: data.serverName, region: data.region }); + } else if (actionId.startsWith('button_create_image_aws')) { + aws.createServer({ app, body, imageName: data.imageName, ami: data.ami, region: data.region, instanceType: data.instanceType }); + } else if (actionId.startsWith('button_create_image_hetzner')) { + hetzner.createServer({ app, body, imageID: data.imageID, imageName: data.imageName, region: data.region, serverType: data.serverType }); + } else if (actionId.startsWith('button_create_image_libvirt')) { + libvirt.createServer({ app, body, imageName: data.imageName, region: data.region, instanceType: data.instanceType }); + } else if (actionId === 'button_create_vm_hetzner') { hetzner.selectRegion({ app, body }); - } else if (actionId === 'button_create_vm_aws') { - //select the aws server to create before calling the create server + } else if (actionId === 'button_create_vm_aws') { aws.selectRegion({ app, body }); - } else if (actionId === 'button_create_vm_libvirt') { - //select the libvirt image to create before calling the create server - libvirt.selectImage({ app, body }); - } else if (actionId.startsWith('button_select_hetzner_server')) { - const data = JSON.parse(body.actions[0].value); - //select the hetzner server to create before calling the create server + } else if (actionId === 'button_create_vm_libvirt') { + libvirt.selectRegion({ app, body }); + } else if (actionId.startsWith('button_select_hetzner_server')) { hetzner.selectServer({ app, body, data }); - } else if (actionId.startsWith('button_select_aws_server')) { - const data = JSON.parse(body.actions[0].value); - //select the asw server to create before calling the create server + } else if (actionId.startsWith('button_select_libvirt_server')) { + libvirt.selectServer({ app, body, data }); + } else if (actionId.startsWith('button_select_aws_server')) { aws.selectServer({ app, body, data }); - } else if (actionId.startsWith('button_select_hetzner_image')) { - const data = JSON.parse(body.actions[0].value); - //select the hetzner server to create before calling the create server + } else if (actionId.startsWith('button_select_hetzner_image')) { hetzner.selectImage({ app, body, data }); - } else if (actionId.startsWith('button_select_aws_image')) { - const data = JSON.parse(body.actions[0].value); - //select the asw server to create before calling the create server + } else if (actionId.startsWith('button_select_libvirt_image')) { + libvirt.selectImage({ app, body, data }); + } else if (actionId.startsWith('button_select_aws_image')) { aws.selectImage({ app, body, data }); - } else { + } else { response({ - text: `This button is registered with the vm command, but does not have an action associated with it.` + text: `This button is registered with the vm command, but does not have an action associated with it.` }); - } + } }, run: async ({ event, app }) => { diff --git a/command-handler/src/util/libvirt/libvirt-server.js b/command-handler/src/util/libvirt/libvirt-server.js index 7ea2d70..c1be8f5 100644 --- a/command-handler/src/util/libvirt/libvirt-server.js +++ b/command-handler/src/util/libvirt/libvirt-server.js @@ -56,9 +56,7 @@ export default { { "vm_name": serverName, "tags": { - "owner": { - "name": userEmail - } + "owner": userEmail, }, "user_data": Buffer.from(configUserData(serverName)).toString('base64'), "image": imageName @@ -67,7 +65,7 @@ export default { 'Authorization': `${process.env.PROVISIONER_API_TOKEN}`, 'Content-Type': 'application/json' }, - timeout: 1000 * 60 * 2 + timeout: 1000 * 60 * 5 }); } catch (error) { log.error('There was an error creating the server', axiosError(error)); @@ -133,7 +131,7 @@ export default { }); }, - deleteServer: async ({ app, body, serverName }) => { + deleteServer: async ({ app, body, serverName, region }) => { //get servers from tailscale const { deviceId } = await getDevices(serverName) @@ -151,7 +149,10 @@ export default { try { await axios.delete(`${process.env.PROVISIONER_URL}/v1/delete`, { - data: { "vm_name": serverName }, + data: { + "vm_name": serverName, + "region_name": region + }, headers: { 'Authorization': `${process.env.PROVISIONER_API_TOKEN}` } @@ -194,7 +195,7 @@ export default { log.error('Failed to get servers from libvirt', axiosError(error)); }); - const data = response.data; + const data = response?.data; if (!data) { app.client.chat.postEphemeral({ @@ -208,7 +209,7 @@ export default { for (const server of data) { - const owner = server.description.owner.name; + const owner = server.description.owner; const { deviceIP } = await getDevices(server.name); @@ -217,6 +218,7 @@ export default { servers.push({ cloud: "libvirt", serverName: `${server.name}`, + region: `${server.region_name}`, status: `${server.state}`, connect: `https://login.tailscale.com/admin/machines/${deviceIP}` }); @@ -226,10 +228,11 @@ export default { return servers; }, - startServer: async({ app, body, serverName }) => { + startServer: async({ app, body, serverName, region }) => { try { await axios.post(`${process.env.PROVISIONER_URL}/v1/start`, { - "vm_name": serverName + "vm_name": serverName, + "region_name": region }, { headers: { 'Authorization': `${process.env.PROVISIONER_API_TOKEN}` @@ -252,10 +255,11 @@ export default { } }, - stopServer: async({ app, body, serverName }) => { + stopServer: async({ app, body, serverName, region }) => { try { await axios.post(`${process.env.PROVISIONER_URL}/v1/stop`, { - "vm_name": serverName + "vm_name": serverName, + "region_name": region }, { headers: { 'Authorization': `${process.env.PROVISIONER_API_TOKEN}` @@ -278,7 +282,44 @@ export default { } }, - selectImage: async({ app, body }) => { + selectRegion: async ({app, body }) => { + const buttonsArray = []; + //get the regions from the env variable + const regions = await axios.get(`${process.env.PROVISIONER_URL}/v1/regions`, { + headers: { + 'Authorization': `${process.env.PROVISIONER_API_TOKEN}` + }, + timeout: 1000 * 60 * 5 + }) + .catch(error => { + log.error('Failed to get regions from libvirt', axiosError(error)); + }); + + //return if it fails to get a response from libvirt. + if (!regions) { + app.client.chat.postEphemeral({ + channel: `${body.channel.id}`, + user: `${body.user.id}`, + text: `Failed to get region data from libvirt` + }); + + return; + } + + //build button for user to select + for (const region of regions) { + buttonsArray.push({ text: region.region_name, actionId: `button_select_libvirt_image_${region.region_name}`, value: JSON.stringify({ region: region.region_name, instances: region.available_instance_types }) }); + } + const buttons = buttonBuilder({ buttonsArray, headerText: 'Select a region', fallbackText: 'unsupported device' }); + app.client.chat.postEphemeral({ + channel: `${body.channel.id}`, + user: `${body.user.id}`, + text: 'select a region', + ...buttons + }); + }, + + selectImage: async({ app, body, data }) => { //get the libvirt images // Fetch tags from the repository @@ -306,7 +347,8 @@ export default { //build button for user to select for (const image of images) { - buttonsArray.push({ text: image, actionId: `button_create_image_libvirt_${image}`, value: JSON.stringify({ imageName: image }) }) + data.image = image; + buttonsArray.push({ text: image, actionId: `button_create_image_libvirt_${image}`, value: JSON.stringify(data) }) } const buttons = buttonBuilder({ buttonsArray, headerText: 'Select an image', fallbackText: 'unsupported device' }); @@ -317,4 +359,21 @@ export default { ...buttons }); }, + + selectServer: async ({app, body, data }) => { + const buttonsArray = []; + const serverTypes = process.env.HETZNER_SERVER_TYPES.split(',').map(server => server.trim()).filter(server => server); + + for (const serverType of serverTypes) { + data.serverType = serverType; + buttonsArray.push({ text: serverType, actionId: `button_select_hetzner_image_${serverType}`, value: JSON.stringify(data) }); + }; + const buttons = buttonBuilder({ buttonsArray, headerText: 'Select a server', fallbackText: 'unsupported device' }); + app.client.chat.postEphemeral({ + channel: `${body.channel.id}`, + user: `${body.user.id}`, + text: 'select a server', + ...buttons + }); + } }; \ No newline at end of file From c6839ed1cb10a3dd804b9690034dd78ca5cbcc09 Mon Sep 17 00:00:00 2001 From: Nicholas Archuletta Date: Wed, 8 Jan 2025 05:45:18 +0000 Subject: [PATCH 02/12] wip --- command-handler/src/commands/vm.js | 119 ++++++++++++++++++----------- 1 file changed, 76 insertions(+), 43 deletions(-) diff --git a/command-handler/src/commands/vm.js b/command-handler/src/commands/vm.js index 1499641..38b9690 100644 --- a/command-handler/src/commands/vm.js +++ b/command-handler/src/commands/vm.js @@ -52,64 +52,97 @@ export default { } } else if (actionId === 'button_create_vm') { const buttonsArray = [ - { text: "aws", actionId: "button_create_vm_aws" }, - { text: "hetzner", actionId: "button_create_vm_hetzner" }, - { text: "libvirt", actionId: "button_create_vm_libvirt" }, + { text: "aws", actionId: "button_create_vm_aws" }, + { text: "hetzner", actionId: "button_create_vm_hetzner" }, + { text: "libvirt", actionId: "button_create_vm_libvirt" }, ]; const buttons = buttonBuilder({ buttonsArray, headerText: "choose your platform", fallbackText: "device unsupported" }); app.client.chat.postEphemeral({ - channel: `${body.channel.id}`, - user: `${body.user.id}`, - text: 'select a platform', - ...buttons + channel: `${body.channel.id}`, + user: `${body.user.id}`, + text: 'select a platform', + ...buttons }); } else if (actionId === 'button_start_aws') { - aws.startServer({ app, body, instanceId: data.instanceId, region: data.region }); + const { instanceId, region } = JSON.parse(body.actions[0].value); + + aws.startServer({ app, body, instanceId, region }); } else if (actionId === 'button_stop_aws') { - aws.stopServer({ app, body, instanceId: data.instanceId, region: data.region }); + const { instanceId, region } = JSON.parse(body.actions[0].value); + + aws.stopServer({ app, body, instanceId, region }); } else if (actionId === 'button_delete_aws') { - aws.deleteServer({ app, body, instanceId: data.instanceId, serverName: data.serverName, region: data.region }); - } else if (actionId === 'button_start_hetzner') { - hetzner.startServer({ app, body, vmID: data.vmID }); - } else if (actionId === 'button_stop_hetzner') { - hetzner.stopServer({ app, body, vmID: data.vmID }); - } else if (actionId === 'button_delete_hetzner') { - hetzner.deleteServer({ app, body, serverName: data.serverName }); - } else if (actionId === 'button_start_libvirt') { - libvirt.startServer({ app, body, serverName: data.serverName, region: data.region }); - } else if (actionId === 'button_stop_libvirt') { - libvirt.stopServer({ app, body, serverName: data.serverName, region: data.region }); - } else if (actionId === 'button_delete_libvirt') { - libvirt.deleteServer({ app, body, serverName: data.serverName, region: data.region }); - } else if (actionId.startsWith('button_create_image_aws')) { - aws.createServer({ app, body, imageName: data.imageName, ami: data.ami, region: data.region, instanceType: data.instanceType }); - } else if (actionId.startsWith('button_create_image_hetzner')) { - hetzner.createServer({ app, body, imageID: data.imageID, imageName: data.imageName, region: data.region, serverType: data.serverType }); - } else if (actionId.startsWith('button_create_image_libvirt')) { - libvirt.createServer({ app, body, imageName: data.imageName, region: data.region, instanceType: data.instanceType }); - } else if (actionId === 'button_create_vm_hetzner') { + const { instanceId, serverName, region } = JSON.parse(body.actions[0].value); + + //delete the server + aws.deleteServer({ app, body, instanceId, serverName, region }); + } else if (actionId === 'button_start_hetzner') { + const { vmID } = JSON.parse(body.actions[0].value); + + //start a hetzner server + hetzner.startServer({ app, body, vmID }); + } else if (actionId === 'button_stop_hetzner') { + const { vmID } = JSON.parse(body.actions[0].value); + + //stop a hetzner server + hetzner.stopServer({ app, body, vmID }); + } else if (actionId === 'button_delete_hetzner') { + const { serverName } = JSON.parse(body.actions[0].value); + + //delete the server + hetzner.deleteServer({ app, body, serverName }); + } else if (actionId === 'button_start_libvirt') { + const { serverName, region } = JSON.parse(body.actions[0].value); + + libvirt.startServer({ app, body, serverName, region }); + } else if (actionId === 'button_stop_libvirt') { + const { serverName, region } = JSON.parse(body.actions[0].value); + + libvirt.stopServer({ app, body, serverName, region }); + } else if (actionId === 'button_delete_libvirt') { + const { serverName, region } = JSON.parse(body.actions[0].value); + + //delete the server + libvirt.deleteServer({ app, body, serverName, region }); + } else if (actionId.startsWith('button_create_image_aws')) { + const { imageName, ami, region, instanceType } = JSON.parse(body.actions[0].value); + aws.createServer({ app, body, imageName, ami, region, instanceType }); + } else if (actionId.startsWith('button_create_image_hetzner')) { + const { imageID, imageName, region, serverType } = JSON.parse(body.actions[0].value); + hetzner.createServer({ app, body, imageID, imageName, region, serverType }); + } else if (actionId.startsWith('button_create_image_libvirt')) { + const { imageName } = JSON.parse(body.actions[0].value); + libvirt.createServer({ app, body, imageName }); + } else if (actionId === 'button_create_vm_hetzner') { + //select the hetzner server to create before calling the create server hetzner.selectRegion({ app, body }); - } else if (actionId === 'button_create_vm_aws') { + } else if (actionId === 'button_create_vm_aws') { + //select the aws server to create before calling the create server aws.selectRegion({ app, body }); - } else if (actionId === 'button_create_vm_libvirt') { - libvirt.selectRegion({ app, body }); - } else if (actionId.startsWith('button_select_hetzner_server')) { + } else if (actionId === 'button_create_vm_libvirt') { + //select the libvirt image to create before calling the create server + libvirt.selectImage({ app, body }); + } else if (actionId.startsWith('button_select_hetzner_server')) { + const data = JSON.parse(body.actions[0].value); + //select the hetzner server to create before calling the create server hetzner.selectServer({ app, body, data }); - } else if (actionId.startsWith('button_select_libvirt_server')) { - libvirt.selectServer({ app, body, data }); - } else if (actionId.startsWith('button_select_aws_server')) { + } else if (actionId.startsWith('button_select_aws_server')) { + const data = JSON.parse(body.actions[0].value); + //select the asw server to create before calling the create server aws.selectServer({ app, body, data }); - } else if (actionId.startsWith('button_select_hetzner_image')) { + } else if (actionId.startsWith('button_select_hetzner_image')) { + const data = JSON.parse(body.actions[0].value); + //select the hetzner server to create before calling the create server hetzner.selectImage({ app, body, data }); - } else if (actionId.startsWith('button_select_libvirt_image')) { - libvirt.selectImage({ app, body, data }); - } else if (actionId.startsWith('button_select_aws_image')) { + } else if (actionId.startsWith('button_select_aws_image')) { + const data = JSON.parse(body.actions[0].value); + //select the asw server to create before calling the create server aws.selectImage({ app, body, data }); - } else { + } else { response({ - text: `This button is registered with the vm command, but does not have an action associated with it.` + text: `This button is registered with the vm command, but does not have an action associated with it.` }); - } + } }, run: async ({ event, app }) => { From c16441c0e34ba5ba364073fe331f6399c8ec3f0e Mon Sep 17 00:00:00 2001 From: Nicholas Archuletta Date: Wed, 8 Jan 2025 06:04:08 +0000 Subject: [PATCH 03/12] fix: tags destructure --- command-handler/src/util/libvirt/libvirt-server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command-handler/src/util/libvirt/libvirt-server.js b/command-handler/src/util/libvirt/libvirt-server.js index c1be8f5..cdf3478 100644 --- a/command-handler/src/util/libvirt/libvirt-server.js +++ b/command-handler/src/util/libvirt/libvirt-server.js @@ -209,7 +209,7 @@ export default { for (const server of data) { - const owner = server.description.owner; + const owner = server.tags.owner; const { deviceIP } = await getDevices(server.name); From 3f0848eff48aaee16f0a3867b2ca2c165d5cd5f5 Mon Sep 17 00:00:00 2001 From: Nicholas Archuletta Date: Wed, 8 Jan 2025 08:09:07 +0000 Subject: [PATCH 04/12] fix: add create server --- command-handler/src/commands/vm.js | 14 +++++++++++--- command-handler/src/util/libvirt/libvirt-server.js | 8 +++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/command-handler/src/commands/vm.js b/command-handler/src/commands/vm.js index 38b9690..e18d5c6 100644 --- a/command-handler/src/commands/vm.js +++ b/command-handler/src/commands/vm.js @@ -111,8 +111,8 @@ export default { const { imageID, imageName, region, serverType } = JSON.parse(body.actions[0].value); hetzner.createServer({ app, body, imageID, imageName, region, serverType }); } else if (actionId.startsWith('button_create_image_libvirt')) { - const { imageName } = JSON.parse(body.actions[0].value); - libvirt.createServer({ app, body, imageName }); + const { imageName, instanceType } = JSON.parse(body.actions[0].value); + libvirt.createServer({ app, body, imageName, instanceType }); } else if (actionId === 'button_create_vm_hetzner') { //select the hetzner server to create before calling the create server hetzner.selectRegion({ app, body }); @@ -121,7 +121,7 @@ export default { aws.selectRegion({ app, body }); } else if (actionId === 'button_create_vm_libvirt') { //select the libvirt image to create before calling the create server - libvirt.selectImage({ app, body }); + libvirt.selectRegion({ app, body }); } else if (actionId.startsWith('button_select_hetzner_server')) { const data = JSON.parse(body.actions[0].value); //select the hetzner server to create before calling the create server @@ -130,6 +130,10 @@ export default { const data = JSON.parse(body.actions[0].value); //select the asw server to create before calling the create server aws.selectServer({ app, body, data }); + } else if (actionId.startsWith('button_select_libvirt_server')) { + const data = JSON.parse(body.actions[0].value); + //select the libvirt server type to create before calling the create server + libvirt.selectServer({ app, body, data }); } else if (actionId.startsWith('button_select_hetzner_image')) { const data = JSON.parse(body.actions[0].value); //select the hetzner server to create before calling the create server @@ -138,6 +142,10 @@ export default { const data = JSON.parse(body.actions[0].value); //select the asw server to create before calling the create server aws.selectImage({ app, body, data }); + } else if (actionId.startsWith('button_select_libvirt_image')) { + const data = JSON.parse(body.actions[0].value); + //select the hetzner server to create before calling the create server + libvirt.selectImage({ app, body, data }); } else { response({ text: `This button is registered with the vm command, but does not have an action associated with it.` diff --git a/command-handler/src/util/libvirt/libvirt-server.js b/command-handler/src/util/libvirt/libvirt-server.js index cdf3478..a54be56 100644 --- a/command-handler/src/util/libvirt/libvirt-server.js +++ b/command-handler/src/util/libvirt/libvirt-server.js @@ -308,7 +308,7 @@ export default { //build button for user to select for (const region of regions) { - buttonsArray.push({ text: region.region_name, actionId: `button_select_libvirt_image_${region.region_name}`, value: JSON.stringify({ region: region.region_name, instances: region.available_instance_types }) }); + buttonsArray.push({ text: region.region_name, actionId: `button_select_libvirt_server${region.region_name}`, value: JSON.stringify({ region: region.region_name, instances: region.available_instance_types }) }); } const buttons = buttonBuilder({ buttonsArray, headerText: 'Select a region', fallbackText: 'unsupported device' }); app.client.chat.postEphemeral({ @@ -362,11 +362,9 @@ export default { selectServer: async ({app, body, data }) => { const buttonsArray = []; - const serverTypes = process.env.HETZNER_SERVER_TYPES.split(',').map(server => server.trim()).filter(server => server); - for (const serverType of serverTypes) { - data.serverType = serverType; - buttonsArray.push({ text: serverType, actionId: `button_select_hetzner_image_${serverType}`, value: JSON.stringify(data) }); + for (const serverType of data.instances) { + buttonsArray.push({ text: serverType, actionId: `button_select_libvirt_image_${serverType}`, value: JSON.stringify(data) }); }; const buttons = buttonBuilder({ buttonsArray, headerText: 'Select a server', fallbackText: 'unsupported device' }); app.client.chat.postEphemeral({ From bb91ec83cb97d15a73fe488503fe7c8078bf6f9e Mon Sep 17 00:00:00 2001 From: Nicholas Archuletta Date: Wed, 8 Jan 2025 08:15:19 +0000 Subject: [PATCH 05/12] fix: axios data response --- command-handler/src/util/libvirt/libvirt-server.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/command-handler/src/util/libvirt/libvirt-server.js b/command-handler/src/util/libvirt/libvirt-server.js index a54be56..7dd24c5 100644 --- a/command-handler/src/util/libvirt/libvirt-server.js +++ b/command-handler/src/util/libvirt/libvirt-server.js @@ -285,7 +285,7 @@ export default { selectRegion: async ({app, body }) => { const buttonsArray = []; //get the regions from the env variable - const regions = await axios.get(`${process.env.PROVISIONER_URL}/v1/regions`, { + const response = await axios.get(`${process.env.PROVISIONER_URL}/v1/regions`, { headers: { 'Authorization': `${process.env.PROVISIONER_API_TOKEN}` }, @@ -294,6 +294,8 @@ export default { .catch(error => { log.error('Failed to get regions from libvirt', axiosError(error)); }); + + const regions = response?.data; //return if it fails to get a response from libvirt. if (!regions) { From 51203fbdd65cbb21f6ad22c197c553b9b0c5b2fa Mon Sep 17 00:00:00 2001 From: Nicholas Archuletta Date: Wed, 8 Jan 2025 08:29:11 +0000 Subject: [PATCH 06/12] console log --- command-handler/src/util/libvirt/libvirt-server.js | 1 + 1 file changed, 1 insertion(+) diff --git a/command-handler/src/util/libvirt/libvirt-server.js b/command-handler/src/util/libvirt/libvirt-server.js index 7dd24c5..514fd0a 100644 --- a/command-handler/src/util/libvirt/libvirt-server.js +++ b/command-handler/src/util/libvirt/libvirt-server.js @@ -365,6 +365,7 @@ export default { selectServer: async ({app, body, data }) => { const buttonsArray = []; + console.log(data); for (const serverType of data.instances) { buttonsArray.push({ text: serverType, actionId: `button_select_libvirt_image_${serverType}`, value: JSON.stringify(data) }); }; From 7edfb881670b7128ab3673f21cdb3be7a24c1c3b Mon Sep 17 00:00:00 2001 From: Nicholas Archuletta Date: Wed, 8 Jan 2025 08:40:57 +0000 Subject: [PATCH 07/12] fix: destructure instance type --- command-handler/src/util/libvirt/libvirt-server.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/command-handler/src/util/libvirt/libvirt-server.js b/command-handler/src/util/libvirt/libvirt-server.js index 514fd0a..bd62f38 100644 --- a/command-handler/src/util/libvirt/libvirt-server.js +++ b/command-handler/src/util/libvirt/libvirt-server.js @@ -365,9 +365,8 @@ export default { selectServer: async ({app, body, data }) => { const buttonsArray = []; - console.log(data); for (const serverType of data.instances) { - buttonsArray.push({ text: serverType, actionId: `button_select_libvirt_image_${serverType}`, value: JSON.stringify(data) }); + buttonsArray.push({ text: serverType.instance_type, actionId: `button_select_libvirt_image_${serverType.instance_type}`, value: JSON.stringify(data) }); }; const buttons = buttonBuilder({ buttonsArray, headerText: 'Select a server', fallbackText: 'unsupported device' }); app.client.chat.postEphemeral({ From 4a3e057f45c7cf69f7707dee952c9d7712fcb7b4 Mon Sep 17 00:00:00 2001 From: Nicholas Archuletta Date: Wed, 8 Jan 2025 08:44:54 +0000 Subject: [PATCH 08/12] fix: value data --- command-handler/src/util/libvirt/libvirt-server.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/command-handler/src/util/libvirt/libvirt-server.js b/command-handler/src/util/libvirt/libvirt-server.js index bd62f38..5f9d820 100644 --- a/command-handler/src/util/libvirt/libvirt-server.js +++ b/command-handler/src/util/libvirt/libvirt-server.js @@ -366,7 +366,8 @@ export default { const buttonsArray = []; for (const serverType of data.instances) { - buttonsArray.push({ text: serverType.instance_type, actionId: `button_select_libvirt_image_${serverType.instance_type}`, value: JSON.stringify(data) }); + data.serverType = serverType.instance_type; + buttonsArray.push({ text: serverType.instance_type, actionId: `button_select_libvirt_image_${serverType.instance_type}`, value: JSON.stringify(data) }); }; const buttons = buttonBuilder({ buttonsArray, headerText: 'Select a server', fallbackText: 'unsupported device' }); app.client.chat.postEphemeral({ From 3f55f9fdedd42807fc18c1188645d54e10bcbde7 Mon Sep 17 00:00:00 2001 From: Nicholas Archuletta Date: Wed, 8 Jan 2025 09:11:04 +0000 Subject: [PATCH 09/12] fix: create server --- command-handler/src/commands/vm.js | 2 +- command-handler/src/util/libvirt/libvirt-server.js | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/command-handler/src/commands/vm.js b/command-handler/src/commands/vm.js index e18d5c6..95d0b19 100644 --- a/command-handler/src/commands/vm.js +++ b/command-handler/src/commands/vm.js @@ -112,7 +112,7 @@ export default { hetzner.createServer({ app, body, imageID, imageName, region, serverType }); } else if (actionId.startsWith('button_create_image_libvirt')) { const { imageName, instanceType } = JSON.parse(body.actions[0].value); - libvirt.createServer({ app, body, imageName, instanceType }); + libvirt.createServer({ app, body, imageName, region, instanceType }); } else if (actionId === 'button_create_vm_hetzner') { //select the hetzner server to create before calling the create server hetzner.selectRegion({ app, body }); diff --git a/command-handler/src/util/libvirt/libvirt-server.js b/command-handler/src/util/libvirt/libvirt-server.js index 5f9d820..b9c2d3a 100644 --- a/command-handler/src/util/libvirt/libvirt-server.js +++ b/command-handler/src/util/libvirt/libvirt-server.js @@ -16,7 +16,7 @@ const delay = (ms) => { } export default { - createServer: async({ app, body, imageName }) => { + createServer: async({ app, body, imageName, region, instanceType }) => { //auto generate the name const serverName = uniqueNamesGenerator({ dictionaries: [ colors, animals ], @@ -59,7 +59,9 @@ export default { "owner": userEmail, }, "user_data": Buffer.from(configUserData(serverName)).toString('base64'), - "image": imageName + "image": imageName, + "region_name": region, + "instance_type": instanceType }, { headers: { 'Authorization': `${process.env.PROVISIONER_API_TOKEN}`, @@ -366,7 +368,7 @@ export default { const buttonsArray = []; for (const serverType of data.instances) { - data.serverType = serverType.instance_type; + data.instanceType = serverType.instance_type; buttonsArray.push({ text: serverType.instance_type, actionId: `button_select_libvirt_image_${serverType.instance_type}`, value: JSON.stringify(data) }); }; const buttons = buttonBuilder({ buttonsArray, headerText: 'Select a server', fallbackText: 'unsupported device' }); From bfc534b802b11392e70daae319f794eff7bb9df7 Mon Sep 17 00:00:00 2001 From: Nicholas Archuletta Date: Wed, 8 Jan 2025 09:18:25 +0000 Subject: [PATCH 10/12] fix: region undefined --- command-handler/src/commands/vm.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command-handler/src/commands/vm.js b/command-handler/src/commands/vm.js index 95d0b19..0c1ea1e 100644 --- a/command-handler/src/commands/vm.js +++ b/command-handler/src/commands/vm.js @@ -111,7 +111,7 @@ export default { const { imageID, imageName, region, serverType } = JSON.parse(body.actions[0].value); hetzner.createServer({ app, body, imageID, imageName, region, serverType }); } else if (actionId.startsWith('button_create_image_libvirt')) { - const { imageName, instanceType } = JSON.parse(body.actions[0].value); + const { imageName, region, instanceType } = JSON.parse(body.actions[0].value); libvirt.createServer({ app, body, imageName, region, instanceType }); } else if (actionId === 'button_create_vm_hetzner') { //select the hetzner server to create before calling the create server From 1a95805ab3afe12111ca280936579e0ff861f2c1 Mon Sep 17 00:00:00 2001 From: Nicholas Archuletta Date: Wed, 8 Jan 2025 09:23:53 +0000 Subject: [PATCH 11/12] fix: image undefined --- command-handler/src/util/libvirt/libvirt-server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command-handler/src/util/libvirt/libvirt-server.js b/command-handler/src/util/libvirt/libvirt-server.js index b9c2d3a..7c49bbb 100644 --- a/command-handler/src/util/libvirt/libvirt-server.js +++ b/command-handler/src/util/libvirt/libvirt-server.js @@ -351,7 +351,7 @@ export default { //build button for user to select for (const image of images) { - data.image = image; + data.imageName = image; buttonsArray.push({ text: image, actionId: `button_create_image_libvirt_${image}`, value: JSON.stringify(data) }) } From 3b7c806478312e3fd26b7f0ad4fe6099f954f78f Mon Sep 17 00:00:00 2001 From: Nicholas Archuletta <64152648+NichArchA82@users.noreply.github.com> Date: Wed, 8 Jan 2025 16:03:31 -0700 Subject: [PATCH 12/12] fix: remove unnecessary ok check on server creation --- command-handler/src/util/libvirt/libvirt-server.js | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/command-handler/src/util/libvirt/libvirt-server.js b/command-handler/src/util/libvirt/libvirt-server.js index 7c49bbb..a62e29e 100644 --- a/command-handler/src/util/libvirt/libvirt-server.js +++ b/command-handler/src/util/libvirt/libvirt-server.js @@ -81,16 +81,6 @@ export default { return; } - if (serverRes.data !== 'Success') { - app.client.chat.postEphemeral({ - channel: `${body.channel.id}`, - user: `${body.user.id}`, - text: `Failed to create a server.` - }); - - return; - } - let attempts; let maxRetries = 13; @@ -379,4 +369,4 @@ export default { ...buttons }); } -}; \ No newline at end of file +};