2020年10月22日木曜日

Eclipse Sirius Web のサンプルプロジェクトを試す

Eclipse Sirius の クラウドベーススタックである Sirius Web, それのサンプルを動かした。

基本的には sirius-web/README.adoc の手順通り。

各コンポーネントの同期がとれていないのか、手順そのままでは動かなかったので、いくつか詰まった点を追記している。

前提

  • OS: Windows 10 Pro
  • Docker: Docker version 19.03.13, build 4484c46d9d
  • 使用イメージ: debian:buster-slim

GitHub Package Registry 向けのパーソナルアクセストークンを生成する

  1. Settings -> Developer settings -> Personal access tokens -> Generate new token
  2. New personal access token で必要事項入力
    • Note : read package registry
    • repo にチェックを入れる
    • read:packages にチェックを入れる
    • Generate token キー押下
  3. 表示されたアクセストークンをメモ

ビルド用コンテナ起動

最終的に、このコンテナで生成した jar ファイルを Windows 上で動かす。(ので、ポート指定は無し)

docker run -it --rm --name sirius-web -v "$(pwd):/work" -v "$HOME/.m2:/root/.m2" --workdir="/work" debian:buster-slim

作業用コンテナ内で環境構築

ビルドに必須な環境を構築

Java11, Maven, curl, git, nodejs, yalc, rollup が必要。

apt-get update
mkdir -p /usr/share/man/man1
apt-get install -y openjdk-11-jdk-headless curl git maven

# nodejs インストール
curl -sL https://deb.nodesource.com/setup_15.x | bash -
apt-get install -y nodejs

# rollup, yalc インストール
npm install -g rollup yalc

# npm 自身のアップグレード
npm install -g npm

ソース取得・ビルド

ObeoNetwork/Flow-Designer

cd /work
git clone https://github.com/ObeoNetwork/Flow-Designer.git
cd Flow-Designer
mvn clean install

ビルド済みのものが以下 issue にぶら下がっているが、それに気付く前にビルドしてしまった。

See: https://github.com/eclipse-sirius/sirius-web/blob/d4955a228200b22d78b9c37e45101efda03b976b/backend/sirius-web-sample-application/pom.xml#L44

eclipse-sirius/sirius-emf-json

# sirius-emf-json の取得・ビルド・インストール
cd /work
git clone https://github.com/eclipse-sirius/sirius-emf-json.git
cd sirius-emf-json
mvn clean install -f releng/org.eclipse.sirius.emfjson.releng/pom.xml

eclipse-sirius/sirius-components

# sirius-components の取得
cd /work
git clone https://github.com/eclipse-sirius/sirius-components.git

# sirius-components のフロントエンドをビルド・ローカルパブリッシュ
cd /work/sirius-components/frontend
npm install
npm run build
yalc publish

# sirius-components のバックエンドをビルド・インストール
cd /work/sirius-components/backend
mvn clean install

eclipse-sirius/sirius-web

GitHub の npm registry へのログイン設定

フロントエンドで GitHub の NPM パッケージレジストリを使用しているのでその設定。

echo '//npm.pkg.github.com/:_authToken=TOKEN' > ~/.npmrc
  • TOKEN: パーソナルアクセストークン

GitHub の Maven registry へのログイン設定

バックエンドで GitHub の Maven パッケージレジストリを使用しているのでその設定。

export USERNAME=mikoto2000
export PASSWORD=TOKEN
  • TOKEN: パーソナルアクセストークン

eclipse-sirius/sirius-web のソース取得・ビルド

cd /work
git clone https://github.com/eclipse-sirius/sirius-web.git

# フロントエンド
cd /work/sirius-web/frontend

# npm registry の obeo/sirius-components を削除して、
# ローカルの eclipse-sirius/sirius-components を追加
# See: https://github.com/eclipse-sirius/sirius-web/issues/1#issuecomment-712639686
sed -i -e 's/    "@obeo\/sirius-components": "0.1.27",//' ./package.json
yalc add @eclipse-sirius/sirius-components

