diff --git a/src/api/agents.js b/src/api/agents.js index c9b268e..cac64af 100644 --- a/src/api/agents.js +++ b/src/api/agents.js @@ -14,4 +14,11 @@ export async function fetchAgentHistory(pk) { const { data } = await axios.get(`${baseUrl}/history/${pk}`) return data } catch (e) { } -} \ No newline at end of file +} + +export async function runScript(payload) { + try { + const { data } = await axios.post(`${baseUrl}/runscript/`, payload) + return data + } catch (e) { } +} diff --git a/src/api/scripts.js b/src/api/scripts.js new file mode 100644 index 0000000..e2d8a63 --- /dev/null +++ b/src/api/scripts.js @@ -0,0 +1,10 @@ +import axios from "axios" + +const baseUrl = "/scripts" + +export async function fetchScripts(params = {}) { + try { + const { data } = await axios.get(`${baseUrl}/scripts/`, { params: params }) + return data + } catch (e) { } +} diff --git a/src/components/AgentTable.vue b/src/components/AgentTable.vue index 0e19729..9295e55 100644 --- a/src/components/AgentTable.vue +++ b/src/components/AgentTable.vue @@ -141,7 +141,7 @@ Send Command - + @@ -414,10 +414,6 @@ - - - - @@ -443,7 +439,6 @@ export default { PendingActions, SendCommand, AgentRecovery, - RunScript, }, mixins: [mixins], data() { @@ -457,7 +452,6 @@ export default { showEditAgentModal: false, showRebootLaterModal: false, showAgentRecovery: false, - showRunScript: false, showPendingActions: false, pendingActionAgentPk: null, favoriteScripts: [], @@ -803,6 +797,14 @@ export default { }) .catch(() => {}); }, + showRunScript(agent) { + this.$q.dialog({ + component: RunScript, + componentProps: { + agent, + }, + }); + }, }, computed: { ...mapGetters(["selectedAgentPk", "agentTableHeight"]), diff --git a/src/components/ChecksTab.vue b/src/components/ChecksTab.vue index a218f8a..1f669f3 100644 --- a/src/components/ChecksTab.vue +++ b/src/components/ChecksTab.vue @@ -330,6 +330,8 @@ import ScriptOutput from "@/components/modals/checks/ScriptOutput"; import EventLogCheckOutput from "@/components/modals/checks/EventLogCheckOutput"; import CheckGraph from "@/components/graphs/CheckGraph"; +import { truncateText } from "@/utils/format"; + export default { name: "ChecksTab", emits: ["edit"], diff --git a/src/components/modals/agents/RunScript.vue b/src/components/modals/agents/RunScript.vue index aed94dd..d6bf286 100644 --- a/src/components/modals/agents/RunScript.vue +++ b/src/components/modals/agents/RunScript.vue @@ -1,174 +1,184 @@ - - - Run a script on {{ hostname }} - - - - - - - - - - - - - {{ - scope.opt.category - }} - - - - - - - - - - - - - - - - + + + Run a script on {{ agent.hostname }} + + + Minimize + + + Maximize + + + Close + + + + + + + + + - - - - - - - - - - - - - - {{ ret }} - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + {{ ret }} + + + + \ No newline at end of file diff --git a/src/composables/scripts.js b/src/composables/scripts.js new file mode 100644 index 0000000..6cdc8e4 --- /dev/null +++ b/src/composables/scripts.js @@ -0,0 +1,37 @@ +import { ref, watch } from "vue" +import { fetchScripts } from "@/api/scripts" +import { formatScriptOptions } from "@/utils/format" + +// script dropdown +export function useScriptDropdown() { + + const scriptOptions = ref([]) + const defaultTimeout = ref(30) + const defaultArgs = ref([]) + const scriptPK = ref(null) + + // specifing flat returns an array of script names versus {value:id, label: hostname} + async function getScriptOptions(showCommunityScripts = false, flat = false) { + scriptOptions.value = formatScriptOptions(await fetchScripts({ showCommunityScripts }), flat) + } + + // watch scriptPk for changes and update the default timeout and args + watch(scriptPK, (newValue, oldValue) => { + if (newValue) { + const script = scriptOptions.value.find(i => i.value === newValue); + defaultTimeout.value = script.timeout; + defaultArgs.value = script.args; + } + }) + + return { + //data + scriptPK, + scriptOptions, + defaultTimeout, + defaultArgs, + + //methods + getScriptOptions + } +} diff --git a/src/utils/format.js b/src/utils/format.js index 331ee25..6875e6a 100644 --- a/src/utils/format.js +++ b/src/utils/format.js @@ -19,6 +19,43 @@ function _formatOptions(data, { label, value = "id", flat = false, allowDuplicat } } +export function formatScriptOptions(data, flat = false) { + if (flat) { + // returns just script names in array + return _formatOptions(data, { label: "name", value: "pk", flat: true, allowDuplicates: false }) + } else { + + let options = []; + let categories = []; + let create_unassigned = false + data.forEach(script => { + if (!!script.category && !categories.includes(script.category)) { + categories.push(script.category); + } else if (!script.category) { + create_unassigned = true + } + }); + + if (create_unassigned) categories.push("Unassigned") + + categories.sort().forEach(cat => { + options.push({ category: cat }); + let tmp = []; + data.forEach(script => { + if (script.category === cat) { + tmp.push({ label: script.name, value: script.id, timeout: script.default_timeout, args: script.args }); + } else if (cat === "Unassigned" && !script.category) { + tmp.push({ label: script.name, value: script.id, timeout: script.default_timeout, args: script.args }); + } + }) + const sorted = tmp.sort((a, b) => a.label.localeCompare(b.label)); + options.push(...sorted); + }); + + return options; + } +} + export function formatAgentOptions(data, flat = false) { if (flat) { @@ -97,6 +134,7 @@ export function formatDate(date, includeSeconds = false) { // string formatting + export function capitalize(string) { return string[0].toUpperCase() + string.substring(1); } @@ -109,4 +147,8 @@ export function formatTableColumnText(text) { words.forEach(word => string = string + " " + capitalize(word)) return string.trim() +} + +export function truncateText(txt, chars) { + return txt.length >= chars ? txt.substring(0, chars) + "..." : txt; } \ No newline at end of file
{{ ret }}