page_adsence

2015年6月4日木曜日

WindowsのIntelliJで既存のPHPプロジェクトを開く

WindowsでIntelliJを使うようになったのですが、既存のPHPプロジェクトを開こうとした時に、Import Projectを使うと、何故かプロジェクト内のソースをActionScriptだと判断されてしまい、大抵のソースが読み込まれないという自体に。

なので、Create New Projectで既存のディレクトリを指定して開くようにしてみたらうまくいったので、その時のメモ。


1.Create New Projectをクリック


2.PHP Empty Projectを選択して、Nextをクリック


3.Project locationの部分を選択して、読み込みたいディレクトリを選択し、任意のProject nameを入れるてFinishボタンをクリック。このように既存ディレクトリを選択することで、Import Projectと同じ事ができる。

jenkinsを起動時に指定できるオプション一覧

以前の記事にも書いたのですが、Jenkinsをyum経由でインストールするのではなく、warファイルのみをダウンロードして起動しています。
調べてみたらyum経由でインストールした場合にできるファイルがなく、どうすればいいのかわからなかったのですが、どうやら起動時のオプションで指定する事ができるみたいです。
それ以外のオプションも書いてあったので、起動コマンドと一緒にメモしておく。

su -l vagrant -c java -jar /usr/lib/jenkins/jenkins.war --httpPort=18080

その他のパラメータは以下のURLを参考に。
https://wiki.jenkins-ci.org/display/JA/Starting+and+Accessing+Jenkins
コマンドライン・パラメータ説明
--javaHome=$JAVA_HOMEJavaやAntが必要なビルドを実行するときに使用する$JAVA_HOMEをJenkinsに渡します。これはJenkinsを起動するJREではありません(Jenkins上で使用するJREを指定します)。デフォルトは、Jenkinsを実行するのに使用したJREです。
--httpPort=$HTTP_PORTJenkinsが使用するHTTPプロトコルのポート番号を指定します。デフォルトは8080です。これを使用しない(_HTTPS_を使用)ようにするには、-1を指定します。
--httpListenAddress=$HTTP_HOSTこのパラメータで、Jenkinsがリクエストを受け付けるIPアドレスを指定できます。環境変数$HTTP_HOSTが指定されていれば、それを利用します。デフォルトでは 0.0.0.0 です。--- この場合、すべての有効なインタフェース上で(httpPortで指定された)ポートをリスンします。
--httpsPort=$HTTP_PORTHTTPSプロトコルで使用するポート番号を指定します。
--httpsListenAddress=$HTTPS_HOSTJenkinsがHTTPSプロトコルでリクエストを受け付けるIPアドレスを指定できます。環境変数$HTTPS_HOSTが指定されていれば、それを利用します。
--ajp13Port=$AJP_PORT標準のAJP13プロトコルを使う場合、$AJP_PORTRを利用して、Jenkinsがリスンするポートを指定できます。デフォルトは8009です。(_HTTPS_を使用するため) これを使用しないようにするには、-1を指定します。
--ajp13ListenAddress=$AJP_HOSTJenkinsがAJP13プロトコルのリクエストを受け付ける、IPアドレスを指定できます。デフォルトでは 0.0.0.0 です。 --- i.e. この場合、すべての有効なインタフェース上で(ajpPortで指定された) ポートをリスンします。
--argumentsRealm.passwd.$ADMIN_USER=passwordユーザー $ADMIN_USERのパスワードを設定します。Jenkinsのセキュリティが有効化されている場合、Jenkinsやプロジェクトを設定するために、$ADMIN_USERでログインする必要があります。注意: 管理者権限を持つユーザーも指定しなければなりません。(以下を参照してください)
--argumentsRealm.roles.$ADMIN_USER=admin$ADMIN_USERを管理者として設定し、Jenkinsのセキュリティが有効化されていれば、Jenkinsを設定できるようにします。詳しくはJenkinsのセキュリティを参照してください。

2015年5月27日水曜日

vagrantからVirtualBoxが起動出来なくなる問題の解決方法

Windows + Cygwin + Vagrant + VirtualBoxという環境で開発をしているのですが、
たまに、VirtualBoxを立ち上げていない状態で、Cygwinから「vagrant resume」してしまう事がある。
そうすると下記の様なメッセージが出てきて、VMが立ち上がらない状態になってしまう。(人によってはVirtualBoxが立ち上がってくれる?)

