installation system soft implemented
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
package packet
|
||||
|
||||
import "path/filepath"
|
||||
|
||||
type Config struct {
|
||||
BinDir *string
|
||||
BinDir string
|
||||
PacketDir string
|
||||
SourcesDir string
|
||||
RootDir string
|
||||
}
|
||||
|
||||
const defaultBinDir = "/usr/bin"
|
||||
@@ -10,30 +15,19 @@ func checkConfig(cfg *Config) *Config {
|
||||
if cfg == nil {
|
||||
bin := defaultBinDir
|
||||
return &Config{
|
||||
BinDir: &bin,
|
||||
BinDir: bin,
|
||||
}
|
||||
}
|
||||
if *cfg.BinDir == "" || cfg.BinDir == nil {
|
||||
bin := defaultBinDir
|
||||
return &Config{
|
||||
BinDir: &bin,
|
||||
}
|
||||
} else {
|
||||
return cfg
|
||||
}
|
||||
|
||||
}
|
||||
func checkConfigSrc(cfg *GetSourceConfig) *GetSourceConfig {
|
||||
if cfg == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch {
|
||||
case *cfg.PacketDir == "" || cfg.PacketDir == nil:
|
||||
s := randStringBytes(12)
|
||||
cfg.PacketDir = &s
|
||||
case cfg.BinDir == "":
|
||||
return &Config{
|
||||
BinDir: defaultBinDir,
|
||||
}
|
||||
case cfg.PacketDir == "":
|
||||
|
||||
cfg.PacketDir = filepath.Join("/tmp", randStringBytes(12))
|
||||
}
|
||||
|
||||
return cfg
|
||||
|
||||
}
|
||||
|
||||
@@ -9,10 +9,13 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/go-git/go-git/v6"
|
||||
"github.com/go-git/go-git/v6/plumbing"
|
||||
"github.com/klauspost/compress/zstd"
|
||||
|
||||
lua_utils "github.com/roboogg133/packets/internal/lua"
|
||||
@@ -35,6 +38,7 @@ type PacketLua struct {
|
||||
Build *lua.LFunction
|
||||
Install *lua.LFunction
|
||||
PreRemove *lua.LFunction
|
||||
LuaState *lua.LState
|
||||
}
|
||||
|
||||
type Source struct {
|
||||
@@ -85,16 +89,16 @@ func ReadPacket(f []byte, cfg *Config) (PacketLua, error) {
|
||||
cfg = checkConfig(cfg)
|
||||
|
||||
L := lua.NewState()
|
||||
defer L.Close()
|
||||
|
||||
L.SetGlobal("error", L.NewFunction(lua_utils.LError))
|
||||
|
||||
osObject := L.GetGlobal("os").(*lua.LTable)
|
||||
osObject.RawSetString("setenv", L.NewFunction(lua_utils.LSetEnv))
|
||||
ioObject := L.GetGlobal("io").(*lua.LTable)
|
||||
|
||||
L.SetGlobal("os", lua.LNil)
|
||||
L.SetGlobal("io", lua.LNil)
|
||||
|
||||
L.SetGlobal("BIN_DIR", lua.LString(*cfg.BinDir))
|
||||
L.SetGlobal("BIN_DIR", lua.LString(cfg.BinDir))
|
||||
L.SetGlobal("CURRENT_ARCH", lua.LString(runtime.GOARCH))
|
||||
L.SetGlobal("CURRENT_ARCH_NORMALIZED", lua.LString(normalizeArch(runtime.GOARCH)))
|
||||
L.SetGlobal("CURRENT_PLATAFORM", lua.LString(runtime.GOOS))
|
||||
@@ -139,6 +143,7 @@ func ReadPacket(f []byte, cfg *Config) (PacketLua, error) {
|
||||
PreRemove: getFunctionFromTable(table, "pre_remove"),
|
||||
}
|
||||
|
||||
packetLua.LuaState = L
|
||||
if packetLua.Install == nil {
|
||||
return PacketLua{}, ErrInstallFunctionDoesNotExist
|
||||
}
|
||||
@@ -179,11 +184,8 @@ func ReadPacketFromZSTDF(file io.Reader, cfg *Config) (PacketLua, error) {
|
||||
return PacketLua{}, ErrCantFindPacketDotLua
|
||||
}
|
||||
|
||||
type GetSourceConfig struct {
|
||||
PacketDir *string
|
||||
}
|
||||
|
||||
func GetSource(url, method string, info any) ([]byte, error) {
|
||||
// GetSource returns file []byte if method is "GET" or "POST", if is "git" returns *git.CloneOptions{}
|
||||
func GetSource(url, method string, info any, tryAttempts int) (any, error) {
|
||||
|
||||
switch method {
|
||||
case "GET":
|
||||
@@ -194,15 +196,24 @@ func GetSource(url, method string, info any) ([]byte, error) {
|
||||
|
||||
specs := info.(GETSpecs)
|
||||
|
||||
for k, v := range *specs.Headers {
|
||||
req.Header.Set(k, v)
|
||||
if specs.Headers != nil {
|
||||
for k, v := range *specs.Headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
client := http.Client{Timeout: 5 * time.Minute}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
var resp *http.Response
|
||||
for i := 0; i < tryAttempts; i++ {
|
||||
resp, err = client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
|
||||
break
|
||||
}
|
||||
resp.Body.Close()
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
@@ -231,15 +242,24 @@ func GetSource(url, method string, info any) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for k, v := range *specs.Headers {
|
||||
req.Header.Set(k, v)
|
||||
if specs.Headers != nil {
|
||||
for k, v := range *specs.Headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
client := http.Client{Timeout: 5 * time.Minute}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
var resp *http.Response
|
||||
for i := 0; i < tryAttempts; i++ {
|
||||
resp, err = client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
|
||||
break
|
||||
}
|
||||
resp.Body.Close()
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
@@ -254,6 +274,25 @@ func GetSource(url, method string, info any) ([]byte, error) {
|
||||
}
|
||||
|
||||
return data, nil
|
||||
|
||||
case "git":
|
||||
specs := info.(GitSpecs)
|
||||
|
||||
if specs.Tag == nil {
|
||||
return &git.CloneOptions{
|
||||
URL: url,
|
||||
SingleBranch: true,
|
||||
ReferenceName: plumbing.NewBranchReferenceName(specs.Branch),
|
||||
Depth: 1,
|
||||
}, nil
|
||||
} else {
|
||||
return &git.CloneOptions{
|
||||
URL: url,
|
||||
SingleBranch: true,
|
||||
ReferenceName: plumbing.NewTagReferenceName(*specs.Tag),
|
||||
Depth: 1,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("invalid method")
|
||||
@@ -265,3 +304,71 @@ func verifySHA256(checksum string, src []byte) bool {
|
||||
|
||||
return hex.EncodeToString(check[:]) == checksum
|
||||
}
|
||||
|
||||
func (pkg PacketLua) ExecuteBuild(cfg *Config) error {
|
||||
L := pkg.LuaState
|
||||
|
||||
L.SetGlobal("error", L.NewFunction(lua_utils.LError))
|
||||
|
||||
osObject := L.GetGlobal("os").(*lua.LTable)
|
||||
osObject.RawSetString("chdir", L.NewFunction(lua_utils.LCD))
|
||||
osObject.RawSetString("setenv", L.NewFunction(lua_utils.LSetEnv))
|
||||
osObject.RawSetString("copy", L.NewFunction(lua_utils.LCopy))
|
||||
osObject.RawSetString("mkdir", L.NewFunction(lua_utils.LMkdir))
|
||||
osObject.RawSetString("remove", L.NewFunction(lua_utils.LRemove))
|
||||
osObject.RawSetString("rename", L.NewFunction(lua_utils.LRename))
|
||||
osObject.RawSetString("symlink", L.NewFunction(lua_utils.LSymlink))
|
||||
osObject.RawSetString("chmod", L.NewFunction(lua_utils.LChmod))
|
||||
|
||||
L.SetGlobal("BIN_DIR", lua.LString(cfg.BinDir))
|
||||
L.SetGlobal("CURRENT_ARCH", lua.LString(runtime.GOARCH))
|
||||
L.SetGlobal("CURRENT_ARCH_NORMALIZED", lua.LString(normalizeArch(runtime.GOARCH)))
|
||||
L.SetGlobal("CURRENT_PLATAFORM", lua.LString(runtime.GOOS))
|
||||
|
||||
L.SetGlobal("SOURCESDIR", lua.LString(cfg.SourcesDir))
|
||||
L.SetGlobal("PACKETDIR", lua.LString(cfg.PacketDir))
|
||||
|
||||
L.SetGlobal("pathjoin", L.NewFunction(lua_utils.Ljoin))
|
||||
|
||||
os.Chdir(cfg.RootDir)
|
||||
|
||||
os.Setenv("PATH", os.Getenv("PATH")+":"+cfg.BinDir)
|
||||
|
||||
L.Push(pkg.Build)
|
||||
return L.PCall(0, 0, nil)
|
||||
}
|
||||
|
||||
func (pkg PacketLua) ExecuteInstall(cfg *Config) error {
|
||||
L := pkg.LuaState
|
||||
defer L.Close()
|
||||
|
||||
L.SetGlobal("error", L.NewFunction(lua_utils.LError))
|
||||
|
||||
osObject := L.GetGlobal("os").(*lua.LTable)
|
||||
osObject.RawSetString("chdir", L.NewFunction(lua_utils.LCD))
|
||||
osObject.RawSetString("setenv", L.NewFunction(lua_utils.LSetEnv))
|
||||
osObject.RawSetString("copy", L.NewFunction(lua_utils.LCopy))
|
||||
osObject.RawSetString("mkdir", L.NewFunction(lua_utils.LMkdir))
|
||||
osObject.RawSetString("remove", L.NewFunction(lua_utils.LRemove))
|
||||
osObject.RawSetString("rename", L.NewFunction(lua_utils.LRename))
|
||||
osObject.RawSetString("symlink", L.NewFunction(lua_utils.LSymlink))
|
||||
osObject.RawSetString("chmod", L.NewFunction(lua_utils.LChmod))
|
||||
|
||||
L.SetGlobal("BIN_DIR", lua.LString(cfg.BinDir))
|
||||
L.SetGlobal("CURRENT_ARCH", lua.LString(runtime.GOARCH))
|
||||
L.SetGlobal("CURRENT_ARCH_NORMALIZED", lua.LString(normalizeArch(runtime.GOARCH)))
|
||||
L.SetGlobal("CURRENT_PLATAFORM", lua.LString(runtime.GOOS))
|
||||
|
||||
L.SetGlobal("SOURCESDIR", lua.LString(cfg.SourcesDir))
|
||||
L.SetGlobal("PACKETDIR", lua.LString(cfg.PacketDir))
|
||||
|
||||
L.SetGlobal("pathjoin", L.NewFunction(lua_utils.Ljoin))
|
||||
|
||||
os.Chdir(cfg.RootDir)
|
||||
|
||||
os.Setenv("PATH", os.Getenv("PATH")+":"+cfg.BinDir)
|
||||
L.Push(pkg.Install)
|
||||
L.Call(0, 0)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
package packet
|
||||
|
||||
import "math/rand"
|
||||
|
||||
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$%!@%&*()-=+[]{}:;.,1234567890"
|
||||
|
||||
func randStringBytes(n int) string {
|
||||
b := make([]byte, n)
|
||||
for i := range b {
|
||||
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
81
pkg/packet.lua.d/utils.go
Normal file
81
pkg/packet.lua.d/utils.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package packet
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$%!@%&*()-=+[]{}:;.,1234567890"
|
||||
|
||||
func randStringBytes(n int) string {
|
||||
b := make([]byte, n)
|
||||
for i := range b {
|
||||
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func Dearchive(archive, outdir string) error {
|
||||
|
||||
switch {
|
||||
case strings.HasSuffix(archive, ".tar.gz"):
|
||||
|
||||
f, err := os.Open(archive)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
gzReader, err := gzip.NewReader(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tarReader := tar.NewReader(gzReader)
|
||||
|
||||
for {
|
||||
header, err := tarReader.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
destination := filepath.Join(outdir, header.Name)
|
||||
|
||||
fmt.Println(destination)
|
||||
|
||||
switch header.Typeflag {
|
||||
case tar.TypeDir:
|
||||
if err := os.Mkdir(destination, header.FileInfo().Mode()); err != nil {
|
||||
return err
|
||||
}
|
||||
case tar.TypeReg:
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(destination), 0777); err != nil {
|
||||
return err
|
||||
}
|
||||
outFile, err := os.Create(destination)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := io.Copy(outFile, tarReader); err != nil {
|
||||
return err
|
||||
}
|
||||
outFile.Close()
|
||||
|
||||
default:
|
||||
return fmt.Errorf("unknow filetype")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user