Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EnvHash to instance and calculate the hash based on it #1452

Merged
merged 9 commits into from
Oct 31, 2019
17 changes: 10 additions & 7 deletions instance/instance.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions protobuf/types/instance.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ message Instance {
];

bytes serviceHash = 2 [
(gogoproto.moretags) = 'hash:"name:2"',
(gogoproto.customtype) = "github.com/mesg-foundation/engine/hash.Hash",
(gogoproto.nullable) = false
];

bytes envHash = 3 [
(gogoproto.moretags) = 'hash:"name:3"',
(gogoproto.customtype) = "github.com/mesg-foundation/engine/hash.Hash",
(gogoproto.nullable) = false
];
Expand Down
18 changes: 8 additions & 10 deletions sdk/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,25 @@ func (i *Instance) Create(serviceHash hash.Hash, env []string) (*instance.Instan
if err != nil {
return nil, err
}

// calculate the final env vars by overwriting user defined one's with defaults.
instanceEnv := xos.EnvMergeMaps(xos.EnvSliceToMap(srv.Configuration.Env), xos.EnvSliceToMap(env))

// calculate instance's hash.
h := hash.New()
h.Write(srv.Hash)
h.Write([]byte(xos.EnvMapToString(instanceEnv)))
instanceHash := h.Sum(nil)
inst := &instance.Instance{
ServiceHash: srv.Hash,
EnvHash: hash.Dump(instanceEnv),
krhubert marked this conversation as resolved.
Show resolved Hide resolved
}
inst.Hash = hash.Dump(inst)

// check if instance already exists
if exist, err := i.instanceDB.Exist(instanceHash); err != nil {
if exist, err := i.instanceDB.Exist(inst.Hash); err != nil {
return nil, err
} else if exist {
return nil, &AlreadyExistsError{Hash: instanceHash}
return nil, &AlreadyExistsError{Hash: inst.Hash}
}

// save & start instance.
inst := &instance.Instance{
Hash: instanceHash,
ServiceHash: srv.Hash,
}
if err := i.instanceDB.Save(inst); err != nil {
return nil, err
}
Expand Down