Skip to content

Commit

Permalink
Merge pull request #17 from fluentci-io/feat/flu-25-link-agent-to-user
Browse files Browse the repository at this point in the history
feat: link fluentci agent to user
  • Loading branch information
tsirysndr authored Jan 11, 2024
2 parents df2700d + cade967 commit e69304f
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/cmd/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ async function startAgent() {
Deno.exit(1);
}

const accessToken = await getAccessToken();

const uuid = await getWebSocketUuid(id);
const websocket = new WebSocket(
`${FLUENTCI_WS_URL}?agent_id=${id}&token=${accessToken}`
`${FLUENTCI_WS_URL}?agent_id=${id}&uuid=${uuid}`
);
websocket.onopen = function () {
logger.info(`Connected to FluentCI server as ${brightGreen(id)}`);
logger.info(`uuid: ${brightGreen(uuid)}`);
logger.info("FluentCI Agent started successfully ✅");
logger.info("Waiting for jobs ...");
logger.info("Press Ctrl+C to exit");
Expand Down Expand Up @@ -109,20 +109,16 @@ async function startAgent() {
});
}

const exists = (path: string) => {
function exists(path: string) {
try {
Deno.statSync(path);
return true;
} catch (_) {
return false;
}
};
}

const extractZipBlob = async (
blob: Blob,
project_id: string,
sha256: string
) => {
async function extractZipBlob(blob: Blob, project_id: string, sha256: string) {
const zipReader = new ZipReader(new BlobReader(blob));
for (const entry of await zipReader.getEntries()) {
const [_, ...path] = entry.filename.split("/").reverse();
Expand All @@ -141,16 +137,16 @@ const extractZipBlob = async (
new Uint8Array(data)
);
}
};
}

const spawnFluentCI = async (
async function spawnFluentCI(
logger: Logger,
project_id: string,
sha256: string,
pipeline: string,
jobs: [string, ...Array<string>],
clientId: string
) => {
) {
const accessToken = await getAccessToken();
const headers = {
Authorization: `Bearer ${accessToken}`,
Expand Down Expand Up @@ -197,6 +193,16 @@ const spawnFluentCI = async (
headers,
}),
]).catch((e) => logger.error(e.message));
};
}

async function getWebSocketUuid(agentId: string) {
const accessToken = await getAccessToken();
const uuid = await fetch(`${FLUENTCI_EVENTS_URL}/auth?agent_id=${agentId}`, {
method: "GET",
headers: {
Authorization: `Bearer ${accessToken}`,
},
}).then((res) => res.text());
return uuid;
}
export default startAgent;

0 comments on commit e69304f

Please sign in to comment.