cmd

package
v0.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 15, 2017 License: MIT Imports: 11 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   "gce-sleep",
	Short: "gce-sleep is a tool for shutting down/starting up Google Cloud Engine instances based on tags for savings costs when not in use.",
	Run: func(cmd *cobra.Command, args []string) {
		logPrintlnVerbose("Starting gce-sleep")

		now := time.Now()

		logPrintlnVerbose("Basing time on:", now)

		configContent, err := ioutil.ReadFile(configLocation)
		if err != nil {
			log.Fatal(err)
		}

		var config projectConfig
		err = hcl.Unmarshal(configContent, &config)
		if err != nil {
			log.Fatal(err)
		}

		activeRules := make(map[string]configRuleset)
		for index, rawRuleset := range config.Ruleset {
			ruleset, err := newRuleset(rawRuleset)
			if err != nil {
				log.Fatal(err)
			} else {
				activeRules[index] = ruleset
			}
		}

		logPrintlnVerbose("Active rulesets:", activeRules)

		ctx := context.Background()

		client, err := google.DefaultClient(ctx, compute.CloudPlatformScope)
		if err != nil {
			log.Fatal(err)
		}

		computeService, err := compute.New(client)
		if err != nil {
			log.Fatal(err)
		}

		filter := fmt.Sprintf("labels.%s eq on", labelName)
		logPrintlnVerbose(fmt.Sprintf("Filter defined as: %q", filter))

		for projectName, project := range config.Project {
			logPrintlnVerbose(fmt.Sprintf("Checking project %q", projectName))

			for _, zoneName := range project.Zones {
				logPrintlnVerbose(fmt.Sprintf("Checking zone %q", zoneName))

				instancesReq := computeService.Instances.List(projectName, zoneName).Filter(filter)
				if err := instancesReq.Pages(ctx, func(page *compute.InstanceList) error {
					for _, instance := range page.Items {
						logPrintlnVerbose(fmt.Sprintf("Checking instance %q", instance.Name))
						for _, metadata := range instance.Metadata.Items {
							if metadata.Key == "gce-sleep-group" {
								logPrintlnVerbose(fmt.Sprintf("Instance %q qualifies", instance.Name))

								actionableInstances := activeRules[*metadata.Value]
								actionableInstances.Instances = append(actionableInstances.Instances, gceInstance{
									Project: projectName,
									Zone:    zoneName,
									Name:    instance.Name,
									Status:  instance.Status,
								})
								activeRules[*metadata.Value] = actionableInstances
							}
						}
					}

					return nil
				}); err != nil {
					log.Fatal(err)
				}
			}
		}

		for rulesetName, ruleset := range activeRules {
			nowTimezone := now.In(ruleset.Timezone)
			shouldBeRunning := shouldBeRunning(nowTimezone, ruleset.StartTime, ruleset.StopTime)

			logPrintlnVerbose(fmt.Sprintf("Total %d instances to be evaluated in ruleset %q", len(ruleset.Instances), rulesetName))

			for _, instance := range ruleset.Instances {
				logPrintlnVerbose(fmt.Sprintf("Evaluating instance %q", instance.Name))

				if shouldBeRunning && instance.Status == "TERMINATED" {
					logPrintlnVerbose(fmt.Sprintf("Instance %q currently stopped, starting", instance.Name))

					call := computeService.Instances.Start(instance.Project, instance.Zone, instance.Name)
					_, err := call.Do()
					if err != nil {
						log.Fatal(err)
					} else {
						log.Println(fmt.Sprintf("Instance %q starting", instance.Name))
					}
				} else if !shouldBeRunning && instance.Status == "RUNNING" {
					logPrintlnVerbose(fmt.Sprintf("Instance %q currently starting, stopped", instance.Name))

					call := computeService.Instances.Stop(instance.Project, instance.Zone, instance.Name)
					_, err := call.Do()
					if err != nil {
						log.Fatal(err)
					} else {
						log.Println(fmt.Sprintf("Instance %q stopping", instance.Name))
					}
				} else {
					logPrintlnVerbose(fmt.Sprintf("Instance %q does not meet criteria (%s <=, %s >=)", instance.Name, ruleset.rawRuleset.StartTime, ruleset.rawRuleset.StopTime))
				}
			}
		}

		logPrintlnVerbose("Stopping gce-sleep")
	},
}

RootCmd is gce-sleep's root command. Every other command attached to RootCmd is a child command to it.

Functions

func Setup

func Setup(v, c, d string)

Setup passes through ldflags from goreleaser, adds child commands, and sets up flag configuration

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL