Дипломная работа: Автоматизация доставки программного обеспечения при помощи DevOps практик и инструментов в облаке AWS в компании ООО "Команда Лабс"

Внимание! Если размещение файла нарушает Ваши авторские права, то обязательно сообщите нам
126
sudo systemctl status nginx.service | grep -q 'Active: active (running)' >
/dev/null 2>&1
if [ "$?" -gt "0" ]; then
# if the NGINX service is not Active, there is a configuration
# error, so the exit code is set to 1 and the script is aborted
echo -e "\n$(date +"%d-%b-%Y-%H-%M-%S") | NGINX did not start
correctly, aborting script" |& tee -a ${INSTALL_LOG_FILE_PATH}
exit 1
fi
# Reboot the server
echo -e "\n$(date +"%d-%b-%Y-%H-%M-%S") | NGINX is successfully
installed and configured" |& tee -a ${INSTALL_LOG_FILE_PATH}
echo "$(date +"%d-%b-%Y-%H-%M-%S") | Rebooting server to verify
NGINX starts automatically..." |& tee -a ${INSTALL_LOG_FILE_PATH}
sudo shutdown -r now
Создание образа Ubuntu 18.04.04 и установка Nginx из исходного
кода в AWS с помощью Packer
{
"variables": {
"nginx_ver": "1.17.7",
"pcre_ver": "8.42",
"zlib_ver": "1.2.11",
"openssl_ver": "1.1.0h",
"geoip_ver": "20180605",
"headers_more_ver":"v0.33",
"working_dir": "/opt",
"src_folder": "src_files",
"deb_pkg_folder": "deb_pkg",
"log_folder": "log",
"log_file": "install_source.log"
},
"builders": [{
"type": "amazon-ebs",
"access_key": "{{user `aws_access_key`}}",
"secret_key": "{{user `aws_secret_key`}}",
"region": "eu-north-1",
"source_ami_filter": {
"filters": {
"virtualization-type": "hvm",
127
"name": "ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*",
"root-device-type": "ebs"
},
"owners": ["099720109477"],
"most_recent": true
},
"instance_type": "t2.micro",
"ssh_username": "ubuntu",
"ssh_keypair_name": "id_rsa",
"ssh_private_key_file": "/home/akilin/.ssh/id_rsa",
"ami_name": "custom_nginx_ubuntu_{{timestamp}}",
"associate_public_ip_address": "true"
}],
"provisioners": [{
"type": "shell",
"inline": ["sudo mkdir -p {{user `working_dir`}}/{{user `src_folder`}}",
"sudo chown ubuntu:ubuntu {{user `working_dir`}}/{{user `src_folder`}}",
"sudo mkdir -p {{user `working_dir`}}/{{user `deb_pkg_folder`}}/",
"sudo chown ubuntu:ubuntu {{user `working_dir`}}/{{user `deb_pkg_folder`}}"]
},{
"type": "file",
"source": "./upload/",
"destination": "{{user `working_dir`}}/{{user `deb_pkg_folder`}}"
},{
"type": "shell",
"scripts": ["./bash-scripts/00--nginx-prep_install.sh",
"./bash-scripts/01a-nginx-install_from_source.sh",
"./bash-scripts/02--nginx-configure_post_install.sh",
"./bash-scripts/01c-ansible-install_from_apt.sh"],
"expect_disconnect": true,
"environment_vars": [
"NGINX_VER={{user `nginx_ver`}}",
"PCRE_VER={{user `pcre_ver`}}",
"ZLIB_VER={{user `zlib_ver`}}",
"OPENSSL_VER={{user `openssl_ver`}}",
"HEADERS_MORE_VER={{user `headers_more_ver`}}",
"GEOIP_VER={{user `geoip_ver`}}",
"WORKING_DIR={{user `working_dir`}}",
"SRC_FOLDER={{user `src_folder`}}",
"DEB_PKG_FOLDER={{user `deb_pkg_folder`}}",
"LOG_FOLDER={{user `log_folder`}}",
"LOG_FILE={{user `log_file`}}"]
},{
"type": "shell",
"script": "./bash-scripts/03--nginx-verify_install.sh",
"pause_before": "10s",
"environment_vars": [
"WORKING_DIR={{user `working_dir`}}",
"LOG_FOLDER={{user `log_folder`}}",
"LOG_FILE={{user `log_file`}}"]
},{
"type": "file",
"source": "{{user `working_dir`}}/{{user `deb_pkg_folder`}}/",
"destination": "./download",
128
"direction": "download"
},{
"type": "ansible-local",
"playbook_file": "./upload/nginx.yml"
},{
"type": "file",
"source": "{{user `working_dir`}}/{{user `log_folder`}}/",
"destination": "./download",
"direction": "download"
}]
}
Разворачивание инфраструктуры внутри AWS с помощью Terraform
скрипта
/*====
Variables used across all modules
======*/
locals {
production_availability_zones = ["eu-central-1a", "eu-central-1b"]
}
#
# Provider. We assume access keys are provided via environment variables.
#
provider "aws" {
region = "${var.aws_region}"
}
#
# Network. We create a VPC, gateway, subnets and security groups.
#
resource "aws_vpc" "vpc_main" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags {
Name = "Main VPC"
}
}
data "aws_availability_zone" "a" {
name = "eu-central-1a"
}
data "aws_availability_zone" "b" {
name = "eu-central-1b"
}
129
resource "aws_internet_gateway" "default" {
vpc_id = "${aws_vpc.vpc_main.id}"
}
resource "aws_route" "internet_access" {
route_table_id = "${aws_vpc.vpc_main.main_route_table_id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.default.id}"
}
# Create a public subnet to launch our load balancers
resource "aws_subnet" "public1" {
vpc_id = "${aws_vpc.vpc_main.id}"
cidr_block = "10.0.7.0/24" # 10.0.0.0 - 10.0.0.255 (256)
map_public_ip_on_launch = true
availability_zone = "${data.aws_availability_zone.a.name}"
}
resource "aws_subnet" "public2" {
vpc_id = "${aws_vpc.vpc_main.id}"
cidr_block = "10.0.8.0/24" # 10.0.0.0 - 10.0.0.255 (256)
map_public_ip_on_launch = true
availability_zone = "${data.aws_availability_zone.b.name}"
}
# Create a private subnet to launch our backend instances
resource "aws_subnet" "private1" {
vpc_id = "${aws_vpc.vpc_main.id}"
cidr_block = "10.0.10.0/24" # 10.0.1.0 - 10.0.1.255 (256)
// map_public_ip_on_launch = true
// availability_zone = "${data.aws_availability_zone.names[count.index]}"
// availability_zone = ["eu-central-1b","eu-central-1a"]
availability_zone = "${data.aws_availability_zone.a.name}"
}
resource "aws_subnet" "private2" {
vpc_id = "${aws_vpc.vpc_main.id}"
cidr_block = "10.0.11.0/24" # 10.0.1.0 - 10.0.1.255 (256)
// map_public_ip_on_launch = true
// availability_zone = "${data.aws_availability_zone.names[count.index]}"
// availability_zone = ["eu-central-1b","eu-central-1a"]
availability_zone = "${data.aws_availability_zone.b.name}"
}
# A security group for the ELB so it is accessible via the web
resource "aws_security_group" "elb" {
name = "sec_group_elb"
description = "Security group for public facing ELBs"
vpc_id = "${aws_vpc.vpc_main.id}"
# HTTP access from anywhere
ingress {
from_port = 80
to_port = 80
130
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# HTTPS access from anywhere
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# Outbound internet access
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
data "aws_security_group" "db" {
vpc_id = "${aws_vpc.vpc_main.id}"
name = "default"
}
# Our default security group to access the instances over SSH and HTTP
resource "aws_security_group" "bastion" {
name = "sec_group_private"
description = "Security group for backend servers and private ELBs"
vpc_id = "${aws_vpc.vpc_main.id}"
tags {
Name = "HapiProdBastion_SG"
}
# SSH access from anywhere
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# Allow all from private subnet
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["${aws_subnet.private1.cidr_block}"]
}
# Outbound internet access
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
Источник: https://baza.diplomsite.ru/previewfile/1758