AWS – New server setup

I frequently setup instances on AWS and have to do a bunch of configuration to allow ssh access. Here’s a list of things that need to be done:

1. First time login with private key

ssh -i .ssh/your-private-key.pem admin@IP-Address

2. Change hostname – Changing hostname on Jessie build is fairly straightforward.

hostnamectl set-hostname your-new-hostname

Restart the hostname service within /etc/init.d or logout and log back into session

3. Allow ssh without private key – Modify the sshd_config file and make sure the following setting is enabled

PasswordAuthentication yes

Save the file and exit. Restart the ssh services within /etc/init.d

./ssh restart

4. Add new user – Use the following command as opposed to useradd and follow the prompts

adduser username

5. Add sudo privileges to username – Add sudo privileges without password to the /etc/sudoers file

# User privilege with no password
username     ALL=NOPASSWD:ALL

Django – combine two querysets

I had to get a list of accounts that either had some activity going around. Activity could be varying, e.g. has active deployments or quotes being exchanged, and therefore getting data from two different models. Here’s how I managed to merge the querysets together:

from itertools import chain
from django.db.models import Q

active_deal_accounts = Deal.objects.filter(support_end__gte=datetime.datetime.now()).values('account_id').distinct()
active_installation_accounts = Installation.objects.filter(Q(support_expires__gte=datetime.datetime.now())|
    Q(support_extension__gte=datetime.datetime.now())).values('account_id').distinct()
active_deal_results = Account.objects.filter(id__in=active_deal_accounts)
active_installation_results = Account.objects.filter(id__in=active_installation_accounts)
results = list(chain(active_deal_results, active_installation_results))

More info on StackOverflow article

Find Linux distribution name/version

The lsb_release command displays certain LSB (Linux Standard Base) and distribution-specific information. Type the following command:

lsb_release -a

Sample outputs:

No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 6.0.2 (squeeze)
Release:	6.0.2
Codename:	squeeze

Install openssh-server on debian

I installed the latest openssh-server on debian with the following command:

apt-get install openssh-server

I was able to connect to the ssh server but wasn’t able to login via root account. After the 3rd password attempt, I would get the following message:
Permission denied (publickey,password).
The default configuration doesn’t allow root ssh access via password. In order to do that I had to replace the following lines in the file /etc/ssh/sshd_config:

PermitRootLogin without-password
#PasswordAuthentication yes

with the below lines:

PermitRootLogin yes
PasswordAuthentication yes

Installing Perl Modules

Here’s how I was able to add Perl modules. Login as root to do this:

Login to the perl shell

perl -MCPAN -e shell

On the perl shell, type install followed by the module name, e.g.
cpan[1]> install LWP::Protocol::https
cpan[1]> install JSON
cpan[1]> REST::Client

Salesforce Query – Filter by Date

I was trying to get records from Salesforce modified within the last day. But kept running into exceptions where SF would complain that data must be of type datetime and must not be enclosed within quotes which was weird. But here’s how I was able to set the datetime without quotes and execute the query:

SELECT Accellion_Net_Subscription_Revenue__c,AccountId,Amount,CampaignId,CloseDate,Company_Size__c,CreatedDate,Deployment_ID__c,Id,LastModifiedDate 
FROM Opportunity 
WHERE LastModifiedDate >=2016-03-09T00:00:00Z 
ORDER BY LastModifiedDate ASC 
LIMIT 10

PHP – Get log file location

Based on the php version, the error log file may be available at separate locations. Here’s a nice way to find out the location of the log file. Execute the following command on your command prompt:

php --info | grep error

The error_log variable will point to the destination of log file.