Skip to content
This repository has been archived by the owner on Aug 12, 2022. It is now read-only.

Commit

Permalink
Harden and test bigip serivce.
Browse files Browse the repository at this point in the history
  • Loading branch information
zongzw committed Nov 21, 2019
1 parent 186bf39 commit 7c9536c
Show file tree
Hide file tree
Showing 4 changed files with 456 additions and 160 deletions.
215 changes: 81 additions & 134 deletions app/waf/src/services/bigip.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,44 +87,34 @@ export class BigIpManager {
await this.mustBeReachable();

let url = `${this.baseUrl}/mgmt/tm/net/interface`;
let response = await this.bigipService.getInfo(url, this.cred64Encoded);
let resObj = JSON.parse(JSON.stringify(response))['body'][0];
this.logger.debug(`get ${url} responses: ${JSON.stringify(resObj)}`);

let impFunc = async () => {
let response = await this.bigipService.getInfo(url, this.cred64Encoded);
let resObj = JSON.parse(JSON.stringify(response))['body'][0];
this.logger.debug(`get ${url} responses: ${JSON.stringify(resObj)}`);
return resObj;
};
let items = resObj['items'];
let interfaces: BigipInterfaces = {};
for (let intf of items) {
let macAddr = intf.macAddress;
interfaces[macAddr] = {
name: intf.name,
macAddress: macAddr,
};
}

return interfaces;
}

