Skip to content

Commit

Permalink
net: clear assets directory on startup (#1213)
Browse files Browse the repository at this point in the history
Signed-off-by: Abiola Ibrahim <[email protected]>
  • Loading branch information
abiosoft authored Dec 14, 2024
1 parent d6b8e34 commit 70ca03b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
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

0 comments on commit 70ca03b

Please sign in to comment.