July 27, 2013

MacからJenkinsをアンインストールするメモ

http://www.axelscript.com/2012/12/17/notes-to-self-uninstall-jenkins-mac/
の通りのことしかやっていませんが、メモしておきます。
$ sudo launchctl unload /Library/LaunchDaemons/org.jenkins-ci.plist
$ sudo rm -rf /Applications/Jenkins "/Library/Application Support/Jenkins" /Library/Documentation/Jenkins
$ sudo rm -rf /Users/Shared/Jenkins
$ sudo dscl . -delete /Users/jenkins
$ sudo dscl . -delete /Groups/jenkins

July 26, 2013

knife-soloでVagrantにnginxをインストールしてみる

nginx.confとかは全くやっていませんが、とりあえず、手順メモです。

Chefレポジトリをknife-soloで作成
$ knife solo init repo-name
$ cd repo-name
$ git init
$ git add .
$ git commit -m "initial commit"
対象ノードにchef-soloをインストール
$ knife solo prepare hostname
$ git add nodes/hostname.json #自動生成された対象ノード用のjsonファイル
$ git commit -m "added nodes/hostname.json"
nginxをインストールするレシピの作成
$ knife cookbook create nginx -o site-cookbooks
$ vim site-cookbooks/nginx/recipes/default.rb
site-cookbooks/nginx/recipes/default.rb
#
# Cookbook Name:: nginx
# Recipe:: default
#
# Copyright 2013, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#
package "nginx" do
    action :install
end

service "nginx" do
    supports :status => true, :restart => true, :reload => true
    action [ :enable, :start ]
end
$ vim nodes/hostname.json
nodes/hostname.json
{
    "run_list":[
        "nginx"
    ]
}
実行
# Vオプションで詳細が表示されました
$ knife solo cook hostname -V
Vagrantfileの"config.vm.network :private_network, ip:"で指定しているIPアドレスにアクセス

P.S. 実行した時、以下の表示がされ全く進まなくなる現象が出ました。(これでハマった...)
INFO: Chef client  is running, will wait for it to finish and then run.
Vagrantのイメージ上でゴミ(?)プロセスを消して再度実行したらうまくいきました。
$ ps -ef | egrep "(ruby|chef)"
$ sudo kill xxx

July 23, 2013

chef-soloとknife-soloをインストールしてリモート(Vagrant)にchef-soloをインストールしてみる

chef-soloのインストール
http://wiki.opscode.com/display/chef/Installing+Omnibus+Chef+Client+on+Linux+and+Mac
$ sudo true && curl -L https://www.opscode.com/chef/install.sh | sudo bash
$ chef-solo -v
Chef: 11.4.4
knifeの設定
# とりあえず全てデフォルト。設定ファイルは ~/.chef/knife.rb
$ knife configure
knife-solo(0.3.0)のインストール
$ sudo gem install knife-solo --pre
$ vim ~/.chef/knife.rb

# 以下を追記
knife[:solo_path] = '/tmp/chef-solo'
参考: http://k-mats.github.io/blog/2013/04/12/set-up-knife-solo/

knife-soloで、以下で作成したVagrantの仮想マシンにchef-soloをインストール(sshコマンドでログイン出来るようにしておくこと)
Vagrantのインストールと起動メモ http://madroom-project.blogspot.jp/2013/07/vagrant.html
# xxxはホスト名(Vagrant)
$ knife solo prepare xxx
Bootstrapping Chef...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  6516  100  6516    0     0    985      0  0:00:06  0:00:06 --:--:--  4292
Downloading Chef 11.4.4 for ubuntu...
Installing Chef 11.4.4
(Reading database ... 65296 files and directories currently installed.)
Preparing to replace chef 10.12.0-3 (using .../chef_11.4.4_amd64.deb) ...
 * Stopping chef-client chef-client                                      [ OK ]
Unpacking replacement chef ...
dpkg: warning: unable to delete old directory '/etc/chef': Directory not empty
dpkg: warning: unable to delete old directory '/var/log/chef': Directory not empty
Setting up chef (11.4.4-2.ubuntu.11.04) ...
Thank you for installing Chef!
Processing triggers for ureadahead ...
Processing triggers for man-db ...
Generating node config 'nodes/ubuntu1304.json'...
Vagrantの仮想マシンでchef-soloのバージョンを確認
$ ssh xxx
~$ chef-solo -v
Chef: 11.4.4