// The interface mac addresses are 'none' at the very beginning of the bigip readiness.
// So we need to check and wait it becomes non-none.
async getInterfacesNoNone(): Promise<BigipInterfaces> {
let infs: BigipInterfaces;
let checkFunc = async () => {
return await impFunc().then(resObj => {
let items = resObj['items'];
for (let intf of items) {
if (intf.macAddress === 'none') {
this.logger.warn("bigip interface's mac addr is 'none', waiting..");
return false;
}
}
this.logger.debug('bigip mac addresses are ready to get.');
return true;
});
infs = await this.getInterfaces();
return Object.keys(infs).indexOf('none') < 0;
};

// The interface mac addresses are 'none' at the very beginning of the bigip readiness.
return await checkAndWait(checkFunc, 60).then(
async () => {
return await impFunc().then(resObj => {
let items = resObj['items'];
let interfaces: BigipInterfaces = {};
for (let intf of items) {
let macAddr = intf.macAddress;
interfaces[macAddr] = {
name: intf.name,
macAddress: macAddr,
};
}
return interfaces;
});
},
() => infs,
() => {
throw new Error('bigip mac addresses are not ready to get.');
},
Expand Down Expand Up @@ -163,50 +153,17 @@ export class BigIpManager {
await this.mustBeReachable();

let url = `${this.baseUrl}/mgmt/tm/cm/device`;
let response = await this.bigipService.getInfo(url, this.cred64Encoded);
let resObj = JSON.parse(JSON.stringify(response))['body'][0];
this.logger.debug(`get ${url} responses: ${JSON.stringify(resObj)}`);

let impFunc = async () => {
let response = await this.bigipService.getInfo(url, this.cred64Encoded);
let resObj = JSON.parse(JSON.stringify(response))['body'][0];
this.logger.debug(`get ${url} responses: ${JSON.stringify(resObj)}`);
return resObj;
};

let checkFunc = async () => {
return await impFunc().then(resObj => {
let items = resObj['items'];
for (let item of items) {
if (
item.managementIp === this.config.ipAddr &&
item.configsyncIp !== 'none'
) {
return true;
} else {
this.logger.warn('No configsync IP, waiting...');
return false;
}
}
this.logger.debug('Configsync IP is ready.');
return true;
});
};

return await checkAndWait(checkFunc, 60).then(
async () => {
return await impFunc().then(resObj => {
let items = resObj['items'];
let ip = '';
for (let item of items) {
if (item.managementIp === this.config.ipAddr) {
ip = item.configsyncIp;
}
}
return ip;
});
},
() => {
throw new Error('No configsync IP');
},
);
let items = resObj['items'];
for (let item of items) {
if (item.managementIp === this.config.ipAddr) {
return item.configsyncIp;
}
}
throw new Error('No configsync IP');
}

async getPartition(partition: string): Promise<string> {
Expand All @@ -227,33 +184,28 @@ export class BigIpManager {
return resObj;
}

async uploadDO(): Promise<string> {
async uploadDO(): Promise<object> {
await this.mustBeReachable();
const filename = process.env.DO_RPM_PACKAGE!;
let fs = require('fs');
if (!filename || filename === '' || !fs.existsSync(filename)) {
throw new Error(`DO RPM file doesn't exist: '${filename}'`);
}
let fstats = fs.statSync(filename);
try {
let url = `${
this.baseUrl
}/mgmt/shared/file-transfer/uploads/${path.basename(filename)}`;
let buffer = fs.readFileSync(filename, {endcoding: 'utf8'});
let response = await this.bigipService.uploadFile(
url,
this.cred64Encoded,
fstats.size - 1,
fstats.size,
buffer,
);
let resObj = JSON.stringify(response);
return resObj;
} catch (error) {
throw new Error(
`Upload DO RPM file error with error message ${error.message}`,
);
}

let url = `${
this.baseUrl
}/mgmt/shared/file-transfer/uploads/${path.basename(filename)}`;
let buffer = fs.readFileSync(filename, {endcoding: 'utf8'});
let response = await this.bigipService.uploadFile(
url,
this.cred64Encoded,
fstats.size - 1,
fstats.size,
buffer,
);
let resObj = JSON.parse(JSON.stringify(response)).body[0];
return resObj;
}

async installDO(): Promise<string> {
Expand All @@ -264,50 +216,45 @@ export class BigIpManager {
process.env.DO_RPM_PACKAGE!,
)}`,
};
try {
let url = `${this.baseUrl}/mgmt/shared/iapp/package-management-tasks`;
let response = await this.bigipService.installObject(
url,
this.cred64Encoded,
body,
);
let taskid = JSON.parse(JSON.stringify(response))['body'][0]['id'];
let dourl = `${this.baseUrl}/mgmt/shared/iapp/package-management-tasks/${taskid}`;

let impFunc = async () => {
let checkinfo = await this.bigipService.getInfo(
dourl,
this.cred64Encoded,
);
let resObj = JSON.parse(JSON.stringify(checkinfo))['body'][0];
this.logger.debug(`get ${url} responses: ${JSON.stringify(resObj)}`);
return resObj;
};
let url = `${this.baseUrl}/mgmt/shared/iapp/package-management-tasks`;
let response = await this.bigipService.installObject(
url,
this.cred64Encoded,
body,
);
let taskid = JSON.parse(JSON.stringify(response))['body'][0]['id'];
let dourl = `${this.baseUrl}/mgmt/shared/iapp/package-management-tasks/${taskid}`;

let checkFunc = async () => {
return await impFunc().then(resObj => {
let status = resObj['status'];
if (status === 'FINISHED') return true;
});
};
let status: string;
let resChk: object;

return await checkAndWait(checkFunc, 60).then(
async () => {
return await impFunc().then(resObj => {
let status = resObj['status'];
return status;
});
},
() => {
throw new Error('Install DO failed.');
},
);
} catch (error) {
throw new Error(
`Install DO RPM file error with error message ${error.message}`,
let checkFunc = async () => {
let checkinfo = await this.bigipService.getInfo(
dourl,
this.cred64Encoded,
);
}
let resObj = JSON.parse(JSON.stringify(checkinfo))['body'][0];
this.logger.debug(`get ${dourl} responses: ${JSON.stringify(resObj)}`);
status = resObj['status'];
resChk = resObj;

if (status === 'FAILED') return Promise.reject(true);
return status === 'FINISHED';
};

return await checkAndWait(checkFunc, 60).then(
async () => status,
() => {
throw new Error(
`Install DO failed: (status: ${status}, detail: ${JSON.stringify(
resChk,
)})`,
);
},
);
}

async getAS3Info(): Promise<object> {
await this.mustBeReachable();

Expand Down
2 changes: 1 addition & 1 deletion app/waf/src/services/do.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ export class OnboardingManager {
},
this.reqId,
).then(async bigipMgr => {
return await bigipMgr.getInterfaces();
return await bigipMgr.getInterfacesNoNone();
}),
subnets: await this.subnetInfo(obData, addon),
onboarding: addon.onboarding === true,
Expand Down
Loading

0 comments on commit 7c9536c

Please sign in to comment.