-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.go
174 lines (157 loc) · 5.26 KB
/
install.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
package workbench
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
"github.com/sol-eng/wbi/internal/config"
"github.com/sol-eng/wbi/internal/install"
cmdlog "github.com/sol-eng/wbi/internal/logging"
"github.com/sol-eng/wbi/internal/system"
)
// InstallerInfo contains the information needed to download and install Workbench
type InstallerInfo struct {
BaseName string `json:"basename"`
URL string `json:"url"`
Version string `json:"version"`
Label string `json:"label"`
}
// OperatingSystems contains the installer information for each supported operating system
type OperatingSystems struct {
Focal InstallerInfo `json:"focal"`
Jammy InstallerInfo `json:"jammy"`
Redhat7 InstallerInfo `json:"redhat7_64"`
Redhat8 InstallerInfo `json:"rhel8"`
Redhat9 InstallerInfo `json:"rhel9"`
}
// Installer contains the installer information for a product
type Installer struct {
Installer OperatingSystems `json:"installer"`
}
// ProductType contains the installer for each product type
type ProductType struct {
Server Installer `json:"server"`
}
// Category contains information for stable and preview product types
type Category struct {
Stable ProductType `json:"stable"`
}
// Product contains information for each RStudio product
type Product struct {
Pro Category `json:"pro"`
}
// RStudio contains product information
type RStudio struct {
Rstudio Product `json:"rstudio"`
}
// Retrieves JSON data from Posit, downloads the Workbench installer, and installs Workbench
func DownloadAndInstallWorkbench(osType config.OperatingSystem) error {
// Retrieve JSON data
rstudio, err := RetrieveWorkbenchInstallerInfo()
if err != nil {
return fmt.Errorf("RetrieveWorkbenchInstallerInfo: %w", err)
}
// Retrieve installer info
installerInfo, err := rstudio.GetInstallerInfo(osType)
if err != nil {
return fmt.Errorf("GetInstallerInfo: %w", err)
}
// Download installer
filepath, err := install.DownloadFile("Workbench", installerInfo.URL, installerInfo.BaseName)
if err != nil {
return fmt.Errorf("DownloadFile: %w", err)
}
// Install Workbench
err = InstallWorkbench(filepath, osType)
if err != nil {
return fmt.Errorf("InstallWorkbench: %w", err)
}
// save to command log
installCommand, err := RetrieveInstallCommandForWorkbench(installerInfo.BaseName, osType)
if err != nil {
return fmt.Errorf("RetrieveInstallCommand: %w", err)
}
cmdlog.Info("curl -O " + installerInfo.URL)
cmdlog.Info(installCommand)
return nil
}
// Installs Workbench in a certain way based on the operating system
func InstallWorkbench(filepath string, osType config.OperatingSystem) error {
installCommand, err := RetrieveInstallCommandForWorkbench(filepath, osType)
if err != nil {
return fmt.Errorf("RetrieveInstallCommandForWorkbench: %w", err)
}
err = system.RunCommand(installCommand, false, 0, false)
if err != nil {
return fmt.Errorf("issue installing Workbench with the command '%s': %w", installCommand, err)
}
system.PrintAndLogInfo("\nWorkbench has been successfully installed!")
return nil
}
// Creates the proper command to install Workbench based on the operating system
func RetrieveInstallCommandForWorkbench(filepath string, osType config.OperatingSystem) (string, error) {
switch osType {
case config.Ubuntu22, config.Ubuntu20:
return "DEBIAN_FRONTEND=noninteractive gdebi -n " + filepath, nil
case config.Redhat7, config.Redhat8, config.Redhat9:
return "yum install -y " + filepath, nil
default:
return "", errors.New("operating system not supported")
}
}
// Pulls out the installer information from the JSON data based on the operating system
func (r *RStudio) GetInstallerInfo(osType config.OperatingSystem) (InstallerInfo, error) {
switch osType {
case config.Ubuntu20:
return r.Rstudio.Pro.Stable.Server.Installer.Focal, nil
case config.Ubuntu22:
return r.Rstudio.Pro.Stable.Server.Installer.Jammy, nil
case config.Redhat7:
return r.Rstudio.Pro.Stable.Server.Installer.Redhat7, nil
case config.Redhat8:
return r.Rstudio.Pro.Stable.Server.Installer.Redhat8, nil
case config.Redhat9:
return r.Rstudio.Pro.Stable.Server.Installer.Redhat9, nil
default:
return InstallerInfo{}, errors.New("operating system not supported")
}
}
// Retrieves JSON data from Posit
func RetrieveWorkbenchInstallerInfo() (RStudio, error) {
client := &http.Client{
Timeout: 30 * time.Second,
}
req, err := http.NewRequestWithContext(context.Background(),
http.MethodGet, "https://www.rstudio.com/wp-content/downloads.json", nil)
if err != nil {
return RStudio{}, errors.New("error creating request")
}
res, err := client.Do(req)
if err != nil {
return RStudio{}, errors.New("error retrieving JSON data")
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return RStudio{}, errors.New("error retrieving JSON data")
}
var rstudio RStudio
err = json.NewDecoder(res.Body).Decode(&rstudio)
if err != nil {
return RStudio{}, errors.New("error unmarshalling JSON data")
}
return rstudio, nil
}
func CheckDownloadAndInstallWorkbench(osType config.OperatingSystem) error {
workbenchInstalled := VerifyWorkbench()
if !workbenchInstalled {
err := DownloadAndInstallWorkbench(osType)
if err != nil {
return fmt.Errorf("issue installing Workbench: %w", err)
}
} else {
return fmt.Errorf("workbench is already installed")
}
return nil
}