-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.go
157 lines (145 loc) · 3.99 KB
/
install.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"fmt"
"os"
"path"
"strings"
)
const templateEnv = `AGENTCORE_TOKEN = <AGENTCORE_TOKEN>
AGENT_TOKEN = <AGENT_TOKEN>
AGENTCORE_ZONE_ID = <AGENTCORE_ZONE_ID>
SOCAT_TARGET_ADDR = ""
`
const templateCompose = `## InfraSonar docker-compose.yml file
##
## !! This file is managed by InfraSonar !!
services:
agentcore:
environment:
AGENTCORE_ZONE: ${AGENTCORE_ZONE_ID}
HUB_HOST: <HUB_ADDRESS>
LOG_COLORIZED: 1
LOG_LEVEL: info
TOKEN: ${AGENTCORE_TOKEN}
image: ghcr.io/infrasonar/agentcore
labels: &id001
com.centurylinklabs.watchtower.scope: ${AGENTCORE_TOKEN}
logging: &id002
options:
max-size: 5m
network_mode: host
restart: always
volumes: &id003
- ./data:/data/
rapp:
environment:
COMPOSE_FILE: <INSTALLATION_PATH>/docker-compose.yml
CONFIG_FILE: <INSTALLATION_PATH>/data/config/infrasonar.yaml
ENV_FILE: <INSTALLATION_PATH>/.env
USE_DEVELOPMENT: <USE_DEVELOPMENT>
image: ghcr.io/infrasonar/rapp
labels: *id001
logging: *id002
network_mode: host
restart: always
volumes:
- <INSTALLATION_PATH>:<INSTALLATION_PATH>
- /var/run/docker.sock:/var/run/docker.sock
watchtower:
environment:
WATCHTOWER_CLEANUP: true
WATCHTOWER_INCLUDE_RESTARTING: true
WATCHTOWER_POLL_INTERVAL: 21600
WATCHTOWER_SCOPE: ${AGENTCORE_TOKEN}
image: containrrr/watchtower
labels: *id001
logging: *id002
network_mode: host
restart: always
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /etc/localtime:/etc/localtime:ro
x-infrasonar-template:
labels: *id001
logging: *id002
network_mode: host
restart: always
volumes: *id003
`
const configContent = `## WARNING: InfraSonar will make 'password' and 'secret' values unreadable but
## this must not be regarded as true encryption as the encryption key is
## publicly available.
##
## Example configuration for 'myprobe' collector:
##
## myprobe:
## config:
## username: alice
## password: "secret password"
## assets:
## - id: [12345, 34567]
## config:
## username: bob
## password: "my secret"
##
## !! This file is managed by InfraSonar !!
##
## It's okay to add custom probe configuration for when you want to
## specify the "_use" value for assets. The appliance manager will not
## overwrite these custom probe configurations. You can also add additional
## assets configurations for managed probes.
`
func installEnv(args *Arguments) error {
content := strings.Replace(templateEnv, "<AGENTCORE_TOKEN>", args.agentcoreToken, 1)
content = strings.Replace(content, "<AGENT_TOKEN>", args.agentToken, 1)
content = strings.Replace(content, "<AGENTCORE_ZONE_ID>", fmt.Sprint(args.zone), 1)
fn := path.Join(args.installationPath, ".env")
fp, err := os.Create(fn)
if err != nil {
return err
}
defer fp.Close()
_, err = fp.WriteString(content)
return err
}
func installCompose(args *Arguments) error {
hub_address := "hub.infrasonar.com"
use_development := "0"
if args.useDevelopment {
hub_address = "devhub.infrasonar.com"
use_development = "1"
}
content := strings.Replace(templateCompose, "<HUB_ADDRESS>", hub_address, 1)
content = strings.ReplaceAll(content, "<INSTALLATION_PATH>", args.installationPath)
content = strings.Replace(content, "<USE_DEVELOPMENT>", use_development, 1)
fn := path.Join(args.installationPath, "docker-compose.yml")
fp, err := os.Create(fn)
if err != nil {
return err
}
defer fp.Close()
_, err = fp.WriteString(content)
return err
}
func installConfig(args *Arguments) error {
fn := path.Join(args.installationPath, "data", "config", "infrasonar.yaml")
fp, err := os.Create(fn)
if err != nil {
return err
}
defer fp.Close()
_, err = fp.WriteString(configContent)
return err
}
func install(args *Arguments) error {
if err := installEnv(args); err != nil {
return err
}
if err := installCompose(args); err != nil {
return err
}
if err := installConfig(args); err != nil {
return err
}
return nil
}