update fix
This commit is contained in:
parent
40c55034ea
commit
d39ee251a5
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
token.txt
|
188
IasS/ex1-2/hetzner-gitea-init.py
Normal file
188
IasS/ex1-2/hetzner-gitea-init.py
Normal file
@ -0,0 +1,188 @@
|
||||
from hcloud import Client
|
||||
from hcloud.networks.domain import NetworkSubnet
|
||||
from hcloud.locations.domain import Location
|
||||
from hcloud.images.domain import Image
|
||||
from hcloud.server_types.domain import ServerType
|
||||
import time
|
||||
|
||||
client = Client(
|
||||
token="KccUEiddxtzGoLWSNC3V8tylq7MYHCjdnShtgasQ8jSbHqCjGoaa6Rq7yoz4uS23"
|
||||
)
|
||||
PREFIX = "s444498"
|
||||
YOUR_LOCAL_SSH_PUBKEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAj15oTbXA0KGBw5V/76KXDgJZpLNrxUO/rTA0u0eRl1 wirus@wirusowylapek"
|
||||
try:
|
||||
ssh_key = client.ssh_keys.create(name=f"{PREFIX}-pzc-ssh-key-2", public_key=YOUR_LOCAL_SSH_PUBKEY)
|
||||
print(f"Key {ssh_key.data_model.name} created: {ssh_key.data_model.public_key}")
|
||||
except:
|
||||
ssh_key = client.ssh_keys.get_by_name(f"{PREFIX}-pzc-ssh-key-2")
|
||||
print(f"Key {ssh_key.data_model.name} already in use: {ssh_key.data_model.public_key}")
|
||||
|
||||
try:
|
||||
vnet = client.networks.create(
|
||||
name=f"{PREFIX}-pzc-test-vnet",
|
||||
ip_range="10.10.10.0/24",
|
||||
subnets=[
|
||||
NetworkSubnet(ip_range="10.10.10.0/24", network_zone="eu-central", type="cloud")
|
||||
]
|
||||
)
|
||||
print(f"Created network: {vnet.data_model.name} ({vnet.data_model.ip_range})")
|
||||
except:
|
||||
vnet = client.networks.get_by_name(
|
||||
f"{PREFIX}-pzc-test-vnet",
|
||||
)
|
||||
print(f"Network in use: {vnet.data_model.name} ({vnet.data_model.ip_range})")
|
||||
|
||||
try:
|
||||
shared_volume = client.volumes.create(
|
||||
size=15,
|
||||
name="gitea-volume-s444498",
|
||||
location=Location("hel1"))
|
||||
shared_volume.action.wait_until_finished()
|
||||
print(f"Created volume: {shared_volume.volume}")
|
||||
except:
|
||||
shared_volume = client.volumes.get_by_name("gitea-volume-s444498")
|
||||
shared_volume.action.wait_until_finished()
|
||||
print(f"Volume in use: {shared_volume.volume}")
|
||||
|
||||
volume_device = shared_volume.volume.linux_device
|
||||
|
||||
cloud_init_mysql=r'''#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:5.7
|
||||
restart: always
|
||||
environment:
|
||||
MYSQL_DATABASE: gitea
|
||||
MYSQL_USER: gitea
|
||||
MYSQL_PASSWORD: gitea
|
||||
MYSQL_ROOT_PASSWORD: gitea
|
||||
ports:
|
||||
- "10.10.10.2:3306:3306"
|
||||
volumes:
|
||||
- my-db:/var/lib/mysql
|
||||
phpmyadmin:
|
||||
image: phpmyadmin
|
||||
restart: always
|
||||
ports:
|
||||
- "8080:80"
|
||||
volumes:
|
||||
my-db: {}
|
||||
|
||||
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
|
||||
'''
|
||||
|
||||
mysql_server = client.servers.create(
|
||||
name=f"{PREFIX}-mysql1",
|
||||
server_type=ServerType("cx11"),
|
||||
image=Image(name="ubuntu-20.04"),
|
||||
ssh_keys=[ssh_key],
|
||||
networks=[vnet],
|
||||
location=Location("hel1"),
|
||||
user_data=cloud_init_mysql
|
||||
)
|
||||
|
||||
mysql_server.action.wait_until_finished()
|
||||
print(f"Creating mysql server: {mysql_server.action.complete}")
|
||||
|
||||
time.sleep(20)
|
||||
|
||||
cloud_init_gitea=r'''#cloud-config
|
||||
packages:
|
||||
- apt-transport-https
|
||||
- ca-certificates
|
||||
- curl
|
||||
- gnupg-agent
|
||||
- software-properties-common
|
||||
|
||||
write_files:
|
||||
- path: /root/docker-compose.yml
|
||||
content: |
|
||||
version: "3"
|
||||
|
||||
networks:
|
||||
gitea:
|
||||
external: false
|
||||
|
||||
services:
|
||||
server:
|
||||
image: gitea/gitea:1.17.4
|
||||
container_name: gitea
|
||||
environment:
|
||||
- USER_UID=1000
|
||||
- USER_GID=1000
|
||||
- GITEA__server__DOMAIN=${DOMAIN}
|
||||
- GITEA__database__DB_TYPE=mysql
|
||||
- GITEA__database__HOST=10.10.10.2:3306
|
||||
- GITEA__database__NAME=gitea
|
||||
- GITEA__database__USER=gitea
|
||||
- GITEA__database__PASSWD=gitea
|
||||
restart: always
|
||||
networks:
|
||||
- gitea
|
||||
volumes:
|
||||
- ./data:/root/gitea
|
||||
- ./config:/root/gitea/config
|
||||
- /etc/timezone:/etc/timezone:ro
|
||||
- /etc/localtime:/etc/localtime:ro
|
||||
- /mnt/data_gitea:/data
|
||||
ports:
|
||||
- "3000:3000"
|
||||
- "222:22"
|
||||
'''
|
||||
runcmd = f'''
|
||||
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 /mnt/
|
||||
- mkdir data_gitea
|
||||
- cd /root/
|
||||
- IP=$(hostname -I | cut -d ' ' -f 1)
|
||||
- echo "DOMAIN=$IP" >> .env
|
||||
- sudo mkfs.xfs -f {volume_device}
|
||||
- mount -o discard,defaults {volume_device} /mnt/data_gitea
|
||||
- docker-compose up -d
|
||||
'''
|
||||
|
||||
cloud_init_gitea += runcmd
|
||||
|
||||
gitea_server = client.servers.create(
|
||||
name=f"{PREFIX}-gitea1",
|
||||
server_type=ServerType("cx11"),
|
||||
image=Image(name="ubuntu-20.04"),
|
||||
ssh_keys=[ssh_key],
|
||||
networks=[vnet],
|
||||
location=Location("hel1"),
|
||||
user_data=cloud_init_gitea,
|
||||
volumes=[shared_volume.volume],
|
||||
automount=True
|
||||
)
|
||||
|
||||
gitea_server.action.wait_until_finished()
|
||||
print(f"Creating gitea server: {gitea_server.action.complete}")
|
@ -2,7 +2,7 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 34,
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@ -30,17 +30,9 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 35,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Network in use: s444498-pzc-test-vnet (10.10.10.0/24)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from hcloud.networks.domain import NetworkSubnet\n",
|
||||
"\n",
|
||||
@ -62,17 +54,9 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 36,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Creating gitea server: True\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from hcloud.locations.domain import Location\n",
|
||||
"from hcloud.images.domain import Image\n",
|
||||
@ -124,8 +108,10 @@
|
||||
" - cd /root/ && docker-compose up -d\n",
|
||||
" '''\n",
|
||||
"\n",
|
||||
"for i in range(20):\n",
|
||||
"\n",
|
||||
" mysql_server = client.servers.create(\n",
|
||||
" name=f\"{PREFIX}-mysql\", \n",
|
||||
" name=f\"{PREFIX}-mysql-{i}\", \n",
|
||||
" server_type=ServerType(\"cx11\"), \n",
|
||||
" image=Image(name=\"ubuntu-20.04\"), \n",
|
||||
" ssh_keys=[ssh_key], \n",
|
||||
@ -137,6 +123,47 @@
|
||||
" mysql_server.action.wait_until_finished()\n",
|
||||
" print(f\"Creating gitea server: {mysql_server.action.complete}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "NameError",
|
||||
"evalue": "name 'client' is not defined",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
|
||||
"Cell \u001b[0;32mIn [1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m shared_volume \u001b[39m=\u001b[39m client\u001b[39m.\u001b[39mvolumes\u001b[39m.\u001b[39mcreate(\n\u001b[1;32m 2\u001b[0m size\u001b[39m=\u001b[39m\u001b[39m15\u001b[39m,\n\u001b[1;32m 3\u001b[0m name\u001b[39m=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mgitea-volume\u001b[39m\u001b[39m\"\u001b[39m)\n",
|
||||
"\u001b[0;31mNameError\u001b[0m: name 'client' is not defined"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Usuwanie 9 serwerów\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"servers = client.servers.get_all()\n",
|
||||
"print(f\"Usuwanie {len(servers)} serwerów\")\n",
|
||||
"for s in servers:\n",
|
||||
" if s.data_model.name.startswith(PREFIX):\n",
|
||||
" action = client.servers.delete(s)\n",
|
||||
" print(f\"\\tUsuwanie serwera {s.data_model.name} ({s.data_model.public_net.ipv4.ip}): {action.data_model.status}\")"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
@ -2,7 +2,7 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@ -30,7 +30,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@ -62,14 +62,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Creating gitea server: True\n"
|
||||
"Creating vscode server: True\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@ -85,16 +85,14 @@
|
||||
" - curl\n",
|
||||
" - gnupg-agent\n",
|
||||
" - software-properties-common\n",
|
||||
"\n",
|
||||
"write_files:\n",
|
||||
" - content: |\n",
|
||||
" curl -fsSL https://code-server.dev/install.sh > /root/install.sh\n",
|
||||
" path: /root/install.sh\n",
|
||||
" owner: root:root\n",
|
||||
" permissions: '755'\n",
|
||||
"\n",
|
||||
"runcmd:\n",
|
||||
" - bash /root/install.sh\n",
|
||||
" - bash install.sh\n",
|
||||
" - code-server --bind-addr 0.0.0.0:8080\n",
|
||||
" '''\n",
|
||||
"\n",
|
||||
@ -109,7 +107,7 @@
|
||||
")\n",
|
||||
"\n",
|
||||
"vscode_server.action.wait_until_finished()\n",
|
||||
"print(f\"Creating gitea server: {vscode_server.action.complete}\")"
|
||||
"print(f\"Creating vscode server: {vscode_server.action.complete}\")"
|
||||
]
|
||||
}
|
||||
],
|
@ -4,7 +4,7 @@ from hcloud.server_types.domain import ServerType
|
||||
from hcloud.networks.domain import NetworkSubnet
|
||||
from hcloud.locations.domain import Location
|
||||
from hcloud.load_balancer_types.domain import LoadBalancerType
|
||||
from hcloud.load_balancers.domain import LoadBalancerTarget, LoadBalancerService, LoadBalancerHealthCheck, LoadBalancerHealtCheckHttp
|
||||
from hcloud.load_balancers.domain import LoadBalancerTarget, LoadBalancerService, LoadBalancerServiceHttp, LoadBalancerHealthCheck, LoadBalancerHealtCheckHttp
|
||||
from pathlib import Path
|
||||
|
||||
with open("../token.txt", "r") as file:
|
||||
@ -42,14 +42,38 @@ except:
|
||||
)
|
||||
print(f"Network in use: {vnet.data_model.name} ({vnet.data_model.ip_range})")
|
||||
|
||||
webservice_init=r'''
|
||||
#cloud-config
|
||||
webservice_init=r'''#cloud-config
|
||||
packages:
|
||||
- apt-transport-https
|
||||
- ca-certificates
|
||||
- curl
|
||||
- gnupg-agent
|
||||
- software-properties-common
|
||||
|
||||
write_files:
|
||||
- path: /root/Dockerfile
|
||||
content: |
|
||||
FROM ubuntu
|
||||
COPY ./webservice /
|
||||
RUN chmod +x ./webservice
|
||||
EXPOSE 80:8080/tcp
|
||||
CMD ./webservice
|
||||
|
||||
runcmd:
|
||||
- git clone https://git.wmi.amu.edu.pl/s444498/cloud
|
||||
- cd Public_cloud
|
||||
- chmod +x webservice
|
||||
- ./webservice
|
||||
- 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
|
||||
- curl -fsSL https://git.wmi.amu.edu.pl/s444498/cloud/raw/branch/master/Public_cloud/webservice > /root/webservice
|
||||
- systemctl start docker
|
||||
- systemctl enable docker
|
||||
- cd /root/
|
||||
- docker build -t webservice .
|
||||
- docker run -d -p 80:8080 -t webservice
|
||||
'''
|
||||
|
||||
for i in range(NUM_OF_SERVERS):
|
||||
webservice_server = client.servers.create(
|
||||
name=f"{PREFIX}-webservice-{i+1}",
|
||||
@ -63,22 +87,23 @@ for i in range(NUM_OF_SERVERS):
|
||||
|
||||
webservice_server.action.wait_until_finished()
|
||||
print(f"Creating webservice {i+1}: {webservice_server.action.complete}")
|
||||
loadbalancer_targets.append(LoadBalancerTarget(type="server", server=webservice_server.server, use_private_ip=True))
|
||||
loadbalancer_targets.append(LoadBalancerTarget(type="server", server=webservice_server.server, use_private_ip=False))
|
||||
|
||||
try:
|
||||
loadbalancer = client.load_balancers.create(
|
||||
name= f"{PREFIX}-loadbalancer",
|
||||
load_balancer_type=LoadBalancerType(name="lb11"),
|
||||
location=Location("hel1"),
|
||||
targets=loadbalancer_targets,
|
||||
public_interface=True,
|
||||
services=[
|
||||
LoadBalancerService(
|
||||
protocol="http",
|
||||
listen_port=8080,
|
||||
destination_port=8080,
|
||||
listen_port=80,
|
||||
destination_port=80,
|
||||
proxyprotocol=False,
|
||||
health_check=LoadBalancerHealthCheck(
|
||||
protocol="http",
|
||||
port="8080",
|
||||
port=80,
|
||||
interval=15,
|
||||
timeout=10,
|
||||
retries=3,
|
||||
@ -88,17 +113,18 @@ try:
|
||||
tls=False
|
||||
)
|
||||
),
|
||||
)
|
||||
http=LoadBalancerServiceHttp(
|
||||
cookie_name='HCLBSTICKY',
|
||||
cookie_lifetime=300,
|
||||
sticky_sessions=True,
|
||||
certificates=[],
|
||||
),
|
||||
),
|
||||
],
|
||||
targets=loadbalancer_targets,
|
||||
public_interface=True,
|
||||
network=vnet
|
||||
)
|
||||
except:
|
||||
loadbalancer = client.load_balancers.get_by_name(f"{PREFIX}-loadbalancer")
|
||||
|
||||
loadbalancer.action.wait_until_finished()
|
||||
print(f"Loadbalancer creating...: {loadbalancer.action.complete}")
|
||||
|
||||
load_balancer = client.load_balancers.get_by_name(f"{PREFIX}-loadbalancer")
|
||||
print(f"Webserwis: http://{load_balancer.data_model.public_net.ipv4.ip}:8080/factors/")
|
||||
print(f"Webserwis: http://{load_balancer.data_model.public_net.ipv4.ip}:80/factors/")
|
||||
|
@ -32,6 +32,20 @@ except:
|
||||
)
|
||||
print(f"Network in use: {vnet.data_model.name} ({vnet.data_model.ip_range})")
|
||||
|
||||
try:
|
||||
shared_volume = client.volumes.create(
|
||||
size=15,
|
||||
name="gitea-volume-s444498",
|
||||
location=Location("hel1"))
|
||||
shared_volume.action.wait_until_finished()
|
||||
print(f"Created volume: {shared_volume.volume}")
|
||||
except:
|
||||
shared_volume = client.volumes.get_by_name("gitea-volume-s444498")
|
||||
shared_volume.action.wait_until_finished()
|
||||
print(f"Volume in use: {shared_volume.volume}")
|
||||
|
||||
volume_device = shared_volume.volume.linux_device
|
||||
|
||||
cloud_init_mysql=r'''#cloud-config
|
||||
packages:
|
||||
- apt-transport-https
|
||||
@ -89,7 +103,7 @@ mysql_server = client.servers.create(
|
||||
)
|
||||
|
||||
mysql_server.action.wait_until_finished()
|
||||
print(f"Creating gitea server: {mysql_server.action.complete}")
|
||||
print(f"Creating mysql server: {mysql_server.action.complete}")
|
||||
|
||||
time.sleep(20)
|
||||
|
||||
@ -131,7 +145,7 @@ write_files:
|
||||
- ./config:/root/gitea/config
|
||||
- /etc/timezone:/etc/timezone:ro
|
||||
- /etc/localtime:/etc/localtime:ro
|
||||
- /mnt/volume:/data
|
||||
- /mnt/data_gitea:/data
|
||||
ports:
|
||||
- "3000:3000"
|
||||
- "222:22"
|
||||
@ -146,9 +160,13 @@ runcmd:
|
||||
- chmod +x /usr/local/bin/docker-compose
|
||||
- systemctl start docker
|
||||
- systemctl enable docker
|
||||
- cd /mnt/
|
||||
- mkdir data_gitea
|
||||
- cd /root/
|
||||
- IP=$(hostname -I | cut -d ' ' -f 1)
|
||||
- echo "DOMAIN=$IP" >> .env
|
||||
- sudo mkfs.xfs -f {volume_device}
|
||||
- mount -o discard,defaults {volume_device} /mnt/data_gitea
|
||||
- docker-compose up -d
|
||||
'''
|
||||
|
||||
@ -161,7 +179,9 @@ gitea_server = client.servers.create(
|
||||
ssh_keys=[ssh_key],
|
||||
networks=[vnet],
|
||||
location=Location("hel1"),
|
||||
user_data=cloud_init_gitea
|
||||
user_data=cloud_init_gitea,
|
||||
volumes=[shared_volume.volume],
|
||||
automount=True
|
||||
)
|
||||
|
||||
gitea_server.action.wait_until_finished()
|
||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user