diff --git a/.github/workflows/firebase.yml b/.github/workflows/firebase.yml index f5bcea8..6ed1422 100644 --- a/.github/workflows/firebase.yml +++ b/.github/workflows/firebase.yml @@ -1,3 +1,20 @@ +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# Version 2, December 2004 + +# Copyright (C) 2004 Sam Hocevar + +# Everyone is permitted to copy and distribute verbatim or modified +# copies of this license document, and changing it is allowed as long +# as the name is changed. + +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + +# 0. You just DO WHAT THE FUCK YOU WANT TO. + +# WTFPLなので、オープンソース +# よしかわたいき https://github.com/yoshikawa + name: deploy to firebase on: diff --git a/django/.env b/django/.env deleted file mode 100644 index 9afc9bb..0000000 --- a/django/.env +++ /dev/null @@ -1,10 +0,0 @@ -DATABASE_HOST=mysql -DATABASE_HOST_BACKUP=a4shittyo-app:asia-northeast1:db -DATABASE_USER=test -DATABASE_PASSWORD=test -DATABASE_NAME=test -REDIS_HOST=redis -REDIS_USER= -REDIS_PASSWORD= -SENDGRID_USER=sgny6u7f@kke.com -SENDGRID_PASSWORD=Naoki0206 \ No newline at end of file diff --git a/django/.gitignore b/django/.gitignore index 01e7ca6..9e70f04 100644 --- a/django/.gitignore +++ b/django/.gitignore @@ -1,3 +1,3 @@ -# 本当は駄目だけど、許してニャン -!.env +# ついに .envをけすことになったにゃん! +.env tmp \ No newline at end of file diff --git a/django/Dockerfile b/django/Dockerfile index a89c477..e0d6ff9 100644 --- a/django/Dockerfile +++ b/django/Dockerfile @@ -4,19 +4,18 @@ ENV PORT 8000 EXPOSE 8000 RUN apt-get update -RUN apt-get install -y libgl1-mesa-glx -RUN apt-get install libx264-dev -RUN apt-get install -y ffmpeg +RUN apt-get install -y libgl1-mesa-glx libx264-dev \ + ffmpeg nasm yasm libmp3lame-dev \ + libopus-dev libvorbis-dev libvpx-dev ENV PYTHONUNBUFFERED 1 RUN mkdir /code WORKDIR /code -RUN bash install-ffmpeg.sh - COPY requirements.txt /code/ RUN pip3 install -r requirements.txt COPY . . +# RUN bash install-ffmpeg.sh CMD ["python", "./manage.py", "runserver", "0.0.0.0:8000"] diff --git a/django/example/settings.py b/django/example/settings.py index 5b2d0a6..f6222a1 100644 --- a/django/example/settings.py +++ b/django/example/settings.py @@ -71,8 +71,12 @@ 'example.middleware.AuthMiddleware', ] +CORS_ORIGIN_ALLOW_ALL = True +CORS_ALLOW_CREDENTIALS = True CORS_ORIGIN_WHITELIST = [ "http://localhost:3000", # TODO: デプロイ時変更 + "https://a4shittyo-frontend.web.app", + "https://a4shittyo-frontend.firebaseapp.com" ] ROOT_URLCONF = 'example.urls' @@ -102,10 +106,10 @@ DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', - 'NAME': env('DATABASE_NAME'), #  作成したデータベース名 - 'PASSWORD': env('DATABASE_PASSWORD'), - 'USER': env('DATABASE_USER'), # ログインユーザー - 'HOST': env('DATABASE_HOST'), # コンテナ名 + 'NAME': 'test', #  作成したデータベース名 + 'PASSWORD': 'test', + 'USER': 'test', # ログインユーザー + 'HOST': env('db_private_ip'), # コンテナ名 'PORT': '3306', } } @@ -113,7 +117,7 @@ SESSION_ENGINE = 'redis_sessions.session' SESSION_REDIS = { - 'host': env('REDIS_HOST'), + 'host': env('redis_host'), 'port': 6379, 'db': 0, 'prefix': 'session', diff --git a/docker-compose.yml b/docker-compose.yml index ee0813d..f706774 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,8 +1,8 @@ -version: '3.7' +version: "3.7" services: db: - image: mysql:latest + image: mysql:5.7 restart: always container_name: mysql environment: @@ -17,7 +17,7 @@ services: volumes: - mysqldata:/var/lib/mysql - ./.docker/mysql/conf:/etc/mysql/conf.d - + redis: image: redis:latest ports: diff --git a/images/terraform.png b/images/terraform.png new file mode 100644 index 0000000..1233093 Binary files /dev/null and b/images/terraform.png differ diff --git a/terraform/README.md b/terraform/README.md index cf066f3..07d489c 100644 --- a/terraform/README.md +++ b/terraform/README.md @@ -1,10 +1,19 @@ # Deploy it with Terraform +### デプロイ先の構成要素 + +![terraform-build](../images/terraform.png) + +### Contributors + +[Yoshikawa Taiki](https://github.com/yoshikawa) + ### Docker Image を gcr にアップロード ```sh cd ../django # DjangoのDockerfileをビルドしたいので -gcloud builds submit --tag gcr.io/a4shittyo-app/django +vim .env # .env でsendgridのアカウント、パスワードを指定してください +gcloud builds submit --timeout=1800s --tag gcr.io/a4shittyo-app/django ``` ### GCP の初期設定 @@ -46,10 +55,15 @@ gcloud projects add-iam-policy-binding $PROJECT_ID \ gcloud iam service-accounts keys create CREDENTIALS_FILE.json --iam-account=terraform@$PROJECT_ID.iam.gserviceaccount.com --project $PROJECT_ID ``` -**Google API が足りないというエラーが出たら、URL 先から APIwo 有効化してください** +**Google API が足りないというエラーが出たら、URL 先から API を有効化してください** ```sh terraform init terraform plan terraform apply ``` + +### References + +[Build it with Terraform](https://hackersandslackers.com/terraform-with-google-cloud/) +[Cloud Run から Cloud SQL や Memorystore への繋ぎ方](https://allabout-tech.hatenablog.com/entry/2020/06/30/133859) diff --git a/terraform/main.tf b/terraform/main.tf index 585cd16..2ce5230 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -1,9 +1,25 @@ +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# Version 2, December 2004 + +# Copyright (C) 2004 Sam Hocevar + +# Everyone is permitted to copy and distribute verbatim or modified +# copies of this license document, and changing it is allowed as long +# as the name is changed. + +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + +# 0. You just DO WHAT THE FUCK YOU WANT TO. + +# WTFPLなので、オープンソース +# よしかわたいき https://github.com/yoshikawa locals { database_version = "MYSQL_5_7" # "MYSQL_5_7" - network = "default" # Network name + network = "default" # Network name region = "asia-northeast1" # asia-northeast1 project_id = "a4shittyo-app" # GCP Project ID - db_instance_name = "testdayo" + db_instance_name = "murikamo" } // Configure the Google Cloud provider @@ -33,15 +49,15 @@ module "cloudrun" { network = local.network project = local.project_id region = local.region - database_version = local.database_version + database_version = local.database_version database_instance = local.db_instance_name - instance_name = local.db_instance_name - display_name = "redis" # Display Name - name = "redis-sittyo" # Instance name - location = "" - redis_version = "REDIS_5_0" # 5.0 - size = "1" # 1 - tier = "STANDARD_HA" # STANDARD_HA + instance_name = local.db_instance_name + display_name = "lastredis" # Display Name + name = "lastredis" # Instance name + location = "" + redis_version = "REDIS_5_0" # 5.0 + size = "1" # 1 + tier = "STANDARD_HA" # STANDARD_HA } # module "memorystore" { diff --git a/terraform/modules/cloudrun/main.tf b/terraform/modules/cloudrun/main.tf index c76c64e..e97d19d 100644 --- a/terraform/modules/cloudrun/main.tf +++ b/terraform/modules/cloudrun/main.tf @@ -1,3 +1,19 @@ +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# Version 2, December 2004 + +# Copyright (C) 2004 Sam Hocevar + +# Everyone is permitted to copy and distribute verbatim or modified +# copies of this license document, and changing it is allowed as long +# as the name is changed. + +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + +# 0. You just DO WHAT THE FUCK YOU WANT TO. + +# WTFPLなので、オープンソース +# よしかわたいき https://github.com/yoshikawa locals { db_network = join("/", ["projects", var.project, "global", "networks", var.network]) } @@ -8,6 +24,7 @@ resource "google_compute_global_address" "private_ip_address" { # name = var.private_ip_name name = "private-ip-address" purpose = "VPC_PEERING" + address = "10.2.0.0" address_type = "INTERNAL" prefix_length = 16 network = "projects/${var.project}/global/networks/default" @@ -17,7 +34,7 @@ resource "google_compute_global_address" "private_ip_address" { resource "google_service_networking_connection" "private_vpc_connection" { provider = google-beta - network = "projects/${var.project}/global/networks/default" + network = "projects/${var.project}/global/networks/default" service = "servicenetworking.googleapis.com" reserved_peering_ranges = [google_compute_global_address.private_ip_address.name] } @@ -27,7 +44,7 @@ resource "google_cloud_run_service" "mywebapp" { project = var.project location = var.region - depends_on = [ + depends_on = [ google_vpc_access_connector.connector, google_redis_instance.cache ] @@ -40,15 +57,15 @@ resource "google_cloud_run_service" "mywebapp" { container_port = 8000 } env { - name = "redis_host" + name = "redis_host" value = google_redis_instance.cache.host } env { - name = "redis_port" + name = "redis_port" value = google_redis_instance.cache.port } env { - name = "db_private_ip" + name = "db_private_ip" value = google_sql_database_instance.instance.private_ip_address } } @@ -71,7 +88,7 @@ resource "google_sql_database_instance" "instance" { provider = google-beta database_version = var.database_version - project = var.project + project = var.project name = var.instance_name region = var.region @@ -95,28 +112,28 @@ resource "google_sql_user" "users" { } resource "google_redis_instance" "cache" { - display_name = var.display_name - name = var.name - memory_size_gb = var.size - location_id = var.location - project = var.project - redis_version = var.redis_version - region = var.region - tier = var.tier + display_name = var.display_name + name = var.name + memory_size_gb = var.size + location_id = var.location + project = var.project + redis_version = var.redis_version + region = var.region + tier = var.tier } # Add a private VPC connector to for private access from Cloud Run to CloudSQL(MySQL) and Memorystore(redis). resource "google_vpc_access_connector" "connector" { provider = google-beta - + name = "connector" ip_cidr_range = "10.0.0.0/28" - region = var.region - network = "default" - project = var.project + region = var.region + network = "default" + project = var.project depends_on = [ google_vpc_access_connector.connector ] -} \ No newline at end of file +} diff --git a/terraform/modules/cloudrun/variables.tf b/terraform/modules/cloudrun/variables.tf index 3b9b429..cfced78 100644 --- a/terraform/modules/cloudrun/variables.tf +++ b/terraform/modules/cloudrun/variables.tf @@ -1,3 +1,20 @@ +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# Version 2, December 2004 + +# Copyright (C) 2004 Sam Hocevar + +# Everyone is permitted to copy and distribute verbatim or modified +# copies of this license document, and changing it is allowed as long +# as the name is changed. + +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + +# 0. You just DO WHAT THE FUCK YOU WANT TO. + +# WTFPLなので、オープンソース +# よしかわたいき https://github.com/yoshikawa + variable "project" { description = "Project ID" } @@ -54,4 +71,4 @@ variable "size" { variable "tier" { description = "Service Tier" -} \ No newline at end of file +} diff --git a/terraform/modules/cloudsql/main.tf b/terraform/modules/cloudsql/main.tf index 1021162..9d5c976 100644 --- a/terraform/modules/cloudsql/main.tf +++ b/terraform/modules/cloudsql/main.tf @@ -1,3 +1,20 @@ +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# Version 2, December 2004 + +# Copyright (C) 2004 Sam Hocevar + +# Everyone is permitted to copy and distribute verbatim or modified +# copies of this license document, and changing it is allowed as long +# as the name is changed. + +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + +# 0. You just DO WHAT THE FUCK YOU WANT TO. + +# WTFPLなので、オープンソース +# よしかわたいき https://github.com/yoshikawa + resource "google_sql_database_instance" "instance" { provider = google-beta diff --git a/terraform/modules/cloudsql/variables.tf b/terraform/modules/cloudsql/variables.tf index 5c2b569..082a0a5 100644 --- a/terraform/modules/cloudsql/variables.tf +++ b/terraform/modules/cloudsql/variables.tf @@ -1,3 +1,20 @@ +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# Version 2, December 2004 + +# Copyright (C) 2004 Sam Hocevar + +# Everyone is permitted to copy and distribute verbatim or modified +# copies of this license document, and changing it is allowed as long +# as the name is changed. + +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + +# 0. You just DO WHAT THE FUCK YOU WANT TO. + +# WTFPLなので、オープンソース +# よしかわたいき https://github.com/yoshikawa + variable "database_version" { description = "The database version" } @@ -29,4 +46,4 @@ variable "user_password" { variable "instance_name" { description = "Name of the postgres instance (PROJECT_ID:REGION:INSTANCE_NAME))" type = string -} \ No newline at end of file +} diff --git a/terraform/modules/memorystore/main.tf b/terraform/modules/memorystore/main.tf index 33cd0a6..5df3e4c 100644 --- a/terraform/modules/memorystore/main.tf +++ b/terraform/modules/memorystore/main.tf @@ -1,3 +1,20 @@ +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# Version 2, December 2004 + +# Copyright (C) 2004 Sam Hocevar + +# Everyone is permitted to copy and distribute verbatim or modified +# copies of this license document, and changing it is allowed as long +# as the name is changed. + +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + +# 0. You just DO WHAT THE FUCK YOU WANT TO. + +# WTFPLなので、オープンソース +# よしかわたいき https://github.com/yoshikawa + resource "google_redis_instance" "cache" { authorized_network = var.network display_name = var.display_name diff --git a/terraform/modules/memorystore/variables.tf b/terraform/modules/memorystore/variables.tf index 46e8b9e..82e568b 100644 --- a/terraform/modules/memorystore/variables.tf +++ b/terraform/modules/memorystore/variables.tf @@ -1,3 +1,20 @@ +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# Version 2, December 2004 + +# Copyright (C) 2004 Sam Hocevar + +# Everyone is permitted to copy and distribute verbatim or modified +# copies of this license document, and changing it is allowed as long +# as the name is changed. + +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + +# 0. You just DO WHAT THE FUCK YOU WANT TO. + +# WTFPLなので、オープンソース +# よしかわたいき https://github.com/yoshikawa + variable "display_name" { description = "Instance Name" } @@ -32,4 +49,4 @@ variable "size" { variable "tier" { description = "Service Tier" -} \ No newline at end of file +} diff --git a/terraform/modules/vpc/main.tf b/terraform/modules/vpc/main.tf index a9de884..da471e3 100644 --- a/terraform/modules/vpc/main.tf +++ b/terraform/modules/vpc/main.tf @@ -1,3 +1,20 @@ +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# Version 2, December 2004 + +# Copyright (C) 2004 Sam Hocevar + +# Everyone is permitted to copy and distribute verbatim or modified +# copies of this license document, and changing it is allowed as long +# as the name is changed. + +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + +# 0. You just DO WHAT THE FUCK YOU WANT TO. + +# WTFPLなので、オープンソース +# よしかわたいき https://github.com/yoshikawa + module "vpc" { source = "terraform-google-modules/network/google" version = "~> 2.3" @@ -16,4 +33,4 @@ module "vpc" { description = var.subnet_description } ] -} \ No newline at end of file +} diff --git a/terraform/modules/vpc/variables.tf b/terraform/modules/vpc/variables.tf index 1cdd0fd..4bf44d2 100644 --- a/terraform/modules/vpc/variables.tf +++ b/terraform/modules/vpc/variables.tf @@ -1,3 +1,20 @@ +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# Version 2, December 2004 + +# Copyright (C) 2004 Sam Hocevar + +# Everyone is permitted to copy and distribute verbatim or modified +# copies of this license document, and changing it is allowed as long +# as the name is changed. + +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + +# 0. You just DO WHAT THE FUCK YOU WANT TO. + +# WTFPLなので、オープンソース +# よしかわたいき https://github.com/yoshikawa + variable "auto_create_subnetworks" { type = bool description = "When set to true, the network is created in 'auto subnet mode' and it will create a subnet for each region automatically across the 10.128.0.0/9 address range. When set to false, the network is created in 'custom subnet mode' so the user can explicitly connect subnetwork resources." @@ -42,4 +59,4 @@ variable "shared_vpc_host" { variable "subnetwork" { description = "The name of the subnetwork being created" -} \ No newline at end of file +}