Move box loading to machine

This commit is contained in:
sophia 2021-11-18 15:35:56 -06:00 committed by Paul Hinze
parent e68c210921
commit de996a6047
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
2 changed files with 15 additions and 32 deletions

View File

@ -40,16 +40,25 @@ func (m *Machine) SetID(value string) (err error) {
func (m *Machine) Box() (b core.Box, err error) { func (m *Machine) Box() (b core.Box, err error) {
if m.box == nil { if m.box == nil {
box, err := NewBox( // TODO: get provider info here too/generate full machine config?
BoxWithBasis(m.project.basis), // We know that these are machines so, save the Machine record
BoxWithBox(m.machine.Box), boxes, _ := m.project.Boxes()
BoxWithLogger(m.logger), b, err := boxes.Find(m.target.Configuration.ConfigVm.Box, "")
)
if err != nil { if err != nil {
return nil, err return nil, err
} }
m.box = box if b == nil {
// Add the box
b, err = addBox(m.target.Configuration.ConfigVm.Box, "virtualbox", m.project.basis)
if err != nil {
return nil, err
}
}
m.machine.Box = b.(*Box).ToProto()
m.Save()
m.box = b.(*Box)
} }
return m.box, nil return m.box, nil
} }

View File

@ -11,7 +11,6 @@ import (
"github.com/hashicorp/go-argmapper" "github.com/hashicorp/go-argmapper"
"github.com/hashicorp/go-hclog" "github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-multierror" "github.com/hashicorp/go-multierror"
"google.golang.org/protobuf/types/known/anypb"
"github.com/hashicorp/vagrant-plugin-sdk/component" "github.com/hashicorp/vagrant-plugin-sdk/component"
"github.com/hashicorp/vagrant-plugin-sdk/core" "github.com/hashicorp/vagrant-plugin-sdk/core"
@ -441,37 +440,12 @@ func (p *Project) InitTargets() (err error) {
if t == nil { if t == nil {
continue continue
} }
// TODO: get provider info here too/generate full machine config?
// We know that these are machines so, save the Machine record
machineRecord := &vagrant_server.Target_Machine{}
boxes, _ := p.Boxes()
b, erro := boxes.Find(t.ConfigVm.Box, "")
if erro != nil {
return erro
}
if b == nil {
// Add the box
b, erro = addBox(t.ConfigVm.Box, "virtualbox", p.basis)
if erro != nil {
return nil
}
}
machineRecord.Box = b.(*Box).ToProto()
machineRecordAny, erro := anypb.New(machineRecord)
if err != nil {
p.logger.Debug("Error generating machine record for target",
"project", p.Name(),
"target", t.Name,
)
return erro
}
_, err = p.Client().UpsertTarget(p.ctx, _, err = p.Client().UpsertTarget(p.ctx,
&vagrant_server.UpsertTargetRequest{ &vagrant_server.UpsertTargetRequest{
Target: &vagrant_server.Target{ Target: &vagrant_server.Target{
Name: t.Name, Name: t.Name,
Project: p.Ref().(*vagrant_plugin_sdk.Ref_Project), Project: p.Ref().(*vagrant_plugin_sdk.Ref_Project),
Configuration: t, Configuration: t,
Record: machineRecordAny,
}, },
}, },
) )