Files
mcp/cmd/mcp/deploy.go
Kyle Isom 08b3e2a472 Migrate module path from kyle/ to mc/ org
All import paths updated to git.wntrmute.dev/mc/. Bumps mcdsl to v1.2.0,
mc-proxy to v1.1.0.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-27 02:07:42 -07:00

139 lines
3.6 KiB
Go

package main
import (
"context"
"fmt"
"path/filepath"
"strings"
"github.com/spf13/cobra"
mcpv1 "git.wntrmute.dev/mc/mcp/gen/mcp/v1"
"git.wntrmute.dev/mc/mcp/internal/config"
"git.wntrmute.dev/mc/mcp/internal/runtime"
"git.wntrmute.dev/mc/mcp/internal/servicedef"
)
func deployCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "deploy <service>[/<component>]",
Short: "Deploy service from service definition",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.LoadCLIConfig(cfgPath)
if err != nil {
return fmt.Errorf("load config: %w", err)
}
serviceName, component := parseServiceArg(args[0])
def, err := loadServiceDef(cmd, cfg, serviceName)
if err != nil {
return err
}
// Auto-build missing images if the service has build config.
rt := &runtime.Podman{}
if err := ensureImages(cmd.Context(), cfg, def, rt, component); err != nil {
return err
}
spec := servicedef.ToProto(def)
address, err := findNodeAddress(cfg, def.Node)
if err != nil {
return err
}
client, conn, err := dialAgent(address, cfg)
if err != nil {
return fmt.Errorf("dial agent: %w", err)
}
defer func() { _ = conn.Close() }()
resp, err := client.Deploy(context.Background(), &mcpv1.DeployRequest{
Service: spec,
Component: component,
})
if err != nil {
return fmt.Errorf("deploy: %w", err)
}
printComponentResults(resp.GetResults())
return nil
},
}
cmd.Flags().StringP("file", "f", "", "service definition file")
return cmd
}
// parseServiceArg splits a "service/component" argument into its parts.
func parseServiceArg(arg string) (service, component string) {
parts := strings.SplitN(arg, "/", 2)
service = parts[0]
if len(parts) == 2 {
component = parts[1]
}
return service, component
}
// loadServiceDef attempts to load a service definition from the -f flag,
// the configured services directory, or by falling back to the agent registry.
func loadServiceDef(cmd *cobra.Command, cfg *config.CLIConfig, serviceName string) (*servicedef.ServiceDef, error) {
// Check -f flag first.
filePath, _ := cmd.Flags().GetString("file")
if filePath != "" {
def, err := servicedef.Load(filePath)
if err != nil {
return nil, fmt.Errorf("load service def from %q: %w", filePath, err)
}
return def, nil
}
// Try services directory.
dirPath := filepath.Join(cfg.Services.Dir, serviceName+".toml")
def, err := servicedef.Load(dirPath)
if err == nil {
return def, nil
}
// Fall back to agent registry: query each node for the service.
for _, node := range cfg.Nodes {
client, conn, dialErr := dialAgent(node.Address, cfg)
if dialErr != nil {
continue
}
resp, listErr := client.ListServices(context.Background(), &mcpv1.ListServicesRequest{})
_ = conn.Close()
if listErr != nil {
continue
}
for _, svc := range resp.GetServices() {
if svc.GetName() == serviceName {
return servicedef.FromProto(serviceSpecFromInfo(svc), node.Name), nil
}
}
}
return nil, fmt.Errorf("service definition %q not found in %q or agent registry", serviceName, cfg.Services.Dir)
}
// serviceSpecFromInfo converts a ServiceInfo to a ServiceSpec for use with
// servicedef.FromProto. This is needed because the agent registry returns
// ServiceInfo, not ServiceSpec.
func serviceSpecFromInfo(info *mcpv1.ServiceInfo) *mcpv1.ServiceSpec {
spec := &mcpv1.ServiceSpec{
Name: info.GetName(),
Active: info.GetActive(),
}
for _, c := range info.GetComponents() {
spec.Components = append(spec.Components, &mcpv1.ComponentSpec{
Name: c.GetName(),
Image: c.GetImage(),
})
}
return spec
}