aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/admin/jvm/container.go
blob: 32110c5e1bccc7cfeeb3672b1b2e0a308cab44de (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Author: arnej

package jvm

import (
	"fmt"
	"sort"
	"strings"

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

type Container interface {
	ServiceName() string
	ConfigId() string
	ArgForMain() string
	JvmOptions() *Options
	Exec()
	exportExtraEnv(ps *prog.Spec)
}

type containerBase struct {
	configId    string
	serviceName string
	jvmOpts     *Options
	propsFile   string
}

func (cb *containerBase) ServiceName() string {
	return cb.serviceName
}

func (cb *containerBase) JvmOptions() *Options {
	return cb.jvmOpts
}

func (cb *containerBase) ConfigId() string {
	return cb.configId
}

func keysOfMap(m map[string]string) []string {
	keys := make([]string, 0, len(m))
	for k, _ := range m {
		keys = append(keys, k)
	}
	return keys
}

func readableEnv(env map[string]string) string {
	keys := keysOfMap(env)
	sort.Strings(keys)
	var buf strings.Builder
	for _, k := range keys {
		fmt.Fprintf(&buf, " %s=%s", k, env[k])
	}
	return buf.String()
}

func (cb *containerBase) Exec() {
	argv := util.ArrayListOf(cb.JvmOptions().Args())
	argv.Insert(0, "java")
	p := prog.NewSpec(argv)
	p.ConfigureNumaCtl()
	cb.JvmOptions().exportEnvSettings(p)
	if cb.propsFile != "" {
		writeEnvAsProperties(p.EffectiveEnv(), cb.propsFile)
	}
	trace.Info("JVM env:", readableEnv(p.Env))
	trace.Info("JVM exec:", argv)
	err := p.Run()
	util.JustExitWith(err)
}