-
Notifications
You must be signed in to change notification settings - Fork 14
/
install.go
195 lines (163 loc) · 4.53 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package internal
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/hashicorp/go-getter"
"github.com/samber/lo"
"github.com/IceWhaleTech/CasaOS-Common/utils/file"
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
"github.com/IceWhaleTech/CasaOS-Common/utils/systemctl"
"github.com/IceWhaleTech/CasaOS-Installer/codegen"
)
func GetPackageURLByCurrentArch(release codegen.Release, mirror string) (string, error) {
// get current arch
arch := runtime.GOARCH
if arch == "arm" {
arch = "arm-7"
}
if !lo.Contains([]string{string(codegen.Amd64), string(codegen.Arm64), string(codegen.Arm7)}, arch) {
return "", fmt.Errorf("unsupported architecture: %s", arch)
}
for _, pkg := range release.Packages {
if string(pkg.Architecture) == arch {
return strings.TrimSuffix(mirror, "/") + pkg.Path, nil
}
}
return "", fmt.Errorf("package not found for architecture: %s", arch)
}
func Download(ctx context.Context, outDir, url string) (string, error) {
filename := filepath.Base(url)
_filepath := filepath.Join(outDir, filename)
return _filepath, DownloadAs(ctx, _filepath, url)
}
func DownloadAs(ctx context.Context, filepath, url string) error {
url = url + "?archive=false" // disable automatic archive extraction
// download package
getClient := getter.Client{
Ctx: ctx,
Dst: filepath,
Mode: getter.ClientModeFile,
Src: url,
Umask: 0o022,
Options: []getter.ClientOption{
getter.WithProgress(NewTracker(
func(downladed, totalSize int64) {
// TODO: send progress event to message bus if it exists
// logger.Info("Downloading package", zap.String("url", url), zap.Int64("downloaded", downladed), zap.Int64("totalSize", totalSize))
},
)),
},
}
return getClient.Get()
}
func Extract(tarFilePath, destinationFolder string) error {
// to check tarFilePath is a tar.gz file and destinationFolder is a folder
if strings.HasSuffix(tarFilePath, ".tar.gz") {
return UnGzip(tarFilePath, destinationFolder)
}
if strings.HasSuffix(tarFilePath, ".tar") {
return UnTar(tarFilePath, destinationFolder)
}
if strings.HasSuffix(tarFilePath, ".zip") {
exec.Command("unzip", tarFilePath, "-d", destinationFolder).Run()
return nil
}
return nil
}
// extract each archive in dir
func BulkExtract(dir string) error {
return filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
return Extract(path, dir)
})
}
func InstallRelease(releaseDir string, sysrootPath string) error {
srcSysroot := filepath.Join(releaseDir, "build", "sysroot") + "/"
if _, err := os.Stat(srcSysroot); err != nil {
return err
}
return file.CopyDir(srcSysroot, sysrootPath, "")
}
func NewDecompressor(filepath string) getter.Decompressor {
matchingLen := 0
archiveV := ""
for k := range getter.Decompressors {
if strings.HasSuffix(filepath, "."+k) && len(k) > matchingLen {
archiveV = k
matchingLen = len(k)
}
}
return getter.Decompressors[archiveV]
}
func InstallDependencies() error {
CASA_DEPANDS_PACKAGE := []string{"curl", "smartmontools", "parted", "ntfs-3g", "net-tools", "udevil", "samba", "cifs-utils", "rclone"}
Output, err := exec.Command("apt", "update").Output()
fmt.Println(string(Output))
if err != nil {
return err
}
for _, depName := range CASA_DEPANDS_PACKAGE {
Output, err := exec.Command("apt", "install", "-y", depName).Output()
fmt.Println(string(Output))
if err != nil {
return err
}
}
return nil
}
func GetDockerRunningStatus() (bool, error) {
return systemctl.IsServiceRunning("docker.service")
}
func IsDockerInstalled() bool {
err := systemctl.StartService("docker.service")
if err != nil {
return false
} else {
return true
}
}
func InstallDockerByShell() error {
// to install docker
// curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
fmt.Println("请手动执行 curl", "-fsSL https://get.docker.com | bash -s docker --mirror Aliyun")
return nil
}
func InstallDocker() error {
// to check docker is running
isDockerRunning, err := GetDockerRunningStatus()
if err != nil {
return err
}
if isDockerRunning {
logger.Info("docker is running. skip install docker")
return nil
}
// to check docker is installed
if IsDockerInstalled() {
return nil
}
// to install docker
err = InstallDockerByShell()
if err != nil {
return err
}
// to run docker
isDockerRunning, err = GetDockerRunningStatus()
if err != nil {
return err
}
if !isDockerRunning {
return fmt.Errorf("docker is launch failed")
}
return nil
}