Readme update
This commit is contained in:
parent
0e5dea34d5
commit
448d2d5209
@ -1 +1,3 @@
|
|||||||
Init
|
1. Instalacja paczek komendą "pip3 install -r requirements.txt"
|
||||||
|
2. Uzupełnienie pliku config_base.json wartościami token i ssh_public
|
||||||
|
3. Uruchomienie skryptu komendą "python3 deploy.py config_base.json"
|
13
config_base.json
Normal file
13
config_base.json
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"ssh_name": "s434784",
|
||||||
|
"network": "ms-network-1",
|
||||||
|
"network-ip": "10.10.10.0/24",
|
||||||
|
"volume": "ms-volume",
|
||||||
|
"db-server-name": "ms-gitea-db",
|
||||||
|
"gitea-server-name": "ms-gitea",
|
||||||
|
"server-type": "cx11",
|
||||||
|
"server-image": "ubuntu-20.04",
|
||||||
|
"location": "hel1",
|
||||||
|
"token": "<FILL>",
|
||||||
|
"ssh_public": "<FILL>"
|
||||||
|
}
|
51
deploy.py
51
deploy.py
@ -1,5 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
import sys
|
||||||
from hcloud import Client
|
from hcloud import Client
|
||||||
from hcloud.networks.domain import NetworkSubnet
|
from hcloud.networks.domain import NetworkSubnet
|
||||||
from hcloud.locations.domain import Location
|
from hcloud.locations.domain import Location
|
||||||
@ -8,22 +9,28 @@ from hcloud.server_types.domain import ServerType
|
|||||||
|
|
||||||
|
|
||||||
class Client_MS:
|
class Client_MS:
|
||||||
def __init__(self) -> None:
|
def __init__(self, json_config) -> None:
|
||||||
self.config = self.load_config()
|
self.config = self.load_config(json_config)
|
||||||
self.client = Client(token=self.config["token"])
|
|
||||||
|
self.client = self.get_client()
|
||||||
|
|
||||||
self.ssh_key = self.get_ssh()
|
self.ssh_key = self.get_ssh()
|
||||||
self.network = self.create_network()
|
self.network = self.create_network()
|
||||||
self.volume = self.create_volume()
|
self.volume = self.create_volume()
|
||||||
self.server_db = None
|
|
||||||
self.server_gitea = None
|
|
||||||
|
|
||||||
servers = self.client.servers.get_all()
|
self.server_db = self.create_db_server()
|
||||||
print(servers[0])
|
self.server_gitea = self.create_gitea_server()
|
||||||
|
|
||||||
def load_config(self):
|
def load_config(self, json_config):
|
||||||
with open('config.json') as json_file:
|
with open(json_config) as json_file:
|
||||||
return json.load(json_file)
|
return json.load(json_file)
|
||||||
|
|
||||||
|
def get_client(self):
|
||||||
|
try:
|
||||||
|
return Client(token=self.config["token"])
|
||||||
|
except:
|
||||||
|
return None
|
||||||
|
|
||||||
def get_ssh(self):
|
def get_ssh(self):
|
||||||
if(self.client.ssh_keys.get_by_name(self.config["ssh_name"])):
|
if(self.client.ssh_keys.get_by_name(self.config["ssh_name"])):
|
||||||
print("SSH_KEY: Key exists.")
|
print("SSH_KEY: Key exists.")
|
||||||
@ -50,11 +57,15 @@ class Client_MS:
|
|||||||
def create_volume(self):
|
def create_volume(self):
|
||||||
if(self.client.volumes.get_by_name(self.config["volume"])):
|
if(self.client.volumes.get_by_name(self.config["volume"])):
|
||||||
print("VOLUME: Volume exists.")
|
print("VOLUME: Volume exists.")
|
||||||
return self.client.volumes.get_by_name(self.config["volume"])
|
volume = self.client.volumes.get_by_name(self.config["volume"])
|
||||||
|
return volume
|
||||||
else:
|
else:
|
||||||
print("VOLUME: Creating volume.")
|
print("VOLUME: Creating volume.")
|
||||||
return self.client.volumes.create(
|
volume = self.client.volumes.create(
|
||||||
size=10, name=self.config["volume"], location=Location(id=self.config["location"]))
|
size=10, name=self.config["volume"], location=Location(id=self.config["location"]))
|
||||||
|
volume.action.wait_until_finished()
|
||||||
|
time.sleep(5)
|
||||||
|
return volume
|
||||||
|
|
||||||
def create_db_server(self):
|
def create_db_server(self):
|
||||||
with open("db.yml", "r") as f:
|
with open("db.yml", "r") as f:
|
||||||
@ -87,6 +98,7 @@ class Client_MS:
|
|||||||
self.config["gitea-server-name"])
|
self.config["gitea-server-name"])
|
||||||
return self.server_gitea
|
return self.server_gitea
|
||||||
else:
|
else:
|
||||||
|
print("GITEA: Creating server.")
|
||||||
self.server_gitea = self.client.servers.create(
|
self.server_gitea = self.client.servers.create(
|
||||||
name=self.config["gitea-server-name"],
|
name=self.config["gitea-server-name"],
|
||||||
server_type=ServerType(self.config["server-type"]),
|
server_type=ServerType(self.config["server-type"]),
|
||||||
@ -102,7 +114,8 @@ class Client_MS:
|
|||||||
def delete_all(self):
|
def delete_all(self):
|
||||||
self.network.delete()
|
self.network.delete()
|
||||||
print("NETWORK: Deleted.")
|
print("NETWORK: Deleted.")
|
||||||
self.volume.detach()
|
w = self.volume.detach()
|
||||||
|
w.wait_until_finished()
|
||||||
self.volume.delete()
|
self.volume.delete()
|
||||||
print("VOLUME: Deleted.")
|
print("VOLUME: Deleted.")
|
||||||
self.server_db.delete()
|
self.server_db.delete()
|
||||||
@ -111,12 +124,10 @@ class Client_MS:
|
|||||||
print("GITEA: Deleted.")
|
print("GITEA: Deleted.")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
c = Client_MS()
|
|
||||||
c.create_db_server()
|
|
||||||
c.create_gitea_server()
|
|
||||||
# c.delete_all()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
if(len(sys.argv) != 2):
|
||||||
|
print("Podaj ścieżkę do pliku konfiguracyjnego.")
|
||||||
|
else:
|
||||||
|
c = Client_MS(sys.argv[1])
|
||||||
|
# time.sleep(10)
|
||||||
|
# c.delete_all()
|
||||||
|
Loading…
Reference in New Issue
Block a user