From 3a286f4d1d0d9d1b0ad3ec54dcf22d52da944e49 Mon Sep 17 00:00:00 2001 From: Aleksey Kuravskij Date: Fri, 10 Nov 2023 18:06:26 +0300 Subject: [PATCH] packer-base 1.3 --- .gitignore | 5 +++ README.md | 1 + config-scripts/create-reddit-vm.sh | 2 +- packer/key-orig.json | 8 ----- packer/ubuntu16.json | 2 +- terraform/.terraform.lock.hcl | 9 ----- terraform/.terraformrc | 9 ----- terraform/files/deploy.sh | 14 -------- terraform/files/puma.service | 13 -------- terraform/lb.tf | 35 -------------------- terraform/main.tf | 51 ----------------------------- terraform/outputs.tf | 7 ---- terraform/terraform.tf | 8 ----- terraform/terraform.tfvars.examples | 8 ----- terraform/variables.tf | 38 --------------------- 15 files changed, 8 insertions(+), 202 deletions(-) delete mode 100644 packer/key-orig.json delete mode 100644 terraform/.terraform.lock.hcl delete mode 100644 terraform/.terraformrc delete mode 100644 terraform/files/deploy.sh delete mode 100644 terraform/files/puma.service delete mode 100644 terraform/lb.tf delete mode 100644 terraform/main.tf delete mode 100644 terraform/outputs.tf delete mode 100644 terraform/terraform.tf delete mode 100644 terraform/terraform.tfvars.examples delete mode 100644 terraform/variables.tf diff --git a/.gitignore b/.gitignore index c91d481..a9491ac 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,6 @@ packer/variables.json +*.tfstate +*.tfstate.*.backup +*.tfstate.backup +*.tfvars +.terraform/ diff --git a/README.md b/README.md index 7d753d9..a7c328d 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ startup_script.sh - shell-скрипт создания инстанса metadata.yaml - файл с метаданными для настройки инстанса ``` + # HW №5 1. Установка packer: ``` diff --git a/config-scripts/create-reddit-vm.sh b/config-scripts/create-reddit-vm.sh index 827a9e5..73d50e5 100755 --- a/config-scripts/create-reddit-vm.sh +++ b/config-scripts/create-reddit-vm.sh @@ -1,6 +1,6 @@ #!/bin/sh -folder_id=$(yc config list | grep folder-id | awk '{print $2}') # грепаем id-каталога +folder_id=$(yc config list | grep folder-id | awk '{print $2}') yc compute instance create \ --name reddit-app \ diff --git a/packer/key-orig.json b/packer/key-orig.json deleted file mode 100644 index 27b8c0e..0000000 --- a/packer/key-orig.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "id": "ajeei6l16cc4havceuv9", - "service_account_id": "ajepveubr1hs27mdara3", - "created_at": "2023-10-30T12:38:46.252077555Z", - "key_algorithm": "RSA_2048", - "public_key": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApuUN79DrpcrZ8Sdi4iE6\nfio3eRgfB5/Jyqf0PGKK9YGuTn948F3H2dD26KccfjlViM5hzy/P1BPbSAwpt58o\nXyoEjdQAE0hVtO4tn65NMQ7P7hl4T/jFbjXVO7QBZIc+ler60eubxvvHuM4mnbQ/\nFuei11mpj55Jnk9JXrQJ1ZvO5gwQFymhO0JdwWh1a6wLdgvc6dOzPUKVcZoWuVbQ\nm+gIcaXaehaZrmR7EqC2PFaQk2QSju+1Rw6C/ss78YujhQE5C8aAe3SauEl6cyxD\ndzVzYk5vD5RRkTO/OnfZ7LNAiNpr5rnTlw/BQHvgz8XQlHtMfkqKwABu3WaFmhOf\nmwIDAQAB\n-----END PUBLIC KEY-----\n", - "private_key": "PLEASE DO NOT REMOVE THIS LINE! Yandex.Cloud SA Key ID \u003cajeei6l16cc4havceuv9\u003e\n-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCm5Q3v0Oulytnx\nJ2LiITp+Kjd5GB8Hn8nKp/Q8Yor1ga5Of3jwXcfZ0Pbopxx+OVWIzmHPL8/UE9tI\nDCm3nyhfKgSN1AATSFW07i2frk0xDs/uGXhP+MVuNdU7tAFkhz6V6vrR65vG+8e4\nziadtD8W56LXWamPnkmeT0letAnVm87mDBAXKaE7Ql3BaHVrrAt2C9zp07M9QpVx\nmha5VtCb6Ahxpdp6FpmuZHsSoLY8VpCT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0\nH+aevr/WYUaahCFYbwKxlHfVDYYQM0vMwsAR+KXfm/z/lAmrWn+M29UchRQTNoGz\nqIZ7nnelVfpOgy1+1GSaa0n3TaTpoOGtdHAB4fN1AoGAOJQSSDUjYnS9KNDWgMJ4\nqbku/JBLLtHgtDwxmyoShzbAqfJI5HJTxswdDi+ilegmq+flNy7IwhxOZ6/kExvP\nIv/3c2DwwXbC0ETwkj2BXqvIZD0xp2ryYHDdESABP91Gx1SZF7kyLjAXGvojeC6k\nTDgL4KUoNU//WzNWwIx2ZK0=\n-----END PRIVATE KEY-----\n" -} diff --git a/packer/ubuntu16.json b/packer/ubuntu16.json index 03f618e..529cefe 100644 --- a/packer/ubuntu16.json +++ b/packer/ubuntu16.json @@ -2,7 +2,7 @@ "builders": [ { "type": "yandex", - "service_account_key_file": "key.json", + "service_account_key_file": "/home/admins/otus/secrets/key.json", "folder_id": "b1g9rbhfq6ba8dfpgv37", "subnet_id": "e9bmh97l31novt99ndqb", "zone": "ru-central1-a", diff --git a/terraform/.terraform.lock.hcl b/terraform/.terraform.lock.hcl deleted file mode 100644 index d2d5b0e..0000000 --- a/terraform/.terraform.lock.hcl +++ /dev/null @@ -1,9 +0,0 @@ -# This file is maintained automatically by "terraform init". -# Manual edits may be lost in future updates. - -provider "registry.terraform.io/yandex-cloud/yandex" { - version = "0.101.0" - hashes = [ - "h1:eT52gyrNCgpV/KvhsIwq33bhMFkaZHwafn8HXPJqHrw=", - ] -} diff --git a/terraform/.terraformrc b/terraform/.terraformrc deleted file mode 100644 index 17415e3..0000000 --- a/terraform/.terraformrc +++ /dev/null @@ -1,9 +0,0 @@ -provider_installation { - network_mirror { - url = "https://terraform-mirror.yandexcloud.net/" - include = ["registry.terraform.io/*/*"] - } - direct { - exclude = ["registry.terraform.io/*/*"] - } -} diff --git a/terraform/files/deploy.sh b/terraform/files/deploy.sh deleted file mode 100644 index 65d0e94..0000000 --- a/terraform/files/deploy.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -echo Waiting for apt-get to finish... -a=1; while [ -n "$(pgrep apt-get)" ]; do echo $a; sleep 1s; a=$(expr $a + 1); done -echo Done. -set -e -APP_DIR=${1:-$HOME} -sudo apt-get install -y git -git clone -b monolith https://github.com/express42/reddit.git $APP_DIR/reddit -cd $APP_DIR/reddit -bundle install -sudo mv /tmp/puma.service /etc/systemd/system/puma.service -sudo systemctl start puma -sudo systemctl enable puma diff --git a/terraform/files/puma.service b/terraform/files/puma.service deleted file mode 100644 index ade466c..0000000 --- a/terraform/files/puma.service +++ /dev/null @@ -1,13 +0,0 @@ -[Unit] -Description=Puma HTTP Server -After=network.target - -[Service] -Type=simple -User=ubuntu -WorkingDirectory=/home/ubuntu/reddit -ExecStart=/bin/bash -lc 'puma' -Restart=always - -[Install] -WantedBy=multi-user.target diff --git a/terraform/lb.tf b/terraform/lb.tf deleted file mode 100644 index d40a2f0..0000000 --- a/terraform/lb.tf +++ /dev/null @@ -1,35 +0,0 @@ -resource "yandex_lb_network_load_balancer" "reddit" { - name = "reddit-lb" - - listener { - name = "reddit-listener" - port = 80 - target_port = 9292 - protocol = "tcp" - external_address_spec { - ip_version = "ipv4" - } - } - - attached_target_group { - target_group_id = yandex_lb_target_group.tg.id - healthcheck { - name = "http" - http_options { - port = 9292 - path = "/" - } - } - } -} - -resource "yandex_lb_target_group" "tg" { - name = "reddit-tg" - dynamic "target" { - for_each = yandex_compute_instance.app.*.network_interface.0.ip_address - content { - address = target.value - subnet_id = var.subnet_id - } - } -} diff --git a/terraform/main.tf b/terraform/main.tf deleted file mode 100644 index 568a36f..0000000 --- a/terraform/main.tf +++ /dev/null @@ -1,51 +0,0 @@ -provider "yandex" { - service_account_key_file = var.service_account_key_file - cloud_id = var.cloud_id - folder_id = var.folder_id - zone = var.zone -} - -resource "yandex_compute_instance" "app" { - count = var.instance_count - name = "reddit-app-${count.index + 1}" - zone = var.zone - platform_id = "standard-v3" - - resources { - core_fraction = 20 - cores = 2 - memory = 2 - } - - boot_disk { - initialize_params { - image_id = var.image_id - } - } - - network_interface { - subnet_id = var.subnet_id - nat = true - } - - metadata = { - ssh-keys = "ubuntu:${file(var.public_key_path)}" - } - - connection { - type = "ssh" - host = self.network_interface.0.nat_ip_address - user = "ubuntu" - agent = false - private_key = file(var.privat_key_path) - } - - provisioner "file" { - source = "files/puma.service" - destination = "/tmp/puma.service" - } - - provisioner "remote-exec" { - script = "files/deploy.sh" - } -} diff --git a/terraform/outputs.tf b/terraform/outputs.tf deleted file mode 100644 index be71a47..0000000 --- a/terraform/outputs.tf +++ /dev/null @@ -1,7 +0,0 @@ -output "external_ip_address_app" { - value = [for ip in yandex_compute_instance.app.*.network_interface.0.nat_ip_address : ip] -} - -output "external_ip_address_lb" { - value = yandex_lb_network_load_balancer.reddit.listener.*.external_address_spec[0].*.address -} diff --git a/terraform/terraform.tf b/terraform/terraform.tf deleted file mode 100644 index da0fe05..0000000 --- a/terraform/terraform.tf +++ /dev/null @@ -1,8 +0,0 @@ -terraform { - required_providers { - yandex = { - source = "yandex-cloud/yandex" - } - } - required_version = ">= 0.13" -} diff --git a/terraform/terraform.tfvars.examples b/terraform/terraform.tfvars.examples deleted file mode 100644 index fb7338a..0000000 --- a/terraform/terraform.tfvars.examples +++ /dev/null @@ -1,8 +0,0 @@ -cloud_id = "********************" -folder_id = "********************" -zone = "ru-central1-a" -image_id = "********************" -public_key_path = "~/.ssh/yc.pub" -privat_key_path = "~/.ssh/yc" -subnet_id = "********************" -service_account_key_file = "~/otus/secrets/sa-terraform-key.json" diff --git a/terraform/variables.tf b/terraform/variables.tf deleted file mode 100644 index 53f5194..0000000 --- a/terraform/variables.tf +++ /dev/null @@ -1,38 +0,0 @@ -variable "cloud_id" { - description = "Cloud" -} -variable "folder_id" { - description = "Folder" -} -variable "zone" { - description = "Zone" - default = "ru-central1-a" -} -variable "public_key_path" { - description = "Path to the public key used for ssh access" -} -variable "privat_key_path" { - description = "Path to the public key used for ssh access" -} -variable "image_id" { - description = "Disk image" -} -variable "subnet_id" { - description = "Subnet" - type = string -} -variable "service_account_key_file" { - description = "key .json" - type = string - default = "key.json" -} -variable "instance_count" { - description = "Number of instances to provision." - type = number - default = 2 -} -variable "region_id" { - description = "region" - type = string - default = "ru-central1" -}