Skip to content

Instantly share code, notes, and snippets.

View khaledosman's full-sized avatar
🤘

Khaled Osman khaledosman

🤘
View GitHub Profile
@khaledosman
khaledosman / find-first-and-last-days-of-the-week.js
Created September 13, 2018 09:26
returns date timestamp for the first day of the current week as well as the last day of the current week
const now = new Date()
const currentDay = now.getDay()
const firstDayOfTheWeek = new Date().setHours(new Date().getHours() - new Date().getDay() * 24)
const lastDayOfTheWeek = new Date().setHours(new Date().getHours() + ((7 - currentDay) * 24))
@khaledosman
khaledosman / free-docker-space.sh
Created September 14, 2018 15:02
clean up space for docker
# Check disk space on docker
docker system df
# Check server disk space
df -h
# Delete docker volumes that are not in use by any containers
docker volume prune
# Delete containers that are not running
@khaledosman
khaledosman / Graceful start & Graceful shutdown.js
Last active February 26, 2019 13:43
Graceful start & Shutdown of NodeJS application
// Graceful start & Graceful shutdown of application to prevent errors with ongoing requests on restarting https://pm2.io/doc/en/runtime/best-practices/graceful-shutdown/?utm_source=pm2&utm_medium=website&utm_campaign=rebranding
const server = http.createServer(app)
mongoose.connect(process.env.MONGO_URL)
.then(db => {
console.log('connected to mongo successfully')
server.listen(port, err => {
if (err) {
console.log('something bad happened', err)
} else {
@khaledosman
khaledosman / load-testing.sh
Created October 1, 2018 09:18
Load testing cmds using auto-cannon and artillery
npm i -g autocannon
autocannon -c 100 -d 5 -p 10 <url>
npm i -g artillery
artillery quick --count 10 -n 20 <url>
@khaledosman
khaledosman / mongo_backup.sh
Created October 15, 2018 08:57
Takes Mongo Backups and Uploads them to S3 from a docker container
#!/bin/sh
export CONTAINER_NAME="docker-container-name"
export DATABASE_NAME="database-name"
export BACKUP_LOCATION="/home/ec2-user/backups"
export MONGO_USER="mongo authentication user"
export MONGO_PASSWORD="mongo authnetication password"
export TIMESTAMP=$(date +'%Y%m%d%H%M%S')
export DUMP_PATH=/data/${DATABASE_NAME}-backup-${TIMESTAMP}
export ZIP_PATH=${DATABASE_NAME}-backup-${TIMESTAMP}.zip
@khaledosman
khaledosman / Automated releases & release notes with semantic-release bitbucket piplines.md
Last active June 13, 2019 12:41
Automated releases & Changelog generation / version bumping using semantic-version , bitbucket & pipelines.

Description:

This document describes how to automate releases, release notes generation & semantic versioning based on commit messages using https://github.com/semantic-release/semantic-release.

For this to work, The commit messages need be written in conventional commits https://www.conventionalcommits.org/en/v1.0.0-beta.2/ or Angular commit style https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#commits . A quick cheat sheet / example for the commit messages is that they need to start with

  • feat: A new feature
  • fix: A bug fix
  • docs: Documentation only changes
  • style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
  • refactor: A code change that neither fixes a bug nor adds a feature
@khaledosman
khaledosman / frontend deployment on s3.md
Last active February 11, 2019 14:08
Frontend deployment with s3 & cloudfront
  1. Create a Bucket with the url as name (i.e potato.com)
  2. Grant Public Read Access
  3. Go to Bucket properties
  4. Enable static website hosting
  5. set index.html as the Error & Index pages (Frontend router will handle the 404 errors)
  6. Go to bucket policy and add get permission to the s3 bucket
 {
    "Version": "2012-10-17",
    "Statement": [
@khaledosman
khaledosman / Private npm registry with verdaccio.md
Last active February 11, 2019 14:15
Private NPM Registry with Verdacccio

Description

Verdaccio is a private NPM registry that allows developers to publish and install packages privately for better code sharing. https://github.com/verdaccio/verdaccio

Authentication & Creating a new user

  1. Go to http://www.htaccesstools.com/htpasswd-generator/
  2. Create your username and password.
  3. Send the generated htpasswd line and copy it into the verdaccio/conf/htpasswd file on the server.
  4. You should now be able to login using that username and password.. (No server restart is required)

Usage

@khaledosman
khaledosman / dynamodb-helpers.js
Created October 23, 2018 15:28
dynamodb helpers
const AWS = require('aws-sdk')
AWS.config.update({ region: 'us-east-1' })
const { DynamoDB } = AWS
const dynamoDB = new DynamoDB.DocumentClient()
module.exports.query = function (attributeName, attributeValue, tableName) {
const params = {
ExpressionAttributeNames: {
[`#${attributeName}`]: attributeName
@khaledosman
khaledosman / sns-helpers.js
Created November 29, 2018 16:12
Email / SMS notifications with AWS
const { SNS, SES } = require('aws-sdk')
const sns = new SNS()
const ses = new SES()
let isReady = false
function publishToTopic () {
const params = {
Message: 'MESSAGE_TEXT',
TopicArn: 'TOPIC_ARN'
}
return sns.publish(params).promise()