Monthly Archives: July 2012

Execute a command in all running OpenVZ Containers

If you wish to execute a command in all running Containers, you can use the following script:
[codesyntax lang="bash"]

for i in `cat /proc/vz/veinfo | awk '{print $1}'|egrep -v '^0$'`; \

do echo "Container $i"; vzctl exec $i <command>; done

[/codesyntax]

where <command> is the command to be executed in all the running Containers. For example:

[codesyntax lang="bash"]
for i in `cat /proc/vz/veinfo | awk '{print $1}'|egrep -v '^0$'`; \

do echo "Container $i"; vzctl exec $i uptime; done
[/codesyntax]

Container 1
2:26pm up 6 days, 1:28, 0 users, load average: 0.00, 0.00, 0.00
Container 101
2:26pm up 6 days, 1:39, 0 users, load average: 0.00, 0.00, 0.00

Source: http://download.swsoft.com/virtuozzo/virtuozzo4.0/docs/en/lin/VzLinuxUG/260.htm

How to install mercurial on debian squeeze

This document describes how to install and configure mercurial on linux debian squeeze. With minor changes you can use this procedure on different linux distributions.

1. Install required packages
[codesyntax lang="bash"]

aptitude install mercurial libapache2-mod-wsgi

[/codesyntax]

2. Create repositories directories

mkdir -pv /repositories
chown -R www-data:www-data /repositories
cd /repositories

3. Configure hgweb.cgi script in order to server the repositories via Apache
[codesyntax lang="bash"]

cp /usr/share/doc/mercurial/examples/hgweb.cgi .
chmod a+x hgweb.cgi
vim /repositories/hgweb.cgi
:%s/\/path\/to\/repo\/or\/config/\/repositories\/hgweb.config
:wq

[/codesyntax]

4. Configure hgweb.config

[codesyntax lang="bash"]

vim /repositories/hgweb.config

[/codesyntax]

[collections]
/repositories = /repositories

[codesyntax lang="bash"]

:wq

[/codesyntax]

5. Configure Apache

[codesyntax lang="bash"]

vim /etc/apache2/sites-available/code.domain.com

[/codesyntax]

ServerName code.domain.com
ServerAlias code.domain.com

ScriptAlias /repositories "/repositories/hgweb.cgi"

DocumentRoot /repositories

ErrorLog /var/log/apache2/code.domain.com-error_log
CustomLog /var/log/apache2/code.domain.com-access.log combined
LogLevel warn
ServerSignature Off
[codesyntax lang="bash"]

:wq

[/codesyntax]

6. Restart Apache
[codesyntax lang="bash"]

/etc/init.d/apache2 restart

[/codesyntax]

7. Make a test repository

[codesyntax lang="bash"]

mkdir test
cd test
hg init

[/codesyntax]

8. Enable notify extension

[codesyntax lang="bash"]

vim /etc/mercurial/hgrc.d/hgext.rc
:%s/# hgext.notify/hgext.notify
:wq

[/codesyntax]

9. Configure email notifications for earlier created repository
[codesyntax lang="bash"]

vim /repositories/test/.hg/hgrc

[/codesyntax]

[paths]
default = ssh://root@code.domain.com//repositories/configs
default-push = ssh://root@code.domain.com//repositories/configs
# in case you have ssh on a non standard port then you should use something like that
#default-push = ssh://root@code.domain.com:port//repositories/configs

[extensions]
hgext.notify=

[hooks]
changegroup.notify = python:hgext.notify.hook

[email]
from = mercurial@domain.com

[smtp]
host = domain.com

[web]
baseurl= http://code.domain.com/repositories/

[notify]
sources = serve

test = False
diffstat = False
merge = False

maxdiff = 0

template = Subject: [{webroot|basename}]: {desc|strip|firstline}\n\ndetails: {baseurl}{webroot|basename}/rev/{node|short}\nchangeset: {rev}:{node|short}\nuser: {author}\ndate: {date|date}\ndescription:\n{desc}\n\nfiles changed:\n {files}\n\n\n

[usersubs]
user@example.com = *

[reposubs]
configs = user@example.com
[codesyntax lang="bash"]

:wq

[/codesyntax]

10. Copying some files in the repository
[codesyntax lang="bash"]

