-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
173 lines (158 loc) · 6.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const core = require('@actions/core');
const { HttpClient } = require('@actions/http-client');
const yaml = require('js-yaml');
const fs = require('fs').promises;
const KARNOT_CLOUD_URL = process.env.KARNOT_CLOUD_URL;
const KARNOT_CLOUD_TOKEN = process.env.KARNOT_CLOUD_TOKEN;
if (!KARNOT_CLOUD_URL || !KARNOT_CLOUD_TOKEN) {
core.setFailed('KARNOT_CLOUD_URL and KARNOT_CLOUD_TOKEN must be set');
process.exit(1);
}
core.info(`Karnot Cloud URL: ${KARNOT_CLOUD_URL}`);
// Karnot Cloud API Session
function getHttpClient() {
const client = new HttpClient('karnot-cloud-action');
client.requestOptions = {
headers: {
'X-Api-Key': KARNOT_CLOUD_TOKEN
}
};
return client;
}
async function checkProjectExists(projectId) {
const client = getHttpClient();
const url = `${KARNOT_CLOUD_URL}/project/${projectId}`;
try {
const response = await client.get(url);
if (response.message.statusCode === 200) {
const resp = JSON.parse(await response.readBody()).data;
core.debug(`Project data: ${JSON.stringify(resp)}`);
core.info(`project: ${resp.name}, organization: ${resp.organisation_name}, stack: ${resp.stack}`);
return true;
}
} catch (error) {
core.error(`Error checking project: ${error.message}`);
}
return false;
}
async function updateImage(projectId, serviceName, image) {
const client = getHttpClient();
const url = `${KARNOT_CLOUD_URL}/project/deployment/image`;
try {
core.info(`Updating image to ${image} for ${serviceName} of project ${projectId}`);
const data = {image, projectId, serviceName, async: false};
core.debug(`Updating image: Request- ${JSON.stringify(data)}`);
const response = await client.postJson(url, data);
core.debug(`Updating image: Response- ${JSON.stringify(response.result)}`);
return response.statusCode === 200;
} catch (error) {
core.error(`Error updating image: ${error.message}`);
return false;
}
}
async function updateFiles(projectId, serviceName, serviceFile) {
const client = getHttpClient();
const url = `${KARNOT_CLOUD_URL}/project/deployment/chain-forge`;
let allFilesUpdated = true;
for (const [file, content] of Object.entries(serviceFile)) {
try {
core.info(`Uploading file ${file} for ${serviceName} of project ${projectId}`);
const data = {url: content, projectId, serviceName, async: false};
core.debug(`Updating files: Request- ${JSON.stringify(data)}`);
const response = await client.postJson(url, data);
core.debug(`Updating files: Response- ${JSON.stringify(response.result)}`);
if (response.statusCode !== 200) {
allFilesUpdated = false;
}
} catch (error) {
core.error(`Error updating files: ${error.message}`);
allFilesUpdated = false;
}
}
return allFilesUpdated
}
async function updateConfig(projectId, serviceName, config) {
const client = getHttpClient();
setTimeout(() => {
}, 1000);
const url = `${KARNOT_CLOUD_URL}/project/deployment/config`;
try {
core.info(`Updating config for ${serviceName} of project ${projectId}`);
const data = {config, projectId, serviceName};
core.debug(`Updating config: Request- ${JSON.stringify(data)}`);
const response = await client.postJson(url, { config, projectId, serviceName, async: false });
core.debug(`Updating config: Response- ${JSON.stringify(response.result)}`);
return response.statusCode === 200;
} catch (error) {
core.error(`Error updating config: ${error.message}`);
return false;
}
}
async function deployPipeline(data, environment) {
core.info(`Deploying pipeline for environment: ${environment}`);
for (const project of data.environments) {
const projectName = project.project;
if (projectName === environment) {
const projectId = project.id;
if (!(await checkProjectExists(projectId))) {
core.error(`Project with id: ${projectId} does not exist`);
continue;
}
core.info(`Deploying pipeline for project: ${projectName} with id: ${projectId}`);
const services = project.repos;
core.debug(`Project data: ${JSON.stringify(project)}`);
for (const [service, serviceData] of Object.entries(services)) {
core.info(`Service name: ${service}`);
const serviceImage = serviceData.image;
const serviceConfig = serviceData.config;
const serviceFiles = serviceData.files;
core.info(`Deploying pipeline for service: ${service} with image: ${serviceImage} and config: ${JSON.stringify(serviceConfig)}`);
if (serviceFiles) {
if (!(await updateFiles(projectId, service, serviceFiles))) {
const errorMessage = `Failed to update files for service: ${service}`
core.error(errorMessage);
throw new Error(errorMessage);
}
}
if (serviceConfig) {
if (!(await updateConfig(projectId, service, serviceConfig))) {
const errorMessage = `Failed to update config for service: ${service}`;
core.error(errorMessage);
throw new Error(errorMessage);
}
}
// wait for 15 seconds before sending a new request to the api
await new Promise(resolve => setTimeout(resolve, 15e3));
if (serviceImage) {
if (!(await updateImage(projectId, service, serviceImage))) {
const errorMessage = `Failed to update image for service: ${service}`;
core.error(errorMessage);
throw new Error(errorMessage);
}
}
}
}
}
}
async function yamlToDict(yamlFile) {
const fileContents = await fs.readFile(yamlFile, 'utf8');
return yaml.load(fileContents);
}
async function main() {
try {
const inputFile = process.env.INPUT_FILE;
const environment = process.env.ENVIRONMENT;
core.info(`Reading file: ${inputFile}`);
const data = await yamlToDict(inputFile);
core.debug(`File contents: ${JSON.stringify(data)}`);
core.info(`Environment: ${environment}`);
if (!data.environments.map(v => v.project).includes(environment)) {
core.error(`Environment ${environment} does not exist in config file`);
throw new Error(`Environment ${environment} does not exist in config file`);
}
await deployPipeline(data, environment);
} catch (error) {
return core.setFailed(error.message);
}
}
main();