add plex
This commit is contained in:
@@ -33,7 +33,7 @@ The API token is generated in Portainer under **Account → Access tokens**.
|
||||
|---------|-------------|
|
||||
| `knecht` | Launch the TUI |
|
||||
| `knecht list` | Print all stacks with status and drift summary |
|
||||
| `knecht deploy <stack>` | Deploy a new stack from `services/<stack>/docker-compose.yml` |
|
||||
| `knecht deploy <stack>` | Deploy a new stack from `services/<stack>/docker-compose.yml`. Env values come from `--from <stack>` and/or `-e KEY=VALUE` (otherwise empty) |
|
||||
| `knecht update <stack>` | Update an existing stack, preserving Portainer env vars |
|
||||
| `knecht restart <stack>` | Stop then start a stack |
|
||||
| `knecht diff <stack>` | Print compose and env key drift |
|
||||
@@ -41,6 +41,20 @@ The API token is generated in Portainer under **Account → Access tokens**.
|
||||
|
||||
`<stack>` always maps to a folder name under `services/`.
|
||||
|
||||
### Deploying
|
||||
|
||||
`knecht deploy` takes env var *keys* from `.env.example`. Values are empty by default, which
|
||||
breaks stacks with bind mounts — seed them from an existing stack or pass them explicitly:
|
||||
|
||||
```sh
|
||||
knecht deploy plex --from jellyfin # reuse jellyfin's PUID/PGID/TZ/TV_PATH/MOVIES_PATH
|
||||
knecht deploy plex --from jellyfin -e TV_PATH=/mnt/external/tv
|
||||
knecht deploy foo -e API_KEY=abc -e TZ=Europe/Berlin
|
||||
```
|
||||
|
||||
`--from` copies values for keys that also exist in the new stack's `.env.example`; `-e` overrides
|
||||
afterwards. Any keys still left empty are reported at the end.
|
||||
|
||||
## TUI
|
||||
|
||||
Run `knecht` with no arguments to launch the interactive TUI.
|
||||
|
||||
@@ -2,16 +2,26 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/jensbecker/homelab/knecht/portainer"
|
||||
"github.com/jensbecker/homelab/knecht/stack"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
deployFrom string
|
||||
deployEnv []string
|
||||
)
|
||||
|
||||
var deployCmd = &cobra.Command{
|
||||
Use: "deploy <stack>",
|
||||
Short: "Deploy a new stack from services/<stack>/docker-compose.yml",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Long: "Deploy a new stack from services/<stack>/docker-compose.yml.\n\n" +
|
||||
"Env var keys are taken from .env.example. Values are empty unless provided via\n" +
|
||||
"--from (copy values from an existing stack) or -e KEY=VALUE.",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
_, client, svcPath, err := setup()
|
||||
if err != nil {
|
||||
@@ -38,14 +48,14 @@ var deployCmd = &cobra.Command{
|
||||
return err
|
||||
}
|
||||
|
||||
// Collect env vars from .env.example keys — values left empty for user to set in Portainer
|
||||
exampleKeys, err := local.EnvExampleKeys()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
env := make([]portainer.EnvVar, len(exampleKeys))
|
||||
for i, k := range exampleKeys {
|
||||
env[i] = portainer.EnvVar{Name: k, Value: ""}
|
||||
|
||||
env, empties, err := buildDeployEnv(client, exampleKeys)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s, err := client.CreateStack(name, compose, env)
|
||||
@@ -53,9 +63,10 @@ var deployCmd = &cobra.Command{
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Deployed stack %q (id %d)\n", s.Name, s.ID)
|
||||
if len(exampleKeys) > 0 {
|
||||
fmt.Println("Set the following env vars in Portainer:")
|
||||
for _, k := range exampleKeys {
|
||||
|
||||
if len(empties) > 0 {
|
||||
fmt.Println("Warning: these env vars have no value yet — set them in Portainer (or redeploy with --from/-e):")
|
||||
for _, k := range empties {
|
||||
fmt.Printf(" • %s\n", k)
|
||||
}
|
||||
}
|
||||
@@ -63,6 +74,71 @@ var deployCmd = &cobra.Command{
|
||||
},
|
||||
}
|
||||
|
||||
// buildDeployEnv assembles the env var list for a new stack.
|
||||
// Keys come from .env.example (empty by default); values are seeded from
|
||||
// --from <stack> and then overridden by -e KEY=VALUE flags.
|
||||
// It returns the ordered env slice and the names of keys left empty.
|
||||
func buildDeployEnv(client *portainer.Client, exampleKeys []string) ([]portainer.EnvVar, []string, error) {
|
||||
envMap := make(map[string]string, len(exampleKeys))
|
||||
for _, k := range exampleKeys {
|
||||
envMap[k] = ""
|
||||
}
|
||||
|
||||
if deployFrom != "" {
|
||||
src, err := client.GetStackByName(deployFrom)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if src == nil {
|
||||
return nil, nil, fmt.Errorf("source stack %q not found", deployFrom)
|
||||
}
|
||||
for _, e := range src.Env {
|
||||
if _, ok := envMap[e.Name]; ok {
|
||||
envMap[e.Name] = e.Value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, kv := range deployEnv {
|
||||
k, v, ok := strings.Cut(kv, "=")
|
||||
k = strings.TrimSpace(k)
|
||||
if !ok || k == "" {
|
||||
return nil, nil, fmt.Errorf("invalid -e %q, expected KEY=VALUE", kv)
|
||||
}
|
||||
envMap[k] = v
|
||||
}
|
||||
|
||||
// Ordered output: .env.example order first, then any extra keys (sorted).
|
||||
ordered := make([]string, 0, len(envMap))
|
||||
seen := make(map[string]bool, len(envMap))
|
||||
for _, k := range exampleKeys {
|
||||
if !seen[k] {
|
||||
ordered = append(ordered, k)
|
||||
seen[k] = true
|
||||
}
|
||||
}
|
||||
var extra []string
|
||||
for k := range envMap {
|
||||
if !seen[k] {
|
||||
extra = append(extra, k)
|
||||
}
|
||||
}
|
||||
sort.Strings(extra)
|
||||
ordered = append(ordered, extra...)
|
||||
|
||||
env := make([]portainer.EnvVar, 0, len(ordered))
|
||||
var empties []string
|
||||
for _, k := range ordered {
|
||||
env = append(env, portainer.EnvVar{Name: k, Value: envMap[k]})
|
||||
if envMap[k] == "" {
|
||||
empties = append(empties, k)
|
||||
}
|
||||
}
|
||||
return env, empties, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
deployCmd.Flags().StringVar(&deployFrom, "from", "", "copy env var values from an existing deployed stack")
|
||||
deployCmd.Flags().StringArrayVarP(&deployEnv, "env", "e", nil, "set env var value (KEY=VALUE, repeatable)")
|
||||
rootCmd.AddCommand(deployCmd)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user