Skip to content

Commit

Permalink
Move ensureStaticWebContent to dedicated step in predeploy; ensure ac…
Browse files Browse the repository at this point in the history
…count is upgraded to StorageV2 if necessary
  • Loading branch information
tsatam committed Jan 31, 2025
1 parent 0d7eabd commit 5153021
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 17 deletions.
5 changes: 5 additions & 0 deletions pkg/deploy/predeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
69 changes: 52 additions & 17 deletions pkg/deploy/saveversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
}

0 comments on commit 5153021

Please sign in to comment.