github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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 model should look or about existing
    25  resources within an model, 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  	"gopkg.in/juju/names.v2"
    46  )
    47  
    48  const (
    49  	// FilenamePrefix is the prefix used for backup archive files.
    50  	FilenamePrefix = "juju-backup-"
    51  
    52  	// FilenameTemplate is used with time.Time.Format to generate a filename.
    53  	FilenameTemplate = FilenamePrefix + "20060102-150405.tar.gz"
    54  )
    55  
    56  var logger = loggo.GetLogger("juju.state.backups")
    57  
    58  var (
    59  	getFilesToBackUp = GetFilesToBackUp
    60  	getDBDumper      = NewDBDumper
    61  	runCreate        = create
    62  	finishMeta       = func(meta *Metadata, result *createResult) error {
    63  		return meta.MarkComplete(result.size, result.checksum)
    64  	}
    65  	storeArchive = StoreArchive
    66  )
    67  
    68  // StoreArchive sends the backup archive and its metadata to storage.
    69  // It also sets the metadata's ID and Stored values.
    70  func StoreArchive(stor filestorage.FileStorage, meta *Metadata, file io.Reader) error {
    71  	id, err := stor.Add(meta, file)
    72  	if err != nil {
    73  		return errors.Trace(err)
    74  	}
    75  	meta.SetID(id)
    76  	stored, err := stor.Metadata(id)
    77  	if err != nil {
    78  		return errors.Trace(err)
    79  	}
    80  	meta.SetStored(stored.Stored())
    81  	return nil
    82  }
    83  
    84  // Backups is an abstraction around all juju backup-related functionality.
    85  type Backups interface {
    86  	// Create creates and stores a new juju backup archive. It updates
    87  	// the provided metadata.
    88  	Create(meta *Metadata, paths *Paths, dbInfo *DBInfo) error
    89  
    90  	// Add stores the backup archive and returns its new ID.
    91  	Add(archive io.Reader, meta *Metadata) (string, error)
    92  
    93  	// Get returns the metadata and archive file associated with the ID.
    94  	Get(id string) (*Metadata, io.ReadCloser, error)
    95  
    96  	// List returns the metadata for all stored backups.
    97  	List() ([]*Metadata, error)
    98  
    99  	// Remove deletes the backup from storage.
   100  	Remove(id string) error
   101  
   102  	// Restore updates juju's state to the contents of the backup archive,
   103  	// it returns the tag string for the machine where the backup originated
   104  	// or error if the process fails.
   105  	Restore(backupId string, dbInfo *DBInfo, args RestoreArgs) (names.Tag, error)
   106  }
   107  
   108  type backups struct {
   109  	storage filestorage.FileStorage
   110  }
   111  
   112  // NewBackups creates a new Backups value using the FileStorage provided.
   113  func NewBackups(stor filestorage.FileStorage) Backups {
   114  	b := backups{
   115  		storage: stor,
   116  	}
   117  	return &b
   118  }
   119  
   120  // Create creates and stores a new juju backup archive and updates the
   121  // provided metadata.
   122  func (b *backups) Create(meta *Metadata, paths *Paths, dbInfo *DBInfo) error {
   123  	// TODO(fwereade): 2016-03-17 lp:1558657
   124  	meta.Started = time.Now().UTC()
   125  
   126  	// The metadata file will not contain the ID or the "finished" data.
   127  	// However, that information is not as critical. The alternatives
   128  	// are either adding the metadata file to the archive after the fact
   129  	// or adding placeholders here for the finished data and filling
   130  	// them in afterward.  Neither is particularly trivial.
   131  	metadataFile, err := meta.AsJSONBuffer()
   132  	if err != nil {
   133  		return errors.Annotate(err, "while preparing the metadata")
   134  	}
   135  
   136  	// Create the archive.
   137  	filesToBackUp, err := getFilesToBackUp("", paths, meta.Origin.Machine)
   138  	if err != nil {
   139  		return errors.Annotate(err, "while listing files to back up")
   140  	}
   141  	dumper, err := getDBDumper(dbInfo)
   142  	if err != nil {
   143  		return errors.Annotate(err, "while preparing for DB dump")
   144  	}
   145  	args := createArgs{filesToBackUp, dumper, metadataFile}
   146  	result, err := runCreate(&args)
   147  	if err != nil {
   148  		return errors.Annotate(err, "while creating backup archive")
   149  	}
   150  	defer result.archiveFile.Close()
   151  
   152  	// Finalize the metadata.
   153  	err = finishMeta(meta, result)
   154  	if err != nil {
   155  		return errors.Annotate(err, "while updating metadata")
   156  	}
   157  
   158  	// Store the archive.
   159  	err = storeArchive(b.storage, meta, result.archiveFile)
   160  	if err != nil {
   161  		return errors.Annotate(err, "while storing backup archive")
   162  	}
   163  
   164  	return nil
   165  }
   166  
   167  // Add stores the backup archive and returns its new ID.
   168  func (b *backups) Add(archive io.Reader, meta *Metadata) (string, error) {
   169  	// Store the archive.
   170  	err := storeArchive(b.storage, meta, archive)
   171  	if err != nil {
   172  		return "", errors.Annotate(err, "while storing backup archive")
   173  	}
   174  
   175  	return meta.ID(), nil
   176  }
   177  
   178  // Get retrieves the associated metadata and archive file from model storage.
   179  func (b *backups) Get(id string) (*Metadata, io.ReadCloser, error) {
   180  	rawmeta, archiveFile, err := b.storage.Get(id)
   181  	if err != nil {
   182  		return nil, nil, errors.Trace(err)
   183  	}
   184  
   185  	meta, ok := rawmeta.(*Metadata)
   186  	if !ok {
   187  		return nil, nil, errors.New("did not get a backups.Metadata value from storage")
   188  	}
   189  
   190  	return meta, archiveFile, nil
   191  }
   192  
   193  // List returns the metadata for all stored backups.
   194  func (b *backups) List() ([]*Metadata, error) {
   195  	metaList, err := b.storage.List()
   196  	if err != nil {
   197  		return nil, errors.Trace(err)
   198  	}
   199  	result := make([]*Metadata, len(metaList))
   200  	for i, meta := range metaList {
   201  		m, ok := meta.(*Metadata)
   202  		if !ok {
   203  			msg := "expected backups.Metadata value from storage for %q, got %T"
   204  			return nil, errors.Errorf(msg, meta.ID(), meta)
   205  		}
   206  		result[i] = m
   207  	}
   208  	return result, nil
   209  }
   210  
   211  // Remove deletes the backup from storage.
   212  func (b *backups) Remove(id string) error {
   213  	return errors.Trace(b.storage.Remove(id))
   214  }