$ vagrant resume
==> default: VM not created. Moving on...

VirtualBoxを後から起動させてみると、VirtualBox上はsuspend状態のまま。
しかしvagrantコマンドでステータスを確認してみると

$ vagrant status
Current machine states:

default                   not created (virtualbox)

The environment has not yet been created. Run `vagrant up` to
create the environment. If a machine is not created, only the
default provider will be shown. So if a provider is not listed,
then the machine is not created for that environment.

VMは作成されていない状態になっている。
ちなみにこの状態で「vagrant up」してもすでに同名のVirtualBox名(Vagrantfile内のvb.name)が存在しているためエラーになる。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos6.4'...
==> default: Matching MAC address for NAT networking...
A VirtualBox machine with the name 'centos' already exists.
Please use another name or delete the machine with the existing
name, and try again.

なぜこんな状態になっているかというと、vagrantとVirtualBoxの関連付けをしているファイルが無い、もしくは中身が空の状態になっている事が原因らしい。
下記のファイルがあるか、中身が書かれているかを確認してみる。(Vagrantfileが置いてある所がvagrant_home)

/path/to/vagrant_home/.vagrant/machines/default/virtualbox/id

試しに確認してみたら、

$ ls -la .vagrant/machines/default/virtualbox/
合計 4
drwxr-xr-x+ 1 kusagaya-naoki Domain Users 0 5月  26 15:31 .
drwxr-xr-x+ 1 kusagaya-naoki Domain Users 0 5月  18 13:11 ..

案の定ファイルは存在していなかった。
なので、ここにファイルを作成してやればいいのですが、VirtualBox側のVMのUUIDを調べる必要がある。
UUIDの調べ方は以下の通り。

VirtualBoxのVMのUUIDの調べ方


1.コマンドプロンプトを立ち上げて、VirtualBoxのインストールディレクトリへ移動

cd C:\Program Files\Oracle\VirtualBox

2.VBoxManageでVirtualBoxにあるVMのUUIDの一覧を確認する。
※vm_nameにはvb.nameの名前が書かれている

C:\Program Files\Oracle\VirtualBox> VBoxManage.exe list vms
"vm_name" {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}

これでVMのUUIDがわかったので、下記のディレクトリ配下にidファイルを作成する。

$ vi /path/to/vagrant_home/.vagrant/machines/default/virtualbox/id

ファイルの先頭に調べたUUIDを書いて保存する。

XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

vagrant側のステータスを確認してみる。

$ vagrant status
Current machine states:

default                   saved (virtualbox)

To resume this VM, simply run `vagrant up`.

not createdからsavedに変わった事を確認して起動してみる。

$ vagrant resume
==> default: Resuming suspended VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2223
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection refused. Retrying...
==> default: Machine booted and ready!

ちゃんと起動しました。
よかった・・・。

2015年5月11日月曜日

Ansibleで変数入りのファイルをリモートマシンへコピーする

Ansibleで開発環境を作成しようとすると、各種設定ファイル等を使い回したくなります。
通常であれば、ホストマシン側に設定ファイルを置いておき、それをリモートマシンへcopyモジュールを利用してコピーするのですが、それだとホストマシンに置いてある設定ファイルをそのまま設置することになってしまいます。
プロジェクトによって書き換えたい部分とかが出てくると思います。ログの吐き出し先とか。
そういった場合はcopyモジュールを使用するのではなく、templateモジュールを使用する事で、元ファイルに入れてある変数を展開した状態でリモートマシンにコピーする事が出来ます。
変数の使い方に関しては割愛します。

変数入りのファイルを用意します。
$ vi files/file.conf
{{ project_name }}

プレイブックに下記の様な行を追加します。
$ vi playbook.yml
- template: src=files/file.conf dest=/etc/file.conf mode=0644

プレイブックを実行します。
ansible-playbook playbook.yml --extra-vars "project_name=test-project"

処理終了後にリモートマシンにコピーしたファイルを確認します。
$ vi /etc/file.conf
test-project

この様な感じで変数展開された状態のファイルが設置されています。

2015年4月9日木曜日

Vagrantでsuspendからresumeする際のポート問題

