Wednesday, August 1, 2012

Run multiple commands in db2cmd

In Windows DOS, you need to run db2cmd before you can do any db2 command. If you want to run multiple commands, usually you can output the commands into a file and call db2cmd like this:
db2cmd -c db2 -tvf cmd_file.sql
But if you don't want to generate a file, here is how
db2cmd -c db2 connect to mydb user me using passwd && db2 select * from my_table && db2 terminate
DOS uses && to run multiple commands in one line.

Monday, June 11, 2012

Fixing CentOS Root Certificate Authority Issues

Here is the link: http://eric.lubow.org/2011/security/fixing-centos-root-certificate-authority-issues/ I backed up the existing certificate file just to be on the safe side. # cp /etc/pki/tls/certs/ca-bundle.crt /root/backup/ To fix the issue, just download a new certificate bundle. I used the one from haxx.se. # curl http://curl.haxx.se/ca/cacert.pem -o /etc/pki/tls/certs/ca-bundle.crt

Thursday, May 31, 2012

Issues after installing VirtualBox 4.1.16

After upgrading to VirtualBox 4.1.16 on my Windows 7 laptop, I found several issues.
  • My Guest "Host-Only Network Interface" stopped working.

    IP address for my guest CentOS 5.6 was 192.168.56.101, but CentOS said the address was already occupied. I found that there WERE processes in Resource Monitor using that address and listening on 137/138/139. It turns out that my "VirtualBox Host-Only Network adapter" was using 192.168.56.101.

    Here is how you can access Resources Monitor:

    • Right click task bar
    • Start Task Manager
    • Performance
    • Resource Monitor
    • Network
    • Listening Ports

    After installing/uninstalling VirutalBox several times, "VirtualBox Host-Only Network" adapter went back to 192.168.56.1.

  • Then another weird thing happened: I lost all printers and couldn't even access any Window File server. After several tries, I found that Network feature "Client for Microsoft Networks" were gone for "Local Area Connection" and "Wireless Network Connection". I forget if I deleted it or it was deleted during upgrading VirtualBox. Installing "Client for Microsoft Networks" restored my printers and I can access all file servers.

Wednesday, May 30, 2012

Setup Git-P4 using puppet

git-p4 is included into git RPM in CentOS 5.6, but it is not setup by default. Here is my puppet module for setup git-p4:
class git (
  $version = "1.7.4.1"
) {

  package { "git-$version":
    ensure => present
  }

  file { "/usr/local/bin/git-p4":
    source => "/usr/share/doc/git-$version/contrib/fast-import/git-p4",
    mode => 0755,
    require => Package["git-$version"]
  }

}
References:

Git-p4: create a new project in Perforce

The wiki and tutorial of Git-P4 usually ask you to clone a project from perforce. Can I create a new project in git and import it into Perforce using Git-P4? I don't know if there is a better way to do it. Here is how I do it:
  • Create a new p4 client spec: bewang-my-app-ws with this map "//depot/my-app/... //bewang-my-app-ws/..."
  • Create a p4 workspace directory: ~/p4-ws/my-app
  • Create .p4config with one line "P4CLIENT=bewang-my-app-ws" under ~/p4-ws/my-app
  • Create a new file in ~/p4-ws/my-app, and check it into perforce.

    Although you can see a Perforce file in nested folders/directories, but there are not such objects (folders/directories) in perforce indeed. You cannot just create an empty folder and check it into Perforce.

    This step creates the depot folder. Otherwise you may see this error when you run "git p4 rebase":

    [bewang@pmaster my-app]$ git p4 rebase 
    Traceback (most recent call last):
      File "/usr/local/bin/git-p4", line 1926, in ?
        main()
      File "/usr/local/bin/git-p4", line 1921, in main
        if not cmd.run(args):
      File "/usr/local/bin/git-p4", line 1716, in run
        sync.run([])
      File "/usr/local/bin/git-p4", line 1673, in run
        changes = p4ChangesForPaths(self.depotPaths, self.changeRange)
      File "/usr/local/bin/git-p4", line 442, in p4ChangesForPaths
        assert depotPaths
    
  • Create a folder for git repository: ~/git/my-app
  • Copy ~/p4-ws/my-app/.p4config to ~/git/my-app/.p4config
  • Run "git p4 clone //depot/my-app ."
  • You should see the file you just checked in and .git folder under ~/git/my-app
  • Work on your git repository
  • Run "git p4 rebase" before you want to check-in into perforce
  • Then run "git p4 submit" to submit each git commit to perforce
  • I can totally work offline without Perforce server

