...
1 package helper
2
3 import (
4 "encoding/json"
5 "fmt"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "regexp"
10 "strconv"
11 "strings"
12
13 . "github.com/onsi/gomega"
14
15 "github.com/redhat-developer/odo/pkg/podman"
16 )
17
18 func getBooleanValueFromEnvVar(envvar string, defaultValue bool) bool {
19 strVal := os.Getenv("PODMAN_USE_NAMESPACES")
20 boolValue, err := strconv.ParseBool(strVal)
21 if err != nil {
22 return defaultValue
23 }
24 return boolValue
25 }
26
27 func GenerateAndSetContainersConf(dir string) {
28 useNamespaces := getBooleanValueFromEnvVar("PODMAN_USE_NAMESPACES", true)
29 if !useNamespaces {
30 return
31 }
32 ns := GenerateProjectName()
33 containersConfPath := filepath.Join(dir, "containers.conf")
34 err := CreateFileWithContent(containersConfPath, fmt.Sprintf(`
35 [engine]
36 namespace=%q
37 `, ns))
38 Expect(err).ShouldNot(HaveOccurred())
39 os.Setenv("CONTAINERS_CONF", containersConfPath)
40 }
41
42
43 func ExtractK8sAndOcComponentsFromOutputOnPodman(out string) []string {
44 lines, err := ExtractLines(out)
45 Expect(err).ShouldNot(HaveOccurred())
46
47 var handled []string
48
49
50
51
52 re := regexp.MustCompile(`(?:Kubernetes|OpenShift|Kubernetes/Openshift) components are not supported on Podman\.\s*Skipping:\s*([^\n]+)\.`)
53 for _, l := range lines {
54 matches := re.FindStringSubmatch(l)
55 if len(matches) > 1 {
56 handled = append(handled, strings.Split(matches[1], ", ")...)
57 }
58 }
59
60 return handled
61 }
62
63
64 func GetPodmanVersion() string {
65 cmd := exec.Command("podman", "version", "--format", "json")
66 out, err := cmd.Output()
67 Expect(err).ToNot(HaveOccurred(), func() string {
68 if exiterr, ok := err.(*exec.ExitError); ok {
69 err = fmt.Errorf("%s: %s", err, string(exiterr.Stderr))
70 }
71 return err.Error()
72 })
73 var result podman.SystemVersionReport
74 err = json.Unmarshal(out, &result)
75 Expect(err).ToNot(HaveOccurred())
76 return result.Client.Version
77 }
78
79
80
81 func GenerateDelayedPodman(commonVarContext string, delaySecond int) string {
82 delayer := filepath.Join(commonVarContext, "podman-cmd-delayer")
83 fileContent := fmt.Sprintf(`#!/bin/bash
84
85 echo Delaying command execution... >&2
86 sleep %d
87 echo "$@"
88 `, delaySecond)
89 err := CreateFileWithContentAndPerm(delayer, fileContent, 0755)
90 Expect(err).ToNot(HaveOccurred())
91 return delayer
92 }
93
View as plain text