SetupSSHForm.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <a-form :form="form" v-bind="formItemLayout">
  3. <a-form-item :label="$t('network.ssh-proxy.port')">
  4. <a-input-number v-decorator="decorators.port" />
  5. </a-form-item>
  6. <a-form-item :label="$t('compute.vminstance.setup_ssh_authentication.method')">
  7. <a-radio-group v-model="method">
  8. <a-radio-button v-for="m in METHODS" :value="m.value" :key="m.key">{{ m.label }}</a-radio-button>
  9. </a-radio-group>
  10. </a-form-item>
  11. <a-form-item v-if="method !== 'scripts'">
  12. <span slot="label">
  13. {{ $t('compute.text_163') }}
  14. <a-tooltip :title="$t('compute.vminstance.setup_ssh_authentication.create.username.tips')">
  15. <a-icon type="question-circle-o" />
  16. </a-tooltip>
  17. </span>
  18. <a-input v-decorator="decorators.user" />
  19. </a-form-item>
  20. <a-form-item :label="$t('compute.text_108')" v-if="method === 'keypair'">
  21. <a-textarea :placeholder="$t('compute.text_797')" v-decorator="decorators.private_key" />
  22. </a-form-item>
  23. <a-form-item :label="$t('compute.text_340')" v-if="method === 'password'">
  24. <a-input-password :placeholder="$t('compute.text_204')" v-decorator="decorators.password" />
  25. </a-form-item>
  26. <a-form-item :label="$t('compute.vminstance.setup_ssh_authentication.method.script')" v-if="method === 'scripts'">
  27. <a-alert :message="$t('compute.vminstance.setup_ssh_authentication.method.script.tips')" banner />
  28. <code-mirror :value="scripts" :options="cmOptions" />
  29. <div class="text-right">
  30. <a-button
  31. type="link"
  32. icon="copy"
  33. class="pl-0 pr-0"
  34. @click="handleCopy">{{$t('compute.text_801')}}</a-button>
  35. </div>
  36. </a-form-item>
  37. </a-form>
  38. </template>
  39. <script>
  40. import 'codemirror/mode/shell/shell'
  41. export default {
  42. name: 'SetupSshForm',
  43. props: {
  44. servers: {
  45. type: Array,
  46. default: () => [],
  47. },
  48. },
  49. data () {
  50. return {
  51. method: 'keypair',
  52. METHODS: [
  53. { key: 'keypair', value: 'keypair', label: this.$t('compute.text_108') },
  54. { key: 'password', value: 'password', label: this.$t('compute.text_340') },
  55. { key: 'scripts', value: 'scripts', label: this.$t('compute.vminstance.setup_ssh_authentication.method.script') },
  56. ],
  57. scripts: '',
  58. cmOptions: {
  59. styleActiveLine: true,
  60. lineNumbers: true,
  61. lineWrapping: true,
  62. line: true,
  63. readOnly: true,
  64. theme: 'material',
  65. },
  66. formItemLayout: {
  67. wrapperCol: {
  68. span: 20,
  69. },
  70. labelCol: {
  71. span: 4,
  72. },
  73. },
  74. form: this.$form.createForm(this),
  75. decorators: {
  76. port: [
  77. 'port',
  78. {
  79. initialValue: 22,
  80. rules: [
  81. { required: true, message: this.$t('network.text_176') },
  82. { type: 'number', min: 1, max: 65535, message: this.$t('network.text_347') },
  83. ],
  84. },
  85. ],
  86. method: [
  87. 'method',
  88. {
  89. initialValue: 'keypair',
  90. },
  91. ],
  92. user: [
  93. 'user',
  94. {
  95. initialValue: '',
  96. validateTrigger: 'blur',
  97. validateFirst: true,
  98. rules: [
  99. { required: true, message: this.$t('compute.text_210') },
  100. ],
  101. },
  102. ],
  103. password: [
  104. 'password',
  105. {
  106. rules: [
  107. { required: true, message: this.$t('compute.text_204') },
  108. ],
  109. },
  110. ],
  111. private_key: [
  112. 'private_key',
  113. {
  114. rules: [
  115. { required: true, message: this.$t('compute.vminstance.setup_ssh_authentication.keypair.tips') },
  116. ],
  117. },
  118. ],
  119. },
  120. }
  121. },
  122. watch: {
  123. method (val) {
  124. if (val === 'scripts') {
  125. this.fetchScripts()
  126. }
  127. },
  128. },
  129. methods: {
  130. validateForm () {
  131. return new Promise((resolve, reject) => {
  132. this.form.validateFields((err, values) => {
  133. if (!err) {
  134. resolve(values)
  135. } else {
  136. reject(err)
  137. }
  138. })
  139. })
  140. },
  141. handleSubmitResult (ret) {
  142. const tasks = {}
  143. for (const item of ret) {
  144. if (item.data && item.data.ansible_playbook_id) {
  145. tasks[item.id] = item.data.ansible_playbook_id
  146. } else {
  147. console.log('make-sshable', item)
  148. }
  149. }
  150. this.$emit('tasks', tasks)
  151. },
  152. async submit () {
  153. try {
  154. if (!this.servers) {
  155. console.log('no servers to submit make-sshable')
  156. return
  157. }
  158. const values = await this.validateForm()
  159. if (this.method === 'scripts') {
  160. await new this.$Manager('servers').batchUpdate({ ids: this.servers, data: { ssh_port: values.port } })
  161. this.$emit('tasks', {})
  162. return
  163. }
  164. const ret = await new this.$Manager('servers').batchPerformAction({
  165. action: 'make-sshable',
  166. ids: this.servers,
  167. data: values,
  168. })
  169. if (!ret.data || !ret.data.data) {
  170. console.log('make-sshable empty result', ret)
  171. return
  172. }
  173. this.handleSubmitResult(ret.data.data)
  174. } catch (error) {
  175. throw error
  176. }
  177. },
  178. async fetchScripts () {
  179. if (!this.servers) {
  180. console.log('no servers to fetch make-sshable-cmd')
  181. return
  182. }
  183. try {
  184. this.scripts = this.$t('scope.text_184')
  185. const ret = await new this.$Manager('servers').getSpecific({ id: this.servers[0], spec: 'make-sshable-cmd' })
  186. this.scripts = ret.data.shell_cmd
  187. } catch (error) {
  188. if (error.response.status === 404) {
  189. this.scripts = error.response.data.class
  190. } else {
  191. this.scripts = this.$t('common_340')
  192. }
  193. throw error
  194. }
  195. },
  196. handleCopy () {
  197. this.$copyText(this.scripts).then(() => {
  198. this.$message.success(this.$t('compute.text_802'))
  199. }).catch(() => {
  200. this.$message.error(this.$t('compute.text_803'))
  201. })
  202. },
  203. },
  204. }
  205. </script>
  206. <style scoped>
  207. </style>