Skip to content

Commit

Permalink
feat: uniform application service status return (#7426)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssongliu authored Dec 18, 2024
1 parent f6475e7 commit 55a6cdc
Show file tree
Hide file tree
Showing 24 changed files with 171 additions and 294 deletions.
3 changes: 2 additions & 1 deletion agent/app/dto/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ type DaemonJsonUpdateByFile struct {

type DaemonJsonConf struct {
IsSwarm bool `json:"isSwarm"`
Status string `json:"status"`
IsExist bool `json:"isExist"`
IsActive bool `json:"isActive"`
Version string `json:"version"`
Mirrors []string `json:"registryMirrors"`
Registries []string `json:"insecureRegistries"`
Expand Down
3 changes: 2 additions & 1 deletion agent/app/dto/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package dto

type FirewallBaseInfo struct {
Name string `json:"name"`
Status string `json:"status"`
IsExist bool `json:"isExist"`
IsActive bool `json:"isActive"`
Version string `json:"version"`
PingStatus string `json:"pingStatus"`
}
Expand Down
3 changes: 2 additions & 1 deletion agent/app/dto/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ type SSHUpdate struct {

type SSHInfo struct {
AutoStart bool `json:"autoStart"`
Status string `json:"status"`
IsExist bool `json:"isExist"`
IsActive bool `json:"isActive"`
Message string `json:"message"`
Port string `json:"port"`
ListenAddress string `json:"listenAddress"`
Expand Down
10 changes: 7 additions & 3 deletions agent/app/service/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,19 @@ func (u *DockerService) LoadDockerConf() *dto.DaemonJsonConf {
ctx := context.Background()
var data dto.DaemonJsonConf
data.IPTables = true
data.Status = constant.StatusRunning
data.Version = "-"
if cmd.Which("docker") {
data.IsExist = false
return &data
}
data.IsExist = true
client, err := docker.NewDockerClient()
if err != nil {
data.Status = constant.Stopped
data.IsActive = false
} else {
defer client.Close()
if _, err := client.Ping(ctx); err != nil {
data.Status = constant.Stopped
data.IsActive = false
}
itemVersion, err := client.ServerVersion(ctx)
if err == nil {
Expand Down
4 changes: 2 additions & 2 deletions agent/app/service/fail2ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ func (u *Fail2BanService) UpdateConf(req dto.Fail2BanUpdate) error {
if client.Name() != itemName {
return buserr.WithName("ErrBanAction", itemName)
}
status, _ := client.Status()
if status != "running" {
isActive, _ := client.Status()
if !isActive {
return buserr.WithName("ErrBanAction", itemName)
}
}
Expand Down
5 changes: 3 additions & 2 deletions agent/app/service/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ func NewIFirewallService() IFirewallService {

func (u *FirewallService) LoadBaseInfo() (dto.FirewallBaseInfo, error) {
var baseInfo dto.FirewallBaseInfo
baseInfo.Status = "not running"
baseInfo.Version = "-"
baseInfo.Name = "-"
client, err := firewall.NewFirewallClient()
if err != nil {
baseInfo.IsExist = false
return baseInfo, err
}
baseInfo.IsExist = true
baseInfo.Name = client.Name()

var wg sync.WaitGroup
Expand All @@ -60,7 +61,7 @@ func (u *FirewallService) LoadBaseInfo() (dto.FirewallBaseInfo, error) {
}()
go func() {
defer wg.Done()
baseInfo.Status, _ = client.Status()
baseInfo.IsActive, _ = client.Status()
}()
go func() {
defer wg.Done()
Expand Down
17 changes: 7 additions & 10 deletions agent/app/service/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func NewISSHService() ISSHService {
func (u *SSHService) GetSSHInfo() (*dto.SSHInfo, error) {
data := dto.SSHInfo{
AutoStart: true,
Status: constant.StatusEnable,
IsExist: true,
IsActive: true,
Message: "",
Port: "22",
ListenAddress: "",
Expand All @@ -56,17 +57,13 @@ func (u *SSHService) GetSSHInfo() (*dto.SSHInfo, error) {
}
serviceName, err := loadServiceName()
if err != nil {
data.Status = constant.StatusDisable
data.IsExist = false
data.Message = err.Error()
} else {
active, err := systemctl.IsActive(serviceName)
if !active {
data.Status = constant.StatusDisable
if err != nil {
data.Message = err.Error()
}
} else {
data.Status = constant.StatusEnable
data.IsActive = active
if !active && err != nil {
data.Message = err.Error()
}
}

Expand All @@ -84,7 +81,7 @@ func (u *SSHService) GetSSHInfo() (*dto.SSHInfo, error) {
sshConf, err := os.ReadFile(sshPath)
if err != nil {
data.Message = err.Error()
data.Status = constant.StatusDisable
data.IsActive = false
}
lines := strings.Split(string(sshConf), "\n")
for _, line := range lines {
Expand Down
2 changes: 1 addition & 1 deletion agent/utils/firewall/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type FirewallClient interface {
Stop() error
Restart() error
Reload() error
Status() (string, error) // running not running
Status() (bool, error) // running not running
Version() (string, error)

ListPort() ([]client.FireInfo, error)
Expand Down
7 changes: 2 additions & 5 deletions agent/utils/firewall/client/firewalld.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ func (f *Firewall) Name() string {
return "firewalld"
}

func (f *Firewall) Status() (string, error) {
func (f *Firewall) Status() (bool, error) {
stdout, _ := cmd.Exec("firewall-cmd --state")
if stdout == "running\n" {
return "running", nil
}
return "not running", nil
return stdout == "running\n", nil
}

func (f *Firewall) Version() (string, error) {
Expand Down
10 changes: 7 additions & 3 deletions agent/utils/firewall/client/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func NewIptables() (*Iptables, error) {
return iptables, nil
}

func (iptables *Iptables) runf(rule string, a ...any) error {
stdout, err := cmd.Execf("%s iptables -t nat %s", iptables.CmdStr, fmt.Sprintf(rule, a...))
func (iptables *Iptables) run(rule string) error {
stdout, err := cmd.Execf("%s iptables -t nat %s", iptables.CmdStr, rule)
if err != nil {
return fmt.Errorf("%s, %s", err, stdout)
}
Expand All @@ -39,6 +39,10 @@ func (iptables *Iptables) runf(rule string, a ...any) error {
return nil
}

func (iptables *Iptables) runf(rule string, a ...any) error {
return iptables.run(fmt.Sprintf(rule, a...))
}

func (iptables *Iptables) Check() error {
stdout, err := cmd.Exec("cat /proc/sys/net/ipv4/ip_forward")
if err != nil {
Expand Down Expand Up @@ -100,7 +104,7 @@ func (iptables *Iptables) NatAdd(protocol, src, destIp, destPort string, save bo
if destIp != "" && destIp != "127.0.0.1" && destIp != "localhost" {
rule = fmt.Sprintf("-A %s -p %s --dport %s -j DNAT --to-destination %s:%s", NatChain, protocol, src, destIp, destPort)
}
if err := iptables.runf(rule); err != nil {
if err := iptables.run(rule); err != nil {
return err
}

Expand Down
8 changes: 4 additions & 4 deletions agent/utils/firewall/client/ufw.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ func (f *Ufw) Name() string {
return "ufw"
}

func (f *Ufw) Status() (string, error) {
func (f *Ufw) Status() (bool, error) {
stdout, _ := cmd.Execf("%s status | grep Status", f.CmdStr)
if stdout == "Status: active\n" {
return "running", nil
return true, nil
}
stdout1, _ := cmd.Execf("%s status | grep 状态", f.CmdStr)
if stdout1 == "状态: 激活\n" {
return "running", nil
return true, nil
}
return "not running", nil
return false, nil
}

func (f *Ufw) Version() (string, error) {
Expand Down
40 changes: 10 additions & 30 deletions frontend/src/views/container/compose/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
<div v-show="isOnDetail">
<ComposeDetail ref="composeDetailRef" />
</div>
<el-card v-if="dockerStatus != 'Running'" class="mask-prompt">
<span>{{ $t('container.serviceUnavailable') }}</span>
<el-button type="primary" link class="bt" @click="goSetting">【 {{ $t('container.setting') }} 】</el-button>
<span>{{ $t('container.startIn') }}</span>
</el-card>

<LayoutContent v-if="!isOnDetail" :title="$t('container.compose')" :class="{ mask: dockerStatus != 'Running' }">
<docker-status v-model:isActive="isActive" v-model:loading="loading" @search="search" />

<LayoutContent v-if="!isOnDetail" :title="$t('container.compose')" :class="{ mask: !isActive }">
<template #leftToolBar>
<el-button type="primary" @click="onOpenDialog()">
{{ $t('container.createCompose') }}
Expand Down Expand Up @@ -90,12 +87,13 @@
</template>

<script lang="ts" setup>
import { reactive, onMounted, ref } from 'vue';
import { reactive, ref } from 'vue';
import EditDialog from '@/views/container/compose/edit/index.vue';
import CreateDialog from '@/views/container/compose/create/index.vue';
import DeleteDialog from '@/views/container/compose/delete/index.vue';
import ComposeDetail from '@/views/container/compose/detail/index.vue';
import { loadContainerLog, loadDockerStatus, searchCompose } from '@/api/modules/container';
import { loadContainerLog, searchCompose } from '@/api/modules/container';
import DockerStatus from '@/views/container/docker-status/index.vue';
import i18n from '@/lang';
import { Container } from '@/api/interface/container';
import router from '@/routers';
Expand All @@ -114,31 +112,16 @@ const paginationConfig = reactive({
});
const searchName = ref();
const dockerStatus = ref('Running');
const loadStatus = async () => {
loading.value = true;
await loadDockerStatus()
.then((res) => {
loading.value = false;
dockerStatus.value = res.data;
if (dockerStatus.value === 'Running') {
search();
}
})
.catch(() => {
dockerStatus.value = 'Failed';
loading.value = false;
});
};
const goSetting = async () => {
router.push({ name: 'ContainerSetting' });
};
const isActive = ref(false);
const toComposeFolder = async (row: Container.ComposeInfo) => {
router.push({ path: '/hosts/files', query: { path: row.workdir } });
};
const search = async () => {
if (!isActive.value) {
return;
}
let params = {
info: searchName.value,
page: paginationConfig.currentPage,
Expand Down Expand Up @@ -225,7 +208,4 @@ const buttons = [
},
},
];
onMounted(() => {
loadStatus();
});
</script>
38 changes: 8 additions & 30 deletions frontend/src/views/container/container/index.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
<template>
<div v-loading="loading">
<el-card v-if="dockerStatus != 'Running'" class="mask-prompt">
<span>{{ $t('container.serviceUnavailable') }}</span>
<el-button type="primary" class="bt" link @click="goSetting">【 {{ $t('container.setting') }} 】</el-button>
<span>{{ $t('container.startIn') }}</span>
</el-card>
<docker-status v-model:isActive="isActive" @search="search" />

<div class="mt-5">
<div class="mt-5" v-if="isActive">
<el-tag @click="searchWithStatus('all')" v-if="countItem.all" effect="plain" size="large">
{{ $t('commons.table.all') }} * {{ countItem.all }}
</el-tag>
Expand Down Expand Up @@ -69,7 +65,7 @@
</el-tag>
</div>

<LayoutContent :title="$t('container.container')" :class="{ mask: dockerStatus != 'Running' }">
<LayoutContent :title="$t('container.container')" :class="{ mask: !isActive }">
<template #leftToolBar>
<el-button type="primary" @click="onContainerOperate('')">
{{ $t('container.create') }}
Expand Down Expand Up @@ -414,14 +410,14 @@ import ContainerLogDialog from '@/views/container/container/log/index.vue';
import TerminalDialog from '@/views/container/container/terminal/index.vue';
import CodemirrorDialog from '@/components/codemirror-dialog/index.vue';
import PortJumpDialog from '@/components/port-jump/index.vue';
import DockerStatus from '@/views/container/docker-status/index.vue';
import Status from '@/components/status/index.vue';
import { reactive, onMounted, ref, computed } from 'vue';
import {
containerListStats,
containerOperator,
inspect,
loadContainerStatus,
loadDockerStatus,
searchContainer,
} from '@/api/modules/container';
import { Container } from '@/api/interface/container';
Expand All @@ -434,6 +430,7 @@ const globalStore = GlobalStore();
const mobile = computed(() => {
return globalStore.isMobile();
});
const isActive = ref(false);
const loading = ref(false);
const data = ref();
Expand Down Expand Up @@ -466,23 +463,6 @@ const countItem = reactive({
dead: 0,
});
const dockerStatus = ref('Running');
const loadStatus = async () => {
loading.value = true;
await loadDockerStatus()
.then((res) => {
loading.value = false;
dockerStatus.value = res.data;
if (dockerStatus.value === 'Running') {
search();
}
})
.catch(() => {
dockerStatus.value = 'Failed';
loading.value = false;
});
};
const goDashboard = async (port: any) => {
if (port.indexOf('127.0.0.1') !== -1) {
MsgWarning(i18n.global.t('container.unExposedPort'));
Expand All @@ -499,10 +479,6 @@ const goDashboard = async (port: any) => {
dialogPortJumpRef.value.acceptParams({ port: portEx, ip: ip });
};
const goSetting = async () => {
router.push({ name: 'ContainerSetting' });
};
interface Filters {
filters?: string;
}
Expand All @@ -517,6 +493,9 @@ const dialogRenameRef = ref();
const dialogPruneRef = ref();
const search = async (column?: any) => {
if (!isActive.value) {
return;
}
localStorage.setItem('includeAppStore', includeAppStore.value ? 'true' : 'false');
let filterItem = props.filters ? props.filters : '';
paginationConfig.orderBy = column?.order ? column.prop : paginationConfig.orderBy;
Expand Down Expand Up @@ -802,7 +781,6 @@ const buttons = [
onMounted(() => {
let includeItem = localStorage.getItem('includeAppStore');
includeAppStore.value = !includeItem || includeItem === 'true';
loadStatus();
});
</script>

Expand Down
Loading

0 comments on commit 55a6cdc

Please sign in to comment.