Thursday, May 17, 2012

Implements CORS using Backbone.js and Rails3

I encountered "406 Not Aceeptable" issue when my Backbone.js application fetch models in json from Rail server using CORS. Because this is a CORS call, I always doubt if something is wrong in my CORS headers, but it is Rails who causes the problem, because it tries to handle the request as HTML, see the log:
Started GET "/components/1" for 127.0.0.1 at 2012-05-14 07:48:08 -0700
Processing by ComponentsController#show as HTML
  Parameters: {"id"=>"1"}
Completed 406 Not Acceptable in 1424ms (ActiveRecord: 1283.8ms)
The reason why I got this error is that my application is supposed to accept and respond JSON only. So my controller only responds a JSON result.
  def show
    set_access_control_headers
    @components = ComponentModel.today_component_status(params[:id]).first
    respond_to do |format|
      format.json { 
        render :json => @components 
      } 
    end
  end 
This page is very helpful on stackoverflow: http://stackoverflow.com/questions/9241045/backbone-client-with-a-remote-rails-server

Thursday, April 26, 2012

rssh chroot jail setup on CentOS 6 64bit

It took me hours to make rssh chroot jail work. There are some steps missing in the document of rssh. Here is how I made it work. I want to set up an account called "deployer" on host "chroot_host" to allow some of users remotely copy files under a directory under /app/platform using passwordless scp. I don't want any user log on the host using "deployer".
  1. Install rssh
  2. sudo yum install rssh
    
    rssh.x86_64                 2.3.3-2.el6.rf                  @rpmforge-el6-x86_64
    
  3. Edit /etc/rssh.conf
  4. # /etc/rssh.conf
    logfacility = LOG_USER 
    
    allowscp
    #allowsftp
    #allowcvs
    #allowrdist
    #allowrsync
    
    umask = 022
    
    chrootpath = /app/platform/chroot
    
    user=deployer:011:00001:/app/platform/chroot 
    
  5. Create the user deployer
  6. sudo /bin/groupadd builder
    sudo /sbin/useradd -g builder -s /usr/bin/rssh -d /app/platform deployer
    
  7. Create .ssh folder for deployer and add public keys to /app/platform/.ssh/authorized_keys
  8. sudo mkdir /app/platform/.ssh
    sudo cp id_rsa.pub /app/platform/.ssh/authorized_keys
    
  9. Create chroot jail
  10. sudo /usr/share/doc/rssh-2.3.3/mkchroot.sh /app/platform/chroot/
    
    NOTE: Must have "/", otherwise the script will create a folder "/app/platform/chroot."
  11. Create /dev/null
  12. sudo mknod -m 666 dev/null c 1 3
    
  13. Create bin and copy /bin/sh
  14. sudo mkdir /app/platform/chroot/bin
    sudo cp /bin/sh /app/platform/chroot/bin
    
  15. Copy all files in /lib64 to /app/platform/chroot/lib64
  16. sudo cp /lib64/* /app/platform/chroot/lib64
    
    NOTE: Some of lib files must not be used, but I don't know which files should be kept.
  17. Create home directory in chroot jail
  18. sudo mkdir /app/platform/chroot/home/deployer
    sudo chown deployer:builder /app/platform/chroot/home/deployer
    
  19. Edit jailed {{etc/password}} to change the home directory of deployer
  20. deployer:x:501:34075::/home/deployer:/usr/bin/rssh
    
  21. Then you can successfully scp a file to the repository
  22. scp a_file.xml deployer@chroot_host:/repository
    
    NOTE: Because we use chroot, the folder is /repository.
Here are the problems I encountered during the setup. Unfortunately, I don't know which step fix the problem.
  1. "lost connection" Anything is wrong you will get this error.
  2. "Couldn't open /dev/null: No such file or directory" After copying /bin/sh, got this error. mknod resolved this issue.
  3. "unknown user 501" After copying all files in /lib64/, it is gone
  4. "Could not chdir to home directory : No such file or directory" chroot/home/deployer is not created