Skip to content

Commit

Permalink
updates deployement
Browse files Browse the repository at this point in the history
  • Loading branch information
kundan59 committed Sep 13, 2023
1 parent ab6541e commit 0b3e1cf
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import argparse

import mysql.connector
import gcpresources.connector

parser = argparse.ArgumentParser()

Expand All @@ -21,7 +21,7 @@ def list_of_databases(arg):

for dataBase in dataBases:

mydb = mysql.connector.connect(
mydb = gcpresources.connector.connect(
host=args.host,
user=args.user,
password=args.password,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ gcloud sql databases create shipment_db \
--project=boreal-gravity-396810
```

### 5. create tables
### 5. create tables if needed(table creatation is done by code so no need to create manually)
change the host name with yours mysql db instance host(public ip) user and password
install python 3.8 and
pip install mysql-connector-python
Expand All @@ -74,7 +74,7 @@ python create_tables.py --host 35.237.111.227 --user java_db_user --password Jav

follow the link - https://cloud.google.com/sql/docs/mysql/create-instance

## Deploy mysql on GCP through terraform
## Deploy gcp resources through terraform

install terraform in your local

Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions deployment/gcpresources/terraform/external-secret-gcsm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

GKE_CLUSTER="$1"
REGION="$2"
gcloud container clusters get-credentials "$GKE_CLUSTER" --region "$REGION"
kubectl create secret generic gcpsm-secret --from-file=secret-access-credentials=key.json

helm repo add external-secrets https://charts.external-secrets.io
helm install external-secrets external-secrets/external-secrets -n default --set installCRDs=true
161 changes: 161 additions & 0 deletions deployment/gcpresources/terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#Terraform plugin for creating random ids
resource "random_id" "instance_id" {
byte_length = 4
}
#----------------------------------------my-sql-----------------------------
# create My SQL database instance
resource "google_sql_database_instance" "my_sql" {
name = "${var.app_name}-${var.app_environment}-db-${random_id.instance_id.hex}"
project = var.app_project
region = var.gcp_region_1
database_version = var.db_version

settings {
tier = var.db_tier
activation_policy = var.db_activation_policy
disk_autoresize = var.db_disk_autoresize
disk_size = var.db_disk_size
disk_type = var.db_disk_type

location_preference {
zone = var.gcp_zone_1
}

ip_configuration {
ipv4_enabled = "true"
authorized_networks {
value = var.db_instance_access_cidr
}
}
}
deletion_protection = "false"
}

# create order database
resource "google_sql_database" "order_db" {
name = var.orders_db_name
project = var.app_project
instance = google_sql_database_instance.my_sql.name
}

# Create inventory database
resource "google_sql_database" "inventory_db" {
name = var.inventory_db_name
project = var.app_project
instance = google_sql_database_instance.my_sql.name
}

# Create payment database
resource "google_sql_database" "payment_db" {
name = var.payment_db_name
project = var.app_project
instance = google_sql_database_instance.my_sql.name
}

# Create shipment database
resource "google_sql_database" "shipment_db" {
name = var.shipment_db_name
project = var.app_project
instance = google_sql_database_instance.my_sql.name
}

# create user
resource "random_id" "user_password" {
byte_length = 8
}

resource "google_sql_user" "my_sql_user" {
name = var.user_name
project = var.app_project
instance = google_sql_database_instance.my_sql.name
password = var.user_password == "" ? random_id.user_password.hex : var.user_password
}

#-----------------------pub sub--------------------------------
resource "google_pubsub_topic" "shipment-notification" {
name = "shipment-notification"
message_retention_duration = "604800s"
}

#-----------------------GKE Cluster for applications----------------------------
resource "google_container_cluster" "order-service-gke" {
name = "order-service-gke"
location = var.gcp_zone_1
initial_node_count = 1
}

resource "google_container_cluster" "inventory-service-gke" {
name = "inventory-service-gke"
location = var.gcp_zone_1
initial_node_count = 1
}

resource "google_container_cluster" "payment-service-gke" {
name = "payment-service-gke"
location = var.gcp_zone_1
initial_node_count = 1
}

resource "google_container_cluster" "shipment-service-gke" {
name = "shipment-service-gke"
location = var.gcp_zone_1
initial_node_count = 1
}

resource "null_resource" "external-secret-order" {
provisioner "local-exec" {
command = "/bin/bash external-secret-gcsm.sh order-service-gke ${var.gcp_zone_1}"
}
depends_on = [google_container_cluster.order-service-gke]
}

resource "null_resource" "external-secret-inventory" {
provisioner "local-exec" {
command = "/bin/bash external-secret-gcsm.sh inventory-service-gke ${var.gcp_zone_1}"
}
depends_on = [google_container_cluster.inventory-service-gke]
}

resource "null_resource" "external-secret-payment" {
provisioner "local-exec" {
command = "/bin/bash external-secret-gcsm.sh payment-service-gke ${var.gcp_zone_1}"
}
depends_on = [google_container_cluster.payment-service-gke]
}

resource "null_resource" "external-secret-shipment" {
provisioner "local-exec" {
command = "/bin/bash external-secret-gcsm.sh shipment-service-gke ${var.gcp_zone_1}"
}
depends_on = [google_container_cluster.shipment-service-gke]
}

# use null resources to create my sql tables if needed

#resource "null_resource" "create_order_table" {
# provisioner "local-exec" {
# command = "python create_tables.py ${google_sql_database_instance.my_sql.ip_address.0.ip_address} ${var.user_name} ${var.user_password} ${var.orders_db_name} '${var.create_order_table}'"
# }
# depends_on = [google_sql_database.order_db, google_sql_database_instance.my_sql, random_id.user_password, random_id.instance_id, google_sql_user.my_sql_user]
#}
#
#resource "null_resource" "create_inventory_table" {
# provisioner "local-exec" {
# command = "python create_tables.py ${google_sql_database_instance.my_sql.ip_address.0.ip_address} ${var.user_name} ${var.user_password} ${var.inventory_db_name} '${var.create_inventory_table}'"
# }
# depends_on = [google_sql_database.inventory_db, google_sql_database_instance.my_sql, random_id.user_password, random_id.instance_id, google_sql_user.my_sql_user]
#}
#
#resource "null_resource" "create_payment_table" {
# provisioner "local-exec" {
# command = "python create_tables.py ${google_sql_database_instance.my_sql.ip_address.0.ip_address} ${var.user_name} ${var.user_password} ${var.payment_db_name} '${var.create_payment_table}'"
# }
# depends_on = [google_sql_database.payment_db, google_sql_database_instance.my_sql, random_id.user_password, random_id.instance_id, google_sql_user.my_sql_user]
#}
#
#resource "null_resource" "create_shipment_table" {
# provisioner "local-exec" {
# command = "python create_tables.py ${google_sql_database_instance.my_sql.ip_address.0.ip_address} ${var.user_name} ${var.user_password} ${var.shipment_db_name} '${var.create_shipment_table}'"
# }
# depends_on = [google_sql_database.shipment_db, google_sql_database_instance.my_sql, random_id.user_password, random_id.instance_id, google_sql_user.my_sql_user]
#}
File renamed without changes.
File renamed without changes.
File renamed without changes.
100 changes: 0 additions & 100 deletions deployment/mysql/terraform/main.tf

This file was deleted.

1 change: 0 additions & 1 deletion projects-changes-deploy.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
order-service

0 comments on commit 0b3e1cf

Please sign in to comment.