Monday, 3 September 2018

Powershell Software installation automation

New-Item -ItemType directory -Path C:\Install\API

$secpasswd = ConvertTo-SecureString "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("xxxxxxxxx", $secpasswd)
#$env:computername current computer name
$collectorName = $env:computername

$response = iwr "https://api.au.sumologic.com/api/v1/collectors" -Credential $mycreds

$content = ConvertFrom-Json $response.Content
$collector = $content.collectors | Where {$_.name -eq $collectorName} | Select -First 1
Write-Host "Collector: $collector"

$response2 = iwr "https://api.au.sumologic.com/api/v1/collectors/$($collector.id)/sources" -Credential $mycreds
$content2 = ConvertFrom-Json $response2.Content

$sourceName='ProdA BasketsAPI'

$source = $content2.sources | Where {$_.name -eq $sourceName} | Select -First 1
Write-Host "Source: $source"

$gist = iwr "https://api.au.sumologic.com/api/v1/collectors/$($collector.id)/sources/$($source.id)?download=true" -Credential $mycreds | Set-Content C:\Docs\Sumo\testingapi.json




New-Item -ItemType directory -Path C:\Install\Sumo
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls,Tls11,Tls12'
Invoke-WebRequest 'https://collectors.sumologic.com/rest/download/win64' -outfile 'C:\Install\Sumo\SumoCollector.exe'
C:\Install\Sumo\SumoCollector.exe -console -q "-VskipRegistration=true" "-Vsumo.accessid=xxxx" "-Vsumo.accesskey=xxxxxxxxxxxxxxxxxxxxx" "-Vsources=C:\\Install\\API\\" + testingapi + ".json"
timeout 150
net start sumo-collector
timeout 150
net start sumo-collector

Brute Calling API


New-Item -ItemType directory -Path C:\Install\API
$acctname = 'xxxxxxxxxxxxxxxx'
$password = 'xxxxxxxxxxxxxxxx'
$auth = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("${acctname}:${password}"))}
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls,Tls11,Tls12'
$gist = Invoke-RestMethod -Method Get -Uri https://api.au.sumologic.com/api/v1/collectors/103320654/sources/122955606?download=true -Headers $auth
$gist | ConvertTo-Json -depth 100 | Set-Content C:\Install\API\testingapi.json

Monday, 13 August 2018

Time specified Windows server logs

$types = (Get-WinEvent -ListLog  *).LogName
$start = Get-Date 10/08/2017
$end = Get-Date 11/08/2017
ForEach ( $type in $types )
{
Get-WinEvent -FilterHashtable @{logname=$type;StartTime=$start;EndTime=$end} | Export-clixml "c:\Backup\$type.xml"
}

Output only in CSV of XML

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

Non Time based:

$types = wevtutil el
ForEach ($type in $types)
{
wevtutil epl $type C:\Backup\$type.evtx
}

Output is in evt or evtx

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

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.