Skip to content

Commit e36f3a9

Browse files
committed
feat: redeploy strategy without recreating of the server
1 parent 4a090ae commit e36f3a9

File tree

16 files changed

+153
-118
lines changed

16 files changed

+153
-118
lines changed

module/alias/cargo_will/src/bin/willbe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#[ allow( unused_imports ) ]
77
use::willbe::*;
88

9-
fn main() -> Result< (), wtools::error::untyped::Error >
9+
fn main() -> Result< (), error::untyped::Error >
1010
{
1111
Ok( willbe::run( std::env::args().collect() )? )
1212
}

module/move/willbe/src/action/deploy_renew.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,34 +47,30 @@ mod private
4747
.file().data( include_str!( "../../template/deploy/key/pack.sh" ) ).path( "./key/pack.sh" ).end()
4848
.file().data( include_str!( "../../template/deploy/key/Readme.md" ) ).path( "./key/Readme.md" ).end()
4949
// /deploy/
50+
.file().data( include_str!( "../../template/deploy/deploy/redeploy.sh" ) ).path( "./deploy/redeploy.sh" ).end()
51+
.file().data( include_str!( "../../template/deploy/deploy/cloud-init.tpl.hbs" ) ).path( "./deploy/cloud-init.tpl" ).is_template( true ).end()
5052
.file().data( include_str!( "../../template/deploy/deploy/Dockerfile" ) ).path( "./deploy/Dockerfile" ).end()
5153
.file().data( include_str!( "../../template/deploy/deploy/Readme.md" ) ).path( "./deploy/Readme.md" ).end()
5254
// /deploy/gar
5355
.file().data( include_str!( "../../template/deploy/deploy/gar/Readme.md" ) ).path( "./deploy/gar/Readme.md" ).end()
54-
.file().data( include_str!( "../../template/deploy/deploy/gar/main.tf" ) ).path( "./deploy/gar/main.tf" ).end()
56+
.file().data( include_str!( "../../template/deploy/deploy/gar/main.tf.hbs" ) ).path( "./deploy/gar/main.tf" ).is_template( true ).end()
5557
.file().data( include_str!( "../../template/deploy/deploy/gar/outputs.tf" ) ).path( "./deploy/gar/outputs.tf" ).end()
5658
.file().data( include_str!( "../../template/deploy/deploy/gar/variables.tf" ) ).path( "./deploy/gar/variables.tf" ).end()
5759
// /deploy/gce
5860
.file().data( include_str!( "../../template/deploy/deploy/gce/Readme.md" ) ).path( "./deploy/gce/Readme.md" ).end()
59-
.file().data( include_str!( "../../template/deploy/deploy/gce/main.tf" ) ).path( "./deploy/gce/main.tf" ).end()
60-
.file().data( include_str!( "../../template/deploy/deploy/gce/outputs.tf" ) ).path( "./deploy/gce/outputs.tf" ).end()
61+
.file().data( include_str!( "../../template/deploy/deploy/gce/main.tf.hbs" ) ).path( "./deploy/gce/main.tf" ).is_template( true ).end()
62+
.file().data( include_str!( "../../template/deploy/deploy/gce/outputs.tf.hbs" ) ).path( "./deploy/gce/outputs.tf" ).is_template( true ).end()
6163
.file().data( include_str!( "../../template/deploy/deploy/gce/variables.tf" ) ).path( "./deploy/gce/variables.tf" ).end()
62-
// /deploy/gce/templates
63-
.file().data( include_str!( "../../template/deploy/deploy/gce/templates/cloud-init.tpl" ) ).path( "./deploy/gce/templates/cloud-init.tpl" ).end()
6464
// /deploy/gcs
6565
.file().data( include_str!( "../../template/deploy/deploy/gcs/main.tf" ) ).path( "./deploy/gcs/main.tf" ).end()
6666
// /deploy/hetzner
6767
.file().data( include_str!( "../../template/deploy/deploy/hetzner/main.tf.hbs" ) ).path( "./deploy/hetzner/main.tf" ).is_template( true ).end()
6868
.file().data( include_str!( "../../template/deploy/deploy/hetzner/outputs.tf.hbs" ) ).path( "./deploy/hetzner/outputs.tf" ).is_template( true ).end()
6969
.file().data( include_str!( "../../template/deploy/deploy/hetzner/variables.tf" ) ).path( "./deploy/hetzner/variables.tf" ).end()
70-
// /deploy/hetzner/templates
71-
.file().data( include_str!( "../../template/deploy/deploy/hetzner/templates/cloud-init.tpl.hbs" ) ).path( "./deploy/hetzner/templates/cloud-init.tpl" ).end()
7270
// /deploy/aws
7371
.file().data( include_str!( "../../template/deploy/deploy/aws/main.tf" ) ).path( "./deploy/aws/main.tf" ).end()
7472
.file().data( include_str!( "../../template/deploy/deploy/aws/outputs.tf" ) ).path( "./deploy/aws/outputs.tf" ).end()
7573
.file().data( include_str!( "../../template/deploy/deploy/aws/variables.tf" ) ).path( "./deploy/aws/variables.tf" ).end()
76-
// /deploy/aws/templates
77-
.file().data( include_str!( "../../template/deploy/deploy/aws/templates/cloud-init.tpl.hbs" ) ).path( "./deploy/aws/templates/cloud-init.tpl" ).is_template( true ).end()
7874
.form();
7975

