Skip to content

Commit

Permalink
readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ankithreddypati committed Dec 5, 2023
1 parent 3d2f822 commit 1021ef3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 39 deletions.
35 changes: 26 additions & 9 deletions .github/workflows/buildami.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,21 @@ jobs:
-var "aws_access_key=${{ secrets.AWS_ACCESS_KEY }}" \
debian12-ami.pkr.hcl | sudo tee output.txt
tail -2 output.txt | head -2 | awk 'match($0, /ami-.*/) { print substr($0, RSTART, RLENGTH) }' >> ami.txt
echo "Captured AMI ID: $(tail -1 ami.txt)"
- name: Create new launch template version
env:
AWS_ACCESS_KEY_ID: ${{ secrets.DEMO_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.DEMO_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: ${{ secrets.AWS_REGION }}
run: |
AMI_ID=$(cat ami.txt)
LAUNCH_TEMPLATE_NAME=$(aws ec2 describe-launch-templates --query 'LaunchTemplates[?starts_with(LaunchTemplateName, `launchTemplate`) == `true`].LaunchTemplateName' --output text)
aws ec2 create-launch-template-version --launch-template-name $LAUNCH_TEMPLATE_NAME --version-description latest --source-version 1 --launch-template-data "ImageId=$AMI_ID"
launch_template_id=$(aws ec2 describe-launch-templates --query 'LaunchTemplates[0].LaunchTemplateId' --output text)
ami_id=$(tail -1 ami.txt)
aws ec2 create-launch-template-version \
--launch-template-id $launch_template_id \
--source-version 1 \
--version-description "Updated with latest AMI" \
--launch-template-data "ImageId=$ami_id"
- name: Update the autoscaling group
id: set_asg_name
Expand All @@ -79,14 +84,26 @@ jobs:
AWS_SECRET_ACCESS_KEY: ${{ secrets.DEMO_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: ${{ secrets.AWS_REGION }}
run: |
AUTO_SCALING_GROUP_NAME=$(aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[?starts_with(AutoScalingGroupName, 'autoScalingGroup')].AutoScalingGroupName" --output text | head -n 1)
AUTO_SCALING_GROUP_NAME=$(aws autoscaling describe-auto-scaling-groups --query "AutoScalingGroups[?starts_with(AutoScalingGroupName, 'autoScalingGroup-')].AutoScalingGroupName" --output text | head -n 1)
if [ -z "$AUTO_SCALING_GROUP_NAME" ]; then
echo "No Auto Scaling Group found with the specified prefix."
exit 1
echo "No Auto Scaling Group found with the specified prefix."
exit 1
fi
echo "Auto Scaling Group Name: $AUTO_SCALING_GROUP_NAME"
echo "AUTO_SCALING_GROUP_NAME=$AUTO_SCALING_GROUP_NAME" >> $GITHUB_ENV
aws autoscaling update-auto-scaling-group --auto-scaling-group-name $AUTO_SCALING_GROUP_NAME --launch-template LaunchTemplateName=$LAUNCH_TEMPLATE_NAME,Version='$Latest'
LAUNCH_TEMPLATE_NAME=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name $AUTO_SCALING_GROUP_NAME --query "AutoScalingGroups[0].LaunchTemplate.LaunchTemplateName" --output text)
LAUNCH_TEMPLATE_VERSION='Latest'
if [ -z "$LAUNCH_TEMPLATE_NAME" ]; then
echo "No Launch Template found for the Auto Scaling Group."
exit 1
fi
echo "Launch Template Name: $LAUNCH_TEMPLATE_NAME"
aws autoscaling update-auto-scaling-group --auto-scaling-group-name $AUTO_SCALING_GROUP_NAME --launch-template LaunchTemplateName=$LAUNCH_TEMPLATE_NAME,Version=$LAUNCH_TEMPLATE_VERSION
- name: Instance refresh and wait
env:
Expand All @@ -98,11 +115,11 @@ jobs:
STATUS="pending"
while [ "$STATUS" != "Successful" ]; do
echo "Waiting for instance refresh to complete..."
sleep 60
sleep 60
STATUS=$(aws autoscaling describe-instance-refreshes --auto-scaling-group-name "$AUTO_SCALING_GROUP_NAME" --instance-refresh-ids $REFRESH_ID --query 'InstanceRefreshes[0].Status' --output text)
if [ "$STATUS" == "Failed" ]; then
echo "Instance refresh failed."
exit 1
fi
done
echo "Instance refresh completed successfully."
echo "Instance refresh completed successfully."
1 change: 0 additions & 1 deletion .github/workflows/instancerefresh.yml

This file was deleted.

62 changes: 33 additions & 29 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ app.use(express.json());


// Create Assignment
app.post('/v2/assignments', authenticate, async (req, res) => {
app.post('/v1/assignments', authenticate, async (req, res) => {
try {
await sequelize.authenticate();
// Specify the fields you want to accept
Expand Down Expand Up @@ -355,7 +355,7 @@ app.post('/v2/assignments', authenticate, async (req, res) => {


// Delete Assignment by ID
app.delete('/v2/assignments/:id',rejectRequestsWithBody, authenticate, async (req, res) => {
app.delete('/v1/assignments/:id',rejectRequestsWithBody, authenticate, async (req, res) => {
try {
const assignmentId = req.params.id;

Expand Down Expand Up @@ -390,7 +390,7 @@ app.delete('/v2/assignments/:id',rejectRequestsWithBody, authenticate, async (re


// Get Assignment by ID
app.get('/v2/assignments/:assignmentId', rejectRequestsWithBody, authenticate, async (req, res) => {
app.get('/v1/assignments/:assignmentId', rejectRequestsWithBody, authenticate, async (req, res) => {
try {
const assignmentId = req.params.assignmentId;

Expand All @@ -415,13 +415,13 @@ app.get('/v2/assignments/:assignmentId', rejectRequestsWithBody, authenticate, a
}
});

app.patch('/v2/assignments/:id', (req, res) => {
app.patch('/v1/assignments/:id', (req, res) => {
res.status(405).json({ error: 'Update (PATCH) is not allowed' });

});

// Get all Assignments of a user by authentication
app.get('/v2/assignments',rejectRequestsWithBody, authenticate, async (req, res) => {
app.get('/v1/assignments',rejectRequestsWithBody, authenticate, async (req, res) => {
try {
// Use the authenticated user from the middleware
const authenticatedUser = req.user;
Expand All @@ -446,7 +446,7 @@ app.get('/v2/assignments',rejectRequestsWithBody, authenticate, async (req, res)
}
});

app.put('/v2/assignments/:id', async (req, res) => {
app.put('/v1/assignments/:id', async (req, res) => {
try {
const assignmentId = req.params.id;
const updatedAssignmentData = req.body;
Expand Down Expand Up @@ -541,51 +541,47 @@ function publishToSNSTopic(message, topicArn) {
return sns.publish(params).promise();
}

app.post('/v2/assignments/:id/submission', authenticate, async (req, res) => {

app.post('/v1/assignments/:id/submission', authenticate, async (req, res) => {
try {
const assignmentId = req.params.id;
const { submission_url } = req.body;
const userEmail = req.user.email;
const userEmail = req.user.email;

// Check if submission_url exists and is a non-empty string
if (!submission_url || typeof submission_url !== 'string' || !submission_url.trim()) {
return res.status(400).json({ error: 'Submission URL is missing or empty' });
}


// Check if the assignment's deadline has passed
const assignment = await AssignmentModel.findByPk(assignmentId);
if (!assignment) {
return res.status(404).json({ error: 'Assignment not found' });
}
if (new Date() > new Date(assignment.deadline)) {
return res.status(400).json({ error: 'Deadline for this assignment has passed' });
}

// Check for existing submissions and retry limit
const submissionCount = await SubmissionModel.count({
const existingSubmissions = await SubmissionModel.findAll({
include: [{
model: AssignmentModel,
where: { id: assignmentId },
include: [{
model: UserModel,
where: { email: userEmail }
}]
}]
}],
where: { assignment_id: assignmentId }
});

if (submissionCount >= assignment.num_of_attempts) {
if (existingSubmissions.length >= assignment.num_of_attempts) {
return res.status(400).json({ error: 'Retry limit exceeded' });
}

// Create or update submission
const [submission, created] = await SubmissionModel.findOrCreate({
where: { assignment_id: assignmentId },
defaults: {
submission_url,
submission_updated: new Date()
}
// Create new submission
const newSubmission = await SubmissionModel.create({
assignment_id: assignmentId,
submission_url
});

if (!created) {
submission.submission_url = submission_url;
submission.submission_updated = new Date();
await submission.save();
}

// Prepare the message for SNS
const message = {
Expand All @@ -594,18 +590,26 @@ app.post('/v2/assignments/:id/submission', authenticate, async (req, res) => {
userEmail: userEmail
};




const topicArn = SNS_ARN;

// Publish the message to the SNS topic
await publishToSNSTopic(message, topicArn);

res.status(created ? 201 : 200).json(submission);
res.status(201).json(newSubmission);
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'Internal Server Error' });
res.status(503).json({ error: 'Service Unavailable' });
}
});







logger.exceptions.handle(
new winston.transports.File({ filename: 'csye6225.log' })
Expand Down

0 comments on commit 1021ef3

Please sign in to comment.