Skip to content
Merged
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
26 changes: 18 additions & 8 deletions create-a-container/config/config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
require('dotenv').config();

const config = {
host: process.env.MYSQL_HOST,
port: process.env.MYSQL_PORT,
username: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE,
dialect: 'mysql',
};
const config = { dialect: process.env.DATABASE_DIALECT || 'sqlite' };
if (config.dialect === 'mysql') {
config.host = process.env.MYSQL_HOST;
config.port = process.env.MYSQL_PORT;
config.username = process.env.MYSQL_USER;
config.password = process.env.MYSQL_PASSWORD;
config.database = process.env.MYSQL_DATABASE;
} else if (config.dialect === 'postgres') {
config.host = process.env.POSTGRES_HOST;
config.port = process.env.POSTGRES_PORT;
config.username = process.env.POSTGRES_USER;
config.password = process.env.POSTGRES_PASSWORD;
config.database = process.env.POSTGRES_DATABASE;
} else if (config.dialect === 'sqlite') {
config.storage = process.env.SQLITE_STORAGE || 'data/database.sqlite';
} else {
throw new Error(`Unsupported Database Dialect: ${process.env.DB_DIALECT}`);
}

module.exports = {
development: config,
Expand Down
17 changes: 17 additions & 0 deletions create-a-container/example.env
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
# secret used to compute express-session hash, generate randomly
SESSION_SECRET=

# dialect used by sequelize (mysql, sqlite, postgres)
# you also need to configure the relevant variables for the selected dialect
DATABASE_DIALECT=

# sqlite3 file path
SQLITE_STORAGE=

# mysql/mariadb connection information
MYSQL_HOST=
MYSQL_PORT=
MYSQL_USER=
MYSQL_PASSWORD=
MYSQL_DATABASE=

# postgresql connection information
POSTGRES_HOST=
POSTGRES_PORT=
POSTGRES_USER=
POSTGRES_PASSWORD=
POSTGRES_DATABASE=

# Only used for bin/json-to-sql.js (for now)
PROXMOX_URL=
PROXMOX_USER=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,35 @@ module.exports = {

// Step 2: Populate node based on aiContainer values
// FORTWAYNE -> mie-phxdc-ai-pve1
await queryInterface.sequelize.query(
"UPDATE Containers SET node = 'mie-phxdc-ai-pve1' WHERE aiContainer = 'FORTWAYNE'"
await queryInterface.bulkUpdate('Containers',
{ node: 'mie-phxdc-ai-pve1' },
{ aiContainer: 'PHOENIX' }
);

// PHOENIX -> intern-phxdc-pve3-ai
await queryInterface.sequelize.query(
"UPDATE Containers SET node = 'intern-phxdc-pve3-ai' WHERE aiContainer = 'PHOENIX'"
await queryInterface.bulkUpdate('Containers',
{ node: 'intern-phxdc-pve3-ai' },
{ aiContainer: 'FORTWAYNE' }
);

// quote everything properly
const containersTable = queryInterface.quoteIdentifier('Containers');
const aiContainerField = queryInterface.quoteIdentifier('aiContainer');
const containerIdField = queryInterface.quoteIdentifier('containerId');
const nodeField = queryInterface.quoteIdentifier('node');

// N + odd containerId -> intern-phxdc-pve1
await queryInterface.sequelize.query(
"UPDATE Containers SET node = 'intern-phxdc-pve1' WHERE aiContainer = 'N' AND MOD(containerId, 2) = 1"
`UPDATE ${containersTable}
SET ${nodeField} = 'intern-phxdc-pve1'
WHERE ${aiContainerField} = 'N' AND MOD(${containerIdField}, 2) = 1`
);

// N + even containerId -> intern-phxdc-pve2
await queryInterface.sequelize.query(
"UPDATE Containers SET node = 'intern-phxdc-pve2' WHERE aiContainer = 'N' AND MOD(containerId, 2) = 0"
`UPDATE ${containersTable}
SET ${nodeField} = 'intern-phxdc-pve2'
WHERE ${aiContainerField} = 'N' AND MOD(${containerIdField}, 2) = 0`
);

// Step 3: Make node NOT NULL
Expand Down
17 changes: 11 additions & 6 deletions create-a-container/migrations/20251104193601-create-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
'use strict';

const { sequelize } = require('../models');

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
Expand Down Expand Up @@ -33,12 +36,14 @@ module.exports = {
});

// Seed the Nodes table with existing node values from Containers
await queryInterface.sequelize.query(`
INSERT INTO Nodes (name, apiUrl, tlsVerify, createdAt, updatedAt)
SELECT DISTINCT node, NULL, NULL, NOW(), NOW()
FROM Containers
ORDER BY node
`);
const containersTable = queryInterface.quoteIdentifier('Containers');
const [nodeNames, _] = await queryInterface.sequelize.query(
`SELECT DISTINCT node FROM ${containersTable}`
);
const nodes = nodeNames.map(n => ({ name: n.node, createdAt: new Date(), updatedAt: new Date() }));
if (nodes.length > 0) {
await queryInterface.bulkInsert('Nodes', nodes);
}
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('Nodes');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ module.exports = {
});

// Step 2: Populate nodeId based on node string values
await queryInterface.sequelize.query(
"UPDATE Containers c JOIN Nodes n ON c.node = n.name SET c.nodeId = n.id"
const nodesTable = queryInterface.quoteIdentifier('Nodes');
const [nodes, _] = await queryInterface.sequelize.query(
`SELECT id, name FROM ${nodesTable}`
);
for (const { id, name } of nodes) {
await queryInterface.bulkUpdate('Containers', {
nodeId: id
}, {
node: name
});
}

// Step 3: Make nodeId NOT NULL
await queryInterface.changeColumn('Containers', 'nodeId', {
Expand Down Expand Up @@ -53,9 +61,16 @@ module.exports = {
});

// Populate node from nodeId
await queryInterface.sequelize.query(
"UPDATE Containers c JOIN Nodes n ON c.nodeId = n.id SET c.node = n.name"
const [nodes, _] = await queryInterface.sequelize.query(
'SELECT id, name FROM Nodes'
);
for (const { id, name } of nodes) {
await queryInterface.bulkUpdate('Containers', {
node: name
}, {
nodeId: id
});
}

// Make node NOT NULL
await queryInterface.changeColumn('Containers', 'node', {
Expand Down
7 changes: 1 addition & 6 deletions create-a-container/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.js')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
const sequelize = new Sequelize(config);

fs
.readdirSync(__dirname)
Expand Down
Loading
Loading