| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- import React, {Component} from 'react';
- import {Layout, Button, notification, Spin} from 'antd';
- import {getCookie} from "../../../cookie";
- import {graphqlUrl, storeFile} from "../../../config";
- import {SHOW_CLOUD} from "../../../gql";
- import {request} from 'graphql-request'
- import axios from 'axios';
- import {deployAll} from "../../../config";
- import {ADD_APIGWPATH, ADD_APIGROUP, ADD_DEPLOY, ADD_PROJECT} from "../../../gql";
- import {idGen} from "../../../func";
- const {Content} = Layout;
- axios.defaults.withCredentials = true;
- class DeployCloudChoose extends Component {
- constructor() {
- super();
- this.state = {
- userID: 'demo',
- dbKind: 'mongodb', // mongodb fc-db
- cloudName: 'tencent',
- cloudID: 'tencent_CloudID',
- secretID: '',
- secretKey: '',
- appId: '',
- bucketName: '',
- disabled: false
- }
- }
- componentWillMount() {
- this._isMounted = true;
- let userID = getCookie('user_id') || this.state.userID;
- if (userID !== undefined && userID !== '') {
- this.setState({
- userID
- });
- // 查询是否设置 cloud
- request(graphqlUrl, SHOW_CLOUD, {user_id: userID}).then(data => {
- data.cloud_by_props.forEach(cloud => {
- switch (cloud.cloudName) {
- case 'tencent':
- this.setState({
- cloudID: cloud.id,
- secretID: cloud.secretId,
- secretKey: cloud.secretKey,
- appId: cloud.appId,
- cloudName: 'tencent'
- });
- break;
- case 'aliyun':
- // 没写
- break;
- case 'amazon':
- // 没写
- break;
- default:
- break;
- }
- });
- }
- )
- }
- if (this.props.location && this.props.location.state) {
- switch (this.props.location.state.schemaID) {
- case 'order_schemaID':
- this.setState({
- bucketName: 'appointment'
- });
- break;
- case 'ecommerce_schemaID':
- this.setState({
- bucketName: 'e-commerce'
- });
- break;
- default:
- break;
- }
- }
- }
- componentWillUnmount() {
- this._isMounted = false;
- }
- deploy() {
- let _this = this;
- let {userID, dbKind, bucketName} = this.state;
- if (bucketName === '') {
- console.log('state, 没有传值');
- } else {
- // 开始调用
- console.log('开始调用');
- this.setState({
- disabled: true
- });
- let now = new Date().getTime(),
- functionName = this.state.userID + '_' + bucketName.substr(0, 2) + '_' + now,
- serviceName = this.state.userID + '_' + bucketName.substr(0, 2) + '_' + now,
- resources = [`${bucketName}/schema.edn`, `${bucketName}/resolve-map.edn`, `${bucketName}/${dbKind}/${userID}/deploy-conf.edn`];
- if (dbKind === 'mongodb') {
- resources.push(`${bucketName}/${dbKind}/${userID}/mongo-config.edn`)
- }
- console.log('now', now, 'functionName', functionName, 'serviceName', serviceName, 'resources', resources);
- axios.post(deployAll,
- {
- 'fc-name': functionName,
- 'bucket': dbKind === 'mongodb'? 'native-fc' : 'fcdb-deploy',
- 'object-file': 'fc-only.zip',
- 'res-bucket': 'case',
- 'resources': resources,
- 'service-name': serviceName,
- 'path': "/*"
- })
- .then(function (response) {
- console.log('response', response);
- if(response['data']['apigw-result'] && response['data']['fc-result']) {
- // 处理数据
- let result = response['data']['apigw-result'];
- let apiData = result['api-info'];
- let serviceData = result['service-info'];
- // 存数据
- let pathID = idGen('path'),
- groupID = idGen('group'),
- deployID = idGen('deploy'),
- projectID = idGen('project');
- let {apiId, path, method} = apiData;
- let {serviceName, serviceId, subDomain} = serviceData;
- let {userID, cloudID} = _this.state;
- let pathVarObj = {
- id: idGen('path'),
- user_id: userID,
- apiGWGroup_id: pathID,
- deploy_id: deployID,
- apiGWName: functionName,
- requestMethod: method,
- apiGWPath: path,
- apiId: apiId,
- apiGWDesc: '',
- serviceType: 'SCF',
- timeout: 300,
- createdAt: now,
- updatedAt: ''
- };
- let projectVarObj = {
- id: projectID,
- projectType: 'case',
- cloud_id: cloudID,
- user_id: userID,
- projectName: functionName,
- database_id: '',
- apiGWGroup_id: '',
- deploy_id: '',
- case_id: '',
- wxConfig_id: '',
- schema_id: '',
- createdAt: now,
- updatedAt: '',
- projectStatus: 'deployed'
- };
- let groupVarObj = {
- id: groupID,
- cloud_id: cloudID,
- user_id: userID,
- userStatus: '',
- userDomain: '',
- groupName: serviceName,
- frontType: '',
- region: '',
- environmentName: '',
- defaultDomain: subDomain,
- status: '',
- serviceId: serviceId,
- createdAt: now,
- updatedAt: ''
- };
- let deployVarObj = {
- id: deployID,
- cloud_id: cloudID,
- functionName,
- cosObjectName: '',
- region: '',
- cosBucketRegion: '',
- description: '',
- cosBucketName: '',
- vpcId: '',
- subnetId: '',
- memorySize: 512,
- timeout: 300,
- handler: '',
- serviceName: "",
- fc_id: '',
- createdAt: now,
- updatedAt: ''
- };
- let add_apigwpath = request(graphqlUrl, ADD_APIGWPATH, pathVarObj),
- add_project = request(graphqlUrl, ADD_PROJECT, projectVarObj),
- add_apigroup = request(graphqlUrl, ADD_APIGROUP, groupVarObj),
- add_deploy = request(graphqlUrl, ADD_DEPLOY, deployVarObj);
- Promise.all([add_apigwpath, add_project, add_apigroup, add_deploy])
- .then(value => {
- console.log(value);
- // 展示数据
- if(this._isMounted) {
- _this.setState({
- disabled: false
- });
- }
- _this.props.changeTabBar('my-deploy');
- _this.props.history.push({
- pathname: `/common/deploy`,
- state: {
- // 处理传回数据,直接拼接
- url: `http://${subDomain}/test/graphql`
- }
- });
- })
- .catch(err => {
- console.log(err);
- });
- } else {
- console.log('deployAll 失败');
- }
- })
- .catch(function (error) {
- console.log('axios error', error);
- });
- }
- }
- render() {
- let {userID, disabled, appId, secretID, secretKey, bucketName, dbKind} = this.state;
- return (
- <div className={'deploy-choose-cloud'}>
- <Layout style={{padding: '24px', minHeight: '300px'}}>
- <Content style={{padding: '20px 50px', background: '#fff'}}>
- <div className={'choose-cloud'}>
- <span className={'choose-cloud-title'}>系统默认设置</span><span
- className={'choose-cloud-content'}>部署在系统云服务商,部署成功后返回结果</span>
- </div>
- <div className={'choose-cloud'}>
- <span className={'choose-cloud-title'}>自定义设置</span><span
- className={'choose-cloud-content'}>部署在自有云服务商,部署成功后返回结果,可登陆云服务商后台查看部署结果</span>
- </div>
- <div className={'choose-cloud-button-group'}>
- {
- disabled ?
- <Spin/>
- :
- <div>
- <Button className={'choose-cloud-button'} type='primary' onClick={() => {
- this.deploy();
- }}>系统默认部署</Button>
- <Button className={'choose-cloud-button'} type='primary' onClick={() => {
- if (userID === 'demo') {
- notification['warning']({
- message: '需要登录',
- description: '后续使用,需要先登录',
- });
- this.props.history.push({
- pathname: `/login`
- })
- } else {
- if (appId && secretID && secretKey) {
- this.setState({
- disabled: true
- });
- // store *.edn to cos
- let deployConf = dbKind === 'mongodb' ?
- ` {:secretId "${secretID}"\n` +
- ` :secretKey "${secretKey}"\n` +
- ` :appId "${appId}"\n` +
- ' :region "ap-beijing" \n' +
- ' }\n'
- :
- ` {:secretId "${secretID}"\n` +
- ` :secretKey "${secretKey}"\n` +
- ` :appId "${appId}"\n` +
- ' :region "ap-beijing"\n' +
- ' :bucket "fc-db"\n' +
- ' :trustStore "/etc/ssl/certs/java/cacerts"\n' +
- ` :fc-db-store "save/${bucketName}.dat"\n` +
- ' :fc-db-dir "fc-db"\n' +
- ' :graphql-url "http://orderfcdb.ioobot.cn/graphql"\n' +
- ' :local-tmp-dir "/tmp"\n' +
- ' :local-db-file "fcdb.dat"\n' +
- ' :update-tx? true \n' +
- ' :force-down? true\n' +
- ' }';
- let a = axios.post(storeFile, {
- 'file-name': `${bucketName}/${dbKind}/${userID}/deploy-conf.edn`,
- bucket: 'case',
- cont: deployConf
- });
- let cont = '{:uri {\n' +
- ':auth {:admin-db "order"\n' +
- ' :u "ly"\n' +
- ' :p "autorunC1705"\n' +
- ' :host "119.27.174.13"}}\n' +
- ' :db-name "order"}';
- let b = dbKind === 'mongodb' ?
- axios.post(storeFile, {
- 'file-name': `${bucketName}/${dbKind}/${userID}/mongo-config.edn`,
- bucket: 'case',
- cont
- })
- :
- Promise.resolve({status: 200});
- Promise.all([a, b]).then(value=> {
- if(value.every(res=> res.status === 200)) {
- console.log('store file success , start deploying');
- this.deploy();
- }
- });
- } else {
- notification['warning']({
- message: '需要填写配置',
- description: '后续使用,需要先填写配置',
- });
- this.props.history.push({
- pathname: `/login/cloud`
- })
- }
- }
- }}>自定义参数设置</Button>
- </div>
- }
- </div>
- </Content>
- </Layout>
- </div>
- )
- }
- }
- export default DeployCloudChoose
|