5 Commits

10 changed files with 95 additions and 91 deletions

1
.vscode/launch.json vendored
View File

@@ -14,6 +14,7 @@
"GO_ENV": "development", "GO_ENV": "development",
"LLAMACTL_CONFIG_PATH": "${workspaceFolder}/llamactl.dev.yaml" "LLAMACTL_CONFIG_PATH": "${workspaceFolder}/llamactl.dev.yaml"
}, },
"console": "integratedTerminal",
} }
] ]
} }

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"llamactl/pkg/config" "llamactl/pkg/config"
"llamactl/pkg/database" "llamactl/pkg/database"
@@ -11,6 +12,7 @@ import (
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"time"
) )
// version is set at build time using -ldflags "-X main.version=1.0.0" // version is set at build time using -ldflags "-X main.version=1.0.0"
@@ -116,14 +118,23 @@ func main() {
<-stop <-stop
fmt.Println("Shutting down server...") fmt.Println("Shutting down server...")
if err := server.Close(); err != nil { // Create shutdown context with timeout
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 30*time.Second)
defer shutdownCancel()
// Shutdown HTTP server gracefully
if err := server.Shutdown(shutdownCtx); err != nil {
log.Printf("Error shutting down server: %v\n", err) log.Printf("Error shutting down server: %v\n", err)
} else { } else {
fmt.Println("Server shut down gracefully.") fmt.Println("Server shut down gracefully.")
} }
// Wait for all instances to stop // Stop all instances and cleanup
instanceManager.Shutdown() instanceManager.Shutdown()
if err := db.Close(); err != nil {
log.Printf("Error closing database: %v\n", err)
}
fmt.Println("Exiting llamactl.") fmt.Println("Exiting llamactl.")
} }

View File

@@ -107,6 +107,12 @@ func Open(config *Config) (*sqliteDB, error) {
func (db *sqliteDB) Close() error { func (db *sqliteDB) Close() error {
if db.DB != nil { if db.DB != nil {
log.Println("Closing database connection") log.Println("Closing database connection")
// Checkpoint WAL to merge changes back to main database file
if _, err := db.DB.Exec("PRAGMA wal_checkpoint(TRUNCATE)"); err != nil {
log.Printf("Warning: Failed to checkpoint WAL: %v", err)
}
return db.DB.Close() return db.DB.Close()
} }
return nil return nil

View File

@@ -45,7 +45,7 @@ func (db *sqliteDB) Create(ctx context.Context, inst *instance.Instance) error {
) VALUES (?, ?, ?, ?, ?, ?) ) VALUES (?, ?, ?, ?, ?, ?)
` `
_, err = db.DB.ExecContext(ctx, query, result, err := db.DB.ExecContext(ctx, query,
row.Name, row.Status, row.CreatedAt, row.UpdatedAt, row.OptionsJSON, row.OwnerUserID, row.Name, row.Status, row.CreatedAt, row.UpdatedAt, row.OptionsJSON, row.OwnerUserID,
) )
@@ -53,6 +53,14 @@ func (db *sqliteDB) Create(ctx context.Context, inst *instance.Instance) error {
return fmt.Errorf("failed to insert instance: %w", err) return fmt.Errorf("failed to insert instance: %w", err)
} }
// Get the auto-generated ID and set it on the instance
id, err := result.LastInsertId()
if err != nil {
return fmt.Errorf("failed to get last insert ID: %w", err)
}
inst.ID = int(id)
return nil return nil
} }
@@ -263,6 +271,7 @@ func (db *sqliteDB) rowToInstance(row *instanceRow) (*instance.Instance, error)
// Build complete instance JSON with all fields // Build complete instance JSON with all fields
instanceJSON, err := json.Marshal(map[string]any{ instanceJSON, err := json.Marshal(map[string]any{
"id": row.ID,
"name": row.Name, "name": row.Name,
"created": row.CreatedAt, "created": row.CreatedAt,
"status": row.Status, "status": row.Status,

View File

@@ -114,11 +114,6 @@ func (im *instanceManager) Shutdown() {
} }
wg.Wait() wg.Wait()
fmt.Println("All instances stopped.") fmt.Println("All instances stopped.")
// 4. Close database connection
if err := im.db.Close(); err != nil {
log.Printf("Error closing database: %v\n", err)
}
}) })
} }
@@ -181,6 +176,7 @@ func (im *instanceManager) loadInstance(persistedInst *instance.Instance) error
inst := instance.New(name, im.globalConfig, options, statusCallback) inst := instance.New(name, im.globalConfig, options, statusCallback)
// Restore persisted fields that NewInstance doesn't set // Restore persisted fields that NewInstance doesn't set
inst.ID = persistedInst.ID
inst.Created = persistedInst.Created inst.Created = persistedInst.Created
inst.SetStatus(persistedInst.GetStatus()) inst.SetStatus(persistedInst.GetStatus())

