github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/scripts/juju-inspect/rules/mongo.go (about)

     1  // Copyright 2021 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package rules
     5  
     6  import (
     7  	"fmt"
     8  	"io"
     9  )
    10  
    11  type MongoRule struct {
    12  	found     map[string]bool
    13  	primaries map[string]bool
    14  }
    15  
    16  func NewMongoRule() *MongoRule {
    17  	return &MongoRule{
    18  		found:     make(map[string]bool),
    19  		primaries: make(map[string]bool),
    20  	}
    21  }
    22  
    23  func (r *MongoRule) Run(name string, report Report) error {
    24  	mgo, ok := report.Manifolds["transaction-pruner"]
    25  	if !ok {
    26  		r.found[name] = false
    27  		return nil
    28  	}
    29  
    30  	r.found[name] = true
    31  	r.primaries[name] = mgo.State == "started"
    32  
    33  	return nil
    34  }
    35  
    36  func (r *MongoRule) Write(w io.Writer) {
    37  	fmt.Fprintln(w, "Mongo Primary:")
    38  	fmt.Fprintln(w, "")
    39  
    40  	var primary bool
    41  	var ctrl string
    42  	for name, ldr := range r.primaries {
    43  		if primary && ldr {
    44  			// Two or more primaries
    45  			fmt.Fprintln(w, "\tTwo or more primaries have been found in the files!")
    46  			fmt.Fprintln(w, "")
    47  			return
    48  		}
    49  		if ldr {
    50  			primary = true
    51  			ctrl = name
    52  		}
    53  	}
    54  	if !primary {
    55  		fmt.Fprintln(w, "\tThere are no primaries found.")
    56  		fmt.Fprintln(w, "")
    57  		return
    58  	}
    59  	fmt.Fprintf(w, "\t%s is the primary.\n", ctrl)
    60  	fmt.Fprintln(w, "")
    61  }