vagrantでマシンを起動や停止を行う際に、「vagrant up」「vagrant halt」を使っていたが、
これだと起動や停止にかかる時間が長いのでよく利用するVMに関しては「vagrant suspend」(一時停止)「vagrant resume」(復帰)を利用するようにしている。
しかしこの「vagrant suspend」「vagrant resume」を複数のVMで併用していると、ある工程で作業をすると問題が出てくる。

問題となる手順は以下の通り。
1.VM1を「vagrant up」で起動
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => C:/cygwin64/home/XXXXXXXXX/XXXXXXXXX/XXXXXXXXX-1
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: to force provisioning. Provisioners marked to run always will still run.

2.VM1を「vagrant suspend」で一時停止
$ vagrant suspend
==> default: Saving VM state and suspending execution...

3.VM2を「vagrant up」で起動
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => C:/cygwin64/home/XXXXXXXXX/XXXXXXXXX/XXXXXXXXX-2
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: to force provisioning. Provisioners marked to run always will still run.

4.VM2を「vagrant suspend」で一時停止
$ vagrant suspend
==> default: Saving VM state and suspending execution...

5.VM1で「vagrant resume」で起動
$ vagrant resume
==> default: Resuming suspended VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection refused. Retrying...
==> default: Machine booted and ready!

6.VM2で「vagrant resume」をするが起動しない。
$ vagrant resume
Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening
on these ports. The forwarded port to 2222 is already in use
on the host machine.

To fix this, modify your current projects Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:

  config.vm.network :forwarded_port, guest: 22, host: 1234

Sometimes, Vagrant will attempt to auto-correct this for you. In this
case, Vagrant was unable to. This is usually because the guest machine
is in a state which doesn't allow modifying port forwarding.

ここで、何が問題になって起動しないかというと、ホストマシンからVMにSSH接続する際に使用するポートの重複が問題となっている。
※VMを2台同時に立ち上げる事が問題なのではなく、立ち上げる時の工程に問題がある。

こういった問題を回避する為に、予めローカルからVMにSSH接続する際に使用するポート番号を変更しておく必要がある。
ポート番号の変更方法は、Vagrantfileに下記の様な設定を追加する事で回避出来る。

config.vm.network "forwarded_port", id: "ssh", guest: 22, host: 2223

一度全てのVMを「vagrant halt」で停止させてから、Vagrantfileを編集し、上記設定を追記した状態で「vagrant up」する。
追加する場所は下記のコメントがあるので、その下辺りに追加する。

# config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "forwarded_port", id: "ssh", guest: 22, host: 2223 ← この部分を追記

保存した状態で再び「vagrant up」すると、
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2223 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2223
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => C:/cygwin64/home/XXXXXXXXX/XXXXXXXXX/XXXXXXXXX-2
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: to force provisioning. Provisioners marked to run always will still run.

この様な感じで立ち上がる。
ちなみに複数台立ち上げて「vagrant resume」「vagrant suspend」しても問題ない手順は以下の通り。

1.VM1を「vagrant up」で起動
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => C:/cygwin64/home/kusagaya-naoki/VirtualBoxMachines/jenkins-laravel
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: to force provisioning. Provisioners marked to run always will still run.

2.VM2を「vagrant up」で起動
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2200 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => C:/cygwin64/home/kusagaya-naoki/VirtualBoxMachines/jenkins-laravel-2
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: to force provisioning. Provisioners marked to run always will still run.

3.VM1を「vagrant suspend」で停止
$ vagrant suspend
==> default: Saving VM state and suspending execution...

4.VM2を「vagrant suspend」で停止
$ vagrant suspend
==> default: Saving VM state and suspending execution...

5.VM1を「vagrant resume」で起動
$ vagrant resume
==> default: Resuming suspended VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection refused. Retrying...
==> default: Machine booted and ready!

6.VM2を「vagrant resume」で起動
$ vagrant resume
==> default: Resuming suspended VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection refused. Retrying...
==> default: Machine booted and ready!