View File

@@ -37,7 +37,6 @@ func (im *instanceManager) ListInstances() ([]*instance.Instance, error) {
if node := im.getNodeForInstance(inst); node != nil { if node := im.getNodeForInstance(inst); node != nil {
remoteInst, err := im.remote.getInstance(ctx, node, inst.Name) remoteInst, err := im.remote.getInstance(ctx, node, inst.Name)
if err != nil { if err != nil {
// Log error but continue with stale data
// Don't fail the entire list operation due to one remote failure // Don't fail the entire list operation due to one remote failure
continue continue
} }

View File

@@ -11,17 +11,12 @@ import (
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
) )
// InstancePermission defines the permissions for an API key on a specific instance.
type InstancePermission struct {
InstanceID int `json:"instance_id"`
}
// CreateKeyRequest represents the request body for creating a new API key. // CreateKeyRequest represents the request body for creating a new API key.
type CreateKeyRequest struct { type CreateKeyRequest struct {
Name string Name string `json:"name"`
PermissionMode auth.PermissionMode PermissionMode auth.PermissionMode `json:"permission_mode"`
ExpiresAt *int64 ExpiresAt *int64 `json:"expires_at,omitempty"`
InstancePermissions []InstancePermission InstanceIDs []int `json:"instance_ids,omitempty"`
} }
// CreateKeyResponse represents the response returned when creating a new API key. // CreateKeyResponse represents the response returned when creating a new API key.
@@ -87,8 +82,8 @@ func (h *Handler) CreateKey() http.HandlerFunc {
writeError(w, http.StatusBadRequest, "invalid_permission_mode", "Permission mode must be 'allow_all' or 'per_instance'") writeError(w, http.StatusBadRequest, "invalid_permission_mode", "Permission mode must be 'allow_all' or 'per_instance'")
return return
} }
if req.PermissionMode == auth.PermissionModePerInstance && len(req.InstancePermissions) == 0 { if req.PermissionMode == auth.PermissionModePerInstance && len(req.InstanceIDs) == 0 {
writeError(w, http.StatusBadRequest, "missing_permissions", "Instance permissions required when permission mode is 'per_instance'") writeError(w, http.StatusBadRequest, "missing_permissions", "Instance IDs required when permission mode is 'per_instance'")
return return
} }
if req.ExpiresAt != nil && *req.ExpiresAt <= time.Now().Unix() { if req.ExpiresAt != nil && *req.ExpiresAt <= time.Now().Unix() {
@@ -108,9 +103,9 @@ func (h *Handler) CreateKey() http.HandlerFunc {
instanceIDMap[inst.ID] = true instanceIDMap[inst.ID] = true
} }
for _, perm := range req.InstancePermissions { for _, instanceID := range req.InstanceIDs {
if !instanceIDMap[perm.InstanceID] { if !instanceIDMap[instanceID] {
writeError(w, http.StatusBadRequest, "invalid_instance_id", fmt.Sprintf("Instance ID %d does not exist", perm.InstanceID)) writeError(w, http.StatusBadRequest, "invalid_instance_id", fmt.Sprintf("Instance ID %d does not exist", instanceID))
return return
} }
} }
@@ -142,12 +137,12 @@ func (h *Handler) CreateKey() http.HandlerFunc {
UpdatedAt: now, UpdatedAt: now,
} }
// Convert InstancePermissions to KeyPermissions // Convert InstanceIDs to KeyPermissions
var keyPermissions []auth.KeyPermission var keyPermissions []auth.KeyPermission
for _, perm := range req.InstancePermissions { for _, instanceID := range req.InstanceIDs {
keyPermissions = append(keyPermissions, auth.KeyPermission{ keyPermissions = append(keyPermissions, auth.KeyPermission{
KeyID: 0, // Will be set by database after key creation KeyID: 0, // Will be set by database after key creation
InstanceID: perm.InstanceID, InstanceID: instanceID,
}) })
} }

View File

@@ -8,9 +8,9 @@ import { Checkbox } from "@/components/ui/checkbox";
import { Alert, AlertDescription } from "@/components/ui/alert"; import { Alert, AlertDescription } from "@/components/ui/alert";
import { Loader2 } from "lucide-react"; import { Loader2 } from "lucide-react";
import { apiKeysApi } from "@/lib/api"; import { apiKeysApi } from "@/lib/api";
import { CreateKeyRequest, PermissionMode, InstancePermission } from "@/types/apiKey"; import { PermissionMode, type CreateKeyRequest } from "@/types/apiKey";
import { useInstances } from "@/contexts/InstancesContext"; import { useInstances } from "@/contexts/InstancesContext";
import { format, addDays } from "date-fns"; import { format } from "date-fns";
interface CreateApiKeyDialogProps { interface CreateApiKeyDialogProps {
open: boolean; open: boolean;
@@ -61,22 +61,19 @@ function CreateApiKeyDialog({ open, onOpenChange, onKeyCreated }: CreateApiKeyDi
} }
// Build request // Build request
const permissions: InstancePermission[] = []; const instanceIds: number[] = [];
if (permissionMode === PermissionMode.PerInstance) { if (permissionMode === PermissionMode.PerInstance) {
Object.entries(instancePermissions).forEach(([instanceId, canInfer]) => { Object.entries(instancePermissions).forEach(([instanceId, hasPermission]) => {
if (canInfer) { if (hasPermission) {
permissions.push({ instanceIds.push(parseInt(instanceId));
InstanceID: parseInt(instanceId),
CanInfer: true,
});
} }
}); });
} }
const request: CreateKeyRequest = { const request: CreateKeyRequest = {
Name: name.trim(), name: name.trim(),
PermissionMode: permissionMode, permission_mode: permissionMode,
InstancePermissions: permissions, instance_ids: instanceIds,
}; };
// Add expiration if provided // Add expiration if provided
@@ -87,7 +84,7 @@ function CreateApiKeyDialog({ open, onOpenChange, onKeyCreated }: CreateApiKeyDi
setError("Expiration date must be in the future"); setError("Expiration date must be in the future");
return; return;
} }
request.ExpiresAt = Math.floor(expirationDate.getTime() / 1000); request.expires_at = Math.floor(expirationDate.getTime() / 1000);
} }
setLoading(true); setLoading(true);
@@ -107,10 +104,10 @@ function CreateApiKeyDialog({ open, onOpenChange, onKeyCreated }: CreateApiKeyDi
}; };
const handleInstancePermissionChange = (instanceId: number, checked: boolean) => { const handleInstancePermissionChange = (instanceId: number, checked: boolean) => {
setInstancePermissions({ setInstancePermissions(prev => ({
...instancePermissions, ...prev,
[instanceId]: checked, [instanceId]: checked,
}); }));
}; };
return ( return (
@@ -172,14 +169,19 @@ function CreateApiKeyDialog({ open, onOpenChange, onKeyCreated }: CreateApiKeyDi
<p className="text-sm text-muted-foreground">No instances available</p> <p className="text-sm text-muted-foreground">No instances available</p>
) : ( ) : (
<div className="space-y-2"> <div className="space-y-2">
{instances.map((instance) => ( {instances.map((instance, index) => {
<div key={instance.id} className="flex items-center space-x-2"> const isChecked = !!instancePermissions[instance.id];
return (
<div
key={`${instance.name}-${index}`}
className="flex items-center space-x-2"
>
<Checkbox <Checkbox
id={`instance-${instance.id}`} id={`instance-${instance.id}`}
checked={instancePermissions[instance.id] || false} checked={isChecked}
onCheckedChange={(checked) => onCheckedChange={(checked) => {
handleInstancePermissionChange(instance.id, checked as boolean) handleInstancePermissionChange(instance.id, checked as boolean);
} }}
disabled={loading} disabled={loading}
/> />
<Label <Label
@@ -188,9 +190,9 @@ function CreateApiKeyDialog({ open, onOpenChange, onKeyCreated }: CreateApiKeyDi
> >
{instance.name} {instance.name}
</Label> </Label>
<span className="text-sm text-muted-foreground">Can Infer</span>
</div> </div>
))} );
})}
</div> </div>
)} )}
</div> </div>

