阅读视图

发现新文章,点击刷新页面。
☑️ ⭐

rails-5-2-credentials

辑编rails的credentials

EDITOR=vim bundle exec rails credentials:edit

系统中使用credentials.yml.enc

vi config/initializers/rollbar.rb

Rollbar.configure do |config|
  # Without configuration, Rollbar is enabled in all environments.
  # To disable in specific environments, set config.enabled=false.

  config.access_token = Rails.application.credentials[:rollbar_token]
... ...
end

rollbar 在cap中使用

set :rollbar_token, YAML.load(`rails credentials:show`)['rollbar_token']
set :rollbar_env, Proc.new { fetch :stage }
set :rollbar_role, Proc.new { :app }

ref:

http://dlj.bz/d5Ll3K

https://github.com/rails/rails/pull/30067

☑️ ⭐

添加阿里云服务器的swap分区

阿里云默认没有开启swap分区!小内存测试服务器常爆机!

Cannot allocate memory - identify

Cannot allocate memory - convert -size 240x240 xc:'rgb(142,125,214)' -pointsize 140 -font

查看 swap 空间大小

$ sudo swapon -s
Filename        Type    Size  Used  Priority

首先先登录root

sudo su -

1、创建用于交换分区的文件:

# dd if=/dev/zero of=/mnt/swapfile bs=1k count=1024000

1024000+0 records in
1024000+0 records out
1048576000 bytes (1.0 GB) copied, 17.7815 s, 59.0 MB/s

2、设置交换分区文件:

# mkswap /mnt/swapfile

Setting up swapspace version 1, size = 1023996 KiB
no label, UUID=c09e7797-1481-4d87-a620-29aa24ede564

3、立即启用交换分区文件

swapon /mnt/swapfile
# free -m
             total       used       free     shared    buffers     cached
Mem:           992        914         77          0          5         70
-/+ buffers/cache:        839        153
Swap:          999          0        999

注:如果在 /etc/rc.local 中有 swapoff -a 需要修改为 swapon -a

4、设置开机时自启用 SWAP 分区:

需要修改文件 /etc/fstab 中的 SWAP 行,添加

# echo "/mnt/swapfile swap swap defaults 0 0" >>/etc/fstab

注:/mnt/swapfile 路径可以修改,可以根据创建的 SWAP 文件具体路径来配置。

5、修改 swpapiness 参数

# echo 30 >/proc/sys/vm/swappiness

可以使用下述方法临时修改此参数,假设我们配置为空闲内存少于 30% 时才使用 SWAP 分区:

# vim /etc/sysctl.conf
vm.swappiness=30
# sysctl -p

ref:

http://coderschool.cn/1486.html

阿里云官方说明

http://stackoverflow.com/questions/11013755/rails-assets-pipeline-cannot-allocate-memory-nodejs

☑️ ⭐

bower-rails 配置与capistrano3整合

说索

项目大了,载入js开源的插件越来越多,碰到了:

  • 1. 没有打成rails包 (难道要把js都打开gem包)
  • 2. 都放在assets/javascripts (没有gem包,难道都要放在这下面,太多了!)
  • 3. 不易与作者同步维护

bower 是一个不错的前端类库管理的工具,感觉像ruby中的bundler一样!赞!

bower-rails这个gem就是方便把rails与js管理拉近距离了!

ubuntu的bower环境安装

sudo apt-get install nodejs
sudo apt-get install npm
sudo npm install -g bower

bower-rails 安装

  gem "bower-rails", "~> 0.9.2"
  bundle install
  rails g bower_rails:initialize

bower-rails 配置

vi Bowerfile

# A sample Bowerfile
# Check out https://github.com/42dev/bower-rails#ruby-dsl-configuration for more options

# asset 'bootstrap'

asset "timepicker", "1.3.2", git: "https://github.com/wvega/timepicker.git"

# 配置完Bowerfile
rake bower:install

bower:install完会发现vendor/assets下会多了这些多东西

$ tree vendor/assets
vendor/assets
├── bower.json
├── bower_components
│   └── timepicker
│       ├── AUTHORS
│       ├── CHANGELOG
│       ├── Gruntfile.js
│       ├── LICENSE-GPL
│       ├── LICENSE-MIT
│       ├── Makefile
│       ├── README.md
│       ├── jquery-timepicker.jquery.json
│       ├── jquery.timepicker.css
│       ├── jquery.timepicker.js
│       ├── package.json

... ...

capistrano3的配置

把这个加入config/deploy.rb中

set :linked_dirs,  %w{bin log tmp/pids tmp/cache tmp/sockets vendor/cache public/system public/assets vendor/assets/bower_components}
#注: vendor/assets/bower_components 这里不要忘了