# それにともなって、 import している箇所も修正
sed -i -e 's/obeo\/sirius-components/eclipse-sirius\/sirius-components/' ./src/index.js
sed -i -e 's/obeo\/sirius-components/eclipse-sirius\/sirius-components/' ./src/main/Main.js
npm install
npm run build

# バックエンド
cd /work/sirius-web
mkdir -p backend/sirius-web-frontend/src/main/resources/static
cp -R frontend/build/* backend/sirius-web-frontend/src/main/resources/static
cd /work/sirius-web/backend
mvn -s /work/sirius-web/settings.xml clean package

サンプルアプリケーションの実行

ビルドの時点で必要なものは jar の中に全部入ったから、これ以降は Windows 上での作業。

# sirius web 用の PostgreSQL サーバー起動
# See: https://github.com/eclipse-sirius/sirius-web/blob/master/scripts/restart-siriusweb-postgresql.sh
docker run -p 5433:5432 --rm --name sirius-web-postgres -e POSTGRES_USER=dbuser -e POSTGRES_PASSWORD=dbpwd -e POSTGRES_DB=sirius-web-db -d postgres

# Sirius Web サンプルアプリケーション起動
java -jar sirius-web/backend/sirius-web-sample-application/target/sirius-web-sample-application-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev --spring.datasource.url=jdbc:postgresql://localhost:5433/sirius-web-db --spring.datasource.username=dbuser --spring.datasource.password=dbpwd

http://localhost:8080 へアクセスすると、 Sirius Web の画面が表示される。

参考資料

2020年10月5日月曜日

Sprint Boot で Jackson を使う

試していきます。

前提

  • OS: Windows 10 Pro
  • Docker: Docker version 19.03.13, build 4484c46d9d
    • 使用イメージ: mikoto2000/openjdk:15
  • Java: openjdk 15 2020-09-15

プロジェクト作成

Spring Initializr で作成。

ここ を参照。

依存関係追加

pom.xml に Jackson の依存関係を追加。

        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.3</version>
        </dependency>

処理実装

まず、 JacksonApplication.java 全体を載せる。

package dev.mikoto2000.study.jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

import lombok.Data;

@SpringBootApplication
public class JacksonApplication {

    @Autowired
    private ObjectMapper jacksonObjectMapper;

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(JacksonApplication.class, args);
        JacksonApplication app = context.getBean(JacksonApplication.class);
        app.process(args);
    }

    public void process(String... args) throws JsonProcessingException {

        String userStringOrig = """
                                {
                                    "id": 0,
                                    "name": "mikoto2000"
                                }
                                """;

        // JSON 文字列からオブジェクトへの変換
        User user = jacksonObjectMapper.readValue(userStringOrig, User.class);
        System.out.println(user);

        // オブジェクトから JSON 文字列への変換
        String userString = jacksonObjectMapper.writeValueAsString(user);
        System.out.println(userString);

    }

    @Bean
    public ObjectMapper mapper() {
        return new ObjectMapper();
    }

}

@Data
class User {
    Long id;
    String name;
}

ObjectMapper の設定

JacksonApplication クラスに、以下修正を行う。

@Autowired できるように(?)、 ObjectMapper を返却する mapper メソッドを追加。

    @Bean
    public ObjectMapper mapper() {
        return new ObjectMapper();
    }

@Autowired で Jackson の ObjectMapper クラスをマッピング。

    @Autowired
    private ObjectMapper jacksonObjectMapper;

変換主処理

readValue, writeValueAsString を使って、オブジェクトと JSON 文字列の変換を行う。

    public void process(String... args) throws JsonProcessingException {

        String userStringOrig = """
                                {
                                    "id": 0,
                                    "name": "mikoto2000"
                                }
                                """;

        // JSON 文字列からオブジェクトへの変換
        User user = jacksonObjectMapper.readValue(userStringOrig, User.class);
        System.out.println(user);

        // オブジェクトから JSON 文字列への変換
        String userString = jacksonObjectMapper.writeValueAsString(user);
        System.out.println(userString);

    }

ログ出力設定

src/main/resources/application.yaml

spring:
  main:
    banner-mode: off

src/main/resources/logback.xml

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <appender name="CONSOLE_APPENDER" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d %-5level [%thread] %logger{0}: %msg%n</pattern>
        </encoder>
    </appender>
    <root level="ERROR">
        <appender-ref ref="CONSOLE_APPENDER"/>
    </root>
</configuration>

で、完成したものがこちら。

参考資料

Reprepro で APT リポジトリを構築する

前々回, 前回 と、deb パッケージを作ったので、これを公開する APT リポジトリを作っていく。

コンテナ起動

docker run -it --rm -v "$(pwd):/work" --workdir="/work" debian:buster-slim

APT リポジトリの構築

必須パッケージのインストール

apt-get update
apt-get install -y gnupg2 reprepro

署名用の鍵生成

鍵生成の設定ファイル作成

gpg --full-generate-key
  1. Please select what kind of key you want : `(1) RSA and RSA (default)`
  2. What keysize do you want? (3072) : 4096
  3. Please specify how long the key should be valid. : 0
  4. Is this correct? (y/N) : y
  5. Real name : mikoto2000
  6. Email address : mikoto2000@gmail.com
  7. Comment : Athrill apt repository key.
  8. Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? : O
  9. Passphrase : (空)

鍵の ID をメモしておく。

(今回は 6B5F3367A3F74C173E2E83612F6719AE3DEFC0C0 だった。)

リポジトリ設定

リポジトリ設定格納用ディレクトリの作成

mkdir -p /work/reprepro/conf

リポジトリ設定作成

/work/reprepro/conf/distributions

cat <<EOF> /work/reprepro/conf/distributions
Origin: TOPPERS/hakoniwa
Label: Athrill
Codename: bionic
Architectures: amd64
Components: main
Description: Apt repository for project Athrill
SignWith: 6B5F3367A3F74C173E2E83612F6719AE3DEFC0C0
EOF
  • Codename : ディストリビューションのコードネーム。bionic だったり buster だったり
  • SignWith : gpg コマンドで作成した公開鍵の ID

パッケージの取り込み

cd /work/reprepro
reprepro includedeb bionic /work/athrill-rh850f1x_0.A.0-1_amd64.deb

署名の設定

cd /work/reprepro
gpg --armor --output athrill.gpg.key --export 6B5F3367A3F74C173E2E83612F6719AE3DEFC0C0

動作確認

Web サーバーを立ててそこからインストールしてみる。

APT リポジトリとなる nginx 起動

新しく nginx のコンテナを立てる。

先ほど作成した reprepro ディレクトリを /usr/share/nginx/html にマウントする。

docker run -it --rm -v "$(pwd)/reprepro:/usr/share/nginx/html:ro" -p "80:80" --name nginx nginx

※ 実際は、 db/, conf/ は見えないようにするらしい

正しくインストールできるか確認

先ほど立ち上げた nginx から、正しく apt でインストールできるかを確認する。

新たに debian:buster-slim のコンテナを立ち上げて、 host.docker.internal の APT リポジトリを登録する。

インストールする debian の起動

docker run -it --rm ubuntu:18.04

必要パッケージインストール

apt-get update
apt-get install -y curl gnupg2 software-properties-common

APT リポジトリのキー登録

curl -fsSL http://host.docker.internal/athrill.gpg.key | apt-key add -

APT リポジトリ登録

add-apt-repository "deb http://host.docker.internal bionic main"
apt-get update

インストール

apt-get install -y athrill-rh850f1x

鍵の管理

このままだと、コンテナ終了時に秘密鍵も消えてしまうので、コンテナからエクスポートしてコピーすること。

鍵のエクスポート

cd /
gpg --list-secret-keys
gpg --export-secret-keys --output backup.sec.key mikoto2000@gmail.com
gpg --export --output backup.pub.key mikoto2000@gmail.com

鍵のインポート

gpg --import ./backup.sec.key
gpg --import ./backup.pub.key

参考資料