examples: DEVfromGround vc_org, NODEJS, VM, POSTGRES updates

This commit is contained in:
Naeel
2026-03-26 08:33:03 +03:00
parent 2b03ba520e
commit 547994dc55
11 changed files with 216 additions and 217 deletions
+2
View File
@@ -69,3 +69,5 @@ event-dispatcher
# build artifacts
/sless
examples/POSTGRES/stress_log*.txt
examples/VM/vm_key
examples/VM/vm_key.pub
+56
View File
@@ -4,6 +4,62 @@
---
## 2026-03-26 — БАГ ГЕНЕРАТОРА: modify-only поля помечаются Required в schema ресурса
```
Error: Missing required argument
on vc_org.tf line 5, in resource "nubes_vc_org" "dev_org":
5: resource "nubes_vc_org" "dev_org" {
The argument "v_i_p_configure" is required, but no definition was found.
The argument "resource_name" is required, but no definition was found.
```
### Причина
Генератор (`~/terra/terraform/devops/`) при создании Go-кода ресурса (`19_vc_org_resource.go`)
помечает **все операции ресурса** как `Required` в схеме, включая поля,
которые нужны только для операции `modify` (не для `create`).
Конкретный пример: `vIPConfigure` (код параметра 662) — это поле операции `modify`,
но попадает в schema с `Required: true`:
```go
"v_i_p_configure": schema.StringAttribute{Required: true},
```
В YAML-описании сервиса (19_vc_org.yaml) `vIPConfigure` объявлен только под
`operations.modify.params`, а не под `operations.create.params`.
### Что нужно исправить в генераторе
В `~/terra/terraform/devops/` (файлы `02_generate_resources_and_docs*.go/sh`):
Поля, принадлежащие только операции `modify` (или другим не-create операциям),
должны генерироваться как **`Optional: true, Computed: true`**, а не `Required: true`.
Логика:
- поле в `operations[create].params``Required: true`
- поле только в `operations[modify].params``Optional: true, Computed: true`
- поле только в state (read-only) → `Computed: true`
### Временный workaround (действующий)
В `terraform.tf`-манифесте указывать пустую строку:
```hcl
v_i_p_configure = "" # modify-only поле; при create не отправляется в API
```
Провайдер при Create не передаёт это поле в API (строка 418/556 params),
но schema.Required требует non-null значение в плане.
### Файлы для правки
- `~/terra/terraform/devops/profiles/test/generated/go/19_vc_org_resource.go` — сгенерированный, не менять вручную
- **Править нужно шаблоны/генераторы** в `~/terra/terraform/devops/`
---
## 2026-03-22 — БАГ: CreateService/CreateFunction возвращает 409 при `terraform apply -replace` (ИСПРАВЛЕН)
### Симптом
+1 -1
View File
@@ -24,5 +24,5 @@ variable "resource_realm" {
provider "nubes" {
api_token = var.api_token
api_endpoint = "https://deck-api-dev.ngcloud.ru/api/v1"
api_endpoint = "https://deck-api-dev.ngcloud.ru/api/v1/index.cfm"
}
+7 -3
View File
@@ -3,15 +3,19 @@
// resource_realm задаётся через переменную (terraform.tfvars или -var).
resource "nubes_vc_org" "dev_org" {
resource_name = "vcOrg-2"
resource_realm = var.resource_realm
# organization_type "iaas" — единственный вариант с доступом к организации.
# Значение по умолчанию "iaas", явно прописано для читаемости.
organization_type = "iaas"
# adopt_existing_on_create = true позволяет взять существующий инстанс под управление.
# Для DEV-стенда оставляем false — при apply создаётся новый; ошибка если уже есть.
adopt_existing_on_create = false
# v_i_p_configure — JSON-список ipSpaces для операции modify.
# При create провайдер не передаёт его в API, но требует non-null значение в плане.
v_i_p_configure = ""
# adopt_existing_on_create = true — берёт существующий инстанс (dev-org-sless-demo уже создан с null realm от предыдущей попытки).
adopt_existing_on_create = true
# suspend_on_destroy = true (по умолчанию) — при destroy инстанс уходит в Suspend, не удаляется.
suspend_on_destroy = true
+32
View File
@@ -0,0 +1,32 @@
// Создано: 2026-03-23
// main.tf — провайдер Nubes + переменные для примера NODEJS.
// Ресурс nubes_nodejs: managed Node.js приложение в облаке (не sless-функция).
terraform {
required_providers {
nubes = {
source = "terra.k8c.ru/nubes/nubes"
version = "5.0.19"
}
}
}
variable "api_token" {
type = string
sensitive = true
}
variable "realm" {
type = string
description = "resource_realm — зона размещения ресурса (например: k8s-3-sandbox-nubes-ru)"
}
variable "git_path" {
type = string
description = "URL git-репозитория с кодом приложения"
}
provider "nubes" {
api_token = var.api_token
api_endpoint = "https://deck-api-test.ngcloud.ru/api/v1/index.cfm"
}
+22
View File
@@ -0,0 +1,22 @@
# Создано: 2026-03-23
# nodejs.tf — ресурс nubes_nodejs: managed Node.js приложение.
# Параметры взяты из документации terra.k8c.ru/docs/nubes/nubes/5.0.19/30_registry/resources/nodejs_params_create/
resource "nubes_nodejs" "app" {
resource_name = "nodejsdemo1"
domain = "domma"
resource_realm = var.realm
git_path = var.git_path
app_version = "23"
resource_c_p_u = 500
resource_memory = 1024
resource_instances = 1
json_env = jsonencode({})
adopt_existing_on_create = true
# health_path не задан — используется дефолтный /
}
output "nodejs_domain" {
description = "Домен развёрнутого Node.js приложения"
value = nubes_nodejs.app.domain
}
+4 -111
View File
@@ -1,116 +1,9 @@
// Создано: 2026-04-10
// Простой калькулятор — возвращает HTML страницу с кнопками.
// GET → HTML; POST с {a, op, b} → вычисление с результатом на странице.
// Рантайм nodejs20 v0.1.3+ поддерживает HTML-ответ (строка начинающаяся с '<').
// Демо-функция: возвращает текущее время сервера.
// Юзер меняет код под себя и перебилдит через terraform apply.
'use strict';
const HTML = `<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Калькулятор (Node.js)</title>
<style>
body { font-family: sans-serif; display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; background: #0f2027; }
.calc { background: #1a2a1a; border-radius: 16px; padding: 24px; box-shadow: 0 8px 32px rgba(0,0,0,0.5); width: 280px; }
h2 { color: #d4f7d4; text-align: center; margin: 0 0 16px; font-size: 18px; }
.badge { text-align: center; font-size: 11px; color: #4a7a4a; margin-bottom: 16px; }
.display { background: #0a1a0a; color: #e0ffe0; font-size: 28px; text-align: right; padding: 12px 16px; border-radius: 8px; margin-bottom: 16px; min-height: 52px; word-break: break-all; }
.result { color: #4ade80; font-size: 20px; text-align: right; padding: 4px 16px; margin-bottom: 8px; min-height: 28px; }
.btns { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
button { border: none; border-radius: 8px; padding: 16px 0; font-size: 18px; cursor: pointer; transition: filter 0.1s; }
button:active { filter: brightness(1.3); }
.btn-num { background: #1e3a1e; color: #d4f7d4; }
.btn-op { background: #166534; color: #fff; }
.btn-eq { background: #15803d; color: #fff; grid-column: span 2; }
.btn-clr { background: #7f1d1d; color: #fca5a5; grid-column: span 2; }
.btn-zero { grid-column: span 2; }
</style>
</head>
<body>
<div class="calc">
<h2>Калькулятор</h2>
<div class="badge">Node.js 20</div>
<div id="display" class="display">0</div>
<div id="result" class="result">RESULT_LINE</div>
<div class="btns">
<button class="btn-clr btn-zero" onclick="clr()">C</button>
<button class="btn-op" onclick="setOp('%2F')">÷</button>
<button class="btn-op" onclick="setOp('*')">×</button>
<button class="btn-num" onclick="inp('7')">7</button>
<button class="btn-num" onclick="inp('8')">8</button>
<button class="btn-num" onclick="inp('9')">9</button>
<button class="btn-op" onclick="setOp('-')"></button>
<button class="btn-num" onclick="inp('4')">4</button>
<button class="btn-num" onclick="inp('5')">5</button>
<button class="btn-num" onclick="inp('6')">6</button>
<button class="btn-op" onclick="setOp('+')">+</button>
<button class="btn-num" onclick="inp('1')">1</button>
<button class="btn-num" onclick="inp('2')">2</button>
<button class="btn-num" onclick="inp('3')">3</button>
<button class="btn-num btn-zero" onclick="inp('0')">0</button>
<button class="btn-num" onclick="inp('.')">.</button>
<button class="btn-eq" onclick="calc()">=</button>
</div>
</div>
<script>
let a = '', op = '', b = '';
const disp = document.getElementById('display');
function inp(v) {
if (op === '') { a += v; disp.textContent = a || '0'; }
else { b += v; disp.textContent = b || '0'; }
}
function setOp(o) {
if (a === '') return;
if (b !== '') calc();
else { op = o; disp.textContent = a + ' ' + decodeURIComponent(o) + ' …'; }
}
function clr() { a = ''; op = ''; b = ''; disp.textContent = '0'; }
function calc() {
if (a === '' || op === '' || b === '') return;
const form = document.createElement('form');
form.method = 'POST'; form.action = '';
const fields = {a, op, b};
for (const [k, v] of Object.entries(fields)) {
const i = document.createElement('input');
i.type = 'hidden'; i.name = k; i.value = decodeURIComponent(v);
form.appendChild(i);
}
document.body.appendChild(form);
form.submit();
}
</script>
</body>
</html>`;
// Форматирует число — убирает лишний .0 для целых
function fmt(n) { return n === Math.trunc(n) ? String(Math.trunc(n)) : String(n); }
function buildPage(resultLine) {
return HTML.replace('RESULT_LINE', resultLine || '');
}
function compute(event) {
// POST: тело приходит как JSON; поля a, op, b — строки из form
const a = parseFloat(event.a);
const b = parseFloat(event.b);
const op = event.op;
if (isNaN(a) || isNaN(b)) return buildPage('Некорректное число');
let res;
switch (op) {
case '+': res = a + b; break;
case '-': res = a - b; break;
case '*': res = a * b; break;
case '/':
if (b === 0) return buildPage('Деление на ноль');
res = a / b; break;
default: return buildPage('Неизвестный оператор');
}
return buildPage(`${fmt(a)} ${op} ${fmt(b)} = ${fmt(res)}`);
}
module.exports.handler = async function handler(event) {
if (event._method === 'POST') return compute(event);
return buildPage('');
module.exports.handler = function handler(event) {
return `Текущее время: ${new Date().toISOString()}`;
};
+83 -98
View File
@@ -1,120 +1,105 @@
# Создано: 2026-04-10
# Простой калькулятор — возвращает HTML страницу с кнопками.
# Роутинг: GET → HTML страница; POST с {a, op, b} → вычисление и редирект с результатом.
# _method приходит от python3.11 рантайма в event.
# Изменено: 2026-03-23 — упрощён до поля ввода выражения (демонстрация деплоя).
# Принимает произвольное математическое выражение: "2+2*(3-1)", "(10/3)**2" и т.д.
# GET → HTML страница с формой; POST с {expr} → вычисление через безопасный eval.
# Безопасность eval: __builtins__=None, только math-функции в locals.
_HTML = """<!DOCTYPE html>
import math
_PAGE = """<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Калькулятор (Python)</title>
<title>Калькулятор Python 3.11</title>
<style>
body { font-family: sans-serif; display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; background: #1a1a2e; }
.calc { background: #16213e; border-radius: 16px; padding: 24px; box-shadow: 0 8px 32px rgba(0,0,0,0.5); width: 280px; }
h2 { color: #e2e8f0; text-align: center; margin: 0 0 16px; font-size: 18px; }
.badge { text-align: center; font-size: 11px; color: #64748b; margin-bottom: 16px; }
.display { background: #0f172a; color: #f1f5f9; font-size: 28px; text-align: right; padding: 12px 16px; border-radius: 8px; margin-bottom: 16px; min-height: 52px; word-break: break-all; }
.result { color: #22d3ee; font-size: 20px; text-align: right; padding: 4px 16px; margin-bottom: 8px; min-height: 28px; }
.btns { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
button { border: none; border-radius: 8px; padding: 16px 0; font-size: 18px; cursor: pointer; transition: filter 0.1s; }
button:active { filter: brightness(1.3); }
.btn-num { background: #334155; color: #f1f5f9; }
.btn-op { background: #0891b2; color: #fff; }
.btn-eq { background: #0d9488; color: #fff; grid-column: span 2; }
.btn-clr { background: #7f1d1d; color: #fca5a5; grid-column: span 2; }
.btn-zero { grid-column: span 2; }
body { font-family: monospace; background: #0f172a; color: #e2e8f0;
display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; }
.box { background: #1e293b; border-radius: 12px; padding: 32px; width: 420px; box-shadow: 0 8px 32px #0005; }
h2 { margin: 0 0 4px; font-size: 20px; color: #7dd3fc; }
.sub { color: #475569; font-size: 12px; margin-bottom: 24px; }
input { width: 100%; box-sizing: border-box; padding: 10px 14px; font-size: 18px; font-family: monospace;
background: #0f172a; border: 1px solid #334155; border-radius: 8px; color: #f1f5f9; outline: none; }
input:focus { border-color: #38bdf8; }
button { margin-top: 12px; width: 100%; padding: 12px; font-size: 16px; background: #0369a1;
color: #fff; border: none; border-radius: 8px; cursor: pointer; }
button:hover { background: #0284c7; }
button:disabled { background: #1e3a5f; color: #475569; cursor: default; }
.result { margin-top: 20px; padding: 14px; border-radius: 8px; font-size: 22px; text-align: center; display: none; }
.ok { background: #064e3b; color: #6ee7b7; display: block; }
.err { background: #450a0a; color: #fca5a5; font-size: 14px; display: block; }
</style>
</head>
<body>
<div class="calc">
<div class="box">
<h2>Калькулятор</h2>
<div class="badge">Python 3.11</div>
<div id="display" class="display">0</div>
<div id="result" class="result">{result_line}</div>
<div class="btns">
<button class="btn-clr btn-zero" onclick="clr()">C</button>
<button class="btn-op" onclick="setOp('%2F')">÷</button>
<button class="btn-op" onclick="setOp('*')">×</button>
<button class="btn-num" onclick="inp('7')">7</button>
<button class="btn-num" onclick="inp('8')">8</button>
<button class="btn-num" onclick="inp('9')">9</button>
<button class="btn-op" onclick="setOp('-')"></button>
<button class="btn-num" onclick="inp('4')">4</button>
<button class="btn-num" onclick="inp('5')">5</button>
<button class="btn-num" onclick="inp('6')">6</button>
<button class="btn-op" onclick="setOp('+')">+</button>
<button class="btn-num" onclick="inp('1')">1</button>
<button class="btn-num" onclick="inp('2')">2</button>
<button class="btn-num" onclick="inp('3')">3</button>
<button class="btn-num btn-zero" onclick="inp('0')">0</button>
<button class="btn-num" onclick="inp('.')">.</button>
<button class="btn-eq" onclick="calc()">=</button>
</div>
<div class="sub">Python 3.11 · runtime: sless</div>
<input id="expr" autofocus placeholder="например: 2 + 2 * (3 - 1)">
<button id="btn" onclick="calc()">Вычислить</button>
<div id="result" class="result"></div>
</div>
<script>
let a = '', op = '', b = '', fresh = {fresh_js};
const disp = document.getElementById('display');
function inp(v) {{
if (op === '') {{ a += v; disp.textContent = a || '0'; }}
else {{ b += v; disp.textContent = b || '0'; }}
fresh = false;
}}
function setOp(o) {{
if (a === '') return;
if (b !== '') calc();
else {{ op = o; disp.textContent = a + ' ' + decodeURIComponent(o) + ''; }}
fresh = false;
}}
function clr() {{ a = ''; op = ''; b = ''; disp.textContent = '0'; }}
function calc() {{
if (a === '' || op === '' || b === '') return;
const form = document.createElement('form');
form.method = 'POST'; form.action = '';
const fields = {{a, op, b}};
for (const [k, v] of Object.entries(fields)) {{
const i = document.createElement('input');
i.type = 'hidden'; i.name = k; i.value = decodeURIComponent(v);
form.appendChild(i);
}}
document.body.appendChild(form);
form.submit();
}}
document.getElementById('expr').addEventListener('keydown', function(e) {
if (e.key === 'Enter') calc();
});
async function calc() {
const expr = document.getElementById('expr').value.trim();
if (!expr) return;
const btn = document.getElementById('btn');
const res = document.getElementById('result');
btn.disabled = true;
btn.textContent = '';
try {
const r = await fetch('', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({expr: expr})
});
const data = await r.json();
if (data.error) {
res.className = 'result err';
res.textContent = data.error;
} else {
res.className = 'result ok';
res.textContent = expr + ' = ' + data.result;
}
} catch(e) {
res.className = 'result err';
res.textContent = 'Ошибка сети: ' + e.message;
}
btn.disabled = false;
btn.textContent = 'Вычислить';
}
</script>
</body>
</html>"""
# Разрешённые math-функции в eval — без __builtins__ нет доступа к exec/open/etc.
_MATH_LOCALS = {k: getattr(math, k) for k in dir(math) if not k.startswith('_')}
def handler(event):
# POST: тело приходит как JSON от рантайма
if event.get('_method') == 'POST':
return _compute(event)
# GET: показываем чистую страницу
return _html()
expr = str(event.get('expr', '')).strip()
return _compute(expr)
# GET → HTML страница
return _PAGE
def _html(result_line='', fresh_js='true'):
return _HTML.replace('{result_line}', result_line).replace('{fresh_js}', fresh_js)
def _compute(event):
def _compute(expr):
if not expr:
return {'error': 'Введите выражение'}
try:
a = float(event.get('a', 0))
b = float(event.get('b', 0))
op = event.get('op', '')
if op == '+':
res = a + b
elif op == '-':
res = a - b
elif op == '*':
res = a * b
elif op == '/':
if b == 0:
return _html('Деление на ноль', 'false')
res = a / b
else:
return _html('Неизвестный оператор', 'false')
# Убираем лишний .0 для целых результатов
res_str = str(int(res)) if res == int(res) else str(res)
return _html(f'{int(a) if a == int(a) else a} {op} {int(b) if b == int(b) else b} = {res_str}', 'false')
except (ValueError, TypeError) as exc:
return _html(f'Ошибка: {exc}', 'false')
result = eval(expr, {'__builtins__': None}, _MATH_LOCALS) # noqa: S307
if not isinstance(result, (int, float)):
return {'error': 'Результат не является числом'}
return {'expr': expr, 'result': result}
except ZeroDivisionError:
return {'error': 'Деление на ноль'}
except Exception as exc:
return {'error': f'Ошибка: {exc}'}
def _esc(s):
# Экранируем HTML-спецсимволы — безопасный вывод в атрибут и тело.
return s.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;')
+4
View File
@@ -9,6 +9,8 @@ resource "sless_service" "calc_python" {
name = "calc-python"
runtime = "python3.11"
entrypoint = "handler.handler"
memory_mb = 128
timeout_sec = 30
source_dir = "${path.module}/code/calc-python"
}
@@ -23,6 +25,8 @@ resource "sless_service" "calc_node" {
name = "calc-node"
runtime = "nodejs20"
entrypoint = "handler.handler"
memory_mb = 128
timeout_sec = 30
source_dir = "${path.module}/code/calc-node"
}
+1 -1
View File
@@ -4,7 +4,7 @@ terraform {
required_providers {
nubes = {
source = "terra.k8c.ru/nubes/nubes"
version = "5.0.19"
version = "5.0.31"
}
sless = {
source = "terra.k8c.ru/naeel/sless"
+2 -1
View File
@@ -6,7 +6,8 @@ resource "nubes_vc_vm_v3" "vm" {
resource_name = "vm-sless-demo"
vapp_uid = nubes_vapp.vapp.id # ссылка на vApp. Не изменяется после создания.
image_vm = "Ubuntu 22.04 LTS" # Не изменяется после создания.
image_vm = "Ubuntu_22-20G" # Не изменяется после создания.
# image_vm = "Ubuntu 22.04 LTS" # Не изменяется после создания.
ip_space_name = "internet-ipv4-v1"
user_login = "ubuntu"