赤文字にした部分も見てもらえれば一目瞭然だと思いますが、
なぜこの手順で問題ないかというと、「vagrant up」時にはSSHポート番号が重複していた場合に
vagrantが自動で別の開いているポートを使用して立ち上げる様になっている。
しかし、このポートチェックは現在起動しているVMで使用されているポート番号だけで、一時停止されているVMに使われているポート番号はチェック対象にならない。
そのため、手順によってはこういった現象が発生する。
常にこの様な手順で起動させるようにしていればこういった事態にはならないが、常にこの手順を守れるわけではないので、予めポート番号は設定しておいたほうが良さげ。

スッキリわかるJava 第2版を買ってみた。

会社のメンバーと何か作ろうという話になったので、とりあえずJavaの勉強から初めて見る。
どんな本がいいかなと色々探したけど、評価的にはこれが一番良さそうだったので買ってみた。


Kindle版買うかどうか悩みましたが、結局紙の本を買った。
でも電車の中で読んだりするにはKindle版の方が良かったかなと。

これからどんどん読み進めて行こうと思う。

2015年4月7日火曜日

AnsibleでJenkinsのインストールからJenkinsのプラグインインストールまで。

ローカルのVMにjenkinsをインストールして使おうと思ったのですが、調べてみたらyumでインストールしている記事ばかりだったので、 warファイルをダウンロードしてきて、それを起動するシェルスクリプトを作り、サービスとして登録し、起動させるという方法でやってみた。
最終的な成果物としては下記の様なplaybookとなった。
playbookで書かれているjenkins.shも書いておく。

files/jenkins.shの中身
#!/bin/sh
# chkconfig: 2345 70 30
# description: jenkins
# processname: jenkins

java -jar /usr/lib/jenkins/jenkins.war &

ここからAnsibleのプレイブック
---
- hosts: "target_host"
  sudo: yes
  remote_user: vagrant
  tasks:
    - name: JavaのOpenJDKをインストール
      yum: name=java-1.8.0-openjdk state=latest

    - name: JavaのOpenJDK develをインストール
      yum: name=java-1.8.0-openjdk-devel state=latest

    - name: Jenkinsのwarファイルの設置用ディレクトリを作成
      file: dest=/usr/lib/jenkins state=directory

    - name: jenkins.warファイルをサイトからダウンロード
      get_url: url=http://mirrors.jenkins-ci.org/war/latest/jenkins.war
               dest=/usr/lib/jenkins

    - name: ホストPCに置いておいたjenkins.shをリモートPCへコピー
      template: src=files/jenkins.sh
                dest=/etc/init.d/jenkins
                mode=0777

    - name: chkconfigにjenkinsを登録
      action: command chkconfig --add jenkins

    - name: jenkinsの起動レベルを変更
      action: command chkconfig --level 2345 jenkins on

    - name: jenkinsを起動
      service: name=jenkins state=started enabled=yes
      sudo: no

    - name: jenkinsのcliツールをダウンロード
      get_url: url=http://localhost:8080/jnlpJars/jenkins-cli.jar
               dest=/tmp

    - name: プラグインをインストール [build-pipeline-plugin]
      action: command java -jar /tmp/jenkins-cli.jar -s http://localhost:8080 install-plugin build-pipeline-plugin

    - name: プラグインをインストール [timestamper]
      action: command java -jar /tmp/jenkins-cli.jar -s http://localhost:8080 install-plugin timestamper

    - name: プラグインをインストール [git]
      action: command java -jar /tmp/jenkins-cli.jar -s http://localhost:8080 install-plugin git

    - name: プラグインをインストール [jobConfigHistory]
      action: command java -jar /tmp/jenkins-cli.jar -s http://localhost:8080 install-plugin jobConfigHistory

    - name: プラグインをインストール [cron_column]
      action: command java -jar /tmp/jenkins-cli.jar -s http://localhost:8080 install-plugin cron_column

    - name: プラグインをインストール [next-executions]
      action: command java -jar /tmp/jenkins-cli.jar -s http://localhost:8080 install-plugin next-executions

    - name: プラグインをインストール [ssh]
      action: command java -jar /tmp/jenkins-cli.jar -s http://localhost:8080 install-plugin ssh

    - name: jenkins restart
      action: command java -jar /tmp/jenkins-cli.jar -s http://localhost:8080 safe-restart


