Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions environment/vm/lima/limautil/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ const networkFile = "networks.yaml"
func NetworkFile() string {
return filepath.Join(config.LimaDir(), "_config", networkFile)
}

// NetworkAssetsDirecotry returns the directory for the generated network assets.
func NetworkAssetsDirectory() string {
return filepath.Join(config.LimaDir(), "_networks")
}
17 changes: 17 additions & 0 deletions environment/vm/lima/limautil/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ func Instances(ids ...string) ([]InstanceInfo, error) {
return instances, nil
}

// RunningInstances return Lima instances that are has a running status.
func RunningInstances() ([]InstanceInfo, error) {
allInstances, err := Instances()
if err != nil {
return nil, err
}

var runningInstances []InstanceInfo
for _, instance := range allInstances {
if instance.Running() {
runningInstances = append(runningInstances, instance)
}
}

return runningInstances, nil
}

func getRuntime(conf config.Config) string {
var runtime string

Expand Down
8 changes: 8 additions & 0 deletions environment/vm/lima/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/abiosoft/colima/embedded"
"github.com/abiosoft/colima/environment/vm/lima/limautil"
"github.com/abiosoft/colima/util"
"github.com/sirupsen/logrus"
)

func (l *limaVM) writeNetworkFile() error {
Expand All @@ -19,6 +20,13 @@ func (l *limaVM) writeNetworkFile() error {
return fmt.Errorf("error reading embedded network config file: %w", err)
}

// if there are no running instances, clear network directory
if instances, err := limautil.RunningInstances(); err == nil && len(instances) == 0 {
if err := os.RemoveAll(limautil.NetworkAssetsDirectory()); err != nil {
logrus.Warnln(fmt.Errorf("could not clear network assets directory: %w", err))
}
}

if err := os.MkdirAll(filepath.Dir(networkFile), 0755); err != nil {
return fmt.Errorf("error creating Lima config directory: %w", err)
}
Expand Down