Skip to content

Commit

Permalink
chore: update ci scripts & workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Orkuncakilkaya committed Nov 6, 2023
1 parent 42f86fd commit e0bf0a3
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 73 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ jobs:
AK_GROUP_ID: '${{secrets.AK_GROUP_ID}}'
AK_CONTRACT_ID: '${{secrets.AK_CONTRACT_ID}}'
run: yarn ts-node scripts/createProperty.ts
- name: Build Patch Body
run: yarn build --type patchBody
- name: Deploy Akamai Rules
id: deploy-rules
env:
Expand Down
28 changes: 13 additions & 15 deletions scripts/checkPropertyCanBeCreated.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import {eg} from "./utils/edgeGrid";
import {getProperty} from "./utils/getProperty";

eg.auth({
path: `/papi/v1/properties?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`,
method: 'GET',
})

eg.send((err, response, body) => {
const {properties} = JSON.parse(body ?? '{}');
if (!properties) {
const handler = async () => {
try {
const property = await getProperty();
if (property) {
console.log('Property already exists');
process.exit(1);
}
} catch (e) {
console.log('Error during property check:', e);
process.exit(1);
}
const property = properties.items.find(t => t.propertyName === `${process.env.BRANCH_NAME}.cfi-fingerprint.com`);
if (property) {
process.exit(1)
}
process.exit(0)
});
}

handler().finally();
147 changes: 114 additions & 33 deletions scripts/createProperty.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,122 @@
import {eg} from "./utils/edgeGrid";

eg.auth({
path: `/papi/v1/properties?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`,
method: 'POST',
body: JSON.stringify({
productId: "Site_Accel",
propertyName: `${process.env.BRANCH_NAME}.cfi-fingerprint.com`,
ruleFormat: "latest"
})
})
import {akamaiRequest} from "./utils/akamaiRequest";
import {getProperty} from "./utils/getProperty";
import {CI_DOMAIN} from "./utils/constants";

eg.send((err, response, body) => {
if (!response) {
process.exit(1)
}
if (response?.status >= 200 && response?.status < 300) {
eg.auth({
path: `/papi/v1/edgehostnames?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`,
const createProperty = async () => {
await akamaiRequest({
path: `/papi/v1/properties?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`,
method: 'POST',
body: JSON.stringify({
productId: "Site_Accel",
propertyName: CI_DOMAIN,
ruleFormat: "latest"
})
});
const {propertyId} = await getProperty();
return propertyId;
}

const createEdgeHostname = async () => {
await akamaiRequest({
path: `/papi/v1/edgehostnames?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`,
method: 'POST',
body: JSON.stringify({
ipVersionBehavior: 'IPV4',
domainPrefix: CI_DOMAIN,
domainSuffix: `edgesuite.net`,
productId: `Site_Accel`,
})
});

return findEdgeHostname()
}

const createCPCode = async () => {
let cpcodeId = await findCpcode();
if (!cpcodeId) {
await akamaiRequest({
path: `/papi/v1/cpcodes?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`,
method: 'POST',
body: JSON.stringify({
ipVersionBehavior: 'IPV4',
domainPrefix: `${process.env.BRANCH_NAME}.cfi-fingerprint.com`,
domainSuffix: `edgesuite.net`,
cpcodeName: CI_DOMAIN,
productId: `Site_Accel`,
})
}),
});
}

eg.send((err, response, body) => {
if (!response) {
process.exit(1)
}
if (response?.status >= 200 && response?.status < 300) {
process.exit(0)
return findCpcode()
}

const findCpcode = async () => {
const {body: {cpcodes: {items}}} = await akamaiRequest({
path: `/papi/v1/cpcodes?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`,
method: 'GET',
});

const cpcode = items.find(t => t.cpcodeName === CI_DOMAIN);
return cpcode?.cpcodeId;
};

const patchCpcode = async (propertyId: string, cpcodeId: string) => akamaiRequest({
path: `/papi/v1/properties/${propertyId}/versions/1/rules?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`,
method: 'PATCH',
headers: {
"Content-Type": "application/json-patch+json",
},
body: [
{
op: "replace",
path: "/rules/children/0/children/0/behaviors/0/options",
value: {
value: {
id: Number(cpcodeId.replace('cpc_', '')),
},
}
process.exit(1);
});
process.exit(0)
}
]
});

const findEdgeHostname = async () => {
const {body: {edgeHostnames: {items}}} = await akamaiRequest({
path: `/papi/v1/edgehostnames?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`,
method: 'GET',
});
return items.find(t => t.domainPrefix === CI_DOMAIN);
}

const patchEdgeHostname = async (propertyId: string) => {
await akamaiRequest({
path: `/papi/v1/properties/${propertyId}/versions/1/hostnames`,
method: 'PATCH',
body: JSON.stringify({
"add": [
{
"cnameType": "EDGE_HOSTNAME",
"cnameFrom": CI_DOMAIN,
"cnameTo": `${CI_DOMAIN}.edgesuite.net`
},
]
})
})
};

const handler = async () => {
try {
let {propertyId} = await getProperty();
if (!propertyId) {
propertyId = await createProperty();
}
const hostname = await findEdgeHostname();
if (!hostname) {
await createEdgeHostname();
}
await patchEdgeHostname(propertyId);
const cpcodeId = await createCPCode();
await patchCpcode(propertyId, cpcodeId);
} catch (e: any) {
console.error(e)
process.exit(1);
}
process.exit(1)
});
}

handler().finally();
151 changes: 126 additions & 25 deletions scripts/deployRules.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,134 @@
import {patchBody} from "../build";
import {eg} from "./utils/edgeGrid";
import {akamaiRequest} from "./utils/akamaiRequest";
import {getProperty} from "./utils/getProperty";
import {ORIGIN_DOMAIN} from "./utils/constants";

patchBody({
integrationPath: 'worker',
agentPath: 'agent',
resultPath: 'result',
proxySecret: 'abc123',
})
const getLatestVersion = async (propertyId: string) => {
const {body} = await akamaiRequest({
path: `/papi/v1/properties/${propertyId}/versions?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`,
method: 'GET',
});

import('../dist/patch-body/body.json').then(module => {
const content = module.default as object;
return body.versions.items[0];
};

eg.auth({
path: `/papi/v1/properties?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`,
method: 'GET',
const createNewVersion = async (propertyId: string) => {
const latestVersion = await getLatestVersion(propertyId);
await akamaiRequest({
path: `/papi/v1/properties/${propertyId}/versions?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`,
method: 'POST',
body: JSON.stringify({
createFromVersion: latestVersion.propertyVersion,
createFromVersionEtag: latestVersion.etag,
})
});

return getLatestVersion(propertyId);
};

const patchOriginHostname = async (propertyId: string, version: string) => akamaiRequest({
path: `/papi/v1/properties/${propertyId}/versions/${version}/rules?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`,
method: 'PATCH',
headers: {
"Content-Type": "application/json-patch+json",
},
body: JSON.stringify([
{
op: "add",
path: "/rules/behaviors/0/options/hostname",
value: ORIGIN_DOMAIN,
},
{
op: "replace",
path: "/rules/behaviors/0/options/cacheKeyHostname",
value: "ORIGIN_HOSTNAME",
},
{
op: "replace",
path: "/rules/behaviors/0/options/forwardHostHeader",
value: "ORIGIN_HOSTNAME",
}
])
});

const patchRemoveFingerprintRules = async(propertyId: string, version: string) => akamaiRequest({
path: `/papi/v1/properties/${propertyId}/versions/${version}/rules?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`,
method: 'PATCH',
headers: {
"Content-Type": "application/json-patch+json",
},
body: JSON.stringify([
{
op: "remove",
path: "/rules/children/6",
}
])
});

const patchAddFingerprintRules = async (propertyId: string, version: string, content: string) => {
await akamaiRequest({
path: `/papi/v1/properties/${propertyId}/versions/${version}/rules?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`,
method: 'PATCH',
headers: {
"Content-Type": "application/json-patch+json",
},
body: content,
})
}

eg.send((err, response, body) => {
const {properties} = JSON.parse(body ?? '{}');
const property = properties.items.find(t => t.propertyName === `${process.env.BRANCH_NAME}.cfi-fingerprint.com`);
const clearVariables = async (propertyId: string, version: string) => {
await akamaiRequest({
path: `/papi/v1/properties/${propertyId}/versions/${version}/rules?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`,
method: 'PATCH',
headers: {
"Content-Type": "application/json-patch+json",
},
body: JSON.stringify([
{
"op": "replace",
"path": "/rules/variables",
"value": []
}
])
})
};

eg.auth({
path: `/papi/v1/properties/${property.propertyId}/versions/${property.latestVersion}/rules?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`,
method: 'PATCH',
body: JSON.stringify(content),
});
const activateVersion = async (propertyId: string, version: string) => {
await akamaiRequest({
path: `/papi/v1/properties/${propertyId}/activations?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`,
method: 'POST',
body: JSON.stringify({
propertyVersion: version,
network: "PRODUCTION",
notifyEmails: [
"[email protected]",
],
acknowledgeAllWarnings: true,
activationType: "ACTIVATE",

eg.send((err, response, body) => {
// TODO: Deploy Successful
});
});
}),
})
}

import('../dist/patch-body/body.json').then((module) => {
const content = module.default as object;

const handler = async () => {
try {
const {propertyId} = await getProperty()
const {propertyVersion} = await createNewVersion(propertyId);
await patchOriginHostname(propertyId, propertyVersion);
await clearVariables(propertyId, propertyVersion);
try {
await patchRemoveFingerprintRules(propertyId, propertyVersion);
} catch (_) {
// Ignore error if fingerprint rules not exists
}
await patchAddFingerprintRules(propertyId, propertyVersion, JSON.stringify(content));
await activateVersion(propertyId, propertyVersion);
} catch (e: any) {
console.error(e.error)
process.exit(1)
}
};
handler().finally();
})
23 changes: 23 additions & 0 deletions scripts/utils/akamaiRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {eg} from "./edgeGrid";
import {AxiosResponse} from "axios";

type AkamaiResponse<ResponseBody extends any | any [] = any> = {
response: AxiosResponse;
body: ResponseBody;
}

export const akamaiRequest = async <ResponseBody = any>(authOptions: any,) => {
return new Promise<AkamaiResponse<ResponseBody>>((resolve, reject) => {
eg.auth(authOptions);
eg.send((err, response, body) => {
if (err) {
reject({error: err});
}
if (!err && response && response.status >= 200 && response.status < 300 && body) {
resolve({body: JSON.parse(body ?? '{}'), response});
} else {
reject({error: body, response});
}
});
});
}
2 changes: 2 additions & 0 deletions scripts/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const CI_DOMAIN = `${process.env.BRANCH_NAME}.cfi-fingerprint.com`;
export const ORIGIN_DOMAIN = "origin-akamai.cfi-fingerprint.com";
Loading

0 comments on commit e0bf0a3

Please sign in to comment.