Rackspace Cloud – Automatic delete orphan backup agent IDs

>> set your variables:
TOKEN=""
REGION="lon"
DDI=""  < this is the account number

>> Generate a list of backup agents
curl -sH  "X-Auth-Token: $TOKEN" -H "Content-type: application/json" -X GET https://$REGION.backup.api.rackspacecloud.com/v1.0/$DDI/user/agents | python -m json.tool | egrep "MachineName|MachineAgentId" | awk -F":" '{print $2}' | sed 's/ //g' | sed '{N;s/\n//}' > list.txt

>> Manually remove WANTED backup agents (leave only the ones you want to remove):
vim list.txt 

>> Generate remove list
awk -F, '{print $1}' list.txt > remove.txt


>> generate the exec file to review
for AGENTID in `cat remove.txt`; do echo curl -sH \"X-Auth-Token: $TOKEN\" -H \"Content-type: application/json\" -X POST https://$REGION.backup.api.rackspacecloud.com/v1.0/$DDI/agent/delete -d \'{\"MachineAgentId\": $AGENTID}\' ; done >> exec_me

>> exec the API calls
/bin/bash exec_me

PHP Sessions in Redis

If your php application requires sessions and it’s hosted on a scaled high available infrastructure, it’s required to have these sessions stored on a decentralised and HA platform as well, in order to avoid to rely on session persistent options on the load balancer or another Cloud Server.
Redis as a Service is a nice fit for this purpose.

Here an example using Rackspace Object Rocket http://www.rackspace.co.uk/objectrocket/redis

To achieve this it’s required to install the right package.

In Centos/RHEL, there is the IUS package available:

yum install php56u-pecl-redis

After that, the php.ini should be changed like this:

 session.save_handler = redis 
 session.save_path = "tcp://REDISOBJECTROCKETFQDN:PORT?auth=REDISPASSWORD"

To increase performance and reduce the “noise” for repetitive DNS queries (especially in case of SaaS which uses FQDN instead of an IP) it is also recommended to install nscd to cache the DNS queries.

API Calls with options and parameters

>> Query parameter (GET, DELETE) => add into the URL like ...?param=value

e.g.
curl -sH "X-Auth-Token: $TOKEN" -H "Content-type: application/json" -X DELETE https://lon.autoscale.api.rackspacecloud.com/v1.0/$ID/groups/$GROUPID/servers/$SERVER_UID?replace=false


>> Payload/Options (PUT, POST) => use -d 

e.g. 
curl -sH "X-Auth-Token: $TOKEN" -H "Content-type: application/json" -X PUT https://lon.backup.api.rackspacecloud.com/v1.0/$DDI/agent/migratevault -d '{"DestinationMachineAgentId": "$DEST_AGENT_ID", "SourceMachineAgentId": "$SOURCE_AGENT_ID"}' 


>> To print output:

| python -m json.tool


=========================================================
CREATE IMAGE

TOKEN=""
IMAGENAME=""
curl -sH "X-Auth-Token: $TOKEN" -H "Content-Type: application/json" -X POST https://{region}.servers.api.rackspacecloud.com/v2/{ddi}/servers/{server_id}/action -d "{\"createImage\": {\"name\": \"$IMAGENAME\"}}" -v; echo

Linux Cloud Server migration script

This script allows you to migrate a Linux Server from one server to another one. It uses rsync and it could be use when you need to resize down a server for example, or if you want to migrate onto another Cloud Provider.

git clone git://github.com/cloudnull/InstanceSync.git

Source:
http://cloudnull.io/2012/07/cloud-server-migration/
https://github.com/cloudnull/InstanceSync

Auto deploy from BitBucket repository via Cron

Setup the environment

Create the environment for Apache

mkdir /home/deployment
chown apache:apache /home/deployment

Create ssh key in /home/deployment/.ssh/id_rsa

Create wrapper for git to auth via SSH keys

cat > /home/deployment/git.sh <<EOF
#!/bin/bash

