Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cluster: fix malformed commands in local executor #1734

Merged
merged 3 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions pkg/cluster/executor/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,19 @@ var _ ctxt.Executor = &Local{}

// Execute implements Executor interface.
func (l *Local) Execute(ctx context.Context, cmd string, sudo bool, timeout ...time.Duration) ([]byte, []byte, error) {
// change wd to default home
cmd = fmt.Sprintf("cd; %s", cmd)
// get current user name
user, err := user.Current()
if err != nil {
return nil, nil, err
}

AstroProfundis marked this conversation as resolved.
Show resolved Hide resolved
// try to acquire root permission
if l.Sudo || sudo {
cmd = fmt.Sprintf("/usr/bin/sudo -H -u root bash -c 'cd; %s'", cmd)
} else {
cmd = fmt.Sprintf("/usr/bin/sudo -H -u %s bash -c 'cd; %s'", l.Config.User, cmd)
cmd = fmt.Sprintf("/usr/bin/sudo -H -u root bash -c \"%s\"", strings.ReplaceAll(cmd, "\"", "\\\""))
} else if l.Config.User != user.Name {
cmd = fmt.Sprintf("/usr/bin/sudo -H -u %s bash -c \"%s\"", l.Config.User, strings.ReplaceAll(cmd, "\"", "\\\""))
}

// set a basic PATH in case it's empty on login
Expand All @@ -67,14 +75,14 @@ func (l *Local) Execute(ctx context.Context, cmd string, sudo bool, timeout ...t
defer cancel()
}

command := exec.CommandContext(ctx, "/bin/sh", "-c", cmd)
command := exec.CommandContext(ctx, "/bin/bash", "-c", cmd)

stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
command.Stdout = stdout
command.Stderr = stderr

err := command.Run()
err = command.Run()

zap.L().Info("LocalCommand",
zap.String("cmd", cmd),
Expand Down Expand Up @@ -117,7 +125,7 @@ func (l *Local) Transfer(ctx context.Context, src, dst string, download bool, li
cmd = fmt.Sprintf("/usr/bin/sudo -H -u root bash -c \"cp %[1]s %[2]s && chown %[3]s:$(id -g -n %[3]s) %[2]s\"", src, dst, l.Config.User)
}

command := exec.Command("/bin/sh", "-c", cmd)
command := exec.Command("/bin/bash", "-c", cmd)
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
command.Stdout = stdout
Expand Down
4 changes: 2 additions & 2 deletions pkg/cluster/executor/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func TestLocalExecuteWithQuotes(t *testing.T) {
defer os.RemoveAll(deployDir)

cmds := []string{
fmt.Sprintf(`find %s -type f -exec sed -i "s/\${DS_.*-CLUSTER}/hello/g" {} \;`, deployDir),
fmt.Sprintf(`find %s -type f -exec sed -i "s/DS_.*-CLUSTER/hello/g" {} \;`, deployDir),
fmt.Sprintf(`find %s -type f -exec sed -i 's/\${DS_.*-CLUSTER}/hello/g' {} \;`, deployDir),
fmt.Sprintf(`find %s -type f -exec sed -i 's/DS_.*-CLUSTER/hello/g' {} \;`, deployDir),
`ls '/tmp'`,
}
for _, cmd := range cmds {
Expand Down
12 changes: 6 additions & 6 deletions pkg/cluster/spec/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,12 @@ func (i *GrafanaInstance) initDashboards(ctx context.Context, e ctxt.Executor, s

// Deal with the cluster name
for _, cmd := range []string{
`find %s -type f -exec sed -i "s/\${DS_.*-CLUSTER}/%s/g" {} \;`,
`find %s -type f -exec sed -i "s/DS_.*-CLUSTER/%s/g" {} \;`,
`find %s -type f -exec sed -i "s/\${DS_LIGHTNING}/%s/g" {} \;`,
`find %s -type f -exec sed -i "s/DS_LIGHTNING/%s/g" {} \;`,
`find %s -type f -exec sed -i "s/test-cluster/%s/g" {} \;`,
`find %s -type f -exec sed -i "s/Test-Cluster/%s/g" {} \;`,
`find %s -type f -exec sed -i 's/\${DS_.*-CLUSTER}/%s/g' {} \;`,
`find %s -type f -exec sed -i 's/DS_.*-CLUSTER/%s/g' {} \;`,
`find %s -type f -exec sed -i 's/\${DS_LIGHTNING}/%s/g' {} \;`,
`find %s -type f -exec sed -i 's/DS_LIGHTNING/%s/g' {} \;`,
`find %s -type f -exec sed -i 's/test-cluster/%s/g' {} \;`,
`find %s -type f -exec sed -i 's/Test-Cluster/%s/g' {} \;`,
} {
cmd := fmt.Sprintf(cmd, dashboardsDir, clusterName)
_, stderr, err := e.Execute(ctx, cmd, false)
Expand Down
8 changes: 4 additions & 4 deletions pkg/cluster/spec/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func (i *MonitorInstance) installRules(ctx context.Context, e ctxt.Executor, dep
`find %[1]s -type f -name "*.rules.yml" -delete`,
`find %[2]s/dm-master/conf -type f -name "*.rules.yml" -exec cp {} %[1]s \;`,
"rm -rf %[2]s",
`find %[1]s -maxdepth 1 -type f -name "*.rules.yml" -exec sed -i "s/ENV_LABELS_ENV/%[3]s/g" {} \;`,
`find %[1]s -maxdepth 1 -type f -name "*.rules.yml" -exec sed -i 's/ENV_LABELS_ENV/%[3]s/g' {} \;`,
}
_, stderr, err = e.Execute(ctx, fmt.Sprintf(strings.Join(cmds, " && "), targetDir, tmp, clusterName), false)
if err != nil {
Expand All @@ -420,7 +420,7 @@ func (i *MonitorInstance) initRules(ctx context.Context, e ctxt.Executor, spec *
"mkdir -p %[1]s/conf",
`find %[1]s/conf -type f -name "*.rules.yml" -delete`,
`find %[1]s/bin/prometheus -maxdepth 1 -type f -name "*.rules.yml" -exec cp {} %[1]s/conf/ \;`,
`find %[1]s/conf -maxdepth 1 -type f -name "*.rules.yml" -exec sed -i -e "s/ENV_LABELS_ENV/%[2]s/g" {} \;`,
`find %[1]s/conf -maxdepth 1 -type f -name "*.rules.yml" -exec sed -i -e 's/ENV_LABELS_ENV/%[2]s/g' {} \;`,
}

_, stderr, err := e.Execute(ctx, fmt.Sprintf(strings.Join(cmds, " && "), paths.Deploy, clusterName), false)
Expand All @@ -438,8 +438,8 @@ func (i *MonitorInstance) initRules(ctx context.Context, e ctxt.Executor, spec *
}
// only need to render the cluster name
cmds = []string{
`find %[1]s/conf -maxdepth 1 -type f -name "*.rules.yml" -exec sed -i -e "s/env: [^ ]*/env: %[2]s/g" {} \;`,
`find %[1]s/conf -maxdepth 1 -type f -name "*.rules.yml" -exec sed -i -e "s/cluster: [^ ]*,/cluster: %[2]s,/g" {} \;`,
`find %[1]s/conf -maxdepth 1 -type f -name "*.rules.yml" -exec sed -i -e 's/env: [^ ]*/env: %[2]s/g' {} \;`,
`find %[1]s/conf -maxdepth 1 -type f -name "*.rules.yml" -exec sed -i -e 's/cluster: [^ ]*,/cluster: %[2]s,/g' {} \;`,
}
_, stderr, err := e.Execute(ctx, fmt.Sprintf(strings.Join(cmds, " && "), paths.Deploy, clusterName), false)
if err != nil {
Expand Down