Skip to content

Commit

Permalink
SDA-4182: Add GPOs to auto update channel (#1896)
Browse files Browse the repository at this point in the history
  • Loading branch information
NguyenTranHoangSym committed Jul 18, 2023
1 parent 7d23cd6 commit 15056f1
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 114 deletions.
1 change: 1 addition & 0 deletions config/Symphony.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"autoUpdateUrl": "",
"autoUpdateChannel": "latest",
"isAutoUpdateEnabled": true,
"forceAutoUpdate": false,
"autoUpdateCheckInterval": "30",
"enableBrowserLogin": false,
"browserLoginAutoConnect": false,
Expand Down
44 changes: 44 additions & 0 deletions spec/registryHandler.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { retrieveWindowsRegistry } from '../src/app/registry-handler';
import {
EChannelRegistry,
RegistryStore,
} from '../src/app/stores/registry-store';

let mockChannel = { value: '', type: 'REG_SZ' };

jest.mock('winreg', () => {
return jest.fn().mockImplementation(() => {
return {
get: (_file, callback) => callback(null, mockChannel),
};
});
});

jest.mock('../src/common/env', () => {
return {
isWindowsOS: true,
isLinux: false,
isMac: false,
isDevEnv: true,
};
});

describe('Windows Registry', () => {
beforeEach(() => {
jest.clearAllMocks().resetModules();
});

it('it should return channel - latest', async () => {
mockChannel.value = 'latest';
await retrieveWindowsRegistry();
const registry = RegistryStore.getRegistry();
expect(registry.currentChannel).toBe(EChannelRegistry.LATEST);
});

it('it should return channel - beta', async () => {
mockChannel.value = 'beta';
await retrieveWindowsRegistry();
const registry = RegistryStore.getRegistry();
expect(registry.currentChannel).toBe(EChannelRegistry.BETA);
});
});
262 changes: 148 additions & 114 deletions src/app/auto-update-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { logger } from '../common/logger';
import { isUrl } from '../common/utils';
import { whitelistHandler } from '../common/whitelist-handler';
import { config } from './config-handler';
import { retrieveWindowsRegistry } from './registry-handler';
import { EChannelRegistry, RegistryStore } from './stores/registry-store';
import { windowHandler } from './window-handler';

const DEFAULT_AUTO_UPDATE_CHANNEL = 'client-bff/sda-update';
Expand All @@ -23,122 +25,139 @@ export class AutoUpdate {
private autoUpdateTrigger: AutoUpdateTrigger | undefined = undefined;

constructor() {
const opts = this.getGenericServerOptions();
if (isMac) {
this.autoUpdater = new MacUpdater(opts);
} else if (isWindowsOS) {
this.autoUpdater = new NsisUpdater(opts);
}
this.getGenericServerOptions().then((opts) => {
if (isMac) {
this.autoUpdater = new MacUpdater(opts);
} else if (isWindowsOS) {
this.autoUpdater = new NsisUpdater(opts);
}

if (this.autoUpdater) {
this.autoUpdater.logger = electronLog;
this.autoUpdater.autoDownload = false;
this.autoUpdater.autoInstallOnAppQuit = true;
this.autoUpdater.allowDowngrade = true;

this.autoUpdater.on('update-not-available', () => {
if (this.autoUpdateTrigger === AutoUpdateTrigger.AUTOMATED) {
logger.info(
'auto-update-handler: no update available found with automatic check',
);
if (this.autoUpdater) {
this.autoUpdater.logger = electronLog;
this.autoUpdater.autoDownload = false;
this.autoUpdater.autoInstallOnAppQuit = true;
this.autoUpdater.allowDowngrade = true;

this.autoUpdater.on('update-not-available', () => {
if (this.autoUpdateTrigger === AutoUpdateTrigger.AUTOMATED) {
logger.info(
'auto-update-handler: no update available found with automatic check',
);
this.autoUpdateTrigger = undefined;
return;
}
const mainWebContents = windowHandler.mainWebContents;
// Display client banner
if (mainWebContents && !mainWebContents.isDestroyed()) {
mainWebContents.send('display-client-banner', {
reason: 'autoUpdate',
action: 'update-not-available',
});
}
this.autoUpdateTrigger = undefined;
return;
}
const mainWebContents = windowHandler.mainWebContents;
// Display client banner
if (mainWebContents && !mainWebContents.isDestroyed()) {
mainWebContents.send('display-client-banner', {
reason: 'autoUpdate',
action: 'update-not-available',
});
}
this.autoUpdateTrigger = undefined;
});
});

const { autoUpdateChannel } = config.getConfigFields([
'autoUpdateChannel',
]);
const { installVariant } = config.getConfigFields(['installVariant']);
this.autoUpdater.on('update-available', (info) => {
const mainWebContents = windowHandler.mainWebContents;
// Display client banner
if (mainWebContents && !mainWebContents.isDestroyed()) {
mainWebContents.send('display-client-banner', {
reason: 'autoUpdate',
action: 'update-available',
data: {
...info,
autoUpdateTrigger: this.autoUpdateTrigger,
autoUpdateChannel,
installVariant,
channelConfigLocation: null,
sessionStartDatetime: null,
machineStartDatetime: null,
machineId: null,
},
});
}
});

this.autoUpdater.on('download-progress', (info) => {
const mainWebContents = windowHandler.mainWebContents;
// Display client banner
if (
mainWebContents &&
!mainWebContents.isDestroyed() &&
!this.didPublishDownloadProgress
) {
mainWebContents.send('display-client-banner', {
reason: 'autoUpdate',
action: 'download-progress',
data: {
...info,
autoUpdateTrigger: this.autoUpdateTrigger,
autoUpdateChannel,
installVariant,
channelConfigLocation: null,
sessionStartDatetime: null,
machineStartDatetime: null,
machineId: null,
},
});
this.didPublishDownloadProgress = true;
}
});

this.autoUpdater.on('update-downloaded', (info) => {
this.isUpdateAvailable = true;
const mainWebContents = windowHandler.mainWebContents;
// Display client banner
if (mainWebContents && !mainWebContents.isDestroyed()) {
mainWebContents.send('display-client-banner', {
reason: 'autoUpdate',
action: 'update-downloaded',
data: {
...info,
autoUpdateTrigger: this.autoUpdateTrigger,
autoUpdateChannel,
installVariant,
channelConfigLocation: null,
sessionStartDatetime: null,
machineStartDatetime: null,
machineId: null,
},
});
}
if (isMac) {
config.backupGlobalConfig();
const { autoUpdateChannel } = config.getConfigFields([
'autoUpdateChannel',
]);
let finalAutoUpdateChannel = autoUpdateChannel;

if (isWindowsOS) {
const registryAutoUpdate = RegistryStore.getRegistry();
const identifiedChannelFromRegistry = [
EChannelRegistry.BETA,
EChannelRegistry.LATEST,
].includes(registryAutoUpdate.currentChannel)
? registryAutoUpdate.currentChannel
: '';

if (identifiedChannelFromRegistry) {
finalAutoUpdateChannel = identifiedChannelFromRegistry;
}
}
});

this.autoUpdater.on('error', (error) => {
this.autoUpdateTrigger = undefined;
logger.error(
'auto-update-handler: Error occurred while updating. ',
error,
);
});
}

const { installVariant } = config.getConfigFields(['installVariant']);
this.autoUpdater.on('update-available', (info) => {
const mainWebContents = windowHandler.mainWebContents;
// Display client banner
if (mainWebContents && !mainWebContents.isDestroyed()) {
mainWebContents.send('display-client-banner', {
reason: 'autoUpdate',
action: 'update-available',
data: {
...info,
autoUpdateTrigger: this.autoUpdateTrigger,
autoUpdateChannel: finalAutoUpdateChannel,
installVariant,
channelConfigLocation: null,
sessionStartDatetime: null,
machineStartDatetime: null,
machineId: null,
},
});
}
});

this.autoUpdater.on('download-progress', (info) => {
const mainWebContents = windowHandler.mainWebContents;
// Display client banner
if (
mainWebContents &&
!mainWebContents.isDestroyed() &&
!this.didPublishDownloadProgress
) {
mainWebContents.send('display-client-banner', {
reason: 'autoUpdate',
action: 'download-progress',
data: {
...info,
autoUpdateTrigger: this.autoUpdateTrigger,
autoUpdateChannel: finalAutoUpdateChannel,
installVariant,
channelConfigLocation: null,
sessionStartDatetime: null,
machineStartDatetime: null,
machineId: null,
},
});
this.didPublishDownloadProgress = true;
}
});

this.autoUpdater.on('update-downloaded', (info) => {
this.isUpdateAvailable = true;
const mainWebContents = windowHandler.mainWebContents;
// Display client banner
if (mainWebContents && !mainWebContents.isDestroyed()) {
mainWebContents.send('display-client-banner', {
reason: 'autoUpdate',
action: 'update-downloaded',
data: {
...info,
autoUpdateTrigger: this.autoUpdateTrigger,
autoUpdateChannel: finalAutoUpdateChannel,
installVariant,
channelConfigLocation: null,
sessionStartDatetime: null,
machineStartDatetime: null,
machineId: null,
},
});
}
if (isMac) {
config.backupGlobalConfig();
}
});

this.autoUpdater.on('error', (error) => {
this.autoUpdateTrigger = undefined;
logger.error(
'auto-update-handler: Error occurred while updating. ',
error,
);
});
}
});
}

/**
Expand Down Expand Up @@ -173,7 +192,7 @@ export class AutoUpdate {
this.autoUpdateTrigger = trigger;
logger.info('auto-update-handler: Checking for updates', trigger);
if (this.autoUpdater) {
const opts: GenericServerOptions = this.getGenericServerOptions();
const opts: GenericServerOptions = await this.getGenericServerOptions();
this.autoUpdater.setFeedURL(opts);
const updateCheckResult = await this.autoUpdater.checkForUpdates();
logger.info('auto-update-handler: ', updateCheckResult);
Expand Down Expand Up @@ -221,16 +240,31 @@ export class AutoUpdate {
return updateUrl;
};

private getGenericServerOptions = (): GenericServerOptions => {
private getGenericServerOptions = async (): Promise<GenericServerOptions> => {
let userAutoUpdateChannel;
const { autoUpdateChannel, betaAutoUpdateChannelEnabled } =
config.getConfigFields([
'autoUpdateChannel',
'betaAutoUpdateChannelEnabled',
]);

userAutoUpdateChannel = betaAutoUpdateChannelEnabled
? 'beta'
: autoUpdateChannel;
if (isWindowsOS) {
await retrieveWindowsRegistry();
const registryAutoUpdate = RegistryStore.getRegistry();
const identifiedChannelFromRegistry = [
EChannelRegistry.BETA,
EChannelRegistry.LATEST,
].includes(registryAutoUpdate.currentChannel)
? registryAutoUpdate.currentChannel
: '';
if (identifiedChannelFromRegistry) {
userAutoUpdateChannel = identifiedChannelFromRegistry;
}
}

logger.info(`auto-update-handler: using channel ${userAutoUpdateChannel}`);

const opts: GenericServerOptions = {
Expand Down
Loading

0 comments on commit 15056f1

Please sign in to comment.