Pages:  1 2 Ruby – lytsing's Blog

Category: Ruby

  • 再见,Bluehost,省下一年一千块

    2010 年,我还是一个有追求的程序员。在境外买了台虚拟主机,用来部署个人网站和 WordPress 技术博客,当然,还有些别的用途,你懂的。

    后来工作是越来越忙,我也再没心思去打理。每年都要续费 225 美刀,折合一千多人民币,哎,咬咬牙续了,不忍心把网站关掉。

    再说了,这年头也没人去写技术博客了,真要写,也是奔着知识付费去的。

    看了阿里云的轻量级服务器,2 核 1 GB 内存,一年也才 400 多块,能省一千块钱。这年头,钱难赚,蚊子腿那也是肉啊!

    于是,我在印象笔记里用 AI 帮我写了一篇文章:《WordPress 迁移 Bluehost 到阿里云 ECS 流程指南》。

    当我吭哧吭哧地迁移完,发现大家早都不玩 WordPress 了,改用 Hexo。

    以前我用 Emacs Muse 搭建一个 wiki,现在流行用 Outline 做私有部署。

    以前在 Bluehost 上折腾 Rails 应用,部署很麻烦,最终放弃。现在阿里云主机,装啥都行,一个 Nginx 前置代理服务器,可以配置许多域名,然后转发到 Rails、Node.js 后台应用,简直不要太爽了。

    有 AI 加持,整个迁移比较顺利,就是编译 Ruby 卡了很久,折腾了一个晚上才算搞定,特记录一下。

    下面几段是排障记录,想自己折腾的朋友可以直接照抄,不感兴趣的跳过。

    先确认是不是真的被 OOM 杀了,另开一个终端,运行:

    dmesg -T | grep -i -E 'oom|killed process'

    如果输出里有类似 Killed process ... (cc1) 的记录,就证实了是内存问题。

    轻量服务器内存如果不到 2 GB,编译 Ruby 时会失败。先看剩余内存:

    free -h

    可用内存低于 1 GB 时加上 2 GB 交换分区:

    fallocate -l 2G /swapfile
    chmod 600 /swapfile
    mkswap /swapfile
    swapon /swapfile
    grep -q '/swapfile ' /etc/fstab || echo '/swapfile none swap sw 0 0' >> /etc/fstab

    创建之后确认它已经挂上:

    swapon --show
    free -h

    swapon --show 应列出 /swapfile,大小约 2 GB。输出为空就再执行一次 swapon /swapfile。

    交换分区挂上之后,编译 Ruby 仍可能被系统杀掉,同时 free -h 里 Swap 的已用一直是 0。这通常是 vm.swappiness 为 0 或很低,内核不肯把内存换出去。查看当前值,改成 60,并让重启后仍然生效:

    cat /proc/sys/vm/swappiness
    sysctl vm.swappiness=60
    echo 'vm.swappiness=60' > /etc/sysctl.d/99-swappiness.conf

    编译期间再看 free -h。Swap 的已用开始增加,交换分区才算真正生效。

    有了 AI,做运维的门槛低多了。以前得准备很久才敢动手,现在可以跟 AI 共创:先做一个详细的计划,再照着实施。遇到问题,直接复制给 AI,让它帮你解决,不用像以前那样 Google 到处搜。

    哎,时代已经大变样了!

  • Gitlab 日常维护

    安装完gitlab只是第一步,后期维护还需要费力。

    文件约定

    VERSION 文件

    每个项目要增加这个文件,里面写着版本号, 效果见 https://gitlab.com/gitlab-org/gitlab-ce 右则导航栏,方便查看当前版本号。

    .gitignore

    避免提交一堆垃圾文件,及时加上.gitignore文件,有在线工具生成 https://www.gitignore.io/

    版本升级

    参考官方提供的文档 https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/update/upgrader.md 操作,一般不会遇到遇到很大的问题。如果本地修改一些文件,自动升级的脚本会执行git stash存档,同步完后,需要手动 git stash pop

    但不会是一劳永逸,lib 目录下一些配置更新了,还需要对照版本升级说明 https://github.com/gitlabhq/gitlabhq/tree/master/doc/update 手动修改 。

    以上的方式,比较适合不需要对gitlab大修改,如果定制自己的UI或页面,最好建立自己的分支:

    su - git
    cd /home/git/gitlab/
    git checkout -b my_branch
    1. Stop gitlab service
    2. Run git fetch –all
    3. Run git branch 7-5-stable origin/7-5-stable (replace with appropriate version)
    4. Run git rebase 7-5-stable my_branch
    5. Follow the rest of instructions (db:migrate, assets:precompile, etc.)

    参考: http://axilleas.me/en/blog/2014/custom-gitlab-login-page/

    代码备份

    已经有人写了一个脚本脚本,见 https://github.com/sund/auto-gitlab-backup 定期把备份数据拷贝到备机,避免硬盘挂掉的悲剧,更保险一点,需要每月拷贝到移动硬盘上存档。

    性能优化

    Nginx

    根据CPU,work数跟CPU数一样。

  • CentOs 6.5 安装 GitLab 笔记

    硬件要求: https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/install/requirements.md
    注意了,使用阿里云最便宜的主机,内存只有1G,会不够的哦,需要使用 swap交换分区,具体操作自行Google。

    虽然可以按照官方文档 https://github.com/gitlabhq/gitlab-recipes/tree/master/install/centos 从头到尾 ctrl+c、ctrl+v,但有些地方需要注意的。

    使用Ruby镜像

    在运行

    gem install bundler --no-doc

    之前,参考这个 http://ruby.taobao.org/ ,使用淘宝提供的镜像,感谢阿里。否则要等半天。

    nginx启动问题

    [root@git gitlab-shell]# service nginx restart
    Stopping nginx: [FAILED]
    Starting nginx: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
    nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)
    nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
    nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)
    nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
    nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)
    nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
    nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)
    nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
    nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)

    编辑 /etc/nginx/conf.d/gitlab.conf,将ipv6端口注释掉,我们还没有高级的使用ipv6。
    listen 0.0.0.0:80;
    # listen [::]:80 default_server;

    listen 0.0.0.0:443 ssl;
    #listen [::]:443 ssl default_server;

    nginx再启动,运行正常,打开网页报 500错误,查看 log发现:

    == Seed from /home/git/gitlab/db/fixtures/production/001_admin.rb
    2014-11-08T15:52:20Z 25461 TID-otwilgzbo INFO: Sidekiq client with redis options {:url=>”unix:/var/run/redis/redis.sock”, :namespace=>”resque:gitlab”}
    rake aborted!

    Errno::EACCES: Permission denied – connect(2) for /var/run/redis/redis.sock

    简单粗暴的方式,把权限都打开了,

    # chmod 777 /var/run/redis/redis.sock

    不过每次都要设置,终极方案是要再补一刀。
    # echo -e 'unixsocketperm 775' | sudo tee -a /etc/redis.conf

    邮件发送问题

    默认安装postfix,死活没有发送出去,查看日志cat /var/log/maillog,才知道要配置一下 主机,域名,编辑 /etc/postfix/main.cf,把myhostname,mydomain这两个值取消注释,填上你的值。

    myhostname = git.XXX.com
    #myhostname = virtual.domain.tld

    # The mydomain parameter specifies the local internet domain name.
    # The default is to use $myhostname minus the first component.
    # $mydomain is used as a default value for many other configuration
    # parameters.
    #
    mydomain = XXX.com

    如果是阿里云主机需要打开这个:
    #myorigin = $myhostname
    myorigin = $mydomain

    然后重启。测试邮件是否可以发出去,简单的方式是使用mail 命令行发送:

    mail -s "hello world” your_email

    回车,随便输入几个字,ctrl +d 结束输入。

    修改logo

    在 /home/git/gitlab/app/assets 这目录下,

    images/brand_logo.png
    images/favicon.ico
    images/logo-black.png
    images/logo-white.png

    把这几个图片改成你自己的,再重新编译资源文件:

    sudo -u git RAILS_ENV=production bundle exec rake assets:precompile

    重启 gitlab。

    修改默认皮肤

    编译 config/gitlab.yml
    # default_theme: 2 # default: 2

    注释取消,记得要与上面的这一行 default_projects_limit: 10 对齐,多个空格少个空格都不行,不然启动报错,rails新手会经常犯错。当然,每个开发成员可以定制主题,Profile Settings -> Design,爱哪选哪。

    其他注意

    默认master分支是受保护的,developer成员没有权限提交,所以,要开发成员组自己创建分支,管理员才有权限 merge进来。

    btw:) windows用户使用可能比较蛋疼,他们不太喜欢在命令行敲,需要有个学习适应的过程。

    Puma 替换 Unicorn

    为性能考虑,使用Puma,不过要跟jruby使用,不然会遇到多线程并发问题。参考 这篇 http://icyleaf.com/2014/01/moving-unicorn-to-puma-on-gitlab/

    运行的时候,遇到:
    [root@git gitlab]# service gitlab start
    /etc/init.d/gitlab: line 38: PPID: readonly variable
    Starting puma: /etc/init.d/gitlab: Usage: daemon [+/-nicelevel] {program}

    ^[[Ating sidekiq: [ OK ]

    PPID跟系统的冲突了,改成 UPID即可。已经提交补丁,https://gitlab.com/lytsing/gitlab-recipes/commit/3c8d0a35f54f93caac28bac464829dba7e6dc736

  • simulate http api(json) server.

    有这样的一种场景:客户端、后台同时开发。客户端组的同事等后台的接口,而后台的同事在忙于写逻辑实现,为了解决这个问题,去年我用ruby写了一个模拟器,删除公司的业务代码,只保留两个简单的接口,已经放在 gits上 https://gist.github.com/lytsing/d2db3e588990b69e616b。这样,后台的同事定义好接口,也顺便把模拟器给写好了,这样两边都不影响,腾出中间的时间磨合调整接口参数。

    ruby是很简单的解释性脚本语言,开发、测试、客户端不需要太多的ruby开发经验,基本上照着写就够了。我们系统小概率出现 302、500、502、503这样的http返回码,而测试组的同事需要拿客户端反复测试,才能出现,浪费不少时间,直接编辑模拟器的脚本,可以手动修改返回码。

    如果要支持 gzip数据压缩,前面加上一个 nginx做转发就好了:
    假设是使用系统自带的nginx,编辑/etc/nginx/nginx.conf,新增加:

    server {
      listen       80;
      server_name  localhost;
    
      server_name localhost:8080;
      location / {
        proxy_pass http://localhost:8080;
      }
    }

    新增文件 /etc/nginx/conf.d/gzip.conf,内容为:

    gzip on;
    gzip_comp_level  9;
    gzip_proxied any;
    gzip_types application/json;
    gzip_vary on;
  • Bluehost Rails 3.2.8 Ruby 1.9.3

    This is my bluehost environment:
    # ruby --version
    ruby 1.8.7 (2012-02-08 MBARI 8/0x6770 on patchlevel 358) [x86_64-linux], MBARI 0x6770, Ruby Enterprise Edition 2012.02
    # rails -v
    Rails 2.3.11

    I need to upgrade the ruby and rails version, take a Live Chat with Bluehost support, the guy let me follow this post https://my.bluehost.com/cgi/help/rails but it just tell us how to deploy the rails, so I need to reinstall the rails.

    Install Ruby 1.9

    When I install RubyGems, it complains:

    [~/src/rubygems-1.8.24]# ruby setup.rb
    /home4/lytsingo/.local/lib/ruby/1.9.1/yaml.rb:56:in `<top (required)>':
    It seems your ruby installation is missing psych (for YAML output).
    To eliminate this warning, please install libyaml and reinstall your ruby.
    RubyGems 1.8.24 installed

    so need to install libyaml first

    # mkdir ~/src
    # cd ~/src
    # wget http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz
    # tar xvf yaml-0.1.4.tar.gz
    # cd yaml-0.1.4
    # ./configure -prefix=$HOME/.local
    # make -j8
    # make install
    # make clean

    *Note*: Let compile faster, I use make -j 8, it depends your cpu processors, see:

    cat /proc/cpuinfo |grep processor |wc -l
    8

    # wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p0.tar.gz
    # tar xvf ruby-1.9.3-p0.tar.gz
    # cd ruby-1.9.3-p0
    # ./configure -prefix=$HOME/.local --disable-install-doc --with-opt-dir=$HOME/.local
    # make -j8
    # make install
    # make clean

    Install gems

    # wget http://production.cf.rubygems.org/rubygems/rubygems-1.8.24.tgz
    # tar xvf rubygems-1.8.24.tgz
    # cd rubygems-1.8.24
    # ruby setup.rb 

    Install Rails 3

    gem install rails -v 3.2.8 --no-rdoc --no-ri

    Check it out:

    # ruby -v
    ruby 1.9.3p0 (2011-10-30) [x86_64-linux]
    # rails -v
    Rails 3.2.8

    Em, it looks so nice, enjoy!