Explorar el Código

create and stream ecr provisioning logs

sunguroku hace 5 años
padre
commit
0be4a76b56

+ 4 - 2
dashboard/src/main/home/Home.tsx

@@ -28,6 +28,7 @@ type StateType = {
   forceSidebar: boolean,
   showWelcome: boolean,
   currentView: string,
+  viewData: any,
 
   // Track last project id for refreshing clusters on project change
   prevProjectId: number | null,
@@ -39,6 +40,7 @@ export default class Home extends Component<PropsType, StateType> {
     showWelcome: false,
     currentView: 'dashboard',
     prevProjectId: null as number | null,
+    viewData: null as any
   }
 
   // Possibly consolidate into context (w/ ProjectSection + NewProject)
@@ -122,10 +124,10 @@ export default class Home extends Component<PropsType, StateType> {
       return <Integrations />;
     } else if (currentView === 'new-project') {
       return (
-        <NewProject setCurrentView={(x: string) => this.setState({ currentView: x })} />
+        <NewProject setCurrentView={(x: string, data: any ) => this.setState({ currentView: x, viewData: data })} />
       );
     } else if (currentView === 'provisioner') {
-      return <Provisioner />
+      return <Provisioner viewData={this.state.viewData}/>
     }
 
     return (

+ 1 - 1
dashboard/src/main/home/cluster-dashboard/expanded-chart/status/Logs.tsx

@@ -43,7 +43,7 @@ export default class Logs extends Component<PropsType, StateType> {
     if (!selectedPod.metadata?.name) return
     let protocol = process.env.NODE_ENV == 'production' ? 'wss' : 'ws'
     let ws = new WebSocket(`${protocol}://${process.env.API_SERVER}/api/projects/${currentProject.id}/k8s/${selectedPod?.metadata?.namespace}/pod/${selectedPod?.metadata?.name}/logs?cluster_id=${currentCluster.id}&service_account_id=${currentCluster.service_account_id}`)
-    // let ws = new WebSocket(`${protocol}://${process.env.API_SERVER}/api/projects/${currentProject.id}/provisioning/ecr/abcdef/logs?cluster_id=${currentCluster.id}`)
+    
     this.setState({ ws }, () => {
       if (!this.state.ws) return;
   

+ 26 - 3
dashboard/src/main/home/new-project/NewProject.tsx

@@ -16,7 +16,7 @@ import SaveButton from '../../../components/SaveButton';
 const providers = ['aws', 'gcp', 'do',];
 
 type PropsType = {
-  setCurrentView: (x: string) => void,
+  setCurrentView: (x: string, data: any) => void,
 };
 
 type StateType = {
@@ -195,6 +195,8 @@ export default class NewProject extends Component<PropsType, StateType> {
 
   createProject = () => {
     this.setState({ status: 'loading' });
+    let { awsAccessId, awsSecretKey, awsRegion } = this.state;
+
     api.createProject('<token>', {
       name: this.state.projectName
     }, {}, (err: any, res: any) => {
@@ -212,9 +214,30 @@ export default class NewProject extends Component<PropsType, StateType> {
               this.context.setCurrentProject(proj);
 
               if (this.state.selectedProvider === 'aws') {
-                this.props.setCurrentView('provisioner');
+
+                api.createAWSIntegration('<token>', {
+                  aws_region: awsRegion,
+                  aws_access_key_id: awsAccessId,
+                  aws_secret_access_key: awsSecretKey,
+                }, { id: proj.id }, (err2: any, res2: any) => {
+                  if (err2) {
+                    console.log(err2);
+                  } else {
+                    api.provisionECR('<token>', {
+                      aws_integration_id: res2.data.id,
+                      ecr_name: `${proj.name}-registry`
+                    }, {id: proj.id}, (err3: any, res3:any) => {
+                      if (err3) {
+                        console.log(err3)
+                      } else {
+                        this.props.setCurrentView('provisioner', {infra_id: res3.data.id, kind: res3.data.kind});
+                      }
+                    })
+                  }
+                });
+
               } else {
-                this.props.setCurrentView('dashboard');
+                this.props.setCurrentView('dashboard', null);
               }
             } 
           }

+ 36 - 1
dashboard/src/main/home/new-project/Provisioner.tsx

@@ -9,10 +9,12 @@ import loading from '../../../assets/loading.gif';
 import Helper from '../../../components/values-form/Helper';
 
 type PropsType = {
+  viewData: any,
 };
 
 type StateType = {
   logs: string[],
+  ws: any
 };
 
 const loadMax = 40;
@@ -20,10 +22,43 @@ const loadMax = 40;
 export default class Provisioner extends Component<PropsType, StateType> {
   state = {
     logs: [] as string[],
+    ws : null as any
+  }
+
+  scrollToBottom = () => {
+    this.scrollRef.current.scrollTop = this.scrollRef.current.scrollHeight
   }
 
   componentDidMount() {
-    this.setState({ logs: ['test-1', 'test-2'] });
+    let { currentProject } = this.context;
+    let protocol = process.env.NODE_ENV == 'production' ? 'wss' : 'ws'
+    let ws = new WebSocket(`${protocol}://${process.env.API_SERVER}/api/projects/${currentProject.id}/provisioning/${this.props.viewData.kind}/${this.props.viewData.infra_id}/logs`)
+    
+    this.setState({ ws }, () => {
+      if (!this.state.ws) return;
+  
+      this.state.ws.onopen = () => {
+        console.log('connected to websocket')
+      }
+  
+      this.state.ws.onmessage = (evt: MessageEvent) => {
+        this.setState({ logs: [...this.state.logs, evt.data] }, () => {
+          this.scrollToBottom()
+        })
+      }
+  
+      this.state.ws.onerror = (err: ErrorEvent) => {
+        console.log(err)
+      }
+    })
+
+    this.setState({ logs: [] });
+  }
+
+  componentWillUnmount() {
+    if (this.state.ws) {
+      this.state.ws.close()
+    }
   }
 
   scrollRef = React.createRef<HTMLDivElement>();

+ 9 - 0
dashboard/src/shared/api.tsx

@@ -216,6 +216,14 @@ const createAWSIntegration = baseApi<{
   return `/api/projects/${pathParams.id}/integrations/aws`;
 });
 
+const provisionECR = baseApi<{
+  ecr_name: string,
+  aws_integration_id: string,
+}, { id: number }>('POST', pathParams => {
+  return `/api/projects/${pathParams.id}/provision/ecr`;
+});
+
+
 const createECR = baseApi<{
   name: string,
   aws_integration_id: string,
@@ -292,6 +300,7 @@ export default {
   getProjectRegistries,
   getProjectRepos,
   createAWSIntegration,
+  provisionECR,
   createECR,
   getImageRepos,
   getImageTags,

+ 1 - 1
internal/config/redis.go

@@ -3,5 +3,5 @@ package config
 // RedisConf is the redis config required for the provisioner container
 type RedisConf struct {
 	Host string `env:"REDIS_HOST,default=redis"`
-	Port string `env:"REDIS_PORT,default=5432"`
+	Port string `env:"REDIS_PORT,default=6379"`
 }