Mac provisioning method in the spring of 2017


## はじめに
macをクリーンインストールするたびに、デスクトップアプリの再インストールやら
brewでの環境構築やらめんどくさいので、自動化できるところはしておこうというのが本エントリをき書いた目的です。
個人的にはansibleを使って自動構築するのが一番めんどくさくないのでそれでやります。
以下、メモ程度の記載になりますが参考にしてください。

## mac version
Mac : sierra (OS install at 2017/04 )

## install Xcode

``$ sudo xcodebuild -license``

## install Homebrew

``$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" \``

## install Ansible
``$ brew install ansible``

## clone The following repository

```
$ git@github.com:hayashiki/mac-provisioning.git
$ cd mac-provisioning
$ sh setup.sh
```

### ansibleで具体的にやっていること

- install brew-package

 ``brew list`` で表示されるものを一括install

以下を自分用にいじればOK

```:homebrew.yml
    homebrew_packages:
      - { name: elasticsearch }
      - { name: kibana }
      - { name: logstash }
```

- brew-cask-package
desktop applicationを一括install

以下を自分用にいじればOK

```:homebrew.yml
    homebrew_cask_packages:
      - { name: iterm2 }
      - { name: skype }
      - { name: slack }
```

- atom package install
atom packageを一括install

以下を自分用にいじればOK

```:atom-package.yml
  vars:
    atom_packages_packages:
      - project-manager
      - editorconfig
```

## setup go

```:~/.bash_profile
export GOPATH=$HOME
export PATH=$PATH:$GOPATH/bin
```

## setup ghq

### install ghq
``$ go get github.com/motemen/ghq``

edit ``.gitconfig``

```:.gitconfig
$ git config --global user.name ":yourname"
$ git config --global user.email ":youremail"
$ git config --global ghq.root "~/src"
```

### install ghq-open

``$ go get -u github.com/typester/gh-open``

### how to use

```
$ cd $(ghq list -p | peco)
$ ghq get git@github.com:hayashiki/mac-provisioning.git
```

## alias 登録
こんな感じで適当にエイリアス作ると便利


```:~/.bash_profile
alias gh='cd $(ghq list -p | peco)'
alias gho='gh-open $(ghq list -p | peco)'
```

## peco スニペット

```:~/.snippets
# rails
bundle exec rake db:migrate

```

```:~/.bash_profile
alias cmd='$(grep -v "^#" ~/.snippets | peco )'
```

## git config

### git ghq

### git secrets


### global gitignore

git config --global core.excludesfile ~/.gitignore_global


```:~/.gitignore_global
.DS_Store
.envrc
```

### 最終的なgit

```:~/.gitconfig
[user]
>-name = hayashiki
>-email = example@example.com
[ghq]
>-root = ~/src
[secrets]
>-providers = git secrets --aws-provider
>-patterns = [A-Z0-9]{20}
>-patterns = (\"|')?(AWS|aws|Aws)?_?(SECRET|secret|Secret)?_?(ACCESS|access|Access)?_?(KEY|key|Key)(\"|')?\\s*(:|=>|=)\\s*(\"|')?[A-Za-z0-9/\\+=]{40}(\"|')?
>-patterns = (\"|')?(AWS|aws|Aws)?_?(ACCOUNT|account|Account)_?(ID|id|Id)?(\"|')?\\s*(:|=>|=)\\s*(\"|')?[0-9]{4}\\-?[0-9]{4}\\-?[0-9]{4}(\"|')?
>-allowed = AKIAIOSFODNN7EXAMPLE
>-allowed = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
[init]
>-templatedir = ~/.git-templates/git-secrets
[core]
>-editor = /usr/local/bin/vim
>-excludesfile = /Users/hayashida/.gitignore_global
```




rbenvを更新してrailsセットアップ

## setup rbenv & bundler

```:~/.bash_profile
export PATH=/usr/local/bin:$PATH
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
```

source ~/.bash_profile 

```
rbenv install 2.3.1
rbenv global 2.3.1
gem install bundler 
bundle update
```

2.3.1箇所をお好きなバージョンで。

