github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/nomad/system_endpoint.go (about)

     1  package nomad
     2  
     3  import (
     4  	"fmt"
     5  
     6  	log "github.com/hashicorp/go-hclog"
     7  
     8  	"github.com/hashicorp/nomad/nomad/structs"
     9  )
    10  
    11  // System endpoint is used to call invoke system tasks.
    12  type System struct {
    13  	srv    *Server
    14  	logger log.Logger
    15  }
    16  
    17  // GarbageCollect is used to trigger the system to immediately garbage collect nodes, evals
    18  // and jobs.
    19  func (s *System) GarbageCollect(args *structs.GenericRequest, reply *structs.GenericResponse) error {
    20  	if done, err := s.srv.forward("System.GarbageCollect", args, args, reply); done {
    21  		return err
    22  	}
    23  
    24  	// Check management level permissions
    25  	if acl, err := s.srv.ResolveToken(args.AuthToken); err != nil {
    26  		return err
    27  	} else if acl != nil && !acl.IsManagement() {
    28  		return structs.ErrPermissionDenied
    29  	}
    30  
    31  	// Get the states current index
    32  	snapshotIndex, err := s.srv.fsm.State().LatestIndex()
    33  	if err != nil {
    34  		return fmt.Errorf("failed to determine state store's index: %v", err)
    35  	}
    36  
    37  	s.srv.evalBroker.Enqueue(s.srv.coreJobEval(structs.CoreJobForceGC, snapshotIndex))
    38  	return nil
    39  }
    40  
    41  // ReconcileSummaries reconciles the summaries of all the jobs in the state
    42  // store
    43  func (s *System) ReconcileJobSummaries(args *structs.GenericRequest, reply *structs.GenericResponse) error {
    44  	if done, err := s.srv.forward("System.ReconcileJobSummaries", args, args, reply); done {
    45  		return err
    46  	}
    47  
    48  	// Check management level permissions
    49  	if acl, err := s.srv.ResolveToken(args.AuthToken); err != nil {
    50  		return err
    51  	} else if acl != nil && !acl.IsManagement() {
    52  		return structs.ErrPermissionDenied
    53  	}
    54  
    55  	_, index, err := s.srv.raftApply(structs.ReconcileJobSummariesRequestType, args)
    56  	if err != nil {
    57  		return fmt.Errorf("reconciliation of job summaries failed: %v", err)
    58  	}
    59  	reply.Index = index
    60  	return nil
    61  }