rails web UIでスキーマ定義 ryakuzu
できたらいいのになって考えてたんだけど、ぴったりなgemがあった。
GitHub - ID25/ryakuzu: Interface for schema.rb
だが、2016年を境に更新が止まっている。
自作したいな。
rails5 DBをPostgreSQLに変更する
環境
- OS: CentOS7
- rails: 5
- postgres: 10
PostgreSQL側の作業
postgresユーザのパスワード変更(OSとは独立したPostgres側の認証情報の設定)
初期状態ではOSの認証情報を使用してPostgresのDBにログインできる。
この状態でpostgresユーザーでpsqlを起動し、postgresユーザのパスワードを設定する。
(OSとは独立した、Postgres側の認証情報)
$ sudo -u postgres psql # alter role postgres with password '[PASSWORD]';
[PASSWORD]の部分に任意のパスワードを設定する。
認証方式の切り替え
認証方式をpeerからmd5に変更する。
peer/identは認証にOSの情報が使用される。
(peerはlocal, identはtcp接続時にそれぞれ使用される。)
md5にすると前述のパスワード変更の項で設定したPostgres側の認証情報を使用して認証が行われる。
※デフォルトでpostgresユーザのパスワードは何も設定されていないため、あらかじめpeerの認証でpsqlを起動し、設定しておかないと、md5の認証に切り替えた後、ログイン不能になる。
- peer: https://www.postgresql.org/docs/10/static/auth-methods.html#AUTH-PEER
- ident: https://www.postgresql.org/docs/10/static/auth-methods.html#AUTH-IDENT
$ sudo vim /var/lib/pgsql/10/data/pg_hba.conf
※pg_hba.confの場所は環境により異なる可能性がある。
デーモン再起動
認証方式の設定変更を反映するため、デーモンを再起動する。
$ sudo systemctl restart postgresql-10
railsアプリのDBで使用する任意のUserを作成
$ sudo su - postgres -bash-4.2$ /bin/createuser -P --interactive [username]
新しいロールのためのパスワード: もう一度入力してください: 新しいロールをスーパーユーザにしますか? (y/n)n 新しいロールに対してデータベースを作成する権限を与えますか? (y/n)y 新しいロールに対して別のロールを作成する権限を与えますか? (y/n)n
-bash-4.2$ psql
postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- username | Create DB | {} postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
新規に作成したuserに対してAttributesにCreate DBが付与されていることを確認。
Rails側の作業
アプリ作成時に、DBにpostgresqlを指定する場合
$ rails new <app_name> -d postgresql
上記-dオプションでpostgresqlを指定すると、自動作成されるGemfileに以下の通り、pgが追加される。
# Use postgresql as the database for Active Record gem 'pg', '>= 0.18', '< 2.0'
また、config/database.ymlのadapter設定にpostgresqlがデフォルトで指定される。
default: &default adapter: postgresql
「config/database.ymlを編集」以降の手順は下記「アプリ作成後に、DBをpostgresqlに変更する場合」と同様。
アプリ作成後に、DBをpostgresqlに変更する場合
gemをインストール
gemfileに 'pg'を追加
# Gemfile: 以下を追加 # sqliteを使わない場合はコメントアウト # gem 'sqlite3' gem 'pg'
$ bundle install
以下のerrorが出た場合
Gem::Ext::BuildError: ERROR: Failed to build gem native extension. current directory: /home/ryohei/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/pg-1.0.0/ext /home/ryohei/.rbenv/versions/2.5.0/bin/ruby -r ./siteconf20180314-25746-bfjmau.rb extconf.rb checking for pg_config... no No pg_config... trying anyway. If building fails, please try again with --with-pg-config=/path/to/pg_config checking for libpq-fe.h... no Can't find the 'libpq-fe.h header *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options.
- checking for pg_config... no について
メッセージの通り、pg_configが見つからないので、--with-pg-config=/path/to/pg_configを付与してbundle installすればよい 自分の環境では/usr/pgsql-10/bin/pg_configにあるので、
$ bundle config build.pg --with-pg-config=/usr/pgsql-10/bin/pg_config
- checking for libpq-fe.h... no について
$ sudo yum install -y postgresql10-devel #使用するpostgresqlのver.が10の場合
$ bundle install
config/database.ymlを編集
# config/database.yml # development環境でPostgresを使う場合の例 default: &default adapter: postgresql encoding: unicode pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> timeout: 5000 development: <<: *default database: [app_name]_dev username: [app_name]_dev password: [password] host: localhost
上記の情報をもとに、dbを作成
$ rails db:migrate $ rails db:setup
ここでエラーが出る場合は認証周り(postgres側はpg_hba.confの認証設定、ユーザのパスワード設定、rails側はconfig/database.ymlのuser/password)
CentOS 7/Ruby on Rails 5 開発環境構築
環境
開発環境用。
対象 | version |
---|---|
CentOS | 7.4 |
Ruby | 2.5.0p0 |
Rails | 5.1.2 |
PostgreSQL | 10 |
OS
minimalをDL https://www.centos.org/download/
インストール後、以下を設定
ruby, railsその他
環境構築に時間を割かずさっさと触りたいので雑なansibleスクリプトを書いた。 github.com
基本方針
- firewallは
3000番だけ空けるdiable (どうせAWS/GCP側で制御するよね) - selinuxはoff
- rbenvでrubyをインストールする
- JavaScriptのruntimeはNode.js
- DBはPostgreSQL
- firewallは
playbook実行後
- DBまわりの初期設定作業 midnight-engineering.hatenadiary.jp
Ruby on Rails再入門
随分間が空いて忘れてしまったので例のtutorialで再入門しよう。
前に手を出したときは3系だったが、いまは5.1.2。 色々と様変わりしていそうだ。 tutorial自体も相当updateがかかってbrush upされているみたい。
ansibleでTASKがfailした際のstderrが見辛い読み辛い
タイトルの通り。改行コードが無視されるので非常に見辛い。
何かよい方法はないものだろうか。
現状はとりあえず以下で対応している。
echo -e 'ここにstderr の内容をコピペ'
$ man echo
-e: enable interpretation of backslash escapes
ambariでhadoopクラスタインストールができない
[調査中]
事象
Confirm Hostsの画面にて、全ホストのstatusが問題なくSuccessとなった後、
"Please wait while the hosts are being checked for potential problems..."
の状態から先に進まなくなる。
ambari serverのログには以下のようなメッセージ出力があるが、これの意味するところがわからず調査中。
$ less /var/log/ambari-server/ambari-server.log
WARN [ambari-action-scheduler] ExecutionCommandWrapper:225 - Unable to lookup the cluster by ID; assuming that there is no cluster and therefore no configs for this execution command: Cluster not found, clusterName=clusterID=-1
環境
- OS: CentOS Linux release 7.4.1708
- HDP version: 2.6.3.0 Automated (with Ambari 2.6.0) https://docs.hortonworks.com/HDPDocuments/Ambari-2.6.0.0/bk_ambari-installation/content/ch_Getting_Ready.html
- 構成:
確認事項
- Network Settings
参考情報
同じ境遇の人がいた。(未解決で進行中) Ambari setup stuck at step 3 - Hortonworks
Disable SELinux and PackageKit and check the umask Value - Hortonworks Data Platform
sudo sh -c "echo umask 0022 >> /etc/profile"
mac ターミナルのプロンプト「ユーザ名noMacBook-Pro」を消す
OS Xのターミナルのプロンプトは、デフォルトでは「ユーザ名noMacBook-Pro」などというダサくて長い表記になっているのでこれを修正したい。
macOS Catalina以降のバージョンの場合(bash)
以下のように、/etc/zshrcを編集してプロンプトの表示をシンプルに「ユーザ名」のみが表示されるよう変更する。
$ sudo vim /etc/zshrc
- /etc/zshrc
#PS1="%n@%m %1~ %# " #既存の設定をコメントアウト PS1="%n@%1~ %# "
:wq!で変更を保存
ファイルを修正後、ターミナルを起動し直すかsource /etc/zshrc
を実行すると設定変更が反映される。
macOS Mojave以前のバージョンの場合(bash)
以下のように、/etc/bashrcを編集してプロンプトの表示をシンプルに「ユーザ名」のみが表示されるよう変更する。
$ sudo vim /etc/bashrc
- /etc/bashrc
#PS1='\h:\W \u\$ ' #既存の設定をコメントアウト PS1='[\u@Mac \W]$ '
:wq!で変更を保存
ファイルを修正後、ターミナルを起動し直すと設定変更が反映される。
- 補足
- \h: ホスト名
- \W: カレントディレクトリ
- \u: ユーザ名