Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 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
24 changes: 21 additions & 3 deletions create-a-container/models/transport-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ module.exports = (sequelize, DataTypes) => {
TransportService.belongsTo(models.Service, { foreignKey: 'serviceId', as: 'service' });
}

// Find the next available external port for the given protocol in the specified range
static async nextAvailablePortInRange(protocol, minPort, maxPort, transaction = null) {
// Find the next available external port for the given protocol in the specified range,
// scoped to a specific site when siteId is provided.
static async nextAvailablePortInRange(protocol, minPort, maxPort, siteId = null, transaction = null) {
const { Service, Container } = sequelize.models;

const queryOptions = {
where: {
protocol: protocol,
Expand All @@ -19,7 +22,22 @@ module.exports = (sequelize, DataTypes) => {
attributes: ['externalPort'],
order: [['externalPort', 'ASC']]
};


if (siteId !== null) {
queryOptions.include = [{
model: Service,
as: 'service',
attributes: [],
required: true,
include: [{
model: Container,
attributes: [],
required: true,
where: { siteId }
}]
}];
}

if (transaction) {
queryOptions.transaction = transaction;
queryOptions.lock = sequelize.Sequelize.Transaction.LOCK.UPDATE;
Expand Down
6 changes: 3 additions & 3 deletions create-a-container/routers/containers.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ router.post('/', async (req, res) => {
}, { transaction: t });
} else {
const minPort = 2000;
const maxPort = 65565;
const externalPort = await TransportService.nextAvailablePortInRange(protocol, minPort, maxPort, t);
const maxPort = 65535;
const externalPort = await TransportService.nextAvailablePortInRange(protocol, minPort, maxPort, siteId, t);
await TransportService.create({
serviceId: createdService.id,
protocol: protocol,
Expand Down Expand Up @@ -615,7 +615,7 @@ router.put('/:id', requireAuth, async (req, res) => {
} else if (serviceType === 'dns') {
await DnsService.create({ serviceId: createdService.id, recordType: 'SRV', dnsName }, { transaction: t });
} else {
const externalPort = await TransportService.nextAvailablePortInRange(protocol, 2000, 65565);
const externalPort = await TransportService.nextAvailablePortInRange(protocol, 2000, 65535, siteId, t);
await TransportService.create({ serviceId: createdService.id, protocol, externalPort }, { transaction: t });
}
}
Expand Down
Loading