Skip to content

Commit

Permalink
automate docker and dos2unix installation in ec2
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayush272002 committed Dec 11, 2024
1 parent 7437fe0 commit e4e1863
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
37 changes: 37 additions & 0 deletions apps/engine/src/utils/ec2Setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { NodeSSH } from 'node-ssh';

export async function installDos2unix(ssh: NodeSSH) {
console.log('Installing dos2unix...');
const result = await ssh.execCommand(
'sudo apt-get update && sudo apt-get install -y dos2unix'
);
if (result.code !== 0) {
console.error('Failed to install dos2unix:', result.stderr);
throw new Error('Failed to install dos2unix');
}
console.log('dos2unix installed successfully.');
}

export async function installDocker(ssh: NodeSSH) {
console.log('Installing Docker...');
const commands = [
'sudo apt-get update',
'sudo apt-get install -y ca-certificates curl',
'sudo install -m 0755 -d /etc/apt/keyrings',
'sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc',
'sudo chmod a+r /etc/apt/keyrings/docker.asc',
'echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null',
'sudo apt-get update',
'sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin',
];

for (const command of commands) {
const result = await ssh.execCommand(command);
if (result.code !== 0) {
console.error(`Failed to execute command: ${command}`);
console.error('Error:', result.stderr);
throw new Error('Failed to install Docker');
}
}
console.log('Docker installed successfully.');
}
5 changes: 5 additions & 0 deletions apps/engine/src/utils/submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NodeSSH } from 'node-ssh';
import fs from 'fs';
import path from 'path';
import prisma from '@repo/db/client';
import { installDocker, installDos2unix } from './ec2Setup';

async function convertLineEndings(ssh: NodeSSH, filePath: string) {
const result = await ssh.execCommand(`
Expand Down Expand Up @@ -35,6 +36,10 @@ export async function processSubmission(submission: any) {
await ssh.connect({ host, username, privateKey });
console.log('Connected to EC2 instance.');

console.log('Installing required packages on EC2...');
await installDos2unix(ssh);
await installDocker(ssh);

const remoteWorkDir = `/tmp/${assignmentId}_work`;

console.log('Creating working directory on EC2...');
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ services:
engine:
image: ayush272002/automarker-engine:latest
environment:
- KAFKA_URI=sepp-
- KAFKA_URI=
- KAFKA_USERNAME=
- KAFKA_PASSWORD=
- DATABASE_URL=
Expand Down

0 comments on commit e4e1863

Please sign in to comment.