Sunday, November 9, 2014

linux history with date

1, export HISTTIMEFORMAT='%F %T '

2, history

[ec2-user@ip-10-0-3-22 ~]$ history | grep log | grep rm
  398  2014-11-10 01:16:01  rm python_etl_today_error.log
  426  2014-11-10 01:16:01  rm sqlldr_log_folder
  765  2014-11-10 01:16:01  rm python_etl_dev.log
 1004  2014-11-10 01:25:28  history | grep log | grep rm
 1006  2014-11-10 01:33:03  history | grep log | grep rm

Sunday, September 21, 2014

bash


Bash Round
chang@ACPNEW:~/tmp$ echo "scale = 5; 5+50*3/20 + (19*2)/7" | bc -l | xargs printf "%2.3f\n"
17.929

Avg
chang@ACPNEW:~/tmp$ awk '{s+=$1}END{print "ave:",s/NR}' RS=" "  avg.txt
ave: 54.646

for loop

for i in {1..50}
do
  echo $i
done


chang@ACPNEW:~/tmp$ cat b.txt | tr -s " "


tab seperated
cut -d$'\t' -f2-

chang@ACPNEW:~/tmp$ cut -d$'\t' -f2- b.txt




Tuesday, August 19, 2014

wordpress menu get erased

My wordpress menu get erased randomly. And I cannot replicate this issue.

I duplicated my whole site to a testing site, and dump the DB.
One day, I found my menu is gone, and I check the database,

mysql> select post_title, post_name, post_type from wp_posts where post_type='nav_menu_item' ;

It return 0 menu.

I disabled the plugin  Stealth Publish and fixed the issue.

It took me 3 weeks find the solution.

Tuesday, May 13, 2014

rails paperclip disable validation

1, Disable from global config file: config/application.rb

require 'paperclip/media_type_spoof_detector'
module Paperclip
  class MediaTypeSpoofDetector
    def spoofed?
      false
    end
  end
end

2, Disable from controller
class Logserver < ActiveRecord::Base
  has_attached_file :serverkey, :path => "public/keys/:id/:filename", :url => "/keys/:id/:basename.:extension"
  #validates_attachment_content_type :serverkey, :content_type => /\Atext.*\Z/
  do_not_validate_attachment_file_type :serverkey
 end

Wednesday, April 30, 2014

install awscli

apt-get install python-pip
pip install awscli

aws configure

aws ec2 describe-instances

aws ec2 run-instances --image-id ami-3495735c --count 1 --instance-type t1.micro

Friday, February 28, 2014

Github/Markdown Gem usage

Under root:
1, install github markdown gem
gem install github-markdown

2, Add markdown gem into Gem file
 root@ubuntuChef:/opt/ruby/user# vim Gemfile
gem 'github-markdown'

3, may need do:
bundle install

4, Define your markdown in your controller
I put MD file under app/assets/markdown folder

ang@ubuntuChef:/opt/ruby/user$ vim app/controllers/myusers_controller.rb

  def markdown
    #@myusers = Myuser.all
    require 'github/markdown'
    #require 'rdiscount'

    mdfile = File.join(Rails.root, 'app', 'assets', 'markdown', 'test.md')
    mymd = File.open(mdfile, 'rb') { |file| file.read }
    @md = GitHub::Markdown.render(mymd)
    #@md =  RDiscount.new(mymd, :smart, :filter_html).to_html
  end

5, update the view  - DON'T forget RAW

root@ubuntuChef:/opt/ruby/user# vim app/views/myusers/markdown.html.erb
<h1>Markdown</h1>

<%= raw(@md) %>

Github tutorial

1,  generate ssh key and add your public key to github
https://help.github.com/articles/generating-ssh-keys

2, test connection with github
$ ssh -T git@github.com
Warning: Permanently added the RSA host key for IP address '192.0.52.28' to the list of known hosts.
Hi gds! You've successfully authenticated, but GitHub does not provide shell access.

3, Add your project to Github
A, create a repo in github

B, in your local box,
DO:
$ git config --global user.name "Smith Chris" $ git config --global user.email schris@example.com

touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/gdsdong/csstemplate-acupuncture.git
git push -u origin master
 
git remote add origin git@github.com:gdsdong/csstemplate-decoration.git
git push -u origin master 
 
