This repository has been archived by the owner on May 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
util.go
152 lines (134 loc) · 5.03 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package util
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/flyteorg/flytectl/pkg/configutil"
"github.com/flyteorg/flytectl/pkg/docker"
"github.com/enescakir/emoji"
hversion "github.com/hashicorp/go-version"
)
const (
ProgressSuccessMessage = "Flyte is ready! Flyte UI is available at"
ProgressSuccessMessagePending = "Flyte would be ready after this! Flyte UI would be available at"
SandBoxConsolePort = 30081
DemoConsolePort = 30080
)
var Ext string
// WriteIntoFile will write content in a file
func WriteIntoFile(data []byte, file string) error {
err := ioutil.WriteFile(file, data, os.ModePerm)
if err != nil {
return err
}
return nil
}
func CreatePathAndFile(pathToConfig string) error {
p, err := filepath.Abs(pathToConfig)
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(p), os.ModePerm); err != nil {
return err
}
// Created a empty file with right permission
if _, err := os.Stat(p); err != nil {
if os.IsNotExist(err) {
if err := os.WriteFile(p, []byte(""), os.ModePerm); err != nil {
return err
}
}
}
return nil
}
// SetupFlyteDir will create .flyte dir if not exist
func SetupFlyteDir() error {
if err := os.MkdirAll(docker.FlyteSandboxConfigDir, os.ModePerm); err != nil {
return err
}
// Created a empty file with right permission
if _, err := os.Stat(docker.Kubeconfig); err != nil {
if os.IsNotExist(err) {
if err := os.WriteFile(docker.Kubeconfig, []byte(""), os.ModePerm); err != nil {
return err
}
}
}
return nil
}
// PrintDemoStartMessage will print demo start success message
func PrintDemoStartMessage(flyteConsolePort int, kubeconfigLocation string, dryRun bool) {
kubeconfig := strings.Join([]string{
"$KUBECONFIG",
kubeconfigLocation,
}, ":")
var successMsg string
if dryRun {
successMsg = fmt.Sprintf("%v http://localhost:%v/console", ProgressSuccessMessagePending, flyteConsolePort)
} else {
successMsg = fmt.Sprintf("%v http://localhost:%v/console", ProgressSuccessMessage, flyteConsolePort)
}
fmt.Printf("%v %v %v %v %v \n", emoji.ManTechnologist, successMsg, emoji.Rocket, emoji.Rocket, emoji.PartyPopper)
fmt.Printf("%v Run the following command to export demo environment variables for accessing flytectl\n", emoji.Sparkle)
fmt.Printf(" export FLYTECTL_CONFIG=%v \n", configutil.FlytectlConfig)
if dryRun {
fmt.Printf("%v Run the following command to export kubeconfig variables for accessing flyte pods locally\n", emoji.Sparkle)
fmt.Printf(" export KUBECONFIG=%v \n", kubeconfig)
}
fmt.Printf("%s Flyte sandbox ships with a Docker registry. Tag and push custom workflow images to localhost:30000\n", emoji.Whale)
fmt.Printf("%s The Minio API is hosted on localhost:30002. Use http://localhost:30080/minio/login for Minio console, default credentials - username: minio, password: miniostorage\n", emoji.OpenFileFolder)
}
// PrintSandboxStartMessage will print sandbox start success message
func PrintSandboxStartMessage(flyteConsolePort int, kubeconfigLocation string, dryRun bool) {
kubeconfig := strings.Join([]string{
"$KUBECONFIG",
kubeconfigLocation,
}, ":")
var successMsg string
if dryRun {
successMsg = fmt.Sprintf("%v http://localhost:%v/console", ProgressSuccessMessagePending, flyteConsolePort)
} else {
successMsg = fmt.Sprintf("%v http://localhost:%v/console", ProgressSuccessMessage, flyteConsolePort)
}
fmt.Printf("%v %v %v %v %v \n", emoji.ManTechnologist, successMsg, emoji.Rocket, emoji.Rocket, emoji.PartyPopper)
fmt.Printf("%v Run the following command to export sandbox environment variables for accessing flytectl\n", emoji.Sparkle)
fmt.Printf(" export FLYTECTL_CONFIG=%v \n", configutil.FlytectlConfig)
if dryRun {
fmt.Printf("%v Run the following command to export kubeconfig variables for accessing flyte pods locally\n", emoji.Sparkle)
fmt.Printf(" export KUBECONFIG=%v \n", kubeconfig)
}
}
// PrintSandboxTeardownMessage will print sandbox teardown success message
func PrintSandboxTeardownMessage(flyteConsolePort int, kubeconfigLocation string) {
fmt.Printf("%v Run the following command to unset sandbox environment variables for accessing flytectl\n", emoji.Sparkle)
fmt.Printf(" unset FLYTECTL_CONFIG \n")
}
// SendRequest will create request and return the response
func SendRequest(method, url string, option io.Reader) (*http.Response, error) {
client := &http.Client{}
req, _ := http.NewRequest(method, url, option)
response, err := client.Do(req)
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, fmt.Errorf("someting goes wrong while sending request to %s. Got status code %v", url, response.StatusCode)
}
return response, nil
}
// IsVersionGreaterThan check version if it's greater then other
func IsVersionGreaterThan(version1, version2 string) (bool, error) {
semanticVersion1, err := hversion.NewVersion(version1)
if err != nil {
return false, err
}
semanticVersion2, err := hversion.NewVersion(version2)
if err != nil {
return false, err
}
return semanticVersion1.GreaterThan(semanticVersion2), nil
}