MacにRuby1.9をインストール

標準では1.8系が入っていたので。
$ wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p448.tar.bz2
$ tar zxvf ruby-1.9.3-p448.tar.bz2
$ cd ruby-1.9.3-p448
$ ./configure
$ make
$ sudo make install
$ ruby -v
ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-darwin12.4.0]

July 21, 2013

Vagrantのsaharaを使ってみる

saharaというプラグインを使うと、OSの状態をロールバックできるようになるみたいです。

というわけで、saharaをインストールしてみます。以下、"$"をローカル、"~$"を仮想マシンとしています。
$ sudo vagrant plugin install sahara
$ vagrant sandbox
Usage: vagrant plugin <command> [<args>]

Available subcommands:
     commit
     off
     on
     rollback
     status
sandboxモードをonにしてから仮想マシンにsshで接続します。
$ vagrant sandbox on
$ vagrant ssh
Chef Soloをインストールしてみます。
~$ chef-solo -v
Chef: 10.12.0 #古いバージョンが最初から入っているようです
~$ curl -L http://www.opscode.com/chef/install.sh | sudo bash
~$ chef-solo -v
Chef: 11.4.4
~$ exit
状態をロールバックしてから仮想マシンにsshで接続してChef Soloのバージョンを確認してみます。
$ vagrant sandbox rollback
$ vagrant ssh

~$ chef-solo -v
Chef: 10.12.0
状態のコミットは
$ vagrant sandbox commit
で出来るようです。

Vagrantのインストールと起動メモ

VagrantやChefをそろそろ見ていかないと取り残されてしまう気がしてきたので。。。w

とりあえずVagrantのインストールと起動のメモです。

VirtualBoxのインストール
https://www.virtualbox.org/wiki/Downloads から自分のOSのインストーラをDLして、指示に従いインストールします。

