aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/admin/deploy/persist.go
blob: 2c40606e1ab8111930406f2f520172bad170fd7a (plain) (blame)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// vespa-deploy command
// Author: arnej

package deploy

import (
	"os"
	"os/user"
	"path/filepath"

	"github.com/vespa-engine/vespa/client/go/internal/admin/trace"
	"github.com/vespa-engine/vespa/client/go/internal/util"
)

const (
	vespaDeployDir              = "vespa-deploy"
	configsourceUrlUsedFileName = "deploy-configsource-url-used"
	sessionIdFileName           = "deploy-session-id"
)

func createVespaDeployDir() (string, error) {
	tempDir := os.TempDir()
	currentUser, err := user.Current()
	if err != nil {
		return "", err
	}
	vespaDeployTempDir := filepath.Join(tempDir, currentUser.Username, vespaDeployDir)
	if err := os.MkdirAll(vespaDeployTempDir, 0700); err != nil {
		return "", err
	}
	return vespaDeployTempDir, nil
}

func configsourceUrlUsedFile() string {
	vespaDeployTempDir, err := createVespaDeployDir()
	if err != nil {
		vespaDeployTempDir = "/tmp"
	}
	return filepath.Join(vespaDeployTempDir, configsourceUrlUsedFileName)
}

func createTenantDir(tenant string) string {
	vespaDeployTempDir, err := createVespaDeployDir()
	if err != nil {
		util.JustExitWith(err)
	}
	tdir := filepath.Join(vespaDeployTempDir, tenant)
	if err := os.MkdirAll(tdir, 0700); err != nil {
		util.JustExitWith(err)
	}
	return tdir
}

func writeConfigsourceUrlUsed(url string) {
	fn := configsourceUrlUsedFile()
	os.WriteFile(fn, []byte(url), 0600)
}

func getConfigsourceUrlUsed() string {
	fn := configsourceUrlUsedFile()
	bytes, err := os.ReadFile(fn)
	if err != nil {
		return ""
	}
	return string(bytes)
}

func writeSessionIdToFile(tenant, newSessionId string) {
	if newSessionId != "" {
		dir := createTenantDir(tenant)
		fn := filepath.Join(dir, sessionIdFileName)
		os.WriteFile(fn, []byte(newSessionId), 0600)
		trace.Trace("wrote", newSessionId, "to", fn)
	}
}

func getSessionIdFromFile(tenant string) string {
	dir := createTenantDir(tenant)
	fn := filepath.Join(dir, sessionIdFileName)
	bytes, err := os.ReadFile(fn)
	if err != nil {
		util.JustExitMsg("Could not read session id from file, and no session id supplied as argument. Exiting.")
	}
	trace.Trace("Session-id", string(bytes), "found from file", fn)
	return string(bytes)
}