Documentation ¶
Index ¶
- Constants
- Variables
- func DialInfo(info Info, opts DialOpts) (*mgo.DialInfo, error)
- func DialWithInfo(info Info, opts DialOpts) (*mgo.Session, error)
- func EnsureAdminUser(p EnsureAdminUserParams) (added bool, err error)
- func EnsureServer(args EnsureServerParams) error
- func GenerateSharedSecret() (string, error)
- func GetOplog(session *mgo.Session) *mgo.Collection
- func IsMaster(session *mgo.Session, obj WithAddresses) (bool, error)
- func NewMongoTimestamp(t time.Time) bson.MongoTimestamp
- func Path() (string, error)
- func RemoveService(namespace string) error
- func SelectPeerAddress(addrs []network.Address) string
- func SelectPeerHostPort(hostPorts []network.HostPort) string
- func ServiceName(namespace string) string
- func SetAdminMongoPassword(session *mgo.Session, user, password string) error
- func UpdateSSLKey(dataDir, cert, privateKey string) error
- type Collection
- type DialOpts
- type EnsureAdminUserParams
- type EnsureServerParams
- type Info
- type MongoInfo
- type OplogDoc
- type OplogTailer
- type WithAddresses
- type WriteCollection
Constants ¶
const ( // located within the Juju data directory. SharedSecretFile = "shared-secret" // ReplicaSetName is the name of the replica set that juju uses for its // state servers. ReplicaSetName = "juju" )
const AdminUser = "admin"
AdminUser is the name of the user that is initially created in mongo.
const SocketTimeout = 21 * time.Second
SocketTimeout should be long enough that even a slow mongo server will respond in that length of time. Since mongo servers ping themselves every 10 seconds, we use a value of just over 2 ping periods to allow for delayed pings due to issues such as CPU starvation etc.
Variables ¶
var IsServiceInstalled = isServiceInstalled
IsServiceInstalled returns whether the MongoDB init service configuration is present.
var ( // JujuMongodPath holds the default path to the juju-specific // mongod. JujuMongodPath = "/usr/lib/juju/bin/mongod" )
Functions ¶
func DialInfo ¶
DialInfo returns information on how to dial the state's mongo server with the given info and dial options.
func DialWithInfo ¶
DialWithInfo establishes a new session to the cluster identified by info, with the specified options.
func EnsureAdminUser ¶
func EnsureAdminUser(p EnsureAdminUserParams) (added bool, err error)
EnsureAdminUser ensures that the specified user and password are added to the admin database.
This function will stop the Mongo service if it needs to add the admin user, as it must restart Mongo in --noauth mode.
func EnsureServer ¶
func EnsureServer(args EnsureServerParams) error
EnsureServer ensures that the MongoDB server is installed, configured, and ready to run.
This method will remove old versions of the mongo init service as necessary before installing the new version.
The namespace is a unique identifier to prevent multiple instances of mongo on this machine from colliding. This should be empty unless using the local provider.
func GenerateSharedSecret ¶
GenerateSharedSecret generates a pseudo-random shared secret (keyfile) for use with Mongo replica sets.
func GetOplog ¶
func GetOplog(session *mgo.Session) *mgo.Collection
GetOplog returns the the oplog collection in the local database.
func IsMaster ¶
func IsMaster(session *mgo.Session, obj WithAddresses) (bool, error)
IsMaster returns a boolean that represents whether the given machine's peer address is the primary mongo host for the replicaset
func NewMongoTimestamp ¶
func NewMongoTimestamp(t time.Time) bson.MongoTimestamp
NewMongoTimestamp returns a bson.MongoTimestamp repesentation for the time.Time given. Note that these timestamps are not the same the usual MongoDB time fields. These are an internal format used only in a few places such as the replication oplog.
See: http://docs.mongodb.org/manual/reference/bson-types/#timestamps
func Path ¶
Path returns the executable path to be used to run mongod on this machine. If the juju-bundled version of mongo exists, it will return that path, otherwise it will return the command to run mongod from the path.
func RemoveService ¶
RemoveService removes the mongoDB init service from this machine.
func SelectPeerAddress ¶
SelectPeerAddress returns the address to use as the mongo replica set peer address by selecting it from the given addresses. If no addresses are available an empty string is returned.
func SelectPeerHostPort ¶
SelectPeerHostPort returns the HostPort to use as the mongo replica set peer by selecting it from the given hostPorts.
func ServiceName ¶
ServiceName returns the name of the init service config for mongo using the given namespace.
func SetAdminMongoPassword ¶
SetAdminMongoPassword sets the administrative password to access a mongo database. If the password is non-empty, all subsequent attempts to access the database must be authorized; otherwise no authorization is required.
func UpdateSSLKey ¶
UpdateSSLKey writes a new SSL key used by mongo to validate connections from Juju state server(s)
Types ¶
type Collection ¶
type Collection interface { // Name returns the name of the collection. Name() string // Count, Find, and FindId methods act as documented for *mgo.Collection. Count() (int, error) Find(query interface{}) *mgo.Query FindId(id interface{}) *mgo.Query // Writeable gives access to methods that enable direct DB access. It // should be used with judicious care, and for only the best of reasons. Writeable() WriteCollection }
Collection imperfectly insulates clients from the capacity to write to MongoDB. Query results can still be used to write; and the Writeable method exposes the underlying *mgo.Collection when absolutely required; but the general expectation in juju is that writes will occur only via mgo/txn, and any layer-skipping is done only in exceptional and well- supported circumstances.
func CollectionFromName ¶
func CollectionFromName(db *mgo.Database, coll string) (Collection, func())
CollectionFromName returns a named collection on the specified database, initialised with a new session. Also returned is a close function which must be called when the collection is no longer required.
func WrapCollection ¶
func WrapCollection(coll *mgo.Collection) Collection
WrapCollection returns a Collection that wraps the supplied *mgo.Collection.
type DialOpts ¶
type DialOpts struct { // Timeout is the amount of time to wait contacting // a state server. Timeout time.Duration // SocketTimeout is the amount of time to wait for a // non-responding socket to the database before it is forcefully // closed. If this is zero, the value of the SocketTimeout const // will be used. SocketTimeout time.Duration // Direct informs whether to establish connections only with the // specified seed servers, or to obtain information for the whole // cluster and establish connections with further servers too. Direct bool // PostDial, if non-nil, is called by DialWithInfo with the // mgo.Session after a successful dial but before DialWithInfo // returns to its caller. PostDial func(*mgo.Session) error }
DialOpts holds configuration parameters that control the Dialing behavior when connecting to a state server.
func DefaultDialOpts ¶
func DefaultDialOpts() DialOpts
DefaultDialOpts returns a DialOpts representing the default parameters for contacting a state server.
type EnsureAdminUserParams ¶
type EnsureAdminUserParams struct { // DialInfo specifies how to connect to the mongo server. DialInfo *mgo.DialInfo // Namespace is the agent namespace, used to derive the Mongo service name. Namespace string // DataDir is the Juju data directory, used to start a --noauth server. DataDir string // Port is the listening port of the Mongo server. Port int // User holds the user to log in to the mongo server as. User string // Password holds the password for the user to log in as. Password string }
type EnsureServerParams ¶
type EnsureServerParams struct { // APIPort is the port to connect to the api server. APIPort int // StatePort is the port to connect to the mongo server. StatePort int // Cert is the certificate. Cert string // PrivateKey is the certificate's private key. PrivateKey string // CAPrivateKey is the CA certificate's private key. CAPrivateKey string SharedSecret string // SystemIdentity is the identity of the system. SystemIdentity string // DataDir is the machine agent data directory. DataDir string // Namespace is the machine agent's namespace, which is used to // generate a unique service name for Mongo. Namespace string // OplogSize is the size of the Mongo oplog. // If this is zero, then EnsureServer will // calculate a default size according to the // algorithm defined in Mongo. OplogSize int // SetNumaControlPolicy preference - whether the user // wants to set the numa control policy when starting mongo. SetNumaControlPolicy bool }
EnsureServerParams is a parameter struct for EnsureServer.
type Info ¶
type Info struct { // Addrs gives the addresses of the MongoDB servers for the state. // Each address should be in the form address:port. Addrs []string // CACert holds the CA certificate that will be used // to validate the state server's certificate, in PEM format. CACert string }
Info encapsulates information about cluster of mongo servers and can be used to make a connection to that cluster.
type MongoInfo ¶
type MongoInfo struct { // mongo.Info contains the addresses and cert of the mongo cluster. Info // Tag holds the name of the entity that is connecting. // It should be nil when connecting as an administrator. Tag names.Tag // Password holds the password for the connecting entity. Password string }
MongoInfo encapsulates information about cluster of servers holding juju state and can be used to make a connection to that cluster.
type OplogDoc ¶
type OplogDoc struct { Timestamp bson.MongoTimestamp `bson:"ts"` OperationId int64 `bson:"h"` MongoVersion int `bson:"v"` Operation string `bson:"op"` // "i" - insert, "u" - update, "d" - delete Namespace string `bson:"ns"` Object *bson.Raw `bson:"o"` UpdateObject *bson.Raw `bson:"o2"` }
OplogDoc represents a document in the oplog.rs collection. See: http://www.kchodorow.com/blog/2010/10/12/replication-internals/
The Object and UpdateObject fields are returned raw to allow unmarshalling into arbitrary types. Use the UnmarshalObject and UnmarshalUpdate methods to unmarshall these fields.
func (*OplogDoc) UnmarshalObject ¶
UnmarshalObject unmarshals the Object field into out. The out argument should be a pointer or a suitable map.
func (*OplogDoc) UnmarshalUpdate ¶
UnmarshalUpdate unmarshals the UpdateObject field into out. The out argument should be a pointer or a suitable map.
type OplogTailer ¶
type OplogTailer struct {
// contains filtered or unexported fields
}
OplogTailer tails MongoDB's replication oplog.
func NewOplogTailer ¶
func NewOplogTailer( oplog *mgo.Collection, query bson.D, initialTs time.Time, ) *OplogTailer
NewOplogTailer returns a new OplogTailer.
Arguments:
- "oplog" is the collection to use for the oplog. Typically this would be the result of GetOpLog.
- "query" can be used to limit the returned oplog entries. A typical filter would limit based on ns ("<database>.<collection>") and o (object).
- "initialTs" sets the operation timestamp to start returning results from. This can be used to avoid an expensive initial search through the oplog when the tailer first starts.
Remember to call Stop on the returned OplogTailer when it is no longer needed.
func (*OplogTailer) Dying ¶
func (t *OplogTailer) Dying() <-chan struct{}
Dying returns a channel that will be closed with the OplogTailer is shutting down.
func (*OplogTailer) Err ¶
func (t *OplogTailer) Err() error
Err returns the error that caused the OplogTailer to stop. If it finished normally or hasn't stopped then nil will be returned.
func (*OplogTailer) Out ¶
func (t *OplogTailer) Out() <-chan *OplogDoc
Out returns a channel that reports the oplog entries matching the query passed to NewOplogTailer as they appear.
func (*OplogTailer) Stop ¶
func (t *OplogTailer) Stop() error
Stop shuts down the OplogTailer. It will block until shutdown is complete.
type WithAddresses ¶
WithAddresses represents an entity that has a set of addresses. e.g. a state Machine object
type WriteCollection ¶
type WriteCollection interface { Collection // Underlying returns the underlying *mgo.Collection. Underlying() *mgo.Collection // All other methods act as documented for *mgo.Collection. Insert(docs ...interface{}) error Upsert(selector interface{}, update interface{}) (info *mgo.ChangeInfo, err error) UpsertId(id interface{}, update interface{}) (info *mgo.ChangeInfo, err error) Update(selector interface{}, update interface{}) error UpdateId(id interface{}, update interface{}) error Remove(sel interface{}) error RemoveId(id interface{}) error RemoveAll(sel interface{}) (*mgo.ChangeInfo, error) }
WriteCollection allows read/write access to a MongoDB collection.