DragonFlyBSD + php7 インストールログ

目的としては php-fpm を使いたい。

環境

DragonFly v4.4.3-RELEASE

ログ

適当に検索すると下記のものが出てくる。とりあえず php7 を使うので php70 を入れる。( fpm で検索しても出てこなかった)

php55-5.5.35                   PHP Scripting Language
php56-5.6.21                   PHP Scripting Language
php70-7.0.6_1                  PHP Scripting Language
$ sudo pkg install php70
Updating Avalon repository catalogue...
Avalon repository is up-to-date.
All repositories are up-to-date.
The following 1 package(s) will be affected (of 0 checked):

New packages to be INSTALLED:
        php70: 7.0.6_1

The process will require 17 MiB more space.
2 MiB to be downloaded.

Proceed with this action? [y/N]: y
php70-7.0.6_1.txz                        : 100%    2 MiB 350.8kB/s    00:06
Checking integrity... done (0 conflicting)
[1/1] Installing php70-7.0.6_1...
[1/1] Extracting php70-7.0.6_1: 100%

php-fpm が入っていることを確認

$ php-fpm -v
PHP 7.0.6 (fpm-fcgi) (built: May 27 2016 16:49:24)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

sudo php-fpm すれば動くけど rc.conf には書けるんだろうか

適当に rc.conf に書いたら動いた

$ sudo sh -c 'echo "php_fpm_enable=\"YES\"" >> /etc/rc.conf'

unix socket を使う場合、デフォルトで入っている設定が /usr/local/etc/php-fpm.d/www.conf にあるので下記のように変更する。とりあえず /var/run 以下に socket を設置。 owner や group も nginx と同じユーザでないと permission denied になる。

listen = /var/run/php-fpm.sock
...
listen.owner = www
listen.group = www
listen.mode = 0660

DragonFlyBSD + nginx インストールログ

単純に pkg で nginx インストールするだけ.

環境

DragonFly v4.4.3-RELEASE

ログ

事前に sudo を入れてあります。

$ sudo pkg install nginx

現時点ではバージョンは 1.10.0 らしい。

$ nginx -v
nginx version: nginx/1.10.0

OS 起動時に nginx が起動するようにする。

$ sudo sh -c 'echo "nginx_enable=\"YES\"" >> /etc/rc.conf'

nginx 起動

$ sudo service nginx start
Performing sanity check on nginx configuration:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
Starting nginx.

設定ファイルは 上記のとおり /usr/local/etc/nginx/nginx.conf にある。

一つのリポジトリに複数サーバの etckeeper で push する

一つのリポジトリに複数サーバの etckeeper で push したい。

etckeeper は一つのサーバから複数のリモートリポジトリに push できるような設定はあるが逆に 複数のサーバから 一つのリモートリポジトリに push できるようにする設定はなさそう。

etckeeper は master しか使わないみたいなのでそのまま使うと push できるのは master のみ。なので下記のように push のフックで push するブランチ先を指定するようにすると一旦はよさそう。

diff --git a/etckeeper/commit.d/99push b/etckeeper/commit.d/99push
index b5418f7..a8d20d9 100755
--- a/etckeeper/commit.d/99push
+++ b/etckeeper/commit.d/99push
@@ -2,7 +2,7 @@
 if [ -n "$PUSH_REMOTE" ]; then
        if [ "$VCS" = git ] && [ -d .git ]; then
                for REMOTE in $PUSH_REMOTE; do
-                       git push "$REMOTE" master || true
+                       git push "$REMOTE" master:mybranch || true
                done
        elif [ "$VCS" = hg ] && [ -d .hg ]; then
                for REMOTE in $PUSH_REMOTE; do

HttpComponents で JSON レスポンスをオブジェクトに変換して取得する

チュートリアルに書いてあるとおり ResponseHandler を使うと HttpClient#execute() の返り値を特定のクラスにして返すようにできる。これを jackson を使って返すようにしてみた。

http://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/fundamentals.html#d5e199

ResponseHandler (Apache HttpClient 4.5.2 API)

public class JacksonResponseHandler<T> implements ResponseHandler<T> {
    private static final ObjectMapper mapper = new ObjectMapper();
    private TypeReference<T> typeRef;

