Monday, 30 July 2018

HTTP APIs calling and injecting Json data

#!/bin/bash
set -xvv

grouplist=$(aws autoscaling describe-auto-scaling-groups --query AutoScalingGroups[].[AutoScalingGroupName] --output text)
while read list; do
  var=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name "$list" --query AutoScalingGroups[].Instances[].[InstanceId] --output text)
  if [ -z "$var" ]; then continue; else
    echo $list
    aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name "$list" --query AutoScalingGroups[].Instances[].[InstanceId] --output text
    echo ""
  fi
done <<<"$grouplist" >asop
asopa=$(perl -00 -lpe 's/\n/,/g;' asop | egrep -i 'Hotshow A')
echo $asopa | tr -s ',' '\n' >final.txt

asc=64
while read ids; do
  let asc=$asc+1
  char=$(awk 'BEGIN{printf "%c", '$asc'}')
  midtemp=$(cat jsonmid)
  count+=$(printf '%s' "$midtemp" | sed 's/i-0123456/'${ids}'/g; s/.*refId.*/"refId":"'$char'"/; /.*refId.*/ s/$/,/')
done < <(tail -n +2 final.txt)
merger=$(echo $count | sed '$ s/.$//')

value1=$(curl -H "Authorization:Bearer eyJrIjoieDZNazBVcFROZ2N1OFlJR1pvZFJiYm8wZzFqWXI3MGEiLCJuIjoiT2ZmaWNlIiwiaWQiOjF9" http://localhost/api/dashboards/id/11/versions?limit=1)
temp=$(echo $value1 | tr -s ',' '\n' | tail -4 | head -1)
jsonbot=$(sed 's/.*version.*/'$temp'/g' jsonbottom)
jsontemptop1=$(sed 's/.*version".*/'$temp'/g; /.*version".*/ s/$/,/' jsontop)
temp1=$(head -1 final.txt)
jsontemptop2=$(printf '%s' "$jsontemptop1" | sed 's/.*title.*/"title":"'"$temp1"'"/; /.*title.*/ s/$/,/')
echo $jsontemptop2$merger$jsonbot >datainject.json

curl -H "Authorization: Bearer eyJrIjoieDZNazBVcFROZ2N1OFlJR1pvZFJiYm8wZzFqWXI3MGEiLCJuIjoiT2ZmaWNlIiwiaWQiOjF9" -H "Accept: application/json" -H "Content-type: application/json" -X POST -d @datainject.json http://localhost/api/dashboards/db

rm -fr datainject* asop*

--------------------------------------------------------------------------------------------------------------------------

jsontop:

{
"meta": {
"type": "db",
"canSave": true,
"canEdit": true,
"canAdmin": true,
"canStar": true,
"slug": "apiboard",
"url": "/d/ZfrvEIKmz/Apiboard",
"expires": "0001-01-01T00:00:00Z",
"created": "2018-07-22T22:02:53Z",
"updated": "2018-07-23T12:53:11Z",
"updatedBy": "admin",
"createdBy": "admin",
"version":37,
"hasAcl": false,
"isFolder": false,
"folderId": 0,
"folderTitle": "General",
"folderUrl": "",
"provisioned": false
},
"dashboard": {
"annotations": {
"list": [{
"builtIn": 1,
"datasource": "-- Grafana --",
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"type": "dashboard"
}]
},
"description": "Data has been loaded via API Inserted",
"editable": true,
"gnetId": null,
"graphTooltip": 0,
"id": 11,
"links": [],
"panels": [{
"gridPos": {
"h": 1,
"w": 24,
"x": 0,
"y": 0
},
"id": 4,
"title":"Hotshow A",
"type": "row"
},
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "AWS",
"fill": 1,
"gridPos": {
"h": 10,
"w": 24,
"x": 0,
"y": 1
},
"id": 2,
"legend": {
"alignAsTable": true,
"avg": true,
"current": true,
"max": true,
"min": true,
"rightSide": true,
"show": true,
"sort": "current",
"sortDesc": true,
"total": false,
"values": true
},
"lines": true,
"linewidth": 1,
"links": [],
"nullPointMode": "null",
"percentage": false,
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
--------------------------------------------------------------------------------------------------------------------------

jsonmid:

{
"alias": "{{InstanceId}}",
"dimensions": {
"InstanceId": "i-0123456"
},
"highResolution": false,
"metricName": "CPUUtilization",
"namespace": "AWS/EC2",
"period": "",
"refId": "A",
"region": "ap-southeast-2",
"statistics": [
"Average"
]
},
--------------------------------------------------------------------------------------------------------------------------

jsonbottom:

],
"thresholds": [],
"timeFrom": null,
"timeShift": null,
"title": "CPU Utilization",
"tooltip": {
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [{
"format": "percent",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
}
],
"refresh": "5s",
"schemaVersion": 16,
"style": "dark",
"tags": [
"final"
],
"templating": {
"list": []
},
"time": {
"from": "now-2d",
"to": "now"
},
"timepicker": {
"refresh_intervals": [
"5s",
"10s",
"30s",
"1m",
"5m",
"15m",
"30m",
"1h",
"2h",
"1d"
],
"time_options": [
"5m",
"15m",
"1h",
"6h",
"12h",
"24h",
"2d",
"7d",
"30d"
]
},
"timezone": "browser",
"uid": "iZr08GFik",
"title": "ApiBoard",
"version":37
}
}
--------------------------------------------------------------------------------------------------------------------------

Thursday, 17 May 2018

WinScp - Directory Syncing between linux and windows

Import-Module WinScp
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Scp
$sessionOptions.HostName = "nash.duckdns.org"
$sessionOptions.UserName = "xyz"
$sessionOptions.Password = "abc"

$sessionOptions.GiveUpSecurityAndAcceptAnySshHostKey = $true
$sessionOptions.SshPrivateKeyPath = "C:\Users\NashAIO\Downloads\Nisa.ppk"

$session = New-Object WinSCP.Session

$session.Open($sessionOptions)

$SourcePath = "C:\Source\"
$DestinationPath = "/home/ubuntu/html/"

$session.SynchronizeDirectories( [WinSCP.SynchronizationMode]::Remote, "$SourcePath", "$DestinationPath", $False)

$session.Dispose()

Some Powershell Stuff

Script1

$cred = Get-Credential

$Server = "192.168.109.135"

Invoke-Command -ComputerName $server -ScriptBlock {

Set-ExecutionPolicy Unrestricted -Force
Sleep 5

Install-PackageProvider -Name NuGet -Force

Enable-WindowsOptionalFeature -Online -FeatureName "IIS-WebServer" -All
Enable-WindowsOptionalFeature -Online -FeatureName "IIS-WebServerManagementTools" -All
Enable-WindowsOptionalFeature -Online -FeatureName "IIS-WebServerRole" -All
#Install-WindowsFeature -Name Web-Server -IncludeAllSubFeature -IncludeManagementTools

Import-Module ServerManager -Force
Import-Module WebAdministration -Force

Stop-Website "Default Web Site"

Md $Env:systemdrive\Website1
Md $Env:systemdrive\MyApp
Md $Env:systemdrive\Backup

New-Website -Name "Website1" -Port 80 -IPAddress "*" -HostHeader "" -PhysicalPath "C:\Website1"

New-WebVirtualDirectory -Site "Website1" -Name "MyApp" -PhysicalPath "$Env:systemdrive\MyApp"
ConvertTo-WebApplication -PSPath "IIS:\Sites\Website1\MyApp"

Restart-WebAppPool DefaultAppPool

} -Credential $cred

New-PSDrive -Name J -PSProvider FileSystem -Root \\$Server\C$ -Credential $cred

Copy-Item -Path "J:\Website1" -Destination "C:\ServerCopy" -Recurse -Force
Copy-Item -Path "J:\MyApp" -Destination "C:\ServerCopy" -Recurse -Force

Get-PSDrive J | Remove-PSDrive

--------------------------------------------------------------------------------------------------------------------------

Script2

$cred = Get-Credential

$server = "192.168.109.135"

New-PSDrive -Name J -PSProvider FileSystem -Root \\$server\C$ -Credential $cred

Copy-Item -Path "J:\Website1" -Destination "J:\Backup" -Recurse -Force
Copy-Item -Path "J:\MyApp" -Destination "J:\Backup" -Recurse -Force

do {

$Source=  Read-Host "Input The Full file Path "
$Destination = Read-Host "Input The Full file Path "

get-childitem $Source -Recurse | foreach {
    $SrcFile = $_.FullName
    $SrcHash = Get-FileHash -Path $SrcFile -Algorithm MD5

    $DestFile = $_.Fullname -replace [RegEx]::escape($Source),$Destination

    if (Test-Path $DestFile) {
        $DestHash = Get-FileHash -Path $DestFile -Algorithm MD5
        if ($SrcHash.hash -ne $DestHash.hash) {
            Copy-Item $SrcFile -Destination $DestFile -Force
        }
    }
else {
        Copy-Item $SrcFile -Destination $DestFile -Force
    }
}
$response = read-host "Repeat?"
}
while ($response -eq "Y")

Get-PSDrive J | Remove-PSDrive

Invoke-Command -ComputerName $server -ScriptBlock {
Restart-WebAppPool DefaultAppPool
} -Credential $cred

--------------------------------------------------------------------------------------------------------------------------

Tuesday, 6 March 2018

Automating Ruby App Deployment


The Problem: Application out of dev is ready but deployment to production server is nuisance.
1.     Current scenario had around 20+ dev whose continuously work on the application source code using Git.
2.     After update of code, deployment to production server of even a minor change in the code of the app is a huge hassle.

How did Capistrano Solve This Problem?
·        What is Capistrano?
Not an application or service, but a gem that works like rake; it bears similar functionality to the Ant and Nant scripts used for the purpose of deploying.
Capistrano is run on a user's local machine (typically the Rails app folder), and requires configuration on their end to run.
It directly connects from the user's machine to the remote server via SSH to perform deploys.
Commands are run through a console i.e. cap deploy (this is as close as it gets to 'one click' deploys.)
It produce logs, perform rollbacks etc.
It does not kick-off scheduled builds on its own.

Solution:
Deploy the latest commit from my 'master' branch in GitHub repository to staging server.
Configuring Capistrano's deploy.rb file will accomplish this.
One-click build on-demand at any time
All Capistrano deploys are done through 'force'
Have the capability to run custom commands on the staging server as a part of the deploy (i.e. 'bundle install', restart Apache, etc...)
Configuring Capistrano's deploy.rb file will accomplish this. Most of these commands are included out of the box.
Automatic deploy daily or after a commit on GitHub (optional)
A cron job is placed.
Applying changes to the production server become hassle free and updates are implied within seconds!

Implementation:
sudo apt-get update
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev

cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

git clone https://github.com/rbenv/rbenv-gem-rehash.git ~/.rbenv/plugins/rbenv-gem-rehash

rbenv install 2.2.4
rbenv global 2.2.4
ruby -v

echo "gem: --no-document" > ~/.gemrc
gem install bundler
rbenv rehash

sudo apt-get install libgmp-dev
gem install rails
rbenv rehash
rails -v

curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs

sudo apt-get install postgresql postgresql-contrib libpq-dev
sudo -u postgres createuser -s pguser
sudo -u postgres psql
\password pguser
\q

cd ~
rails new appname -d postgresql

cd appname

nano config/database.yml

  host: localhost
  username: nash
  password: qaz

rake db:create

rails server -b 0.0.0.0

OR we can do this as well!

nano config/boot.rb

require 'rubygems' 
require 'rails/commands/server'

module Rails 
  class Server
    alias :default_options_bk :default_options
    def default_options
      default_options_bk.merge!(Host: '0.0.0.0', Port: 8000)
    end
  end
end

Also,

Fork on Github

git config --global color.ui true
git config --global user.name "YOUR NAME"
git config --global user.email "YOUR@EMAIL.com"
ssh-keygen -t rsa -C "YOUR@EMAIL.com"

eval $(ssh-agent)
ssh-add

git config --global credential.helper 'cache --timeout=3600'

cat ~/.ssh/id_rsa.pub

ssh -T git@github.com

ssh user@productionserver

git clone https://github.com/jeremyolliver/hello_app.git

cd appname

bundle install --without test development

RAILS_ENV=production bundle exec rake db:create
RAILS_ENV=production bundle exec rake db:migrate

bundle exec rake secret

ddf4a6d37a956089984c8fe6160a6e3c18e48a448a07a50e4ab10a4edd6d3597f13ad9b6e0af4f5723f1ef52bfd2ffa78ab5b815d2bb8b15f14f48e7e307baad

export SECRET_KEY_BASE=ddf4a6d37a956089984c8fe6160a6e3c18e48a448a07a50e4ab10a4edd6d3597f13ad9b6e0af4f5723f1ef52bfd2ffa78ab5b815d2bb8b15f14f48e7e307baad

RAILS_ENV=production bundle exec rails server

Deployment for Capistrano
gem 'figaro'
gem 'puma'
group :development do
  gem 'capistrano'
  gem 'capistrano3-puma'
  gem 'capistrano-rails', require: false
  gem 'capistrano-bundler', require: false
  gem 'capistrano-rbenv'
end

Install the gems via bundler:
bundle install
It’s time to configure Capistrano, first by generating the config file, as follows:
cap install STAGES=production
This will create configuration files for Capistrano at config/deploy.rb and config/deploy/production.rb. deploy.rb is the main configuration file and production.rb contains environment specific settings, such as server IP, username, etc.
Add the following lines into the Capfile, found in the root of the application. The Capfile includes RBENV, Rails, and Puma integration tasks when finished:
require 'capistrano/bundler'
require 'capistrano/rbenv'
require 'capistrano/rails/assets' # for asset handling add
require 'capistrano/rails/migrations' # for running migrations
require 'capistrano/puma'

Now, edit deploy.rb as follows:
lock '3.4.0'
set :application, 'contactbook'
set :repo_url, 'git@github.com:user/contactbook.git' # Edit this to match your repository
set :branch, :master
set :deploy_to, '/home/deploy/contactbook'
set :pty, true
set :linked_files, %w{config/database.yml config/application.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}
set :keep_releases, 5
set :rbenv_type, :user
set :rbenv_ruby_version, 'jruby-1.7.19' # Edit this if you are using MRI Ruby
set :puma_rackup, -> { File.join(current_path, 'config.ru') }
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/puma.sock"    #accept array for multi-bind
set :puma_conf, "#{shared_path}/puma.rb"
set :puma_access_log, "#{shared_path}/log/puma_error.log"
set :puma_error_log, "#{shared_path}/log/puma_access.log"
set :puma_role, :app
set :puma_env, fetch(:rack_env, fetch(:rails_env, 'production'))
set :puma_threads, [0, 8]
set :puma_workers, 0
set :puma_worker_timeout, nil
set :puma_init_active_record, true
set :puma_preload_app, false

After configuring NGINX we just hit
# cap deploy production
And that’s it! Problem solved!

Monday, 8 January 2018

Ansible ( a small working example )

This is brief tutorial on how to install wordpress on your remote Ubuntu server using Ansible.
Well before starting work, I will assume you have already installed Ansible on your local machine and have a password less entry to the remote server using SSH keys.
Well then on with work:

# cd /home
# mkdir wordpress-full-install && cd wordpress-full-install
# touch site.yml
# touch hosts
# mkdir roles && cd roles

Staying in the same folder initialize four different roles using Ansible galaxy:

# ansible-galaxy init common
# ansible-galaxy init php
# ansible-galaxy init mysql
# ansible-galaxy init wordpress
# nano ~/wordpress-full-install/hosts (This will tell Ansible on which server to deploy the role)

[wpserver]
wp.remoteserver.com

# nano ~/wordpress-full-install/site.yml (This is where we make our roles are executed sequentially when the role is executed)

- hosts: wpserver

  roles:
    - common
    - php
    - mysql
    - wordpress


# nano roles/common/tasks/main.yml ( First we set out a complete lamp stack )

---
- name: Update apt cache
  apt: update_cache=yes cache_valid_time=3600
  sudo: yes

- name: Install required software
  apt: name={{ item }} state=present
  sudo: yes
  with_items:
    - apache2
    - mysql-server
    - php5-mysql
    - php5
    - libapache2-mod-php5
    - php5-mcrypt
    - python-mysqldb


# nano roles/php/tasks/main.yml ( Here we install the required php dependencies )

---
- name: Install php extensions
  apt: name={{ item }} state=present
  sudo: yes
  with_items:
    - php5-gd
    - libssh2-php


# nano roles/mysql/defaults/main.yml ( Sets up some default variable for DB )

---
wp_mysql_db: wordpress
wp_mysql_user: wordpress
wp_mysql_password: mysecretpassword


# nano roles/mysql/tasks/main.yml ( Installs and configures the DB on the targeted server )

---
- name: Create mysql database
  mysql_db: name={{ wp_mysql_db }} state=present

- name: Create mysql user
  mysql_user:
    name={{ wp_mysql_user }}
    password={{ wp_mysql_password }}
    priv=*.*:ALL


# nano roles/wordpress/tasks/main.yml ( This is where the entire install takes place for wordpress and different configurations are made to adjust to lamp stack )

---
- name: Download WordPress
  get_url:
    url=https://wordpress.org/latest.tar.gz
    dest=/tmp/wordpress.tar.gz
    validate_certs=no
  sudo: yes

- name: Extract WordPress
  unarchive:
    src=/tmp/wordpress.tar.gz dest=/var/www/
    copy=no
  sudo: yes

- name: Update default Apache site
  sudo: yes
  lineinfile:
    dest=/etc/apache2/sites-enabled/000-default.conf
    regexp="(.)+DocumentRoot /var/www/html"
    line="DocumentRoot /var/www/wordpress"
  notify:
    - restart apache

- name: Copy sample config file
  command: mv /var/www/wordpress/wp-config-sample.php /var/www/wordpress/wp-config.php creates=/var/www/wordpress/wp-config.php
  sudo: yes

- name: Update WordPress config file
  lineinfile:
    dest=/var/www/wordpress/wp-config.php
    regexp="{{ item.regexp }}"
    line="{{ item.line }}"
  with_items:
    - {'regexp': "define\\('DB_NAME', '(.)+'\\);", 'line': "define('DB_NAME', '{{wp_mysql_db}}');"}
    - {'regexp': "define\\('DB_USER', '(.)+'\\);", 'line': "define('DB_USER', '{{wp_mysql_user}}');"}
    - {'regexp': "define\\('DB_PASSWORD', '(.)+'\\);", 'line': "define('DB_PASSWORD', '{{wp_mysql_password}}');"}
  sudo: yes


# nano roles/wordpress/handlers/main.yml ( This is the action listener, when apache configuration is changed while installing wordpress a service restart is required for apache – This Handler is just a task but only executes when the previous task has been executed successfully )

---
- name: restart apache
  service: name=apache2 state=restarted
  sudo: yes


# cd /home/wordpress-full-install
# ansible-playbook -i hosts site.yml

That’s it! With just the above command your entire wordpress will be deployed onto the remote server.
If needed to be installed on different servers just change the entries in the hosts file. Or just give the –I path to the specific file which contains the entries to the different server list.

Monday, 23 May 2016

HHVM or PHP-FPM on Apache 2.4 ( Centos 7 )

Now i have been going through a lot of blogs lately for this configuration but only found conf. with nginx, none with apache, so i decided to come up with this tutorial!

Lot of architecture are still using apache due to historic reasons one of them being .htaccess file!

Dont want to touch your apache, or whatever reason you want to stick with it, with this you can update your server performance using this tutorial!

On with installations then:

get lamp stack up and running:

yum install httpd
systemctl start httpd.service
systemctl enable httpd.service

yum install mariadb-server mariadb
systemctl start mariadb
mysql_secure_installation
systemctl enable mariadb.service

yum install php php-mysql php-mbstring php-pear

nano /var/www/html/info.php

<?php phpinfo(); ?>

nano /etc/php.ini

# line 878: uncomment and add your timezone

date.timezone = "Asia/Tokyo"

nano /var/www/html/index.php
<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
<?php
   print Date("Y/m/d");
?>
</div>
</body>
</html>

systemctl restart httpd.service

Open Firewall if using firewalld or iptables adjust accordingly:

sudo firewall-cmd --permanent --zone=public --add-service=http 
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

Now we fix out PHP-FPM:

yum -y install php-fpm

Normally the mod_proxy_fcgi module comes by default installed and enabled on apache 2.4 which can be verified by the following commands:

ls -l /usr/lib64/httpd/modules/mod_proxy*

cat /etc/httpd/conf.modules.d/00-proxy.conf

httpd -M | grep -i proxy

Now this as verified we need to do this to the vhost conf file: ( just insert the red line )

<VirtualHost *:80>

    ServerName localhost

    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/$1

    DocumentRoot /var/www/html
    <Directory /var/www/html>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog /var/log/httpd/localhost_error.log
    CustomLog /var/log/httpd/localhost_access.log combined
</VirtualHost>

systemctl start php-fpm 
systemctl enable php-fpm 
systemctl restart httpd 

This will make the php-fpm on! not getting into fine tuning php-fpm at the moment!

verify if php-fpm is listening on port 9000 by :

netstat -tulpn | grep 9000

Now on with HHVM :

yum -y install epel-release

yum install cpp gcc-c++ cmake git psmisc {binutils,boost,jemalloc,numactl}-devel \
{ImageMagick,sqlite,tbb,bzip2,openldap,readline,elfutils-libelf,gmp,lz4,pcre}-devel \
lib{xslt,event,yaml,vpx,png,zip,icu,mcrypt,memcached,cap,dwarf}-devel \
{unixODBC,expat,mariadb}-devel lib{edit,curl,xml2,xslt}-devel \
glog-devel oniguruma-devel ocaml gperf enca libjpeg-turbo-devel openssl-devel \
mariadb mariadb-server make -y

rpm -Uvh http://mirrors.linuxeye.com/hhvm-repo/7/x86_64/hhvm-3.12.0-1.el7.centos.x86_64.rpm

Check using this is successfully installed:

hhvm --version

Now do this stuff:

nano /usr/lib/systemd/system/hhvm.service

[Unit]
Description=HHVM HipHop Virtual Machine (FCGI)
[Service]
ExecStart=/usr/local/bin/hhvm
--config /etc/hhvm/server.ini \
--config /etc/hhvm/php.ini \
--config /etc/hhvm/config.hdf
--user apache
--mode daemon
[Install]
WantedBy=multi-user.target

mkdir /var/run/hhvm
chown apache:apache /var/run/hhvm
mkdir /var/log/hhvm
chown apache:apache /var/log/hhvm

Create config.hdf in /etc/hhvm:

ResourceLimit {
    CoreFileSize = 0          # in bytes
    MaxSocket = 10000         # must be not 0, otherwise HHVM will not start
    SocketDefaultTimeout = 5  # in seconds
    MaxRSS = 0
    MaxRSSPollingCycle = 0    # in seconds, how often to check max memory
    DropCacheCycle = 0        # in seconds, how often to drop disk cache
}
Log {
    Level = Info
    AlwaysLogUnhandledExceptions = true
    RuntimeErrorReportingLevel = 8191
    UseLogFile = true
    UseSyslog = false
    File = /var/log/hhvm/error.log
    Access {
        * {
            File = /var/log/hhvm/access.log
            Format = %h %l %u % t \"%r\" %>s %b
        }
    }
}
MySQL {
    ReadOnly = false
    ConnectTimeout = 1000      # in ms
    ReadTimeout = 1000         # in ms
    SlowQueryThreshold = 1000  # in ms, log slow queries as errors
    KillOnTimeout = false
}
Mail {
    SendmailPath = /usr/sbin/sendmail -t -i
    ForceExtraParameters =
}

Create server.ini in /etc/hhvm:

; php options
pid = /var/run/hhvm/pid
; hhvm specific
hhvm.server.port = 8000
;hhvm.server.file_socket = /var/run/hhvm/sock
hhvm.server.type = fastcgi
hhvm.server.default_document = index.php
hhvm.log.use_log_file = true
hhvm.log.file = /var/log/hhvm/error.log
hhvm.repo.central.path = /var/run/hhvm/hhvm.hhbc

Create php.ini in /etc/hhvm:

hhvm.mysql.socket = /tmp/mysql.sock
hhvm.server.expose_hphp = true
memory_limit = 400M
post_max_size = 50M

systemctl enable hhvm
systemctl start hhvm
systemctl status hhvm

Verify if the HHVM is listening on port 8000 by :

netstat -tulpn | grep 8000

Once done we need to edit the vhost file like this:

    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:8000/var/www/html/$1

systemctl restart httpd

Thats it!

You can verify this by opening info.php page, if HHVM has served it, it will show you!

Which ever compiler you go with is completely your choice!

This marks the end of tutorial!

Comments are welcome!


Tuesday, 12 April 2016

Checking email Remotely thru python ( Complete Solution )

This is done in three scripts one in bash rest other 2 in python:

Bash script for cron job and calling the entire procedure: (where the credits.txt is user1 pass1 sequence, all arranged like this)

user1 pass1
user2 pass2
.......

Bash Script:

#!/bin/bash

> /home/admin/mailstatus.txt

count=0

while IFS=' ' read -r f1 f2
do
a=($f1 $f2)
for ((i=0; i<${#a[@]}; i+=2)); do
 ./working1.py ${a[i]} ${a[i+1]}
done
let count=$count+1
done < /home/admin/credits.txt

minimumsize=$count

actualsize=$(wc -c < /home/admin/mailstatus.txt)
if [ $actualsize -gt $minimumsize ]; then
 value=$(cat /home/admin/credits.txt | awk 'NR==3{print $2}')
 ./sendnew.py $value
else
echo "This script ran correctly at -- > $(date)" >> /home/admin/logfile
fi

Check the box:

#!/usr/bin/python

import sys
import poplib, re 

if len(sys.argv) < 3:
    sys.exit(-1)

class Logger(object):
    def __init__(self):
        self.terminal = sys.stdout
        self.log = open("/home/admin/mailstatus.txt", "a")
    def write(self, message):
        self.log.write(message)  

sys.stdout = Logger()

# Change this to your needs 
POPHOST = "pop.gmail.com" 
POPUSER = sys.argv[1] 
POPPASS = sys.argv[2]

# How many lines of message body to retrieve
MAXLINES = 10

# Headers we're actually interrested in
rx_headers  = re.compile(r"^(From|Subject)")

try:

    # Connect to the POPer and identify user
    pop = poplib.POP3_SSL(POPHOST)
    pop.user(POPUSER)

    # Authenticate user
    pop.pass_(POPPASS)

    # Get some general informations (msg_count, box_size)
    stat = pop.stat()

    output = stat[0]
    check = stat[1]/1000000

    if output > 0 and check >15:

    #print "" * 30
    print "" * 30
    print "-" * 30

    # Print some useless information
    print "Checked %s for oversized emails" % (POPUSER)
    print "-" * 30
    print ("There are total: %d message(s), which is consuming %d MB's" % (stat[0], check))    
    print "-" * 30

for n in range(stat[0]):

  msgnum = n+1

        # Retrieve headers
        response, lines, bytes = pop.top(msgnum, MAXLINES)

        # Print message info and headers we're interrested in
        print " || ".join(filter(rx_headers.match, lines))
        print "-" * 30

    # Commit operations and disconnect from server
    pop.quit()
    print "" * 30

Getting the report:

#!/usr/bin/python

import smtplib, os, sys
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

if len(sys.argv) < 2:
    sys.exit(-1)

fromaddr = 'admin@nasheikh.com'
toaddrs  = ['naveed@nasheikh.com', 'tamy@nasheikh.com']

# Credentials (if needed)
username = 'admin@nasheikh.com'
password = sys.argv[1]

raport_file = open('/home/admin/mailstatus.txt','rb')
alert_msg = raport_file.read()
raport_file.close()

msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = ", ".join(toaddrs)
msg['Subject'] = "Boxes Mail Status"

body = alert_msg
msg.attach(MIMEText(body, 'plain'))

text = msg.as_string()

# The actual mail send
server = smtplib.SMTP_SSL('smtp.gmail.com:465')
server.login(username,password)
server.sendmail(fromaddr, toaddrs, text)


server.quit()

This marks the end of tutorial !