UserCustom.js 18 KB

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