diff --git a/pkg/deploy/predeploy.go b/pkg/deploy/predeploy.go index e34874d5101..6153ac266cb 100644 --- a/pkg/deploy/predeploy.go +++ b/pkg/deploy/predeploy.go @@ -113,6 +113,11 @@ func (d *deployer) PreDeploy(ctx context.Context, lbHealthcheckWaitTimeSec int) return err } + err = d.ensureStaticWebContent(ctx) + if err != nil { + return err + } + // Due to https://github.com/Azure/azure-resource-manager-schemas/issues/1067 // we can't use conditions to define ACR replication object deployment. // Also, an ACR replica cannot be defined in the home registry location. diff --git a/pkg/deploy/saveversion.go b/pkg/deploy/saveversion.go index aa80598a0a6..b9c824ed656 100644 --- a/pkg/deploy/saveversion.go +++ b/pkg/deploy/saveversion.go @@ -18,6 +18,55 @@ import ( // SaveVersion for current location in shared storage account for environment func (d *deployer) SaveVersion(ctx context.Context) error { d.log.Printf("saving RP and OCP versions for RP %s deployed in %s to storage account %s", d.version, d.config.Location, *d.config.Configuration.RPVersionStorageAccountName) + blobClient, err := d.getAccountSASClient(ctx) + if err != nil { + return err + } + + // save version of RP which is deployed in this location + containerRef := blobClient.GetContainerReference("$web") + blobRef := containerRef.GetBlobReference(fmt.Sprintf("rpversion/%s", d.config.Location)) + return blobRef.CreateBlockBlobFromReader(bytes.NewReader([]byte(d.version)), nil) +} + +func (d *deployer) ensureStaticWebContent(ctx context.Context) error { + rgName, saName := *d.config.Configuration.GlobalResourceGroupName, *d.config.Configuration.RPVersionStorageAccountName + account, err := d.globalaccounts.GetProperties(ctx, rgName, saName, "") + if err != nil { + d.log.Errorf("error retrieving storage account %s properties: %v", saName, err) + return err + } + + if account.Kind != mgmtstorage.KindStorageV2 { + d.log.Printf("upgrading storage account %s to v2", saName) + params := mgmtstorage.AccountUpdateParameters{Kind: mgmtstorage.KindStorageV2} + account, err = d.globalaccounts.Update(ctx, rgName, saName, params) + if err != nil { + d.log.Errorf("error upgrading storage account %s: %v", saName, err) + return err + } + } + + d.log.Printf("enabling static web content on storage account %s", saName) + blobClient, err := d.getAccountSASClient(ctx) + if err != nil { + d.log.Errorf("error getting SAS client for storage account %s: %v", saName, err) + return err + } + + props := azstorage.ServiceProperties{ + StaticWebsite: &azstorage.StaticWebsite{Enabled: true}, + } + err = blobClient.SetServiceProperties(props) + if err != nil { + d.log.Errorf("error enabling static web content on storage account %s: %v", saName, err) + return err + } + + return nil +} + +func (d *deployer) getAccountSASClient(ctx context.Context) (*azstorage.BlobStorageClient, error) { t := time.Now().UTC().Truncate(time.Second) res, err := d.globalaccounts.ListAccountSAS( ctx, *d.config.Configuration.GlobalResourceGroupName, *d.config.Configuration.RPVersionStorageAccountName, mgmtstorage.AccountSasParameters{ @@ -29,30 +78,16 @@ func (d *deployer) SaveVersion(ctx context.Context) error { SharedAccessExpiryTime: &date.Time{Time: t.Add(24 * time.Hour)}, }) if err != nil { - return err + return nil, err } v, err := url.ParseQuery(*res.AccountSasToken) if err != nil { - return err + return nil, err } blobClient := azstorage.NewAccountSASClient( *d.config.Configuration.RPVersionStorageAccountName, v, (*d.env.Environment()).Environment).GetBlobService() - // ensure static web content is enabled - props := azstorage.ServiceProperties{ - StaticWebsite: &azstorage.StaticWebsite{ - Enabled: true, - }, - } - err = blobClient.SetServiceProperties(props) - if err != nil { - return err - } - - // save version of RP which is deployed in this location - containerRef := blobClient.GetContainerReference("$web") - blobRef := containerRef.GetBlobReference(fmt.Sprintf("rpversion/%s", d.config.Location)) - return blobRef.CreateBlockBlobFromReader(bytes.NewReader([]byte(d.version)), nil) + return &blobClient, nil }