github.com/jrxfive/nomad@v0.6.1-0.20170802162750-1fef470e89bf/nomad/deployment_watcher_shims.go (about)

     1  package nomad
     2  
     3  import (
     4  	"github.com/hashicorp/nomad/nomad/structs"
     5  )
     6  
     7  // deploymentWatcherStateShim is the shim that provides the state watching
     8  // methods. These should be set by the server and passed to the deployment
     9  // watcher.
    10  type deploymentWatcherStateShim struct {
    11  	// region is the region the server is a member of. It is used to
    12  	// auto-populate requests that do not have it set
    13  	region string
    14  
    15  	// evaluations returns the set of evaluations for the given job
    16  	evaluations func(args *structs.JobSpecificRequest, reply *structs.JobEvaluationsResponse) error
    17  
    18  	// allocations returns the set of allocations that are part of the
    19  	// deployment.
    20  	allocations func(args *structs.DeploymentSpecificRequest, reply *structs.AllocListResponse) error
    21  
    22  	// list is used to list all the deployments in the system
    23  	list func(args *structs.DeploymentListRequest, reply *structs.DeploymentListResponse) error
    24  
    25  	// GetDeployment is used to lookup a particular deployment.
    26  	getDeployment func(args *structs.DeploymentSpecificRequest, reply *structs.SingleDeploymentResponse) error
    27  
    28  	// getJobVersions is used to lookup the versions of a job. This is used when
    29  	// rolling back to find the latest stable job
    30  	getJobVersions func(args *structs.JobVersionsRequest, reply *structs.JobVersionsResponse) error
    31  
    32  	// getJob is used to lookup a particular job.
    33  	getJob func(args *structs.JobSpecificRequest, reply *structs.SingleJobResponse) error
    34  }
    35  
    36  func (d *deploymentWatcherStateShim) Evaluations(args *structs.JobSpecificRequest, reply *structs.JobEvaluationsResponse) error {
    37  	if args.Region == "" {
    38  		args.Region = d.region
    39  	}
    40  
    41  	return d.evaluations(args, reply)
    42  }
    43  
    44  func (d *deploymentWatcherStateShim) Allocations(args *structs.DeploymentSpecificRequest, reply *structs.AllocListResponse) error {
    45  	if args.Region == "" {
    46  		args.Region = d.region
    47  	}
    48  
    49  	return d.allocations(args, reply)
    50  }
    51  
    52  func (d *deploymentWatcherStateShim) List(args *structs.DeploymentListRequest, reply *structs.DeploymentListResponse) error {
    53  	if args.Region == "" {
    54  		args.Region = d.region
    55  	}
    56  
    57  	return d.list(args, reply)
    58  }
    59  
    60  func (d *deploymentWatcherStateShim) GetDeployment(args *structs.DeploymentSpecificRequest, reply *structs.SingleDeploymentResponse) error {
    61  	if args.Region == "" {
    62  		args.Region = d.region
    63  	}
    64  
    65  	return d.getDeployment(args, reply)
    66  }
    67  
    68  func (d *deploymentWatcherStateShim) GetJobVersions(args *structs.JobVersionsRequest, reply *structs.JobVersionsResponse) error {
    69  	if args.Region == "" {
    70  		args.Region = d.region
    71  	}
    72  
    73  	return d.getJobVersions(args, reply)
    74  }
    75  
    76  func (d *deploymentWatcherStateShim) GetJob(args *structs.JobSpecificRequest, reply *structs.SingleJobResponse) error {
    77  	if args.Region == "" {
    78  		args.Region = d.region
    79  	}
    80  
    81  	return d.getJob(args, reply)
    82  }
    83  
    84  // deploymentWatcherRaftShim is the shim that provides the state watching
    85  // methods. These should be set by the server and passed to the deployment
    86  // watcher.
    87  type deploymentWatcherRaftShim struct {
    88  	// apply is used to apply a message to Raft
    89  	apply raftApplyFn
    90  }
    91  
    92  // convertApplyErrors parses the results of a raftApply and returns the index at
    93  // which it was applied and any error that occured. Raft Apply returns two
    94  // seperate errors, Raft library errors and user returned errors from the FSM.
    95  // This helper, joins the errors by inspecting the applyResponse for an error.
    96  func (d *deploymentWatcherRaftShim) convertApplyErrors(applyResp interface{}, index uint64, err error) (uint64, error) {
    97  	if applyResp != nil {
    98  		if fsmErr, ok := applyResp.(error); ok && fsmErr != nil {
    99  			return index, fsmErr
   100  		}
   101  	}
   102  	return index, err
   103  }
   104  
   105  func (d *deploymentWatcherRaftShim) UpsertEvals(evals []*structs.Evaluation) (uint64, error) {
   106  	update := &structs.EvalUpdateRequest{
   107  		Evals: evals,
   108  	}
   109  	fsmErrIntf, index, raftErr := d.apply(structs.EvalUpdateRequestType, update)
   110  	return d.convertApplyErrors(fsmErrIntf, index, raftErr)
   111  }
   112  
   113  func (d *deploymentWatcherRaftShim) UpsertJob(job *structs.Job) (uint64, error) {
   114  	job.SetSubmitTime()
   115  	update := &structs.JobRegisterRequest{
   116  		Job: job,
   117  	}
   118  	fsmErrIntf, index, raftErr := d.apply(structs.JobRegisterRequestType, update)
   119  	return d.convertApplyErrors(fsmErrIntf, index, raftErr)
   120  }
   121  
   122  func (d *deploymentWatcherRaftShim) UpdateDeploymentStatus(u *structs.DeploymentStatusUpdateRequest) (uint64, error) {
   123  	fsmErrIntf, index, raftErr := d.apply(structs.DeploymentStatusUpdateRequestType, u)
   124  	return d.convertApplyErrors(fsmErrIntf, index, raftErr)
   125  }
   126  
   127  func (d *deploymentWatcherRaftShim) UpdateDeploymentPromotion(req *structs.ApplyDeploymentPromoteRequest) (uint64, error) {
   128  	fsmErrIntf, index, raftErr := d.apply(structs.DeploymentPromoteRequestType, req)
   129  	return d.convertApplyErrors(fsmErrIntf, index, raftErr)
   130  }
   131  
   132  func (d *deploymentWatcherRaftShim) UpdateDeploymentAllocHealth(req *structs.ApplyDeploymentAllocHealthRequest) (uint64, error) {
   133  	fsmErrIntf, index, raftErr := d.apply(structs.DeploymentAllocHealthRequestType, req)
   134  	return d.convertApplyErrors(fsmErrIntf, index, raftErr)
   135  }