Skip to content

Commit

Permalink
Update 0.2 is almost here
Browse files Browse the repository at this point in the history
  • Loading branch information
ma4z-sys committed Jan 5, 2025
1 parent 26db691 commit 10cd6f0
Show file tree
Hide file tree
Showing 29 changed files with 441 additions and 245 deletions.
16 changes: 9 additions & 7 deletions routes/Instance/Archives.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ router.get("/instance/:id/archives", async (req, res) => {
return res.redirect('/instances');
}

if(instance.suspended === true) {
return res.redirect('../../instances?err=SUSPENDED');
}

const isAuthorized = await isUserAuthorizedForContainer(req.user.userId, instance.Id);
if (!isAuthorized) {
return res.status(403).send('Unauthorized access to this instance.');
Expand Down Expand Up @@ -109,10 +113,9 @@ router.post('/instance/:id/archives/create', async (req, res) => {
db.set(id + '_instance', instance);
}

if(instance.suspended === true) {
return res.redirect('../../instance/' + id + '/suspended');
if(instance.suspended === true) {
return res.redirect('../../instances?err=SUSPENDED');
}

const RequestData = {
method: 'post',
// url: `http://${instance.Node.address}:${instance.Node.port}/archive/${instance.ContainerId}/archives`,
Expand Down Expand Up @@ -160,10 +163,9 @@ router.post('/instance/:id/archives/delete/:archivename', async (req, res) => {
db.set(id + '_instance', instance);
}

if(instance.suspended === true) {
return res.redirect('../../instance/' + id + '/suspended');
if(instance.suspended === true) {
return res.redirect('../../instances?err=SUSPENDED');
}

const RequestData = {
method: 'post',
// url: `http://${instance.Node.address}:${instance.Node.port}/archive/${instance.ContainerId}/archives`,
Expand Down Expand Up @@ -214,7 +216,7 @@ router.post('/instance/:id/archives/rollback/:archivename', async (req, res) =>
}

if(instance.suspended === true) {
return res.redirect('../../instance/' + id + '/suspended');
return res.redirect('../../instances?err=SUSPENDED');
}

const RequestData = {
Expand Down
10 changes: 4 additions & 6 deletions routes/Instance/CreateFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ router.post("/instance/:id/files/create/:filename", async (req, res) => {
db.set(id + '_instance', instance);
}

if(instance.suspended === true) {
return res.redirect('../../instance/' + id + '/suspended');
if(instance.suspended === true) {
return res.redirect('../../instances?err=SUSPENDED');
}

if (!instance.Node || !instance.Node.address || !instance.Node.port) {
return res.status(500).send('Invalid instance node configuration');
}
Expand Down Expand Up @@ -73,10 +72,9 @@ router.get("/instance/:id/files/create", async (req, res) => {
db.set(id + '_instance', instance);
}

if(instance.suspended === true) {
return res.redirect('../../instance/' + id + '/suspended');
if(instance.suspended === true) {
return res.redirect('../../instances?err=SUSPENDED');
}

if (!instance || !instance.VolumeId) {
return res.redirect('../instances');
}
Expand Down
8 changes: 4 additions & 4 deletions routes/Instance/CreateFolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ router.get("/instance/:id/files/folder/create", async (req, res) => {
db.set(id + '_instance', instance);
}

if(instance.suspended === true) {
return res.redirect('../../instance/' + id + '/suspended');
if(instance.suspended === true) {
return res.redirect('../../instances?err=SUSPENDED');
}

if (!instance || !instance.VolumeId) {
Expand Down Expand Up @@ -77,8 +77,8 @@ router.post("/instance/:id/files/folder/create/:foldername", async (req, res) =>
db.set(id + '_instance', instance);
}

if(instance.suspended === true) {
return res.redirect('../../instance/' + id + '/suspended');
if(instance.suspended === true) {
return res.redirect('../../instances?err=SUSPENDED');
}

if (!instance.Node || !instance.Node.address || !instance.Node.port) {
Expand Down
4 changes: 2 additions & 2 deletions routes/Instance/DeleteFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ router.get("/instance/:id/files/delete/:filename", async (req, res) => {
}

if(instance.suspended === true) {
return res.redirect('../../instance/' + id + '/suspended');
}
return res.redirect('../../instances?err=SUSPENDED');
}

if (!instance.Node || !instance.Node.address || !instance.Node.port) {
return res.status(500).send('Invalid instance node configuration');
Expand Down
4 changes: 2 additions & 2 deletions routes/Instance/EditFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ router.post("/instance/:id/files/edit/:filename", async (req, res) => {
}

if(instance.suspended === true) {
return res.redirect('../../instance/' + id + '/suspended');
}
return res.redirect('../../instances?err=SUSPENDED');
}

if (!instance.Node || !instance.Node.address || !instance.Node.port) {
return res.status(500).send('Invalid instance node configuration');
Expand Down
7 changes: 3 additions & 4 deletions routes/Instance/FileUnzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ router.get('/instance/:id/files/unzip/:file', async (req, res) => {
db.set(id + '_instance', instance);
}

if (instance.suspended === true) {
console.log(`Instance ${id} is suspended, redirecting to suspended page`); // Log suspension
return res.redirect('../../instance/' + id + '/suspended');
}
if(instance.suspended === true) {
return res.redirect('../../instances?err=SUSPENDED');
}

if (!instance || !instance.VolumeId) {
console.error(`Instance ${id} missing VolumeId, redirecting to instances page`); // Log missing VolumeId
Expand Down
7 changes: 3 additions & 4 deletions routes/Instance/Files.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ router.get("/instance/:id/files", async (req, res) => {
}


if(!instance.suspended) {
instance.suspended = false;
db.set(id + '_instance', instance);
}
if(instance.suspended === true) {
return res.redirect('../../instances?err=SUSPENDED');
}

if(instance.suspended === true) {
return res.redirect('../../instance/' + id + '/suspended');
Expand Down
8 changes: 4 additions & 4 deletions routes/Instance/ImageFeatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ router.post("/instance/:id/imagefeatures/eula", async (req, res) => {
db.set(id + '_instance', instance);
}

if(instance.suspended === true) {
return res.redirect('../../instance/' + id + '/suspended');
if(instance.suspended === true) {
return res.redirect('../../instances?err=SUSPENDED');
}
createFile(instance, 'eula.txt', 'eula=true');

Expand Down Expand Up @@ -59,8 +59,8 @@ router.get("/instance/:id/imagefeatures/cracked", async (req, res) => {
db.set(id + '_instance', instance);
}

if(instance.suspended === true) {
return res.redirect('../../instance/' + id + '/suspended');
if(instance.suspended === true) {
return res.redirect('../../instances?err=SUSPENDED');
}
const serverport = instance.Ports.split(':')[1];
const now = new Date();
Expand Down
72 changes: 62 additions & 10 deletions routes/Instance/Instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ router.get("/instance/:id", async (req, res) => {
return res.redirect('../../instance/' + id + '/installing')
}
if(instance.suspended === true) {
return res.redirect('../../instance/' + id + '/suspended');
}
return res.redirect('../../instances?err=SUSPENDED');
}

const config = require('../../config.json');
const { port, domain } = config;
Expand Down Expand Up @@ -99,6 +99,7 @@ router.get("/instance/:id/installing", async (req,res) => {
if (!isAuthorized) {
return res.status(403).send('Unauthorized access to this instance.');
}
await checkState(id);
res.render('instance/installing', {
req,
instance,
Expand All @@ -123,13 +124,64 @@ router.get("/instance/:id/installing/status", async (req, res) => {
return res.status(403).send('Unauthorized access to this instance.');
}

const getStateUrl = `http://${instance.Node.address}:${instance.Node.port}/instances/${instance.Id}/states/get`;
const getStateResponse = await axios.get(getStateUrl, {
auth: {
username: "Skyport",
password: instance.Node.apiKey,
},
});
res.status(200).json({ state: getStateResponse.data.state })
await checkState(id);

res.status(200).json({ state: instance.State })
});

async function checkState(instanceId) {
try {
// Get the specific instance from the "instances" database
const instance = await db.get(`${instanceId}_instance`);

if (!instance) {
return ("Instance not found.");
}

try {
// Fetch the current state from the remote server
const getStateUrl = `http://${instance.Node.address}:${instance.Node.port}/instances/${instance.Id}/states/get`;
const getStateResponse = await axios.get(getStateUrl, {
auth: {
username: "Skyport",
password: instance.Node.apiKey,
},
});

const newState = getStateResponse.data.state;

// Update the state on the remote server
const setStateUrl = `http://${instance.Node.address}:${instance.Node.port}/instances/${instance.Id}/states/set/${newState}`;
await axios.get(setStateUrl, {
auth: {
username: "Skyport",
password: instance.Node.apiKey,
},
});

// Update the instance state locally
instance.State = newState;

// Get the instance-specific database and ensure it has the "State" property
const instanceDbKey = `${instanceId}_instance`; // Define the key for instance-specific database
let instanceDb = await db.get(instanceDbKey);

if (!instanceDb) {
instanceDb = {};
}

instanceDb.State = newState;

// Save the updated instance-specific database
await db.set(instanceDbKey, instanceDb);

} catch (instanceError) {
console.error(`Error processing instance ${instance.Id}:`, instanceError.message);
}
} catch (error) {
console.error("Error processing instances:", error.message);
}
}


module.exports = router;
8 changes: 4 additions & 4 deletions routes/Instance/InstanceDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ router.get("/instance/:id/db", async (req, res) => {
db.set(id + '_instance', instance);
}

if(instance.suspended === true) {
return res.redirect('../../instance/' + id + '/suspended');
if(instance.suspended === true) {
return res.redirect('../../instances?err=SUSPENDED');
}

if (instance.Node && instance.Node.address && instance.Node.port) {
Expand Down Expand Up @@ -104,8 +104,8 @@ router.post("/instance/:id/db/create/:name", async (req, res) => {
db.set(id + '_instance', instance);
}

if(instance.suspended === true) {
return res.redirect('../../instance/' + id + '/suspended');
if(instance.suspended === true) {
return res.redirect('../../instances?err=SUSPENDED');
}

if (instance.Node && instance.Node.address && instance.Node.port) {
Expand Down
7 changes: 3 additions & 4 deletions routes/Instance/InstanceFTP.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ router.get("/instance/:id/ftp", async (req, res) => {
}


if(!instance.suspended) {
instance.suspended = false;
db.set(id + '_instance', instance);
}
if(instance.suspended === true) {
return res.redirect('../../instances?err=SUSPENDED');
}

if(instance.suspended === true) {
return res.redirect('../../instance/' + id + '/suspended');
Expand Down
5 changes: 2 additions & 3 deletions routes/Instance/InstanceReinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ router.post('/instance/reinstall/:id', async (req, res) => {
}

if(instance.suspended === true) {
return res.redirect('../../instance/' + id + '/suspended');
}

return res.redirect('../../instances?err=SUSPENDED');
}
const { Node: node, imageData, Memory: memory, Cpu: cpu, Ports: ports, Name: name, User: user, Primary: primary, ContainerId: containerId, Env } = instance;
const nodeId = node.id;

Expand Down
4 changes: 2 additions & 2 deletions routes/Instance/InstanceSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ router.get("/instance/:id/settings", async (req, res) => {
}

if(instance.suspended === true) {
return res.redirect('../../instance/' + id + '/suspended');
}
return res.redirect('../../instances?err=SUSPENDED');
}

const allPluginData = Object.values(plugins).map(plugin => plugin.config);
res.render('instance/settings', {
Expand Down
48 changes: 0 additions & 48 deletions routes/Instance/InstanceSupended.js

This file was deleted.

6 changes: 2 additions & 4 deletions routes/Instance/Network.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ router.get("/instance/:id/network", async (req, res) => {
instance.suspended = false;
db.set(id + '_instance', instance);
}

if(instance.suspended === true) {
return res.redirect('../../instance/' + id + '/suspended');
}

return res.redirect('../../instances?err=SUSPENDED');
}
const allPluginData = Object.values(plugins).map(plugin => plugin.config);
const ports = processPorts(instance.Ports, instance);

Expand Down
Loading

0 comments on commit 10cd6f0

Please sign in to comment.