2019-04-22 21:35:39 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Ceph-CSI Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-08-22 17:19:06 +00:00
|
|
|
"context"
|
2020-06-25 06:41:35 +00:00
|
|
|
"errors"
|
2019-04-22 21:35:39 +00:00
|
|
|
"fmt"
|
2022-03-31 13:29:33 +00:00
|
|
|
"os"
|
2019-04-22 21:35:39 +00:00
|
|
|
"os/exec"
|
2021-12-22 03:07:25 +00:00
|
|
|
"time"
|
2019-06-10 07:22:13 +00:00
|
|
|
|
2021-08-24 15:03:25 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
|
|
|
|
2020-01-16 15:33:54 +00:00
|
|
|
"github.com/ceph/go-ceph/rados"
|
2019-04-22 21:35:39 +00:00
|
|
|
)
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// InvalidPoolID used to denote an invalid pool.
|
2020-01-24 16:26:56 +00:00
|
|
|
const InvalidPoolID int64 = -1
|
|
|
|
|
2022-03-31 13:29:33 +00:00
|
|
|
// ExecuteCommandWithNSEnter executes passed in program with args with nsenter
|
|
|
|
// and returns separate stdout and stderr streams. In case ctx is not set to
|
|
|
|
// context.TODO(), the command will be logged after it was executed.
|
|
|
|
func ExecuteCommandWithNSEnter(ctx context.Context, netPath, program string, args ...string) (string, string, error) {
|
|
|
|
var (
|
2022-04-14 10:15:11 +00:00
|
|
|
stdoutBuf bytes.Buffer
|
|
|
|
stderrBuf bytes.Buffer
|
|
|
|
nsenter = "nsenter"
|
2022-03-31 13:29:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// check netPath exists
|
|
|
|
if _, err := os.Stat(netPath); err != nil {
|
|
|
|
return "", "", fmt.Errorf("failed to get stat for %s %w", netPath, err)
|
|
|
|
}
|
|
|
|
// nsenter --net=%s -- <program> <args>
|
|
|
|
args = append([]string{fmt.Sprintf("--net=%s", netPath), "--", program}, args...)
|
2022-04-14 10:15:11 +00:00
|
|
|
sanitizedArgs := StripSecretInArgs(args)
|
|
|
|
cmd := exec.Command(nsenter, args...) // #nosec:G204, commands executing not vulnerable.
|
2022-03-31 13:29:33 +00:00
|
|
|
cmd.Stdout = &stdoutBuf
|
|
|
|
cmd.Stderr = &stderrBuf
|
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
stdout := stdoutBuf.String()
|
|
|
|
stderr := stderrBuf.String()
|
|
|
|
|
|
|
|
if err != nil {
|
2022-04-14 10:15:11 +00:00
|
|
|
err = fmt.Errorf("an error (%w) occurred while running %s args: %v", err, nsenter, sanitizedArgs)
|
2022-03-31 13:29:33 +00:00
|
|
|
if ctx != context.TODO() {
|
|
|
|
log.UsefulLog(ctx, "%s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdout, stderr, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx != context.TODO() {
|
2022-04-14 10:15:11 +00:00
|
|
|
log.UsefulLog(ctx, "command succeeded: %s %v", nsenter, sanitizedArgs)
|
2022-03-31 13:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return stdout, stderr, nil
|
|
|
|
}
|
|
|
|
|
2020-07-22 12:11:41 +00:00
|
|
|
// ExecCommand executes passed in program with args and returns separate stdout
|
|
|
|
// and stderr streams. In case ctx is not set to context.TODO(), the command
|
|
|
|
// will be logged after it was executed.
|
2020-07-22 12:53:22 +00:00
|
|
|
func ExecCommand(ctx context.Context, program string, args ...string) (string, string, error) {
|
2019-04-22 21:35:39 +00:00
|
|
|
var (
|
2020-07-20 12:49:05 +00:00
|
|
|
cmd = exec.Command(program, args...) // #nosec:G204, commands executing not vulnerable.
|
2019-06-08 05:06:03 +00:00
|
|
|
sanitizedArgs = StripSecretInArgs(args)
|
|
|
|
stdoutBuf bytes.Buffer
|
|
|
|
stderrBuf bytes.Buffer
|
2019-04-22 21:35:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
cmd.Stdout = &stdoutBuf
|
|
|
|
cmd.Stderr = &stderrBuf
|
|
|
|
|
2020-07-22 12:53:22 +00:00
|
|
|
err := cmd.Run()
|
|
|
|
stdout := stdoutBuf.String()
|
|
|
|
stderr := stderrBuf.String()
|
|
|
|
|
|
|
|
if err != nil {
|
2020-07-22 12:11:41 +00:00
|
|
|
err = fmt.Errorf("an error (%w) occurred while running %s args: %v", err, program, sanitizedArgs)
|
|
|
|
if ctx != context.TODO() {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.UsefulLog(ctx, "%s", err)
|
2020-07-22 12:11:41 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-07-22 12:53:22 +00:00
|
|
|
return stdout, stderr, err
|
2020-07-22 12:11:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ctx != context.TODO() {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.UsefulLog(ctx, "command succeeded: %s %v", program, sanitizedArgs)
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 12:53:22 +00:00
|
|
|
return stdout, stderr, nil
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 03:07:25 +00:00
|
|
|
// ExecCommandWithTimeout executes passed in program with args, timeout and
|
|
|
|
// returns separate stdout and stderr streams. If the command is not executed
|
|
|
|
// within given timeout, the process will be killed. In case ctx is not set to
|
|
|
|
// context.TODO(), the command will be logged after it was executed.
|
|
|
|
func ExecCommandWithTimeout(
|
|
|
|
ctx context.Context,
|
|
|
|
timeout time.Duration,
|
|
|
|
program string,
|
|
|
|
args ...string) (
|
|
|
|
string,
|
|
|
|
string,
|
|
|
|
error) {
|
|
|
|
var (
|
|
|
|
sanitizedArgs = StripSecretInArgs(args)
|
|
|
|
stdoutBuf bytes.Buffer
|
|
|
|
stderrBuf bytes.Buffer
|
|
|
|
)
|
|
|
|
|
|
|
|
cctx, cancel := context.WithTimeout(context.Background(), timeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
cmd := exec.CommandContext(cctx, program, args...) // #nosec:G204, commands executing not vulnerable.
|
|
|
|
cmd.Stdout = &stdoutBuf
|
|
|
|
cmd.Stderr = &stderrBuf
|
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
stdout := stdoutBuf.String()
|
|
|
|
stderr := stderrBuf.String()
|
|
|
|
if err != nil {
|
|
|
|
// if its a timeout log return context deadline exceeded error message
|
|
|
|
if errors.Is(cctx.Err(), context.DeadlineExceeded) {
|
|
|
|
err = fmt.Errorf("timeout: %w", cctx.Err())
|
|
|
|
}
|
|
|
|
err = fmt.Errorf("an error (%w) and stderror (%s) occurred while running %s args: %v",
|
|
|
|
err,
|
|
|
|
stderr,
|
|
|
|
program,
|
|
|
|
sanitizedArgs)
|
|
|
|
|
|
|
|
if ctx != context.TODO() {
|
|
|
|
log.ErrorLog(ctx, "%s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdout, stderr, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx != context.TODO() {
|
|
|
|
log.UsefulLog(ctx, "command succeeded: %s %v", program, sanitizedArgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdout, stderr, nil
|
|
|
|
}
|
|
|
|
|
2020-01-16 15:33:54 +00:00
|
|
|
// GetPoolID fetches the ID of the pool that matches the passed in poolName
|
2020-07-19 12:21:03 +00:00
|
|
|
// parameter.
|
2020-05-14 11:23:55 +00:00
|
|
|
func GetPoolID(monitors string, cr *Credentials, poolName string) (int64, error) {
|
2020-01-16 15:33:54 +00:00
|
|
|
conn, err := connPool.Get(monitors, cr.ID, cr.KeyFile)
|
2019-06-28 01:10:32 +00:00
|
|
|
if err != nil {
|
2020-05-14 12:28:55 +00:00
|
|
|
return InvalidPoolID, err
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
2020-01-16 15:33:54 +00:00
|
|
|
defer connPool.Put(conn)
|
2019-04-22 21:35:39 +00:00
|
|
|
|
2020-01-16 15:33:54 +00:00
|
|
|
id, err := conn.GetPoolByName(poolName)
|
2020-07-02 21:43:40 +00:00
|
|
|
if errors.Is(err, rados.ErrNotFound) {
|
2020-07-08 23:00:23 +00:00
|
|
|
return InvalidPoolID, fmt.Errorf("%w: pool (%s) not found in Ceph cluster",
|
|
|
|
ErrPoolNotFound, poolName)
|
2020-01-16 15:33:54 +00:00
|
|
|
} else if err != nil {
|
2020-05-14 12:28:55 +00:00
|
|
|
return InvalidPoolID, err
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 15:33:54 +00:00
|
|
|
return id, nil
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 09:24:01 +00:00
|
|
|
// GetPoolName fetches the pool whose pool ID is equal to the requested poolID
|
2020-07-19 12:21:03 +00:00
|
|
|
// parameter.
|
2020-05-14 13:28:14 +00:00
|
|
|
func GetPoolName(monitors string, cr *Credentials, poolID int64) (string, error) {
|
2020-03-13 09:24:01 +00:00
|
|
|
conn, err := connPool.Get(monitors, cr.ID, cr.KeyFile)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-03-13 09:24:01 +00:00
|
|
|
defer connPool.Put(conn)
|
2019-04-22 21:35:39 +00:00
|
|
|
|
2020-03-13 09:24:01 +00:00
|
|
|
name, err := conn.GetPoolByID(poolID)
|
2021-06-14 10:40:57 +00:00
|
|
|
if errors.Is(err, rados.ErrNotFound) {
|
|
|
|
return "", fmt.Errorf("%w: pool ID(%d) not found in Ceph cluster",
|
2020-07-08 23:00:23 +00:00
|
|
|
ErrPoolNotFound, poolID)
|
2021-06-14 10:40:57 +00:00
|
|
|
} else if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to get pool ID %d: %w", poolID, err)
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-03-13 09:24:01 +00:00
|
|
|
return name, nil
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 16:26:56 +00:00
|
|
|
// GetPoolIDs searches a list of pools in a cluster and returns the IDs of the pools that matches
|
|
|
|
// the passed in pools
|
2020-07-19 12:21:03 +00:00
|
|
|
// TODO this should take in a list and return a map[string(poolname)]int64(poolID).
|
2020-01-24 16:26:56 +00:00
|
|
|
func GetPoolIDs(ctx context.Context, monitors, journalPool, imagePool string, cr *Credentials) (int64, int64, error) {
|
2020-05-14 11:23:55 +00:00
|
|
|
journalPoolID, err := GetPoolID(monitors, cr, journalPool)
|
2020-01-24 16:26:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return InvalidPoolID, InvalidPoolID, err
|
|
|
|
}
|
|
|
|
|
|
|
|
imagePoolID := journalPoolID
|
|
|
|
if imagePool != journalPool {
|
2020-05-14 11:23:55 +00:00
|
|
|
imagePoolID, err = GetPoolID(monitors, cr, imagePool)
|
2020-01-24 16:26:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return InvalidPoolID, InvalidPoolID, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return journalPoolID, imagePoolID, nil
|
|
|
|
}
|
|
|
|
|
2019-04-22 21:35:39 +00:00
|
|
|
// CreateObject creates the object name passed in and returns ErrObjectExists if the provided object
|
2020-07-19 12:21:03 +00:00
|
|
|
// is already present in rados.
|
2019-08-22 17:19:06 +00:00
|
|
|
func CreateObject(ctx context.Context, monitors string, cr *Credentials, poolName, namespace, objectName string) error {
|
2020-06-02 08:03:32 +00:00
|
|
|
conn := ClusterConnection{}
|
|
|
|
err := conn.Connect(monitors, cr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
2020-06-02 08:03:32 +00:00
|
|
|
defer conn.Destroy()
|
|
|
|
|
|
|
|
ioctx, err := conn.GetIoctx(poolName)
|
|
|
|
if err != nil {
|
2020-07-08 23:00:23 +00:00
|
|
|
if errors.Is(err, ErrPoolNotFound) {
|
|
|
|
err = JoinErrors(ErrObjectNotFound, err)
|
2020-06-02 08:03:32 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-02 08:03:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer ioctx.Destroy()
|
2019-05-28 19:03:18 +00:00
|
|
|
|
|
|
|
if namespace != "" {
|
2020-06-02 08:03:32 +00:00
|
|
|
ioctx.SetNamespace(namespace)
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 08:03:32 +00:00
|
|
|
err = ioctx.Create(objectName, rados.CreateExclusive)
|
2020-07-02 21:43:40 +00:00
|
|
|
if errors.Is(err, rados.ErrObjectExists) {
|
2020-07-08 23:00:23 +00:00
|
|
|
return JoinErrors(ErrObjectExists, err)
|
2020-06-02 08:03:32 +00:00
|
|
|
} else if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed creating omap (%s) in pool (%s): (%v)", objectName, poolName, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-04-22 21:35:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveObject removes the entire omap name passed in and returns ErrObjectNotFound is provided omap
|
2020-07-19 12:21:03 +00:00
|
|
|
// is not found in rados.
|
2019-08-22 17:19:06 +00:00
|
|
|
func RemoveObject(ctx context.Context, monitors string, cr *Credentials, poolName, namespace, oMapName string) error {
|
2020-06-02 08:02:02 +00:00
|
|
|
conn := ClusterConnection{}
|
|
|
|
err := conn.Connect(monitors, cr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
2020-06-02 08:02:02 +00:00
|
|
|
defer conn.Destroy()
|
|
|
|
|
|
|
|
ioctx, err := conn.GetIoctx(poolName)
|
|
|
|
if err != nil {
|
2020-07-08 23:00:23 +00:00
|
|
|
if errors.Is(err, ErrPoolNotFound) {
|
|
|
|
err = JoinErrors(ErrObjectNotFound, err)
|
2020-06-02 08:02:02 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-02 08:02:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer ioctx.Destroy()
|
2019-05-28 19:03:18 +00:00
|
|
|
|
|
|
|
if namespace != "" {
|
2020-06-02 08:02:02 +00:00
|
|
|
ioctx.SetNamespace(namespace)
|
2019-05-28 19:03:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 08:02:02 +00:00
|
|
|
err = ioctx.Delete(oMapName)
|
2020-07-02 21:43:40 +00:00
|
|
|
if errors.Is(err, rados.ErrNotFound) {
|
2020-07-08 23:00:23 +00:00
|
|
|
return JoinErrors(ErrObjectNotFound, err)
|
2020-06-02 08:02:02 +00:00
|
|
|
} else if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed removing omap (%s) in pool (%s): (%v)", oMapName, poolName, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-04-22 21:35:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|