Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
NichArchA82 committed Jan 8, 2025
1 parent b85772a commit cfa78fd
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 90 deletions.
119 changes: 43 additions & 76 deletions command-handler/src/commands/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down
87 changes: 73 additions & 14 deletions command-handler/src/util/libvirt/libvirt-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
Expand Down Expand Up @@ -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)

Expand All @@ -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}`
}
Expand Down Expand Up @@ -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({
Expand All @@ -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);

Expand All @@ -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}`
});
Expand All @@ -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}`
Expand All @@ -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}`
Expand All @@ -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
Expand Down Expand Up @@ -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' });
Expand All @@ -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
});
}
};

0 comments on commit cfa78fd

Please sign in to comment.