140 lines
3.6 KiB
TypeScript
140 lines
3.6 KiB
TypeScript
|
|
// Pinia store for container management
|
||
|
|
import { defineStore } from 'pinia'
|
||
|
|
import { ref, computed } from 'vue'
|
||
|
|
import { containerClient, type ContainerStatus, type ContainerAppInfo } from '@/api/container-client'
|
||
|
|
|
||
|
|
export const useContainerStore = defineStore('container', () => {
|
||
|
|
// State
|
||
|
|
const containers = ref<ContainerStatus[]>([])
|
||
|
|
const healthStatus = ref<Record<string, string>>({})
|
||
|
|
const loading = ref(false)
|
||
|
|
const error = ref<string | null>(null)
|
||
|
|
|
||
|
|
// Getters
|
||
|
|
const runningContainers = computed(() =>
|
||
|
|
containers.value.filter(c => c.state === 'running')
|
||
|
|
)
|
||
|
|
|
||
|
|
const stoppedContainers = computed(() =>
|
||
|
|
containers.value.filter(c => c.state === 'stopped' || c.state === 'exited')
|
||
|
|
)
|
||
|
|
|
||
|
|
const getContainerById = computed(() => (id: string) =>
|
||
|
|
containers.value.find(c => c.name.includes(id))
|
||
|
|
)
|
||
|
|
|
||
|
|
const getHealthStatus = computed(() => (appId: string) =>
|
||
|
|
healthStatus.value[appId] || 'unknown'
|
||
|
|
)
|
||
|
|
|
||
|
|
// Actions
|
||
|
|
async function fetchContainers() {
|
||
|
|
loading.value = true
|
||
|
|
error.value = null
|
||
|
|
try {
|
||
|
|
containers.value = await containerClient.listContainers()
|
||
|
|
} catch (e) {
|
||
|
|
error.value = e instanceof Error ? e.message : 'Failed to fetch containers'
|
||
|
|
console.error('Failed to fetch containers:', e)
|
||
|
|
} finally {
|
||
|
|
loading.value = false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function fetchHealthStatus() {
|
||
|
|
try {
|
||
|
|
healthStatus.value = await containerClient.getHealthStatus()
|
||
|
|
} catch (e) {
|
||
|
|
console.error('Failed to fetch health status:', e)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function installApp(manifestPath: string) {
|
||
|
|
loading.value = true
|
||
|
|
error.value = null
|
||
|
|
try {
|
||
|
|
const containerName = await containerClient.installApp(manifestPath)
|
||
|
|
await fetchContainers()
|
||
|
|
return containerName
|
||
|
|
} catch (e) {
|
||
|
|
error.value = e instanceof Error ? e.message : 'Failed to install app'
|
||
|
|
throw e
|
||
|
|
} finally {
|
||
|
|
loading.value = false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function startContainer(appId: string) {
|
||
|
|
loading.value = true
|
||
|
|
error.value = null
|
||
|
|
try {
|
||
|
|
await containerClient.startContainer(appId)
|
||
|
|
await fetchContainers()
|
||
|
|
await fetchHealthStatus()
|
||
|
|
} catch (e) {
|
||
|
|
error.value = e instanceof Error ? e.message : 'Failed to start container'
|
||
|
|
throw e
|
||
|
|
} finally {
|
||
|
|
loading.value = false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function stopContainer(appId: string) {
|
||
|
|
loading.value = true
|
||
|
|
error.value = null
|
||
|
|
try {
|
||
|
|
await containerClient.stopContainer(appId)
|
||
|
|
await fetchContainers()
|
||
|
|
} catch (e) {
|
||
|
|
error.value = e instanceof Error ? e.message : 'Failed to stop container'
|
||
|
|
throw e
|
||
|
|
} finally {
|
||
|
|
loading.value = false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function removeContainer(appId: string) {
|
||
|
|
loading.value = true
|
||
|
|
error.value = null
|
||
|
|
try {
|
||
|
|
await containerClient.removeContainer(appId)
|
||
|
|
await fetchContainers()
|
||
|
|
} catch (e) {
|
||
|
|
error.value = e instanceof Error ? e.message : 'Failed to remove container'
|
||
|
|
throw e
|
||
|
|
} finally {
|
||
|
|
loading.value = false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function getContainerLogs(appId: string, lines: number = 100) {
|
||
|
|
try {
|
||
|
|
return await containerClient.getContainerLogs(appId, lines)
|
||
|
|
} catch (e) {
|
||
|
|
error.value = e instanceof Error ? e.message : 'Failed to get logs'
|
||
|
|
throw e
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
// State
|
||
|
|
containers,
|
||
|
|
healthStatus,
|
||
|
|
loading,
|
||
|
|
error,
|
||
|
|
// Getters
|
||
|
|
runningContainers,
|
||
|
|
stoppedContainers,
|
||
|
|
getContainerById,
|
||
|
|
getHealthStatus,
|
||
|
|
// Actions
|
||
|
|
fetchContainers,
|
||
|
|
fetchHealthStatus,
|
||
|
|
installApp,
|
||
|
|
startContainer,
|
||
|
|
stopContainer,
|
||
|
|
removeContainer,
|
||
|
|
getContainerLogs,
|
||
|
|
}
|
||
|
|
})
|