Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ton chart first version #320

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions dysnix/ton/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
28 changes: 28 additions & 0 deletions dysnix/ton/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apiVersion: v2
name: ton
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.0.1

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.16.0"

maintainers:
- name: Tony
email: [email protected]
37 changes: 37 additions & 0 deletions dysnix/ton/files/api_healthcheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

import socket
import requests
import sys

def is_port_open(host, port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.connect((host, port))
return True
except (socket.timeout, ConnectionRefusedError):
return False

def check_url(url):
try:
response = requests.get(url)
return response.status_code == 200
except requests.RequestException:
return False

def main():
host = '127.0.0.1'
port = 8081
url = f'http://{host}:{port}/getMasterchainInfo'

if not is_port_open(host, port):
print(f"Port {port} on {host} is not open.")
sys.exit(1)

if not check_url(url):
print(f"URL {url} did not return status code 200.")
sys.exit(1)

print(f"Port {port} on {host} is open and URL {url} returned status code 200.")

if __name__ == "__main__":
main()
97 changes: 97 additions & 0 deletions dysnix/ton/files/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@

import json
import os
from time import sleep

TON_ROOT = os.getenv('TON_ROOT', '/var/ton-work/db')
ETC_ROOT=f"{TON_ROOT}/etc"
GLOBAL_CONFIG=f"{ETC_ROOT}/global-config.json"
API_GLOBAL_CONFIG=f"{ETC_ROOT}/api-global-config.json"
CERT_ROOT=f"{ETC_ROOT}/node_certs"
NODE_CONFIG=f"{TON_ROOT}/config.json"
NODE_CONFIG_OUT=f"{TON_ROOT}/config.json"

if not os.path.exists(f"{CERT_ROOT}/json_liteserver"):
print("Waiting for json_liteserver")
sleep(2)

if os.path.exists(NODE_CONFIG):
with open(NODE_CONFIG, 'r') as f:
config = json.load(f)

# TON NODE CONFIG GENERATION
cert_files = [f for f in os.listdir(CERT_ROOT)]
if "json_server" in cert_files:
server_cert: dict = json.load(open(f"{CERT_ROOT}/json_server"))
else:
raise Exception("json_server not found")

if "json_client" in cert_files:
client_cert: dict = json.load(open(f"{CERT_ROOT}/json_client"))
else:
raise Exception("json_client not found")

if "json_liteserver" in cert_files:
liteserver_cert: dict = json.load(open(f"{CERT_ROOT}/json_liteserver"))
else:
raise Exception("json_liteserver not found")

config['control'] = [{
"id": server_cert.get('PUB'),
"port": int(os.getenv('CONSOLER_PORT', "30001")),
"allowed": [
{
"id": client_cert.get('PUB'),
"permissions": 15
}
]
}
]
config['liteservers'] = [
{
"id": liteserver_cert.get('PUB'),
"port": int(os.getenv('LITESERVER_PORT', "43679"))
}
]

with open(NODE_CONFIG_OUT, 'w') as f:
json.dump(config, f, indent=4)

# HERE PART FOR TON-HTTP-API
# Need get global-config.json and change liteserver only to my node
# This is 127.0.0.1 + port 43679

# Make ip
# CERT str(codecs.encode(lite_cert_read, "base64")).replace("\n", "")[2:46]

import socket
import struct
import codecs
ip_local = "127.0.0.1"
if "liteserver.pub" in cert_files:
f = open(f"{CERT_ROOT}/liteserver.pub", "rb+")
lite_cert_read = f.read()[4:]
else:
raise Exception("liteserver.pub not found")
liteserver = [
{
"ip": struct.unpack('>i', socket.inet_aton(ip_local))[0],
"port": int(os.getenv('LITESERVER_PORT', "43679")),
"id": {
"@type": "pub.ed25519",
"key": str(codecs.encode(lite_cert_read, "base64").decode("utf-8").replace("\n", ""))
}
}
]

if os.path.exists(GLOBAL_CONFIG):
with open(GLOBAL_CONFIG, 'r') as f:
global_config = json.load(f)
else:
raise Exception("global-config.json not found")

global_config['liteservers'] = liteserver

with open(API_GLOBAL_CONFIG, 'w') as f:
json.dump(global_config, f, indent=4)

63 changes: 63 additions & 0 deletions dysnix/ton/files/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

#!/bin/sh
#
# TON_ROOT="/var/ton-work/db"
# CONSOLE_PORT="30001"

ETC_ROOT="$TON_ROOT/etc"
LOG_ROOT="$TON_ROOT/log"
CERT_ROOT="$ETC_ROOT/node_certs"
MAINNET_CONFIG_URL="https://ton.org/global-config.json"
MAINNET_CONFIG="$ETC_ROOT/global-config.json"
NODE_CONFIG="$TON_ROOT/config.json"
KEYRING_ROOT="$TON_ROOT/keyring"
# Function for execute generate-random-id in specific directory
generate_random_id() {
local mode=$1
local key_name=$2
local mv
if [ ! -f "${CERT_ROOT}/${key_name}" ]; then
cd $CERT_ROOT
read -r PRI PUB <<< $(generate-random-id -m $mode -n $key_name)
echo "{\"PRI\": \"${PRI}\",\"PUB\": \"${PUB}\"}" > "${CERT_ROOT}/json_${key_name}"
if [ $key_name == "server" ]; then
cp "${CERT_ROOT}/${key_name}" "${KEYRING_ROOT}/${PRI}"
fi
if [ $key_name == "liteserver" ]; then
cp "${CERT_ROOT}/${key_name}" "${KEYRING_ROOT}/${PRI}"
fi
else
echo -e "##### Found existing ${key_name} keys, skipping"
fi
}

mkdir -p $ETC_ROOT
mkdir -p $KEYRING_ROOT
mkdir -p $CERT_ROOT

# GET IP
PUBLIC_IP=$(curl -s ifconfig.me)

# DOWNLOAD MAINNET CONFIG

if [ -f $MAINNET_CONFIG ]; then
echo -e "##### Found existing global config, skipping"
else
echo -e "##### Downloading provided global config."
wget -q $MAINNET_CONFIG_URL -O $MAINNET_CONFIG
fi

# GENERATE CONFIG
if [ -f $NODE_CONFIG ]; then
echo -e "##### Found existing local config, skipping"
else
echo -e "##### Using provided IP: $PUBLIC_IP:$CONSOLE_PORT"
validator-engine -C $MAINNET_CONFIG --db $TON_ROOT --ip "$PUBLIC_IP:$CONSOLE_PORT"
fi



# GENERATE CERTIFICATES
generate_random_id "keys" "server"
generate_random_id "keys" "client"
generate_random_id "keys" "liteserver"
32 changes: 32 additions & 0 deletions dysnix/ton/files/node_healthcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
# TON_ROOT="/var/ton-work/db"
# CONSOLE_PORT="30001"

ETC_ROOT="$TON_ROOT/etc"
LOG_ROOT="$TON_ROOT/log"
CERT_ROOT="$ETC_ROOT/node_certs"

SERVER_PUB="${CERT_ROOT}/server.pub"
CLIENT_CERT="${CERT_ROOT}/client"

if [ ! -f "${SERVER_PUB}" ]; then
echo "Server keys not found, exiting"
exit 1
fi

if [ ! -f "${CLIENT_CERT}" ]; then
echo "Client keys not found, exiting"
exit 1
fi

server_time=`validator-engine-console -p $SERVER_PUB -k $CLIENT_CERT -a 127.0.0.1:30001 -c getstats | grep unixtime | awk '{print $2}'`
current_time=`date +%s`
time_diff=$((current_time - server_time))
echo "Time difference: $time_diff"

if [ $time_diff -gt 2 ]; then
echo "Time difference is greater than 2 seconds, exiting"
exit 1
fi

exit 0
22 changes: 22 additions & 0 deletions dysnix/ton/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
1. Get the application URL by running these commands:
{{- if .Values.ingress.enabled }}
{{- range $host := .Values.ingress.hosts }}
{{- range .paths }}
http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }}
{{- end }}
{{- end }}
{{- else if contains "NodePort" .Values.service.type }}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "ton.fullname" . }})
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
{{- else if contains "LoadBalancer" .Values.service.type }}
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "ton.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "ton.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}")
echo http://$SERVICE_IP:{{ .Values.service.port }}
{{- else if contains "ClusterIP" .Values.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "ton.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT
{{- end }}
62 changes: 62 additions & 0 deletions dysnix/ton/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "ton.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "ton.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "ton.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "ton.labels" -}}
helm.sh/chart: {{ include "ton.chart" . }}
{{ include "ton.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "ton.selectorLabels" -}}
app.kubernetes.io/name: {{ include "ton.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "ton.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "ton.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
Loading
Loading