Skip to content

Commit

Permalink
Detect if running inside a snap or on Ubuntu Core
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrancis committed Oct 10, 2024
1 parent bc9d82e commit 4cba700
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ apps:
plugs:
- network
- network-bind
- system-observe

parts:
python-deps:
Expand Down
48 changes: 48 additions & 0 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,41 @@ export function getOS(): string {
}
}

// If not running inside a snap, give up at this point.
if (!isSnap()) {
console.log('Unknown Linux distribution');
return 'linux-unknown';
}

// Otherwise try to detect if running on Ubuntu Core.
try {
const osReleaseLines = fs
.readFileSync('/var/lib/snapd/hostfs/etc/os-release', {
encoding: 'utf8',
})
.split('\n');
// Iterate through the file
for (let line of osReleaseLines) {
// Trim whitespace
line = line.trim();
// Find the line containing ID
if (line.startsWith('ID=')) {
// Get the value of the ID
let id = line.substring(3, line.length);
// Remove any quotation marks
id = id.replace(/"/g, '');
if (id == 'ubuntu-core') {
return 'linux-ubuntu-core';
} else {
console.log('Unknown host Linux distribution');
}
}
}
} catch (error) {
console.log(`Error trying to read os-release file: ${error}`);
console.log('Check that the system-observe interface is connected');
}

return 'linux-unknown';
}

Expand All @@ -85,6 +120,19 @@ export function isContainer(): boolean {
);
}

/**
* Determine whether or not we're running inside a snap package.
*
* @returns boolean True if running inside snap, false if not.
*/
export function isSnap(): boolean {
if (process.env.SNAP_NAME == 'webthings-gateway') {
return true;
} else {
return false;
}
}

/**
* Get the current node version.
*/
Expand Down

0 comments on commit 4cba700

Please sign in to comment.