-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdeploy.go
70 lines (63 loc) · 1.68 KB
/
deploy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(deployCmd)
}
var deployCmd = &cobra.Command{
Use: `deploy`,
Short: `Deploys systems to NixOS instances`,
Long: `Deploy will build, push and activate systems on NixOS instances.`,
RunE: runDeploy,
}
func runDeploy(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
instances, err := inv.matchInstances(args...)
if err != nil {
return err
}
systems := inv.instanceSystems(instances...)
err = inv.build(ctx, systems...)
if err != nil {
return err
}
err = inv.push(ctx, instances, ``)
if err != nil {
return err
}
err = generateSshConfig(ctx)
if err != nil {
return err
}
return inv.deploy(ctx, instances...)
}
func (inv *Inventory) deploy(ctx context.Context, instances ...string) error {
for _, instance := range instances {
err := inv.deployInstance(ctx, instance)
if err != nil {
return err
}
}
return nil
}
func (inv *Inventory) deployInstance(ctx context.Context, instance string) error {
cfg := inv.Instances[instance]
path := inv.Systems[cfg.System].Result
args := []string{`-F`, filepath.Join(tmp, `ssh_config`), instance, `sudo`, path + `/bin/switch-to-configuration`, `switch`}
opts := os.Getenv("NIX_COPYOPTS") //TODO: DOC this and NIX_SSHOPTS
if opts != `` {
args = append(args, strings.Split(opts, " ")...)
}
inform(ctx, `running ssh %v`, strings.Join(args, " "))
cmd := exec.CommandContext(ctx, `ssh`, args...)
cmd.Env = append(os.Environ(), `NIX_SSHOPTS=-F `+filepath.Join(tmp, `ssh_config`)+` `+os.Getenv(`NIX_SSHOPTS`))
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return cmd.Run()
}