-
Notifications
You must be signed in to change notification settings - Fork 0
/
software.go
114 lines (100 loc) · 3.14 KB
/
software.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
package model
import (
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"os/user"
"github.com/hellgate75/vmkube/utils"
)
type MachineISO struct {
Name string `json:"name" xml:"name"`
BaseURL string `json:"baseurl" xml:"baseurl"`
ISOName string `json:"isoname" xml:"isoname"`
FolderName string `json:"folder" xml:"folder"`
FinalNamePrefix string `json:"fileprefix" xml:"fileprefix"`
FinalNameSuffix string `json:"filesuffix" xml:"filesuffix"`
}
func HomeFolder() string {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
return os.TempDir()
}
return usr.HomeDir
}
func VMBaseFolder() string {
home := HomeFolder()
return home + string(os.PathSeparator) + ".vmkube"
}
func GetEmergencyFolder() string {
return HomeFolder()
}
type MachineActions interface {
Download(v string) bool
Check(v string) bool
Path(v string) string
}
func (isoTemplate *MachineISO) Path(version string) string {
home := VMBaseFolder()
folder := home + string(os.PathSeparator) + "images" + string(os.PathSeparator) + isoTemplate.FolderName
fileName := folder + string(os.PathSeparator) + isoTemplate.FinalNamePrefix + version + isoTemplate.FinalNameSuffix
return fileName
}
func (isoTemplate *MachineISO) Check(version string) bool {
home := VMBaseFolder()
folder := home + string(os.PathSeparator) + "images" + string(os.PathSeparator) + isoTemplate.FolderName
fileName := folder + string(os.PathSeparator) + isoTemplate.FinalNamePrefix + version + isoTemplate.FinalNameSuffix
_, error := os.Stat(fileName)
return !os.IsNotExist(error)
}
func (isoTemplate *MachineISO) Download(version string) bool {
url := isoTemplate.BaseURL + version + isoTemplate.ISOName
home := VMBaseFolder()
folder := home + string(os.PathSeparator) + "images" + string(os.PathSeparator) + isoTemplate.FolderName
os.MkdirAll(folder, 0777)
fileName := folder + string(os.PathSeparator) + isoTemplate.FinalNamePrefix + version + isoTemplate.FinalNameSuffix
fmt.Printf("Downloading %s to %s\n", url, fileName)
// Check() for Download Prevent File Presence on the Disk
err := utils.CreateNewEmptyFile(fileName)
if err != nil {
fmt.Printf("Error while creating %s - %s\n", fileName, err)
return false
}
response, err := http.Get(url)
if err != nil {
fmt.Printf("Error while downloading %s - %s\n", url, err)
return false
}
defer response.Body.Close()
output, err := os.OpenFile(fileName, os.O_RDWR, 0777)
if err != nil {
return false
}
defer output.Close()
n, err := io.Copy(output, response.Body)
if err != nil {
fmt.Printf("Error while downloading %s - %s\n", url, err)
return false
}
fmt.Printf("%d bytes downloaded.\n", n)
_, error := os.Stat(fileName)
return !os.IsNotExist(error)
}
func GetMachineAction(name string) (*MachineISO, error) {
switch name {
case "rancheros":
return &MachineISO{
Name: "rancheros",
BaseURL: "https://github.com/rancher/os/releases/download/v",
ISOName: "/rancheros.iso",
FinalNamePrefix: "rancheros-",
FinalNameSuffix: ".iso",
FolderName: "rancheros",
}, nil
default:
return &MachineISO{}, errors.New("Unbable to discover machine type : " + name)
}
}