forked from TrueCloudLab/frostfs-node
[#932] adm: Move InitializeContext
to util
package
Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
This commit is contained in:
parent
b68f7be0b6
commit
77694a2f3b
29 changed files with 809 additions and 794 deletions
|
@ -1,10 +1,14 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"compress/gzip"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/config"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring"
|
||||
|
@ -84,3 +88,72 @@ func NewActor(c actor.RPCActor, committeeAcc *wallet.Account) (*actor.Actor, err
|
|||
Account: committeeAcc,
|
||||
}})
|
||||
}
|
||||
|
||||
func ReadContract(ctrPath, ctrName string) (*ContractState, error) {
|
||||
rawNef, err := os.ReadFile(filepath.Join(ctrPath, ctrName+"_contract.nef"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("can't read NEF file for %s contract: %w", ctrName, err)
|
||||
}
|
||||
rawManif, err := os.ReadFile(filepath.Join(ctrPath, "config.json"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("can't read manifest file for %s contract: %w", ctrName, err)
|
||||
}
|
||||
|
||||
cs := &ContractState{
|
||||
RawNEF: rawNef,
|
||||
RawManifest: rawManif,
|
||||
}
|
||||
|
||||
return cs, cs.Parse()
|
||||
}
|
||||
|
||||
func ReadContractsFromArchive(file io.Reader, names []string) (map[string]*ContractState, error) {
|
||||
m := make(map[string]*ContractState, len(names))
|
||||
for i := range names {
|
||||
m[names[i]] = new(ContractState)
|
||||
}
|
||||
|
||||
gr, err := gzip.NewReader(file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("contracts file must be tar.gz archive: %w", err)
|
||||
}
|
||||
|
||||
r := tar.NewReader(gr)
|
||||
for h, err := r.Next(); ; h, err = r.Next() {
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
dir, _ := filepath.Split(h.Name)
|
||||
ctrName := filepath.Base(dir)
|
||||
|
||||
cs, ok := m[ctrName]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
switch {
|
||||
case strings.HasSuffix(h.Name, filepath.Join(ctrName, ctrName+"_contract.nef")):
|
||||
cs.RawNEF, err = io.ReadAll(r)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("can't read NEF file for %s contract: %w", ctrName, err)
|
||||
}
|
||||
case strings.HasSuffix(h.Name, "config.json"):
|
||||
cs.RawManifest, err = io.ReadAll(r)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("can't read manifest file for %s contract: %w", ctrName, err)
|
||||
}
|
||||
}
|
||||
m[ctrName] = cs
|
||||
}
|
||||
|
||||
for ctrName, cs := range m {
|
||||
if cs.RawNEF == nil {
|
||||
return nil, fmt.Errorf("NEF for %s contract wasn't found", ctrName)
|
||||
}
|
||||
if cs.RawManifest == nil {
|
||||
return nil, fmt.Errorf("manifest for %s contract wasn't found", ctrName)
|
||||
}
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue