github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/caasoperator/download.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package caasoperator
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/core/status"
    10  	"github.com/juju/juju/downloader"
    11  	jujucharm "github.com/juju/juju/worker/uniter/charm"
    12  )
    13  
    14  // Downloader provides an interface for downloading files to disk.
    15  type Downloader interface {
    16  	// Download downloads a file to a local directory, and
    17  	// returns the local path to the file.
    18  	Download(downloader.Request) (string, error)
    19  }
    20  
    21  type charmInfo struct {
    22  	curl   string
    23  	sha256 string
    24  }
    25  
    26  func (c *charmInfo) URL() string {
    27  	return c.curl
    28  }
    29  
    30  func (c *charmInfo) ArchiveSha256() (string, error) {
    31  	return c.sha256, nil
    32  }
    33  
    34  func (c *charmInfo) LXDProfileRequired() (bool, error) {
    35  	// Not applicable to CAAS units.
    36  	return false, errors.NotImplementedf("LXDProfileRequired")
    37  }
    38  
    39  func (op *caasOperator) ensureCharm(localState *LocalState) error {
    40  	dbCharmInfo, err := op.config.CharmGetter.Charm(op.config.Application)
    41  	if err != nil {
    42  		return errors.Trace(err)
    43  	}
    44  	op.deploymentMode = dbCharmInfo.DeploymentMode
    45  	localState.CharmModifiedVersion = dbCharmInfo.CharmModifiedVersion
    46  	curl := dbCharmInfo.URL
    47  	if localState.CharmURL == curl {
    48  		op.config.Logger.Debugf("charm %s already downloaded", curl)
    49  		return nil
    50  	}
    51  	if err := op.setStatus(status.Maintenance, "downloading charm (%s)", curl); err != nil {
    52  		return errors.Trace(err)
    53  	}
    54  
    55  	info := &charmInfo{curl: curl.String(), sha256: dbCharmInfo.SHA256}
    56  	if err := op.deployer.Stage(info, op.catacomb.Dying()); err != nil {
    57  		return errors.Trace(err)
    58  	}
    59  
    60  	if err := op.deployer.Deploy(); err != nil {
    61  		if err == jujucharm.ErrConflict {
    62  			err = op.setStatus(status.Error, "upgrade failed")
    63  		}
    64  		return errors.Trace(err)
    65  	}
    66  	localState.CharmURL = curl
    67  	return op.stateFile.Write(localState)
    68  }