|
@@ -1,6 +1,5 @@
|
|
|
-import axios from 'axios';
|
|
|
import cockpit from "cockpit";
|
|
|
-import React, { useEffect, useState } from 'react';
|
|
|
+import React from 'react';
|
|
|
import { Badge, Button, Card, Col, Row, Table } from 'react-bootstrap';
|
|
|
import { useNavigate } from 'react-router-dom';
|
|
|
|
|
@@ -8,106 +7,106 @@ const _ = cockpit.gettext;
|
|
|
|
|
|
const AppContainer = (props): React$Element<React$FragmentType> => {
|
|
|
const navigate = useNavigate(); //用于页面跳转
|
|
|
- const [containersInfo, setContainersInfo] = useState([]);
|
|
|
- const customer_name = props.data.customer_name;
|
|
|
- const [endpointsId, setEndpointsId] = useState(null);
|
|
|
+ const containersInfo = props.containersInfo;
|
|
|
+ const customer_name = props.customer_name;
|
|
|
+ const endpointsId = props.endpointsId;
|
|
|
|
|
|
//通过Portainer的接口获取容器数据
|
|
|
- const getContainersData = async () => {
|
|
|
- try {
|
|
|
- let jwt = window.localStorage.getItem("portainer.JWT2"); //获取存储在本地的JWT数据
|
|
|
- let id = null;
|
|
|
+ // const getContainersData = async () => {
|
|
|
+ // try {
|
|
|
+ // let jwt = window.localStorage.getItem("portainer.JWT"); //获取存储在本地的JWT数据
|
|
|
+ // let id = null;
|
|
|
|
|
|
- //如果获取不到jwt,则模拟登录并写入新的jwt
|
|
|
- if (jwt === null) {
|
|
|
- const response = await axios.get('./config.json'); //从项目下读取配置文件
|
|
|
- if (response.status === 200) {
|
|
|
- let config = response.data.PORTAINER;
|
|
|
- const { PORTAINER_USERNAME, PORTAINER_PASSWORD, PORTAINER_AUTH_URL, PORTAINER_HOME_PAGE } = config;
|
|
|
+ // //如果获取不到jwt,则模拟登录并写入新的jwt
|
|
|
+ // if (jwt === null) {
|
|
|
+ // const response = await axios.get('./config.json'); //从项目下读取配置文件
|
|
|
+ // if (response.status === 200) {
|
|
|
+ // let config = response.data.PORTAINER;
|
|
|
+ // const { PORTAINER_USERNAME, PORTAINER_PASSWORD, PORTAINER_AUTH_URL, PORTAINER_HOME_PAGE } = config;
|
|
|
|
|
|
- //调用portainer的登录API,模拟登录
|
|
|
- const authResponse = await axios.post(PORTAINER_AUTH_URL, {
|
|
|
- username: PORTAINER_USERNAME,
|
|
|
- password: PORTAINER_PASSWORD
|
|
|
- });
|
|
|
- if (authResponse.status === 200) {
|
|
|
- jwt = "\"" + authResponse.data.jwt + "\"";
|
|
|
- //jwt = authResponse.data.jwt
|
|
|
- window.localStorage.setItem('portainer\.JWT2', jwt); //关键是将通过API登录后获取的jwt,存储到本地localStorage
|
|
|
- } else {
|
|
|
- console.error('Error:', authResponse);
|
|
|
- }
|
|
|
- }
|
|
|
- else {
|
|
|
- console.error('Error:', response);
|
|
|
- }
|
|
|
- }
|
|
|
+ // //调用portainer的登录API,模拟登录
|
|
|
+ // const authResponse = await axios.post(PORTAINER_AUTH_URL, {
|
|
|
+ // username: PORTAINER_USERNAME,
|
|
|
+ // password: PORTAINER_PASSWORD
|
|
|
+ // });
|
|
|
+ // if (authResponse.status === 200) {
|
|
|
+ // jwt = "\"" + authResponse.data.jwt + "\"";
|
|
|
+ // //jwt = authResponse.data.jwt
|
|
|
+ // window.localStorage.setItem('portainer\.JWT', jwt); //关键是将通过API登录后获取的jwt,存储到本地localStorage
|
|
|
+ // } else {
|
|
|
+ // console.error('Error:', authResponse);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // else {
|
|
|
+ // console.error('Error:', response);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
|
|
|
- //从portainer接口获取endpoints
|
|
|
- const endpointsData = await axios.get('/portainer/api/endpoints', {
|
|
|
- headers: {
|
|
|
- 'Authorization': 'Bearer ' + jwt.replace(/"/g, '')
|
|
|
- }
|
|
|
- });
|
|
|
- if (endpointsData.status === 200) {
|
|
|
- //先判断是否获取了“本地”endpoint
|
|
|
- if (endpointsData.data.length == 0) { //没有“本地”endpoint
|
|
|
- //调用添加"本地"环境的接口
|
|
|
- const addEndpoint = await axios.post('/portainer/api/endpoints', {},
|
|
|
- {
|
|
|
- params: {
|
|
|
- Name: "websoft9-local",
|
|
|
- EndpointCreationType: 1
|
|
|
- },
|
|
|
- headers: {
|
|
|
- 'Authorization': 'Bearer ' + jwt.replace(/"/g, '')
|
|
|
- }
|
|
|
- }
|
|
|
- );
|
|
|
- if (addEndpoint.status === 200) {
|
|
|
- id = addEndpoint.data?.Id;
|
|
|
- setEndpointsId(id);
|
|
|
- }
|
|
|
- else {
|
|
|
- console.error('Error:', addEndpoint);
|
|
|
- }
|
|
|
- }
|
|
|
- else {
|
|
|
- //应该可能会返回“远程”的endpoint,这里只获取“本地”endpoint,条件为URL包含'/var/run/docker.sock'
|
|
|
- id = endpointsData.data.find(({ URL }) => URL.includes('/var/run/docker.sock')).Id;
|
|
|
- setEndpointsId(id);
|
|
|
- }
|
|
|
+ // //从portainer接口获取endpoints
|
|
|
+ // const endpointsData = await axios.get('/portainer/api/endpoints', {
|
|
|
+ // headers: {
|
|
|
+ // 'Authorization': 'Bearer ' + jwt.replace(/"/g, '')
|
|
|
+ // }
|
|
|
+ // });
|
|
|
+ // if (endpointsData.status === 200) {
|
|
|
+ // //先判断是否获取了“本地”endpoint
|
|
|
+ // if (endpointsData.data.length == 0) { //没有“本地”endpoint
|
|
|
+ // //调用添加"本地"环境的接口
|
|
|
+ // const addEndpoint = await axios.post('/portainer/api/endpoints', {},
|
|
|
+ // {
|
|
|
+ // params: {
|
|
|
+ // Name: "websoft9-local",
|
|
|
+ // EndpointCreationType: 1
|
|
|
+ // },
|
|
|
+ // headers: {
|
|
|
+ // 'Authorization': 'Bearer ' + jwt.replace(/"/g, '')
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // );
|
|
|
+ // if (addEndpoint.status === 200) {
|
|
|
+ // id = addEndpoint.data?.Id;
|
|
|
+ // setEndpointsId(id);
|
|
|
+ // }
|
|
|
+ // else {
|
|
|
+ // console.error('Error:', addEndpoint);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // else {
|
|
|
+ // //应该可能会返回“远程”的endpoint,这里只获取“本地”endpoint,条件为URL包含'/var/run/docker.sock'
|
|
|
+ // id = endpointsData.data.find(({ URL }) => URL.includes('/var/run/docker.sock')).Id;
|
|
|
+ // setEndpointsId(id);
|
|
|
+ // }
|
|
|
|
|
|
- //调用接口获取
|
|
|
- const containersData = await axios.get(`/portainer/api/endpoints/${id}/docker/containers/json`, {
|
|
|
- headers: {
|
|
|
- 'Authorization': 'Bearer ' + jwt.replace(/"/g, '')
|
|
|
- },
|
|
|
- params: {
|
|
|
- all: true,
|
|
|
- filters: JSON.stringify({ "label": [`com.docker.compose.project=${customer_name}`] })
|
|
|
- }
|
|
|
- })
|
|
|
- if (containersData.status === 200) {
|
|
|
- setContainersInfo(containersData.data);
|
|
|
- }
|
|
|
- else {
|
|
|
- console.error('Error:', containersData);
|
|
|
- }
|
|
|
- }
|
|
|
- else {
|
|
|
- console.error('Error:', endpointsData);
|
|
|
- }
|
|
|
- }
|
|
|
- catch (error) {
|
|
|
- console.error('Error:', error);
|
|
|
- //navigate("/error-500");
|
|
|
- }
|
|
|
- }
|
|
|
+ // //调用接口获取
|
|
|
+ // const containersData = await axios.get(`/portainer/api/endpoints/${id}/docker/containers/json`, {
|
|
|
+ // headers: {
|
|
|
+ // 'Authorization': 'Bearer ' + jwt.replace(/"/g, '')
|
|
|
+ // },
|
|
|
+ // params: {
|
|
|
+ // all: true,
|
|
|
+ // filters: JSON.stringify({ "label": [`com.docker.compose.project=${customer_name}`] })
|
|
|
+ // }
|
|
|
+ // })
|
|
|
+ // if (containersData.status === 200) {
|
|
|
+ // setContainersInfo(containersData.data);
|
|
|
+ // }
|
|
|
+ // else {
|
|
|
+ // console.error('Error:', containersData);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // else {
|
|
|
+ // console.error('Error:', endpointsData);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // catch (error) {
|
|
|
+ // console.error('Error:', error);
|
|
|
+ // //navigate("/error-500");
|
|
|
+ // }
|
|
|
+ // }
|
|
|
|
|
|
- useEffect(() => {
|
|
|
- getContainersData();
|
|
|
- }, []);
|
|
|
+ // useEffect(() => {
|
|
|
+ // getContainersData();
|
|
|
+ // }, []);
|
|
|
|
|
|
return (
|
|
|
<Row>
|