UserCustom.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. import React, {Component} from 'react';
  2. import {deployAll, graphqlUrl, storeFile} from "../../../config";
  3. import {ADD_APIGROUP, ADD_APIGWPATH, ADD_DEPLOY, ADD_PROJECT, SHOW_CLOUD} from "../../../gql";
  4. import {CloudConfig} from "../../../login/CloudConfig";
  5. import {Button, notification, Spin, Input, Radio, Modal, Icon} from 'antd';
  6. import {FormattedMessage} from 'react-intl';
  7. import {request} from 'graphql-request'
  8. import {idGen} from "../../../func";
  9. import axios from 'axios';
  10. const RadioGroup = Radio.Group;
  11. axios.defaults.withCredentials = true;
  12. class UserCustom extends Component {
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. userID: props.userID,
  17. bucketName: props.bucketName,
  18. customName: '',
  19. cloudName: 'tencent',
  20. dbKind: 'fc-db', // mongodb fc-db
  21. check: 0,
  22. disableDeployButton: false,
  23. };
  24. this.cloudState = {
  25. cloudID: '',
  26. secretID: '',
  27. secretKey: '',
  28. appId: ''
  29. }
  30. }
  31. getCloudDetail = (cloudID, secretID, secretKey, appId) => {
  32. this.cloudState = {
  33. cloudID,
  34. secretID,
  35. secretKey,
  36. appId
  37. }
  38. };
  39. deploy = () => {
  40. this._isMounted = true;
  41. let _this = this;
  42. let {userID, dbKind, bucketName, customName} = this.state;
  43. let {cloudID} = this.cloudState;
  44. if (bucketName === '') {
  45. console.log('state, 没有传值');
  46. } else {
  47. // 开始调用
  48. console.log('开始调用');
  49. this.setState({
  50. disableDeployButton: true
  51. });
  52. let now = new Date().getTime(),
  53. functionName = userID + '_' + customName,
  54. serviceName = userID + '_' + customName,
  55. resources = [`${bucketName}/schema.edn`, `${bucketName}/resolve-map.edn`, `${bucketName}/${dbKind}/${userID}/deploy-conf.edn`];
  56. if (dbKind === 'mongodb') {
  57. resources.push(`${bucketName}/${dbKind}/${userID}/mongo-config.edn`)
  58. }
  59. console.log('now', now, 'functionName', functionName, 'serviceName', serviceName, 'resources', resources);
  60. axios.post(deployAll,
  61. {
  62. 'fc-name': functionName,
  63. 'bucket': dbKind === 'mongodb' ? 'native-fc' : 'fcdb-deploy',
  64. 'object-file': 'fc-only.zip',
  65. 'res-bucket': 'case',
  66. 'resources': resources,
  67. 'service-name': serviceName,
  68. 'path': "/*"
  69. })
  70. .then(function (response) {
  71. console.log('response', response);
  72. if (response['data']['apigw-result'] && response['data']['fc-result']) {
  73. // 处理数据
  74. let result = response['data']['apigw-result'];
  75. let apiData = result['api-info'];
  76. let serviceData = result['service-info'];
  77. // 存数据
  78. let pathID = idGen('path'),
  79. groupID = idGen('group'),
  80. deployID = idGen('deploy'),
  81. projectID = idGen('project');
  82. let {apiId, path, method} = apiData;
  83. let {serviceName, serviceId, subDomain} = serviceData;
  84. let pathVarObj = {
  85. id: idGen('path'),
  86. user_id: userID,
  87. apiGWGroup_id: pathID,
  88. deploy_id: deployID,
  89. apiGWName: functionName,
  90. requestMethod: method,
  91. apiGWPath: path,
  92. apiId: apiId,
  93. apiGWDesc: '',
  94. serviceType: 'SCF',
  95. timeout: 300,
  96. createdAt: now,
  97. updatedAt: ''
  98. };
  99. let projectVarObj = {
  100. id: projectID,
  101. projectType: 'case',
  102. cloud_id: cloudID,
  103. user_id: userID,
  104. projectName: functionName,
  105. database_id: '',
  106. apiGWGroup_id: '',
  107. deploy_id: '',
  108. case_id: '',
  109. wxConfig_id: '',
  110. schema_id: '',
  111. createdAt: now,
  112. updatedAt: '',
  113. projectStatus: 'deployed'
  114. };
  115. let groupVarObj = {
  116. id: groupID,
  117. cloud_id: cloudID,
  118. user_id: userID,
  119. userStatus: '',
  120. userDomain: '',
  121. groupName: serviceName,
  122. frontType: '',
  123. region: '',
  124. environmentName: '',
  125. defaultDomain: subDomain,
  126. status: '',
  127. serviceId: serviceId,
  128. createdAt: now,
  129. updatedAt: ''
  130. };
  131. let deployVarObj = {
  132. id: deployID,
  133. cloud_id: cloudID,
  134. functionName,
  135. cosObjectName: '',
  136. region: '',
  137. cosBucketRegion: '',
  138. description: '',
  139. cosBucketName: '',
  140. vpcId: '',
  141. subnetId: '',
  142. memorySize: 512,
  143. timeout: 300,
  144. handler: '',
  145. serviceName: "",
  146. fc_id: '',
  147. createdAt: now,
  148. updatedAt: ''
  149. };
  150. let add_apigwpath = request(graphqlUrl, ADD_APIGWPATH, pathVarObj),
  151. add_project = request(graphqlUrl, ADD_PROJECT, projectVarObj),
  152. add_apigroup = request(graphqlUrl, ADD_APIGROUP, groupVarObj),
  153. add_deploy = request(graphqlUrl, ADD_DEPLOY, deployVarObj);
  154. Promise.all([add_apigwpath, add_project, add_apigroup, add_deploy])
  155. .then(value => {
  156. console.log(value);
  157. // 展示数据
  158. if (_this._isMounted) {
  159. _this.setState({
  160. disableDeployButton: false
  161. });
  162. }
  163. _this.props.changeTabBar('my-deploy');
  164. _this.props.history.push({
  165. pathname: `/common/deploy`,
  166. state: {
  167. // 处理传回数据,直接拼接
  168. url: `http://${subDomain}/test/graphql`
  169. }
  170. });
  171. })
  172. .catch(err => {
  173. console.log(err);
  174. });
  175. } else {
  176. console.log('deployAll 失败');
  177. }
  178. })
  179. .catch(function (error) {
  180. console.log('axios error', error);
  181. });
  182. }
  183. };
  184. componentWillMount() {
  185. this._isMounted = true;
  186. }
  187. componentWillUnmount() {
  188. this._isMounted = false;
  189. }
  190. render() {
  191. let {cloudName, customName, disableDeployButton, userID, check, dbKind, bucketName} = this.state;
  192. let {appId, secretID, secretKey} = this.cloudState;
  193. return (
  194. <div>
  195. <div className="column-menu" onClick={() => {
  196. this.props.backToMe()
  197. }}>
  198. <Icon type="left" style={{color: '#3187FA'}}/> <FormattedMessage id="back to case show"/>
  199. </div>
  200. <div>
  201. <div className={'schema-name'}><FormattedMessage id='Name'/></div>
  202. <div>
  203. <span className='item-title-cloud'><FormattedMessage id='name'/>:</span>
  204. <Input
  205. style={{width: 250}}
  206. value={customName}
  207. onChange={(e) => {
  208. this.setState({customName: e.target.value})
  209. }}/>
  210. </div>
  211. </div>
  212. <div style={{marginTop: 20}}>
  213. <div className={'schema-name'}><FormattedMessage id='Cloud Setting'/></div>
  214. <RadioGroup
  215. onChange={(e) => {
  216. this.setState({cloudName: e.target.value})
  217. }}
  218. value={cloudName}
  219. >
  220. <Radio value={'tencent'}><FormattedMessage id='tencent cloud'/></Radio>
  221. <Radio value={'aliyun'}><FormattedMessage id='aliyun'/></Radio>
  222. <Radio value={'amazon'}><FormattedMessage id='amazon cloud'/></Radio>
  223. </RadioGroup>
  224. {/*该处设置 check 属性,就是为了:当点击取消后,能够再次查询*/}
  225. <CloudQueryAndConfig
  226. userID={userID}
  227. getCloudDetail={this.getCloudDetail}
  228. cloudName={cloudName}
  229. check={check}
  230. reCheck={() => {
  231. this.setState({check: check + 1})
  232. }}/>
  233. </div>
  234. <div style={{marginTop: 20}}>
  235. <div className={'schema-name'}><FormattedMessage id='DB Choose'/></div>
  236. <RadioGroup
  237. onChange={(e) => {
  238. this.setState({dbKind: e.target.value})
  239. }}
  240. value={this.state.dbKind}
  241. >
  242. <Radio value='fc-db'>fc-db</Radio>
  243. <Radio value='mongodb'>mongodb</Radio>
  244. </RadioGroup>
  245. </div>
  246. {
  247. disableDeployButton ?
  248. <Spin/>
  249. :
  250. <Button style={{marginTop: 20}} type='primary' onClick={() => {
  251. if (appId && secretID && secretKey) {
  252. this.setState({
  253. disableDeployButton: true
  254. });
  255. // store *.edn to cos
  256. let deployConf = dbKind === 'mongodb' ?
  257. ` {:secretId "${secretID}"\n` +
  258. ` :secretKey "${secretKey}"\n` +
  259. ` :appId "${appId}"\n` +
  260. ' :region "ap-beijing" \n' +
  261. ' }\n'
  262. :
  263. ` {:secretId "${secretID}"\n` +
  264. ` :secretKey "${secretKey}"\n` +
  265. ` :appId "${appId}"\n` +
  266. ' :region "ap-beijing"\n' +
  267. ' :bucket "fc-db"\n' +
  268. ' :trustStore "/etc/ssl/certs/java/cacerts"\n' +
  269. ` :fc-db-store "save/${bucketName}.dat"\n` +
  270. ' :fc-db-dir "fc-db"\n' +
  271. ' :graphql-url "http://orderfcdb.ioobot.cn/graphql"\n' +
  272. ' :local-tmp-dir "/tmp"\n' +
  273. ' :local-db-file "fcdb.dat"\n' +
  274. ' :update-tx? true \n' +
  275. ' :force-down? true\n' +
  276. ' }';
  277. let a = axios.post(storeFile, {
  278. 'file-name': `${bucketName}/${dbKind}/${userID}/deploy-conf.edn`,
  279. bucket: 'case',
  280. cont: deployConf
  281. });
  282. let cont = '{:uri {\n' +
  283. ':auth {:admin-db "order"\n' +
  284. ' :u "ly"\n' +
  285. ' :p "autorunC1705"\n' +
  286. ' :host "119.27.174.13"}}\n' +
  287. ' :db-name "order"}';
  288. let b = dbKind === 'mongodb' ?
  289. axios.post(storeFile, {
  290. 'file-name': `${bucketName}/${dbKind}/${userID}/mongo-config.edn`,
  291. bucket: 'case',
  292. cont
  293. })
  294. :
  295. Promise.resolve({status: 200});
  296. Promise.all([a, b]).then(value => {
  297. if (value.every(res => res.status === 200)) {
  298. console.log('store file success , start deploying');
  299. this.deploy();
  300. }
  301. });
  302. } else {
  303. notification['warning']({
  304. message: '需要填写配置',
  305. description: '后续使用,需要先填写配置',
  306. });
  307. this.props.history.push({
  308. pathname: `/login/cloud`
  309. })
  310. }
  311. }}>部署</Button>
  312. }
  313. </div>
  314. )
  315. }
  316. }
  317. export default UserCustom;
  318. class CloudQueryAndConfig extends Component {
  319. constructor(props) {
  320. super(props);
  321. this.state = {
  322. cloudName: props.cloudName,
  323. userID: props.userID,
  324. cloudID: '',
  325. secretID: '',
  326. secretKey: '',
  327. appId: '',
  328. showCloudConfigTip: false,
  329. visible: false,
  330. confirmLoading: false,
  331. }
  332. }
  333. searchCloud = () => {
  334. let {userID, cloudName} = this.state;
  335. // 如果登录,查询该用户是否设置 cloud
  336. request(graphqlUrl, SHOW_CLOUD, {user_id: userID}).then(data => {
  337. let clouds = data.cloud_by_props.filter(cloud => cloud.cloudName === cloudName);
  338. // 如果限制一个云服务商一个 cloud,那么就是clouds[0]
  339. if (clouds.length === 1) {
  340. let cloud = clouds[0];
  341. let {id, secretId, secretKey, appId} = cloud;
  342. this.setState({
  343. cloudID: id,
  344. secretID: secretId,
  345. secretKey,
  346. appId
  347. });
  348. this.props.getCloudDetail(id, secretId, secretKey, appId);
  349. this.setState({
  350. showCloudConfigTip: false
  351. })
  352. } else if (clouds.length > 1) {
  353. console.log('数据库有多个同一云服务商的 key');
  354. } else {
  355. this.setState({
  356. showCloudConfigTip: true
  357. })
  358. }
  359. }
  360. )
  361. };
  362. componentWillMount() {
  363. this.searchCloud()
  364. }
  365. componentWillReceiveProps(next) {
  366. this.setState({
  367. cloudName: next.cloudName,
  368. userID: next.userID,
  369. }, this.searchCloud);
  370. }
  371. showModal = () => {
  372. this.setState({
  373. visible: true,
  374. });
  375. };
  376. handleCancel = () => {
  377. this.setState({
  378. visible: false,
  379. });
  380. this.props.reCheck();
  381. };
  382. render() {
  383. let {showCloudConfigTip, visible, confirmLoading} = this.state;
  384. return (
  385. <div>
  386. {
  387. showCloudConfigTip ?
  388. <div>
  389. <Button type="danger" size="small" onClick={this.showModal}>
  390. <FormattedMessage id='no this cloud, click to save'/>
  391. </Button>
  392. <Modal
  393. title="设置 cloud"
  394. visible={visible}
  395. confirmLoading={confirmLoading}
  396. footer={null}
  397. onCancel={this.handleCancel}
  398. >
  399. <CloudConfig/>
  400. </Modal>
  401. </div>
  402. :
  403. ''
  404. }
  405. </div>
  406. )
  407. }
  408. }