    @Override
    public T handleResponse(HttpResponse response) throws IOException {
        HttpEntity entity = response.getEntity();
        if (entity == null) {
            throw new ClientProtocolException("Response contains no content");
        }
        if (!entity.getContentType().getValue().startsWith("application/json")) {
            throw new ClientProtocolException("Response is not json");
        }

        InputStream content = entity.getContent();
        if (content == null) {
            throw new ClientProtocolException("Response contains no content");
        }

        try {
            return mapper.readValue(content, typeRef);
        } catch (IOException e) {
            throw new ClientProtocolException("cannot parse as response", e);
        }
    }
}

etckeepr + github のプライベートリポジトリで /etc 以下をバックアップする

etckeepr + github のプライベートリポジトリで /etc 以下をバックアップする

etckeeper 導入

$ sudo apt-get install etckeeper

etckeeper 側で push 先を設定

diff --git a/etckeeper/etckeeper.conf b/etckeeper/etckeeper.conf
index f988c10..1fecf03 100644
--- a/etckeeper/etckeeper.conf
+++ b/etckeeper/etckeeper.conf
@@ -40,4 +40,4 @@ LOWLEVEL_PACKAGE_MANAGER=dpkg
 # To push each commit to a remote, put the name of the remote here.
 # (eg, "origin" for git). Space-separated lists of multiple remotes
 # also work (eg, "origin gitlab github" for git).
-PUSH_REMOTE=""
+PUSH_REMOTE="origin"

github でプライベートリポジトリを作成しておいてそれをリモートリポジトリに設定

$ cd /etc
$ sudo git remote add origin git@github.com:name/repo-name.git

鍵設定

ルート権限で push する様子なので /root/.ssh/ 以下に鍵を作成(もともと鍵がないか一応注意)。

$ sudo ls /root/.ssh/
known_hosts
$ sudo ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
...

root 用に ssh key を設定して プライペートリポジトリで鍵を追加する。「Allow write access」にチェックが入れられるのでそのリポジトリだけに書き込み権限を追加した鍵を設定することができる(現状ここは bitbucket ではできない)。

f:id:nise_nabe:20160513102229p:plain

最後に

人のサーバとかで鍵を置きたくない場合などにリポジトリ単位で鍵が設定できる github 便利。 プライベートリポジトリ無制限になりさらに便利。 そしてわりと簡単に導入できて apt でアップデートしたり cron.daily だったりで自動的にコミットとプッシュがされる etckeeper 便利。

参考

github.com

qiita.com

javadoc の html を生成したときに一番最初のページに説明を追加したい

Java Platform SE 8

例えば下記の「このドキュメントはJava(tm) Platform, Standard EditionのAPI仕様です。」と書かれてるようなところ。

f:id:nise_nabe:20160310114112p:plain

下記ページをみると「概要コメント・ファイル」と呼ぶらしいファイルを使うことができればよいらしい。 通常は -overview でパスを指定するとのこと。

docs.oracle.com

mavenjavadoc を生成している場合は下記ページ「Overview Comment File: overview.html」を参考に、 src/main/javadoc ディレクトリを作って overview.html を作成してその中に記載すれば良いらしい。

Apache Maven Javadoc Plugin – Using Javadoc Resources

一応上記のように src/main/javadoc/overview.html に記載したものが javadoc の最初のページに反映されたが パッケージの下の部分に表示されてしまい、上のほうに表示されなかったのであとで追記するかも。

JUnit でテスト全体の実行前および実行後の処理を書く

例えば DB を実行する前や後に初期化する場合など BeforeClass や AfterClass よりももっと大きな単位で JUnit テストの前と後で実行したいことがある場合。

org.junit.runner.notification.RunListener を使う。

package my.hogehoge;

import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.RunListener;

public class MyListener extends RunListener {
    @Override
    public void testRunStarted(Description description) throws Exception {
    }

    @Override
    public void testRunFinished(Result result) throws Exception {
    }
}

直接 JUnit を動かす場合は JUnitCore#addListener() などでこれを登録するといいらしい。 実際 JUnit 動かすのは maven-surefire-plugin 経由などで実行すると思うので、その場合は下記のように設定するとよい。 ちなみに 一つも Test アノテーションが付いたものがないと testRunStarted() なども呼ばれない。

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <configuration>
                    <properties>
                        <property>
                            <name>listener</name>
                            <value>my.hogehoge.MyListener</value>
                        </property>
                    </properties>
                </configuration>
            </plugin>
        <plugins>
    </build>