-
Notifications
You must be signed in to change notification settings - Fork 83
Update Instances AutoStackUpdate when TeamType Changes #6780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hardillb
wants to merge
11
commits into
main
Choose a base branch
from
enforce-team-autoUpdate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a40499c
Update Instances AutoStackUpdate when TeamType Changes
hardillb 9f9a2af
Remove run on start
hardillb a77c495
lint fix
hardillb fd7b4c7
Add tests and fix new instance code
hardillb b14b378
Merge branch 'main' into enforce-team-autoUpdate
hardillb d86315e
Merge branch 'main' into enforce-team-autoUpdate
hardillb b22497e
Add logging and fix existing details test
hardillb 0f9bb8e
Merge branch 'enforce-team-autoUpdate' of https://github.com/FlowFuse…
hardillb 96663e9
remove test only
hardillb 4e5c5b0
Merge branch 'main' into enforce-team-autoUpdate
hardillb 5ba5539
Merge branch 'main' into enforce-team-autoUpdate
hardillb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /** | ||
| * If a Team changes TeamType apply new AutoUpdate rules | ||
| */ | ||
| const { KEY_STACK_UPGRADE_HOUR } = require('../../../../db/models/ProjectSettings') | ||
| const { randomInt } = require('../../../../housekeeper/utils') | ||
|
|
||
| module.exports = { | ||
| name: 'fixTeamStackUpdateRules', | ||
| startup: false, | ||
| schedule: `${randomInt(0, 29)} ${randomInt(0, 23)} * * *`, // random time every day | ||
| run: async function (app) { | ||
| if (app.config.features.enabled('autoStackUpdate')) { | ||
| app.log.info('Running AutoStackUpgrade Teams Tests') | ||
| const teamTypes = await app.db.models.TeamType.getAll({}, { active: true }) | ||
| for (const teamType of teamTypes.types) { | ||
| if (teamType) { | ||
| const typeProperties = teamType.properties | ||
| if (typeProperties.autoStackUpdate?.allowDisable === false) { | ||
| app.log.info(`Found TeamType ${teamType.name} needs to enforce AutoStackUpdate`) | ||
| const autoStackUpdate = teamType.getProperty('autoStackUpdate') | ||
| // this TeamType needs to enforce restart rules. | ||
| // get all teams with this type, then get all instances in those teams | ||
| const teams = await app.db.models.Team.findAll({ | ||
| where: { | ||
| TeamTypeId: teamType.id | ||
| } | ||
| }) | ||
| for (const team of teams) { | ||
| if (team) { | ||
| const instances = await app.db.models.Project.byTeam(team.id) | ||
| for (const instance of instances) { | ||
| if (instance) { | ||
| let found = false | ||
| for (let day = 0; day < 7; day++) { | ||
| const k = await instance.getSetting(`${KEY_STACK_UPGRADE_HOUR}_${day}`) | ||
| if (k) { | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if (!found) { | ||
| const days = autoStackUpdate.days | ||
| const hours = autoStackUpdate.hours | ||
| // generate random day and hour in ranges | ||
| const day = days[Math.floor(days.length * Math.random())] | ||
| const hour = hours[Math.floor(hours.length * Math.random())] | ||
| app.log.info(`AutoStackUpgrade: applying update schedule ${day}/${hour} to instance ${instance.id} in team ${team.hashid}`) | ||
| await instance.updateSetting(`${KEY_STACK_UPGRADE_HOUR}_${day}`, { hour }) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| const { Op } = require('sequelize') | ||
| const should = require('should') // eslint-disable-line | ||
|
|
||
| const { KEY_STACK_UPGRADE_HOUR } = require('../../../../../../forge/db/models/ProjectSettings') | ||
| const enforceTeamRulesTask = require('../../../../../../forge/ee/lib/autoUpdateStacks/tasks/enforce-team-rules') | ||
| const setup = require('../../setup') | ||
|
|
||
| describe('Automatic Stack Upgrade', function () { | ||
| let app | ||
|
|
||
| before(async function () { | ||
| setup.setupStripe() | ||
| app = await setup() | ||
|
|
||
| const defaultTeamType = await app.db.models.TeamType.findOne({ where: { id: 1 } }) | ||
|
|
||
| const autoTeamTypeProperties = { | ||
| name: 'AutoUpdate', | ||
| order: 2, | ||
| description: 'TeamType that forces Stack Updates', | ||
| properties: { | ||
| users: { | ||
| limit: 10 | ||
| }, | ||
| runtimes: { | ||
| limit: 20 | ||
| }, | ||
| devices: { | ||
| productId: 'prod_device', | ||
| priceId: 'price_device', | ||
| description: '$5/month', | ||
| limit: 10 | ||
| }, | ||
| billing: { | ||
| productId: 'prod_team', | ||
| priceId: 'price_team', | ||
| description: '$10/month', | ||
| proration: 'always_invoice' | ||
| }, | ||
| trial: { | ||
| active: false | ||
| }, | ||
| enableAllFeatures: true, | ||
| features: { | ||
| fileStorageLimit: null, | ||
| contextLimit: null | ||
| }, | ||
| teamBroker: { | ||
| clients: { | ||
| limit: 10 | ||
| } | ||
| }, | ||
| autoStackUpdate: { | ||
| enabled: true, | ||
| days: [0, 6], | ||
| hours: [0, 1, 2, 3, 4], | ||
| allowDisable: false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| autoTeamTypeProperties.instances = defaultTeamType.properties.instances | ||
|
|
||
| const autoTeamType = await app.db.models.TeamType.create(autoTeamTypeProperties) | ||
| app.autoTeamType = autoTeamType | ||
| }) | ||
|
|
||
| after(async function () { | ||
| if (app) { | ||
| await app.close() | ||
| app = null | ||
| } | ||
| setup.resetStripe() | ||
| }) | ||
|
|
||
| describe('Update after TeamType change', async function () { | ||
| it('Instance properties updated', async function () { | ||
| const instanceStartSettings = await app.db.models.ProjectSettings.findAll({ | ||
| where: { | ||
| ProjectId: app.instance.id, | ||
| key: { | ||
| [Op.like]: `${KEY_STACK_UPGRADE_HOUR}_%` | ||
| } | ||
| } | ||
| }) | ||
| instanceStartSettings.should.have.length(0) | ||
| await app.team.updateTeamType(app.autoTeamType, { interval: 'month' }) | ||
| await enforceTeamRulesTask.run(app) | ||
| const instanceAfterSettings = await app.db.models.ProjectSettings.findAll({ | ||
| where: { | ||
| ProjectId: app.instance.id, | ||
| key: { | ||
| [Op.like]: `${KEY_STACK_UPGRADE_HOUR}_%` | ||
| } | ||
| } | ||
| }) | ||
| instanceAfterSettings.should.have.length(1) | ||
| instanceAfterSettings[0].value.hour.should.be.oneOf([0, 1, 2, 3, 4]) | ||
| const day = parseInt(instanceAfterSettings[0].key.split('_')[1]) | ||
| day.should.be.oneOf([0, 6]) | ||
| }) | ||
| it('Existing Instance properties not updated', async function () { | ||
| // depends on previous test | ||
| const instanceStartSettings = await app.db.models.ProjectSettings.findAll({ | ||
| where: { | ||
| ProjectId: app.instance.id, | ||
| key: { | ||
| [Op.like]: `${KEY_STACK_UPGRADE_HOUR}_%` | ||
| } | ||
| } | ||
| }) | ||
| instanceStartSettings.should.have.length(1) | ||
| const day = parseInt(instanceStartSettings[0].key.split('_')[1]) | ||
| const hour = instanceStartSettings[0].value.hour | ||
| await enforceTeamRulesTask.run(app) | ||
| const instanceAfterSettings = await app.db.models.ProjectSettings.findAll({ | ||
| where: { | ||
| ProjectId: app.instance.id, | ||
| key: { | ||
| [Op.like]: `${KEY_STACK_UPGRADE_HOUR}_%` | ||
| } | ||
| } | ||
| }) | ||
| instanceAfterSettings.should.have.length(1) | ||
| day.should.equal(parseInt(instanceAfterSettings[0].key.split('_')[1])) | ||
| hour.should.equal(instanceAfterSettings[0].value.hour) | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.