Skip to content

Commit

Permalink
Merge pull request #205 from run-crank/rhart/RemoveBlob
Browse files Browse the repository at this point in the history
Remove screenshot blob storage
  • Loading branch information
saludangelito authored Jan 17, 2023
2 parents da4cd6a + fd61715 commit 78ce215
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 653 deletions.
563 changes: 4 additions & 559 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@
"typescript": "^4.2.4"
},
"dependencies": {
"@azure/identity": "^2.1.0",
"@azure/storage-blob": "^12.11.0",
"@run-crank/utilities": "^0.5.2",
"bluebird": "^3.5.5",
"google-protobuf": "^3.8.0",
Expand Down
4 changes: 1 addition & 3 deletions src/client/client-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ class ClientWrapper {
public client: Page;
public lighthouse: any;
public idMap: any;
public blobContainerClient: any;
public marketoClient: Marketo;
public marketoConnected: boolean = false;
public delayInSeconds: number;

constructor (page: Page, auth: grpc.Metadata, idMap: any, blobContainerClient: any, lighthouse = Lighthouse, delayInSeconds = 3) {
constructor (page: Page, auth: grpc.Metadata, idMap: any, lighthouse = Lighthouse, delayInSeconds = 3) {
this.client = page;
this.idMap = idMap;
this.blobContainerClient = blobContainerClient;
this.lighthouse = lighthouse;

// Make a marketo connection if the auth metadata is passed.
Expand Down
10 changes: 5 additions & 5 deletions src/core/cog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Cog implements ICogServiceServer {

private steps: StepInterface[];

constructor (private cluster: Cluster, private clientWrapperClass, private stepMap: any = {}, private blobContainerClient) {
constructor (private cluster: Cluster, private clientWrapperClass, private stepMap: any = {}) {
this.steps = [].concat(...Object.values(this.getSteps(`${__dirname}/../steps`, clientWrapperClass)));
}

Expand Down Expand Up @@ -90,7 +90,7 @@ export class Cog implements ICogServiceServer {
scenarioId: runStepRequest.getScenarioId(),
requestorId: runStepRequest.getRequestorId(),
};
client = await this.getClientWrapper(page, call.metadata, idMap, this.blobContainerClient);
client = await this.getClientWrapper(page, call.metadata, idMap);
clientCreated = true;
}

Expand Down Expand Up @@ -150,7 +150,7 @@ export class Cog implements ICogServiceServer {
scenarioId: runStepRequest.getScenarioId(),
requestorId: runStepRequest.getRequestorId(),
};
wrapper = this.getClientWrapper(page, metadata, idMap, this.blobContainerClient);
wrapper = this.getClientWrapper(page, metadata, idMap);
}

const stepId = step.getStepId();
Expand All @@ -174,8 +174,8 @@ export class Cog implements ICogServiceServer {
return response;
}

private getClientWrapper(page: Page, auth: grpc.Metadata, idMap: {} = null, blobContainerClient: any) {
return new this.clientWrapperClass(page, auth, idMap, blobContainerClient);
private getClientWrapper(page: Page, auth: grpc.Metadata, idMap: {} = null) {
return new this.clientWrapperClass(page, auth, idMap);
}

}
21 changes: 1 addition & 20 deletions src/core/grpc-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,10 @@ import { ClientWrapper } from '../client/client-wrapper';
import puppeteerExtra from 'puppeteer-extra';
import puppeteerExtraPluginRecaptcha from 'puppeteer-extra-plugin-recaptcha';
const stealthPlugin = require('puppeteer-extra-plugin-stealth'); // needs to use require
const { DefaultAzureCredential } = require('@azure/identity');
const { BlobServiceClient } = require('@azure/storage-blob');

const server = new grpc.Server();
const port = process.env.PORT || 28866;
const host = process.env.HOST || '0.0.0.0';
const azureTenantId = process.env.AZURE_TENANT_ID || null;
const azureClientId = process.env.AZURE_CLIENT_ID || null;
const azureClientSecret = process.env.AZURE_CLIENT_SECRET || null;
const azureStorageAccount = process.env.AZURE_BLOB_STORAGE_ACCOUNT || null;
const azureContainerName = process.env.AZURE_BLOB_STORAGE_CONTAINER || null;
let blobContainerClient;
let credentials: grpc.ServerCredentials;

if (process.env.USE_SSL) {
Expand All @@ -32,17 +24,6 @@ if (process.env.USE_SSL) {
credentials = grpc.ServerCredentials.createInsecure();
}

if (azureTenantId && azureClientId && azureClientSecret && azureStorageAccount && azureContainerName) {
const defaultAzureCredential = new DefaultAzureCredential();

const blobServiceClient = new BlobServiceClient(
`https://${azureStorageAccount}.blob.core.windows.net`,
defaultAzureCredential,
);

blobContainerClient = blobServiceClient.getContainerClient(azureContainerName);
}

async function instantiateCluster(): Promise<Cluster> {

// add stealth and recaptcha plugins
Expand Down Expand Up @@ -83,7 +64,7 @@ async function instantiateCluster(): Promise<Cluster> {
}

instantiateCluster().then((cluster) => {
server.addService(CogService, new Cog(cluster, ClientWrapper, {}, blobContainerClient));
server.addService(CogService, new Cog(cluster, ClientWrapper, {}));
server.bind(`${host}:${port}`, credentials);
server.start();
console.log(`Server started, listening: ${host}:${port}`);
Expand Down
12 changes: 1 addition & 11 deletions src/steps/take-screenshot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ContainerClient } from '@azure/storage-blob';
import { BaseStep, Field, StepInterface, ExpectedRecord } from '../core/base-step';
import { Step, RunStepResponse, FieldDefinition, StepDefinition } from '../proto/cog_pb';

Expand All @@ -19,16 +18,7 @@ export class TakeScreenshot extends BaseStep implements StepInterface {
try {
const screenshot = await this.client.client.screenshot({ type: 'jpeg', encoding: 'binary', quality: 40, fullPage: true });
const binaryRecord = this.binary('showScreenshot', 'Screenshot', 'image/jpeg', screenshot);
if (this.client.blobContainerClient) {
const blobName = `${this.client.idMap.scenarioId}-Step${stepData['__stepOrder']}`;
const blobRecord = this.binary('showScreenshot', 'Screenshot', 'blobName', blobName);
const blockBlobClient = this.client.blobContainerClient.getBlockBlobClient(blobName);
const uploadBlobResponse = await blockBlobClient.uploadData(screenshot);
console.log(`Upload block blob ${blobName} successfully`, uploadBlobResponse.requestId);
return this.pass('Successfully took a screenshot', [], [blobRecord]);
} else {
return this.pass('Successfully took a screenshot', [], [binaryRecord]);
}
return this.pass('Successfully took a screenshot', [], [binaryRecord]);
} catch (e) {
return this.error('There was a problem taking a screenshot: %s', [e.toString()], []);
}
Expand Down
Loading

0 comments on commit 78ce215

Please sign in to comment.