Error:
$ git push -u origin master
error: The requested URL returned error: 403 while accessing 
https://github.com/gdsdong/csstemplate-acupuncture.git/info/refs
Need to update local git config
  1. edit .git/config file under your repo directory
  2. find url=entry under section [remote "origin"]
  3. change it from url=https://github.com/xxxx/lxxx.git  to url=ssh://git@github.com/xxxx/lxxx.git. that is.
  4. Save config file and quit. now you could use git push origin master to sync your repo on GitHub




Thursday, February 27, 2014

Ruby on Rails Paperclip tutorial - rails 4.0.3, Ruby 2.1, Paperclip 4.1.1

Step 1, New a project
rails new demo

Step 2, Update Gemfile
add paperclip, mysql2 gem, enable JavaScript runtime
gem 'mysql2'
gem 'paperclip'
gem 'therubyracer', platforms: :ruby // remove the comment


You should be able to run rails server now:
ang@ubuntuChef:/opt/ruby/demo$ rails server
=> Booting WEBrick
=> Rails 4.0.3 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2014-02-27 18:39:15] INFO  WEBrick 1.3.1
[2014-02-27 18:39:15] INFO  ruby 2.1.1 (2014-02-24) [x86_64-linux]
[2014-02-27 18:39:15] INFO  WEBrick::HTTPServer#start: pid=3187 port=3000

Step 3, Creating the Database

ang@ubuntuChef:/opt/ruby/demo$ rails generate scaffold User username:string

Your migration file looks like:
ang@ubuntuChef:/opt/ruby/demo$ less db/migrate/20140227235934_create_users.rb

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :username

      t.timestamps
    end
  end
end

add paperclip  - one is image, one is zip file


ang@ubuntuChef:/opt/ruby/demo$ rails generate paperclip User avator download

ang@ubuntuChef:/opt/ruby/demo$ vim db/migrate/20140228000158_add_attachment_avator_download_to_users.rb

class AddAttachmentAvatorDownloadToUsers < ActiveRecord::Migration
  def self.up
    change_table :users do |t|
      t.attachment :avator
      t.attachment :download
    end
  end

  def self.down
    drop_attached_file :users, :avator
    drop_attached_file :users, :download
  end
end
 
Step 4, Applying the Migration - there are two migrations.

ang@ubuntuChef:/opt/ruby/demo$ rake db:migrate
==  CreateUsers: migrating ====================================================
-- create_table(:users)
   -> 0.0196s
==  CreateUsers: migrated (0.0199s) ===========================================

==  AddAttachmentAvatorDownloadToUsers: migrating =============================
-- change_table(:users)
   -> 0.2719s
==  AddAttachmentAvatorDownloadToUsers: migrated (0.2721s) ====================

Step 5, Start server
http://192.168.88.216:3000/users/new

You will see the new user page, but no file upload options. We need to add them manually.

Step 6, update model
ang@ubuntuChef:/opt/ruby/demo$ vim app/models/user.rb

class User < ActiveRecord::Base
  has_attached_file :avator
  has_attached_file :download
  validates_attachment_content_type :avator, :content_type => %w(image/jpeg image/jpg image/png application/pdf)
  validates_attachment_content_type :download, :content_type => %w(application/octet-stream  application/x-zip application/x-zip-compressed application/pdf application/x-pdf)
end



Step 7, update view
ang@ubuntuChef:/opt/ruby/demo$ vim app/views/users/_form.html.erb



  <div class="field">
    <%= f.label :avator %><br>
    <%= f.file_field :avator %>
  </div>
  <div class="field">
    <%= f.label :download %><br>
    <%= f.file_field :download %>
  </div>







ang@ubuntuChef:/opt/ruby/demo$ vim app/views/users/show.html.erb

<p>
  <strong>Avator:</strong>
 <%= image_tag @user.avator.url %>
<%= image_tag @user.avator.url(:thumb) %>
</p>
<p>
  <strong>Download:</strong>
  <a href="<%= @user.download.url %>" > Download File </a>
</p>




Step 8, update controller
ang@ubuntuChef:/opt/ruby/demo$ vim app/controllers/users_controller.rb

    def user_params
      params.require(:user).permit(:username, :avator , :download)
    end





Step 9
Run server:
ang@ubuntuChef:/opt/ruby/demo$ rails server




Monday, February 24, 2014

ipad php session issue / not working