8076
formed.files

module/move/willbe/template/deploy/Makefile.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,7 @@ state_storage_pull:
207207
state_storage_init:
208208
terraform -chdir=$(tf_dir)/gcs init
209209
terraform -chdir=$(tf_dir)/gcs apply
210+
211+
# Destroys GCS Bucket for terraform states
212+
state_storage_destroy:
213+
terraform -chdir=$(tf_dir)/gcs destroy

module/move/willbe/template/deploy/deploy/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ ENV TF_VERSION=1.7.4
33

44
WORKDIR /
55

6-
# Installation terraform
6+
# Install terraform
77
RUN apt update --allow-releaseinfo-change \
88
&& apt install wget unzip \
99
&& mkdir -p /usr/lib/terraform/${TF_VERSION} \

module/move/willbe/template/deploy/deploy/aws/main.tf

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,50 @@ resource "aws_instance" "web" {
6060
associate_public_ip_address = true
6161

6262
# Startup script for the instance
63-
# Installs docker, gcloud CLI, downloads docker images and starts the container
64-
user_data = templatefile("${path.module}/templates/cloud-init.tpl", {
63+
# Installs docker and gcloud CLI
64+
user_data = templatefile("${path.module}/../cloud-init.tpl", {
6565
location = "${var.REGION}"
6666
project_id = "${var.PROJECT_ID}"
6767
repo_name = "${var.REPO_NAME}"
6868
image_name = "${var.IMAGE_NAME}"
6969
service_account_creds = "${replace(data.local_sensitive_file.service_account_creds.content, "\n", "")}"
70-
timestamp = "${timestamp()}"
7170
})
7271

73-
user_data_replace_on_change = true
72+
key_name = aws_key_pair.redeploy.key_name
7473
}
7574

7675
# Static IP address for the instace that will persist on restarts and redeploys
7776
resource "aws_eip" "static" {
7877
instance = aws_instance.web.id
7978
domain = "vpc"
8079
}
80+
81+
resource "aws_key_pair" "redeploy" {
82+
public_key = data.local_sensitive_file.ssh_public_key.content
83+
}
84+
85+
resource "terraform_data" "redeploy" {
86+
triggers_replace = timestamp()
87+
88+
connection {
89+
type = "ssh"
90+
user = "ubuntu"
91+
private_key = data.local_sensitive_file.ssh_private_key.content
92+
host = aws_eip.static.public_ip
93+
}
94+
95+
provisioner "file" {
96+
source = "${path.module}/../redeploy.sh"
97+
destination = "/tmp/redeploy.sh"
98+
}
99+
100+
provisioner "remote-exec" {
101+
inline = [
102+
"#!/bin/bash",
103+
"( tail -f -n1 /var/log/deploy-init.log & ) | grep -q 'Docker configuration file updated.'",
104+
"source /etc/environment",
105+
"chmod +x /tmp/redeploy.sh",
106+
"sudo /tmp/redeploy.sh"
107+
]
108+
}
109+
}

module/move/willbe/template/deploy/deploy/aws/templates/cloud-init.tpl.hbs

Lines changed: 0 additions & 46 deletions
This file was deleted.

module/move/willbe/template/deploy/deploy/aws/variables.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,13 @@ variable "IMAGE_NAME" {
2222
data "local_sensitive_file" "service_account_creds" {
2323
filename = "${path.module}/../../key/service_account.json"
2424
}
25+
26+
# Private key for SSH connection
27+
data "local_sensitive_file" "ssh_private_key" {
28+
filename = "${path.module}/../../key/rsa_ssh_key"
29+
}
30+
31+
# Public key for SSH connection
32+
data "local_sensitive_file" "ssh_public_key" {
33+
filename = "${path.module}/../../key/rsa_ssh_key.pub"
34+
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
#cloud-config
22

33
write_files:
4-
- path: /etc/systemd/system/${image_name}.service
5-
permissions: 0644
6-
owner: root
7-
content: |
8-
[Unit]
9-
Description=Start ${image_name} docker container. Build: ${timestamp}
10-
Wants=network-online.target
11-
After=network-online.target
12-
13-
[Service]
14-
Environment="HOME=/root"
15-
ExecStart=/usr/bin/docker run -d -p 80:80 --name=${image_name} ${location}-docker.pkg.dev/${project_id}/${repo_name}/${image_name}
164
- path: /root/service_account.json
175
permissions: 0600
186
owner: root
@@ -22,6 +10,10 @@ write_files:
2210
permissions: 0700
2311
owner: root
2412
content: |
13+
# Configure env for redeploy script
14+
echo "DOCKER_IMAGE=${location}-docker.pkg.dev/${project_id}/${repo_name}/${image_name}" >> /etc/environment
15+
echo "DOCKER_IMAGE_NAME=${image_name}" >> /etc/environment
16+
2517
# Install docker
2618
apt update
2719
apt install apt-transport-https ca-certificates curl software-properties-common -y
@@ -37,10 +29,7 @@ write_files:
3729
# Configure docker with gcloud
3830
gcloud auth activate-service-account --key-file=/root/service_account.json
3931
gcloud auth configure-docker ${location}-docker.pkg.dev --quiet
40-
# Start docker container
41-
systemctl daemon-reload
42-
systemctl start ${image_name}.service
4332

4433

4534
runcmd:
46-
- nohup /root/init.sh > /var/log/{{docker_image_name}}-instance-init.log 2>&1 &
35+
- nohup /root/init.sh > /var/log/deploy-init.log 2>&1 &

module/move/willbe/template/deploy/deploy/gar/main.tf renamed to module/move/willbe/template/deploy/deploy/gar/main.tf.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ resource "google_artifact_registry_repository" "container-images-repo" {
99
location = var.REGION
1010
project = var.PROJECT_ID
1111
repository_id = var.REPO_NAME
12-
description = "Docker image registry for the Learn Together web-site"
12+
description = "Docker image registry for the {{docker_image_name}} deployments"
1313
# Format of the repository. We are using Docker.
1414
format = "DOCKER"
1515
}

module/move/willbe/template/deploy/deploy/gce/main.tf renamed to module/move/willbe/template/deploy/deploy/gce/main.tf.hbs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
locals {
22
# Helper var for formatting docker image name
3-
image_name = format("%s-docker.pkg.dev/%s/%s/%s", var.REGION, var.PROJECT_ID, var.REPO_NAME, var.IMAGE_NAME)
3+
image_name = format("%s-docker.pkg.dev/%s/%s/%s", var.REGION, var.PROJECT_ID, var.REPO_NAME, var.IMAGE_NAME)
44
# Helper var for formatting subnetwork for our instance
5-
subnetwork = format("projects/%s/regions/%s/subnetworks/default", var.PROJECT_ID, var.REGION)
6-
instance_name = format("ltsite-%s", formatdate("YYYYMMDDhhmmss", timestamp()))
5+
subnetwork = format("projects/%s/regions/%s/subnetworks/default", var.PROJECT_ID, var.REGION)
76
}
87

98
# Provider for resource creation
@@ -18,23 +17,23 @@ resource "google_compute_address" "default" {
1817
}
1918

2019
# GCE instance block.
21-
resource "google_compute_instance" "lts-container-vm" {
22-
project = var.PROJECT_ID
20+
resource "google_compute_instance" "{{docker_image_name}}" {
21+
project = var.PROJECT_ID
2322
# Instance name
24-
name = local.instance_name
23+
name = "{{docker_image_name}}"
2524
# Instance size. e2-micro is 0.25-2 vCPU & 1GB RAM
2625
machine_type = "e2-micro"
2726
zone = var.ZONE
2827

2928
# Main disk options
3029
boot_disk {
3130
initialize_params {
32-
# Disk image name. We're using Container-optimised OS (COS).
33-
image = "projects/cos-cloud/global/images/cos-stable-109-17800-147-15"
31+
# Disk image name. We're using Ubuntu 24.04 distro.
32+
image = "projects/ubuntu-os-cloud/global/images/ubuntu-2404-noble-amd64-v20241004"
3433
# Disk size in GB. 10GB is allowed minimum.
35-
size = 10
34+
size = 10
3635
# Disk type. Possible values: pd-standard, pd-ssd, or pd-balanced.
37-
type = "pd-balanced"
36+
type = "pd-balanced"
3837
}
3938
}
4039

@@ -52,19 +51,20 @@ resource "google_compute_instance" "lts-container-vm" {
5251
metadata = {
5352
# Cloud-init startup script for configuring the instance with our docker container.
5453
user-data = "${data.cloudinit_config.conf.rendered}"
54+
ssh-keys = "root:${data.local_sensitive_file.ssh_public_key.content}"
5555
}
5656

5757
allow_stopping_for_update = true
5858

5959
scheduling {
6060
# Restart on failure.
61-
automatic_restart = true
61+
automatic_restart = true
6262
# Describes maintenance behavior for the instance. Possible values: MIGRATE or TERMINATE.
6363
on_host_maintenance = "MIGRATE"
6464
# Configures whether to allow stopping instance at any moment for reduced cost.
65-
preemptible = false
65+
preemptible = false
6666
# Configures spot instance. Possible values: SPOT or STANDARD.
67-
provisioning_model = "STANDARD"
67+
provisioning_model = "STANDARD"
6868
}
6969

7070
# Configues service account scopes.
@@ -86,3 +86,29 @@ resource "google_compute_instance" "lts-container-vm" {
8686
# Use `https-server` for https traffic on port 443.
8787
tags = ["http-server"]
8888
}
89+
90+
resource "terraform_data" "redeploy" {
91+
triggers_replace = timestamp()
92+
93+
connection {
94+
type = "ssh"
95+
user = "root"
96+
private_key = data.local_sensitive_file.ssh_private_key.content
97+
host = google_compute_instance.{{docker_image_name}}.network_interface[0].access_config[0].nat_ip
98+
}
99+
100+
provisioner "file" {
101+
source = "${path.module}/../redeploy.sh"
102+
destination = "/tmp/redeploy.sh"
103+
}
104+
105+
provisioner "remote-exec" {
106+
inline = [
107+
"#!/bin/bash",
108+
"( tail -f -n1 /var/log/deploy-init.log & ) | grep -q 'Docker configuration file updated.'",
109+
"source /etc/environment",
110+
"chmod +x /tmp/redeploy.sh",
111+
"/tmp/redeploy.sh"
112+
]
113+
}
114+
}

0 commit comments

Comments
 (0)