rsync -avz /some/files/* .
hg add
hg status
hg commit -m "Added initial files to the repo" -u user
hg push

[/codesyntax]

Configuring Postfix as a Gmail Relay

This document describes how to configure postfix to use gmail as relay server on debian squeeze.

1. Install the required package. On the dialog window please select No configuration.
[codesyntax lang="bash"]

apt-get install postfix libsasl2-2 ca-certificates libsasl2-modules

[/codesyntax]

2. Configure postfix
[codesyntax lang="bash"]

vim /etc/postfix/main.cf

[/codesyntax]

relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes
alias_maps = hash:/etc/aliases
inet_interfaces = 127.0.0.1

[codesyntax lang="bash"]

:wq

[/codesyntax]

3. Define an username and a password
[codesyntax lang="bash"]

vim /etc/postfix/sasl_passwd

[/codesyntax]

[smtp.gmail.com]:587 email@gmail.com:password

[codesyntax lang="bash"]

:wq

[/codesyntax]

4. Fix file permissions
[codesyntax lang="bash"]

chmod 400 /etc/postfix/sasl_passwd

[/codesyntax]

5. Generate Postix lookup table
[codesyntax lang="bash"]

postmap /etc/postfix/sasl_passwd

[/codesyntax]

6. Use the ca-certificate package we installed and tell it where it can validate the certificate

[codesyntax lang="bash"]

cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | tee -a /etc/postfix/cacert.pem

[/codesyntax]

7. Restart postfix
[codesyntax lang="bash"]

/etc/init.d/postfix restart

[/codesyntax]

8. Test installation
[codesyntax lang="bash"]

echo "Test from a postfix configured to use gmail as a relay server" | mail -s "Test from postfix" "email@example.com"

[/codesyntax]

How to view in what openvz vps a process is running

1. Login on openvz node and use ps command to find the PID
[codesyntax lang="bash"]

ssh root@openvz-node
ps auxwwwf

[/codesyntax]

2. After finding PID execute following commands
[codesyntax lang="bash"]

PID=12345
for i in `vzlist -a | grep running | awk '{print $1}'`; do echo $i; ps $* -p $(grep -l "^envID:[[:space:]]*$i\$" /proc/[0-9]*/status | sed -e 's=/proc/\([0-9]*\)/.*=\1=') | grep $PID; done

[/codesyntax]

How to install openvz on CentOS 6.2

1. Add openvz repo
[codesyntax lang="bash"]

cd /etc/yum.repos.d
wget http://download.openvz.org/openvz.repo
rpm --import http://download.openvz.org/RPM-GPG-Key-OpenVZ

[/codesyntax]

2. Install openvz
[codesyntax lang="bash"]

yum install openvz-kernel-rhel6 vzctl vzquota bridge-utils

[/codesyntax]

3. Modify relevant kernel settings (sysctl.conf)
[codesyntax lang="bash"]

vim /etc/sysctl.conf

[/codesyntax]

net.ipv4.ip_forward=1
kernel.sysrq = 1

net.ipv4.conf.all.rp_filter=1
net.ipv4.icmp_echo_ignore_broadcasts=1

net.ipv4.conf.default.forwarding=1
net.ipv4.conf.default.proxy_arp = 0
net.ipv4.conf.default.send_redirects = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.eth0.proxy_arp=1

[codesyntax lang="bash"]

sysctl -p

[/codesyntax]

4. Reboot the server
[codesyntax lang="bash"]

reboot

[/codesyntax]

5. Disable se linux
[codesyntax lang="bash"]

vim /etc/sysconfig/selinux

[/codesyntax]

SELINUX=disabled

6. Install strace (I do not remember why I initially wanted this installed, but still it has nothing to spoil)

[codesyntax lang="bash"]

yum install strace

[/codesyntax]

Redmine - Upgrade to 1.4.4 on a debian squeeze

This document describes how to upgrade Redmine. The procedure was tested for upgrade from version 1.0.1 to 1.4.4, but it might works on other Redmine versions.
In this procedure I used a basic install of a debian squeeze (the only service installed was openssh-server). I am assuming the server has network connectivity and its IP is 10.34.133.220.

