github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/state/backups/backups.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  /*
     5  Package backups contains all the stand-alone backup-related
     6  functionality for juju state. That functionality is encapsulated by
     7  the backups.Backups type. The package also exposes a few key helpers
     8  and components.
     9  
    10  Backups are not a part of juju state nor of normal state operations.
    11  However, they certainly are tightly coupled with state (the very
    12  subject of backups). This puts backups in an odd position, particularly
    13  with regard to the storage of backup metadata and archives.
    14  
    15  As noted above backups are about state but not a part of state. So
    16  exposing backup-related methods on State would imply the wrong thing.
    17  Thus most of the functionality here is defined at a high level without
    18  relation to state. A few low-level parts or helpers are exposed as
    19  functions to which you pass a state value. Those are kept to a minimum.
    20  
    21  Note that state (and juju as a whole) currently does not have a
    22  persistence layer abstraction to facilitate separating different
    23  persistence needs and implementations. As a consequence, state's
    24  data, whether about how an environment should look or about existing
    25  resources within an environment, is dumped essentially straight into
    26  State's mongo connection. The code in the state package does not
    27  make any distinction between the two (nor does the package clearly
    28  distinguish between state-related abstractions and state-related
    29  data).
    30  
    31  Backups add yet another category, merely taking advantage of State's
    32  mongo for storage. In the interest of making the distinction clear,
    33  among other reasons, backups uses its own database under state's mongo
    34  connection.
    35  */
    36  package backups
    37  
    38  import (
    39  	"io"
    40  	"time"
    41  
    42  	"github.com/juju/errors"
    43  	"github.com/juju/loggo"
    44  	"github.com/juju/utils/filestorage"
    45  )
    46  
    47  const (
    48  	// FilenamePrefix is the prefix used for backup archive files.
    49  	FilenamePrefix = "juju-backup-"
    50  
    51  	// FilenameTemplate is used with time.Time.Format to generate a filename.
    52  	FilenameTemplate = FilenamePrefix + "20060102-150405.tar.gz"
    53  )
    54  
    55  var logger = loggo.GetLogger("juju.state.backups")
    56  
    57  var (
    58  	getFilesToBackUp = GetFilesToBackUp
    59  	getDBDumper      = NewDBDumper
    60  	runCreate        = create
    61  	finishMeta       = func(meta *Metadata, result *createResult) error {
    62  		return meta.MarkComplete(result.size, result.checksum)
    63  	}
    64  	storeArchive = StoreArchive
    65  )
    66  
    67  // StoreArchive sends the backup archive and its metadata to storage.
    68  // It also sets the metadata's ID and Stored values.
    69  func StoreArchive(stor filestorage.FileStorage, meta *Metadata, file io.Reader) error {
    70  	id, err := stor.Add(meta, file)
    71  	if err != nil {
    72  		return errors.Trace(err)
    73  	}
    74  	meta.SetID(id)
    75  	stored, err := stor.Metadata(id)
    76  	if err != nil {
    77  		return errors.Trace(err)
    78  	}
    79  	meta.SetStored(stored.Stored())
    80  	return nil
    81  }
    82  
    83  // Backups is an abstraction around all juju backup-related functionality.
    84  type Backups interface {
    85  	// Create creates and stores a new juju backup archive. It updates
    86  	// the provided metadata.
    87  	Create(meta *Metadata, paths *Paths, dbInfo *DBInfo) error
    88  
    89  	// Add stores the backup archive and returns its new ID.
    90  	Add(archive io.Reader, meta *Metadata) (string, error)
    91  
    92  	// Get returns the metadata and archive file associated with the ID.
    93  	Get(id string) (*Metadata, io.ReadCloser, error)
    94  
    95  	// List returns the metadata for all stored backups.
    96  	List() ([]*Metadata, error)
    97  
    98  	// Remove deletes the backup from storage.
    99  	Remove(id string) error
   100  
   101  	// Restore updates juju's state to the contents of the backup archive.
   102  	Restore(backupId string, args RestoreArgs) error
   103  }
   104  
   105  type backups struct {
   106  	storage filestorage.FileStorage
   107  }
   108  
   109  // NewBackups creates a new Backups value using the FileStorage provided.
   110  func NewBackups(stor filestorage.FileStorage) Backups {
   111  	b := backups{
   112  		storage: stor,
   113  	}
   114  	return &b
   115  }
   116  
   117  // Create creates and stores a new juju backup archive and updates the
   118  // provided metadata.
   119  func (b *backups) Create(meta *Metadata, paths *Paths, dbInfo *DBInfo) error {
   120  	meta.Started = time.Now().UTC()
   121  
   122  	// The metadata file will not contain the ID or the "finished" data.
   123  	// However, that information is not as critical. The alternatives
   124  	// are either adding the metadata file to the archive after the fact
   125  	// or adding placeholders here for the finished data and filling
   126  	// them in afterward.  Neither is particularly trivial.
   127  	metadataFile, err := meta.AsJSONBuffer()
   128  	if err != nil {
   129  		return errors.Annotate(err, "while preparing the metadata")
   130  	}
   131  
   132  	// Create the archive.
   133  	filesToBackUp, err := getFilesToBackUp("", paths, meta.Origin.Machine)
   134  	if err != nil {
   135  		return errors.Annotate(err, "while listing files to back up")
   136  	}
   137  	dumper, err := getDBDumper(dbInfo)
   138  	if err != nil {
   139  		return errors.Annotate(err, "while preparing for DB dump")
   140  	}
   141  	args := createArgs{filesToBackUp, dumper, metadataFile}
   142  	result, err := runCreate(&args)
   143  	if err != nil {
   144  		return errors.Annotate(err, "while creating backup archive")
   145  	}
   146  	defer result.archiveFile.Close()
   147  
   148  	// Finalize the metadata.
   149  	err = finishMeta(meta, result)
   150  	if err != nil {
   151  		return errors.Annotate(err, "while updating metadata")
   152  	}
   153  
   154  	// Store the archive.
   155  	err = storeArchive(b.storage, meta, result.archiveFile)
   156  	if err != nil {
   157  		return errors.Annotate(err, "while storing backup archive")
   158  	}
   159  
   160  	return nil
   161  }
   162  
   163  // Add stores the backup archive and returns its new ID.
   164  func (b *backups) Add(archive io.Reader, meta *Metadata) (string, error) {
   165  	// Store the archive.
   166  	err := storeArchive(b.storage, meta, archive)
   167  	if err != nil {
   168  		return "", errors.Annotate(err, "while storing backup archive")
   169  	}
   170  
   171  	return meta.ID(), nil
   172  }
   173  
   174  // Get retrieves the associated metadata and archive file from environment storage.
   175  func (b *backups) Get(id string) (*Metadata, io.ReadCloser, error) {
   176  	rawmeta, archiveFile, err := b.storage.Get(id)
   177  	if err != nil {
   178  		return nil, nil, errors.Trace(err)
   179  	}
   180  
   181  	meta, ok := rawmeta.(*Metadata)
   182  	if !ok {
   183  		return nil, nil, errors.New("did not get a backups.Metadata value from storage")
   184  	}
   185  
   186  	return meta, archiveFile, nil
   187  }
   188  
   189  // List returns the metadata for all stored backups.
   190  func (b *backups) List() ([]*Metadata, error) {
   191  	metaList, err := b.storage.List()
   192  	if err != nil {
   193  		return nil, errors.Trace(err)
   194  	}
   195  	result := make([]*Metadata, len(metaList))
   196  	for i, meta := range metaList {
   197  		m, ok := meta.(*Metadata)
   198  		if !ok {
   199  			msg := "expected backups.Metadata value from storage for %q, got %T"
   200  			return nil, errors.Errorf(msg, meta.ID(), meta)
   201  		}
   202  		result[i] = m
   203  	}
   204  	return result, nil
   205  }
   206  
   207  // Remove deletes the backup from storage.
   208  func (b *backups) Remove(id string) error {
   209  	return errors.Trace(b.storage.Remove(id))
   210  }