Vagrantを使っている時のssh秘密鍵使い回しではまったメモ

Vagrantで立ち上げたOS上でも、ホストマシンのsshの共通鍵(id_rsa)
を使いまわそうと


(ホストマシン)
# vagrantのフォルダへ移動
cd ~/vagrant/precise32/
ln -s ~/.ssh/id_rsa id_rsa

として
# vagrant ssh


(vagrantのOS上)
# vi ~/.ssh/config
Host heroku.com
 User git
 port 22
 Hostname heroku.com
 IdentityFile /vagrant/id_rsa
 TCPKeepAlive yes
 IdentitiesOnly yes

とかしたけど、全然つながらない…

普通に、ゲストマシンからはホストマシンの~/.sshとか見れないからあたりまえ

コピーして使いまわしました


(ホストマシン)
# vagrantのフォルダへ移動
cd ~/vagrant/precise32/
cp -a ~/.ssh/id_rsa id_rsa

コピーして散乱させたくないので、sshのキー管理のベストプラクティスが知りたいところです
使いまわすなってとこでしょうか..

bundle updateとbundle installの使い分け – ruby

Gemfile.lockには実際インストールされたgemが記録されています

bundle update
 Gemfile.lockを無視してgemをインストール

bundle install
 Gemfile.lockを見てgemをインストール
 Gemfileに変更があればgemをインストールしてGemfile.lockを更新

例えばGemfileに新しく’jquery-rails’を追記して


(Gemfile)
gem 'rails', '>= 4.0.5'
...
gem 'jquery-rails'

うっかり bundle update しちゃうと
railsもバージョンアップしてしまいます
いろんなものが動かなくなったり…

Gemfileにバージョン指定して書いておけばその点は安心でしょうか
(バージョン書かないのは最新版が入る)


(Gemfile)
gem 'rails', '4.0.5'
gem 'jquery-rails', '3.0.4'

というふうに

PHPのコード規約チェックをしてみる CodeSniffer

品質向上のため、phpの書き方をきちんと管理しようとおもい
コード規約に違反してないか調べてくれるPHP_CodeSnifferを入れてみます

開発環境のためComposerでglobalに入れました

Composerのインストールはこちらを参考に
UbuntuでPHPUnitをComposerでインストールしてみる

# composer global require ‘squizlabs/php_codesniffer:*’
# phpcs –version
PHP_CodeSniffer version 1.5.4 (stable) by Squiz (http://www.squiz.net)

入りました

チェックできるコーディング規約を確認します
# phpcs -i
The installed coding standards are Squiz, PSR1, PEAR, PSR2, MySource, PHPCS and Zend

ためしにPSR1とPSR2で、PHPファイルだけをチェックしてみます
# phpcs –standard=PSR1,PSR2 –extensions=php FizzBuzz.php
FILE: /home/user/repos/fizzbuzz.php/FizzBuzz.php
——————————————————————————–
FOUND 9 ERROR(S) AFFECTING 7 LINE(S)
——————————————————————————–
8 | ERROR | Visibility must be declared on method “__construct”
16 | ERROR | Expected “for (…) {\n”; found “for(…) {\n”
22 | ERROR | Visibility must be declared on method “evaluate”
22 | ERROR | Opening brace should be on a new line
23 | ERROR | Inline control structures are not allowed
24 | ERROR | Inline control structures are not allowed
25 | ERROR | Inline control structures are not allowed
29 | ERROR | Visibility must be declared on method “echoFizzBuzz”
29 | ERROR | Opening brace should be on a new line
——————————————————————————–

phpファイル修正前


<?php
class FizzBuzz
{
    private $_start;
    private $_end;

    function __construct($start, $end)
    {   
        $this->_start = $start;
        $this->_end = $end;
    }   

    public function publish()
    {   
        for($i = $this->_start; $i < $this->_end + 1; $i++) {
            $str = $this->evaluate($i);
            $this->echoFizzBuzz($str);
        }   
    }   

    function evaluate($num) {
        if ($num % 15 == 0) return 'FizzBuzz';   
        if ($num % 3 == 0) return 'Fizz';
        if ($num % 5 == 0) return 'Buzz';
        return $num;
    }   

    function echoFizzBuzz($str) {
        echo $str, PHP_EOL;
    }   
}

 
修正します

  • functionにアクセス権をつける
  • forと(の間に半角スペースをいれる
  • {は新しい行から始める
  • インラインの書き方はやめる

<?php

class FizzBuzz
{
    private $_start;
    private $_end;

    public function __construct($start, $end)
    {
        $this->_start = $start;
        $this->_end = $end;
    }

    public function publish()
    {
        for ($i = $this->_start; $i < $this->_end + 1; $i++) {
            $str = $this->evaluate($i);
            $this->echoFizzBuzz($str);
        }
    }

    private function evaluate($num)
    {
        if ($num % 15 == 0) {
            return 'FizzBuzz';
        }
        if ($num % 3 == 0) {
            return 'Fizz';
        }
        if ($num % 5 == 0) {
            return 'Buzz';
        }
        return $num;
    }

    private function echoFizzBuzz($str)
    {
        echo $str, PHP_EOL;
    }
}

これで直りました