Wednesday, March 9, 2016
Moving Xen Guests to another Xen host
Shrink the LVs of Xen guest first, but before shrinking the LVM logical volumn, check its usage first
Follow this guide to shrink the LVs
http://linux-bsd-sharing.blogspot.com/2012/06/howto-shrink-size-of-ext4-lvm-logical.html
prerequisites:
create lvm on target machine
install "pv" on src machine
# apt-get install pv
run
root@src:# dd if=/dev/vg/disk bs=4096 | pv | gzip -1 | ssh -p22 targethost.org "gzip -dc | dd of=/dev/vg/disk"
on your target machine you may want to:
resize2fs if the target lvm is larger
fsck.extX /dev/vg/disk on the target machine
edit the domU.cfg according to your target machine
mount /dev/vg/disk /mnt && chroot /mnt ** edit network, hostname, hosts
Tuesday, March 24, 2015
Setup Fail2ban for Asterisk Verion 11.16 on Debian
INSTALL PACKAGES
Install iptables
# apt-get install iptables
Install fail2ban
# apt-get install fail2ban
SETUP
1 - Add Asterisk into fail2ban filter directory to be monitored
Create this file: /etc/fail2ban/filter.d/asterisk.conf
with the following content:
# Fail2Ban configuration file
#
#
# $Revision: 250 $
#
[INCLUDES]
# Read common prefixes. If any customizations available -- read them from
# common.local
#before = common.conf
[Definition]
#_daemon = asterisk
# Option: failregex
# Notes.: regex to match the password failures messages in the logfile. The
# host must be matched by a group named "host". The tag "<HOST>" can
# be used for standard IP/hostname matching and is only an alias for
# (?:::f{4,6}:)?(?P<host>\S+)
# Values: TEXT
#
# Asterisk 1.4 use the following failregex
failregex = NOTICE.* .*: Registration from '\".*\".*' failed for '<HOST>:.*' - Wrong password
# you can add more regrex here depend on log lines in /var/log/asterisk/full
2 - Now, edit the fail2ban configuration, FreePBX configuration is in /etc/fail2ban/jail.conf, so we will add these configuration info at the end of the file as here:
[asterisk-iptables]
enabled = true
filter = asterisk
action = iptables-allports[name=ASTERISK, protocol=all]
sendmail-whois[name=ASTERISK, dest=root, sender=fail2ban@example.org]
logpath = /var/log/asterisk/full
maxretry = 4
bantime = 259200
3 - Turn it on for good
If all is well up to this point, let's make sure that fail2ban and iptables restart with the server by issuing the following commands.
Debian/Ubuntu:
update-rc.d iptables defaults
update-rc.d fail2ban defaults
You should now be somewhat protected against SIP scans and brute force attacks!
Tuesday, March 17, 2015
How to Clone a Xen Guest on LVM
DD Copy Method
Make the second LV for the cloned one:
lvcreate -L 5000 -n guest02 VG0
Then use dd to copy the contents of the first volume into the second. Be sure to shut down the guest01 VM before you begin:
xm shutdown guest01
Copy the contents of one LV into another:
dd if=/dev/VG0/guest01 bs=4096 of=/dev/VG0/guest02
Configure Xen
In order to make the hypervisor aware of this new guest, a new configuration file must be created. To do this, make a copy of the original configuration file:
cp /etc/xen/guest01 /etc/xen/guest02
Then, edit the new file, /etc/xen/guest02. In order for the guest to function the following lines must be changed:
- The name of the guest (from "guest01" to "guest02"
- The MAC address of the guest on the "vif =" line
- The UUID of the guest
- The logical volume on the "disk = " line.
Other parameters can also be changed as desired (the amount of memory for example). The MAC and the UUID are both random numbers. It is sufficient to simply change a few digits of each. Or, a utility that creates random MAC and UUID numbers can be used.
The new guest can now be started with the command:
xm create guest02 -c
Monday, March 16, 2015
Xen Tuning Commands
1- Resize the Xen guest Disk
Shut down the Xen guest
Resize its logical volume
Linux Commands:
# xm shutdown erp8
# lvresize /dev/VolGroup1/erp8-disk -L +1GB
# e2fsck -f /dev/VolGroup1/erp8-disk
# resize2fs /dev/VolGroup1/erp8-disk
Create new xen guest on DOM0
SSH to DOM0 (Xen host machine)
and run this statement to create one:
# sudo xen-create-image --hostname=myhost --memory=512mb --swap=1024mb --vcpus=2 --pygrub --dist=wheezy
Which is will run Debian Wheezy
RAM 512
SWAP 1024
vcpus number is 2
How To Remove Xen Guest from DOM0
1 - SSH into the Dom0 (the host machine).
2 -
$ xm list | grep <DomU>
where <DomU> is the short hostname of the virtual machine, e.g. yoyodyne. The DomU to be decommissioned should not be listed. If it is, execute
$ xm destroy <DomU>
This command force shutdowns the VM
3 - Remove the auto-boot symlink:
$ rm /etc/xen/auto/<DomU>.cfg
4 - Remove the Xen domain configuration:
$ rm /etc/xen/<DomU>.cfg
5 - Remove all logical volumes associated with the domU. In most cases this will consist of a single LV:
lvremove /dev/<Dom0>/<DomU>-<device>
For example:
lvremove /dev/dom0server/yoyodyne.example.com-hda
Wednesday, November 26, 2014
Process events, functions in Background in Odoo / OpenERP by using cron
In Odoo / OpenERP, sometime we have to wait for a while when we press on a button. We have to wait after all processes completed. Like a Send Mail button, after pressing Send Mail button we have to wait program to send out the email, as long as work is in not done the openerp UI is blocked.
We can escape this waitting. In Odoo / OpenERP, we can have our processes run in background, by using ir.cron of OpenERP like this way:
Example:
In function _let_process_in_backrgound which is invoked by our button, we use a cron task, the cron task will call method: 'function': 'do_something', of model: 'model': 'hr_timesheet_sheet.sheet' (for example), agurments are:
def _let_process_in_backrgound(self, cr, uid, data, context = {}):
timesheet_id = 1
str_now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
nextcall = parser.parse(str_now) + datetime.timedelta(seconds = 60)
pool.get('ir.cron').create(cr, uid, {
'name': 'Cron job to do something that take very long time',
'user_id': uid,
'model': 'hr_timesheet_sheet.sheet',
'function': 'do_something',
'nextcall': nextcall,
'args': repr([timesheet_id])
})
return {}
In Odoo / OpenERP, we have the method do_something like this:
def do_something(self, cr, uid, timesheet_id):
....
....
return True
By this way we created a cron task in OpenERP to be executed at nextcall time.
We can escape this waitting. In Odoo / OpenERP, we can have our processes run in background, by using ir.cron of OpenERP like this way:
Example:
In function _let_process_in_backrgound which is invoked by our button, we use a cron task, the cron task will call method: 'function': 'do_something', of model: 'model': 'hr_timesheet_sheet.sheet' (for example), agurments are:
def _let_process_in_backrgound(self, cr, uid, data, context = {}):
timesheet_id = 1
str_now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
nextcall = parser.parse(str_now) + datetime.timedelta(seconds = 60)
pool.get('ir.cron').create(cr, uid, {
'name': 'Cron job to do something that take very long time',
'user_id': uid,
'model': 'hr_timesheet_sheet.sheet',
'function': 'do_something',
'nextcall': nextcall,
'args': repr([timesheet_id])
})
return {}
In Odoo / OpenERP, we have the method do_something like this:
def do_something(self, cr, uid, timesheet_id):
....
....
return True
By this way we created a cron task in OpenERP to be executed at nextcall time.
Tuesday, November 11, 2014
Automatic start Xen guests after Dom0 reboot
We can start virtual machines using "xm create". However after rebooting the xen host machine, the virtual machines (xen guests) are not started automatically.
We can make a xen guest start automatically after xen host reboot.
Let's say the configuration for the virtual machine is in /etc/xen/XenGuest1.cfg
So we can do like this:
mkdir -p /etc/xen/auto
cd /etc/xen/auto
ln -s /etc/xen/XenGuest1.cfg .
cd -
cd /etc/xen/auto
ln -s /etc/xen/XenGuest1.cfg .
cd -
This creates a link to autostart the virtual machine.
and "XenGuest1.cfg" file must contain the following entries:
on_xend_stop = 'shutdown'
on_xend_start = 'start'
on_xend_start = 'start'
Thursday, October 30, 2014
Make field readonly / display or hide a field in Odoo / OpenERP conditionally
Make a field read-only or visible based on another field value in Odoo / Openerp
Ex1: Hide field field1 when field2 has no data
<field name="field1" attrs="{'invisible':[('field2', '!=', False)]}"/>
Ex2: Hide field field1 when field2 has no data:
<field name="field1" attrs="{'readonly':[('field2', '!=', False)]}"/>
Ex1: Hide field field1 when field2 has no data
<field name="field1" attrs="{'invisible':[('field2', '!=', False)]}"/>
Ex2: Hide field field1 when field2 has no data:
<field name="field1" attrs="{'readonly':[('field2', '!=', False)]}"/>
Wednesday, October 15, 2014
Create new OpenERP database, and get DataError: new encoding (UTF8) is incompatible
Create new OpenERP database, and get DataError: new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII)
HINT: Use the same encoding as in the template database, or use template0 as template
1.1 - ERROR ? openerp.sql_db: bad query: CREATE DATABASE ENCODING 'unicode' TEMPLATE "template1" Traceback (most recent call last):
Fix steps:
Login to Postgresql:
# sudo -u postgres psql
Then execute these statements:
postgres=# UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
postgres=# DROP DATABASE template1;
postgres=# CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = ‘UNICODE’;
postgres=# UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1'
postgres=# \c template1
psql (8.4.13)
You are now connected to database “template1″.
template1=# VACUUM FREEZE;
Then you just have to \q to quit. and rerun the openerp server + run the database creation.
Monday, September 8, 2014
Winscp to Debian / Ubuntu The server rejected SFTP connection, but it listens for FTP connections.
winscp to Debian/ubuntu:
The server rejected SFTP connection, but it listens for FTP connections.
The server rejected SFTP connection, but it listens for FTP connections.
Well the port 22 is for SSH service. So you can probably try installing openssh-server in your Debian / Ubuntu by typing
sudo apt-get install openssh-server
and then try connecting with the following details:
protocol: SSH
hostname: [IP Address of the computer]
port: 22
username: [username]
password: [password]
Hopefully this should work.
Thursday, August 21, 2014
OPENERP Technics - How to auto reload/refresh openerp GTK views each period of time?
OPENERP Technics - How to auto reload/refresh openerp GTK views each period of time?
By using attribute "auto_refresh" in definitions of model "ir.actions.act_window"
The following example will used to reload/refresh the view related with the action_window each 10 seconds:
<record id="your_object_auto_refresh_dashboard" model="ir.actions.act_window">
<field name="name">Your Object Dashboard</field>
<field name="res_model">board.board</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="auto_refresh">10</field>
<field name="usage">menu</field>
<field name="view_id" ref="board_your_object_auto_refresh_form"/>
</record>
By using attribute "auto_refresh" in definitions of model "ir.actions.act_window"
The following example will used to reload/refresh the view related with the action_window each 10 seconds:
<record id="your_object_auto_refresh_dashboard" model="ir.actions.act_window">
<field name="name">Your Object Dashboard</field>
<field name="res_model">board.board</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="auto_refresh">10</field>
<field name="usage">menu</field>
<field name="view_id" ref="board_your_object_auto_refresh_form"/>
</record>
Wednesday, August 20, 2014
Change colors for Tree view in OpenERP 6
We can do this by inheriting the tree view, maybe our tree view has some color settings, but we can override tree view attribute to have another color settings like this example:
The code like this will help:
<record model="ir.ui.view" id="project_workitem_task_tree_view">
<field name="name">Project Task Tracker Tree</field>
<field name="model">project.task</field>
<field name="inherit_id" ref="project.view_task_tree2" />
<field name="type">tree</field>
<field name="arch" type="xml">
<tree position="attributes">
<attribute name="colors">red:project_workitem_action=='Check-in';black:project_workitem_action=='Check-out'</attribute>
</tree>
<field name="name" position="before">
<button name="%(action_check_in_out_task)d" type='action' string='Check in / Check out' icon="terp-project" attrs="{'invisible':[('state','!=','open')]}"/>
<field name="project_workitem_action"></field>
<field name="last_check_out" widget="date"></field>
<field name="last_check_out_calc" invisible="1"></field>
</field>
</field>
</record>
The code like this will help:
<record model="ir.ui.view" id="project_workitem_task_tree_view">
<field name="name">Project Task Tracker Tree</field>
<field name="model">project.task</field>
<field name="inherit_id" ref="project.view_task_tree2" />
<field name="type">tree</field>
<field name="arch" type="xml">
<tree position="attributes">
<attribute name="colors">red:project_workitem_action=='Check-in';black:project_workitem_action=='Check-out'</attribute>
</tree>
<field name="name" position="before">
<button name="%(action_check_in_out_task)d" type='action' string='Check in / Check out' icon="terp-project" attrs="{'invisible':[('state','!=','open')]}"/>
<field name="project_workitem_action"></field>
<field name="last_check_out" widget="date"></field>
<field name="last_check_out_calc" invisible="1"></field>
</field>
</field>
</record>
Tuesday, August 19, 2014
Some useful postgresql commands
Login to Postgresql
sudo -u postgres psql [db_name]
Get PostgreSQL Version information
postgres=# SELECT version();
Create a new role with password and assign it as a supper role
postgres=# CREATE USER openerp WITH PASSWORD 'openerp';
Change the password for users
ALTER USER davide WITH PASSWORD 'hu8jmn3';
Grant a PostgreSQL user as a super user:
postgres=# ALTER USER openerp WITH SUPERUSER;
Linux command to create postgresql user with super database user role:
# sudo -u postgres createuser openerp -s
Create a new database with UTF-8 encoding
sudo -u postgres createdb mydb -E UTF-8 -T template0
Backup a db
sudo -u postgres pg_dump -o db_name > bk_name
Restore a db
psql db_name < bk_name
Drop a database
postgres=# DROP DATABASE db_name;
Change database owner
postgres=# ALTER DATABASE target_database OWNER TO new_onwer;
List all databases
postgres=# \l
List all postgresql users
postgres=#\du
COMMAND LINE: Clone a database
# sudo -u postgres createdb -O ownername -T originaldb newdb
Access Postgresql database with Postgres user without password:
sudo -u postgres psql [db_name]
Get PostgreSQL Version information
postgres=# SELECT version();
Create a new role with password and assign it as a supper role
postgres=# CREATE USER openerp WITH PASSWORD 'openerp';
Change the password for users
ALTER USER davide WITH PASSWORD 'hu8jmn3';
Grant a PostgreSQL user as a super user:
postgres=# ALTER USER openerp WITH SUPERUSER;
Linux command to create postgresql user with super database user role:
# sudo -u postgres createuser openerp -s
sudo -u postgres createdb mydb -E UTF-8 -T template0
Backup a db
sudo -u postgres pg_dump -o db_name > bk_name
Restore a db
psql db_name < bk_name
Drop a database
postgres=# DROP DATABASE db_name;
Change database owner
postgres=# ALTER DATABASE target_database OWNER TO new_onwer;
List all databases
postgres=# \l
List all postgresql users
postgres=#\du
COMMAND LINE: Clone a database
# sudo -u postgres createdb -O ownername -T originaldb newdb
Access Postgresql database with Postgres user without password:
# sudo -u postgres psql postgres
Sunday, August 17, 2014
Debug Odoo / OpenERP on Linux
Debug Python Program by PDB - POPULAR COMMANDSet break point by inserting this python line among python program: import pdb; pdb.set_trace()
View current code area, in pdb console
l (enter)
Run the current statement and move to next statement
n (enter)
Jump into function, in pdb consle
s (enter)
Debug again, in pdb console
q (enter)
Explore variable, in pdb console
print var_name
Start OpenERP in debug mode:
./openerp-server.py --debug --config ./openerp-server.conf
View current code area, in pdb console
l (enter)
Run the current statement and move to next statement
n (enter)
Jump into function, in pdb consle
s (enter)
Debug again, in pdb console
q (enter)
Explore variable, in pdb console
print var_name
Start OpenERP in debug mode:
./openerp-server.py --debug --config ./openerp-server.conf
Group with aggregation, Sort and Filter on Function Field in OpenERP
For OpenERP Version 6, we could not sorting and filtering on function field. Also we could not have group by with aggregation (Sum) on Function fields in OpenERP.
So the work around here we need 1 more field, this is not function field, but its a real field. We add this real field to Tree view, and invisible the function field.
When we calculating the function field, we update the real field = function field value, so that we can have the Tree view sorted by a function field.
I have an example here:
Let say we have the function field: last_check_out_calc which is calculated by function "get_last_check_out", beside this field we add one more real field: "last_check_out", so in the funtion "get_last_check_out" we calculate for field "last_check_out_calc" and we update "last_check_out_calc" value for "last_check_out".
Save the value into OpenERP Database by using write ORM function!
self.pool.get('project.task').write(cr, uid, [item_id], {'last_check_out': item_checkout_time})
Then we can order by "last_check_out".
We have to "last_check_out_calc" on the Tree view, but should keep it be invisible.
So the work around here we need 1 more field, this is not function field, but its a real field. We add this real field to Tree view, and invisible the function field.
When we calculating the function field, we update the real field = function field value, so that we can have the Tree view sorted by a function field.
I have an example here:
Let say we have the function field: last_check_out_calc which is calculated by function "get_last_check_out", beside this field we add one more real field: "last_check_out", so in the funtion "get_last_check_out" we calculate for field "last_check_out_calc" and we update "last_check_out_calc" value for "last_check_out".
Save the value into OpenERP Database by using write ORM function!
self.pool.get('project.task').write(cr, uid, [item_id], {'last_check_out': item_checkout_time})
Then we can order by "last_check_out".
We have to "last_check_out_calc" on the Tree view, but should keep it be invisible.
Subscribe to:
Posts (Atom)


