You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.8 KiB
JavaScript

defineVirtualDevice("vestasync", {
title: "Vestasync",
cells: {
1 year ago
last_push: {
type: "text",
value: "Update...",
title: "Last push"
},
1 year ago
last_commit: {
type: "text",
value: "Update...",
title: "Last commit hash"
},
1 year ago
autopush_inotify: {
type: "switch",
1 year ago
value: false,
title: "Auto push on files changed"
},
1 year ago
autopush_timer: {
type: "switch",
value: false,
title: "Auto push every day"
},
push_now: {
type: "switch",
value: false,
title: "Commit and push"
},
hostname: {
type: "text",
value: "",
title: "Hostname"
}
}
});
function _update_vestasync() {
runShellCommand("git -C /mnt/data/etc log -1 --format=%ct", {
captureOutput: true,
exitCallback: function (exitCode, capturedOutput) {
if (exitCode === 0) {
var last_push_time = parseInt(capturedOutput);
var now = new Date();
var diff_in_seconds = Math.floor((now.getTime() / 1000) - last_push_time);
var diff_in_minutes = Math.floor(diff_in_seconds / 60);
var diff_in_hours = Math.floor(diff_in_minutes / 60);
var diff_in_days = Math.floor(diff_in_hours / 24);
var human_readable_time = "";
if (diff_in_days > 0) {
human_readable_time = diff_in_days + " days ago";
} else if (diff_in_hours > 0) {
human_readable_time = diff_in_hours + " hours ago";
} else if (diff_in_minutes > 0) {
human_readable_time = diff_in_minutes + " minutes ago";
} else {
human_readable_time = "just now";
}
1 year ago
dev.vestasync.last_push = human_readable_time;
}
}
});
runShellCommand("git -C /mnt/data/etc log -1 --format=%H", {
captureOutput: true,
exitCallback: function (exitCode, capturedOutput) {
if (exitCode === 0) {
var shortenedCommit = capturedOutput.trim().substring(0, 10);
1 year ago
dev.vestasync.last_commit = shortenedCommit;
}
}
});
1 year ago
runShellCommand("systemctl is-active pushgit_inotify.service", {
captureOutput: true,
exitCallback: function (exitCode, capturedOutput) {
1 year ago
var isEnabled = capturedOutput.trim() === "active";
1 year ago
dev.vestasync.autopush_inotify = isEnabled;
}
});
runShellCommand("systemctl is-active pushgit.timer", {
captureOutput: true,
exitCallback: function (exitCode, capturedOutput) {
var isEnabled = capturedOutput.trim() === "active";
dev.vestasync.autopush_timer = isEnabled;
}
});
runShellCommand("hostname", {
captureOutput: true,
exitCallback: function (exitCode, capturedOutput) {
if (exitCode === 0) {
var hostname = capturedOutput.trim();
dev.vestasync.hostname = hostname;
} else {
console.error("Error checking hostname:", capturedOutput.trim());
}
}
});
};
1 year ago
defineRule("_vestasync_autopush_timer", {
whenChanged: "vestasync/autopush_timer",
then: function (newValue, devName, cellName) {
if (dev.vestasync.autopush_timer) {
runShellCommand("systemctl daemon-reload ; systemctl enable pushgit.timer ; systemctl start pushgit.timer; sleep 10");
_update_vestasync();
} else {
runShellCommand("systemctl stop pushgit.timer ; systemctl disable pushgit.timer; sleep 10");
_update_vestasync();
}
}
});
1 year ago
defineRule("_vestasync_autopush_inotify", {
whenChanged: "vestasync/autopush_inotify",
then: function (newValue, devName, cellName) {
1 year ago
if (dev.vestasync.autopush_inotify) {
runShellCommand("systemctl daemon-reload ; systemctl enable pushgit_inotify.service ; systemctl start pushgit_inotify.service; sleep 10");
_update_vestasync();
} else {
1 year ago
runShellCommand("systemctl stop pushgit_inotify.service ; systemctl disable pushgit_inotify.service; sleep 10");
_update_vestasync();
}
}
});
defineRule("_vestasync_push", {
whenChanged: "vestasync/push_now",
then: function (newValue, devName, cellName) {
if (dev.vestasync.push_now) {
runShellCommand("systemctl start pushgit.service; sleep 2");
dev.vestasync.push_now = false;
_update_vestasync();
}
}
});
_update_vestasync();
1 year ago
setInterval(_update_vestasync, 11000);