jenkinsのプラグインをコマンドラインからインストールする場合に、
java -jar /tmp/jenkins-cli.jar -s http://localhost:8080 install-plugin XXXXXX
と記載する必要があるのですが、このXXXXXの部分はPluginIDを入れる必要がある。
PluginIDの調べ方はJenkinsのプラグインの一覧があるページから入れたいプラグインをクリックすると、上のほうにある表の左上に「Plugin ID」の項目があるので、そこに書かれているIDを入れる。

Vagrant upをした時のエラー

vagrantを使ってVM作ろうと思ったのですが、Vagrantfileの中身を色々と間違えていた事に気がついたので、1から作リ直そうとした時に出たエラー。

行った作業としては以下の通り。

$ vagrant init box-name

$ vi Vagrantfile

$ vagrant up

ここで間違いに気がついたので、VirtualBoxでマシンを停止。(ここでVirtualBoxで止めるのではなく、vagrant haltしてれば起きなかったかもしれない)

$ vagrant destroy

$ vim Vagrantfile
Vagrantfileの中身を修正(但しvb.nameは同じ)

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos6.4-30gb'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: jenkins-laravel
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["modifyvm", "a36f7bef-e56e-4686-91a4-a649a92f3a17", "--name", "jenkins-laravel"]

Stderr: VBoxManage.exe: error: Could not rename the directory 'C:\Users\username\VirtualBox VMs\vagrant-centos64_1_1427941735336_99768' to 'C:\Users\username\VirtualBox VMs\jenkins-laravel' to save the settings file (VERR_ALREADY_EXISTS)
VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component SessionMachine, interface IMachine, callee IUnknown
VBoxManage.exe: error: Context: "SaveSettings()" at line 2716 of file VBoxManageModifyVM.cpp

こんな感じのエラーが出た。
色々と試行錯誤してみたけど、原因はvagrant destroyした時に
C:\Users\username\VirtualBox VMs\jenkins-laravel
のフォルダが消えていなかったため、重複していてリネームできなかった事が原因だった。

2015年2月13日金曜日

Ansibleで作った環境をServerspecでテストする

ServerspecとはAnsibleやChef、Puppetといったプロビジョニングツールで作成された環境が、本当に意図した通りに作成されているのかをテストするためのツール。
Rubyで書かれていて、RubyのテストフレームワークであるRSpecの書き方に準拠している。

説明はこのくらいにして、実際に使ってみる。
テストする環境としては、自分のローカルPC上に作成したVMマシンで、
既にVagrant、VirtualBox、Ansibleが使える状態になっている。
今回、ServerspecでテストするサーバはVagrant使って新しいVMを作っておく。

まずはインストールから。
gemがインストールしてあることが前提。

user_name@computer_name ~
$ gem install serverspec
DL is deprecated, please use Fiddle
Fetching: rspec-support-3.2.1.gem (100%)
Successfully installed rspec-support-3.2.1
Fetching: rspec-mocks-3.2.0.gem (100%)
Successfully installed rspec-mocks-3.2.0
Fetching: rspec-expectations-3.2.0.gem (100%)
Successfully installed rspec-expectations-3.2.0
Fetching: rspec-core-3.2.0.gem (100%)
Successfully installed rspec-core-3.2.0
Fetching: rspec-3.2.0.gem (100%)
Successfully installed rspec-3.2.0
Fetching: rspec-its-1.1.0.gem (100%)
Successfully installed rspec-its-1.1.0
Fetching: multi_json-1.10.1.gem (100%)
Successfully installed multi_json-1.10.1
Fetching: net-scp-1.2.1.gem (100%)
Successfully installed net-scp-1.2.1
Fetching: specinfra-2.12.7.gem (100%)
Successfully installed specinfra-2.12.7
Fetching: serverspec-2.8.2.gem (100%)
Successfully installed serverspec-2.8.2
Parsing documentation for multi_json-1.10.1
Installing ri documentation for multi_json-1.10.1
Parsing documentation for net-scp-1.2.1
Installing ri documentation for net-scp-1.2.1
Parsing documentation for rspec-3.2.0
Installing ri documentation for rspec-3.2.0
Parsing documentation for rspec-core-3.2.0
Installing ri documentation for rspec-core-3.2.0
Parsing documentation for rspec-expectations-3.2.0
Installing ri documentation for rspec-expectations-3.2.0
Parsing documentation for rspec-its-1.1.0
Installing ri documentation for rspec-its-1.1.0
Parsing documentation for rspec-mocks-3.2.0
Installing ri documentation for rspec-mocks-3.2.0
Parsing documentation for rspec-support-3.2.1
Installing ri documentation for rspec-support-3.2.1
Parsing documentation for serverspec-2.8.2
Installing ri documentation for serverspec-2.8.2
Parsing documentation for specinfra-2.12.7
Installing ri documentation for specinfra-2.12.7
Done installing documentation for multi_json, net-scp, rspec, rspec-core, rspec-expectations, rspec-its, rspec-mocks, rspec-support, serverspec, specinfra after 40 seconds
10 gems installed

