generated from cybozu-go/neco-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile.instancedel
113 lines (99 loc) · 3.87 KB
/
Makefile.instancedel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
GCP_PROJECT = neco-test
REGION := asia-northeast1
SERVICE_ACCOUNT_NAME := gcp-instance-deleter
SERVICE_ACCOUNT_EMAIL := $(SERVICE_ACCOUNT_NAME)@$(GCP_PROJECT).iam.gserviceaccount.com
SERVICE_ACCOUNT_DISPNAME := "For function to extend/shutdown VM instance"
SHUTDOWN_SCHEDULER_NAME := shutdown
TOPIC_NAME := shutdown-events
init: \
enable-api \
create-service-account \
deploy-extend-function \
deploy-shutdown-function \
create-shutdown-scheduler \
clean: \
delete-service-account \
delete-extend-function \
delete-shutdown-function \
delete-shutdown-scheduler \
enable-api:
gcloud services enable --project $(GCP_PROJECT) iam.googleapis.com
gcloud services enable --project $(GCP_PROJECT) cloudfunctions.googleapis.com
gcloud services enable --project $(GCP_PROJECT) cloudscheduler.googleapis.com
gcloud services enable --project $(GCP_PROJECT) cloudbuild.googleapis.com
create-service-account:
gcloud iam service-accounts create $(SERVICE_ACCOUNT_NAME) \
--project $(GCP_PROJECT) \
--display-name $(SERVICE_ACCOUNT_DISPNAME)
gcloud projects add-iam-policy-binding $(GCP_PROJECT) \
--member=serviceAccount:$(SERVICE_ACCOUNT_EMAIL) \
--role=roles/compute.instanceAdmin.v1
gcloud projects add-iam-policy-binding $(GCP_PROJECT) \
--member=serviceAccount:$(SERVICE_ACCOUNT_EMAIL) \
--role=roles/iam.serviceAccountUser
gcloud projects add-iam-policy-binding $(GCP_PROJECT) \
--member=serviceAccount:$(SERVICE_ACCOUNT_EMAIL) \
--role=roles/logging.logWriter
delete-service-account:
gcloud --quiet projects remove-iam-policy-binding $(GCP_PROJECT) \
--member=serviceAccount:$(SERVICE_ACCOUNT_EMAIL) \
--role=roles/compute.instanceAdmin.v1
gcloud --quiet projects remove-iam-policy-binding $(GCP_PROJECT) \
--member=serviceAccount:$(SERVICE_ACCOUNT_EMAIL) \
--role=roles/iam.serviceAccountUser
gcloud --quiet projects remove-iam-policy-binding $(GCP_PROJECT) \
--member=serviceAccount:$(SERVICE_ACCOUNT_EMAIL) \
--role=roles/logging.logWriter
gcloud --quiet iam service-accounts delete $(SERVICE_ACCOUNT_EMAIL) --project $(GCP_PROJECT)
# The extend HTTP function is called from a Slack App. So allowing unauthenticated invocation.
# ref: https://cloud.google.com/functions/docs/securing/managing-access-iam#allowing_unauthenticated_http_function_invocation
deploy-extend-function:
gcloud --quiet functions deploy extend \
--no-gen2 \
--project $(GCP_PROJECT) \
--region $(REGION) \
--entry-point ExtendEntryPoint \
--runtime go121 \
--trigger-http \
--allow-unauthenticated \
--memory 128MiB \
--timeout 300s \
--service-account=$(SERVICE_ACCOUNT_EMAIL)
gcloud functions add-iam-policy-binding extend \
--project $(GCP_PROJECT) \
--region $(REGION) \
--member="allUsers" \
--role="roles/cloudfunctions.invoker"
deploy-shutdown-function:
gcloud --quiet functions deploy shutdown \
--no-gen2 \
--project $(GCP_PROJECT) \
--region $(REGION) \
--entry-point ShutdownEntryPoint \
--runtime go121 \
--trigger-topic $(TOPIC_NAME) \
--memory 128MiB \
--timeout 300s \
--service-account=$(SERVICE_ACCOUNT_EMAIL)
delete-extend-function:
gcloud --quiet functions delete extend --project $(GCP_PROJECT) --region $(REGION)
delete-shutdown-function:
gcloud --quiet functions delete shutdown --project $(GCP_PROJECT) --region $(REGION)
create-shutdown-scheduler:
gcloud scheduler jobs create pubsub $(SHUTDOWN_SCHEDULER_NAME) \
--project $(GCP_PROJECT) \
--schedule '*/5 * * * *' \
--topic $(TOPIC_NAME) \
--message-body '{}' \
--time-zone 'Asia/Tokyo' \
--description 'shutdown vm instances'
delete-shutdown-scheduler:
gcloud --quiet scheduler jobs delete $(SHUTDOWN_SCHEDULER_NAME) --project $(GCP_PROJECT)
.PHONY: \
init \
clean \
enable-api \
create-service-account delete-service-account \
deploy-extend-function delete-extend-function \
deploy-shutdown-function delete-shutdown-function \
create-shutdown-scheduler delete-shutdown-scheduler