# GCP instance group configuration
## [Preset] Instance, Firewall and Autohealing
### 1. Create Autohealing
#### 1.1. Go to [Health checks page](https://console.cloud.google.com/compute/healthChecks).
#### 1.2. Click **Create health check** button.
#### 1.3. Set Name to **autohealer-check**.
#### 1.4. For Protocol select **HTTPS**.
#### 1.5. Set Request path to **/health**. We will check status of Web service and DB service by this route.
#### 1.6. Set the Health criteria
* Set **Check interval** to `12`.
* Set **Timeout** to `5`.
* Set **Healthy threshold** to `2`.
* Set **Unhealthy threshold** to `10`.
#### 1.7. Click **Create** at the bottom.
---
### 2. Create Firewall Rule
#### 2.1. Go to [Create firewall rule page](https://console.cloud.google.com/networking/firewalls)
#### 2.2. Set Name to `default-allow-http-health-check`.
#### 2.3. For Network, select default.
#### 2.4. For Targets, select **All instances in the network**.
#### 2.5. For Source filter, select **IP ranges**.
#### 2.6. For Source IP ranges, enter `130.211.0.0/22` and `35.191.0.0/16`.
#### 2.7. In Protocols and ports, select tcp and enter `443`.
#### 2.8. Click **Create**.
---
### 3. Setup Instance
#### 3.1 Verify the instance template already open HTTPS
* Go to [instance template page](https://console.cloud.google.com/compute/instanceTemplates/).
* Click the [instance template](https://console.cloud.google.com/compute/instanceTemplates/details/n1-standard-1-50-gb-ubuntu-16?project=hikingbook-website&folder&organizationId) in the list or create the new template.
* Under **Firewall**, check the **Allow HTTPS traffic** has been checked.
#### 3.2 Make sure instance group already check
* Go to [Instance groups page](https://console.cloud.google.com/compute/instanceGroups).
* Cick **Create instance group**.
* Select the template that exist in instance template list.
* Set **Number of instances** to `1`.
* For Health check, select **autohealer-check**.
* Set **Initial delay** to 90. The time allowed for the instance to boot up and the application to fully start before performing the first health check.
* Click **Create**.
#### 3.3 [Optional] Create instance by instance group
* Go to [Instance groups page](https://console.cloud.google.com/compute/instanceGroups).
* Click the instance group that you created
* Click **Create VM**
* Click **Create**
## [Migration] Instance Migrate Step
### 1. Disable Uptime Checks in GCP Monitoring
---
### 2. Stop service
---
### 3. Backup database
#### 3.1. Full backup database with [pgBackRest](https://pgbackrest.org/user-guide.html#backup)
```shell=
# Full backup
pgbackrest --type=full --stanza=hikingbook backup
# Check backup info
pgbackrest info
```
#### 3.2. Download the backup folder to local
https://cloud.google.com/compute/docs/instances/transfer-files?hl=zh-tw#transferbrowser
```shell=
scp -i ~/.ssh/hikingbook-ssh-key ansible@hikingbook.net:/var/backup/hikingbookPITRBackup/ .
```
---
### 4. Config the IP to static in GCP
#### 4.1. [Optional] Config ssh key for the new instance for deploy
https://console.cloud.google.com/compute/metadata/sshKeys?project=hikingbook-website&folder&organizationId
---
### 5. Migrate the old server to the new instance group
#### 5.1 Upload the backup database folder to the new instance group with scp
```shell=
scp -i ~/.ssh/hikingbook-ssh-key /hikingbookPITRBackup ansible@hikingbook.net:/var/backup/hikingbookPITRBackup
```
#### 5.2 Migrate database from the old server
```shell=
# PITR Restore DB
sudo -u postgres pgbackrest --stanza=hikingbook --delta restore \
--recovery-option=recovery_target=immediate
```
#### 5.3 Deploy websites with Ansible
```shell=
./script/deploy.sh production
```
---
### 6. Test webservice with IP
---
### 7. Optimize sync performance
#### 7.1 Connect database
```shell=
psql -d hikingbook -U hikingbookadmin
```
#### 7.2 Migrate database
```sql=
SELECT AddGeometryColumn('track_points','point','4326','POINT',3);
SELECT AddGeometryColumn('records','point','4326','POINT',3)
SELECT AddGeometryColumn('realtime_points','point','4326','POINT',3);
UPDATE track_points
SET
point = ST_SetSRID(ST_MakePoint(locations.longitude, locations.latitude, locations.altitude), 4326),
speed = locations.speed,
course = locations.course
from locations
WHERE locations.id=track_points.location_id;
UPDATE records
SET
point = ST_SetSRID(ST_MakePoint(locations.longitude, locations.latitude, locations.altitude), 4326),
speed = locations.speed,
course = locations.course
from locations
WHERE locations.id=records.location_id;
UPDATE realtime_points
SET
point = ST_SetSRID(ST_MakePoint(locations.longitude, locations.latitude, locations.altitude), 4326),
speed = locations.speed,
course = locations.course
from locations
WHERE locations.id=realtime_points.location_id;
DROP INDEX "public"."idx_track_points_location_id";
DROP INDEX "public"."idx_records_location_id";
DROP INDEX "public"."idx_realtime_points_location_id";
```
#### 7.3 Git merge from features/postgis to production brrach
#### 7.4 Deploy webservice: ./script/producion production backend
#### 7.5 Test webservice with IP: Check /hikes/:id
---
### 8. Config Uptime Checks in GCP Monitoring
---
### 9. Shut down the old machine
---
### 10. Delete the old machine after 3 days?
---
### 11. Get rid of the old static IP.