generated from cybozu-go/neco-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile.slack
93 lines (77 loc) · 3.35 KB
/
Makefile.slack
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
GCLOUD := gcloud --quiet
GCP_PROJECT ?=
REGION := asia-northeast1
LOG_LEVEL ?= info
SERVICE_ACCOUNT_NAME := slack-notifier
SERVICE_ACCOUNT_EMAIL := $(SERVICE_ACCOUNT_NAME)@$(GCP_PROJECT).iam.gserviceaccount.com
TOPIC_NAME := slack-notifier-events
FUNCTION_NAME := slack-notifier
SINK_NAME := auto-dctest-alerts
SECRET_NAME := slack-notifier-config
setup:
sudo apt-get update
sudo apt-get install -y --no-install-recommends jq
init: \
enable-api \
setup \
create-service-account \
deploy-function \
create-logging-sink
clean: \
delete-service-account \
delete-function \
delete-logging-sink
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) secretmanager.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_NAME)
gcloud projects add-iam-policy-binding $(GCP_PROJECT) \
--member=serviceAccount:$(SERVICE_ACCOUNT_EMAIL) \
--role=roles/secretmanager.secretAccessor
delete-service-account:
gcloud --quiet projects remove-iam-policy-binding $(GCP_PROJECT) \
--member=serviceAccount:$(SERVICE_ACCOUNT_EMAIL) \
--role=roles/secretmanager.secretAccessor
gcloud --quiet iam service-accounts delete $(SERVICE_ACCOUNT_EMAIL) --project $(GCP_PROJECT)
deploy-function:
gcloud --quiet functions deploy $(FUNCTION_NAME) \
--no-gen2 \
--project $(GCP_PROJECT) \
--region $(REGION) \
--entry-point SlackNotifierEntryPoint \
--runtime go121 \
--trigger-topic $(TOPIC_NAME) \
--set-env-vars GCP_PROJECT=$(GCP_PROJECT),CYBOZU_LOG_LEVEL=$(LOG_LEVEL) \
--memory 128MiB \
--service-account=$(SERVICE_ACCOUNT_EMAIL)
delete-function:
gcloud --quiet functions delete $(FUNCTION_NAME) --project $(GCP_PROJECT) --region $(REGION)
gcloud --quiet pubsub topics delete $(TOPIC_NAME) --project $(GCP_PROJECT)
create-logging-sink:
gcloud logging sinks create $(SINK_NAME) \
pubsub.googleapis.com/projects/$(GCP_PROJECT)/topics/$(TOPIC_NAME) \
--project $(GCP_PROJECT) \
--log-filter 'resource.type=gce_instance AND (jsonPayload.MESSAGE:[auto-dctest] OR ((protoPayload.methodName=v1.compute.instances.insert OR protoPayload.methodName=v1.compute.instances.delete) AND operation.last=true))'
IDENTITY_NAME=$$(gcloud logging sinks describe $(SINK_NAME) --project $(GCP_PROJECT) --format json | jq -r .writerIdentity | cut -d: -f 2) && \
gcloud projects add-iam-policy-binding $(GCP_PROJECT) --member=serviceAccount:$${IDENTITY_NAME} --role=roles/pubsub.publisher
delete-logging-sink:
gcloud --quiet logging sinks delete $(SINK_NAME) --project $(GCP_PROJECT)
create-slack-notifier-config: slack-notifier-config.yaml
gcloud secrets create ${SECRET_NAME} --data-file=$< --project $(GCP_PROJECT)
update-slack-notifier-config: slack-notifier-config.yaml
gcloud secrets versions add ${SECRET_NAME} --data-file=$< --project $(GCP_PROJECT)
.PHONY: \
setup \
init \
clean \
enable-api \
create-service-account delete-service-account \
deploy-function delete-function \
create-logging-sink delete-logging-sink \
create-slack-notifier-config update-slack-notifier-config