@@ -29,6 +29,7 @@ import (
2929 "os"
3030 "os/exec"
3131 "path/filepath"
32+ "slices"
3233 "strconv"
3334 "strings"
3435 "time"
@@ -122,30 +123,51 @@ func (c *cmdLaunch) launch(app string, instanceName string) error {
122123 launchSettings .Profiles = append (launchSettings .Profiles , p )
123124 }
124125 }
125- if ! found {
126- log .Info ("No TrueNAS profiles found" )
127- // get the list of pools
128- pools , err := c .global .client .StorageList (c .Command ().Context ())
126+
127+ // get the list of pools
128+ pools , err := c .global .client .StorageList (c .Command ().Context ())
129+ if err != nil {
130+ log .Error ("Error getting incus storage pools:" , "error" , err )
131+ return err
132+ }
133+ if len (pools ) == 0 {
134+ log .Error ("No storage pools found" )
135+ return errors .New ("no storage pools found" )
136+ }
137+ // get the default pool
138+ defaultPool := ""
139+ for _ , pool := range pools {
140+ if pool .Name == "default" {
141+ defaultPool = pool .Name
142+ break
143+ }
144+ }
145+ if defaultPool == "" {
146+ defaultPool = pools [0 ].Name
147+ }
148+ if found {
149+ scliProfile , err := c .global .client .Profile (c .Command ().Context (), "scriptcli-storage" )
129150 if err != nil {
130- log .Error ("Error getting incus storage pools :" , "error" , err )
151+ log .Error ("Error getting profile :" , "error" , err )
131152 return err
132153 }
133- if len (pools ) == 0 {
134- log .Error ("No storage pools found" )
135- return errors .New ("no storage pools found" )
136- }
137- // get the default pool
138- defaultPool := ""
139- for _ , pool := range pools {
140- if pool .Name == "default" {
141- defaultPool = pool .Name
142- break
154+ rdev , ok := scliProfile .Devices ["root" ]
155+ if ok {
156+ if rdev ["pool" ] != defaultPool {
157+ log .Error ("wrong pool in profile" , "incorrect pool" , rdev ["pool" ], "correct pool" , defaultPool )
158+ log .Warn ("Removing incorrect profile" )
159+ err = exec .Command ("incus" , "profile" , "delete" , "scriptcli-storage" ).Run ()
160+ if err != nil {
161+ fmt .Println ("Error deleting invalid storage profile:" , err )
162+ os .Exit (0 )
163+ }
164+ found = false
143165 }
144166 }
145- if defaultPool == "" {
146- log . Info ( "No default storage pool found, defaulting to first pool" )
147- defaultPool = pools [ 0 ]. Name
148- }
167+ }
168+ if ! found {
169+ log . Info ( "No TrueNAS profiles found" )
170+
149171 log .Debug ("Using storage pool" , "pool" , defaultPool )
150172 // create the profile
151173 p := api.ProfilesPost {
@@ -168,7 +190,9 @@ func (c *cmdLaunch) launch(app string, instanceName string) error {
168190 return err
169191 }
170192 log .Info ("Created Incus profile" , "profile" , p .Name )
171- launchSettings .Profiles = append (launchSettings .Profiles , "scriptcli-storage" )
193+ if ! slices .Contains (launchSettings .Profiles , "default" ) {
194+ launchSettings .Profiles = append (launchSettings .Profiles , "scriptcli-storage" )
195+ }
172196
173197 }
174198 }
0 commit comments