## setup nodejs

### install via nodebrew 

```
$ nodebrew install-binary v6.9.1
$ nodebrew use v6.9.1
```

```:~/.bash_profile
export PATH=\$HOME/.nodebrew/current/bin:\$PATH
export PATH="$HOME/.yarn/bin:$PATH"
```

これでnodeをyarnでつかえるようになる。

## setup redis

### Register launchctl

```
$ ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
```

## setup mysql

### launchctl登録

```
$ ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
```

### if you dont want to register launchctl
``$ mysql.server start``

## setup postgresql

### Register launchctl
```
$ ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
```

### if you dont want to register launchctl

``$ postgres -D /usr/local/var/postgres``

## setup python
pyenvでpythonのバージョン切り替えができるようにする

```:~/.bash_profile
export PYENV_ROOT="${HOME}/.pyenv"
export PATH="${PYENV_ROOT}/bin:$PATH"
eval "$(pyenv init -)"
```

```
$ pyenv list
$ pyenv install anaconda3-4.3.0
$ pyenv global anaconda3-4.3.0
```

## setup mongodb

### Register launchctl

```
$ ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
```

### if you dont want to register launchctl
後で書く

## setup elasticsearch

### Register launchctl

```
$ ln -sfv /usr/local/opt/elasticsearch/*.plist ~/Library/LaunchAgents
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.elasticsearch.plist
```

### if you dont want to register launchctl
後で書く


### 最終的な.bash_profile

```
# general
export PATH=/usr/local/bin:$PATH
export PATH="/usr/local/opt/qt@5.5/bin:$PATH"

# go
export GOPATH=$HOME
export PATH=$PATH:$GOPATH/bin
# rbenv
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
# nodejs
export PATH=$HOME/.nodebrew/current/bin:$PATH
export PATH="$HOME/.yarn/bin:$PATH"

# myalias
alias gh='cd $(ghq list -p | peco)'
alias gho='gh-open $(ghq list -p | peco)'
alias s='ssh $(grep -iE "^host[[:space:]]+[^*]" ~/.ssh/config|peco|awk "{print \$2}")'

# python
export PYENV_ROOT="${HOME}/.pyenv"
export PATH="${PYENV_ROOT}/bin:$PATH"
eval "$(pyenv init -)"
```

## LaunchAgentプロセス確認

```
$ launchctl list | grep brew
57849	0	homebrew.mxcl.elasticsearch
1142	0	homebrew.mxcl.mongodb
1156	0	homebrew.mxcl.postgresql
1146	0	homebrew.mxcl.redis
1149	0	homebrew.mxcl.mysql
```


## dir env

```
:~/.bash_profile
eval "$(direnv hook bash)"
export EDITOR='atom -nw'
```

```
mkdir test
cd test
vi .envrc
export SAMPLE_PORT=6379
env | grep SAMPLE_PORT
```

## tips

- ansible実行は長いので、brew-caskにchrome(デフォブラウザ)は記載せず、手動installする
そしたらブラウザで作業はできるので。。

- imageMagicは手動install
バージョン7系がはいってしまうので

- いくつかのpackageで冪等性がない
ansibleは冪等性があるように構築しないといけないのだが、
vimやkarabinerとか2度目でエラーなっちまったので、コメントアウト、

## todo
- dotfileについても記載する
- もう少し丁寧にかいて、限定投稿やめる

## ref
https://www.kagemiku.com/blog/archives/193
http://webtech-walker.com/archive/2014/06/peco-ghq-gh-open.html
http://qiita.com/hnakamur/items/1c27cf0df19fe57ec624
https://github.com/hnakamur/ansible-role-atom-packages
http://t-wada.hatenablog.jp/entry/mac-provisioning-by-ansible
http://unknownplace.org/archives/gh-open.html
http://qiita.com/issobero/items/e0443b79da117ed48294
http://qiita.com/limonene/items/a10c2755dd2784357c43
http://qiita.com/oct_itmt/items/2d066801a7464a676994
https://github.com/kagemiku/dotfiles


## memo
curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.19.1/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/