-
Notifications
You must be signed in to change notification settings - Fork 112
/
main.go
247 lines (218 loc) · 6.49 KB
/
main.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
Copyright © 2020 Chef Software, Inc
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.
*/
// command to run unit -- go test -tags=unit ./cmd -v -count=1 --cover
// command to run integration -- go test -tags=integration ./integration -v -count=1 --cover
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
platform_lib "github.com/chef/chef-workstation/components/main-chef-wrapper/platform-lib"
licensing "github.com/chef/go-libs/licensing"
_ "embed"
"github.com/chef/chef-workstation/components/main-chef-wrapper/cmd"
homedir "github.com/mitchellh/go-homedir"
)
func doStartupTasks() error {
createDotChef()
platform_lib.GlobalReadFile()
if runtime.GOOS == "windows" {
createRubyEnvWindows()
} else {
createRubyEnvUnix()
}
return nil
}
// Attempts to create the ~/.chef directory.
// Does not report an error if this fails, because it is non-fatal:
// operations can continue if we don't create .chef, but the user might
// see some warnings from specific tools that want it.
func createDotChef() {
path, err := homedir.Expand("~/.chef")
if err != nil {
return
}
os.Mkdir(path, 0700)
}
func createRubyEnvUnix() {
InstallerDir := "/opt/chef-workstation"
home, err := os.UserHomeDir()
installationPath := path.Join(home, ".chef/ruby-env.json")
result, err := exists(installationPath)
if err != nil {
log.Fatalf(err.Error())
}
if result != true {
if createEnvJsonUnix(InstallerDir, installationPath) {
return
}
}
if result == true && platform_lib.MatchVersions() != true {
if createEnvJsonUnix(InstallerDir, installationPath) {
return
}
}
platform_lib.InitializeRubyMap()
}
func createRubyEnvWindows() {
InstallerDir := platform_lib.WorkstationInfo().InstallDirectory
home, err := os.UserHomeDir()
installationPath := path.Join(home, `.chef\ruby-env.json`)
result, err := exists(installationPath)
if err != nil {
log.Fatalf(err.Error())
}
if platform_lib.OmnibusInstall() {
if result != true {
if createEnvJsonWindows(InstallerDir, installationPath) {
return
}
}
if result == true && platform_lib.MatchVersions() != true {
if createEnvJsonWindows(InstallerDir, installationPath) {
return
}
}
} else {
if result != true {
if createEnvJsonWindows(InstallerDir, installationPath) {
return
}
}
if result && platform_lib.MatchVersions() {
if createEnvJsonWindows(InstallerDir, installationPath) {
return
}
}
}
platform_lib.InitializeRubyMap()
}
func createEnvJsonUnix(InstallerDir string, installationPath string) bool {
arg0 := fmt.Sprintf("%s/embedded/bin/ruby", InstallerDir)
arg1 := fmt.Sprintf("%s/bin/ruby-env-script.rb", InstallerDir)
argList := []string{arg1, installationPath}
cmd := exec.Command(arg0, argList...)
stdout, err := cmd.Output()
if err != nil {
fmt.Println(err.Error())
return true
}
// Print the output
fmt.Println(string(stdout))
return false
}
func createEnvJsonWindows(InstallerDir string, installationPath string) bool {
arg0 := fmt.Sprintf(`%s\embedded\bin\ruby`, InstallerDir)
arg1 := fmt.Sprintf(`%s\bin\ruby-env-script.rb`, InstallerDir)
argList := []string{arg1, installationPath}
cmd := exec.Command(arg0, argList...)
stdout, err := cmd.Output()
if err != nil {
fmt.Println(err.Error())
return true
}
// Print the output
fmt.Println(string(stdout))
return false
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// feature flag for license
func checkLicenseFlag() {
home, _ := os.UserHomeDir()
if len(os.Args) > 3 && os.Args[1] == "license" && os.Args[2] == "enable" && os.Args[3] == "true" {
f, err := os.Create(filepath.Join(home, ".chef/fbffb2ea48910514676e1b7a51c7248290ea958c"))
if err != nil {
log.Fatal("Not able to enable chef")
}
defer f.Close()
f.Write([]byte(`true`))
log.Println("Now you can use chef commands using the license.")
os.Exit(0)
} else if len(os.Args) > 3 && os.Args[1] == "license" && os.Args[2] == "enable" && os.Args[3] == "false" {
err := os.Remove(filepath.Join(home, ".chef/fbffb2ea48910514676e1b7a51c7248290ea958c"))
if err != nil {
log.Fatal("Not able to disable chef")
}
log.Println("License feature got disabled")
os.Exit(0)
} else if len(os.Args) > 2 && os.Args[1] == "license" {
info, _ := os.Stat(filepath.Join(home, ".chef/fbffb2ea48910514676e1b7a51c7248290ea958c"))
if info == nil {
log.Fatal("To use chef license feature you need to enable the license flag. \nTo enable it run `chef license enable true`")
}
}
}
func featureEnabled() bool {
home, _ := os.UserHomeDir()
licensePath := filepath.Join(home, ".chef/fbffb2ea48910514676e1b7a51c7248290ea958c")
info, _ := os.Stat(licensePath)
if info != nil {
return true
} else {
return false
}
}
type Configuration struct {
ChefProductName string `json:"chefProductName"`
ChefEntitlementID string `json:"chefEntitlementId"` // TODO : Need to confirm the chefEntitlementId before merge
ChefExecutableName string `json:"chefExecutableName"`
LicenseServerURL string `json:"licenseServerURL"` // TODO : Need to confirm the licenseServerURL before merge
}
//go:embed dist/licensingConfig.json
var config []byte
func readLicenseConfig() Configuration {
var myConf Configuration
err := json.Unmarshal(config, &myConf)
if err != nil {
panic(err)
}
return myConf
}
func main() {
if len(os.Args) > 1 {
if os.Args[1] == "version" || os.Args[1] == "-v" || os.Args[1] == "--version" {
os.Args[1] = "version"
}
}
doStartupTasks()
if len(os.Args) == 1 || os.Args[1] == "version" || os.Args[1] == "help" || os.Args[1] == "-h" || os.Args[1] == "--help" {
cmd.Execute()
os.Exit(0)
}
checkLicenseFlag()
if featureEnabled() {
if os.Args[1] == "license" {
cmd.Execute()
os.Exit(0)
}
// fmt.Println("inside license check")
licenseConfig := readLicenseConfig()
// calling licensing package in go-lib
licensing.CheckSoftwareEntitlement(licenseConfig.ChefEntitlementID, licenseConfig.LicenseServerURL)
}
cmd.Execute()
}