忘備録

日々の調べ物をまとめる。アウトプットする。基本自分用。

tr に min-height 当てたいけどできなかった時

td に height 当たればいいんじゃない?

<!DOCTYPE html>
<html>
<head>
<style>
  table, td, th {
    border: 1px solid black;
    border-collapse: collapse;
  }
  td { height: 10px; }
</style>
</head>
<body>
<h1>Use "height" to td tag for height of tr tag instead of "min-height"</h1>
<table>
  <caption>example</caption>
  <thead>
    <tr>
      <th>th1</th>
      <th>th2</th>
      <th>th3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>td1</td>
      <td>td2</td>
      <td>td3</td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
    </tr>
  </tbody>
</table>

<p>Reference</p>
<cite><a href="https://stackoverflow.com/questions/19432092/can-i-use-a-min-height-for-table-tr-or-td">https://stackoverflow.com/questions/19432092/can-i-use-a-min-height-for-table-tr-or-td</a></cite>
</body>
</html>

tmux インストール時のエラー

環境

エラー内容

Homebrew でインストールした tmux を起動するとエラーが発生

$ tmux
dyld: lazy symbol binding failed: Symbol not found: _clock_gettime
  Referenced from: /usr/local/opt/libevent/lib/libevent-2.0.5.dylib
  Expected in: flat namespace

dyld: Symbol not found: _clock_gettime
  Referenced from: /usr/local/opt/libevent/lib/libevent-2.0.5.dylib
  Expected in: flat namespace

Trace/BPT trap: 5
Makoto-Air:memo_md makoto$ brew remove tmux
Uninstalling /usr/local/Cellar/tmux/2.2... (9 files, 639.7K)

解決方法

Homebrew を update して、再度インストールする

$ brew update

brew update 時に発生したエラーとその対処方法

根本的な解決作は上記の通り brew の update ですが、update 実行時にいくらかエラーが起きました。

エラーその1

内容
$ brew update
Error: /usr/local must be writable!
解決方法

/usr/local 以下のパーミッションを変更する

素人がEl Capitanにアップデートしてみたやつ。 - Qiita

$ sudo chown -R $(whoami):admin /usr/local

エラーその2

内容
$ brew update
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
/usr/local/Library/brew.sh: line 32: /usr/local/Library/ENV/scm/git: No such file or directory
Error: update-report should not be called directly!
解決方法

brew prune を行う

Homebrewでbrew updateしたら/usr/local/Library/ENV/scm/git: No such file or directoryと言われてしまう - Smoky God Express

$ brew prune
Pruned 0 symbolic links and 2 directories from /usr/local

蛇足

ついでに、サブコマンドのコマンド入力補完設定方法をメモっておく

completion の ソース入手

$ curl https://raw.githubusercontent.com/Bash-it/bash-it/master/completion/available/tmux.completion.bash > ~/.tmux.completion.bash

~/.bash_profile に編集

source ~/.tmux.completion.bash

AmazonLinux で EC2を立てたらとりあえずやること

前提

カーネルアップデート

$ yum update

自動更新

yum-cron を起動しておくと、夜間に自動的に yum update がかかるようになるので、入れておく。

インストール。

$ yum install -y yum-cron

起動。

$ service yum-cron start

起動設定。

$ chkconfig yum-cron on

日本語対応

日本語パッケージの追加

日本語パッケージがAmazon Linuxyumリポジトリにはないので、CentOS6のyumリポジトリを追加する。

$  cat << 'EOS' > /etc/yum.repos.d/centos6-base.repo
[base]
name=CentOS-6 - Base
mirrorlist=http://mirrorlist.centos.org/?release=6&arch=x86_64&repo=os
enabled=0
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6
EOS

日本語パッケージのインストール。

$ yum -y groupinstall "Japanese Support" --enablerepo=base

日本語設定

/etc/sysconfig/i18n

LANG=en_US.UTF-8

から

LANG=ja_JP.UTF-8

に変更、再読み込み。

$ source /etc/sysconfig/i18n 

以降、シェルが返すコメントが日本語になっているはず。

サーバ時刻をJSTに変更

$ cp -p /usr/share/zoneinfo/Japan /etc/localtime

確認。

$ date
2016年  9月 28日 水曜日 12:44:34 JST

