client도 storageroot지원

This commit is contained in:
2023-06-14 14:16:47 +09:00
parent 46aedbe767
commit 279c9f47f0
7 changed files with 60 additions and 36 deletions

View File

@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"os/signal"
@ -31,6 +32,7 @@ import (
type clientConfig struct {
GrpcAddress string `json:"grpc_server_address"`
HttpAddress string `json:"http_server_address"`
StorageRoot string `json:"storage_path"`
}
func loadClientConfig() (clientConfig, error) {
@ -132,12 +134,13 @@ func unmarshal[T any](val *T, src map[string]string) {
}
}
func gatherDeployedPrograms(name string) []*protos.VersionAndArgs {
func gatherDeployedPrograms(storageRoot, name string) []*protos.VersionAndArgs {
var rawvers []*protos.VersionAndArgs
if vers, err := os.ReadDir(name); err == nil {
targetPath := path.Join(storageRoot, name)
if vers, err := os.ReadDir(targetPath); err == nil {
for _, ver := range vers {
if ver.IsDir() {
args := lastExecutionArgs(path.Join(name, ver.Name()))
args := lastExecutionArgs(path.Join(targetPath, ver.Name()))
rawvers = append(rawvers, &protos.VersionAndArgs{
Version: ver.Name(),
Args: args,
@ -205,13 +208,26 @@ func NewClient() (HoustonClient, error) {
return nil, err
}
sp, err := os.Stat(clientConfig.StorageRoot)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
err = os.MkdirAll(clientConfig.StorageRoot, 0775)
}
} else if !sp.IsDir() {
err = errors.New(clientConfig.StorageRoot + " is not directory")
}
if err != nil {
return nil, err
}
deploys := make(map[string][]*protos.VersionAndArgs)
if dirs, err := os.ReadDir("./"); err == nil {
if dirs, err := os.ReadDir(clientConfig.StorageRoot); err == nil {
for _, dir := range dirs {
if dir.IsDir() {
flagf := path.Join(dir.Name(), "@houston")
flagf := path.Join(clientConfig.StorageRoot, dir.Name(), "@houston")
if _, err := os.Stat(flagf); !os.IsNotExist(err) {
deploys[dir.Name()] = gatherDeployedPrograms(dir.Name())
deploys[dir.Name()] = gatherDeployedPrograms(clientConfig.StorageRoot, dir.Name())
}
}
}
@ -289,7 +305,7 @@ func NewClient() (HoustonClient, error) {
}
} else {
if err := hc.deploy(&dr); err == nil {
prog := gatherDeployedPrograms(dr.Name)
prog := gatherDeployedPrograms(hc.config.StorageRoot, dr.Name)
hc.deploys[dr.Name] = prog
op.Refresh(ctx, hc.makeOperationQueryRequest())
} else {
@ -302,7 +318,7 @@ func NewClient() (HoustonClient, error) {
unmarshal(&wr, resp.Args)
err := hc.withdraw(&wr)
if err == nil {
prog := gatherDeployedPrograms(wr.Name)
prog := gatherDeployedPrograms(hc.config.StorageRoot, wr.Name)
if len(prog) == 0 {
delete(hc.deploys, wr.Name)
} else {