次にテストしたいVMのVagrantfileがあるディレクトリへ移動。
自分の場合は下記のディレクトリだったので、そこへ移動。

user_name@computer_name ~
$ cd VirtualBoxMachines/test

でserverspecを初期化する。

user_name@computer_name ~/VirtualBoxMachines/test
$ serverspec-init
DL is deprecated, please use Fiddle
Select OS type:

  1) UN*X
  2) Windows

Select number: 1 ← serverspecでチェックするマシンがUnix系なのかWindows系なのかを選択

Select a backend type:

  1) SSH
  2) Exec (local)

Select number: 1 ← SSHで接続するリモートサーバなのか、ローカルサーバなのか。

Vagrant instance y/n: y ← Vagrantで作成したマシンなのか
Auto-configure Vagrant from Vagrantfile? y/n: y ← Vagrantfileを元にしてserverspecの設定ファイルを勝手に作ってくれる
 + spec/
 + spec/default/
 + spec/default/sample_spec.rb
 + spec/spec_helper.rb
 + Rakefile
 + .rspec

初期化すると、上記に書かれているファイルができている。
特に修正するわけでもなく、早速テストしてみる。

user_name@computer_name ~/VirtualBoxMachines/test
$ rake spec
rake aborted!
Circular dependency detected: TOP => spec => spec:all => spec:default => spec:all

Tasks: TOP => spec => spec:all => spec:default
(See full trace by running task with --trace)

エラーになった・・・。
原因をググって見ると、下記の様な記事が出てきたので、とりあえず記事通りに修正してみる。
https://saintaardvarkthecarpeted.com/blog/archive/2014/10/_Circular_dependency_detected__error_in_ServerSpec.html

user_name@computer_name ~/VirtualBoxMachines/test
$ vim Rakefile
require 'rake'
require 'rspec/core/rake_task'

task :spec    => 'spec:all'
task :default => :spec

namespace :spec do
  targets = []
  Dir.glob('./spec/*').each do |dir|
    next unless File.directory?(dir)
    targets << File.basename(dir)
  end

  task :all     => targets
  # task :default => :all ← ここをコメントアウトする

  targets.each do |target|
    desc "Run serverspec tests to #{target}"
    RSpec::Core::RakeTask.new(target.to_sym) do |t|
      ENV['TARGET_HOST'] = target
      t.pattern = "spec/#{target}/*_spec.rb"
    end
  end
end

もう一回試しに走らせてみる。
user_name@computer_name ~/VirtualBoxMachines/test
$ rake spec
/usr/local/bin/ruby.exe -I'/usr/local/lib/ruby/gems/2.1.0/gems/rspec-support-3.2.1/lib':'/usr/local/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib' '/usr/local/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/exe/rspec' --pattern 'spec/default/*_spec.rb'
DL is deprecated, please use Fiddle

Package "httpd"
  should be installed (FAILED - 1)

Service "httpd"
  should be enabled (FAILED - 2)
  should be running (FAILED - 3)

Port "80"
  should be listening (FAILED - 4)