/etc/sysconfig/clockも編集する。

$ cat << EOS > /etc/sysconfig/clock
ZONE="Asia/Tokyo"
UTC=False
EOS

作業ユーザ作成

今回は作業ユーザとして、 hoge ユーザを作成する。

ユーザ作成と同時にグループを wheel を指定。

$ useradd hoge -G wheel

作成したユーザにsudo権限を付与

visudo でsudo設定ファイルを編集する

100行目付近のコメントアウトを外す

## Same thing without a password
%wheel  ALL=(ALL)       NOPASSWD: ALL

これで wheel グループのユーザは、パスワードなしで sudo を実行できるようになる

ちなみに、以下のように環境変数を設定しておくと、 visudo で起動するエディタが vi ではなく vim になり幸せになれるかも(ただし、Vimmerに限る)

$ export EDITOR=/usr/bin/vim

SSHの公開鍵認証設定

この設定は作成したユーザにログインして行う。

認証設定ファイルの作成。

$ mkdir ~/.ssh
$ chmod 700 ~/.ssh
$ touch ~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/authorized_keys

~/.ssh/authorized_keysを編集して公開鍵を登録する。

$ vi ~/.ssh/authorized_keys

ちなみに、SSHの設定ファイルは /etc/ssh/sshd_config

この設定ファイルはデフォでいい感じになっているので、特に変更することはないが、一応 PermitRootLoginPasswordAuthenticationno になっていることくらいは確認しておくと精神衛生上良い。

ec2-user削除

いらんので、 ec2-user を消す。

# userdel -r ec2-user

ツッコミおねしゃす

とりあえず、以上です。

他にこの設定したほうがいいんじゃないってことがあれば、是非突っ込んでください。。。

参考

visudoコマンド(sudoコマンドの為の設定ファイルの編集)

一般ユーザーを sudo できるようにする - maruko2 Note.

sudoersを変更する、よく使う設定例 - それマグで!

cloud-initでAmazon Linuxの初期設定を制御する | OpenGroove

Amazon EC2(Linux)システム管理で知らないとハマる5つの環境設定 | Developers.IO

【Linux】スワップ領域(ファイル)の作り方

環境

Amazon Linux

パーティションを切るか、ファイルで済ますか

スワップ領域の作り方を調べたところ、スワップ用のパーティションを用意する方法と、ファイルを用意する方法の2通りあることが分かった。

linux スワップ(swap)領域の作成

スワップ(swap)領域を作成するには、mkswapコマンドを使います。指定したパーティション全てをスワップ(swap)領域にすることもできるし、ファイルをスワップ(swap)領域とすることもできます。

で、どちらでやるのが良いか(今回はEC2という前提)

Amazon EC2(Linux)のswap領域ベストプラクティス | Developers.IO

スワップのためにボリュームやパーティーションを追加するのは、EBSがボリュームサイズで課金されることからコスト面で敷居が高いです。そのため、既存ボリュームにswapファイルを追加のがお奨めです。

また、知り合いのインフラエンジニアに伺ったところ、「ファイルでしかスワップ領域を作ったことない」とのことだったので、とりあえずスワップファイルでスワップ領域を作れればよさそう。

というわけのなので、今回はスワップファイルでスワップ領域を作成する。

スワップファイルの作り方

1. ファイルの用意

まずは、スワップ用ファイルを用意する。

サイズは今回1GB。

ddコマンドについてはコチラ

# dd if=/dev/zero of=/Swapfile bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 13.0465 s, 82.3 MB/s

権限の変更をする。

変更しておかないと、スワップファイルを有効化した際に警告を受けることになる。

# chmod 600 /Swapfile

参考:権限未設定時の警告

swapon: /Swapfile: insecure permissions 0644, 0600 suggested.

2. フォーマット

mkswapスワップファイル用としてフォーマットする。

# mkswap /Swapfile
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=a209fc68-4a95-4e91-ae85-3d5bd09339b7

3. 有効化

swaponスワップファイルを有効化する。

# swapon /Swapfile

確認

# swapon -s
Filename                Type        Size    Used    Priority
/Swapfile                               file    1048572 0   -1

Finish!!

X. おまけ

スワップファイルの無効化はswapoffでできる

