HostExec.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <template>
  2. <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
  3. <el-tab-pane label="批量执行" name="first">
  4. <div style="margin: 10px 0">执行主机: </div>
  5. <el-input disabled type="textarea" required placeholder="目标主机IP,多个用逗号分隔" v-model="selected_host">
  6. </el-input>
  7. <div style="margin: 10px 0">
  8. <el-button @click="select_host_view">从主机列表中选择<i class="el-icon-plus el-icon--right"></i></el-button>
  9. </div>
  10. <div style="margin: 10px 0">执行命令: </div>
  11. <color-input v-model="exec_command" required></color-input>
  12. <div style="margin: 10px 0">
  13. <el-button @click="select_exec_tpl_view" plain>从执行模板中选择<i class="el-icon-plus el-icon--right"></i></el-button>
  14. </div>
  15. <el-button type="primary" v-if="has_permission('assets_host_exec')"
  16. :disabled="is_disabled" @click="start_exec_command">开始执行</el-button>
  17. </el-tab-pane>
  18. <el-tab-pane label="执行模板" name="second">
  19. <el-row>
  20. <el-col :span="16">
  21. <el-form :inline="true" :model="tpl_query">
  22. <el-form-item>
  23. <el-input v-model="tpl_query.name_field" clearable placeholder="请输入模板名称"></el-input>
  24. </el-form-item>
  25. <el-select v-model="tpl_query.type_field" placeholder="模板类型" clearable>
  26. <el-option v-for="v in tpl_options" :value="v" :key="v"></el-option>
  27. </el-select>
  28. <el-form-item>
  29. <el-button type="primary" icon="search" @click="t_name_search()">查询</el-button>
  30. </el-form-item>
  31. </el-form>
  32. </el-col>
  33. <el-col :span="8" style="text-align: right">
  34. <el-button @click="get_tpl()">刷新</el-button>
  35. <el-button v-if="has_permission('assets_host_exec_tpl_add')" style="float: right" type="primary"
  36. @click="add_exec_tpl()">添加模板
  37. </el-button>
  38. </el-col>
  39. </el-row>
  40. <el-table :data="tpl.data" v-loading="tableLoading" style="width: 100%; margin-top: 20px">
  41. <el-table-column prop="tpl_name" label="模板名称"></el-table-column>
  42. <el-table-column prop="tpl_type" label="模板类型"></el-table-column>
  43. <el-table-column prop="tpl_content" label="模板内容" :show-overflow-tooltip="true"></el-table-column>
  44. <el-table-column prop="tpl_desc" label="描述"></el-table-column>
  45. <el-table-column label="操作" width="220px" v-if="has_permission('assets_host_exec_tpl_edit|assets_host_exec_tpl_del')">
  46. <template slot-scope="scope">
  47. <el-button v-if="has_permission('assets_host_exec_tpl_edit')" size="small" @click="edit_exec_tpl(scope.row)">编辑</el-button>
  48. <el-button v-if="has_permission('assets_host_exec_tpl_del')" size="small" type="danger" @click="del_exec_tpl(scope.row)"
  49. :loading="btnDelLoading[scope.row.id]">删除
  50. </el-button>
  51. </template>
  52. </el-table-column>
  53. </el-table>
  54. <!--分页-->
  55. <div class="pagination-bar" v-if="tpl.total > 10">
  56. <el-pagination
  57. @current-change="handleCurrentChange"
  58. :current-page="currentPage" layout="total, prev, pager, next"
  59. :total="tpl.total">
  60. </el-pagination>
  61. </div>
  62. </el-tab-pane>
  63. <el-dialog title="主机列表" :visible.sync="dialog_host_view" width="80%" :close-on-click-modal="false">
  64. <el-row>
  65. <el-col :span="16">
  66. <!--<el-select v-model="host_zone" @change="zone_Search()" clearable placeholder="区域">-->
  67. <!--<el-option v-for="item in zone_options" :key="item" :value="item"></el-option>-->
  68. <!--</el-select>-->
  69. <el-form :inline="true" :model="host_query">
  70. <el-form-item>
  71. <el-input v-model="host_query.name_field" clearable placeholder="主机名称"></el-input>
  72. </el-form-item>
  73. <el-select v-model="host_query.zone_field" @change="zone_Search()" clearable placeholder="区域">
  74. <el-option v-for="item in zone_options" :key="item" :value="item"></el-option>
  75. </el-select>
  76. <el-form-item>
  77. <el-button type="primary" icon="search" @click="get_hosts()">查询</el-button>
  78. </el-form-item>
  79. </el-form>
  80. </el-col>
  81. </el-row>
  82. <el-table :data="hosts.data" ref="multipleTable" v-loading="hostLoading" @selection-change="handleSelectionChange"
  83. @row-click="handleClickRow" style="width: 100%">
  84. <el-table-column type="selection" width="50">
  85. </el-table-column>
  86. <el-table-column prop="name" label="主机名称"></el-table-column>
  87. <el-table-column prop="zone" label="所属区域"></el-table-column>
  88. <el-table-column prop="type" label="类型"></el-table-column>
  89. <el-table-column prop="ssh_ip" label="SSH连接"></el-table-column>
  90. </el-table>
  91. <!--主机列表分页-->
  92. <div class="pagination-bar" v-if="hosts.total > 10">
  93. <el-pagination
  94. @current-change="hostCurrentChange"
  95. :current-page="hostCurrentPage" layout="total, prev, pager, next"
  96. :total="hosts.total">
  97. </el-pagination>
  98. </div>
  99. <div slot="footer">
  100. <el-button @click="dialog_host_view=false">取消</el-button>
  101. <el-button type="primary" @click="save_select_host">确定</el-button>
  102. </div>
  103. </el-dialog>
  104. <!--模板编辑新增界面-->
  105. <el-dialog :title="FormTitle" :visible.sync="host_tpl_edit_view" :close-on-click-modal="false">
  106. <el-form ref="editForm" :model="editTpl" :rules="rules" label-width="80px">
  107. <el-form-item prop="tpl_name" label="模板名称" required>
  108. <el-input v-model="editTpl.tpl_name" auto-complete="off" ></el-input>
  109. </el-form-item>
  110. <el-form-item label="模板类型" prop="tpl_type" required>
  111. <el-select v-model="editTpl.tpl_type" placeholder="模板类型">
  112. <el-option v-for="v in tpl_options" :value="v" :key="v"></el-option>
  113. </el-select>
  114. <el-button style="margin-left: 15px" type="text" @click="addtplType">添加类型</el-button>
  115. </el-form-item>
  116. <el-form-item prop="tpl_content" label="模板内容" required>
  117. <color-input v-model="editTpl.tpl_content"></color-input>
  118. </el-form-item>
  119. <el-form-item prop="tpl_desc" label="模板描述">
  120. <el-input v-model="editTpl.tpl_desc" auto-complete="off"></el-input>
  121. </el-form-item>
  122. </el-form>
  123. <div slot="footer">
  124. <el-button type="text" @click.native="host_tpl_edit_view = false">取消</el-button>
  125. <el-button type="primary" :loading="editLoading" @click.native="saveTplCommit">保存</el-button>
  126. </div>
  127. </el-dialog>
  128. <el-dialog title="执行模板" :visible.sync="dialog_exec_tpl_view" :close-on-click-modal="false" width="80%">
  129. <el-row>
  130. <el-col :span="16">
  131. <el-form :inline="true" :model="tpl_query">
  132. <el-form-item>
  133. <el-input v-model="tpl_query.name_field" clearable placeholder="请输入模板名称"></el-input>
  134. </el-form-item>
  135. <el-select v-model="tpl_query.type_field" placeholder="模板类型">
  136. <el-option v-for="v in tpl_options" :value="v" :key="v"></el-option>
  137. </el-select>
  138. <el-form-item>
  139. <el-button type="primary" icon="search" @click="t_name_search()">查询</el-button>
  140. </el-form-item>
  141. </el-form>
  142. </el-col>
  143. </el-row>
  144. <el-table ref="singleTable" highlight-current-row :data="tpl.data"
  145. @current-change="handleSelectChange"
  146. v-loading="tableLoading" style="width: 100%; margin-top: 20px">
  147. <el-table-column prop="tpl_name" label="模板名称"></el-table-column>
  148. <el-table-column prop="tpl_type" label="模板类型"></el-table-column>
  149. <el-table-column prop="tpl_content" label="模板内容" :show-overflow-tooltip="true"></el-table-column>
  150. <el-table-column prop="tpl_desc" label="描述"></el-table-column>
  151. </el-table>
  152. <div slot="footer">
  153. <el-button @click="dialog_exec_tpl_view=false">取消</el-button>
  154. <el-button type="primary" @click="save_select_exec_tpl">确定</el-button>
  155. </div>
  156. </el-dialog>
  157. <el-dialog title="执行详情" style="width: 100%" :visible.sync="dialog_exec_detail_view" :close-on-click-modal="false" @close="delete_exec">
  158. <el-collapse>
  159. <el-collapse-item v-for="i in multipleSelection" :key="i.ip" :name="i.ip">
  160. <template slot="title">
  161. <el-tag type="info" style="margin-right: 15px">{{ i.name + '(' + i.ssh_ip + ')'}}</el-tag>
  162. </template>
  163. <pre>** 开始执行 **
  164. <template v-for="line in exec_output"><span v-if="line[i.ssh_ip]">{{line[i.ssh_ip]}}</span></template>
  165. </pre>
  166. </el-collapse-item>
  167. </el-collapse>
  168. </el-dialog>
  169. </el-tabs>
  170. </template>
  171. <script>
  172. import ColorInput from '../publish/ColorInput.vue'
  173. export default {
  174. components: {
  175. 'color-input': ColorInput
  176. },
  177. data() {
  178. return {
  179. activeName: 'first',
  180. hosts: [],
  181. tpl:[],
  182. currentPage: 1,
  183. hostCurrentPage: 1,
  184. tpl_query: {
  185. name_field: '',
  186. type_field: '',
  187. },
  188. host_query: {
  189. name_field: '',
  190. zone_field: '',
  191. },
  192. tableLoading: false,
  193. hostLoading: true,
  194. editLoading:false,
  195. btnDelLoading: {},
  196. exec_command: '',
  197. selected_host: '',
  198. host_zone: '',
  199. zone_options: [],
  200. dialog_host_view: false,
  201. dialog_exec_tpl_view: false,
  202. dialog_exec_detail_view: false,
  203. multipleSelection: [],
  204. selected_host_id: [],
  205. t_name_value:'',
  206. FormTitle:'添加模板',
  207. host_tpl_edit_view: false,
  208. selectTplRow: null,
  209. exec_output: [],
  210. editTpl:{},
  211. socket: undefined,
  212. exec_token: null,
  213. addTpl: {
  214. tpl_name: '',
  215. tpl_desc: '',
  216. tpl_type: '',
  217. tpl_content: '',
  218. },
  219. tpl_options:[],
  220. rules: {
  221. tpl_name: [
  222. {required: true, message: '请输入模板名称', trigger: 'blur'}
  223. ],
  224. tpl_type: [
  225. {required: true, message: '请输入模板类型', trigger: 'blur'}
  226. ],
  227. tpl_content:[
  228. {required: true, message: '请输入模板内容', trigger: 'blur'}
  229. ],
  230. },
  231. };
  232. },
  233. computed: {
  234. is_disabled() {
  235. return ! this.exec_command.replace(' ', '') || ! this.selected_host
  236. }
  237. },
  238. methods: {
  239. handleClick(tab, event) {
  240. if (tab.name === "second"){
  241. this.get_tpl();
  242. this.get_tpl_type();
  243. }
  244. },
  245. handleClickRow(row) {
  246. this.$refs.multipleTable.toggleRowSelection(row)
  247. },
  248. get_hosts (page) {
  249. if (!page) page = 1;
  250. this.hostLoading = true;
  251. let api_uri = '/api/assets/hosts/';
  252. this.$http.get(api_uri, {params: {page: page, host_query: this.host_query}}).then(res => {
  253. this.hosts = res.result
  254. }, res => this.$layer_message(res.result)).finally(() => this.hostLoading = false)
  255. },
  256. get_tpl (page) {
  257. if (!page) page = 1;
  258. this.tableLoading = true;
  259. let api_uri = '/api/assets/hosts_exec/tpl/';
  260. this.$http.get(api_uri, {params: {page: page, tpl_query: this.tpl_query}}).then(res => {
  261. this.tpl = res.result
  262. }, res => this.$layer_message(res.result)).finally(() => this.tableLoading = false)
  263. },
  264. //区域查询
  265. zone_Search(){
  266. this.get_hosts();
  267. },
  268. //获取区域
  269. get_host_zone () {
  270. this.$http.get('/api/assets/hosts/zone/').then(res => {
  271. this.zone_options = res.result
  272. }, res => this.$layer_message(res.result))
  273. },
  274. select_host_view () {
  275. this.dialog_host_view = true;
  276. this.get_hosts();
  277. this.get_host_zone();
  278. },
  279. select_exec_tpl_view () {
  280. this.dialog_exec_tpl_view = true;
  281. this.get_tpl();
  282. this.get_tpl_type();
  283. },
  284. add_exec_tpl(){
  285. this.host_tpl_edit_view = true;
  286. this.editTpl = this.addTpl;
  287. },
  288. edit_exec_tpl (row) {
  289. this.editTpl = this.$deepCopy(row);
  290. this.host_tpl_edit_view = true;
  291. this.title = '编辑模板';
  292. this.importStatus = false;
  293. },
  294. t_name_search(ev) {
  295. this.get_tpl();
  296. },
  297. handleSelectionChange(val) {
  298. this.multipleSelection = val;
  299. },
  300. save_select_host() {
  301. let hosts = [], hosts_id = [];
  302. for (let v of this.multipleSelection) {
  303. hosts.push( v.name + '(' + v.ssh_ip + ')');
  304. hosts_id.push(v.id);
  305. }
  306. this.selected_host_id = hosts_id;
  307. this.selected_host = hosts.toString();
  308. this.dialog_host_view = false;
  309. },
  310. save_select_exec_tpl() {
  311. this.exec_command = this.selectTplRow.tpl_content;
  312. this.dialog_exec_tpl_view = false;
  313. },
  314. start_exec_command () {
  315. this.dialog_exec_detail_view = true;
  316. this.exec_output = [];
  317. let data = {hosts_id: this.selected_host_id, command: this.exec_command};
  318. this.$http.post(`/api/assets/hosts_exec/exec_command`,data).then(res => {
  319. this.exec_token = res.result;
  320. this.fetchExecResult();
  321. });
  322. },
  323. fetchExecResult() {
  324. this.$http.get(`/api/common/queue/state/${this.exec_token}`).then(res => {
  325. if (res.result['complete'] === true) return;
  326. this.fetchExecResult(this.exec_token);
  327. this.exec_output.push(res.result);
  328. }, res => this.$layer_message(res.result))
  329. },
  330. delete_exec () {
  331. this.$http.delete(`/api/assets/hosts_exec/exec_command/${this.exec_token}`)
  332. .then(() => {}, res => {
  333. this.$layer_message(res.result)
  334. })
  335. },
  336. saveTplCommit () {
  337. this.editLoading = true;
  338. let request;
  339. if (this.editTpl.id) {
  340. request = this.$http.put(`/api/assets/hosts_exec/tpl/${this.editTpl.id}`, this.editTpl)
  341. } else {
  342. request = this.$http.post(`/api/assets/hosts_exec/tpl/`, this.editTpl)
  343. }
  344. request.then(() => {
  345. this.host_tpl_edit_view = false;
  346. this.$layer_message('提交成功', 'success');
  347. this.get_tpl(this.currentPage);
  348. this.get_tpl_type();
  349. this.addTpl = { tpl_name: '', tpl_desc: '', tpl_type: '', tpl_content: ''};
  350. }, res => this.$layer_message(res.result)).finally(() => this.editLoading = false)
  351. },
  352. addtplType () {
  353. this.$prompt('请输入模板类型', '提示', {
  354. inputPattern: /.+/,
  355. inputErrorMessage: '请输入类型!'
  356. }).then(({value}) => {
  357. this.editTpl.tpl_type = value
  358. }).catch(() => {
  359. })
  360. },
  361. del_exec_tpl (row) {
  362. this.$confirm('确认删除吗?', '警告', {
  363. type: 'warning'
  364. }).then(() => {
  365. this.btnDelLoading = {[row.id]: true};
  366. this.$layer_message('删除成功', 'success');
  367. this.$http.delete(`/api/assets/hosts_exec/tpl/${row.id}`)
  368. .then(() => this.get_tpl(), res => this.$layer_message(res.result))
  369. .finally(() => this.btnDelLoading = {})
  370. }).catch(() => {
  371. });
  372. },
  373. //获取区域
  374. get_tpl_type () {
  375. this.$http.get('/api/assets/hosts_exec/tpl_type').then(res => {
  376. this.tpl_options = res.result
  377. }, res => this.$layer_message(res.result))
  378. },
  379. handleSelectChange(val){
  380. this.selectTplRow = val;
  381. },
  382. handleCurrentChange(val) {
  383. this.currentPage = val;
  384. this.get_tpl(this.currentPage);
  385. },
  386. hostCurrentChange(val) {
  387. this.hostCurrentPage = val;
  388. this.get_hosts(this.hostCurrentPage);
  389. },
  390. },
  391. };
  392. </script>