Vagrantのインストール
http://downloads.vagrantup.com/ からv1.2.4をインストーラでインストールしました。以下のコマンドだと古いバージョンだったので。
$ sudo gem install vagrant
#アンインストールは以下のコマンドで
$ sudo gem uninstall vagrant
OSイメージのインストール
http://www.vagrantbox.es/ から適当に選んでみます。暫く時間がかかります。
$ vagrant box add ubuntu1304 http://cloud-images.ubuntu.com/raring/current/raring-server-cloudimg-vagrant-amd64-disk1.box
適当なディレクトリを作ってvagrant initして起動してみます。
$ mkdir -p vagrant/test
$ cd vagrant/test/
$ vagrant init ubuntu1304
# config.vm.network :private_network, ip でIPアドレスを設定
$ vim Vagrantfile
$ vagrant up
sshで接続してみます。
$ vagrant ssh
Welcome to Ubuntu 13.04 (GNU/Linux 3.8.0-26-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Sun Jul 21 09:52:33 UTC 2013

  System load:  0.28              Processes:           79
  Usage of /:   2.4% of 39.33GB   Users logged in:     0
  Memory usage: 33%               IP address for eth0: 10.0.2.15
  Swap usage:   0%

  Graph this data and manage this system at https://landscape.canonical.com/

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

  Use Juju to deploy your cloud instances and workloads:
    https://juju.ubuntu.com/#cloud-raring

0 packages can be updated.
0 updates are security updates.

Last login: Sun Jul 21 09:52:20 2013 from 10.0.2.2
vagrant@vagrant-ubuntu-raring-64:~$
とりあえず今回はここまでで。。。

追記:
以下でsshコマンドでログインが可能になる
$ vagrant ssh-config --host xxx >> ~/.ssh/config
$ ssh xxx

各種ブログとかでIPアドレス拒否(簡易かつ力技)

どこでも良いので、どこかのサーバにIPアドレスを出力するプログラムを置きます。
"Access-Control-Allow-Origin"ヘッダは適当に調整して下さい。
<?php
header('Access-Control-Allow-Origin: *');
echo $_SERVER['REMOTE_ADDR'];
各種ブログとかでAjaxしてリダイレクトとかさせます。
// jQueryの読込直後が一番早いと思います
$.get('前述のプログラムのURL', function(ip) {
    if (ip == 'xxx.xxx.xxx.xxx') {
        location.href = 'http://example.com/';
    }
});
抜け道はたくさん有るでしょうが、"簡易かつ力技"ということで。

July 13, 2013

PHPで可変個な引数の受け渡しメモ

メモです。
<?php

function foo()
{
    var_dump('In foo: '.print_r(func_get_args(), true));
    call_user_func_array('bar', func_get_args());
}

function bar()
{
    var_dump('In bar: '.print_r(func_get_args(), true));
}

foo('xxx', 'yyy');
インスタンスの場合は
<?php

class Baz
{
    public function foo()
    {
        var_dump('In foo: '.print_r(func_get_args(), true));
        call_user_func_array(array($this, 'bar'), func_get_args());
    }

    public function bar()
    {
        var_dump('In bar: '.print_r(func_get_args(), true));
    }
}

(new Baz)->foo('xxx', 'yyy');

July 7, 2013

Backgrid.jsでmultipleなSelect2CellEditorを使う際の注意

Backgrid.jsでmultipleなSelect2Cellを使って

表 > プルダウンにする > 適当に選択 > 表に戻す > プルダウンにする

の操作をした時、適当に選択した内容がクリアされてしまいました。たぶん、ビューの生成と破棄を繰り返す関係なんだと思います。

幸いにも、適当に選択した内容は、対応するモデルの中には残っていたので、以下のようにして対応出来ました。
var MyEditor = Backgrid.Extension.Select2CellEditor.extend({
    render: function () {
        Backgrid.SelectCellEditor.prototype.render.apply(this, arguments);

        _.each(this.model.get('labels'), function(labelId) {
            this.$el.find('option').each(function() {
                if ($(this).val() == labelId) {
                    $(this).attr('selected', true);
                }
            });
        }, this);

        this.$el.select2(this.select2Options);
        this.delegateEvents();

        return this;
    }

});
* ボイントは this.$el.select2() を呼び出す前にselected属性を付けることです。
var MyCell = Backgrid.Extension.Select2Cell.extend({
    editor: MyEditor,
    multiple: true,
    optionValues: labels
});
* Backgrid.Extension.Select2Cellを拡張したCellに、前述のMyEditorをセットします。

このMyCellを、カラムのcell値で指定する感じです。

Backgrid.jsを少し見てみた

Backgrid.jsはBackbone.jsのライブラリです。
http://backgridjs.com/

簡単に言えば、表形式のGUIに特化したビュー群ライブラリ。といった感じでしょうか。Excelみたいな感じのGUIをWEBで表現する際に役立ちそうです(Excel自体は大嫌いですけど)。

Backgrid.jsには、Backbone.Viewから派生した色々なビューが用意されています。以下、その一部です。

Backgrid.Grid … 表全体のビューです。
Backgrid.Row … 各行(tbodyのtr)のビューです。
Backgrid.Footer … フッタ行(tfootのtr)のビューです。

Backgrid.HeaderCell … ヘッダ行の各セル(theadのtrのth)のビューです。
Backgrid.Cell … 各セル(tbodyのtrのtd)のビューです。
Backgrid.CellEditor … 各セル(tbodyのtrのtd)の入力欄時のビューです。

Backgrid.CellとBackgrid.CellEditorは、以下のように、デフォルトで色々と拡張されています(v0.2.6のドキュメントより引用)。
* Backgrid.Cell
* Backgrid.DatetimeCell
* Backgrid.DateCell
* Backgrid.TimeCell
* Backgrid.NumberCell
* Backgrid.IntegerCell
* Backgrid.StringCell
* Backgrid.UriCell
* Backgrid.EmailCell
* Backgrid.BooleanCell
* Backgrid.SelectCell
* Backgrid.CellEditor
* Backgrid.InputCellEditor
* Backgrid.SelectCellEditor

Moment.jsを用いたCellや、Select2を用いたCellも用意されていました。

Moment.js
http://momentjs.com/

Select2
http://ivaynberg.github.io/select2/

これらの他にも色々な機能がありそうですが、必要に応じて探って行きたいと思います。