# swapoff /Swapfile

参考

【 Ruby on Rails】一意制約

どうやって一意制約を設けるか。

軽くググるとふたつの方法

[Rails]マイグレーション時にUNIQUEインデックスを設定する方法(単体・複数の場合) – hello-world.jp.net

ActiveRecord4のバリデーションで複数カラムにユニーク制約を付ける方法

SchemaStatements#add_index を使う

add_index :users, [:user_id, :event_id], unique: true

TableDefinition#index を使う

create_table :invited_users do |t|
  t.references :user, foreign_key: true
  t.references :event, foreign_key: true

  t.timestamps

  t.index [:user_id, :event_id], unique: true
end

一意制約キーの名称について

TableDefinition#indexにもSchemaStatements#add_indexにもnameオプションがあり一意制約キーの名称を付けることができる。

nameオプションは省略可能で、以下のフォーマットで命名される。

{テーブル名}_on_{カラム名}_and_{カラム名}...

例)invited_usersテーブルにuser_idカラムとevent_idカラムで一意制約を設けた場合

index_invited_users_on_user_id_and_event_id

長いですね、、、

カラムが2つならまだいいですが、3つ、4つと増える場合は、nameオプションを利用して明示的に命名してあげたほうが良さそうです。

『Web制作者のためのCSS設計の教科書 モダンWeb開発に欠かせない「修正しやすいCSS」の設計手法』

逃げてきたCSSの勉強に立ち向かう時が来た気がするので、ポチりました。

Web制作者のためのCSS設計の教科書 モダンWeb開発に欠かせない「修正しやすいCSS」の設計手法

良著(だと思う)な上、kindle版が現在616円と激安なのでオススメです。

要点だけメモしていきます。

良いCSSとは

  • 予測しやすい
  • 再利用しやすい
  • 保守しやすい
  • 拡張しやすい

破綻しやすいCSS

  • HTMLの構造に依存している
  • スタイルを取り消している
  • 絶対値を多用している

詳細度

上から順に詳細度が高い。

詳細度を考慮して設計することが非常に重要。

  1. !important
  2. インライン記述
  3. IDセレクタ
  4. クラスセレクタ、属性セレクタ、擬似クラス
  5. 要素セレクタ、擬似要素
  6. ユニバーサルセレクタ

以下メモ

  • !importantはむやみに使用しない
  • インライン記述はデバッグなどの 時以外は使用しない

Railsのローカルインストール

Rails5がリリースされたので、ちょこっと試してみたい。

f:id:mktktmr:20160727022341p:plain

でも、普段は4.x系なので、グローバル環境とは別にRailsをインストールしたい。

その方法を調べたのでまとめ。

0. 環境

1. ローカルインストールためのディレクトリを適当に掘る

$ mkdir v5
$ cd v5

2. Gemfileを作成

Gemfile

source "http://rubygems.org"
gem "rails" # 今回は最新5.0を入れたいのでバージョン指定なし

3. rbenv-binstubs をインストール

rbenv-binstubs

ひたすらbundle execを打ち込む苦行(?)から解放されるというナイスなプラグイン

$ git clone https://github.com/ianheggie/rbenv-binstubs.git "$(rbenv root)/plugins/rbenv-binstubs"

4. Rails インストール

$ bundle install --path vendor/bundle

--path vendor/bundle が肝で、このオプションによりローカルのvendor/bundle ディレクトリにインストールする。

5. プロジェクト作成

--skip-bundleを忘れるとbundle installが走って悲しいことになるので注意

$ bundle exec rails new example --skip-bundle

--skip-bundleはお忘れなく(大事なことなのでry)

6. Railsコンポーネントのインストール

プロジェクトのディレクトリに移動し、bundle installするが、その際、手順3でインストールしたrbenv-binstubsを利用するために --binstubs=vendor/binを追加する。

これにより、railsコマンドやrakeコマンドを叩く度にいちいちbundle execをつけなくて済むようになる。

$ cd example
$ bundle install --path vendor/bundle --binstubs=vendor/bin

7. 確認

$ rails -v
Rails 5.0.0

これにてDONE

参考

Rails開発環境の構築(複数バージョン共存可能)(Homebrew編) - Qiita

bundle exec はもういやだ - Qiita