desc "bower_rails install"
namespace :bower_rails do
  task :install do
    on roles(:app) do
      within current_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, "bower:install"
        end
      end
    end
  end
end
before 'deploy:assets:precompile', 'bower_rails:install'
cap production deploy

reference:

☑️ ⭐

git常用命令

在github下初建并提交

git init 
git remote add origin git@github.com:xxxx/xxxx.git
git pull origin master
git branch cgg #本地分支
vi README
git add README
git commit -m "brinch cgg update master"
git pull origin master 

pull某一个版本到当时分支

git pull origin dev_brisk_cgg 

看远程分支

git branch -r

从本地cog提交远程master

git push origin cog:master 

取消对文件的修改

$ git checkout -- benchmarks.rb

本地取消最后一个commit(代码不会还原)

git reset HEAD^

如果commit以后还想添加到本地中

$ git add xxx
$ git commit --amend

任何标识或其他修改。 或使用gitk浏览视觉的变化。

git show HEAD 

占存切换分支

$ git add config/deploy.rb
$ git checkout cgg_stable

error: Your local changes to the following files would be overwritten by checkout:
    config/deploy.rb
Please, commit your changes or stash them before you can switch branches.
error: Your local changes to the following files would be overwritten by checkout:
    config/environments/development.rb
Please, commit your changes or stash them before you can switch branches.
Aborting

$ git stash
Saved working directory and index state WIP on master: 3465109 thin edit
HEAD is now at 3465109 thin edit

$ git checkout cgg_stable
Switched to branch 'cgg_stable'

$ git checkout master
Switched to branch 'master'

$ git stash list
stash@{0}: WIP on master: 3465109 thin edit

$ git stash apply stash@{0}
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   config/deploy.rb
#   modified:   config/environments/development.rb
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   public/profile/
#   vendor/cache/
$ git stash clear 

取消已经暂存的文件

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   README.txt
#       modified:   benchmarks.rb
#

$ git reset HEAD benchmarks.rb
benchmarks.rb: locally modified
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   README.txt
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   benchmarks.rb
#

文件某个文件的日志

git log -p filename 
或
gitk filename

继续补充中。。。。

☑️ ⭐

ubuntu安装gitserver

安装 ```bash Terminal sudo apt-get install git-core sudo apt-get install python-setuptools



生成用户
```bash Terminal
sudo adduser \
    --system \
    --shell /bin/sh \
    --gecos 'git version control' \
    --group \
    --disabled-password \
    --home /home/git \
    git
mkdir ~git/src && cd ~git/src
git clone https://github.com/tv42/gitosis.git

cd ~git/src/gitosis 
python setup.py install

初使化gitosis,上传id_rsa.pub

sudo -H -u git gitosis-init < /tmp/id_rsa.pub

修改post-update权限

sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update

下载到客户端

git clone git@SERVER:gitosis-admin.git

添加新用户foo1

#id_rsa.pub 取到新用户的公钥复制到keydir下:如:
cp id_rsa.pub ./keydir/foo1@gmail.com.pub
git add ./keydir/foo1@gmail.com.pub
git commit -m 'add new user dev key'
git push

建立一个叫做foo的项目

vi gitosis.conf

[gitosis]

[group gitosis-admin]
writable = gitosis-admin
members = xxxx@gmail.com

[group foo]
writable = foo
members = foo1@gmail.com foo2@gmail.com
mkdir foo && cd foo

git init
touch hello.txt
git add hello.txt
git commit -am 'first commit'
git remote add origin git@SERVER:foo.git
git push origin master

参考

http://www.guztech.nl/wordpress/index.php/2010/02/setting-up-a-git-server-in-ubuntu-with-gitosis-and-using-gitextensions-on-windows/

https://github.com/tv42/gitosis

☑️ ⭐

ruby struct用法

# Create a structure with a name in Struct
Struct.new("Customer", :name, :address)    #=> Struct::Customer
Struct::Customer.new("Dave", "123 Main")   #=> #<Struct::Customer name="Dave", address="123 Main">

# Create a structure named by its constant
Customer = Struct.new(:name, :address)     #=> Customer
Customer.new("Dave", "123 Main")           #=> #<Customer name="Dave", address="123 Main">
Customer = Struct.new(:name, :address, :zip)
joe   = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joejr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
jane  = Customer.new("Jane Doe", "456 Elm, Anytown NC", 12345)
joe == joejr   #=> true
joe == jane    #=> false
Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)

joe["name"]   #=> "Joe Smith"
joe[:name]    #=> "Joe Smith"
joe[0]        #=> "Joe Smith"
Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe.length   #=> 3

转: http://ruby-doc.org/core-1.8.7/Struct.html

❌