forked from juju/juju
/
binarystorage.go
49 lines (40 loc) · 1.49 KB
/
binarystorage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
jujutxn "github.com/juju/txn"
"gopkg.in/juju/blobstore.v2"
"gopkg.in/mgo.v2"
"github.com/juju/juju/state/binarystorage"
)
var binarystorageNew = binarystorage.New
// ToolsStorage returns a new binarystorage.StorageCloser that stores tools
// metadata in the "juju" database "toolsmetadata" collection.
//
// TODO(axw) remove this, add a constructor function in binarystorage.
func (st *State) ToolsStorage() (binarystorage.StorageCloser, error) {
return newStorage(st, st.ModelUUID(), toolsmetadataC), nil
}
// GUIStorage returns a new binarystorage.StorageCloser that stores GUI archive
// metadata in the "juju" database "guimetadata" collection.
func (st *State) GUIStorage() (binarystorage.StorageCloser, error) {
return newStorage(st, st.controllerTag.Id(), guimetadataC), nil
}
func newStorage(st *State, uuid, metadataCollection string) binarystorage.StorageCloser {
session := st.session.Clone()
rs := blobstore.NewGridFS(blobstoreDB, uuid, session)
db := session.DB(jujuDB)
c := db.C(metadataCollection)
txnRunner := jujutxn.NewRunner(jujutxn.RunnerParams{Database: db})
managedStorage := blobstore.NewManagedStorage(db, rs)
storage := binarystorageNew(uuid, managedStorage, c, txnRunner)
return &storageCloser{storage, session}
}
type storageCloser struct {
binarystorage.Storage
session *mgo.Session
}
func (t *storageCloser) Close() error {
t.session.Close()
return nil
}