github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/systemd/generate/common.go (about)

     1  package generate
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  // EnvVariable "PODMAN_SYSTEMD_UNIT" is set in all generated systemd units and
    11  // is set to the unit's (unique) name.
    12  const EnvVariable = "PODMAN_SYSTEMD_UNIT"
    13  
    14  // RestartPolicies includes all valid restart policies to be used in a unit
    15  // file.
    16  var RestartPolicies = []string{"no", "on-success", "on-failure", "on-abnormal", "on-watchdog", "on-abort", "always"}
    17  
    18  // validateRestartPolicy checks that the user-provided policy is valid.
    19  func validateRestartPolicy(restart string) error {
    20  	for _, i := range RestartPolicies {
    21  		if i == restart {
    22  			return nil
    23  		}
    24  	}
    25  	return errors.Errorf("%s is not a valid restart policy", restart)
    26  }
    27  
    28  const headerTemplate = `# {{.ServiceName}}.service
    29  # autogenerated by Podman {{.PodmanVersion}}
    30  {{- if .TimeStamp}}
    31  # {{.TimeStamp}}
    32  {{- end}}
    33  
    34  [Unit]
    35  Description=Podman {{.ServiceName}}.service
    36  Documentation=man:podman-generate-systemd(1)
    37  Wants=network.target
    38  After=network-online.target
    39  `
    40  
    41  // filterPodFlags removes --pod and --pod-id-file from the specified command.
    42  func filterPodFlags(command []string) []string {
    43  	processed := []string{}
    44  	for i := 0; i < len(command); i++ {
    45  		s := command[i]
    46  		if s == "--pod" || s == "--pod-id-file" {
    47  			i++
    48  			continue
    49  		}
    50  		if strings.HasPrefix(s, "--pod=") || strings.HasPrefix(s, "--pod-id-file=") {
    51  			continue
    52  		}
    53  		processed = append(processed, s)
    54  	}
    55  	return processed
    56  }
    57  
    58  // quoteArguments makes sure that all arguments with at least one whitespace
    59  // are quoted to make sure those are interpreted as one argument instead of
    60  // multiple ones.
    61  func quoteArguments(command []string) []string {
    62  	for i := range command {
    63  		if strings.ContainsAny(command[i], " \t") {
    64  			command[i] = strconv.Quote(command[i])
    65  		}
    66  	}
    67  	return command
    68  }