UserCustom.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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={'aws'}><FormattedMessage id='amazon cloud'/></Radio>
  223. <Radio value={'huawei'}><FormattedMessage id='Huawei'/></Radio>
  224. </RadioGroup>
  225. {/*该处设置 check 属性,就是为了:当点击取消后,能够再次查询*/}
  226. <CloudQueryAndConfig
  227. userID={userID}
  228. getCloudDetail={this.getCloudDetail}
  229. cloudName={cloudName}
  230. check={check}
  231. reCheck={() => {
  232. this.setState({check: check + 1})
  233. }}/>
  234. </div>
  235. <div style={{marginTop: 20}}>
  236. <div className={'schema-name'}><FormattedMessage id='DB Choose'/></div>
  237. <RadioGroup
  238. onChange={(e) => {
  239. this.setState({dbKind: e.target.value})
  240. }}
  241. value={this.state.dbKind}
  242. >
  243. <Radio value='fc-db'>fc-db</Radio>
  244. <Radio value='mongodb'>mongodb</Radio>
  245. </RadioGroup>
  246. </div>
  247. {
  248. disableDeployButton ?
  249. <Spin/>
  250. :
  251. <Button style={{marginTop: 20}} type='primary' onClick={() => {
  252. if (appId && secretID && secretKey) {
  253. this.setState({
  254. disableDeployButton: true
  255. });
  256. // store *.edn to cos
  257. let deployConf = dbKind === 'mongodb' ?
  258. ` {:secretId "${secretID}"\n` +
  259. ` :secretKey "${secretKey}"\n` +
  260. ` :appId "${appId}"\n` +
  261. ' :region "ap-beijing" \n' +
  262. ' }\n'
  263. :
  264. ` {:secretId "${secretID}"\n` +
  265. ` :secretKey "${secretKey}"\n` +
  266. ` :appId "${appId}"\n` +
  267. ' :region "ap-beijing"\n' +
  268. ' :bucket "fc-db"\n' +
  269. ' :trustStore "/etc/ssl/certs/java/cacerts"\n' +
  270. ` :fc-db-store "save/${bucketName}.dat"\n` +
  271. ' :fc-db-dir "fc-db"\n' +
  272. ' :graphql-url "http://orderfcdb.ioobot.cn/graphql"\n' +
  273. ' :local-tmp-dir "/tmp"\n' +
  274. ' :local-db-file "fcdb.dat"\n' +
  275. ' :update-tx? true \n' +
  276. ' :force-down? true\n' +
  277. ' }';
  278. let a = axios.post(storeFile, {
  279. 'file-name': `${bucketName}/${dbKind}/${userID}/deploy-conf.edn`,
  280. bucket: 'case',
  281. cont: deployConf
  282. });
  283. let cont = '{:uri {\n' +
  284. ':auth {:admin-db "order"\n' +
  285. ' :u "ly"\n' +
  286. ' :p "autorunC1705"\n' +
  287. ' :host "119.27.174.13"}}\n' +
  288. ' :db-name "order"}';
  289. let b = dbKind === 'mongodb' ?
  290. axios.post(storeFile, {
  291. 'file-name': `${bucketName}/${dbKind}/${userID}/mongo-config.edn`,
  292. bucket: 'case',
  293. cont
  294. })
  295. :
  296. Promise.resolve({status: 200});
  297. Promise.all([a, b]).then(value => {
  298. if (value.every(res => res.status === 200)) {
  299. console.log('store file success , start deploying');
  300. this.deploy();
  301. }
  302. });
  303. } else {
  304. notification['warning']({
  305. message: '需要填写配置',
  306. description: '后续使用,需要先填写配置',
  307. });
  308. this.props.history.push({
  309. pathname: `/login/cloud`
  310. })
  311. }
  312. }}>部署</Button>
  313. }
  314. </div>
  315. )
  316. }
  317. }
  318. export default UserCustom;
  319. class CloudQueryAndConfig extends Component {
  320. constructor(props) {
  321. super(props);
  322. this.state = {
  323. cloudName: props.cloudName,
  324. userID: props.userID,
  325. cloudID: '',
  326. secretID: '',
  327. secretKey: '',
  328. appId: '',
  329. showCloudConfigTip: false,
  330. visible: false,
  331. confirmLoading: false,
  332. }
  333. }
  334. searchCloud = () => {
  335. this._isMounted = true;
  336. let {userID, cloudName} = this.state;
  337. // 如果登录,查询该用户是否设置 cloud
  338. request(graphqlUrl, SHOW_CLOUD, {user_id: userID}).then(data => {
  339. let clouds = data.cloud_by_props.filter(cloud => cloud.cloudName === cloudName);
  340. // 如果限制一个云服务商一个 cloud,那么就是clouds[0]
  341. if (clouds.length === 1) {
  342. let cloud = clouds[0];
  343. let {id, secretId, secretKey, appId} = cloud;
  344. if(this._isMounted) {
  345. this.setState({
  346. cloudID: id,
  347. secretID: secretId,
  348. secretKey,
  349. appId,
  350. showCloudConfigTip: false
  351. });
  352. }
  353. this.props.getCloudDetail(id, secretId, secretKey, appId);
  354. } else if (clouds.length > 1) {
  355. console.log('数据库有多个同一云服务商的 key');
  356. } else {
  357. if(this._isMounted) {
  358. this.setState({
  359. showCloudConfigTip: true
  360. })
  361. }
  362. }
  363. }
  364. )
  365. };
  366. showModal = () => {
  367. this.setState({
  368. visible: true,
  369. });
  370. };
  371. handleCancel = () => {
  372. this.setState({
  373. visible: false,
  374. });
  375. this.props.reCheck();
  376. };
  377. componentWillMount() {
  378. this.searchCloud();
  379. this._isMounted = true;
  380. }
  381. componentWillUnmount() {
  382. this._isMounted = false;
  383. }
  384. componentWillReceiveProps(next) {
  385. this.setState({
  386. cloudName: next.cloudName,
  387. userID: next.userID,
  388. }, this.searchCloud);
  389. }
  390. render() {
  391. let {showCloudConfigTip, visible, confirmLoading, cloudName} = this.state;
  392. return (
  393. <div>
  394. {
  395. showCloudConfigTip ?
  396. <div>
  397. <Button type="danger" size="small" onClick={this.showModal}>
  398. <FormattedMessage id='no this cloud, click to save'/>
  399. </Button>
  400. <Modal
  401. title="设置 cloud"
  402. visible={visible}
  403. confirmLoading={confirmLoading}
  404. footer={null}
  405. onCancel={this.handleCancel}
  406. >
  407. <CloudConfig cloudName={cloudName}/>
  408. </Modal>
  409. </div>
  410. :
  411. ''
  412. }
  413. </div>
  414. )
  415. }
  416. }