mirror of
https://github.com/lordmathis/llamactl.git
synced 2025-11-06 09:04:27 +00:00
Migrate from uuid to name
This commit is contained in:
@@ -2,25 +2,23 @@ package llamactl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// InstanceManager defines the interface for managing instances of the llama server.
|
||||
type InstanceManager interface {
|
||||
ListInstances() ([]*Instance, error)
|
||||
CreateInstance(options *InstanceOptions) (*Instance, error)
|
||||
GetInstance(id uuid.UUID) (*Instance, error)
|
||||
UpdateInstance(id uuid.UUID, options *InstanceOptions) (*Instance, error)
|
||||
DeleteInstance(id uuid.UUID) error
|
||||
StartInstance(id uuid.UUID) (*Instance, error)
|
||||
StopInstance(id uuid.UUID) (*Instance, error)
|
||||
RestartInstance(id uuid.UUID) (*Instance, error)
|
||||
GetInstanceLogs(id uuid.UUID) (string, error)
|
||||
GetInstance(name string) (*Instance, error)
|
||||
UpdateInstance(name string, options *InstanceOptions) (*Instance, error)
|
||||
DeleteInstance(name string) error
|
||||
StartInstance(name string) (*Instance, error)
|
||||
StopInstance(name string) (*Instance, error)
|
||||
RestartInstance(name string) (*Instance, error)
|
||||
GetInstanceLogs(name string) (string, error)
|
||||
}
|
||||
|
||||
type instanceManager struct {
|
||||
instances map[uuid.UUID]*Instance
|
||||
instances map[string]*Instance
|
||||
portRange [][2]int // Range of ports to use for instances
|
||||
ports map[int]bool
|
||||
}
|
||||
@@ -28,7 +26,7 @@ type instanceManager struct {
|
||||
// NewInstanceManager creates a new instance of InstanceManager.
|
||||
func NewInstanceManager() InstanceManager {
|
||||
return &instanceManager{
|
||||
instances: make(map[uuid.UUID]*Instance),
|
||||
instances: make(map[string]*Instance),
|
||||
portRange: [][2]int{{8000, 9000}},
|
||||
ports: make(map[int]bool),
|
||||
}
|
||||
@@ -50,10 +48,14 @@ func (im *instanceManager) CreateInstance(options *InstanceOptions) (*Instance,
|
||||
return nil, fmt.Errorf("instance options cannot be nil")
|
||||
}
|
||||
|
||||
// Generate a unique ID for the new instance
|
||||
id := uuid.New()
|
||||
for im.instances[id] != nil {
|
||||
id = uuid.New() // Ensure unique ID
|
||||
// Check if name is provided
|
||||
if options.Name == "" {
|
||||
return nil, fmt.Errorf("instance name cannot be empty")
|
||||
}
|
||||
|
||||
// Check if instance with this name already exists
|
||||
if im.instances[options.Name] != nil {
|
||||
return nil, fmt.Errorf("instance with name %s already exists", options.Name)
|
||||
}
|
||||
|
||||
// Assign a port if not specified
|
||||
@@ -65,101 +67,101 @@ func (im *instanceManager) CreateInstance(options *InstanceOptions) (*Instance,
|
||||
options.Port = port
|
||||
}
|
||||
|
||||
instance := NewInstance(id, options)
|
||||
im.instances[instance.ID] = instance
|
||||
instance := NewInstance(options.Name, options)
|
||||
im.instances[instance.Name] = instance
|
||||
|
||||
return instance, nil
|
||||
|
||||
}
|
||||
|
||||
// GetInstance retrieves an instance by its ID.
|
||||
func (im *instanceManager) GetInstance(id uuid.UUID) (*Instance, error) {
|
||||
instance, exists := im.instances[id]
|
||||
// GetInstance retrieves an instance by its name.
|
||||
func (im *instanceManager) GetInstance(name string) (*Instance, error) {
|
||||
instance, exists := im.instances[name]
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("instance with ID %s not found", id)
|
||||
return nil, fmt.Errorf("instance with name %s not found", name)
|
||||
}
|
||||
return instance, nil
|
||||
}
|
||||
|
||||
// UpdateInstance updates the options of an existing instance and returns it.
|
||||
func (im *instanceManager) UpdateInstance(id uuid.UUID, options *InstanceOptions) (*Instance, error) {
|
||||
instance, exists := im.instances[id]
|
||||
func (im *instanceManager) UpdateInstance(name string, options *InstanceOptions) (*Instance, error) {
|
||||
instance, exists := im.instances[name]
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("instance with ID %s not found", id)
|
||||
return nil, fmt.Errorf("instance with name %s not found", name)
|
||||
}
|
||||
|
||||
instance.SetOptions(options)
|
||||
return instance, nil
|
||||
}
|
||||
|
||||
// DeleteInstance removes stopped instance by its ID.
|
||||
func (im *instanceManager) DeleteInstance(id uuid.UUID) error {
|
||||
_, exists := im.instances[id]
|
||||
// DeleteInstance removes stopped instance by its name.
|
||||
func (im *instanceManager) DeleteInstance(name string) error {
|
||||
_, exists := im.instances[name]
|
||||
if !exists {
|
||||
return fmt.Errorf("instance with ID %s not found", id)
|
||||
return fmt.Errorf("instance with name %s not found", name)
|
||||
}
|
||||
|
||||
if im.instances[id].Running {
|
||||
return fmt.Errorf("instance with ID %s is still running, stop it before deleting", id)
|
||||
if im.instances[name].Running {
|
||||
return fmt.Errorf("instance with name %s is still running, stop it before deleting", name)
|
||||
}
|
||||
|
||||
delete(im.instances, id)
|
||||
delete(im.instances, name)
|
||||
return nil
|
||||
}
|
||||
|
||||
// StartInstance starts a stopped instance and returns it.
|
||||
// If the instance is already running, it returns an error.
|
||||
func (im *instanceManager) StartInstance(id uuid.UUID) (*Instance, error) {
|
||||
instance, exists := im.instances[id]
|
||||
func (im *instanceManager) StartInstance(name string) (*Instance, error) {
|
||||
instance, exists := im.instances[name]
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("instance with ID %s not found", id)
|
||||
return nil, fmt.Errorf("instance with name %s not found", name)
|
||||
}
|
||||
if instance.Running {
|
||||
return instance, fmt.Errorf("instance with ID %s is already running", id)
|
||||
return instance, fmt.Errorf("instance with name %s is already running", name)
|
||||
}
|
||||
|
||||
if err := instance.Start(); err != nil {
|
||||
return nil, fmt.Errorf("failed to start instance %s: %w", id, err)
|
||||
return nil, fmt.Errorf("failed to start instance %s: %w", name, err)
|
||||
}
|
||||
|
||||
return instance, nil
|
||||
}
|
||||
|
||||
// StopInstance stops a running instance and returns it.
|
||||
func (im *instanceManager) StopInstance(id uuid.UUID) (*Instance, error) {
|
||||
instance, exists := im.instances[id]
|
||||
func (im *instanceManager) StopInstance(name string) (*Instance, error) {
|
||||
instance, exists := im.instances[name]
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("instance with ID %s not found", id)
|
||||
return nil, fmt.Errorf("instance with name %s not found", name)
|
||||
}
|
||||
if !instance.Running {
|
||||
return instance, fmt.Errorf("instance with ID %s is already stopped", id)
|
||||
return instance, fmt.Errorf("instance with name %s is already stopped", name)
|
||||
}
|
||||
|
||||
if err := instance.Stop(); err != nil {
|
||||
return nil, fmt.Errorf("failed to stop instance %s: %w", id, err)
|
||||
return nil, fmt.Errorf("failed to stop instance %s: %w", name, err)
|
||||
}
|
||||
|
||||
return instance, nil
|
||||
}
|
||||
|
||||
// RestartInstance stops and then starts an instance, returning the updated instance.
|
||||
func (im *instanceManager) RestartInstance(id uuid.UUID) (*Instance, error) {
|
||||
instance, err := im.StopInstance(id)
|
||||
func (im *instanceManager) RestartInstance(name string) (*Instance, error) {
|
||||
instance, err := im.StopInstance(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return im.StartInstance(instance.ID)
|
||||
return im.StartInstance(instance.Name)
|
||||
}
|
||||
|
||||
// GetInstanceLogs retrieves the logs for a specific instance by its ID.
|
||||
func (im *instanceManager) GetInstanceLogs(id uuid.UUID) (string, error) {
|
||||
_, exists := im.instances[id]
|
||||
// GetInstanceLogs retrieves the logs for a specific instance by its name.
|
||||
func (im *instanceManager) GetInstanceLogs(name string) (string, error) {
|
||||
_, exists := im.instances[name]
|
||||
if !exists {
|
||||
return "", fmt.Errorf("instance with ID %s not found", id)
|
||||
return "", fmt.Errorf("instance with name %s not found", name)
|
||||
}
|
||||
|
||||
// TODO: Implement actual log retrieval logic
|
||||
return fmt.Sprintf("Logs for instance %s", id), nil
|
||||
return fmt.Sprintf("Logs for instance %s", name), nil
|
||||
}
|
||||
|
||||
func (im *instanceManager) getNextAvailablePort() (int, error) {
|
||||
|
||||
Reference in New Issue
Block a user