diff --git a/.gitignore b/.gitignore index dbe9c82..3c5cb9b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,36 @@ -.vscode/ \ No newline at end of file +.vscode/ + +# Local .terraform directories +**/.terraform/* + +# .tfstate files +*.tfstate +*.tfstate.* +.terraform.lock.hcl +# Crash log files +crash.log +crash.*.log + +# Exclude all .tfvars files, which are likely to contain sensitive data, such as +# password, private keys, and other secrets. These should not be part of version +# control as they are data points which are potentially sensitive and subject +# to change depending on the environment. +*.tfvars +*.tfvars.json + +# Ignore override files as they are usually used to override resources locally and so +# are not checked in +override.tf +override.tf.json +*_override.tf +*_override.tf.json + +# Include override files you do wish to add to version control using negated pattern +# !example_override.tf + +# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan +# example: *tfplan* + +# Ignore CLI configuration files +.terraformrc +terraform.rc \ No newline at end of file diff --git a/README.md b/README.md index cad4b86..6d96b59 100644 --- a/README.md +++ b/README.md @@ -42,4 +42,11 @@ Right now I'm using redis for nothing. My instnace of [Twitch-Channel-Points-Miner-v2](https://github.com/rdavydov/Twitch-Channel-Points-Miner-v2) # What I host in hetzner? -At hetzner I "share" some personal services with my friends projetcs. But the basic infrascructure is running proxmox with NAT for an instance of pfSense for router. A traefik in a LXC for reverse proxy. And a nomad server with some jobs for running containers. \ No newline at end of file +At hetzner I "share" some personal services with my friends projetcs. But the basic infrascructure is running proxmox with NAT for an instance of pfSense for router. A traefik in a LXC for reverse proxy. And a nomad server with some jobs for running containers. + +## Setup terraform: + +``` +read -s PGPASSWORD +export PGPASSWORD +``` \ No newline at end of file diff --git a/terraform-home/.gitignore b/terraform-home/.gitignore deleted file mode 100644 index 3935c70..0000000 --- a/terraform-home/.gitignore +++ /dev/null @@ -1,34 +0,0 @@ -# Local .terraform directories -**/.terraform/* - -# .tfstate files -*.tfstate -*.tfstate.* -.terraform.lock.hcl -# Crash log files -crash.log -crash.*.log - -# Exclude all .tfvars files, which are likely to contain sensitive data, such as -# password, private keys, and other secrets. These should not be part of version -# control as they are data points which are potentially sensitive and subject -# to change depending on the environment. -*.tfvars -*.tfvars.json - -# Ignore override files as they are usually used to override resources locally and so -# are not checked in -override.tf -override.tf.json -*_override.tf -*_override.tf.json - -# Include override files you do wish to add to version control using negated pattern -# !example_override.tf - -# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan -# example: *tfplan* - -# Ignore CLI configuration files -.terraformrc -terraform.rc \ No newline at end of file diff --git a/terraform-lab/main.tf b/terraform-lab/main.tf new file mode 100644 index 0000000..01a15ff --- /dev/null +++ b/terraform-lab/main.tf @@ -0,0 +1,14 @@ +module "freqtrade" { + source = "./modules/freqtrade" + providers = { + nomad = nomad + } + exchange_key = var.exchange_key + exchange_secret = var.exchange_secret + exchange_password = var.exchange_password + telegram_token = var.telegram_token + telegram_chat_id = var.telegram_chat_id + jwt_secret_key = var.jwt_secret_key + username = var.username + password = var.password +} diff --git a/terraform-lab/modules/freqtrade/conf/config.json.tpl b/terraform-lab/modules/freqtrade/conf/config.json.tpl new file mode 100644 index 0000000..ab950ae --- /dev/null +++ b/terraform-lab/modules/freqtrade/conf/config.json.tpl @@ -0,0 +1,76 @@ +{ + "$schema": "https://schema.freqtrade.io/schema.json", + "max_open_trades": 3, + "stake_currency": "USDT", + "stake_amount": "unlimited", + "tradable_balance_ratio": 0.99, + "fiat_display_currency": "USD", + "dry_run": true, + "dry_run_wallet": 1000, + "cancel_open_orders_on_exit": false, + "trading_mode": "spot", + "margin_mode": "", + "unfilledtimeout": { + "entry": 10, + "exit": 10, + "exit_timeout_count": 0, + "unit": "minutes" + }, + "entry_pricing": { + "price_side": "same", + "use_order_book": true, + "order_book_top": 1, + "price_last_balance": 0.0, + "check_depth_of_market": { + "enabled": false, + "bids_to_ask_delta": 1 + } + }, + "exit_pricing":{ + "price_side": "same", + "use_order_book": true, + "order_book_top": 1 + }, + "exchange": { + "name": "kucoin", + "key": "${exchange_key}", + "secret": "${exchange_secret}", + "password": "${exchange_password}", + "ccxt_config": {}, + "ccxt_async_config": {}, + "pair_whitelist": [], + "pair_blacklist": [] + }, + "pairlists": [ + { + "method": "VolumePairList", + "number_assets": 20, + "sort_key": "quoteVolume", + "min_value": 0, + "refresh_period": 1800 + } + ], + "telegram": { + "enabled": false, + "token": "${telegram_token}", + "chat_id": "${telegram_chat_id}" + }, + "api_server": { + "enabled": true, + "listen_ip_address": "0.0.0.0", + "listen_port": 8080, + "verbosity": "error", + "enable_openapi": false, + "jwt_secret_key": "${jwt_secret_key}", + "ws_token": "gp5SbWEjm4IMSU6uKKw7tVtcWt6tgWJsug", + "CORS_origins": [], + "username": "${username}", + "password": "${password}" + }, + "bot_name": "freqtrade", + "initial_state": "running", + "force_entry_enable": false, + "internals": { + "process_throttle_secs": 5 + } +} \ No newline at end of file diff --git a/terraform-lab/modules/freqtrade/conf/freqtrade.hcl b/terraform-lab/modules/freqtrade/conf/freqtrade.hcl new file mode 100644 index 0000000..ca56179 --- /dev/null +++ b/terraform-lab/modules/freqtrade/conf/freqtrade.hcl @@ -0,0 +1,88 @@ +job "freqtrade" { + datacenters = ["marioverde"] + type = "service" + + group "freqtrade" { + count = 1 + + network { + port "api" { + static = 9696 + to = 8080 + } + } + + restart { + attempts = 3 + interval = "2m" + delay = "30s" + mode = "delay" + } + + ephemeral_disk { + migrate = true + sticky = true + size = 500 + } + + task "freqtrade" { + driver = "podman" + + config { + image = "docker.io/freqtradeorg/freqtrade:stable" + ports = ["api"] + + volumes = [ + "${NOMAD_ALLOC_DIR}:/freqtrade/user_data/" + ] + + args = [ + "trade", + "--logfile", "/freqtrade/user_data/logs/freqtrade.log", + "--db-url", "sqlite:////freqtrade/user_data/tradesv3.sqlite", + "--config", "/freqtrade/user_data/config.json", + "--strategy", "MyStrategy" + ] + } + + template { + data = < DataFrame: + # generate values for technical analysis indicators + dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14) + + return dataframe + + def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + # generate entry signals based on indicator values + dataframe.loc[ + (dataframe['rsi'] < 30), + 'enter_long'] = 1 + + return dataframe + + def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: + # generate exit signals based on indicator values + dataframe.loc[ + (dataframe['rsi'] > 70), + 'exit_long'] = 1 + + return dataframe \ No newline at end of file diff --git a/terraform-lab/modules/freqtrade/job.tf b/terraform-lab/modules/freqtrade/job.tf new file mode 100644 index 0000000..d7800a3 --- /dev/null +++ b/terraform-lab/modules/freqtrade/job.tf @@ -0,0 +1,46 @@ +resource "nomad_job" "app" { + jobspec = templatefile("${path.module}/conf/freqtrade.hcl", { + NOMAD_ALLOC_DIR = "./private", + CONFIG_JSON = templatefile("${path.module}/conf/config.json.tpl", { + exchange_key = var.exchange_key, + exchange_secret = var.exchange_secret, + exchange_password = var.exchange_password, + telegram_token = var.telegram_token, + telegram_chat_id = var.telegram_chat_id + }) + STRATEGY_NAME = file("${path.module}/conf/strategy.py") + }) + +} + +variable "exchange_key" { + type = string +} + +variable "exchange_secret" { + type = string +} + +variable "exchange_password" { + type = string +} + +variable "telegram_token" { + type = string +} + +variable "telegram_chat_id" { + type = string +} + +variable "jwt_secret_key" { + type = string +} + +variable "username" { + type = string +} + +variable "password" { + type = string +} diff --git a/terraform-lab/modules/freqtrade/providers.tf b/terraform-lab/modules/freqtrade/providers.tf new file mode 100644 index 0000000..9744cce --- /dev/null +++ b/terraform-lab/modules/freqtrade/providers.tf @@ -0,0 +1,9 @@ +terraform { + required_providers { + nomad = { + source = "hashicorp/nomad" + version = "2.4.0" + } + } + required_version = ">= 0.14" +} \ No newline at end of file diff --git a/terraform-lab/providers.tf b/terraform-lab/providers.tf new file mode 100644 index 0000000..9c6c07f --- /dev/null +++ b/terraform-lab/providers.tf @@ -0,0 +1,29 @@ + +terraform { + required_providers { + nomad = { + source = "hashicorp/nomad" + version = "2.4.0" + } + } + backend "pg" { + conn_str = "postgres://postgres@100.64.0.7:5432/terraform_backend" + #conn_str = "postgres://postgres@192.168.5.4:5432/terraform_backend" + } + required_version = ">= 0.14" +} + +/* + +VOCE PRECISA FAZER UM TUNEL PRA ACESSAR O SQL DO LAB. E TESTAR O INIT DO TERRAFORM. BOLAR UM JEITO DE SEMPRE RODAR SAPORRA DE TF DO LAB + +PASSWORD: +read -s PGPASSWORD +export PGPASSWORD +*/ + + +# Configure the Nomad provider. +provider "nomad" { + address = "http://100.64.0.5:4646" +} diff --git a/terraform-lab/variables.tf b/terraform-lab/variables.tf new file mode 100644 index 0000000..557ced6 --- /dev/null +++ b/terraform-lab/variables.tf @@ -0,0 +1,31 @@ +variable "exchange_key" { + type = string +} + +variable "exchange_secret" { + type = string +} + +variable "exchange_password" { + type = string +} + +variable "telegram_token" { + type = string +} + +variable "telegram_chat_id" { + type = string +} + +variable "jwt_secret_key" { + type = string +} + +variable "username" { + type = string +} + +variable "password" { + type = string +} \ No newline at end of file