gcpcomputetimer

package module
v0.0.0-...-de93982 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2021 License: MIT Imports: 17 Imported by: 0

README

forthebadge forthebadge forthebadge forthebadge

gcp-compute-timer

Monitor the uptime of gcp computer instances and act accordingly.

Case

At the moment I have a number of images running in GCP. However my budget is limited and I sometimes forget to turn of unused images (scatterbrain). On the otherhand there are images that run for extended amounts of time (hosting a site or something).

So what I need is a way to:

  • get state and uptime from images
  • indicate the maximum age of an image
  • react to the image exceeding its limit

Google has an excelent API but I don't want to schlep around the SDK all the time therefor I'm writing this in golang (although I am barely past the "hello world" phase).

Configuration

Create a configuration file called ~/.config/gcp-compute-timer.yml with the following content:

gcp:
  project: project_name
  zone: europe-west4-a
  bucket: bucket_name

In the mentioned bucket create a file gcp-compute-timer.txt with the content like this:

tstproj/europe-west4-a/ws_rhel7_01 workday None
project_name/zone/centos-7-ws 7200 stop
project_name/zone/centos-7-ws 7200 shell mailx ......

The column are as follows:

  • instance path (project_name/zone/instancename)
  • max age (seconds, "workday", "day" or "week")
  • action (default: None, FIXME: add something useful)

GCP

API instructions

BEFORE RUNNING:
  1. If not already done, enable the Compute Engine API and check the quota for your project at https://console.developers.google.com/apis/api/compute
  2. This sample uses Application Default Credentials for authentication. If not already done, install the gcloud CLI from https://cloud.google.com/sdk/ and run gcloud beta auth application-default login. For more information, see https://developers.google.com/identity/protocols/application-default-credentials
  3. Install and update the Go dependencies by running go get -u in the project directory.

Documentation

Overview

Monitor the uptime of gcp computer instances and act accordingly.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrintCheck

func PrintCheck(gcp_project string, gcp_zone string, gcp_bucket string, printfull bool)

func RunCheck

func RunCheck(gcp_project string, gcp_zone string, gcp_bucket string, noexec bool)

func SecondsToHuman

func SecondsToHuman(seconds int64) string

SecondsToHuman translate seconds into a human readable string with a specific length.

fmt.Printf("time: %s\n", SecondsToHuman(500))

Types

type Instance

type Instance struct {
	Name               string
	Project            string
	Zone               string
	Status             string
	LastStartTimestamp string
	TooOld             bool
	Age                int64
	Action             string
	MaxAge             int
}

Instance struct to contain the relevant element of an instance

func (Instance) Parse

func (in Instance) Parse(templatestring string) string

Parse string and translate template variables

func (*Instance) StartTime

func (in *Instance) StartTime() int64

StartTime converts the LastStartTimestamp string to an epoch int64

func (Instance) Stop

func (in Instance) Stop() bool

type InstanceConfig

type InstanceConfig struct {
	Project string // Name of the GCP Project
	Zone    string // Name of the GCP Zone
	Name    string // Name of the GCP Instance
	MaxAge  int    // Maximum amount of seconds the machine can be up
	Action  string // Action to be taken when time exceeded
}

InstanceConfig configuration of a single instance.

func NewInstanceConfig

func NewInstanceConfig(instr string) (*InstanceConfig, error)

NewInstanceConfig initialize a new InstanceConfig

func (InstanceConfig) Description

func (ic InstanceConfig) Description() string

Description return the description of an instance

type InstanceConfigs

type InstanceConfigs struct {
	Bucketname      string
	InstanceConfigs []InstanceConfig
}

InstanceConfigs FIXME putin somthing useful

func NewInstanceConfigs

func NewInstanceConfigs(bucketname string) *InstanceConfigs

NewInstanceConfigs initialize the InstanceConfigs object

type Instances

type Instances struct {
	Project   string
	Zone      string
	Now       int64
	Instances []Instance
}

Instances struct to contain all the instances

func NewInstances

func NewInstances(project, zone string) *Instances

NewInstances initializes the instances object

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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