github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/e2e/e2eutil/job.go (about)

     1  package e2eutil
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"io/ioutil"
     7  	"os/exec"
     8  	"regexp"
     9  	"strings"
    10  )
    11  
    12  // Register registers a jobspec from a file but with a unique ID.
    13  // The caller is responsible for recording that ID for later cleanup.
    14  func Register(jobID, jobFilePath string) error {
    15  	cmd := exec.Command("nomad", "job", "run", "-")
    16  	stdin, err := cmd.StdinPipe()
    17  	if err != nil {
    18  		return fmt.Errorf("could not open stdin?: %w", err)
    19  	}
    20  
    21  	content, err := ioutil.ReadFile(jobFilePath)
    22  	if err != nil {
    23  		return fmt.Errorf("could not open job file: %w", err)
    24  	}
    25  
    26  	// hack off the first line to replace with our unique ID
    27  	var re = regexp.MustCompile(`(?m)^job ".*" \{`)
    28  	jobspec := re.ReplaceAllString(string(content),
    29  		fmt.Sprintf("job \"%s\" {", jobID))
    30  
    31  	go func() {
    32  		defer stdin.Close()
    33  		io.WriteString(stdin, jobspec)
    34  	}()
    35  
    36  	out, err := cmd.CombinedOutput()
    37  	if err != nil {
    38  		return fmt.Errorf("could not register job: %w\n%v", err, string(out))
    39  	}
    40  	return nil
    41  }
    42  
    43  // PeriodicForce forces a periodic job to dispatch, returning the child job ID
    44  // or an error
    45  func PeriodicForce(jobID string) error {
    46  	// nomad job periodic force
    47  	cmd := exec.Command("nomad", "job", "periodic", "force", jobID)
    48  
    49  	out, err := cmd.CombinedOutput()
    50  	if err != nil {
    51  		return fmt.Errorf("could not register job: %w\n%v", err, string(out))
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  // JobInspectTemplate runs nomad job inspect and formats the output
    58  // using the specified go template
    59  func JobInspectTemplate(jobID, template string) (string, error) {
    60  	cmd := exec.Command("nomad", "job", "inspect", "-t", template, jobID)
    61  	out, err := cmd.CombinedOutput()
    62  	if err != nil {
    63  		return "", fmt.Errorf("could not inspect job: %w\n%v", err, string(out))
    64  	}
    65  	outStr := string(out)
    66  	outStr = strings.TrimSuffix(outStr, "\n")
    67  	return outStr, nil
    68  }
    69  
    70  // Register registers a jobspec from a string, also with a unique ID.
    71  // The caller is responsible for recording that ID for later cleanup.
    72  func RegisterFromJobspec(jobID, jobspec string) error {
    73  
    74  	cmd := exec.Command("nomad", "job", "run", "-")
    75  	stdin, err := cmd.StdinPipe()
    76  	if err != nil {
    77  		return fmt.Errorf("could not open stdin?: %w", err)
    78  	}
    79  
    80  	// hack off the first line to replace with our unique ID
    81  	var re = regexp.MustCompile(`^job "\w+" \{`)
    82  	jobspec = re.ReplaceAllString(jobspec,
    83  		fmt.Sprintf("job \"%s\" {", jobID))
    84  
    85  	go func() {
    86  		defer stdin.Close()
    87  		io.WriteString(stdin, jobspec)
    88  	}()
    89  
    90  	out, err := cmd.CombinedOutput()
    91  	if err != nil {
    92  		return fmt.Errorf("could not register job: %w\n%v", err, string(out))
    93  	}
    94  	return nil
    95  }
    96  
    97  func ChildrenJobSummary(jobID string) ([]map[string]string, error) {
    98  	out, err := Command("nomad", "job", "status", jobID)
    99  	if err != nil {
   100  		return nil, fmt.Errorf("nomad job status failed: %w", err)
   101  	}
   102  
   103  	section, err := GetSection(out, "Children Job Summary")
   104  	if err != nil {
   105  		return nil, fmt.Errorf("could not find children job summary section: %w", err)
   106  	}
   107  
   108  	summary, err := ParseColumns(section)
   109  	if err != nil {
   110  		return nil, fmt.Errorf("could not parse children job summary section: %w", err)
   111  	}
   112  
   113  	return summary, nil
   114  }
   115  
   116  func PreviouslyLaunched(jobID string) ([]map[string]string, error) {
   117  	out, err := Command("nomad", "job", "status", jobID)
   118  	if err != nil {
   119  		return nil, fmt.Errorf("nomad job status failed: %w", err)
   120  	}
   121  
   122  	section, err := GetSection(out, "Previously Launched Jobs")
   123  	if err != nil {
   124  		return nil, fmt.Errorf("could not find previously launched jobs section: %w", err)
   125  	}
   126  
   127  	summary, err := ParseColumns(section)
   128  	if err != nil {
   129  		return nil, fmt.Errorf("could not parse previously launched jobs section: %w", err)
   130  	}
   131  
   132  	return summary, nil
   133  }