Skip to content
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

#243: improve internet detection checks #254

Merged
merged 2 commits into from
Oct 25, 2024
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
### New Features & Improvements

* Changed `lando setup` to pull common plugins based on release `channel` to better mimic fatcore
* Rebased `axios` on `@npmcli/agent` to improve request consistency across environments
* Improved internet detection tests [#243](https://github.com/lando/core/issues/243)
* Rebased `axios` and `octokit` on `@npmcli/agent` to improve request consistency across environments
* Updated default Docker Desktop version to `4.34.3`
* Updated default Docker Engine version to `27.3.1`
* Udpated recommended Docker Destkop range to `>=4.34.0`
Expand All @@ -19,6 +20,7 @@
* Added `debugShim` to `lando.utils`
* Added `downloadX` to `lando.utils`
* Added `getAxios` to `lando.utils`
* Added `getOctokit` to `lando.utils`
* Added `getUserShell` to `lando.utils`
* Added `getUserShellProfile` to `lando.utils`
* Added `isVersionLte` to `lando.utils`
Expand Down
5 changes: 0 additions & 5 deletions components/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,6 @@ class Plugin {
const channel = this.channel === 'stable' ? 'latest' : this.channel;

try {
// check internet connection
const online = await require('is-online')();
// throw error if not online
if (!online) throw new Error('Cannot detect connection to internet!');
// process to check
// get release data
const data = await packument(this.spec, merge({}, [Plugin.config, {fullMetadata: true}]));
// build a list of highest available versions
Expand Down
10 changes: 7 additions & 3 deletions hooks/lando-setup-build-engine-darwin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const axios = require('../utils/get-axios')();
const os = require('os');
const path = require('path');
const semver = require('semver');
Expand Down Expand Up @@ -86,6 +87,9 @@ module.exports = async (lando, options) => {
// cosmetics
const install = version ? `v${version}` : `build ${build}`;

// download url
const url = getEngineDownloadUrl(build);

// darwin install task
options.tasks.push({
title: `Downloading build engine`,
Expand All @@ -112,8 +116,8 @@ module.exports = async (lando, options) => {
canRun: async () => {
// throw if we cannot resolve a semantic version to a buildid
if (!build) throw new Error(`Could not resolve ${install} to an installable version!`);
// throw error if not online
if (!await require('is-online')()) throw new Error('Cannot detect connection to internet!');
// throw error if we cannot ping the download link
await axios.head(url);
// throw if user is not an admin
if (!await require('../utils/is-admin-user')()) {
throw new Error([
Expand All @@ -127,7 +131,7 @@ module.exports = async (lando, options) => {
task: async (ctx, task) => {
try {
// download the installer
ctx.download = await downloadDockerDesktop(getEngineDownloadUrl(build), {ctx, debug, task});
ctx.download = await downloadDockerDesktop(url, {ctx, debug, task});

// prompt for password if interactive
if (ctx.password === undefined && lando.config.isInteractive) {
Expand Down
8 changes: 5 additions & 3 deletions hooks/lando-setup-build-engine-linux.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const axios = require('../utils/get-axios')();
const os = require('os');
const path = require('path');

Expand Down Expand Up @@ -31,6 +32,7 @@ module.exports = async (lando, options) => {
if (options.buildEngine === false) return;

const version = options.buildEngine;
const url = 'https://get.docker.com';

// darwin install task
options.tasks.push({
Expand All @@ -46,8 +48,8 @@ module.exports = async (lando, options) => {
canRun: async () => {
// throw if we cannot resolve a semantic version to a buildid
if (!version) throw new Error(`Could not resolve ${version} to an installable version!`);
// throw error if not online
if (!await require('is-online')()) throw new Error('Cannot detect connection to internet!');
// throw error if we cannot ping the download link
await axios.head(url);
// throw if user is not an admin
if (!await require('../utils/is-admin-user')()) {
throw new Error([
Expand All @@ -61,7 +63,7 @@ module.exports = async (lando, options) => {
task: async (ctx, task) => {
try {
// download the installer
ctx.download = await downloadDockerEngine('https://get.docker.com', {ctx, debug, task});
ctx.download = await downloadDockerEngine(url, {ctx, debug, task});

// prompt for password if interactive and we dont have it
if (ctx.password === undefined && lando.config.isInteractive) {
Expand Down
10 changes: 7 additions & 3 deletions hooks/lando-setup-build-engine-win32.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const axios = require('../utils/get-axios')();
const os = require('os');
const path = require('path');
const semver = require('semver');
Expand Down Expand Up @@ -90,10 +91,13 @@ module.exports = async (lando, options) => {
// get stuff from config/opts
const build = getId(options.buildEngine);
const version = getVersion(options.buildEngine);

// cosmetics
const buildEngine = process.platform === 'linux' ? 'docker-engine' : 'docker-desktop';
const install = version ? `v${version}` : `build ${build}`;

const url = getEngineDownloadUrl(build);

// win32 install docker desktop task
options.tasks.push({
title: `Downloading build engine`,
Expand All @@ -120,8 +124,8 @@ module.exports = async (lando, options) => {
canRun: async () => {
// throw if we cannot resolve a semantic version to a buildid
if (!build) throw new Error(`Could not resolve ${install} to an installable version!`);
// throw error if not online
if (!await require('is-online')()) throw new Error('Cannot detect connection to internet!');
// throw error if we cannot ping the download link
await axios.head(url);
// @TODO: check for wsl2?
return true;
},
Expand All @@ -135,7 +139,7 @@ module.exports = async (lando, options) => {
task: async (ctx, task) => {
try {
// download the installer
ctx.download = await downloadDockerDesktop(getEngineDownloadUrl(build), {ctx, debug, task});
ctx.download = await downloadDockerDesktop(url, {ctx, debug, task});
// script
const script = [path.join(lando.config.userConfRoot, 'scripts', 'install-docker-desktop.ps1')];
// args
Expand Down
2 changes: 0 additions & 2 deletions hooks/lando-setup-install-ca-linux.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ module.exports = async (lando, options) => {
}
},
canRun: async () => {
// Check for internet connection
if (!await require('is-online')()) throw new Error('Cannot detect connection to internet!');
// Check for admin privileges
if (!await require('../utils/is-admin-user')()) {
throw new Error([
Expand Down
8 changes: 4 additions & 4 deletions hooks/lando-setup-orchestrator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const axios = require('../utils/get-axios')();
const fs = require('fs');
const path = require('path');

Expand Down Expand Up @@ -64,10 +65,9 @@ module.exports = async (lando, options) => {
return !!orchestratorBin && typeof orchestrator === 'string' && fs.existsSync(dest);
},
canRun: async () => {
const online = await require('is-online')();
// throw error if not online
if (!online) throw new Error('Cannot detect connection to internet!');

// throw error if we cannot ping the download link
await axios.head(url);
// true if we get here
return true;
},
task: async (ctx, task) => new Promise((resolve, reject) => {
Expand Down
1 change: 1 addition & 0 deletions lib/lando.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ module.exports = class Lando {

// updater is more complex now
this.updates = new UpdateManager({
agent: this.config.userAgent,
channel: this.config.channel,
cli: _.get(this, 'config.cli'),
config: getPluginConfig(this.config.pluginConfigFile, this.config.pluginConfig),
Expand Down
17 changes: 8 additions & 9 deletions lib/updates.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';

const axios = require('../utils/get-axios')();
const fs = require('fs');
const get = require('lodash/get');
const getOctokit = require('../utils/get-octokit');
const os = require('os');
const path = require('path');
const semver = require('semver');
const uniqBy = require('lodash/uniqBy');

const {color} = require('listr2');
const {Octokit} = require('@octokit/rest');

const getOS = () => {
switch (process.platform) {
Expand All @@ -32,6 +33,7 @@ const getPluginClass = ({channel, config, debug} = {}) => {

module.exports = class UpdateManager {
constructor({
agent = 'Lando',
config = {},
cli,
channel = 'stable',
Expand All @@ -40,6 +42,7 @@ module.exports = class UpdateManager {
plugins = [],
} = {}) {
// set things
this.agent = agent;
this._plugins = plugins;
this.channel = channel;
this.cli = cli;
Expand Down Expand Up @@ -108,11 +111,7 @@ module.exports = class UpdateManager {

checks.push(new Promise(async resolve => {
// summon the katkraken
const octokit = new Octokit({auth: get(process, 'env.LANDO_GITHUB_TOKEN')});
// check internet connection
const online = await require('is-online')();
// throw error if not online
if (!online) throw new Error('Cannot detect connection to internet!');
const octokit = getOctokit({auth: get(process, 'env.LANDO_GITHUB_TOKEN'), userAgent: this.agent});
// just a helper to give consistent prez
const extra = color.dim('@lando/cli');

Expand Down Expand Up @@ -230,8 +229,8 @@ module.exports = class UpdateManager {
throw new Error(`Lando cannot write to ${this._cli.installPath}!`);
}

// throw error if not online
if (!await require('is-online')()) throw new Error('Cannot detect connection to internet!');
// throw error if we cannot ping the download link
await axios.head(this._cli.update.download);

// or true
return true;
Expand Down Expand Up @@ -320,7 +319,7 @@ module.exports = class UpdateManager {
});

// summon the katkraken
const octokit = new Octokit({auth: get(process, 'env.LANDO_GITHUB_TOKEN')});
const octokit = getOctokit({auth: get(process, 'env.LANDO_GITHUB_TOKEN'), userAgent: this.agent});

// get latest
try {
Expand Down
1 change: 1 addition & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ module.exports = {
debugShim: (...args) => require('../utils/debug-shim')(...args),
downloadX: (...args) => require('../utils/download-x')(...args),
getAxios: (...args) => require('../utils/get-axios')(...args),
getOctokit: (...args) => require('../utils/get-octokit')(...args),
getUserShell: (...args) => require('../utils/get-user-shell')(...args),
getUserShellProfile: (...args) => require('../utils/get-user-shell-profile')(...args),
isVersionLte: (...args) => require('../utils/is-lte-version')(...args),
Expand Down
Loading
Loading