Skip to content

Commit

Permalink
Fix buildah issue of using overlay as storage driver (#51)
Browse files Browse the repository at this point in the history
Work around #45

Signed-off-by: divyansh42 <diagrawa@redhat.com>
  • Loading branch information
divyansh42 authored Apr 9, 2021
1 parent 65f18d4 commit 6dbeb7e
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 27 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/dockerfile_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ jobs:
RUN echo "hello world"
EOF
# Workaround to fix https://github.com/containers/buildah/issues/3120
- run: |
sudo apt-get install fuse-overlayfs
mkdir -vp ~/.config/containers
printf "[storage.options]\nmount_program=\"/usr/bin/fuse-overlayfs\"" > ~/.config/containers/storage.conf
# Build image using Buildah action
- name: Build Image
id: build_image
Expand Down
6 changes: 0 additions & 6 deletions .github/workflows/scratch_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ jobs:
path: ${{ env.MVN_REPO_DIR }}
key: ${{ env.MVN_HASH }}

# Workaround to fix https://github.com/containers/buildah/issues/3120
- run: |
sudo apt-get install fuse-overlayfs
mkdir -vp ~/.config/containers
printf "[storage.options]\nmount_program=\"/usr/bin/fuse-overlayfs\"" > ~/.config/containers/storage.conf
# Build image using Buildah action
- name: Build Image
id: build_image
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
"@actions/core": "^1.2.6",
"@actions/exec": "^1.0.4",
"@actions/io": "^1.0.2",
"ini": "^2.0.0",
"language-recognizer": "0.0.1"
},
"devDependencies": {
"@redhat-actions/action-io-generator": "^1.5.0",
"@redhat-actions/eslint-config": "^1.2.11",
"@redhat-actions/tsconfig": "^1.1.1",
"@types/ini": "^1.3.30",
"@types/node": "^12",
"@typescript-eslint/eslint-plugin": "^4.14.1",
"@typescript-eslint/parser": "^4.14.1",
Expand Down
77 changes: 65 additions & 12 deletions src/buildah.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as core from "@actions/core";
import * as exec from "@actions/exec";
import * as path from "path";
import CommandResult from "./types";
import { isStorageDriverOverlay, findFuseOverlayfsPath } from "./utils";

export interface BuildahConfigSettings {
entrypoint?: string[];
Expand All @@ -25,10 +26,32 @@ interface Buildah {
export class BuildahCli implements Buildah {
private readonly executable: string;

public storageOptsEnv = "";

constructor(executable: string) {
this.executable = executable;
}

// Checks for storage driver if found "overlay",
// then checks if "fuse-overlayfs" is installed.
// If yes, add mount program to use "fuse-overlayfs"
async setStorageOptsEnv(): Promise<void> {
if (await isStorageDriverOverlay()) {
const fuseOverlayfsPath = await findFuseOverlayfsPath();
if (fuseOverlayfsPath) {
core.info(`Overriding storage mount_program with "fuse-overlayfs" in environment`);
this.storageOptsEnv = `overlay.mount_program=${fuseOverlayfsPath}`;
}
else {
core.warning(`"fuse-overlayfs" is not found. Install it before running this action. `
+ `For more detail see https://github.com/redhat-actions/buildah-build/issues/45`);
}
}
else {
core.info("Storage driver is not 'overlay', so not overriding storage configuration");
}
}

private static getImageFormatOption(useOCI: boolean): string[] {
return [ "--format", useOCI ? "oci" : "docker" ];
}
Expand Down Expand Up @@ -140,7 +163,10 @@ export class BuildahCli implements Buildah {
return `${arrayAsString.slice(0, -1)}]`;
}

async execute(args: string[], execOptions: exec.ExecOptions = {}): Promise<CommandResult> {
async execute(
args: string[],
execOptions: exec.ExecOptions & { group?: boolean } = {},
): Promise<CommandResult> {
// ghCore.info(`${EXECUTABLE} ${args.join(" ")}`)

let stdout = "";
Expand All @@ -158,20 +184,47 @@ export class BuildahCli implements Buildah {
},
};

const exitCode = await exec.exec(this.executable, args, finalExecOptions);
if (execOptions.group) {
const groupName = [ this.executable, ...args ].join(" ");
core.startGroup(groupName);
}

// To solve https://github.com/redhat-actions/buildah-build/issues/45
const execEnv: { [key: string] : string } = {};
Object.entries(process.env).forEach(([ key, value ]) => {
if (value != null) {
execEnv[key] = value;
}
});

if (this.storageOptsEnv) {
execEnv.STORAGE_OPTS = this.storageOptsEnv;
}

finalExecOptions.env = execEnv;

if (execOptions.ignoreReturnCode !== true && exitCode !== 0) {
// Throwing the stderr as part of the Error makes the stderr
// show up in the action outline, which saves some clicking when debugging.
let error = `${path.basename(this.executable)} exited with code ${exitCode}`;
if (stderr) {
error += `\n${stderr}`;
try {
const exitCode = await exec.exec(this.executable, args, finalExecOptions);

if (execOptions.ignoreReturnCode !== true && exitCode !== 0) {
// Throwing the stderr as part of the Error makes the stderr
// show up in the action outline, which saves some clicking when debugging.
let error = `${path.basename(this.executable)} exited with code ${exitCode}`;
if (stderr) {
error += `\n${stderr}`;
}
throw new Error(error);
}
throw new Error(error);

return {
exitCode, output: stdout, error: stderr,
};
}

return {
exitCode, output: stdout, error: stderr,
};
finally {
if (execOptions.group) {
core.endGroup();
}
}
}
}
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export async function run(): Promise<void> {
const cli: BuildahCli = new BuildahCli(buildahPath);

// print buildah version
await cli.execute([ "version" ]);
await cli.execute([ "version" ], { group: true });

// Check if fuse-overlayfs exists and find the storage driver
await cli.setStorageOptsEnv();

const DEFAULT_TAG = "latest";
const workspace = process.env.GITHUB_WORKSPACE || process.cwd();
Expand Down
56 changes: 56 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as ini from "ini";
import { promises as fs } from "fs";
import * as core from "@actions/core";
import * as path from "path";
import * as io from "@actions/io";
import * as os from "os";

async function findStorageDriver(filePaths: string[]): Promise<string> {
let storageDriver = "";
for (const filePath of filePaths) {
core.debug(`Checking if the storage file exists at ${filePath}`);
if (await fileExists(filePath)) {
core.debug(`Storage file exists at ${filePath}`);
const fileContent = ini.parse(await fs.readFile(filePath, "utf-8"));
if (fileContent.storage.driver) {
storageDriver = fileContent.storage.driver;
}
}
}
return storageDriver;
}

export async function isStorageDriverOverlay(): Promise<boolean> {
let xdgConfigHome = path.join(os.homedir(), ".config");
if (process.env.XDG_CONFIG_HOME) {
xdgConfigHome = process.env.XDG_CONFIG_HOME;
}
const filePaths: string[] = [
"/etc/containers/storage.conf",
path.join(xdgConfigHome, "containers/storage.conf"),
];
const storageDriver = await findStorageDriver(filePaths);
return (storageDriver === "overlay");
}

async function fileExists(filePath: string): Promise<boolean> {
try {
await fs.access(filePath);
return true;
}
catch (err) {
return false;
}
}

export async function findFuseOverlayfsPath(): Promise<string | undefined> {
let fuseOverlayfsPath;
try {
fuseOverlayfsPath = await io.which("fuse-overlayfs");
}
catch (err) {
core.debug(err);
}

return fuseOverlayfsPath;
}

0 comments on commit 6dbeb7e

Please sign in to comment.