if [ $# -eq 0 ]; then
    echo "Git wrapper script that can specify an ssh-key file
Usage:
    git.sh -i ssh-key-file git-command
    "
    exit 1
fi

# remove temporary file on exit
trap 'rm -f /home/deployment/.git_ssh.$$' 0

if [ "$1" = "-i" ]; then
    SSH_KEY=$2; shift; shift
    echo "ssh -o StrictHostKeyChecking=no -i $SSH_KEY \$@" > /home/deployment/.git_ssh.$$
    chmod +x /home/deployment/.git_ssh.$$
    export GIT_SSH=/home/deployment/.git_ssh.$$
fi

# in case the git command is repeated
[ "$1" = "git" ] && shift

# Run the git command
git "$@"
EOF 

Run the fist clone

~# su - apache -s /bin/bash
Last login: Tue Apr  5 16:03:53 BST 2016 on pts/0
-bash-4.2$ cd /var/www/vhosts/

-bash-4.2$ /home/deployment/git.sh -i /home/deployment/.ssh/id_rsa clone [email protected]:repository01.git

Test the Re-Base

Make sure to get into the folder with .git subfolder
(in this case we did the clone into /var/www/vhosts/ BUT the code is stored into a new subfolder pulled with the domain name. So we cd into that one)

-bash-4.2$ cd /var/www/vhosts/blog.com && /home/deployment/git.sh -i /home/deployment/.ssh/id_rsa pull --rebase
Could not create directory '/usr/share/httpd/.ssh'.
Failed to add the host to the list of known hosts (/usr/share/httpd/.ssh/known_hosts).
Current branch master is up to date.
-bash-4.2$

Install the CRON

echo "*/5 * * * * apache cd /var/www/vhosts/blog.com && /home/deployment/git.sh -i /home/deployment/.ssh/id_rsa pull --rebase > /dev/null 2>&1" >> /etc/crontab

Rackspace – Cloud Monitoring – Ansible plugins

Install the required packages (Ubunto/Centos):

apt-get update && apt-get install python-apt python-pip build-essential python-dev git python-virtualenv -y

yum install python-pip git python-devel python-virtualenv gcc -y

Prepare the virtual environment

virtualenv /root/monitorenv
. /root/monitorenv/bin/activate
pip install paramiko PyYAML jinja2 httplib2 ansible

Download the playbook

git clone https://github.com/stevekaten/cloud-monitoring-plugin-deploy
cd cloud-monitoring-plugin-deploy

Install the required plugin:

ansible-playbook -i hosts holland_mysqldump.yml

	This will configure the holland_mysqldump plugin on the localhost.

ansible-playbook -i hosts mysql_slave.yml

	This will configure the mysql_slave plugin on the localhost.

ansible-playbook -i hosts port_check.yml

	This will fail with an error message informing you that you need to set a port.

ansible-playbook -i hosts port_check.yml -e port=8080

	This will configure the port_check plugin on the localhost checking if port 8080 is open.

ansible-playbook -i hosts port_check.yml -e '{"host":"rackspace.com","port":"80"}'

	This will configure the port_check plugin to check rackspace.com:80.

ansible-playbook -i hosts port_check.yml -e '{"host":"10.X.X.X","port":"3306"}'

	This will configure the port_check plugin to check mysql's port 3306 on the ServiceNet address.

ansible-playbook -i hosts lsyncd_status.yml

	This will configure the lsyncd_check plugin.

To UNINSTALL the monitoring, you need to delete the check, removing the related file from /etc/rackspace-monitoring-agent.conf.d/ and restart the Cloud Monitoring agent.

Rackspace – Cloud server inaccessible after creation from custom image

It happens that sometimes a server built from a custom image is not accessible. Sometimes the reason is becase the Nova agent was not running (for various reasons) on the source server and the networking wasn’t set correctly during the building process. This means the new server still have the old IP and routes of the original, the one used to create the image itself.

How to fix it?
Connect on the console and make sure xe-linux-distribution (xe-daemon) and Nova Agent are restarted/up and running.

Important: Make sure xe-linux-distribution is started BEFORE Nova Agent is.

Once this has been done run the following command on the Cloud server to force the Hypervisor to re-push the right configuration (this works only on Linux servers):

UUID=`uuidgen`; xenstore-write data/host/$UUID '{"name":"resetnetwork","value":""}'; sleep 10; xenstore-read data/guest/$UUID; unset UUID

# If completed successfully it will return something like this:
{"message": "", "returncode": "0"}