I have user login script, user session destroyed when redirect the page.

Here is the code:

session_start() ;

if ( $_SESSION["logged_in"] != 1 )
{
    header("Location: index.php");
}

Need to add exit to fix session destroyed issue. 
session_start() ;

if ( $_SESSION["logged_in"] != 1 )
{
    header("Location: index.php");
 
    exit;
}

Sunday, February 23, 2014

install latest ruby and chef-solo

1, remove all old ruby
apt-get remove ruby1.9.1
apt-get remove ruby1.8
apt-get --purge remove ruby-rvm
rm -rf /usr/share/ruby-rvm /etc/rvmrc /etc/profile.d/rvm.sh

2, install rvm
\curl -L https://get.rvm.io | bash -s stable --ruby --autolibs=enable --auto-dotfiles
source /usr/local/rvm/scripts/rvm
ruby -v

IF you see this issue "The program 'rails' is currently not installed."
you need to restart your terminal

rvm install ruby

3, install chef using gem install, NOT "curl -L https://www.opscode.com/chef/install.sh | bash"
gem install rails
gem install chef
chef-solo -v

Thursday, February 13, 2014

Ubuntu, Ruby on Rails, Apache2 with Passenger

ubuntu_ROR_Apache2
==================

How To configure Apache to run ROR

1, Install LAMP and other packages

apt-get install apache2 mysql-server libapache2-mod-auth-mysql php5-mysql  php5 libapache2-mod-php5 php5-mcrypt

apt-get install libcurl4-openssl-dev apache2-threaded-dev libapr1-dev libaprutil1-dev

2, Remove old  Ruby

  apt-get remove ruby1.9.1

  apt-get remove ruby1.8

3 Install RVM (for ubuntu)

  apt-get --purge remove ruby-rvm

  rm -rf /usr/share/ruby-rvm /etc/rvmrc /etc/profile.d/rvm.sh

 env | grep rvm

  \curl -L https://get.rvm.io | bash -s stable --ruby --autolibs=enable --auto-dotfiles

source /usr/local/rvm/scripts/rvm

rvm

4, Install Rails

gem install rails

5, Ruby versions (by Feb. 15 2014)

  ruby 2.1.0p0 (2013-12-25 revision 44422) [i686-linux]
 
  gem 2.2.2
 
  irb 0.9.6(09/06/30)
 
  rvm 1.25.18 (stable)
 
  Rails 4.0.2
 
  rake (10.1.0)

6,install Phusion Passenger.

DO NOT USE APT-GET TO INSTALL PASSENGER, OTHERWISE, RUBY WILL BE DOWNGRADED TO LOWER VERSION, AND RAILS WILL NOT WORK.
  #apt-get install libapache2-mod-passenger

gem install passenger

passenger-install-apache2-module

passenger start

Please edit your Apache configuration file, and add these lines:

vim /etc/apache2/apache2.conf

   LoadModule passenger_module /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.37/buildout/apache2/mod_passenger.so
   <IfModule mod_passenger.c>
     PassengerRoot /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.37
     PassengerDefaultRuby /usr/local/rvm/gems/ruby-2.1.1/wrappers/ruby
   </IfModule>

7, update apache2 config file (assume your rails application path is /opt/ruby/demo/)

  vim /etc/apache2/sites-available/default

  <VirtualHost x.x.x.x:80>
        ServerAdmin xxng@gmail.com

        ServerName  3w.com
        ServerAlias www.3w.com

        RailsEnv development
        DocumentRoot /opt/ruby/demo/public

        <Directory /opt/ruby/demo/public>
               AllowOverride all
               Options -MultiViews
        </Directory>

  </VirtualHost>

8,    service apache2 restart

Wednesday, February 5, 2014

chef-solo apache2 add files

1, following this tutorial to setup apache2
http://gettingstartedwithchef.com/first-steps-with-chef.html

When you finish first step, you will receive 403 forbidden page because there is no index file under apache.

2, add index.html to apache2
A: add index.html into this folder:
 [root@new-host-3 chef-repo]# ll cookbooks/apache2/files/default/
index.html

B:  vim  chef-repo/cookbooks/apache2/recipes/default.rb
add:
cookbook_file "/var/www/html/index.html" do
  source "index.html"
  mode "0644"
end

3: run again:
chef-solo -c solo.rb -j web.json