Please note:
- please announce all people BEFORE starting this procedure
- the Apache server on production environment MUST be stopped so after the upgrade the database be in sync.
- this version of Redmine needs ruby version 1.8.7 or 1.9.3 (I am going to use the debian squeeze default ruby package which has version 1.8.7)
- all rsync-ed files in this procedure are attached

1. Login to the remote machine
[codesyntax lang="bash"]

ssh root@10.34.133.220

[/codesyntax]

2. Configure hostname
[codesyntax lang="bash"]

echo "workflow-vm" > /etc/hostname

[/codesyntax]

3. Install all required packages for Redmine installation
[codesyntax lang="bash"]

aptitude install apache2 mysql-server ruby libmysqlclient-dev libmagickcore-dev libmagickwand-dev imagemagick locate less rubygems sudo postfix mercurial cvs subversion darcs git libapache2-mod-passenger libxslt1-dev vim less

[/codesyntax]

4. Configure and test postfix
[codesyntax lang="bash"]

scp root@workflow-sandbox-vm.test.org:/etc/mailname /etc/
scp root@workflow-sandbox-vm.test.org:/etc/postfix/main.cf /etc/postfix/

echo "root: user@test.org" >> /etc/aliases

vim /etc/mailname
:%s/workflow-sandbox-vm.test.org/workflow-vm.test.org
:wq

vim /etc/postfix/main.cf
:%s/workflow-sandbox-vm.test.org/workflow-vm.test.org
:wq

newaliases
/etc/init.d/postfix restart

echo "test from new redmine server" | mail -s "test from new redmine server" "user@test.org"

[/codesyntax]

5. Configuring mysql database
[codesyntax lang="bash"]

mysql -Bsu root -pmysql123 -e "drop database redmine;"
mysql -Bsu root -pmysql123 -e "drop user redmine@localhost;"
mysql -Bsu root -pmysql123 -e "create database redmine character set utf8;"
mysql -Bsu root -pmysql123 -e "create user 'redmine'@'localhost' identified by 'redmine';"
mysql -Bsu root -pmysql123 -e "grant all privileges on redmine.* to 'redmine'@'localhost';"
mysql -Bsu root -pmysql123 -e "flush privileges;"
# dump mysql database from production environment and importing it to our machine
# maybe it is a good idea to use dbredmine as mysql server
# anyway for now we are going to use localhost as mysql server
mysqldump -hdbredmine redmine_default > ~/redmine_production.sql
mysql -u redmine -predmine redmine < ~/redmine_production.sql

[/codesyntax]

6. Configure gems
[codesyntax lang="bash"]

echo "gem: --no-rdoc --no-ri" > ~/.gemrc

[/codesyntax]

7. Downloading and installing Redmine
[codesyntax lang="bash"]

wget -c http://rubyforge.org/frs/download.php/76255/redmine-1.4.4.tar.gz
tar xfz redmine-1.4.4.tar.gz
mv redmine-1.4.4 /usr/share/redmine
cd /usr/share/
chown -R root:root /usr/share/redmine
chown www-data /usr/share/redmine/config/environment.rb
ln -s /usr/share/redmine/public /var/www/redmine

[/codesyntax]

8. Configuring Redmine
[codesyntax lang="bash"]

cp redmine/config/database.yml.example redmine/config/database.yml
vim redmine/config/database.yml
production:
adapter: mysql
database: redmine
host: localhost
username: redmine
password: redmine
encoding: utf8
:wq
scp root@workflow-sandbox-vm.test.org:/usr/share/redmine/config/configuration.yml redmine/config/configuration.yml

[/codesyntax]

9. Installing required gems for Redmine
[codesyntax lang="bash"]

gem install bundler
cd redmine
/var/lib/gems/1.8/bin/bundle install --without development test postgresql sqlite
ln -s /var/lib/gems/1.8/bin/rake /usr/bin/

[/codesyntax]

10. Attachments
[codesyntax lang="bash"]

