github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/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
    31  
    32    Creates an example quota specification file that can be used as a starting
    33    point to customize further.
    34  
    35  Init Options:
    36  
    37    -json
    38      Create an example JSON quota specification.
    39  `
    40  	return strings.TrimSpace(helpText)
    41  }
    42  
    43  func (c *QuotaInitCommand) Synopsis() string {
    44  	return "Create an example quota specification file"
    45  }
    46  
    47  func (c *QuotaInitCommand) AutocompleteFlags() complete.Flags {
    48  	return complete.Flags{
    49  		"-json": complete.PredictNothing,
    50  	}
    51  }
    52  
    53  func (c *QuotaInitCommand) AutocompleteArgs() complete.Predictor {
    54  	return complete.PredictNothing
    55  }
    56  
    57  func (c *QuotaInitCommand) Name() string { return "quota init" }
    58  
    59  func (c *QuotaInitCommand) Run(args []string) int {
    60  	var jsonOutput bool
    61  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    62  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    63  	flags.BoolVar(&jsonOutput, "json", false, "")
    64  
    65  	if err := flags.Parse(args); err != nil {
    66  		return 1
    67  	}
    68  
    69  	// Check that we get no arguments
    70  	args = flags.Args()
    71  	if l := len(args); l != 0 {
    72  		c.Ui.Error("This command takes no arguments")
    73  		c.Ui.Error(commandErrorText(c))
    74  		return 1
    75  	}
    76  
    77  	fileName := DefaultHclQuotaInitName
    78  	fileContent := defaultHclQuotaSpec
    79  	if jsonOutput {
    80  		fileName = DefaultJsonQuotaInitName
    81  		fileContent = defaultJsonQuotaSpec
    82  	}
    83  
    84  	// Check if the file already exists
    85  	_, err := os.Stat(fileName)
    86  	if err != nil && !os.IsNotExist(err) {
    87  		c.Ui.Error(fmt.Sprintf("Failed to stat %q: %v", fileName, err))
    88  		return 1
    89  	}
    90  	if !os.IsNotExist(err) {
    91  		c.Ui.Error(fmt.Sprintf("Quota specification %q already exists", fileName))
    92  		return 1
    93  	}
    94  
    95  	// Write out the example
    96  	err = ioutil.WriteFile(fileName, []byte(fileContent), 0660)
    97  	if err != nil {
    98  		c.Ui.Error(fmt.Sprintf("Failed to write %q: %v", fileName, err))
    99  		return 1
   100  	}
   101  
   102  	// Success
   103  	c.Ui.Output(fmt.Sprintf("Example quota specification written to %s", fileName))
   104  	return 0
   105  }
   106  
   107  var defaultHclQuotaSpec = strings.TrimSpace(`
   108  name = "default-quota"
   109  description = "Limit the shared default namespace"
   110  
   111  # Create a limit for the global region. Additional limits may
   112  # be specified in-order to limit other regions.
   113  limit {
   114      region = "global"
   115      region_limit {
   116          cpu = 2500
   117          memory = 1000
   118      }
   119  }
   120  `)
   121  
   122  var defaultJsonQuotaSpec = strings.TrimSpace(`
   123  {
   124  	"Name": "default-quota",
   125  	"Description": "Limit the shared default namespace",
   126  	"Limits": [
   127  		{
   128  			"Region": "global",
   129  			"RegionLimit": {
   130  				"CPU": 2500,
   131  				"MemoryMB": 1000
   132  			}
   133  		}
   134  	]
   135  }
   136  `)