This commit is contained in:
Bartosz Karwacki 2021-11-25 13:01:27 +01:00
commit b98ddc5064
5 changed files with 149 additions and 0 deletions

7
Dockerfile Normal file
View File

@ -0,0 +1,7 @@
FROM hashicorp/terraform:latest
COPY init-db init-db
COPY init-server init-server
COPY main.tf main.tf
ENTRYPOINT terraform init && terraform apply -var="token=${token}" -auto-approve && sleep 240 && terraform destroy -var="token=${token}" -auto-approve

5
deploy.sh Executable file
View File

@ -0,0 +1,5 @@
#!/bin/bash
token=$1
docker build -t gitea-hetzner .
docker run -e token=${1} gitea-hetzner

37
init-db Normal file
View File

@ -0,0 +1,37 @@
#cloud-config
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
write_files:
- path: /root/docker-compose.yml
content: |
version: '3.9'
services:
db:
image: mysql:8
restart: always
environment:
- MYSQL_ROOT_PASSWORD=gitea
- MYSQL_USER=gitea
- MYSQL_PASSWORD=gitea
- MYSQL_DATABASE=gitea
ports:
- "10.0.1.2:3306:3306"
volumes:
- ./mysql:/var/lib/mysql
# instalujemy docker, docker-compose a następnie uruchamiamy naszą bazę danych
runcmd:
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
- add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
- apt-get update -y
- apt-get install -y docker-ce docker-ce-cli containerd.io
- curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- chmod +x /usr/local/bin/docker-compose
- systemctl start docker
- systemctl enable docker
- cd /root/ && docker-compose up -d

45
init-server Normal file
View File

@ -0,0 +1,45 @@
#cloud-config
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
write_files:
- path: /root/docker-compose.yml
content: |
version: '3.9'
services:
server:
image: gitea/gitea:1.15.6
container_name: gitea
environment:
- USER_UID=1000
- USER_GID=1000
- GITEA__database__DB_TYPE=mysql
- GITEA__database__HOST=10.0.1.2:3306
- GITEA__database__NAME=gitea
- GITEA__database__USER=gitea
- GITEA__database__PASSWD=gitea
restart: always
volumes:
- ./gitea:/data
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
ports:
- "3000:3000"
- "222:22"
# instalujemy docker, docker-compose a następnie uruchamiamy naszą bazę danych
runcmd:
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
- add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
- apt-get update -y
- apt-get install -y docker-ce docker-ce-cli containerd.io
- curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- chmod +x /usr/local/bin/docker-compose
- systemctl start docker
- systemctl enable docker
- cd /root/ && docker-compose up -d

55
main.tf Normal file
View File

@ -0,0 +1,55 @@
terraform {
required_providers {
hcloud = {
source = "hetznercloud/hcloud"
version = "1.32.1"
}
}
}
variable "token" {
type = string
}
provider "hcloud" {
token = var.token
}
resource "hcloud_server" "ubuntu_server" {
name = "bk-terraform-server"
image = "ubuntu-20.04"
server_type = "cx11"
network {
network_id = hcloud_network.network.id
}
user_data = file("init-server")
depends_on = [
hcloud_server.ubuntu_mysql
]
}
resource "hcloud_server" "ubuntu_mysql" {
name = "bk-terraform-mysql"
image = "ubuntu-20.04"
server_type = "cx11"
user_data = file("init-db")
network {
network_id = hcloud_network.network.id
}
}
resource "hcloud_network" "network" {
name = "bk-terraform-network"
ip_range = "10.0.1.0/24"
}
resource "hcloud_network_subnet" "subnet" {
network_id = hcloud_network.network.id
type = "cloud"
network_zone = "eu-central"
ip_range = "10.0.1.0/24"
}