一つのリポジトリに複数サーバの 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>

Sparrow が起動しなくなった時について

うちの Macbook は容量がギリギリのため、電池切れなどで容量がないまま終了した場合などに Sparrow が起動できなくなる場合がある。 対処としては Sparrow のディレクトリにある各アカウントの data.db ディレクトリと message.db を削除すればよいらしい。(理由はよく調べてない)

$ cd Library/Application\ Support/Sparrow 
$ rm -rf data.db messages.db

これで Sparrow を起動すると取得しなおして動くようになる。

zsh なら下記一つのコマンドでいける

$ rm -rf ~/Library/Application\ Support/Sparrow/*.sparrowdb/(data.db|messages.db)

Intellij IDEA でクラスを作る際に自作のテンプレートを使う

使用バージョン

Intellij IDEA 14.1.3

内容

競技プログラミングなどでファイル生成時に標準入力用ライブラリなどを最初から入れておきたい場合があります。

Intellij IDEA の場合はどうするか。

  • テンプレートを用意する
  • クラス生成時に用意したテンプレートを選択する

Prefereces を開いて Editor > File and Code Template を開いて Templates のタブを選択。 Class を選択してコピー。なかを自由に書き換えます。

f:id:nise_nabe:20150907112326p:plain

生成したテンプレートはたぶん New > Java Class を選択した際に Kind で選択できるはずです。

f:id:nise_nabe:20150907112337p:plain

他のテンプレートをみると include とか使えるようなので共通で使う部分などは inlude で作っていろんなプロコン用のテンプレートをそれぞれ作るとかできるのではないかと思います。

以上。

参考

IntelliJ IDEA 14.1.0 Help :: File and Code Templates