Failures:

  1) Package "httpd" should be installed
     On host `default'
     Failure/Error: it { should be_installed }
       expected Package "httpd" to be installed
       sudo -p 'Password: ' /bin/sh -c rpm\ -q\ httpd
       package httpd is not installed

     # ./spec/default/sample_spec.rb:4:in `block (2 levels) in '

  2) Service "httpd" should be enabled
     On host `default'
     Failure/Error: it { should be_enabled }
       expected Service "httpd" to be enabled
       sudo -p 'Password: ' /bin/sh -c chkconfig\ --list\ httpd\ \|\ grep\ 3:on

     # ./spec/default/sample_spec.rb:8:in `block (2 levels) in '

  3) Service "httpd" should be running
     On host `default'
     Failure/Error: it { should be_running }
       expected Service "httpd" to be running
       sudo -p 'Password: ' /bin/sh -c ps\ aux\ \|\ grep\ -w\ --\ httpd\ \|\ grep\ -qv\ grep

     # ./spec/default/sample_spec.rb:9:in `block (2 levels) in '

  4) Port "80" should be listening
     On host `default'
     Failure/Error: it { should be_listening }
       expected Port "80" to be listening
       sudo -p 'Password: ' /bin/sh -c netstat\ -tunl\ \|\ grep\ --\ :80\\\

     # ./spec/default/sample_spec.rb:13:in `block (2 levels) in '

Finished in 0.33207 seconds (files took 19.95 seconds to load)
4 examples, 4 failures

Failed examples:

rspec ./spec/default/sample_spec.rb:4 # Package "httpd" should be installed
rspec ./spec/default/sample_spec.rb:8 # Service "httpd" should be enabled
rspec ./spec/default/sample_spec.rb:9 # Service "httpd" should be running
rspec ./spec/default/sample_spec.rb:13 # Port "80" should be listening

/usr/local/bin/ruby.exe -I'/usr/local/lib/ruby/gems/2.1.0/gems/rspec-support-3.2.1/lib':'/usr/local/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib' '/usr/local/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/exe/rspec' --pattern 'spec/default/*_spec.rb' failed

テストは通ってない(apacheインストールしてないので当然ですが)けど、動きました。
真っ赤っ赤で不安になる。
ちなみにテストが通る様になった時の結果がこれ。

user_name@computer_name ~/VirtualBoxMachines/test
$ rake spec
/usr/local/bin/ruby.exe -I'/usr/local/lib/ruby/gems/2.1.0/gems/rspec-support-3.2.1/lib':'/usr/local/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/lib' '/usr/local/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.0/exe/rspec' --pattern 'spec/default/*_spec.rb'
DL is deprecated, please use Fiddle

Package "httpd"
  should be installed

Service "httpd"
  should be enabled
  should be running

Port "80"
  should be listening

Finished in 0.1375 seconds (files took 15.45 seconds to load)
4 examples, 0 failures

緑色になって安心。
こうなるまでひたすらAnsibleのplaybookを修正し続ける。

2015年2月12日木曜日

AnsibleでSELinuxの状態を調べて、既に無効だったらスキップする

前回、SELinuxをOFFにする方法を書いたので、その続きというか発展編。
やり方としてはこんな感じ。

    - name: SELinuxの現状のステータスを確認し、結果を保存する
      shell: getenforce               ← ステータス確認コマンド
      register: is_selinux_off            ← 結果を「is_selinux_off」に保存

    - name: Install libselinux-python
      yum: name=libselinux-python state=installed
      when: is_selinux_off.stdout == 'Enforcing'   ← SELinuxのステータスが「Enforcing」だった場合は処理する

    - name: disable selinux
      selinux: state=disabled
      when: is_selinux_off.stdout == 'Enforcing'   ← 上と同様

    - include: reboot.yml
      when: is_selinux_off.stdout == 'Enforcing'   ← 上と同様

registerにどういった内容が入っているのかを確認するにはこんな感じにするとよい。

    - name: SELinuxの現状のステータスを確認し、結果を保存する
      shell: getenforce
      register: is_selinux_off

    - debug: var=is_selinux_off
      when: is_selinux_off

上記の実行結果の一部がこんな感じ。

TASK: [debug var=is_selinux_off] **********************************************
ok: [xwindow] => {
    "is_selinux_off": {
        "changed": true,
        "cmd": "getenforce",
        "delta": "0:00:00.003592",
        "end": "2015-02-12 18:12:03.063290",
        "invocation": {
            "module_args": "getenforce",
            "module_name": "shell"
        },
        "rc": 0,
        "start": "2015-02-12 18:12:03.059698",
        "stderr": "",
        "stdout": "Enforcing",
        "stdout_lines": [
            "Enforcing"
        ],
        "warnings": []
    }
}