-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
164 lines (142 loc) · 4.45 KB
/
config.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
package workbench
import (
"fmt"
"github.com/sol-eng/wbi/internal/system"
)
// WriteRepoConfig writes the repo config to the Workbench config file
func WriteRepoConfig(url string, source string) error {
if source == "cran" {
filepath := "/etc/rstudio/repos.conf"
// check to ensure the line doesn't already exist
lineExists, err := system.CheckStringExists("CRAN=", filepath)
if err != nil {
return fmt.Errorf("failed to check if line exists: %w", err)
}
if !lineExists {
writeLines := []string{
"CRAN=" + url,
}
err := system.WriteStrings(writeLines, filepath, 0644, true, true)
if err != nil {
return fmt.Errorf("failed to write config: %w", err)
}
} else {
return fmt.Errorf("line already exists in repos.conf")
}
} else if source == "pypi" {
filepath := "/etc/pip.conf"
// check to ensure the line doesn't already exist
lineExists, err := system.CheckStringExists("index-url="+url, filepath)
if err != nil {
return fmt.Errorf("failed to check if line exists: %w", err)
}
if !lineExists {
writeLines := []string{
"[global]",
"index-url=" + url,
}
err = system.WriteStrings(writeLines, filepath, 0644, true, true)
if err != nil {
return fmt.Errorf("failed to write config: %w", err)
}
} else {
return fmt.Errorf("line already exists in pip.conf")
}
}
return nil
}
func cleanServerURL(serverURL string) string {
// remove trailing slash if present
if serverURL[len(serverURL)-1] == '/' {
serverURL = serverURL[:len(serverURL)-1]
}
// remove http:// or https:// if present
if serverURL[:7] == "http://" {
serverURL = serverURL[7:]
} else if serverURL[:8] == "https://" {
serverURL = serverURL[8:]
}
return serverURL
}
// WriteSSLConfig writes the SSL config to the Workbench config file
func WriteSSLConfig(certPath string, keyPath string, serverURL string) error {
// clean the serverURL
serverURLClean := cleanServerURL(serverURL)
finalServerURL := "https://" + serverURLClean
// check to ensure the lines don't already exist
linesCheck := []string{
"ssl-enabled=",
"ssl-certificate=",
"ssl-certificate-key=",
}
filepath := "/etc/rstudio/rserver.conf"
var linesExist bool
for _, value := range linesCheck {
matched, err := system.CheckStringExists(value, filepath)
if err == nil && matched {
linesExist = true
}
}
// remove launcher-sessions-callback-address and append the lines if they don't exist
if !linesExist {
// remove launcher-sessions-callback-address
err := system.DeleteStrings([]string{"launcher-sessions-callback-address"}, filepath, 0644)
if err != nil {
return fmt.Errorf("failed to delete launcher-sessions-callback-address: %w", err)
}
// append new lines including the updated launcher-sessions-callback-address
writeLines := []string{
"",
"launcher-sessions-callback-address=" + finalServerURL,
"",
"ssl-enabled=1",
"ssl-certificate=" + certPath,
"ssl-certificate-key=" + keyPath,
}
err = system.WriteStrings(writeLines, filepath, 0644, true, true)
if err != nil {
return fmt.Errorf("failed to write config: %w", err)
}
} else {
return fmt.Errorf("at least one line already exists in rserver.conf")
}
return nil
}
// WriteConnectURLConfig writes the Connect URL config to the Workbench config file
func WriteConnectURLConfig(url string) error {
// check to ensure the line doesn't already exist
filepath := "/etc/rstudio/rsession.conf"
lineExists, err := system.CheckStringExists("default-rsconnect-server=", filepath)
if err != nil {
return fmt.Errorf("failed to check if line exists: %w", err)
}
if !lineExists {
writeLines := []string{
"default-rsconnect-server=" + url,
}
err = system.WriteStrings(writeLines, filepath, 0644, true, true)
if err != nil {
return fmt.Errorf("failed to write config: %w", err)
}
} else {
return fmt.Errorf("line already exists in rsession.conf")
}
return nil
}
// WriteJupyterConfig writes the Jupyter config to the Workbench config file
func WriteJupyterConfig(jupyterPath string) error {
// remove the existing line
filepath := "/etc/rstudio/jupyter.conf"
err := system.DeleteStrings([]string{"# jupyter-exe=/usr/local/bin/jupyter"}, filepath, 0644)
if err != nil {
return fmt.Errorf("failed to delete the old jupyter-exe=: %w", err)
}
writeLines := []string{
"jupyter-exe=" + jupyterPath,
}
err = system.WriteStrings(writeLines, filepath, 0644, true, true)
if err != nil {
return fmt.Errorf("failed to write config: %w", err)
}
return nil
}