4 Commits

8 changed files with 202 additions and 59 deletions

Binary file not shown.

View File

@@ -0,0 +1,46 @@
"""add portainer stack fields to instances
Revision ID: 9206bf87bb8e
Revises: add_quota_fields
Create Date: 2025-06-24 14:02:17.375785
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy import text
# revision identifiers, used by Alembic.
revision = '9206bf87bb8e'
down_revision = 'add_quota_fields'
branch_labels = None
depends_on = None
def upgrade():
conn = op.get_bind()
# Check if columns already exist
result = conn.execute(text("""
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'instances'
AND column_name IN ('portainer_stack_id', 'portainer_stack_name')
"""))
existing_columns = [row[0] for row in result.fetchall()]
# Add portainer stack columns if they don't exist
with op.batch_alter_table('instances', schema=None) as batch_op:
if 'portainer_stack_id' not in existing_columns:
batch_op.add_column(sa.Column('portainer_stack_id', sa.String(length=100), nullable=True))
if 'portainer_stack_name' not in existing_columns:
batch_op.add_column(sa.Column('portainer_stack_name', sa.String(length=100), nullable=True))
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('instances', schema=None) as batch_op:
batch_op.drop_column('portainer_stack_name')
batch_op.drop_column('portainer_stack_id')
# ### end Alembic commands ###

View File

@@ -20,11 +20,21 @@ def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('email_template')
op.drop_table('notification')
# Check if columns already exist before adding them
connection = op.get_bind()
inspector = sa.inspect(connection)
existing_columns = [col['name'] for col in inspector.get_columns('instances')]
with op.batch_alter_table('instances', schema=None) as batch_op:
batch_op.add_column(sa.Column('deployed_version', sa.String(length=50), nullable=True))
batch_op.add_column(sa.Column('deployed_branch', sa.String(length=100), nullable=True))
batch_op.add_column(sa.Column('latest_version', sa.String(length=50), nullable=True))
batch_op.add_column(sa.Column('version_checked_at', sa.DateTime(), nullable=True))
if 'deployed_version' not in existing_columns:
batch_op.add_column(sa.Column('deployed_version', sa.String(length=50), nullable=True))
if 'deployed_branch' not in existing_columns:
batch_op.add_column(sa.Column('deployed_branch', sa.String(length=100), nullable=True))
if 'latest_version' not in existing_columns:
batch_op.add_column(sa.Column('latest_version', sa.String(length=50), nullable=True))
if 'version_checked_at' not in existing_columns:
batch_op.add_column(sa.Column('version_checked_at', sa.DateTime(), nullable=True))
with op.batch_alter_table('room_file', schema=None) as batch_op:
batch_op.drop_constraint(batch_op.f('fk_room_file_deleted_by_user'), type_='foreignkey')
@@ -37,11 +47,20 @@ def downgrade():
with op.batch_alter_table('room_file', schema=None) as batch_op:
batch_op.create_foreign_key(batch_op.f('fk_room_file_deleted_by_user'), 'user', ['deleted_by'], ['id'])
# Check if columns exist before dropping them
connection = op.get_bind()
inspector = sa.inspect(connection)
existing_columns = [col['name'] for col in inspector.get_columns('instances')]
with op.batch_alter_table('instances', schema=None) as batch_op:
batch_op.drop_column('version_checked_at')
batch_op.drop_column('latest_version')
batch_op.drop_column('deployed_branch')
batch_op.drop_column('deployed_version')
if 'version_checked_at' in existing_columns:
batch_op.drop_column('version_checked_at')
if 'latest_version' in existing_columns:
batch_op.drop_column('latest_version')
if 'deployed_branch' in existing_columns:
batch_op.drop_column('deployed_branch')
if 'deployed_version' in existing_columns:
batch_op.drop_column('deployed_version')
op.create_table('notification',
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),

View File

@@ -528,6 +528,9 @@ class Instance(db.Model):
status = db.Column(db.String(20), nullable=False, default='inactive')
status_details = db.Column(db.Text, nullable=True)
connection_token = db.Column(db.String(64), unique=True, nullable=True)
# Portainer integration fields
portainer_stack_id = db.Column(db.String(100), nullable=True) # Portainer stack ID
portainer_stack_name = db.Column(db.String(100), nullable=True) # Portainer stack name
# Version tracking fields
deployed_version = db.Column(db.String(50), nullable=True) # Current deployed version (commit hash or tag)
deployed_branch = db.Column(db.String(100), nullable=True) # Branch that was deployed

View File

@@ -1090,13 +1090,9 @@ def save_instance():
if existing_instance:
# Update existing instance
existing_instance.port = data['port']
existing_instance.domains = data['domains']
existing_instance.stack_id = data['stack_id']
existing_instance.stack_name = data['stack_name']
existing_instance.portainer_stack_id = data['stack_id']
existing_instance.portainer_stack_name = data['stack_name']
existing_instance.status = data['status']
existing_instance.repository = data['repository']
existing_instance.branch = data['branch']
existing_instance.deployed_version = data.get('deployed_version', 'unknown')
existing_instance.deployed_branch = data.get('deployed_branch', data['branch'])
existing_instance.version_checked_at = datetime.utcnow()
@@ -1107,13 +1103,9 @@ def save_instance():
'message': 'Instance data updated successfully',
'data': {
'name': existing_instance.name,
'port': existing_instance.port,
'domains': existing_instance.domains,
'stack_id': existing_instance.stack_id,
'stack_name': existing_instance.stack_name,
'portainer_stack_id': existing_instance.portainer_stack_id,
'portainer_stack_name': existing_instance.portainer_stack_name,
'status': existing_instance.status,
'repository': existing_instance.repository,
'branch': existing_instance.branch,
'deployed_version': existing_instance.deployed_version,
'deployed_branch': existing_instance.deployed_branch
}
@@ -1126,14 +1118,11 @@ def save_instance():
rooms_count=0,
conversations_count=0,
data_size=0.0,
payment_plan='Basic',
payment_plan=data.get('payment_plan', 'Basic'),
main_url=f"https://{data['domains'][0]}" if data['domains'] else f"http://localhost:{data['port']}",
status=data['status'],
port=data['port'],
stack_id=data['stack_id'],
stack_name=data['stack_name'],
repository=data['repository'],
branch=data['branch'],
portainer_stack_id=data['stack_id'],
portainer_stack_name=data['stack_name'],
deployed_version=data.get('deployed_version', 'unknown'),
deployed_branch=data.get('deployed_branch', data['branch'])
)
@@ -1145,13 +1134,9 @@ def save_instance():
'message': 'Instance data saved successfully',
'data': {
'name': instance.name,
'port': instance.port,
'domains': instance.domains,
'stack_id': instance.stack_id,
'stack_name': instance.stack_name,
'portainer_stack_id': instance.portainer_stack_id,
'portainer_stack_name': instance.portainer_stack_name,
'status': instance.status,
'repository': instance.repository,
'branch': instance.branch,
'deployed_version': instance.deployed_version,
'deployed_branch': instance.deployed_branch
}

View File

@@ -593,6 +593,9 @@ async function startLaunch(data) {
// Save instance data
await updateStep(10, 'Saving Instance Data', 'Storing instance information...');
try {
// Get the launch data from sessionStorage to access pricing tier info
const launchData = JSON.parse(sessionStorage.getItem('instanceLaunchData') || '{}');
const instanceData = {
name: data.instanceName,
port: data.port,
@@ -603,7 +606,8 @@ async function startLaunch(data) {
repository: data.repository,
branch: data.branch,
deployed_version: dockerComposeResult.latest_tag || dockerComposeResult.commit_hash || 'unknown',
deployed_branch: data.branch
deployed_branch: data.branch,
payment_plan: launchData.pricingTier?.name || 'Basic' // Use the selected pricing tier name
};
console.log('Saving instance data:', instanceData);
const saveResult = await saveInstanceData(instanceData);
@@ -2046,28 +2050,45 @@ async function saveInstanceData(instanceData) {
if (existingInstance) {
console.log('Instance already exists:', instanceData.port);
// Update existing instance with new data
const updateResponse = await fetch('/api/admin/save-instance', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').content
},
body: JSON.stringify({
name: instanceData.port,
port: instanceData.port,
domains: instanceData.domains,
stack_id: instanceData.stack_id || '',
stack_name: instanceData.stack_name,
status: instanceData.status,
repository: instanceData.repository,
branch: instanceData.branch,
deployed_version: instanceData.deployed_version,
deployed_branch: instanceData.deployed_branch,
payment_plan: instanceData.payment_plan || 'Basic'
})
});
if (!updateResponse.ok) {
const errorText = await updateResponse.text();
console.error('Error updating instance:', errorText);
throw new Error(`Failed to update instance data: ${updateResponse.status} ${updateResponse.statusText}`);
}
const updateResult = await updateResponse.json();
console.log('Instance updated:', updateResult);
return {
success: true,
data: {
name: instanceData.port,
company: 'loading...',
rooms_count: 0,
conversations_count: 0,
data_size: 0.0,
payment_plan: 'Basic',
main_url: `https://${instanceData.domains[0]}`,
status: 'inactive',
port: instanceData.port,
stack_id: instanceData.stack_id || '', // Use empty string if null
stack_name: instanceData.stack_name,
repository: instanceData.repository,
branch: instanceData.branch
}
data: updateResult.data
};
}
// If instance doesn't exist, create it
const response = await fetch('/instances/add', {
const response = await fetch('/api/admin/save-instance', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
@@ -2075,18 +2096,16 @@ async function saveInstanceData(instanceData) {
},
body: JSON.stringify({
name: instanceData.port,
company: 'loading...',
rooms_count: 0,
conversations_count: 0,
data_size: 0.0,
payment_plan: 'Basic',
main_url: `https://${instanceData.domains[0]}`,
status: 'inactive',
port: instanceData.port,
stack_id: instanceData.stack_id || '', // Use empty string if null
domains: instanceData.domains,
stack_id: instanceData.stack_id || '',
stack_name: instanceData.stack_name,
status: instanceData.status,
repository: instanceData.repository,
branch: instanceData.branch
branch: instanceData.branch,
deployed_version: instanceData.deployed_version,
deployed_branch: instanceData.deployed_branch,
payment_plan: instanceData.payment_plan || 'Basic'
})
});

View File

@@ -142,6 +142,21 @@
<div class="company-value" id="instance-payment-plan-value">Loading...</div>
</div>
</div>
<div class="mb-3">
<div class="d-flex">
<div class="text-muted me-2" style="min-width: 120px;">Portainer Stack:</div>
<div class="company-value">
{% if instance.portainer_stack_name %}
<span class="badge bg-primary">{{ instance.portainer_stack_name }}</span>
{% if instance.portainer_stack_id %}
<small class="text-muted d-block mt-1">ID: {{ instance.portainer_stack_id }}</small>
{% endif %}
{% else %}
<span class="text-muted">Not set</span>
{% endif %}
</div>
</div>
</div>
</div>
<div class="col-md-6">
<h5 class="mb-3">Contact Information</h5>

View File

@@ -179,6 +179,7 @@
<th>Main URL</th>
<th>Status</th>
<th>Version</th>
<th>Portainer Stack</th>
<th>Connection Token</th>
<th>Actions</th>
</tr>
@@ -191,7 +192,7 @@
<td>{{ instance.rooms_count }}</td>
<td>{{ instance.conversations_count }}</td>
<td>{{ "%.1f"|format(instance.data_size) }} GB</td>
<td>{{ instance.payment_plan }}</td>
<td id="payment-plan-{{ instance.id }}">{{ instance.payment_plan }}</td>
<td>
<a href="{{ instance.main_url }}"
target="_blank"
@@ -220,6 +221,16 @@
<span class="badge bg-secondary version-badge">unknown</span>
{% endif %}
</td>
<td>
{% if instance.portainer_stack_name %}
<span class="badge bg-primary" data-bs-toggle="tooltip"
title="Stack ID: {{ instance.portainer_stack_id or 'N/A' }}">
{{ instance.portainer_stack_name }}
</span>
{% else %}
<span class="badge bg-secondary">Not set</span>
{% endif %}
</td>
<td>
{% if instance.connection_token %}
<span class="badge bg-success" data-bs-toggle="tooltip" title="Instance is authenticated">
@@ -2479,5 +2490,50 @@ function validateStep6() {
return true;
}
document.addEventListener('DOMContentLoaded', function() {
// For each instance row, fetch the payment plan from the instance API
document.querySelectorAll('[data-instance-id]').forEach(function(badge) {
const instanceId = badge.getAttribute('data-instance-id');
const token = badge.getAttribute('data-token');
const row = badge.closest('tr');
const urlCell = row.querySelector('td:nth-child(7) a');
const paymentPlanCell = document.getElementById('payment-plan-' + instanceId);
if (!urlCell || !token || !paymentPlanCell) return;
const instanceUrl = urlCell.getAttribute('href');
// Get management token
fetch(instanceUrl.replace(/\/$/, '') + '/api/admin/management-token', {
method: 'POST',
headers: {
'X-API-Key': token,
'Accept': 'application/json'
}
})
.then(res => res.json())
.then(data => {
if (!data.token) throw new Error('No management token');
// Fetch version info (which includes pricing_tier_name)
return fetch(instanceUrl.replace(/\/$/, '') + '/api/admin/version-info', {
headers: {
'Authorization': 'Bearer ' + data.token,
'Accept': 'application/json'
}
});
})
.then(res => res.json())
.then(data => {
if (data.pricing_tier_name) {
paymentPlanCell.textContent = data.pricing_tier_name;
} else {
paymentPlanCell.textContent = 'Unknown';
}
})
.catch(err => {
paymentPlanCell.textContent = 'Unknown';
});
});
});
</script>
{% endblock %}