View File

@@ -1,4 +1,4 @@
import { useEffect, useState } from "react"; import { useEffect, useState, Fragment } from "react";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge"; import { Badge } from "@/components/ui/badge";
import { Alert, AlertDescription } from "@/components/ui/alert"; import { Alert, AlertDescription } from "@/components/ui/alert";
@@ -175,9 +175,8 @@ function ApiKeysSection() {
</thead> </thead>
<tbody> <tbody>
{keys.map((key) => ( {keys.map((key) => (
<> <Fragment key={key.id}>
<tr <tr
key={key.id}
className="border-t hover:bg-muted/50 cursor-pointer" className="border-t hover:bg-muted/50 cursor-pointer"
onClick={() => handleRowClick(key)} onClick={() => handleRowClick(key)}
> >
@@ -236,25 +235,15 @@ function ApiKeysSection() {
<p className="text-sm text-muted-foreground">Loading permissions...</p> <p className="text-sm text-muted-foreground">Loading permissions...</p>
) : permissions[key.id] ? ( ) : permissions[key.id] ? (
<div className="space-y-2"> <div className="space-y-2">
<p className="text-sm font-semibold">Instance Permissions:</p> <p className="text-sm font-semibold">Allowed Instances:</p>
<table className="w-full text-sm"> <ul className="text-sm space-y-1">
<thead>
<tr className="border-b">
<th className="text-left py-2">Instance Name</th>
<th className="text-left py-2">Can Infer</th>
</tr>
</thead>
<tbody>
{permissions[key.id].map((perm) => ( {permissions[key.id].map((perm) => (
<tr key={perm.instance_id} className="border-b"> <li key={perm.instance_id} className="flex items-center gap-2">
<td className="py-2">{perm.instance_name}</td> <Check className="h-3 w-3 text-green-600" />
<td className="py-2"> {perm.instance_name}
<Check className="h-4 w-4 text-green-600" /> </li>
</td>
</tr>
))} ))}
</tbody> </ul>
</table>
</div> </div>
) : ( ) : (
<p className="text-sm text-muted-foreground">No permissions data</p> <p className="text-sm text-muted-foreground">No permissions data</p>
@@ -262,7 +251,7 @@ function ApiKeysSection() {
</td> </td>
</tr> </tr>
)} )}
</> </Fragment>
))} ))}
</tbody> </tbody>
</table> </table>

View File

@@ -15,14 +15,10 @@ export interface ApiKey {
} }
export interface CreateKeyRequest { export interface CreateKeyRequest {
Name: string name: string
PermissionMode: PermissionMode permission_mode: PermissionMode
ExpiresAt?: number expires_at?: number
InstancePermissions: InstancePermission[] instance_ids: number[]
}
export interface InstancePermission {
InstanceID: number
} }
export interface CreateKeyResponse extends ApiKey { export interface CreateKeyResponse extends ApiKey {