├── db ├── .data │ └── .gitkeep ├── my.cnf └── init │ └── V2__crate_table_event_log.sql ├── provisioning ├── ami │ ├── packer_config │ │ └── .gitkeep │ ├── .gitignore │ ├── scripts │ │ ├── cloud.cfg.d │ │ │ └── 99_defaults.cfg │ │ └── hakaru │ │ │ └── Makefile │ ├── Dockerfile │ ├── _config.pkr.hcl │ ├── variables.pkr.hcl │ ├── Makefile │ └── hakaru.pkr.hcl └── instance │ ├── systemd │ └── hakaru.service │ ├── amazon-cloudwatch-agent │ └── amazon-cloudwatch-agent.json │ └── Makefile ├── renovate.json ├── user_data.sh ├── .gitignore ├── go.mod ├── go.sum ├── README.md ├── LICENSE ├── .github └── disable-workflows │ └── upload.yaml ├── main.go └── Makefile /db/.data/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /provisioning/ami/packer_config/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "config:base" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /provisioning/ami/.gitignore: -------------------------------------------------------------------------------- 1 | packer_cache/ 2 | packer_config/ 3 | !packer_config/.gitkeep 4 | 5 | *.tgz 6 | -------------------------------------------------------------------------------- /user_data.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | cd /root/hakaru || exit 2 3 | make deploy ARTIFACTS_COMMIT=latest 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .* 2 | !.git* 3 | 4 | hakaru 5 | *.tgz 6 | 7 | !/db/.data 8 | db/.data/* 9 | !/db/.data/.gitkeep 10 | -------------------------------------------------------------------------------- /db/my.cnf: -------------------------------------------------------------------------------- 1 | [mysqld] 2 | character-set-server=utf8mb4 3 | collation-server=utf8mb4_unicode_ci 4 | 5 | [client] 6 | default-character-set=utf8mb4 7 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/VG-Tech-Dojo/hakaru 2 | 3 | go 1.24 4 | 5 | require github.com/go-sql-driver/mysql v1.9.0 6 | 7 | require filippo.io/edwards25519 v1.1.0 // indirect 8 | -------------------------------------------------------------------------------- /provisioning/ami/scripts/cloud.cfg.d/99_defaults.cfg: -------------------------------------------------------------------------------- 1 | #cloud-config 2 | # vim:syntax=yaml expandtab 3 | 4 | locale: en_US.UTF-8 5 | timezone: Asia/Tokyo 6 | 7 | repo_update: true 8 | 9 | preserve_hostname: false 10 | -------------------------------------------------------------------------------- /provisioning/ami/Dockerfile: -------------------------------------------------------------------------------- 1 | ARG VERSION=1.12.0 2 | FROM hashicorp/packer:${VERSION} 3 | 4 | ARG TARGETARCH 5 | 6 | RUN set -x \ 7 | && apk update \ 8 | && apk add curl aws-session-manager-plugin 9 | 10 | ENTRYPOINT ["/bin/packer"] 11 | -------------------------------------------------------------------------------- /db/init/V2__crate_table_event_log.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS `eventlog` ( 2 | `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, 3 | `at` datetime DEFAULT NULL, 4 | `name` varchar(255) NOT NULL, 5 | `value` int(10) unsigned, 6 | PRIMARY KEY (`id`) 7 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 8 | -------------------------------------------------------------------------------- /provisioning/ami/_config.pkr.hcl: -------------------------------------------------------------------------------- 1 | packer { 2 | required_plugins { 3 | amazon-ami-management = { 4 | version = "~> 1.5.0" 5 | source = "github.com/wata727/amazon-ami-management" 6 | } 7 | amazon = { 8 | source = "github.com/hashicorp/amazon" 9 | version = "~> 1" 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /provisioning/instance/systemd/hakaru.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=hakaru server 3 | 4 | [Service] 5 | Type=simple 6 | 7 | # プロセスが不意に終了した場合の挙動 8 | # no: 死んだまま always: 自動起動 9 | Restart=no 10 | 11 | # 起動 12 | ExecStart=/opt/hakaru/bin/hakaru 13 | # 終了 14 | ExecStop=/bin/kill -HUP $MAINPID 15 | # 再起動 16 | ExecReload=/bin/kill -HUP $MAINPID && /opt/hakaru/bin/hakaru 17 | 18 | # 環境変数 19 | EnvironmentFile=/etc/sysconfig/hakaru 20 | 21 | [Install] 22 | WantedBy=multi-user.target 23 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= 2 | filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= 3 | github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= 4 | github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= 5 | github.com/go-sql-driver/mysql v1.9.0 h1:Y0zIbQXhQKmQgTp44Y1dp3wTXcn804QoTptLZT1vtvo= 6 | github.com/go-sql-driver/mysql v1.9.0/go.mod h1:pDetrLJeA3oMujJuvXc8RJoasr589B6A9fwzD3QMrqw= 7 | -------------------------------------------------------------------------------- /provisioning/ami/variables.pkr.hcl: -------------------------------------------------------------------------------- 1 | variable ARTIFACTS_COMMIT { 2 | type = string 3 | default = "latest" 4 | } 5 | 6 | data "amazon-ami" "amzn2" { 7 | most_recent = true 8 | owners = ["137112412989"] 9 | region = "ap-northeast-1" 10 | filters = { 11 | architecture = "x86_64" 12 | name = "amzn2-ami-hvm-2.0*" 13 | root-device-type = "ebs" 14 | virtualization-type = "hvm" 15 | } 16 | } 17 | 18 | locals { 19 | image_time = formatdate("YYYYMMDDhhmmss", timestamp()) 20 | } 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hakaru 2 | 3 | hakaru: 素朴な計測サーバ 4 | 5 | ## 1st step 6 | 7 | - デプロイを実施する 8 | - AMIをビルドする 9 | 10 | ## deployment 11 | 12 | 1. ビルドを実施し、成果物をアップロードする 13 | 14 | ```bash 15 | $ make upload 16 | ``` 17 | 18 | 1. blue/green or in-place のどちらかを実施する 19 | 20 | ### build AMI 21 | 22 | ```bash 23 | $ cd provisioning/ami 24 | $ make 25 | ``` 26 | 27 | ### launch EC2 instance 28 | 29 | - インスタンスタイプ: c5.large 30 | - キーペア: sunrise2025 31 | - VPC: hakaru 32 | - サブネット: プライベートサブネット 33 | - セキュリティグループ: hakaru 34 | - IAMインスタンスプロフィール: hakaru 35 | - ユーザデータに ./user_data.sh の内容を記述する 36 | 37 | ### blue/green deployment 38 | 39 | 1. AMI をビルドする 40 | 1. AMIからEC2インスタンスを起動する 41 | 1. 起動するEC2インスタンスの User data に ./user_data.sh の内容をコピペする 42 | 1. EC2インスタンスをロードバランサーに紐付る 43 | 1. 古いEC2インスタンスを終了する 44 | 45 | ### in-place deployment 46 | 47 | 1. 既にEC2インスタンスを起動していること 48 | 1. インスタンス上でユーザデータ ./user_data.sh の内容を実行する 49 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 VOYAGE GROUP, Inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.github/disable-workflows/upload.yaml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: 4 | push: 5 | branchs: [main] 6 | 7 | jobs: 8 | 9 | build: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions/setup-go@v2 13 | with: 14 | go-version: '^1.16' 15 | - uses: actions/checkout@v2 16 | - uses: actions/cache@v2 17 | with: 18 | path: | 19 | ~/go/pkg/mod 20 | ~/.cache/go-build 21 | key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} 22 | restore-keys: ${{ runner.os }}-go- 23 | - run: make deps 24 | - run: make build 25 | - run: make artifacts.tgz 26 | - uses: actions/upload-artifact@v2 27 | if: !failure() 28 | with: 29 | name: artifacts.tgz 30 | path: ./artifacts.tgz 31 | 32 | upload: 33 | runs-on: ubuntu-latest 34 | needs: [build] 35 | steps: 36 | - uses: actions/checkout@v2 37 | - uses: actions/download-artifacts@v2 38 | with: 39 | name: artifacts.tgz 40 | - uses: aws-actions/configure-aws-credentials@v1 41 | with: 42 | aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} 43 | aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} 44 | aws-region: ap-northeast-1 45 | - run: make upload 46 | -------------------------------------------------------------------------------- /provisioning/ami/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | 3 | AWS_PROFILE ?= sunrise2025-z 4 | 5 | #ARTIFACTS_COMMIT ?= $(shell git rev-parse HEAD) 6 | ARTIFACTS_COMMIT := latest 7 | ARTIFACTS_BUCKET := $(AWS_PROFILE)-hakaru-artifacts 8 | 9 | # https://hub.docker.com/r/hashicorp/packer/tags/ 10 | PACKER_VERSION := 1.12.0 11 | 12 | PACKER_IMAGE := sunrise2025/packer:$(PACKER_VERSION) 13 | 14 | .PHONY: all clean build scripts.tgz docker 15 | 16 | TO ?= hakaru 17 | 18 | all: __require_val clean docker build 19 | 20 | __require_val: $(TO).pkr.hcl 21 | 22 | scripts.tgz: 23 | tar cvzf scripts.tgz -C scripts . 24 | 25 | clean: 26 | -rm -rf *.tgz 27 | 28 | packer = docker run --rm -it \ 29 | --env-file <(aws-vault exec $(AWS_PROFILE) -- env | grep "AWS_" | grep -v "AWS_VAULT") \ 30 | -e TZ=Asia/Tokyo \ 31 | -v $(CURDIR):/work \ 32 | -v $(CURDIR)/packer_config:/root/.config/packer \ 33 | -w /work \ 34 | $(PACKER_IMAGE) 35 | 36 | inspect: __require_val 37 | $(packer) validate -syntax-only $(TO).pkr.hcl 38 | $(packer) inspect . 39 | 40 | init: inspect 41 | $(packer) init -upgrade . 42 | 43 | build: init scripts.tgz 44 | $(packer) build -only '$(TO).*' -var ARTIFACTS_COMMIT=$(ARTIFACTS_COMMIT) . 45 | 46 | docker: Dockerfile 47 | docker build -t $(PACKER_IMAGE) --build-arg VERSION=$(PACKER_VERSION) -f $< . 48 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net/http" 5 | "log" 6 | 7 | "database/sql" 8 | 9 | _ "github.com/go-sql-driver/mysql" 10 | "os" 11 | ) 12 | 13 | func main() { 14 | dataSourceName := os.Getenv("HAKARU_DATASOURCENAME") 15 | if dataSourceName == "" { 16 | dataSourceName = "root:password@tcp(127.0.0.1:13306)/hakaru" 17 | } 18 | 19 | hakaruHandler := func(w http.ResponseWriter, r *http.Request) { 20 | db, err := sql.Open("mysql", dataSourceName) 21 | if err != nil { 22 | panic(err.Error()) 23 | } 24 | defer db.Close() 25 | 26 | stmt, e := db.Prepare("INSERT INTO eventlog(at, name, value) values(NOW(), ?, ?)") 27 | if e != nil { 28 | panic(e.Error()) 29 | } 30 | 31 | defer stmt.Close() 32 | 33 | name := r.URL.Query().Get("name") 34 | value := r.URL.Query().Get("value") 35 | 36 | _, _ = stmt.Exec(name, value) 37 | 38 | origin := r.Header.Get("Origin") 39 | if origin != "" { 40 | w.Header().Set("Access-Control-Allow-Origin", origin) 41 | w.Header().Set("Access-Control-Allow-Credentials", "true") 42 | } else { 43 | w.Header().Set("Access-Control-Allow-Origin", "*") 44 | } 45 | w.Header().Set("Access-Control-Allow-Headers", "Content-Type") 46 | w.Header().Set("Access-Control-Allow-Methods", "GET") 47 | } 48 | 49 | http.HandleFunc("/hakaru", hakaruHandler) 50 | http.HandleFunc("/ok", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) }) 51 | 52 | // start server 53 | if err := http.ListenAndServe(":8081", nil); err != nil { 54 | log.Fatal(err) 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /provisioning/instance/amazon-cloudwatch-agent/amazon-cloudwatch-agent.json: -------------------------------------------------------------------------------- 1 | { 2 | "agent": { 3 | "metrics_collection_interval": 60, 4 | "logfile": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log" 5 | }, 6 | "metrics": { 7 | "metrics_collected": { 8 | "swap": { 9 | "measurement": [ 10 | "swap_used_percent" 11 | ] 12 | }, 13 | "mem": { 14 | "measurement": [ 15 | "mem_used_percent" 16 | ] 17 | } 18 | }, 19 | "append_dimensions": { 20 | "ImageId": "${aws:ImageId}", 21 | "InstanceId": "${aws:InstanceId}", 22 | "InstanceType": "${aws:InstanceType}", 23 | "AutoScalingGroupName": "${aws:AutoScalingGroupName}" 24 | }, 25 | "aggregation_dimensions": [ 26 | [ 27 | "AutoScalingGroupName" 28 | ], 29 | [ 30 | "InstanceId", 31 | "InstanceType" 32 | ], 33 | [] 34 | ] 35 | }, 36 | "logs": { 37 | "logs_collected": { 38 | "files": { 39 | "collect_list": [ 40 | { 41 | "file_path": "/var/log/messages", 42 | "log_group_name": "/hakaru/var/log/messages", 43 | "timestamp_format": "%b %-d %H:%M:%S", 44 | "timezone": "Local" 45 | }, 46 | { 47 | "file_path": "/var/log/secure", 48 | "log_group_name": "/hakaru/var/log/secure", 49 | "timestamp_format": "%b %-d %H:%M:%S", 50 | "timezone": "Local" 51 | } 52 | ] 53 | } 54 | }, 55 | "log_stream_name": "{instance_id}" 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | export AWS_PROFILE ?= sunrise2025-z 2 | export AWS_DEFAULT_REGION := ap-northeast-1 3 | 4 | .PHONY: all deps update fmt test run build clean db upload 5 | 6 | export GOOS ?= 7 | export GOARCH ?= 8 | export GOFLAGS := -mod=$(if $(CI),readonly,mod) 9 | 10 | all: run 11 | 12 | deps: 13 | go mod download 14 | 15 | update: 16 | go get -v -t ./... 17 | 18 | fmt: 19 | gofmt -w . 20 | 21 | test: 22 | go test -v ./... 23 | 24 | run: main.go 25 | go run main.go 26 | 27 | build: deps test 28 | go build -o hakaru 29 | 30 | clean: 31 | rm -rf hakaru *.tgz 32 | 33 | # lcoal mysqld on docker 34 | 35 | db: 36 | docker run --rm -d \ 37 | --name sunrise2025-hakaru-db \ 38 | -e MYSQL_ROOT_PASSWORD=password \ 39 | -e MYSQL_DATABASE=hakaru \ 40 | -e TZ=Asia/Tokyo \ 41 | -p 13306:3306 \ 42 | -v $(CURDIR)/db/.data:/var/lib/mysql \ 43 | -v $(CURDIR)/db/my.cnf:/etc/mysql/conf.d/my.cnf:ro \ 44 | -v $(CURDIR)/db/init:/docker-entrypoint-initdb.d:ro \ 45 | mysql:8.0.33 \ 46 | mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci 47 | 48 | # deployment 49 | 50 | artifacts.tgz: provisioning/instance 51 | $(MAKE) build GOOS=linux GOARCH=amd64 CGO_ENABLED=0 52 | tar czf artifacts.tgz hakaru provisioning/instance 53 | 54 | aws := $(if $(CI),aws,aws-vault exec $(AWS_PROFILE) -- aws) 55 | 56 | ARTIFACTS_BUCKET := $(AWS_PROFILE)-hakaru-artifacts 57 | 58 | upload: $(if $(CI),artifacts.tgz,clean artifacts.tgz) 59 | $(aws) s3 cp artifacts.tgz s3://$(ARTIFACTS_BUCKET)/latest/artifacts.tgz 60 | $(aws) s3 cp artifacts.tgz s3://$(ARTIFACTS_BUCKET)/$$(git rev-parse HEAD)/artifacts.tgz 61 | -------------------------------------------------------------------------------- /provisioning/ami/scripts/hakaru/Makefile: -------------------------------------------------------------------------------- 1 | # provisioning target (run only in ami building) 2 | # 3 | # パッケージのインストール等はここでやる 4 | # 5 | .PHONY: all cloud-config yum app 6 | 7 | export AWS_DEFAULT_REGION := ap-northeast-1 8 | export AWS_DEFAULT_OUTPUT := text 9 | export AWS_PAGER := 10 | 11 | all: cloud-config yum app 12 | 13 | cloud-config: ../cloud.cfg.d/99_defaults.cfg 14 | cp $< /etc/cloud/cloud.cfg.d/ 15 | chmod 0644 /etc/cloud/cloud.cfg.d/99_defaults.cfg 16 | chown -R root:root /etc/cloud/cloud.cfg.d/99_defaults.cfg 17 | 18 | yum: 19 | yum -y update 20 | yum -y install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm 21 | rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 22 | yum-config-manager --disable mysql80-community 23 | yum-config-manager --enable mysql57-community 24 | yum -y install mysql-community-client 25 | yum -y install https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm 26 | yum -y install https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm 27 | 28 | /root/hakaru: 29 | mkdir -p /root/hakaru 30 | 31 | /root/hakaru/Makefile: Makefile /root/hakaru 32 | cp Makefile /root/hakaru/Makefile 33 | 34 | app: /root/hakaru/Makefile 35 | 36 | # application deployment 37 | # 38 | # /root/hakaru/Makefile on ec2 instance 39 | # 40 | 41 | ARTIFACTS_BUCKET ?= sunrise2025-z-hakaru-artifacts 42 | ARTIFACTS_COMMIT ?= latest 43 | 44 | deploy: clean /root/hakaru/app 45 | $(MAKE) -C /root/hakaru/app/provisioning/instance 46 | 47 | clean: 48 | rm -rf /tmp/artifacts.tgz /root/hakaru/app 49 | 50 | /tmp/artifacts.tgz: 51 | aws s3 cp s3://$(ARTIFACTS_BUCKET)/$(ARTIFACTS_COMMIT)/artifacts.tgz /tmp/artifacts.tgz 52 | 53 | /root/hakaru/app: /tmp/artifacts.tgz 54 | mkdir -p /root/hakaru/app 55 | tar xzvf /tmp/artifacts.tgz -C /root/hakaru/app 56 | -------------------------------------------------------------------------------- /provisioning/ami/hakaru.pkr.hcl: -------------------------------------------------------------------------------- 1 | # https://www.packer.io/docs/builders/amazon/ebs 2 | source "amazon-ebs" "hakaru" { 3 | ami_name = format("hakaru - %s", local.image_time) 4 | ami_description = "sunrise2025 hakaru server" 5 | region = "ap-northeast-1" 6 | ena_support = true 7 | sriov_support = true 8 | 9 | tags = { 10 | Name = "hakaru" 11 | Timestamp = timestamp() 12 | SourceAMI = "{{ .SourceAMI }}" 13 | SourceAMIName = "{{ .SourceAMIName }}" 14 | Amazon_AMI_Management_Identifier = "hakaru" 15 | } 16 | 17 | instance_type = "t3.micro" 18 | source_ami = data.amazon-ami.amzn2.id 19 | associate_public_ip_address = true 20 | iam_instance_profile = "hakaru" 21 | ssh_username = "ec2-user" 22 | ssh_interface = "session_manager" 23 | user_data_file = "${path.cwd}/scripts/cloud.cfg.d/99_defaults.cfg" 24 | 25 | security_group_filter { 26 | filters = { 27 | "tag:Name" = "hakaru" 28 | } 29 | } 30 | 31 | subnet_filter { 32 | most_free = true 33 | random = false 34 | filters = { 35 | "tag:Name" = "hakaru-public-ap-northeast-1*" 36 | } 37 | } 38 | 39 | launch_block_device_mappings { 40 | device_name = "/dev/xvda" 41 | volume_size = 20 42 | volume_type = "gp3" 43 | delete_on_termination = true 44 | } 45 | } 46 | 47 | build { 48 | name = "hakaru" 49 | sources = ["source.amazon-ebs.hakaru"] 50 | 51 | provisioner "shell" { 52 | inline = ["while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done"] 53 | } 54 | 55 | provisioner "file" { 56 | source = "${path.cwd}/scripts.tgz" 57 | destination = "/var/tmp/scripts.tgz" 58 | } 59 | 60 | provisioner "shell" { 61 | inline = [ 62 | "mkdir -p /var/tmp/scripts", 63 | "tar xvzf /var/tmp/scripts.tgz -C /var/tmp/scripts", 64 | "rm -rf /var/tmp/scripts.tgz", 65 | "sudo make -C /var/tmp/scripts/hakaru ARTIFACTS_COMMIT=${var.ARTIFACTS_COMMIT}" 66 | ] 67 | } 68 | 69 | provisioner "shell" { 70 | inline = [ 71 | "sudo rm -rf /var/lib/yum && sudo yum clean all", 72 | "sudo rm -rf /tmp/files /home/ec2-user/files", 73 | "sudo rm -f /home/ec2-user/etc /home/ec2-user/.ssh/authorized_keys", 74 | "sudo rm -f /etc/ssh/*_key /etc/ssh/*_key.pub", 75 | "sudo rm -f /etc/udev/rules.d/70-persistent-net.rules" 76 | ] 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /provisioning/instance/Makefile: -------------------------------------------------------------------------------- 1 | # instance setup 2 | # 3 | # /root/hakaru/app/provisioning/instance/Makefile on ec2 instance 4 | # 5 | 6 | export AWS_DEFAULT_REGION := ap-northeast-1 7 | export AWS_DEFAULT_OUTPUT := text 8 | export AWS_PAGER := 9 | 10 | .PHONY: all clean hakaru amazon-cloudwatch-agent healthcheck 11 | 12 | all: clean amazon-cloudwatch-agent hakaru healthcheck 13 | 14 | clean: 15 | -systemctl stop hakaru 16 | -systemctl stop amazon-cloudwatch-agent 17 | -rm -f /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml 18 | 19 | /opt/hakaru/bin: 20 | mkdir -p /opt/hakaru/bin 21 | 22 | /opt/hakaru/bin/hakaru: ../../hakaru /opt/hakaru/bin 23 | cp ../../hakaru /opt/hakaru/bin/hakaru 24 | chmod +x /opt/hakaru/bin/hakaru 25 | 26 | /etc/sysconfig/hakaru: 27 | echo "HAKARU_DATASOURCENAME=hakaru:$$(aws ssm get-parameter --name "/hakaru/rds/hakaru/password" --with-decryption --output text --query Parameter.Value)@tcp($$(aws rds describe-db-instances --db-instance-identifier hakaru --output text --query "DBInstances[0].Endpoint.Address"))/hakaru" > /etc/sysconfig/hakaru 28 | 29 | /etc/systemd/system/hakaru.service: systemd/hakaru.service /opt/hakaru/bin/hakaru /etc/sysconfig/hakaru 30 | cp systemd/hakaru.service /etc/systemd/system/hakaru.service 31 | systemctl daemon-reload 32 | systemctl list-unit-files --type=service | grep hakaru 33 | 34 | hakaru: /etc/systemd/system/hakaru.service 35 | systemctl start hakaru 36 | systemctl enable hakaru 37 | 38 | healthcheck: 39 | curl -v 'http://127.0.0.1:8081/hakaru?name=deploy&value=1' 40 | 41 | # https://docs.aws.amazon.com/ja_jp/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html 42 | /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json: amazon-cloudwatch-agent/amazon-cloudwatch-agent.json 43 | cp amazon-cloudwatch-agent/amazon-cloudwatch-agent.json /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json 44 | 45 | /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml: /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json 46 | /opt/aws/amazon-cloudwatch-agent/bin/config-translator \ 47 | --mode ec2 \ 48 | --os linux \ 49 | --input /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json \ 50 | --output /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml \ 51 | --config /opt/aws/amazon-cloudwatch-agent/etc/common-config.toml 52 | /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent \ 53 | -schematest \ 54 | -config /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml 55 | 56 | amazon-cloudwatch-agent: /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml 57 | systemctl restart amazon-cloudwatch-agent 58 | systemctl enable amazon-cloudwatch-agent 59 | --------------------------------------------------------------------------------