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

Detect if running inside a snap or on Ubuntu Core #3165

Merged
merged 1 commit into from
Oct 10, 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
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
Loading