function

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

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

Go to latest
Published: Mar 3, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

README

gcp-instance-scheduler

Apache 2.0 License Go Report Card

Tools that shutdown GCP Instance on your schedule.

Abstract

  • Shutdown target
    • GCE, GKE, SQL
    • The label state-scheduler: true is required to stop / restart the instance.
    • In order to be processed, it is necessary to assign a label to Instance, InstanceGroup or Cluster. If a label is assigned to Cluster or InstanceGroup, this tool will reduce the size of InstanceGroup to 0.
  • Architecture

Config

QuickStart

Shutdown instances with CLI command.

Install
go get -u github.com/future-architect/gcp-instance-scheduler/cmd/scheduler
Usage

Need to set GOOGLE_APPLICATION_CREDENTIALS in environment variables before cli execution. See setup

And then you set label to gcp resource

gcloud compute instances create <insntance-name> --zone us-central1-a
gcloud compute instances update <insntance-name> --project <project-id> --update-labels state-scheduler=true

Then you can do below commands.

# stop
$ scheduler stop --project <your gcp project>

# restart
$ scheduler restart --project <your gcp project>
Options

You can designate project id and timeout length by using flags. If you use slack notification, you have to enable slack notification by adding the flag --slackNotifyEnable.

>scheduler stop --help
stop is execution command that shutdown gcp resources that assigned target label.

Usage:
  scheduler stop [flags]

Flags:
  -h, --help                  help for stop
  -p, --project string        project id (default $GCP_PROJECT)
  -c, --slackChannel string   Slack Channel name (should enable slack notify) (default SLACK_CHANNEL)
  -s, --slackNotifyEnable     Enable slack notification
  -t, --slackToken string     SlackAPI token (should enable slack notify) (default $SLACK_API_TOKEN)
      --timeout int           set timeout seconds (default 60)


>scheduler restart --help
restart is launch shutdown gcp resource.

Usage:
  scheduler restart [flags]

Flags:
  -h, --help                  help for restart
  -p, --project string        project id (default $GCP_PROJECT)
  -c, --slackChannel string   Slack Channel name (should enable slack notify) (default SLACK_CHANNEL)
  -s, --slackNotifyEnable     Enable slack notification
  -t, --slackToken string     SlackAPI token (should enable slack notify) (default $SLACK_API_TOKEN)
      --timeout int           set timeout seconds (default 60)

Following variables are used when you did not designate these flags.

# flags variables
1 project(p) GCP_PROJECT
2 slackToken SLACK_API_TOKEN
3 slackChannel SLACK_CHANNEL

Example: create target resources

Set label for target instance

# GCE
gcloud compute instances update <insntance-name> \
  --project <project-id> \
  --update-labels state-scheduler=true

# Instance Group
gcloud compute instance-templates create <tmeplate-name> ... \
  --project <project-id> \
  --labels state-scheduler=true

# Cloud SQL (master must be running)
gcloud beta sql instances patch <insntance-name> \
  --project <project-id> \
  --update-labels state-scheduler=true

# GKE
gcloud container clusters update <cluster-name> \
  --project <project-id> \
  --zone <cluster-master-node-zone> \
  --update-labels state-scheduler=true,restore-size-<node-pool-name>=<node-size>

Deploy to GCP CloudFunction

Required variables

When you want to get slack notification, please set these environment variables. You can get slack notification if and only if these three variables are set.

# variables Note
1 SLACK_ENABLE Slack notification enable ("true")
2 SLACK_API_TOKEN Slack api token
3 SLACK_CHANNEL Slack channel name
Steps

As an example, start an instance between 9 and 22:00 on weekdays.

# Deploy Cloud Function: slack notification enable
gcloud functions deploy switchInstanceState --project <project-id> \
  --entry-point SwitchInstanceState --runtime go111 \
  --trigger-topic instance-scheduler-event \
  --set-env-vars SLACK_ENABLE=false

# Create Cloud Scheduler Job(Stop)
gcloud beta scheduler jobs create pubsub shutdown-workday \
  --project <project-id> \
  --schedule '0 22 * * 1-5' \
  --topic instance-scheduler-event \
  --message-body '{"command":"stop"}' \
  --time-zone 'Asia/Tokyo' \
  --description 'automatically stop instances'

# Create Cloud Scheduler Job(Start)
gcloud beta scheduler jobs create pubsub restart-workday \
  --project <project-id> \
  --schedule '0 9 * * 1-5' \
  --topic instance-scheduler-event \
  --message-body '{"command":"start"}' \
  --time-zone 'Asia/Tokyo' \
  --description 'automatically restart instances'

Tips: Debug Function

  • publish message to pub/sub
    • gcloud pubsub topics publish stop-instance-event --project <project-id> --message "{"command":"stop"}"
  • confirm Functions log
    • gcloud functions logs read --project <project-id> --limit 50
  • manual launch for job of scheduler
    • gcloud beta scheduler jobs run shutdown-workday-instance

License

This project is licensed under the Apache License 2.0 License - see the LICENSE file for details

Documentation

Overview

*

  • Copyright (c) 2019-present Future Corporation *
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at *
  • http://www.apache.org/licenses/LICENSE-2.0 *
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SwitchInstanceState

func SwitchInstanceState(ctx context.Context, msg *pubsub.Message) error

Types

type Env

type Env struct {
	ProjectID    string `envconfig:"GCP_PROJECT" required:"true"`
	SlackNotify  bool   `envconfig:"SLACK_ENABLE" required:"true"`
	SlackToken   string `envconfig:"SLACK_API_TOKEN"`
	SlackChannel string `envconfig:"SLACK_CHANNEL"`
}

Env is cloud function environment variables

type Payload

type Payload struct {
	Command string `json:"command"`
}

Directories

Path Synopsis
cmd
scheduler
* * Copyright (c) 2019-present Future Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* * Copyright (c) 2019-present Future Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
scheduler/cmd
* * Copyright (c) 2019-present Future Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* * Copyright (c) 2019-present Future Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* * Copyright (c) 2019-present Future Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* * Copyright (c) 2019-present Future Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* * Copyright (c) 2019-present Future Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* * Copyright (c) 2019-present Future Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* * Copyright (c) 2019-present Future Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.
* * Copyright (c) 2019-present Future Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.

Jump to

Keyboard shortcuts

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