Skip to content

Commit

Permalink
added check to find docker on podman
Browse files Browse the repository at this point in the history
Signed-off-by: msivasubramaniaan <msivasub@redhat.com>
  • Loading branch information
msivasubramaniaan committed Feb 13, 2024
1 parent e6ea0bf commit 606a72d
Showing 1 changed file with 62 additions and 11 deletions.
73 changes: 62 additions & 11 deletions src/serverlessFunction/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import { isKnativeServingAware } from './knative';
import { multiStep } from './multiStepInput';
import { FunctionContent, FunctionObject, FunctionSession } from './types';
import Dockerode = require('dockerode');
import { CliExitData, ChildProcessUtil } from '../util/childProcessUtil';

interface DockerStatus {
error: boolean;
message: string;
}

export class Functions {

Expand Down Expand Up @@ -142,9 +148,20 @@ export class Functions {
}
}

private async checkDocker(): Promise<boolean> {
let dockerStatus: DockerStatus = await this.isDockerRunning();

Check warning on line 152 in src/serverlessFunction/functions.ts

View check run for this annotation

Codecov / codecov/patch

src/serverlessFunction/functions.ts#L151-L152

Added lines #L151 - L152 were not covered by tests
if (dockerStatus.error) {
dockerStatus = await this.isDockerOnPodman();

Check warning on line 154 in src/serverlessFunction/functions.ts

View check run for this annotation

Codecov / codecov/patch

src/serverlessFunction/functions.ts#L154

Added line #L154 was not covered by tests
if (dockerStatus.error) {
void window.showErrorMessage(dockerStatus.message)
return false;

Check warning on line 157 in src/serverlessFunction/functions.ts

View check run for this annotation

Codecov / codecov/patch

src/serverlessFunction/functions.ts#L156-L157

Added lines #L156 - L157 were not covered by tests
}
}
return true;

Check warning on line 160 in src/serverlessFunction/functions.ts

View check run for this annotation

Codecov / codecov/patch

src/serverlessFunction/functions.ts#L160

Added line #L160 was not covered by tests
}

public async build(context: FunctionObject, s2iBuild: boolean): Promise<void> {
const isDockerRunning = await this.isDockerRunning();
if (!isDockerRunning) {
if (! await this.checkDocker()) {
return;

Check warning on line 165 in src/serverlessFunction/functions.ts

View check run for this annotation

Codecov / codecov/patch

src/serverlessFunction/functions.ts#L165

Added line #L165 was not covered by tests
}
const existingTerminal: OpenShiftTerminalApi = this.buildTerminalMap.get(`build-${context.folderURI.fsPath}`);
Expand Down Expand Up @@ -200,8 +217,7 @@ export class Functions {
}

public async run(context: FunctionObject, runBuild = false) {
const isDockerRunning = await this.isDockerRunning();
if (!isDockerRunning) {
if (! await this.checkDocker()) {
return;

Check warning on line 221 in src/serverlessFunction/functions.ts

View check run for this annotation

Codecov / codecov/patch

src/serverlessFunction/functions.ts#L221

Added line #L221 was not covered by tests
}
const terminal = await OpenShiftTerminalManager.getInstance().createTerminal(
Expand Down Expand Up @@ -374,21 +390,56 @@ export class Functions {
return null;
}

private async isDockerRunning(): Promise<boolean> {
return new Promise<boolean>((resolve) => {
private async isDockerRunning(): Promise<DockerStatus> {
return new Promise<DockerStatus>((resolve) => {
try {
const docker = new Dockerode();
docker.ping((err) => {

Check warning on line 397 in src/serverlessFunction/functions.ts

View check run for this annotation

Codecov / codecov/patch

src/serverlessFunction/functions.ts#L393-L397

Added lines #L393 - L397 were not covered by tests
if (err) {
void window.showErrorMessage('Docker is not running, Please start the docker process');
resolve(false);
resolve({

Check warning on line 399 in src/serverlessFunction/functions.ts

View check run for this annotation

Codecov / codecov/patch

src/serverlessFunction/functions.ts#L399

Added line #L399 was not covered by tests
error: true,
message: 'Docker is not running, Please start the docker process'
});
}
resolve(true);
resolve({

Check warning on line 404 in src/serverlessFunction/functions.ts

View check run for this annotation

Codecov / codecov/patch

src/serverlessFunction/functions.ts#L404

Added line #L404 was not covered by tests
error: false,
message: ''
});
});
} catch (e) {
void window.showErrorMessage('Docker is not installed, Please install and start the docker process');
resolve(false);
resolve({

Check warning on line 410 in src/serverlessFunction/functions.ts

View check run for this annotation

Codecov / codecov/patch

src/serverlessFunction/functions.ts#L410

Added line #L410 was not covered by tests
error: true,
message: 'Docker not installed, Please install and start the docker process'
});
}
});
}

private async isDockerOnPodman(): Promise<DockerStatus> {
try {
const resultRaw: CliExitData = await ChildProcessUtil.Instance.execute('podman info -f=json');

Check warning on line 420 in src/serverlessFunction/functions.ts

View check run for this annotation

Codecov / codecov/patch

src/serverlessFunction/functions.ts#L418-L420

Added lines #L418 - L420 were not covered by tests
if (resultRaw.stderr.toLowerCase().indexOf('cannot connect') !== -1) {
return ({

Check warning on line 422 in src/serverlessFunction/functions.ts

View check run for this annotation

Codecov / codecov/patch

src/serverlessFunction/functions.ts#L422

Added line #L422 was not covered by tests
error: true,
message: 'Docker is not running, Please start the docker process'
});
}
const resultObj: { registries: { search: string[] } } = JSON.parse(resultRaw.stdout);

Check warning on line 427 in src/serverlessFunction/functions.ts

View check run for this annotation

Codecov / codecov/patch

src/serverlessFunction/functions.ts#L427

Added line #L427 was not covered by tests
if (resultObj.registries && !resultObj.registries.search?.includes('docker.io')) {
return ({

Check warning on line 429 in src/serverlessFunction/functions.ts

View check run for this annotation

Codecov / codecov/patch

src/serverlessFunction/functions.ts#L429

Added line #L429 was not covered by tests
error: true,
message: 'Docker is not running, Please start the docker process'
});
}
return ({

Check warning on line 434 in src/serverlessFunction/functions.ts

View check run for this annotation

Codecov / codecov/patch

src/serverlessFunction/functions.ts#L434

Added line #L434 was not covered by tests
error: false,
message: ''
})
} catch (e) {
return ({

Check warning on line 439 in src/serverlessFunction/functions.ts

View check run for this annotation

Codecov / codecov/patch

src/serverlessFunction/functions.ts#L439

Added line #L439 was not covered by tests
error: true,
message: 'Docker not installed, Please install and start the docker process'
});
}
}
}

0 comments on commit 606a72d

Please sign in to comment.