{"id":27969170,"url":"https://github.com/future-architect/gcp-instance-scheduler","last_synced_at":"2025-05-07T21:08:47.541Z","repository":{"id":57537019,"uuid":"193000885","full_name":"future-architect/gcp-instance-scheduler","owner":"future-architect","description":"Tools that shutdown GCP Instance on your schedule.","archived":false,"fork":false,"pushed_at":"2023-02-24T19:03:33.000Z","size":144,"stargazers_count":31,"open_issues_count":10,"forks_count":14,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-05-07T21:08:42.551Z","etag":null,"topics":["gcp","gcp-cloud-functions","go","golang"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/future-architect.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-20T23:51:08.000Z","updated_at":"2023-04-02T23:55:26.000Z","dependencies_parsed_at":"2024-06-20T12:57:22.268Z","dependency_job_id":"6828ad0a-ce46-40d3-ad5a-fe26f35675c9","html_url":"https://github.com/future-architect/gcp-instance-scheduler","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/future-architect%2Fgcp-instance-scheduler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/future-architect%2Fgcp-instance-scheduler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/future-architect%2Fgcp-instance-scheduler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/future-architect%2Fgcp-instance-scheduler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/future-architect","download_url":"https://codeload.github.com/future-architect/gcp-instance-scheduler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252954409,"owners_count":21830905,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gcp","gcp-cloud-functions","go","golang"],"created_at":"2025-05-07T21:08:46.907Z","updated_at":"2025-05-07T21:08:47.529Z","avatar_url":"https://github.com/future-architect.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gcp-instance-scheduler\n[![Apache 2.0 License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)\n[![Go Report Card](https://goreportcard.com/badge/github.com/future-architect/gcp-instance-scheduler)](https://goreportcard.com/report/github.com/future-architect/gcp-instance-scheduler)\n\nTools that shutdown GCP Instance on your schedule.\n\n## Abstract\n\n* Shutdown target\n   * GCE, GKE, SQL\n   * The label `state-scheduler: true` is required to stop / restart the instance.\n   * In order to be processed, it is necessary to assign a label to Instance, InstanceGroup or Cluster.\n   If a label is assigned to Cluster or InstanceGroup, this tool will reduce the size of InstanceGroup to 0.   \n* Architecture\n  * Cloud Scheduler --\u003e Pub/Sub --\u003e CloudFunction\n    * https://cloud.google.com/scheduler/docs/start-and-stop-compute-engine-instances-on-a-schedule\n\n## Config\n\n* nothing special\n  * [GCP_PROJECT is automation set by CloudFunction](https://cloud.google.com/functions/docs/concepts/go-runtime#contextcontext)\n\n## QuickStart\n\nShutdown instances with CLI command.\n\n### Install\n\n```bash\ngo get -u github.com/future-architect/gcp-instance-scheduler/cmd/scheduler\n```\n\n### Usage\n\nNeed to set `GOOGLE_APPLICATION_CREDENTIALS` in environment variables before cli execution.\n[See setup](https://cloud.google.com/docs/authentication/getting-started)\n\nAnd then you set label to gcp resource\n```bash\ngcloud compute instances create \u003cinsntance-name\u003e --zone us-central1-a\ngcloud compute instances update \u003cinsntance-name\u003e --project \u003cproject-id\u003e --update-labels state-scheduler=true\n```\nThen you can do below commands.\n\n```bash\n# stop\n$ scheduler stop --project \u003cyour gcp project\u003e\n\n# restart\n$ scheduler restart --project \u003cyour gcp project\u003e\n```\n\n\n#### Options\n\nYou can designate project id and timeout length by using flags.\nIf you use slack notification, you have to enable slack notification by adding the flag `--slackNotifyEnable`.\n\n```console\n\u003escheduler stop --help\nstop is execution command that shutdown gcp resources that assigned target label.\n\nUsage:\n  scheduler stop [flags]\n\nFlags:\n  -h, --help                  help for stop\n  -p, --project string        project id (default $GCP_PROJECT)\n  -c, --slackChannel string   Slack Channel name (should enable slack notify) (default SLACK_CHANNEL)\n  -s, --slackNotifyEnable     Enable slack notification\n  -t, --slackToken string     SlackAPI token (should enable slack notify) (default $SLACK_API_TOKEN)\n      --timeout int           set timeout seconds (default 60)\n\n\n\u003escheduler restart --help\nrestart is launch shutdown gcp resource.\n\nUsage:\n  scheduler restart [flags]\n\nFlags:\n  -h, --help                  help for restart\n  -p, --project string        project id (default $GCP_PROJECT)\n  -c, --slackChannel string   Slack Channel name (should enable slack notify) (default SLACK_CHANNEL)\n  -s, --slackNotifyEnable     Enable slack notification\n  -t, --slackToken string     SlackAPI token (should enable slack notify) (default $SLACK_API_TOKEN)\n      --timeout int           set timeout seconds (default 60)\n``` \n\nFollowing variables are used when you did not designate these flags.\n\n|#  |flags                  |variables       |\n|---|-----------------------|----------------|\n| 1 |project(p)             |GCP_PROJECT     |\n| 2 |slackToken             |SLACK_API_TOKEN |\n| 3 |slackChannel           |SLACK_CHANNEL   |\n\n\n## Example: create target resources\n\nSet label for target instance\n\n```sh\n# GCE\ngcloud compute instances update \u003cinsntance-name\u003e \\\n  --project \u003cproject-id\u003e \\\n  --update-labels state-scheduler=true\n\n# Instance Group\ngcloud compute instance-templates create \u003ctmeplate-name\u003e ... \\\n  --project \u003cproject-id\u003e \\\n  --labels state-scheduler=true\n\n# Cloud SQL (master must be running)\ngcloud beta sql instances patch \u003cinsntance-name\u003e \\\n  --project \u003cproject-id\u003e \\\n  --update-labels state-scheduler=true\n\n# GKE\ngcloud container clusters update \u003ccluster-name\u003e \\\n  --project \u003cproject-id\u003e \\\n  --zone \u003ccluster-master-node-zone\u003e \\\n  --update-labels state-scheduler=true,restore-size-\u003cnode-pool-name\u003e=\u003cnode-size\u003e\n```\n\n\n## Deploy to GCP CloudFunction\n\n* install [gcloud](https://cloud.google.com/sdk/gcloud/)\n\n### Required variables\nWhen you want to get slack notification, please set these environment variables.\nYou can get slack notification if and only if these three variables are set.\n\n|#  |variables       |Note                               |\n|---|----------------|-----------------------------------|\n| 1 |SLACK_ENABLE    |Slack notification enable (\"true\") |\n| 2 |SLACK_API_TOKEN |Slack api token                    |\n| 3 |SLACK_CHANNEL   |Slack channel name                 |\n\n### Steps\n\nAs an example, start an instance between 9 and 22:00 on weekdays.\n\n```sh\n# Deploy Cloud Function: slack notification enable\ngcloud functions deploy switchInstanceState --project \u003cproject-id\u003e \\\n  --entry-point SwitchInstanceState --runtime go111 \\\n  --trigger-topic instance-scheduler-event \\\n  --set-env-vars SLACK_ENABLE=false\n\n# Create Cloud Scheduler Job(Stop)\ngcloud beta scheduler jobs create pubsub shutdown-workday \\\n  --project \u003cproject-id\u003e \\\n  --schedule '0 22 * * 1-5' \\\n  --topic instance-scheduler-event \\\n  --message-body '{\"command\":\"stop\"}' \\\n  --time-zone 'Asia/Tokyo' \\\n  --description 'automatically stop instances'\n\n# Create Cloud Scheduler Job(Start)\ngcloud beta scheduler jobs create pubsub restart-workday \\\n  --project \u003cproject-id\u003e \\\n  --schedule '0 9 * * 1-5' \\\n  --topic instance-scheduler-event \\\n  --message-body '{\"command\":\"start\"}' \\\n  --time-zone 'Asia/Tokyo' \\\n  --description 'automatically restart instances'\n```\n\n\n## Tips: Debug Function\n\n* publish message to pub/sub\n  * `gcloud pubsub topics publish stop-instance-event --project \u003cproject-id\u003e --message \"{\"command\":\"stop\"}\"`\n* confirm Functions log\n  * `gcloud functions logs read --project \u003cproject-id\u003e --limit 50`\n* manual launch for job of scheduler\n  * `gcloud beta scheduler jobs run shutdown-workday-instance`\n\n## License\n\nThis project is licensed under the Apache License 2.0 License - see the [LICENSE](LICENSE) file for details\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffuture-architect%2Fgcp-instance-scheduler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffuture-architect%2Fgcp-instance-scheduler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffuture-architect%2Fgcp-instance-scheduler/lists"}