github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/quota_init.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/posener/complete"
    10  )
    11  
    12  const (
    13  	// DefaultHclQuotaInitName is the default name we use when initializing the
    14  	// example quota file in HCL format
    15  	DefaultHclQuotaInitName = "spec.hcl"
    16  
    17  	// DefaultHclQuotaInitName is the default name we use when initializing the
    18  	// example quota file in JSON format
    19  	DefaultJsonQuotaInitName = "spec.json"
    20  )
    21  
    22  // QuotaInitCommand generates a new quota spec that you can customize to your
    23  // liking, like vagrant init
    24  type QuotaInitCommand struct {
    25  	Meta
    26  }
    27  
    28  func (c *QuotaInitCommand) Help() string {
    29  	helpText := `
    30  Usage: nomad quota init <filename>
    31  
    32    Creates an example quota specification file that can be used as a starting
    33    point to customize further. If no filename is given, the default of "spec.hcl"
    34    or "spec.json" will be used.
    35  
    36  Init Options:
    37  
    38    -json
    39      Create an example JSON quota specification.
    40  `
    41  	return strings.TrimSpace(helpText)
    42  }
    43  
    44  func (c *QuotaInitCommand) Synopsis() string {
    45  	return "Create an example quota specification file"
    46  }
    47  
    48  func (c *QuotaInitCommand) AutocompleteFlags() complete.Flags {
    49  	return complete.Flags{
    50  		"-json": complete.PredictNothing,
    51  	}
    52  }
    53  
    54  func (c *QuotaInitCommand) AutocompleteArgs() complete.Predictor {
    55  	return complete.PredictNothing
    56  }
    57  
    58  func (c *QuotaInitCommand) Name() string { return "quota init" }
    59  
    60  func (c *QuotaInitCommand) Run(args []string) int {
    61  	var jsonOutput bool
    62  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    63  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    64  	flags.BoolVar(&jsonOutput, "json", false, "")
    65  
    66  	if err := flags.Parse(args); err != nil {
    67  		return 1
    68  	}
    69  
    70  	// Check that we get no arguments
    71  	args = flags.Args()
    72  	if l := len(args); l > 1 {
    73  		c.Ui.Error("This command takes no arguments or one: <filename>")
    74  		c.Ui.Error(commandErrorText(c))
    75  		return 1
    76  	}
    77  
    78  	fileName := DefaultHclQuotaInitName
    79  	fileContent := defaultHclQuotaSpec
    80  	if jsonOutput {
    81  		fileName = DefaultJsonQuotaInitName
    82  		fileContent = defaultJsonQuotaSpec
    83  	}
    84  	if len(args) == 1 {
    85  		fileName = args[0]
    86  	}
    87  
    88  	// Check if the file already exists
    89  	_, err := os.Stat(fileName)
    90  	if err != nil && !os.IsNotExist(err) {
    91  		c.Ui.Error(fmt.Sprintf("Failed to stat %q: %v", fileName, err))
    92  		return 1
    93  	}
    94  	if !os.IsNotExist(err) {
    95  		c.Ui.Error(fmt.Sprintf("Quota specification %q already exists", fileName))
    96  		return 1
    97  	}
    98  
    99  	// Write out the example
   100  	err = ioutil.WriteFile(fileName, []byte(fileContent), 0660)
   101  	if err != nil {
   102  		c.Ui.Error(fmt.Sprintf("Failed to write %q: %v", fileName, err))
   103  		return 1
   104  	}
   105  
   106  	// Success
   107  	c.Ui.Output(fmt.Sprintf("Example quota specification written to %s", fileName))
   108  	return 0
   109  }
   110  
   111  var defaultHclQuotaSpec = strings.TrimSpace(`
   112  name        = "default-quota"
   113  description = "Limit the shared default namespace"
   114  
   115  # Create a limit for the global region. Additional limits may
   116  # be specified in-order to limit other regions.
   117  limit {
   118    region = "global"
   119    region_limit {
   120      cpu        = 2500
   121      memory     = 1000
   122      memory_max = 1000
   123    }
   124    variables_limit = 1000
   125  }
   126  `)
   127  
   128  var defaultJsonQuotaSpec = strings.TrimSpace(`
   129  {
   130  	"Name": "default-quota",
   131  	"Description": "Limit the shared default namespace",
   132  	"Limits": [
   133  		{
   134  			"Region": "global",
   135  			"RegionLimit": {
   136  				"CPU": 2500,
   137  				"MemoryMB": 1000,
   138  				"MemoryMaxMB": 1000
   139  			},
   140  			"VariablesLimit": 1000
   141  		}
   142  	]
   143  }
   144  `)