-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ensure requirements in
packageInstall
and not only on `fetchDnpRequ…
…est` (#2054) * ensure requirements in `packageInstall` and not only on `fetchDnpRequest` * increase test timeout * add check type dncore * skip bypass for testing * fix linter * improve error message * remove bypass install
- Loading branch information
1 parent
63d08cf
commit e18ba45
Showing
4 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
68 changes: 68 additions & 0 deletions
68
packages/installer/src/installer/checkInstallRequirements.ts
This file contains 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,68 @@ | ||
import { listPackages, getDockerVersion } from "@dappnode/dockerapi"; | ||
import { params } from "@dappnode/params"; | ||
import { Manifest, InstalledPackageData } from "@dappnode/types"; | ||
import { valid, gt } from "semver"; | ||
|
||
/** | ||
* Get the install requirements and throw an error if they are not met | ||
*/ | ||
export async function checkInstallRequirements({ manifest }: { manifest: Manifest }): Promise<void> { | ||
if (manifest.type === "dncore") return; | ||
const installedPackages = await listPackages(); | ||
const packagesRequiredToBeUninstalled = getRequiresUninstallPackages({ manifest, installedPackages }); | ||
const requiresCoreUpdate = getRequiresCoreUpdateTo({ manifest, installedPackages }); | ||
const requiresDockerUpdate = await getRequiresDockerUpdateTo({ manifest }); | ||
|
||
const errors: string[] = []; | ||
if (packagesRequiredToBeUninstalled.length > 0) | ||
errors.push(`The following packages must be uninstalled: ${packagesRequiredToBeUninstalled.join(", ")}`); | ||
if (requiresCoreUpdate) errors.push(`Core update required to ${requiresCoreUpdate}`); | ||
if (requiresDockerUpdate) errors.push(`Docker update required to ${requiresDockerUpdate}`); | ||
if (errors.length > 0) | ||
throw new Error(`The package cannot be installed because of the following requirements: | ||
${errors.join("\n")}`); | ||
} | ||
|
||
function getRequiresUninstallPackages({ | ||
manifest, | ||
installedPackages | ||
}: { | ||
manifest: Manifest; | ||
installedPackages: InstalledPackageData[]; | ||
}): string[] { | ||
const { notInstalledPackages } = manifest.requirements || {}; | ||
if (!notInstalledPackages || notInstalledPackages.length === 0) return []; | ||
return notInstalledPackages.filter((dnpName) => installedPackages.find((dnp) => dnp.dnpName === dnpName)); | ||
} | ||
|
||
function getRequiresCoreUpdateTo({ | ||
manifest, | ||
installedPackages | ||
}: { | ||
manifest: Manifest; | ||
installedPackages: InstalledPackageData[]; | ||
}): string | null { | ||
const coreVersion = installedPackages.find((dnp) => dnp.dnpName === params.coreDnpName)?.version; | ||
const minDnVersion = manifest.requirements?.minimumDappnodeVersion; | ||
|
||
if (!coreVersion || !minDnVersion) return null; | ||
|
||
const requiresCoreUpdate = Boolean(valid(minDnVersion) && valid(coreVersion) && gt(minDnVersion, coreVersion)); | ||
if (requiresCoreUpdate) return minDnVersion; | ||
|
||
return null; | ||
} | ||
async function getRequiresDockerUpdateTo({ manifest }: { manifest: Manifest }): Promise<string | null> { | ||
const minDockerVersion = manifest.requirements?.minimumDockerVersion; | ||
if (!minDockerVersion) return null; | ||
const currentDockerVersion = await getDockerVersion(); | ||
const requiresDockerUpdate = Boolean( | ||
minDockerVersion && | ||
valid(minDockerVersion) && | ||
valid(currentDockerVersion) && | ||
gt(minDockerVersion, currentDockerVersion) | ||
); | ||
|
||
if (requiresDockerUpdate) return minDockerVersion; | ||
return null; | ||
} |
This file contains 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 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