InstallAgentForm.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <div>
  3. <div v-show="!installing">
  4. <a-tooltip>
  5. <template slot="title" v-if="install_failed_reason">
  6. {{ install_failed_reason }}
  7. </template>
  8. {{ installTips }}
  9. <help-link :href="peHelpLink" v-if="showPEHelpLink && showDocsLink()">PE</help-link>
  10. </a-tooltip>
  11. <a-tooltip>
  12. <template slot="title" v-if="disableTips">
  13. {{ disableTips }}
  14. </template>
  15. <a-button class="ml-2" type="link" @click="handleInstallAgent" :disabled="disable">
  16. {{ buttonText }}
  17. </a-button>
  18. </a-tooltip>
  19. </div>
  20. <div v-show="installing">
  21. {{ $t('compute.vminstance.monitor.install_agent.installing') }}
  22. <a-icon type="loading" />
  23. </div>
  24. </div>
  25. </template>
  26. <script>
  27. import _ from 'lodash'
  28. import WindowsMixin from '@/mixins/windows'
  29. import { DOCS_MAP, showDocsLink } from '@/constants/docs'
  30. export default {
  31. name: 'InstallAgentForm',
  32. mixins: [WindowsMixin],
  33. props: {
  34. data: { // listItemData
  35. type: Object,
  36. required: true,
  37. },
  38. serverColumns: {
  39. type: Array,
  40. required: true,
  41. },
  42. isPageDestroyed: Boolean,
  43. },
  44. data () {
  45. let agent_install_status
  46. const deploy = _.get(this.data, ['metadata', 'telegraf_deployed'])
  47. if (deploy) {
  48. agent_install_status = 'installed'
  49. } else if (this.data.hasOwnProperty('agent_status')) {
  50. const { agent_status } = this.data
  51. if (agent_status === 'succeed') {
  52. agent_install_status = 'installed'
  53. } else if (agent_status === 'applying') {
  54. agent_install_status = 'installing'
  55. } else if (agent_status === 'failed') {
  56. agent_install_status = 'install_failed'
  57. } else {
  58. agent_install_status = 'install'
  59. }
  60. } else {
  61. agent_install_status = 'install'
  62. }
  63. return {
  64. showDocsLink,
  65. /* install, installed, installing, install_failed */
  66. agent_install_status,
  67. }
  68. },
  69. computed: {
  70. installing () {
  71. return this.agent_install_status === 'installing'
  72. },
  73. showInstallButton () {
  74. return this.agent_install_status === 'install' || this.agent_install_status === 'install_failed'
  75. },
  76. installTips () {
  77. switch (this.agent_install_status) {
  78. case 'installed':
  79. return this.$t('compute.vminstance.monitor.install_agent.installed')
  80. case 'install_failed':
  81. switch (this.install_failed_code) {
  82. case 'Others':
  83. return this.$t('compute.vminstance.monitor.install_agent.tips_failed.others')
  84. case 'NoReachInfluxdb':
  85. return showDocsLink() ? this.$t('compute.vminstance.monitor.install_agent.tips_failed.no_reach_influxdb') : this.$t('compute.vminstance.monitor.install_agent.tips_failed.no_reach_influxdb_1')
  86. case 'ServerNotSshable':
  87. return this.$t('compute.vminstance.monitor.install_agent.tips_failed.server_not_sshable')
  88. default:
  89. return this.$t('compute.vminstance.monitor.install_agent.tips_failed.others')
  90. }
  91. case 'install':
  92. return this.$t('compute.vminstance.monitor.install_agent.tips')
  93. default:
  94. return ''
  95. }
  96. },
  97. showPEHelpLink () {
  98. return this.agent_install_status === 'install_failed' && this.install_failed_code === 'NoReachInfluxdb'
  99. },
  100. peHelpLink () {
  101. return DOCS_MAP.sshProxy()
  102. },
  103. install_failed_code () {
  104. return this.agent_install_status === 'install_failed' && this.data.agent_fail_code
  105. },
  106. install_failed_reason () {
  107. return this.agent_install_status === 'install_failed' && this.data.agent_fail_reason
  108. },
  109. disable () {
  110. if (this.data?.os_type === 'Windows') {
  111. return this.data.status !== 'ready'
  112. }
  113. return !['running', 'ready'].includes(this.data.status)
  114. },
  115. buttonText () {
  116. if (this.showInstallButton) {
  117. return this.$t('compute.vminstance.monitor.install_agent')
  118. } else {
  119. return this.$t('compute.vminstance.monitor.reinstall_agent')
  120. }
  121. },
  122. disableTips () {
  123. if (this.data?.os_type === 'Windows') {
  124. if (this.data.status !== 'ready') {
  125. return this.$t('compute.vminstance.monitor.agent_install_tooltip')
  126. }
  127. } else {
  128. if (!['running', 'ready'].includes(this.data.status)) {
  129. return this.$t('compute.text_1397')
  130. }
  131. }
  132. return ''
  133. },
  134. },
  135. watch: {
  136. 'data.agent_status': {
  137. handler: function (val, oldval) {
  138. if (oldval === 'applying' || this.agent_install_status === 'installing') {
  139. if (val === 'succeed' || val === 'failed') {
  140. this.agent_install_status = val === 'succeed' ? 'installed' : 'install_failed'
  141. this.$emit('onInstall', { status: val })
  142. }
  143. }
  144. },
  145. immediate: true,
  146. deep: true,
  147. },
  148. 'data.metadata.telegraf_deployed' (val, oldval) {
  149. if (val === 'true') {
  150. this.agent_install_status = 'installed'
  151. this.$emit('onInstall', { status: 'succeed' })
  152. }
  153. },
  154. },
  155. methods: {
  156. async handleInstallAgent (e) {
  157. if (this.data.hypervisor === 'kvm' || this.data.hypervisor === 'esxi') {
  158. if (this.data.status === 'running') {
  159. const data = {
  160. auto_choose_proxy_endpoint: true,
  161. server_id: this.data.id,
  162. }
  163. const ret = await new this.$Manager('scripts')
  164. .performAction({
  165. id: 'monitor agent',
  166. action: 'apply',
  167. data: data,
  168. })
  169. await this.handleInstallTask(ret.data.script_apply_id)
  170. return
  171. }
  172. if (this.data.status === 'ready') {
  173. const data = {
  174. deploy_telegraf: true,
  175. }
  176. await new this.$Manager('servers')
  177. .performAction({
  178. id: this.data.id,
  179. action: 'deploy',
  180. data: data,
  181. })
  182. this.agent_install_status = 'installing'
  183. const timer = setInterval(() => {
  184. this.$bus.$emit('VMInstanceListSingleRefresh', [this.data.id])
  185. if (this.data.metadata?.telegraf_deployed === 'true') {
  186. clearInterval(timer)
  187. }
  188. }, 5000)
  189. return
  190. }
  191. }
  192. this.createDialog('InstallAgentDialog', {
  193. data: [this.data],
  194. columns: this.serverColumns,
  195. callback: this.handleInstallTask,
  196. })
  197. },
  198. handleInstallTask (id) {
  199. if (!id) return
  200. this.agent_install_status = 'installing'
  201. this.$bus.$emit('agentStatusQuery', id)
  202. },
  203. // async handleInstallTask (id) {
  204. // if (!id) return
  205. // this.agent_install_status = 'installing'
  206. // try {
  207. // const params = {
  208. // script_apply_id: id,
  209. // }
  210. // let maxTry = 60
  211. // while (maxTry > 0) {
  212. // console.log('ispagedestroyed', this.isPageDestroyed)
  213. // if (this.isPageDestroyed) {
  214. // break
  215. // }
  216. // const { data: { data = [] } } = await new this.$Manager('scriptapplyrecords').list({ params: params })
  217. // if (data) {
  218. // if (data[0].status === 'succeed' || data[0].status === 'failed') {
  219. // this.agent_install_status = data[0].status === 'succeed' ? 'installed' : 'install_failed'
  220. // this.install_failed_reason = data[0].reason
  221. // this.install_failed_code = data[0].fail_code || ''
  222. // this.$emit('onInstall', data[0])
  223. // break
  224. // }
  225. // }
  226. // maxTry -= 1
  227. // console.log('maxTry', maxTry)
  228. // await new Promise(resolve => setTimeout(resolve, 6000))
  229. // }
  230. // } catch (e) {
  231. // throw e
  232. // }
  233. // },
  234. },
  235. }
  236. </script>
  237. <style scoped>
  238. </style>