rm -fr /usr/share/redmine/files
mkdir -p /var/lib/redmine/default/files
ln -s /var/lib/redmine/default/files /usr/share/redmine/files
# copying attachments from production environment
rsync -avz --stats --progress root@10.99.0.241:/var/lib/redmine/default/files/*  /usr/share/redmine/files

[/codesyntax]

11. Fixing permissions
[codesyntax lang="bash"]

mkdir public/plugin_assets
chown -R www-data:www-data files log tmp public/plugin_assets
chmod -R 755 files log tmp public/plugin_assets

[/codesyntax]

12. Configuring Apache web server

[codesyntax lang="bash"]

vim /etc/apache2/sites-available/redmine
<VirtualHost *:443>
        ServerName workflow-vm.test.org
        ServerAlias workflow.test.org workflow
        DocumentRoot /var/www

        SSLEngine on
        SSLCertificateFile /etc/apache2/ssl/wildcard-internal-ca-test.org.crt
        SSLCertificateKeyFile /etc/apache2/ssl/wildcard-internal-ca-test.org.key

ServerAdmin ionut@vendio.ro
LogLevel warn
ErrorLog /var/log/apache2/redmine_error
CustomLog /var/log/apache2/redmine_access combined
<Directory /var/www/redmine>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
RailsBaseURI /redmine
PassengerResolveSymlinksInDocumentRoot on
</Directory>
</VirtualHost>
:wq

[/codesyntax]

[codesyntax lang="bash"]

vim /etc/apache2/ports.conf
:%s/NameVirtualHost \*:80/NameVirtualHost \*:443
:%s/Listen 80/#Listen 80
:wq

[/codesyntax]

[codesyntax lang="bash"]

rsync -avz --stats --progress root@workflow-sandbox-vm.test.org:/etc/apache2/ssl /etc/apache2

mv /etc/apache2/sites-enabled/000-default /tmp

a2ensite redmine
a2enmod passenger
a2enmod ssl

[/codesyntax]

13. Installing and migrating Redmine plugins
[codesyntax lang="bash"]

 cd vendor/plugins

[/codesyntax]

13.1. Question plugin
[codesyntax lang="bash"]

rsync -avz --stats --progress root@10.99.0.241:/usr/share/redmine/vendor/plugins/question_plugin /usr/share/redmine/vendor/plugins/
rsync -avz --stats --progress root@10.99.0.241:/usr/share/redmine/app/views/mailer/_issue_text* /usr/share/redmine/app/views/mailer/
mv /usr/share/redmine/app/views/mailer/_issue_text_html.rhtml /usr/share/redmine/app/views/mailer/_issue_text_html.erb
mv /usr/share/redmine/app/views/mailer/_issue_text_plain.rhtml /usr/share/redmine/app/views/mailer/_issue_text_plain.erb

[/codesyntax]

13.2. Redmine Status plugin
[codesyntax lang="bash"]

 rsync -avz --stats --progress root@workflow-sandbox-vm.test.org:/usr/share/redmine/vendor/plugins/redmine_status /usr/share/redmine/vendor/plugins/

[/codesyntax]

13.3. Redmine Stuff to do plugin
[codesyntax lang="bash"]

rsync -avz --stats --progress root@workflow-sandbox-vm.test.org:/usr/share/redmine/vendor/plugins/stuff_to_do_plugin /usr/share/redmine/vendor/plugins/
rsync -avz --stats --progress root@workflow-sandbox-vm.test.org:/usr/share/redmine/lib/ar_condition.rb /usr/share/redmine/lib/

[/codesyntax]

14. Fixing permissions for log file
[codesyntax lang="bash"]

cd /usr/share/redmine
chown www-data:www-data log/production.log

[/codesyntax]

15. Final steps to get Redmine working
[codesyntax lang="bash"]

rake generate_session_store
rake db:migrate RAILS_ENV=production
rake db:migrate_plugins RAILS_ENV=production
rake tmp:cache:clear
rake tmp:sessions:clear

[/codesyntax]

16. Restart Apache web server
[codesyntax lang="bash"]

 /etc/init.d/apache2 restart

[/codesyntax]

Files:
question plugin: question_plugin.tar.gz
redmine status plugin: redmine_status.tar.gz
stuff to do plugin: stuff_to_do_plugin.tar.gz

How to grep by date range

Have you ever wonder how to grep a file within a date range?

[codesyntax lang="bash"]

cat file.log | sed -n '/2012-01-05 16:55/,/2012-01-05 18:30/p' > file.log.date_range

[/codesyntax]