Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

Lyra is a web application designed to manage GPU-enabled build environments and containers effortlessly.

![Project Screenshot](imgs/screenshot.png)

## Quick Start

### Prerequisites
Expand Down
66 changes: 65 additions & 1 deletion backend/app/routers/environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,50 @@ async def create_environment(env: EnvironmentCreate, db: AsyncSession = Depends(
@router.get("/", response_model=List[EnvironmentResponse])
async def read_environments(skip: int = 0, limit: int = 100, db: AsyncSession = Depends(get_db)):
result = await db.execute(select(Environment).offset(skip).limit(limit))
return result.scalars().all()
envs = result.scalars().all()

# Sync with Docker status
client = docker.from_env()
status_changed = False

for env in envs:
container_name = f"lyra-{env.name}-{env.id}"
try:
container = client.containers.get(container_name)

# Check for error state (Exit Code != 0)
exit_code = container.attrs['State'].get('ExitCode', 0)

if container.status == 'running':
if env.status != 'running':
env.status = 'running'
status_changed = True
else:
# Container is not running
new_status = 'stopped'
if exit_code != 0:
new_status = 'error'

if env.status != new_status:
env.status = new_status
status_changed = True
except docker.errors.NotFound:
if env.status in ['running', 'building']:
# If it was supposed to be running/building but not found, mark as stopped or error
# Note: 'building' might be tricky if it's still in image build phase,
# but once container is expected, it should be there.
# For simplicity, if not found and was running, mark stopped.
if env.status == 'running':
env.status = 'stopped'
status_changed = True
except Exception as e:
print(f"Error checking container status: {e}")

if status_changed:
await db.commit()
# Re-fetch or just return updated objects (SQLAlchemy objects act as refs)

return envs


@router.get("/{environment_id}", response_model=EnvironmentResponse)
Expand All @@ -101,6 +144,27 @@ async def read_environment(environment_id: str, db: AsyncSession = Depends(get_d
return env


@router.get("/{environment_id}/logs")
async def get_environment_logs(environment_id: str, db: AsyncSession = Depends(get_db)):
result = await db.execute(select(Environment).where(Environment.id == environment_id))
env = result.scalars().first()
if env is None:
raise HTTPException(status_code=404, detail="Environment not found")

client = docker.from_env()
container_name = f"lyra-{env.name}-{env.id}"

try:
# Check if container exists (even if stopped/exited)
container = client.containers.get(container_name)
logs = container.logs(tail=50).decode('utf-8')
return {"logs": logs}
except docker.errors.NotFound:
raise HTTPException(status_code=404, detail="Container not found")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))


@router.delete("/{environment_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_environment(environment_id: str, db: AsyncSession = Depends(get_db)):
result = await db.execute(select(Environment).where(Environment.id == environment_id))
Expand Down
138 changes: 128 additions & 10 deletions frontend/src/pages/Dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import { HardDrive, RefreshCw, Trash2, X } from 'lucide-react';
import { HardDrive, HelpCircle, LayoutTemplate, Network, Play, RefreshCw, Square, SquareTerminal, Trash2, X } from 'lucide-react';
import { useEffect, useState } from 'react';
import Modal from '../components/Modal';

Expand All @@ -25,6 +25,9 @@ export default function Dashboard() {
const [loading, setLoading] = useState(true);
const [deleteId, setDeleteId] = useState<string | null>(null);
const [selectedVolEnv, setSelectedVolEnv] = useState<Environment | null>(null);
const [errorLogEnv, setErrorLogEnv] = useState<Environment | null>(null);
const [errorLog, setErrorLog] = useState<string>("");
const [logLoading, setLogLoading] = useState(false);

const fetchEnvironments = async () => {
try {
Expand All @@ -38,6 +41,27 @@ export default function Dashboard() {
}
};

const fetchErrorLogs = async (envId: string) => {
try {
setLogLoading(true);
const res = await axios.get(`environments/${envId}/logs`);
setErrorLog(res.data.logs);
} catch (error) {
console.error("Failed to fetch logs", error);
setErrorLog("Failed to fetch logs.");
} finally {
setLogLoading(false);
}
};

useEffect(() => {
if (errorLogEnv) {
fetchErrorLogs(errorLogEnv.id);
} else {
setErrorLog("");
}
}, [errorLogEnv]);

const deleteEnvironment = async () => {
if (!deleteId) return;
try {
Expand Down Expand Up @@ -105,7 +129,48 @@ export default function Dashboard() {
<div className="p-4 border-t border-[#3f3f46] bg-[#27272a]/50 flex justify-end">
<button
onClick={() => setSelectedVolEnv(null)}
className="px-4 py-2 rounded-lg text-sm font-medium bg-blue-600 hover:bg-blue-500 text-white shadow-lg shadow-blue-500/20 transition-all"
className="px-4 py-2 rounded-lg text-sm font-medium bg-[#3f3f46] hover:bg-[#52525b] text-white transition-all"
>
Close
</button>
</div>
</div>
</div>
)}

{/* Error Log Modal */}
{errorLogEnv && (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm">
<div className="bg-[#18181b] rounded-xl border border-[#3f3f46] shadow-2xl w-full max-w-2xl overflow-hidden animate-in fade-in zoom-in duration-200">
<div className="p-6 border-b border-[#3f3f46] flex justify-between items-center">
<h3 className="text-xl font-bold text-white flex items-center gap-2">
<HelpCircle size={20} className="text-red-400" />
Container Error Log
</h3>
<button onClick={() => setErrorLogEnv(null)} className="text-gray-400 hover:text-white transition-colors">
<X size={20} />
</button>
</div>
<div className="p-6">
<p className="text-gray-400 text-sm mb-4">
Last 50 lines of logs for <span className="text-white font-medium">{errorLogEnv.name}</span>
</p>
<div className="bg-[#0f0f12] rounded-lg border border-[#3f3f46] p-4 max-h-[400px] overflow-y-auto">
{logLoading ? (
<div className="flex items-center justify-center py-8 text-gray-500">
<RefreshCw size={24} className="animate-spin" />
</div>
) : (
<pre className="text-xs font-mono text-gray-300 whitespace-pre-wrap font-ligatures-none">
{errorLog || "No logs available."}
</pre>
)}
</div>
</div>
<div className="p-4 border-t border-[#3f3f46] bg-[#27272a]/50 flex justify-end">
<button
onClick={() => setErrorLogEnv(null)}
className="px-4 py-2 rounded-lg text-sm font-medium bg-[#3f3f46] hover:bg-[#52525b] text-white transition-all"
>
Close
</button>
Expand Down Expand Up @@ -150,21 +215,68 @@ export default function Dashboard() {
<tr key={env.id} className="hover:bg-[#3f3f46]/50 transition-colors">
<td className="px-6 py-4 text-white font-medium">{env.name}</td>
<td className="px-6 py-4">
<span className={`inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium ${
env.status === 'running' ? 'bg-green-500/10 text-green-500' :
env.status === 'building' ? 'bg-yellow-500/10 text-yellow-500' :
'bg-red-500/10 text-red-500'
}`}>
{env.status.charAt(0).toUpperCase() + env.status.slice(1)}
</span>
<div className="flex items-center gap-2">
<span className={`inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium ${
env.status === 'running' ? 'bg-green-500/10 text-green-500' :
env.status === 'stopped' ? 'bg-yellow-500/10 text-yellow-500' :
env.status === 'building' ? 'bg-blue-500/10 text-blue-500' :
'bg-red-500/10 text-red-500'
}`}>
{env.status.charAt(0).toUpperCase() + env.status.slice(1)}
</span>
{env.status === 'error' && (
<button
onClick={() => setErrorLogEnv(env)}
className="text-red-400 hover:text-red-300 transition-colors"
title="View Error Logs"
>
<HelpCircle size={16} />
</button>
)}
</div>
</td>
<td className="px-6 py-4 text-gray-300">
{env.ssh_port} / {env.jupyter_port}
<div className="flex items-center gap-3">
<div className="flex items-center gap-1.5" title="SSH Port">
<span>{env.ssh_port}</span>
<button
className="p-1 hover:bg-[#3f3f46] rounded text-gray-500 hover:text-blue-400 transition-colors"
title="Connect via SSH (Coming Soon)"
>
<SquareTerminal size={14} />
</button>
</div>
<span className="text-gray-600">/</span>
<div className="flex items-center gap-1.5" title="Jupyter Port">
<span>{env.jupyter_port}</span>
<button
className="p-1 hover:bg-[#3f3f46] rounded text-gray-500 hover:text-orange-400 transition-colors"
title="Open Jupyter Lab (Coming Soon)"
>
<LayoutTemplate size={14} />
</button>
</div>
</div>
</td>
<td className="px-6 py-4 text-gray-300">
{env.gpu_indices.length > 0 ? env.gpu_indices.join(', ') : "-"}
</td>
<td className="px-6 py-4 text-right space-x-2">
<button
onClick={() => {
// Toggle status locally for UI demo
const newStatus = env.status === 'running' ? 'stopped' : 'running';
setEnvironments(prev => prev.map(e => e.id === env.id ? { ...e, status: newStatus } : e));
}}
className={`p-2 rounded-lg transition-colors ${
env.status === 'running'
? "hover:bg-[#3f3f46] text-gray-400 hover:text-yellow-400"
: "hover:bg-[#3f3f46] text-gray-400 hover:text-green-400"
}`}
title={env.status === 'running' ? "Stop Instance" : "Start Instance"}
>
{env.status === 'running' ? <Square size={18} fill="currentColor" className="opacity-80" /> : <Play size={18} fill="currentColor" />}
</button>
<button
onClick={() => {
if (env.mount_config && env.mount_config.length > 0) {
Expand All @@ -181,6 +293,12 @@ export default function Dashboard() {
>
<HardDrive size={18} />
</button>
<button
className="p-2 hover:bg-[#3f3f46] rounded-lg text-gray-400 hover:text-purple-400 transition-colors"
title="Manage Ports (Coming Soon)"
>
<Network size={18} />
</button>
<button
onClick={() => setDeleteId(env.id)}
className="p-2 hover:bg-gray-700 rounded-lg text-gray-400 hover:text-red-400 transition-colors"
Expand Down
Binary file added imgs/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.