Compose.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. <template>
  2. <transition name="slide-fade" appear>
  3. <div>
  4. <h1 v-if="isAdd" class="mb-3">Compose</h1>
  5. <h1 v-else class="mb-3"><Uptime :stack="globalStack" :pill="true" /> {{ stack.name }}</h1>
  6. <div v-if="stack.isManagedByDockge" class="mb-3">
  7. <div class="btn-group me-2" role="group">
  8. <button v-if="isEditMode" class="btn btn-primary" :disabled="processing" @click="deployStack">
  9. <font-awesome-icon icon="rocket" class="me-1" />
  10. {{ $t("deployStack") }}
  11. </button>
  12. <button v-if="isEditMode" class="btn btn-normal" :disabled="processing" @click="saveStack">
  13. <font-awesome-icon icon="save" class="me-1" />
  14. {{ $t("saveStackDraft") }}
  15. </button>
  16. <button v-if="!isEditMode" class="btn btn-secondary" :disabled="processing" @click="enableEditMode">
  17. <font-awesome-icon icon="pen" class="me-1" />
  18. {{ $t("editStack") }}
  19. </button>
  20. <button v-if="!isEditMode && !active" class="btn btn-primary" :disabled="processing" @click="startStack">
  21. <font-awesome-icon icon="play" class="me-1" />
  22. {{ $t("startStack") }}
  23. </button>
  24. <button v-if="!isEditMode && active" class="btn btn-normal " :disabled="processing" @click="restartStack">
  25. <font-awesome-icon icon="rotate" class="me-1" />
  26. {{ $t("restartStack") }}
  27. </button>
  28. <button v-if="!isEditMode" class="btn btn-normal" :disabled="processing" @click="updateStack">
  29. <font-awesome-icon icon="cloud-arrow-down" class="me-1" />
  30. {{ $t("updateStack") }}
  31. </button>
  32. <button v-if="!isEditMode && active" class="btn btn-normal" :disabled="processing" @click="stopStack">
  33. <font-awesome-icon icon="stop" class="me-1" />
  34. {{ $t("stopStack") }}
  35. </button>
  36. </div>
  37. <button v-if="isEditMode && !isAdd" class="btn btn-normal" :disabled="processing" @click="discardStack">{{ $t("discardStack") }}</button>
  38. <button v-if="!isEditMode" class="btn btn-danger" :disabled="processing" @click="showDeleteDialog = !showDeleteDialog">
  39. <font-awesome-icon icon="trash" class="me-1" />
  40. {{ $t("deleteStack") }}
  41. </button>
  42. </div>
  43. <!-- Progress Terminal -->
  44. <transition name="slide-fade" appear>
  45. <Terminal
  46. v-show="showProgressTerminal"
  47. ref="progressTerminal"
  48. class="mb-3 terminal"
  49. :name="terminalName"
  50. :rows="progressTerminalRows"
  51. @has-data="showProgressTerminal = true; submitted = true;"
  52. ></Terminal>
  53. </transition>
  54. <div v-if="stack.isManagedByDockge" class="row">
  55. <div class="col-lg-6">
  56. <!-- General -->
  57. <div v-if="isAdd">
  58. <h4 class="mb-3">{{ $t("general") }}</h4>
  59. <div class="shadow-box big-padding mb-3">
  60. <!-- Stack Name -->
  61. <div>
  62. <label for="name" class="form-label">{{ $t("stackName") }}</label>
  63. <input id="name" v-model="stack.name" type="text" class="form-control" required @blur="stackNameToLowercase">
  64. <div class="form-text">Lowercase only</div>
  65. </div>
  66. </div>
  67. </div>
  68. <!-- Containers -->
  69. <h4 class="mb-3">{{ $tc("container", 2) }}</h4>
  70. <div v-if="isEditMode" class="input-group mb-3">
  71. <input
  72. v-model="newContainerName"
  73. placeholder="New Container Name..."
  74. class="form-control"
  75. @keyup.enter="addContainer"
  76. />
  77. <button class="btn btn-primary" @click="addContainer">
  78. {{ $t("addContainer") }}
  79. </button>
  80. </div>
  81. <div ref="containerList">
  82. <Container
  83. v-for="(service, name) in jsonConfig.services"
  84. :key="name"
  85. :name="name"
  86. :is-edit-mode="isEditMode"
  87. :first="name === Object.keys(jsonConfig.services)[0]"
  88. :status="serviceStatusList[name]"
  89. />
  90. </div>
  91. <button v-if="false && isEditMode && jsonConfig.services && Object.keys(jsonConfig.services).length > 0" class="btn btn-normal mb-3" @click="addContainer">{{ $t("addContainer") }}</button>
  92. <!-- Combined Terminal Output -->
  93. <div v-show="!isEditMode">
  94. <h4 class="mb-3">Terminal</h4>
  95. <Terminal
  96. ref="combinedTerminal"
  97. class="mb-3 terminal"
  98. :name="combinedTerminalName"
  99. :rows="combinedTerminalRows"
  100. :cols="combinedTerminalCols"
  101. style="height: 350px;"
  102. ></Terminal>
  103. </div>
  104. </div>
  105. <div class="col-lg-6">
  106. <h4 class="mb-3">{{ stack.composeFileName }}</h4>
  107. <!-- YAML editor -->
  108. <div class="shadow-box mb-3 editor-box" :class="{'edit-mode' : isEditMode}">
  109. <prism-editor
  110. ref="editor"
  111. v-model="stack.composeYAML"
  112. class="yaml-editor"
  113. :highlight="highlighter"
  114. line-numbers :readonly="!isEditMode"
  115. @input="yamlCodeChange"
  116. @focus="editorFocus = true"
  117. @blur="editorFocus = false"
  118. ></prism-editor>
  119. </div>
  120. <div v-if="isEditMode" class="mb-3">
  121. {{ yamlError }}
  122. </div>
  123. <div v-if="isEditMode">
  124. <!-- Volumes -->
  125. <div v-if="false">
  126. <h4 class="mb-3">{{ $tc("volume", 2) }}</h4>
  127. <div class="shadow-box big-padding mb-3">
  128. </div>
  129. </div>
  130. <!-- Networks -->
  131. <h4 class="mb-3">{{ $tc("network", 2) }}</h4>
  132. <div class="shadow-box big-padding mb-3">
  133. <NetworkInput />
  134. </div>
  135. </div>
  136. <!-- <div class="shadow-box big-padding mb-3">
  137. <div class="mb-3">
  138. <label for="name" class="form-label"> Search Templates</label>
  139. <input id="name" v-model="name" type="text" class="form-control" placeholder="Search..." required>
  140. </div>
  141. <prism-editor v-if="false" v-model="yamlConfig" class="yaml-editor" :highlight="highlighter" line-numbers @input="yamlCodeChange"></prism-editor>
  142. </div>-->
  143. </div>
  144. </div>
  145. <div v-if="!stack.isManagedByDockge && !processing">
  146. {{ $t("stackNotManagedByDockgeMsg") }}
  147. </div>
  148. <!-- Delete Dialog -->
  149. <BModal v-model="showDeleteDialog" :okTitle="$t('deleteStack')" okVariant="danger" @ok="deleteDialog">
  150. {{ $t("deleteStackMsg") }}
  151. </BModal>
  152. </div>
  153. </transition>
  154. </template>
  155. <script>
  156. import { highlight, languages } from "prismjs/components/prism-core";
  157. import { PrismEditor } from "vue-prism-editor";
  158. import "prismjs/components/prism-yaml";
  159. import { parseDocument, Document } from "yaml";
  160. import "prismjs/themes/prism-tomorrow.css";
  161. import "vue-prism-editor/dist/prismeditor.min.css";
  162. import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
  163. import {
  164. COMBINED_TERMINAL_COLS,
  165. COMBINED_TERMINAL_ROWS,
  166. copyYAMLComments,
  167. getCombinedTerminalName,
  168. getComposeTerminalName,
  169. PROGRESS_TERMINAL_ROWS,
  170. RUNNING
  171. } from "../../../backend/util-common";
  172. import { BModal } from "bootstrap-vue-next";
  173. import NetworkInput from "../components/NetworkInput.vue";
  174. const template = `version: "3.8"
  175. services:
  176. nginx:
  177. image: nginx:latest
  178. restart: unless-stopped
  179. ports:
  180. - "8080:80"
  181. `;
  182. let yamlErrorTimeout = null;
  183. let serviceStatusTimeout = null;
  184. export default {
  185. components: {
  186. NetworkInput,
  187. FontAwesomeIcon,
  188. PrismEditor,
  189. BModal,
  190. },
  191. beforeRouteUpdate(to, from, next) {
  192. this.exitConfirm(next);
  193. },
  194. beforeRouteLeave(to, from, next) {
  195. this.exitConfirm(next);
  196. },
  197. yamlDoc: null, // For keeping the yaml comments
  198. data() {
  199. return {
  200. editorFocus: false,
  201. jsonConfig: {},
  202. yamlError: "",
  203. processing: true,
  204. showProgressTerminal: false,
  205. progressTerminalRows: PROGRESS_TERMINAL_ROWS,
  206. combinedTerminalRows: COMBINED_TERMINAL_ROWS,
  207. combinedTerminalCols: COMBINED_TERMINAL_COLS,
  208. stack: {
  209. },
  210. serviceStatusList: {},
  211. isEditMode: false,
  212. submitted: false,
  213. showDeleteDialog: false,
  214. newContainerName: "",
  215. stopServiceStatusTimeout: false,
  216. };
  217. },
  218. computed: {
  219. isAdd() {
  220. return this.$route.path === "/compose" && !this.submitted;
  221. },
  222. /**
  223. * Get the stack from the global stack list, because it may contain more real-time data like status
  224. * @return {*}
  225. */
  226. globalStack() {
  227. return this.$root.stackList[this.stack.name];
  228. },
  229. status() {
  230. return this.globalStack?.status;
  231. },
  232. active() {
  233. return this.status === RUNNING;
  234. },
  235. terminalName() {
  236. if (!this.stack.name) {
  237. return "";
  238. }
  239. return getComposeTerminalName(this.stack.name);
  240. },
  241. combinedTerminalName() {
  242. if (!this.stack.name) {
  243. return "";
  244. }
  245. return getCombinedTerminalName(this.stack.name);
  246. },
  247. networks() {
  248. return this.jsonConfig.networks;
  249. }
  250. },
  251. watch: {
  252. "stack.composeYAML": {
  253. handler() {
  254. if (this.editorFocus) {
  255. console.debug("yaml code changed");
  256. this.yamlCodeChange();
  257. }
  258. },
  259. deep: true,
  260. },
  261. jsonConfig: {
  262. handler() {
  263. if (!this.editorFocus) {
  264. console.debug("jsonConfig changed");
  265. let doc = new Document(this.jsonConfig);
  266. // Stick back the yaml comments
  267. if (this.yamlDoc) {
  268. copyYAMLComments(doc, this.yamlDoc);
  269. }
  270. this.stack.composeYAML = doc.toString();
  271. this.yamlDoc = doc;
  272. }
  273. },
  274. deep: true,
  275. },
  276. },
  277. mounted() {
  278. if (this.isAdd) {
  279. this.processing = false;
  280. this.isEditMode = true;
  281. let composeYAML;
  282. if (this.$root.composeTemplate) {
  283. composeYAML = this.$root.composeTemplate;
  284. this.$root.composeTemplate = "";
  285. } else {
  286. composeYAML = template;
  287. }
  288. // Default Values
  289. this.stack = {
  290. name: "",
  291. composeYAML,
  292. isManagedByDockge: true,
  293. };
  294. this.yamlCodeChange();
  295. } else {
  296. this.stack.name = this.$route.params.stackName;
  297. this.loadStack();
  298. }
  299. this.requestServiceStatus();
  300. },
  301. unmounted() {
  302. this.stopServiceStatusTimeout = true;
  303. clearTimeout(serviceStatusTimeout);
  304. },
  305. methods: {
  306. startServiceStatusTimeout() {
  307. clearTimeout(serviceStatusTimeout);
  308. serviceStatusTimeout = setTimeout(async () => {
  309. this.requestServiceStatus();
  310. }, 2000);
  311. },
  312. requestServiceStatus() {
  313. this.$root.getSocket().emit("serviceStatusList", this.stack.name, (res) => {
  314. if (res.ok) {
  315. this.serviceStatusList = res.serviceStatusList;
  316. }
  317. if (!this.stopServiceStatusTimeout) {
  318. this.startServiceStatusTimeout();
  319. }
  320. });
  321. },
  322. exitConfirm(next) {
  323. if (this.isEditMode) {
  324. if (confirm("You are currently editing a stack. Are you sure you want to leave?")) {
  325. next();
  326. } else {
  327. next(false);
  328. }
  329. } else {
  330. next();
  331. }
  332. },
  333. bindTerminal() {
  334. this.$refs.progressTerminal?.bind(this.terminalName);
  335. },
  336. loadStack() {
  337. this.processing = true;
  338. this.$root.getSocket().emit("getStack", this.stack.name, (res) => {
  339. if (res.ok) {
  340. this.stack = res.stack;
  341. this.yamlCodeChange();
  342. this.processing = false;
  343. this.bindTerminal();
  344. } else {
  345. this.$root.toastRes(res);
  346. }
  347. });
  348. },
  349. deployStack() {
  350. this.processing = true;
  351. if (!this.jsonConfig.services) {
  352. this.$root.toastError("No services found in compose.yaml");
  353. this.processing = false;
  354. return;
  355. }
  356. // Check if services is object
  357. if (typeof this.jsonConfig.services !== "object") {
  358. this.$root.toastError("Services must be an object");
  359. this.processing = false;
  360. return;
  361. }
  362. let serviceNameList = Object.keys(this.jsonConfig.services);
  363. // Set the stack name if empty, use the first container name
  364. if (!this.stack.name && serviceNameList.length > 0) {
  365. let serviceName = serviceNameList[0];
  366. let service = this.jsonConfig.services[serviceName];
  367. if (service && service.container_name) {
  368. this.stack.name = service.container_name;
  369. } else {
  370. this.stack.name = serviceName;
  371. }
  372. }
  373. this.bindTerminal(this.terminalName);
  374. this.$root.getSocket().emit("deployStack", this.stack.name, this.stack.composeYAML, this.isAdd, (res) => {
  375. this.processing = false;
  376. this.$root.toastRes(res);
  377. if (res.ok) {
  378. this.isEditMode = false;
  379. this.$router.push("/compose/" + this.stack.name);
  380. }
  381. });
  382. },
  383. saveStack() {
  384. this.processing = true;
  385. this.$root.getSocket().emit("saveStack", this.stack.name, this.stack.composeYAML, this.isAdd, (res) => {
  386. this.processing = false;
  387. this.$root.toastRes(res);
  388. if (res.ok) {
  389. this.isEditMode = false;
  390. this.$router.push("/compose/" + this.stack.name);
  391. }
  392. });
  393. },
  394. startStack() {
  395. this.processing = true;
  396. this.$root.getSocket().emit("startStack", this.stack.name, (res) => {
  397. this.processing = false;
  398. this.$root.toastRes(res);
  399. });
  400. },
  401. stopStack() {
  402. this.processing = true;
  403. this.$root.getSocket().emit("stopStack", this.stack.name, (res) => {
  404. this.processing = false;
  405. this.$root.toastRes(res);
  406. });
  407. },
  408. restartStack() {
  409. this.processing = true;
  410. this.$root.getSocket().emit("restartStack", this.stack.name, (res) => {
  411. this.processing = false;
  412. this.$root.toastRes(res);
  413. });
  414. },
  415. updateStack() {
  416. this.processing = true;
  417. this.$root.getSocket().emit("updateStack", this.stack.name, (res) => {
  418. this.processing = false;
  419. this.$root.toastRes(res);
  420. });
  421. },
  422. deleteDialog() {
  423. this.$root.getSocket().emit("deleteStack", this.stack.name, (res) => {
  424. this.$root.toastRes(res);
  425. if (res.ok) {
  426. this.$router.push("/");
  427. }
  428. });
  429. },
  430. discardStack() {
  431. this.loadStack();
  432. this.isEditMode = false;
  433. },
  434. highlighter(code) {
  435. return highlight(code, languages.yaml);
  436. },
  437. yamlCodeChange() {
  438. try {
  439. let doc = parseDocument(this.stack.composeYAML);
  440. if (doc.errors.length > 0) {
  441. throw doc.errors[0];
  442. }
  443. const config = doc.toJS() ?? {};
  444. // Check data types
  445. // "services" must be an object
  446. if (!config.services) {
  447. config.services = {};
  448. }
  449. if (Array.isArray(config.services) || typeof config.services !== "object") {
  450. throw new Error("Services must be an object");
  451. }
  452. if (!config.version) {
  453. config.version = "3.8";
  454. }
  455. this.yamlDoc = doc;
  456. this.jsonConfig = config;
  457. clearTimeout(yamlErrorTimeout);
  458. this.yamlError = "";
  459. } catch (e) {
  460. clearTimeout(yamlErrorTimeout);
  461. if (this.yamlError) {
  462. this.yamlError = e.message;
  463. } else {
  464. yamlErrorTimeout = setTimeout(() => {
  465. this.yamlError = e.message;
  466. }, 3000);
  467. }
  468. }
  469. },
  470. enableEditMode() {
  471. this.isEditMode = true;
  472. },
  473. checkYAML() {
  474. },
  475. addContainer() {
  476. this.checkYAML();
  477. if (this.jsonConfig.services[this.newContainerName]) {
  478. this.$root.toastError("Container name already exists");
  479. return;
  480. }
  481. if (!this.newContainerName) {
  482. this.$root.toastError("Container name cannot be empty");
  483. return;
  484. }
  485. this.jsonConfig.services[this.newContainerName] = {
  486. restart: "unless-stopped",
  487. };
  488. this.newContainerName = "";
  489. let element = this.$refs.containerList.lastElementChild;
  490. element.scrollIntoView({
  491. block: "start",
  492. behavior: "smooth"
  493. });
  494. },
  495. stackNameToLowercase() {
  496. this.stack.name = this.stack?.name?.toLowerCase();
  497. },
  498. }
  499. };
  500. </script>
  501. <style scoped lang="scss">
  502. .terminal {
  503. height: 200px;
  504. }
  505. .editor-box {
  506. font-family: 'JetBrains Mono', monospace;
  507. font-size: 14px;
  508. &.edit-mode {
  509. background-color: #2c2f38 !important;
  510. }
  511. }
  512. </style>