55 "fmt"
66 "math/rand"
77 "net/url"
8+ "path"
89 "strconv"
910 "strings"
1011 "time"
@@ -19,6 +20,10 @@ import (
1920 "github.com/sp-yduck/cluster-api-provider-proxmox/cloud/scope"
2021)
2122
23+ const (
24+ etcCAPP = "/etc/capp"
25+ )
26+
2227func (s * Service ) Reconcile (ctx context.Context ) error {
2328 log := log .FromContext (ctx )
2429 log .Info ("Reconciling instance resources" )
@@ -143,7 +148,7 @@ func (s *Service) CreateInstance(ctx context.Context, bootstrap string) (*vm.Vir
143148 }
144149
145150 // os image
146- if err := SetCloudImage (ctx , vmid , s .scope .GetStorage (). Name , s .remote ); err != nil {
151+ if err := SetCloudImage (ctx , vmid , s .scope .GetStorage (), s . scope . GetImage () , s .remote ); err != nil {
147152 return nil , err
148153 }
149154
@@ -209,24 +214,28 @@ func (s *Service) Delete(ctx context.Context) error {
209214 return instance .Delete ()
210215}
211216
212- func SetCloudImage (ctx context.Context , vmid int , storageName string , ssh scope.SSHClient ) error {
217+ // setCloudImage set OS image to specified storage
218+ func SetCloudImage (ctx context.Context , vmid int , storage infrav1.Storage , image infrav1.Image , ssh scope.SSHClient ) error {
213219 log := log .FromContext (ctx )
214220 log .Info ("setting cloud image" )
215221
222+ url := image .URL
223+ fileName := path .Base (url )
224+ rawImageDirPath := fmt .Sprintf ("%s/images" , etcCAPP )
225+ rawImageFilePath := fmt .Sprintf ("%s/%s" , rawImageDirPath , fileName )
226+
216227 // workaround
217228 // API does not support something equivalent of "qm importdisk"
218- out , err := ssh .RunCommand (fmt .Sprintf ("wget https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64-disk-kvm.img -O /etc/capi-proxmox/jammy-server-cloudimg-amd64-disk-kvm.img - nc" ))
219- // if err != nil {
220- // return nil, errors.Errorf("failed to download image" )
221- // }
229+ out , err := ssh .RunCommand (fmt .Sprintf ("wget %s --directory-prefix %s - nc" , url , rawImageDirPath ))
230+ if err != nil {
231+ return errors .Errorf ("failed to download image: %s : %v" , out , err )
232+ }
222233
223- destPath := fmt .Sprintf ("/var/lib/vz/ %s/images/%d/vm-%d-disk-0.raw" , storageName , vmid , vmid )
224- out , err = ssh .RunCommand (fmt .Sprintf ("/usr/bin/qemu-img convert -O raw /root/jammy-server-cloudimg-amd64-disk-kvm.img %s" , destPath ))
234+ destPath := fmt .Sprintf ("%s/images/%d/vm-%d-disk-0.raw" , storage . Path , vmid , vmid )
235+ out , err = ssh .RunCommand (fmt .Sprintf ("/usr/bin/qemu-img convert -O raw %s %s" , rawImageFilePath , destPath ))
225236 if err != nil {
226- return err
237+ return errors . Errorf ( "failed to convert iamge : %s : %v" , out , err )
227238 }
228- log .Info ("imported cloud image" )
229- log .Info (out )
230239 return nil
231240}
232241
0 commit comments