github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	"gopkg.in/juju/charm.v6"
     9  
    10  	"github.com/juju/juju/core/status"
    11  	"github.com/juju/juju/downloader"
    12  	jujucharm "github.com/juju/juju/worker/uniter/charm"
    13  )
    14  
    15  // Downloader provides an interface for downloading files to disk.
    16  type Downloader interface {
    17  	// Download downloads a file to a local directory, and
    18  	// returns the local path to the file.
    19  	Download(downloader.Request) (string, error)
    20  }
    21  
    22  type charmInfo struct {
    23  	curl   *charm.URL
    24  	sha256 string
    25  }
    26  
    27  func (c *charmInfo) URL() *charm.URL {
    28  	return c.curl
    29  }
    30  
    31  func (c *charmInfo) ArchiveSha256() (string, error) {
    32  	return c.sha256, nil
    33  }
    34  
    35  func (op *caasOperator) ensureCharm(localState *LocalState) error {
    36  	curl, _, sha256, vers, err := op.config.CharmGetter.Charm(op.config.Application)
    37  	if err != nil {
    38  		return errors.Trace(err)
    39  	}
    40  	localState.CharmModifiedVersion = vers
    41  	if localState.CharmURL == curl {
    42  		logger.Debugf("charm %s already downloaded", curl)
    43  		return nil
    44  	}
    45  	if err := op.setStatus(status.Maintenance, "downloading charm (%s)", curl); err != nil {
    46  		return errors.Trace(err)
    47  	}
    48  
    49  	info := &charmInfo{curl: curl, sha256: sha256}
    50  	if err := op.deployer.Stage(info, op.catacomb.Dying()); err != nil {
    51  		return errors.Trace(err)
    52  	}
    53  
    54  	if err := op.deployer.Deploy(); err != nil {
    55  		if err == jujucharm.ErrConflict {
    56  			err = op.setStatus(status.Error, "upgrade failed")
    57  		}
    58  		return errors.Trace(err)
    59  	}
    60  	localState.CharmURL = curl
    61  	return op.stateFile.Write(localState)
    62  }