Skip to content

Commit 5a4586b

Browse files
authored
Add --clobber command to unix install script (#209)
* SUMO-272481 add clobber flag to script * SUMO-272481 use correct flag * SUMO-272481 remove unused variable * SUMO-272481 change clobber to boolean * SUMO-272481 add tests for unix script * SUMO-272481 update installOptions of windows * SUMO-272481 trigger new workflow * SUMO-272481 add unix tests
1 parent ebee328 commit 5a4586b

File tree

8 files changed

+149
-3
lines changed

8 files changed

+149
-3
lines changed

install-script/install.sh

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ ARG_SHORT_TIMEOUT='m'
5252
ARG_LONG_TIMEOUT='download-timeout'
5353
ARG_SHORT_TIMEZONE='z'
5454
ARG_LONG_TIMEZONE='timezone'
55+
ARG_SHORT_CLOBBER='C'
56+
ARG_LONG_CLOBBER='clobber'
5557
ARG_SHORT_PACKAGE_PATH='P'
5658
ARG_LONG_PACKAGE_PATH='package-path'
5759

@@ -68,6 +70,7 @@ readonly ARG_SHORT_REMOTELY_MANAGED ARG_LONG_REMOTELY_MANAGED
6870
readonly ARG_SHORT_EPHEMERAL ARG_LONG_EPHEMERAL
6971
readonly ARG_SHORT_TIMEOUT ARG_LONG_TIMEOUT
7072
readonly ARG_SHORT_TIMEZONE ARG_LONG_TIMEZONE
73+
readonly ARG_SHORT_CLOBBER ARG_LONG_CLOBBER
7174
readonly ARG_SHORT_PACKAGE_PATH ARG_LONG_PACKAGE_PATH
7275
readonly DEPRECATED_ARG_LONG_TOKEN DEPRECATED_ENV_TOKEN DEPRECATED_ARG_LONG_SKIP_TOKEN
7376

@@ -102,6 +105,7 @@ INSTALL_HOSTMETRICS=false
102105
REMOTELY_MANAGED=false
103106
EPHEMERAL=false
104107
TIMEZONE=""
108+
CLOBBER=false
105109

106110
LAUNCHD_CONFIG=""
107111
LAUNCHD_ENV_KEY=""
@@ -166,6 +170,7 @@ Supported arguments:
166170
-${ARG_SHORT_EPHEMERAL}, --${ARG_LONG_EPHEMERAL} Delete the collector from Sumo Logic after 12 hours of inactivity.
167171
-${ARG_SHORT_TIMEOUT}, --${ARG_LONG_TIMEOUT} <timeout> Timeout in seconds after which download will fail. Default is ${CURL_MAX_TIME}.
168172
-${ARG_SHORT_TIMEZONE}, --${ARG_LONG_TIMEZONE} TIMEZONE for the collector.
173+
-${ARG_SHORT_CLOBBER}, --${ARG_LONG_CLOBBER} Overwrite existing installation without asking for confirmation.
169174
-${ARG_SHORT_PACKAGE_PATH}, --${ARG_LONG_PACKAGE_PATH} <path> Install package from file path instead of fetching it.
170175
-${ARG_SHORT_YES}, --${ARG_LONG_YES} Disable confirmation asks.
171176
@@ -185,6 +190,7 @@ function set_defaults() {
185190
TOKEN_ENV_FILE="${USER_ENV_DIRECTORY}/token.env"
186191
TIMEZONE="UTC"
187192
PACKAGE_PATH=""
193+
CLOBBER="false"
188194

189195
LAUNCHD_CONFIG="/Library/LaunchDaemons/com.sumologic.otelcol-sumo.plist"
190196
LAUNCHD_ENV_KEY="EnvironmentVariables"
@@ -263,7 +269,7 @@ function parse_options() {
263269
"--${ARG_LONG_TIMEOUT}")
264270
set -- "$@" "-${ARG_SHORT_TIMEOUT}"
265271
;;
266-
"-${ARG_SHORT_TOKEN}"|"-${ARG_SHORT_HELP}"|"-${ARG_SHORT_API}"|"-${ARG_SHORT_OPAMP_API}"|"-${ARG_SHORT_TAG}"|"-${ARG_SHORT_VERSION}"|"-${ARG_SHORT_FIPS}"|"-${ARG_SHORT_YES}"|"-${ARG_SHORT_UNINSTALL}"|"-${ARG_SHORT_UPGRADE}"|"-${ARG_SHORT_PURGE}"|"-${ARG_SHORT_SKIP_TOKEN}"|"-${ARG_SHORT_DOWNLOAD}"|"-${ARG_SHORT_CONFIG_BRANCH}"|"-${ARG_SHORT_BINARY_BRANCH}"|"-${ARG_SHORT_BRANCH}"|"-${ARG_SHORT_KEEP_DOWNLOADS}"|"-${ARG_SHORT_TIMEOUT}"|"-${ARG_SHORT_INSTALL_HOSTMETRICS}"|"-${ARG_SHORT_REMOTELY_MANAGED}"|"-${ARG_SHORT_EPHEMERAL}"|"-${ARG_SHORT_TIMEZONE}"|"-${ARG_SHORT_PACKAGE_PATH}")
272+
"-${ARG_SHORT_TOKEN}"|"-${ARG_SHORT_HELP}"|"-${ARG_SHORT_API}"|"-${ARG_SHORT_OPAMP_API}"|"-${ARG_SHORT_TAG}"|"-${ARG_SHORT_VERSION}"|"-${ARG_SHORT_FIPS}"|"-${ARG_SHORT_YES}"|"-${ARG_SHORT_UNINSTALL}"|"-${ARG_SHORT_UPGRADE}"|"-${ARG_SHORT_PURGE}"|"-${ARG_SHORT_SKIP_TOKEN}"|"-${ARG_SHORT_DOWNLOAD}"|"-${ARG_SHORT_CONFIG_BRANCH}"|"-${ARG_SHORT_BINARY_BRANCH}"|"-${ARG_SHORT_BRANCH}"|"-${ARG_SHORT_KEEP_DOWNLOADS}"|"-${ARG_SHORT_TIMEOUT}"|"-${ARG_SHORT_INSTALL_HOSTMETRICS}"|"-${ARG_SHORT_REMOTELY_MANAGED}"|"-${ARG_SHORT_EPHEMERAL}"|"-${ARG_SHORT_TIMEZONE}"|"-${ARG_SHORT_CLOBBER}"|"-${ARG_SHORT_PACKAGE_PATH}")
267273
set -- "$@" "${arg}"
268274
;;
269275
"--${ARG_LONG_INSTALL_HOSTMETRICS}")
@@ -278,6 +284,9 @@ function parse_options() {
278284
"--${ARG_LONG_TIMEZONE}")
279285
set -- "$@" "-${ARG_SHORT_TIMEZONE}"
280286
;;
287+
"--${ARG_LONG_CLOBBER}")
288+
set -- "$@" "-${ARG_SHORT_CLOBBER}"
289+
;;
281290
"--${ARG_LONG_PACKAGE_PATH}")
282291
set -- "$@" "-${ARG_SHORT_PACKAGE_PATH}"
283292
;;
@@ -293,7 +302,7 @@ function parse_options() {
293302

294303
while true; do
295304
set +e
296-
getopts "${ARG_SHORT_HELP}${ARG_SHORT_TOKEN}:${ARG_SHORT_API}:${ARG_SHORT_OPAMP_API}:${ARG_SHORT_TAG}:${ARG_SHORT_VERSION}:${ARG_SHORT_FIPS}${ARG_SHORT_YES}${ARG_SHORT_UPGRADE}${ARG_SHORT_UNINSTALL}${ARG_SHORT_PURGE}${ARG_SHORT_SKIP_TOKEN}${ARG_SHORT_DOWNLOAD}${ARG_SHORT_KEEP_DOWNLOADS}${ARG_SHORT_CONFIG_BRANCH}:${ARG_SHORT_BINARY_BRANCH}:${ARG_SHORT_BRANCH}:${ARG_SHORT_EPHEMERAL}${ARG_SHORT_TIMEZONE}:${ARG_SHORT_REMOTELY_MANAGED}${ARG_SHORT_INSTALL_HOSTMETRICS}${ARG_SHORT_TIMEOUT}:${ARG_SHORT_PACKAGE_PATH}:" opt
305+
getopts "${ARG_SHORT_HELP}${ARG_SHORT_TOKEN}:${ARG_SHORT_API}:${ARG_SHORT_OPAMP_API}:${ARG_SHORT_TAG}:${ARG_SHORT_VERSION}:${ARG_SHORT_FIPS}${ARG_SHORT_YES}${ARG_SHORT_UPGRADE}${ARG_SHORT_UNINSTALL}${ARG_SHORT_PURGE}${ARG_SHORT_SKIP_TOKEN}${ARG_SHORT_DOWNLOAD}${ARG_SHORT_KEEP_DOWNLOADS}${ARG_SHORT_CONFIG_BRANCH}:${ARG_SHORT_BINARY_BRANCH}:${ARG_SHORT_BRANCH}:${ARG_SHORT_EPHEMERAL}${ARG_SHORT_TIMEZONE}:${ARG_SHORT_CLOBBER}${ARG_SHORT_REMOTELY_MANAGED}${ARG_SHORT_INSTALL_HOSTMETRICS}${ARG_SHORT_TIMEOUT}:${ARG_SHORT_PACKAGE_PATH}:" opt
297306
set -e
298307

299308
# Invalid argument catched, print and exit
@@ -329,6 +338,7 @@ function parse_options() {
329338
"${ARG_SHORT_REMOTELY_MANAGED}") REMOTELY_MANAGED=true ;;
330339
"${ARG_SHORT_EPHEMERAL}") EPHEMERAL=true ;;
331340
"${ARG_SHORT_TIMEZONE}") TIMEZONE="${OPTARG}" ;;
341+
"${ARG_SHORT_CLOBBER}") CLOBBER=true ;;
332342
"${ARG_SHORT_KEEP_DOWNLOADS}") KEEP_DOWNLOADS=true ;;
333343
"${ARG_SHORT_TIMEOUT}") CURL_MAX_TIME="${OPTARG}" ;;
334344
"${ARG_SHORT_TAG}") FIELDS+=("${OPTARG}") ;;
@@ -499,6 +509,10 @@ function setup_config() {
499509
write_timezone "${TIMEZONE}"
500510
fi
501511

512+
if [[ "${CLOBBER}" == "true" ]]; then
513+
write_clobber_true
514+
fi
515+
502516
# Return/stop function execution early as remaining logic only applies
503517
# to locally-managed installations
504518
return
@@ -532,6 +546,11 @@ function setup_config() {
532546
if [[ -n "${TIMEZONE}" ]]; then
533547
write_timezone "${TIMEZONE}"
534548
fi
549+
550+
if [[ "${CLOBBER}" == "true" ]]; then
551+
write_clobber_true
552+
fi
553+
535554
fi
536555
}
537556

@@ -555,6 +574,10 @@ function setup_config_darwin() {
555574
write_timezone "${TIMEZONE}"
556575
fi
557576

577+
if [[ "${CLOBBER}" == "true" ]]; then
578+
write_clobber_true
579+
fi
580+
558581
if [[ -n "${API_BASE_URL}" ]]; then
559582
write_api_url "${API_BASE_URL}"
560583
elif [[ -n "${USER_API_URL}" ]]; then
@@ -781,6 +804,10 @@ function write_timezone() {
781804
"${SUMO_CONFIG_BINARY_PATH}" --set-timezone "$timezone"
782805
}
783806

807+
function write_clobber_true() {
808+
"${SUMO_CONFIG_BINARY_PATH}" --enable-clobber
809+
}
810+
784811
# write api_url to user configuration file
785812
function write_api_url() {
786813
local api_url

install-script/test/check.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,15 @@ func checkTimezoneInConfig(c check) bool {
247247
return assert.Equal(c.test, c.installOptions.timezone, conf.Extensions.Sumologic.Timezone, "timezone is different than expected")
248248
}
249249

250+
func checkClobberInConfig(c check) bool {
251+
conf, err := getConfig(userConfigPath)
252+
if !assert.NoError(c.test, err, "error while reading configuration") {
253+
return false
254+
}
255+
256+
return assert.Equal(c.test, c.installOptions.clobber, conf.Extensions.Sumologic.Clobber, "clobber is different than expected")
257+
}
258+
250259
func PathHasPermissions(t *testing.T, path string, perms uint32) bool {
251260
info, err := os.Stat(path)
252261
if !assert.NoError(t, err) {

install-script/test/check_unix.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type configExtensions struct {
2525
type sumologicExt struct {
2626
Ephemeral bool `yaml:"ephemeral,omitempty"`
2727
Timezone string `yaml:"timezone,omitempty"`
28+
Clobber bool `yaml:"clobber,omitempty"`
2829
}
2930

3031
func checkAbortedDueToNoToken(c check) bool {
@@ -63,6 +64,22 @@ func checkTimezoneConfigInRemote(p string) func(c check) bool {
6364
}
6465
}
6566

67+
func checkClobberEnabledInRemote(p string) func(c check) bool {
68+
return func(c check) bool {
69+
yamlFile, err := os.ReadFile(p)
70+
if assert.NoError(c.test, err, "sumologic remote config file could not be read") {
71+
return false
72+
}
73+
74+
var config configRoot
75+
76+
if assert.NoError(c.test, yaml.Unmarshal(yamlFile, &config), "could not parse yaml") {
77+
return false
78+
}
79+
80+
return config.Extensions.Sumologic.Clobber
81+
}
82+
}
6683
func checkEphemeralEnabledInRemote(p string) func(c check) bool {
6784
return func(c check) bool {
6885
yamlFile, err := os.ReadFile(p)

install-script/test/command_unix.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type installOptions struct {
3030
version string
3131
timezone string
3232
packagePath string
33+
clobber bool
3334
}
3435

3536
func (io *installOptions) string() []string {
@@ -74,6 +75,10 @@ func (io *installOptions) string() []string {
7475
opts = append(opts, "--timezone", io.timezone)
7576
}
7677

78+
if io.clobber {
79+
opts = append(opts, "--clobber")
80+
}
81+
7782
if len(io.tags) > 0 {
7883
for k, v := range io.tags {
7984
opts = append(opts, "--tag", fmt.Sprintf("%s=%s", k, v))

install-script/test/command_windows.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type installOptions struct {
2020
ephemeral bool
2121
version string
2222
timezone string
23+
clobber bool
2324
}
2425

2526
func (io *installOptions) string() []string {

install-script/test/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type sumologicExtension struct {
2121
APIBaseURL string `yaml:"api_base_url"`
2222
Ephemeral bool `yaml:"ephemeral"`
2323
Timezone string `yaml:"time_zone"`
24+
Clobber bool `yaml:"clobber"`
2425
}
2526

2627
type opampExtension struct {

install-script/test/install_darwin_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,32 @@ func TestInstallScriptDarwin(t *testing.T) {
228228
},
229229
installCode: 0,
230230
},
231+
{
232+
name: "installation token, remotely-managed, and clobber",
233+
options: installOptions{
234+
installToken: installToken,
235+
remotelyManaged: true,
236+
clobber: true,
237+
timezone: "UTC",
238+
},
239+
preChecks: notInstalledChecks,
240+
postChecks: []checkFunc{
241+
checkBinaryCreated,
242+
checkBinaryIsRunning,
243+
checkConfigCreated,
244+
checkRemoteConfigDirectoryCreated,
245+
checkConfigFilesOwnershipAndPermissions(systemUser, systemGroup),
246+
checkUserConfigNotCreated,
247+
checkTimezoneConfigInRemote(sumoRemotePath),
248+
checkLaunchdConfigCreated,
249+
checkTokenInLaunchdConfig,
250+
checkUserExists,
251+
checkGroupExists,
252+
checkHomeDirectoryCreated,
253+
checkClobberEnabledInRemote(sumoRemotePath),
254+
},
255+
installCode: 0,
256+
},
231257
{
232258
name: "same installation token in launchd config",
233259
options: installOptions{
@@ -453,6 +479,31 @@ func TestInstallScriptDarwin(t *testing.T) {
453479
},
454480
installCode: 0,
455481
},
482+
{
483+
name: "installation token, locally-managed, and clobber",
484+
options: installOptions{
485+
installToken: installToken,
486+
remotelyManaged: false,
487+
clobber: true,
488+
timezone: "Asia/Kolkata",
489+
},
490+
preChecks: notInstalledChecks,
491+
postChecks: []checkFunc{
492+
checkBinaryCreated,
493+
checkBinaryIsRunning,
494+
checkConfigCreated,
495+
checkConfigFilesOwnershipAndPermissions(systemUser, systemGroup),
496+
checkUserConfigCreated,
497+
checkTimezoneInConfig,
498+
checkLaunchdConfigCreated,
499+
checkTokenInLaunchdConfig,
500+
checkUserExists,
501+
checkGroupExists,
502+
checkHomeDirectoryCreated,
503+
checkClobberInConfig,
504+
},
505+
installCode: 0,
506+
},
456507
} {
457508
t.Run(spec.name, func(t *testing.T) {
458509
if err := runTest(t, &spec); err != nil {

install-script/test/install_unix_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ func TestInstallScript(t *testing.T) {
182182
checkConfigCreated,
183183
},
184184
},
185-
186185
{
187186
name: "locally-managed and timezone",
188187
options: installOptions{
@@ -200,6 +199,42 @@ func TestInstallScript(t *testing.T) {
200199
checkTokenEnvFileCreated,
201200
},
202201
},
202+
{
203+
name: "installation token, locally-managed, and clobber",
204+
options: installOptions{
205+
installToken: installToken,
206+
remotelyManaged: false,
207+
clobber: true,
208+
},
209+
preChecks: notInstalledChecks,
210+
postChecks: []checkFunc{
211+
checkBinaryCreated,
212+
checkBinaryIsRunning,
213+
checkConfigCreated,
214+
checkHostmetricsConfigNotCreated,
215+
checkTokenEnvFileCreated,
216+
checkClobberInConfig,
217+
},
218+
},
219+
{
220+
name: "installation token, remotely-managed and clobber",
221+
options: installOptions{
222+
installToken: installToken,
223+
remotelyManaged: true,
224+
clobber: true,
225+
},
226+
preChecks: notInstalledChecks,
227+
postChecks: []checkFunc{
228+
checkBinaryCreated,
229+
checkBinaryIsRunning,
230+
checkConfigCreated,
231+
checkClobberEnabledInRemote(sumoRemotePath),
232+
checkEphemeralConfigFileNotCreated(ephemeralConfigPath),
233+
checkEphemeralNotEnabledInRemote(sumoRemotePath),
234+
checkHostmetricsConfigNotCreated,
235+
checkTokenEnvFileCreated,
236+
},
237+
},
203238
} {
204239
t.Run(spec.name, func(t *testing.T) {
205240
if err := runTest(t, &spec); err != nil {

0 commit comments

Comments
 (0)