うちのいぬ Tech Blog

Tech Blog of Uchinoinu/My dog

gulp-slim でgulpからslimに渡す変数の1文字目は小文字でないとエラーになります

qiita.com

gulp-slim(0.3.0)では、以下の様にgulpからslimに変数を渡せます。(dataのところです)

gulp.src("./src/slim/*.slim")
  .pipe(slim({
    pretty: true,
    data: {
      Title: "Just a list of usernames",
      Users: [
        { name: "Fred" },
        { name: "Bill" },
        { name: "Harry" }
      ]
    },
    options: "encoding='utf-8'"
  }))
  .pipe(gulp.dest("./dist/html/"));
});

ここのkeyの1文字目を大文字にすると以下の様なエラーがでました。

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: no writecb in Transform class
    at afterTransform 
    at TransformState.afterTransform 
    at Socket.<anonymous> (
    at emitNone (events.js:91:20)
    at Socket.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
error Command failed with exit code 1.

1文字目は小文字にしておけば、エラーがでませんでした。

NodeやGulpでhost: "0.0.0.0" を指定してサーバーを立ち上げたとき、 listen EADDRINUSE というエラーが出た場合の対応

qiita.com

環境

状況

  • GulpやNodeでサーバーを立ち上げますが、同じローカルネットワーク内の別端末(スマホとか)からもデバッグのために閲覧したいのでgulpflieを以下の様にしました。
gulp.task("webserver.dev", () => {
    gulp.src("hoge")
        .pipe(webserver({
            host: "0.0.0.0",
            livereload: true,
            port: 8080
        }));
});

通常これでの目的は適う様なのですが、今回は以下のエラーがでました。

listen EADDRINUSE 0.0.0.0:8080
listen EADDRINUSE 0.0.0.0:35729

悲しいことに livereloadの方も でした

原因

のエラーはこのIP, Portは使われていますよということですので、実際に確認します。

netstat

コマンドなど普段の開発フローでサーバーが立ち上がっていないことを確認します。 netstat -an をします。

するとtcp4でみたくないものを発見しました

tcp4       0      0  *.8080                 *.*                    LISTEN
tcp4       0      0  *.35729                *.*                    LISTEN

ポートの8080と35729がLISTEN状態のままでした。

おそらく、立ち上げたサーバーが、開発中の何らかの理由で、ちゃんと終了されなかったということにあると思います。

対応

この問題の解決方法はいくつかあると思います。

    1. 別のPortを振ってあげる(暫定的な対応)
    1. このIP/Portを使っているプロセスをkillして、開放する

1をやっても、同じ問題が繰り返されるだけの可能性もあるので、今回は2の方法をとります。

上のPIDを探る

PIDが分かればkillすることが出来ます。(おそらく) なので、以下のコマンドをとります。

$ lsof -n -P -i :8080

するとPIDが表示されると思います。

追記

netstatでもPIDを表示できるようです。

root権限があるのであればnetstatコマンドでも-pオプションでPIDとプログラム名が表示できます。

PIDをkillする

kill {PID}

してあげればOKです。

参考までに

nodeが犯人であることが多い様です

nodeがポートを保持し続けてしまうことが原因であることが多いようです。 なので

ps aux | grep node | grep -v grep

とかしてあげると、nodeのプロセスが出てきます。 動かしているはずがないのに、生きているプロセスがあったらそいつを殺してあげるといいです。

electron系のアプリはnodeをバンバンつかっています

なお、slackアプリなどを使っている場合は、nodeのプロセスがたくさん立ち上がっていると思いますので、 killall node はしないほうがいいと思います。

私の場合には Nginx が犯人でした

nginxでリバースプロキシをmac内に立ち上げていたのですが、そいつが悪さををしていました。nginxが正しく動いていなかったことが問題だったようです。 これも netstatps で探っていったらなんとかなりました。

References

laravel/homestead を利用する際に、Vagrant の box のバージョン遡って導入した手順の例

前置き

LaravelをMacに入れようとして、PHPのバージョンやMySQLを調整していたら、バグがひどくなってきて既存の開発環境を壊しかねなかったので、Vagrantを使って、環境を分けることにしました。

laravel/homestead

laravelにはlaravel/homesteadという便利なboxが既にあって、公式で取り上げられているのでこれをつかいます

問題

laravel/homesteadの最新はv0.6.0で、PHPは7を使っています。詳しい情報をたどるのは結構手間ですが、この辺から、バージョンを遡ってみたりしいきました。

PHPは5.6を使いたいので、v0.3.3を使う必要があります。

$ vagrant box add laravel/homestead --box-version 0.3.3 # とにかく時間がかかるのでひたすら待ちます
==> box: Loading metadata for box 'laravel/homestead'
    box: URL: https://atlas.hashicorp.com/laravel/homestead
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) virtualbox
2) vmware_desktop

Enter your choice: 1
==> box: Adding box 'laravel/homestead' (v0.3.3) for provider: virtualbox
    box: Downloading: https://atlas.hashicorp.com/laravel/boxes/homestead/versions/0.3.3/providers/virtualbox.box
==> box: Successfully added box 'laravel/homestead' (v0.3.3) for 'virtualbox'!

$ vagrant box list | grep homestead
laravel/homestead  (virtualbox, 0.3.3)

Install

Download

githubからcloneするなら

$ git clone https://github.com/laravel/homestead.git Homestead

もしくは以下からダウンロード

https://github.com/laravel/homestead/releases

Create Homestead.yaml

$ bash init.sh

~/.homestead 以下にできます

Vagrant立ち上げ

Homestead.yamlをいい感じに設定

ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: ~/Homestead
      to: /home/vagrant/Code
      type: "nfs"

sites:
    - map: homestead.app
      to: /home/vagrant/Code/Laravel/public

databases:
    - homestead

variables:
    - key: APP_ENV
      value: local

# blackfire:
#     - id: foo
#       token: bar
#       client-id: foo
#       client-token: bar

# ports:
#     - send: 93000
#       to: 9300
#     - send: 7777
#       to: 777
#       protocol: udp

homestead.yamlにbox versionを記述

provider: virtualbox
  version: "0.3.3"

/etc/hosts

192.168.10.10  homestead.app

vagrant up

$ vagrant up

アクセス

http://homestead.app

HomebrewでインストールしたMySQLを自動起動設定する

之だけで済むようです

$ brew services start mysql

==> Tapping homebrew/services
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 10 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (10/10), done.
Checking connectivity... done.
Tapped 0 formulae (26 files, 32.4K)
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)

OSX El Capitan に MySQL をインストールして、ちょっと使ってみる

Install

$ brew install mysql

==> Downloading https://homebrew.bintray.com/bottles/mysql-5.7.16.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mysql-5.7.16.el_capitan.bottle.tar.gz
==> Using the sandbox
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

To connect run:
    mysql -uroot

A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

To start mysql:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Summary
  /usr/local/Cellar/mysql/5.7.16: 13,511 files, 439M
  • 確認
mysql: stable 5.7.16 (bottled)
Open source relational database management system
https://dev.mysql.com/doc/refman/5.7/en/
Conflicts with: mariadb, mariadb-connector-c, mysql-cluster, mysql-connector-c, percona-server
/usr/local/Cellar/mysql/5.7.16 (13,511 files, 439M) *
  Poured from bottle on 2016-11-25 at 21:15:10
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/mysql.rb
==> Dependencies
Build: cmake ✘
Required: openssl 
==> Requirements
Required: macOS >= 10.7 
==> Options
--with-archive-storage-engine
    Compile with the ARCHIVE storage engine enabled
--with-blackhole-storage-engine
    Compile with the BLACKHOLE storage engine enabled
--with-debug
    Build with debug support
--with-embedded
    Build the embedded server
--with-local-infile
    Build with local infile loading support
--with-test
    Build with unit tests
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

To connect run:
    mysql -uroot

A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

To start mysql:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start

Usage

Start MySQL Server

$ mysql.server start
Starting MySQL
.. SUCCESS! 

Entry to MySQL

$ mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.16 Homebrew

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

[(none)] mysql> exit
Bye

Stop MySQL Server

$ mysql.server stop
Shutting down MySQL
.. SUCCESS! 

Make Security Powerful

mysql_secure_installation
mysql_secure_installation: [ERROR] unknown option '--show-warnings'
mysql_secure_installation: [ERROR] unknown variable 'prompt=[\d] mysql> '

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

---

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: y

---

Please set the password for root here.

New password: 

Re-enter new password: 

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

---

By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

---

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

---

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

---

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

Create Database

[(none)] mysql> show databases;
+-----------------------------------+
| Database                          |
+-----------------------------------+
| information_schema                |
| mysql                             |
| performance_schema                |
+-----------------------------------+
3 rows in set (0.02 sec)

[(none)] mysql> create database foobar;
Query OK, 1 row affected (0.00 sec)

[(none)] mysql> show databases;
+-----------------------------------+
| Database                          |
+-----------------------------------+
| information_schema                |
| foobar                            |
| mysql                             |
| performance_schema                |
+-----------------------------------+
4 rows in set (0.02 sec)

Create User with privilege

[(none)] mysql> create user
    -> 'retriever'@'localhost' identified by 'ぱすわあど';

[(none)] mysql> select User,Host from mysql.user;
+-----------------+-----------+
| User            | Host      |
+-----------------+-----------+
| root            | 127.0.0.1 |
| root            | ::1       |
| retriever       | localhost |
| root            | localhost |
+-----------------+-----------+
4 rows in set (0.00 sec)

[(none)] mysql> grant all privileges on `foobar`.* TO 'retriever'@'localhost';
[(none)] mysql> FLUSH PRIVILEGES;

[(none)] mysql> show grants for 'retriever'@'localhost';
+-----------------------------------------------------------------------+
| Grants for retriever@localhost                                        |
+-----------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'retriever'@'localhost'                         |
| GRANT ALL PRIVILEGES ON `foobar`.* TO 'retriever'@'localhost' |
+-----------------------------------------------------------------------+
2 rows in set (0.00 sec)

自動起動設定

http://qiita.com/satomyumi/items/43c83658a3e5bcc820ec

PHPを5.5系から5.6系へバージョンアップする

前置き

Laravel5.2を使うに当たって、ElCapitanデフォルトのPHPのバージョンでは古いらしく、PHPを5.6系にバージョンアップします。(7系じゃないです)

手順

Homebrewのアップデート

$ brew tap homebrew/versions
$ brew tap homebrew/dupes
$ brew tap homebrew/php
$ brew update

PHP 5.6のインストール

$ brew install homebrew/php/php56-amqp

色々とインストールされます

TerminalをRestart

$ php -v
PHP 5.6.27 (cli) (built: Oct 15 2016 09:29:55) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

Composer本体をインストールする方法の1つ

前置き

Laravel 5.2を使うことになり、LaravelはComposerを使ってインストールするようなので、Composerをインストールしてみた。

環境

Composerとは

  • PHPのパッケージ管理システム。依存性を含めて管理できるので、とても楽。
  • RubyにおけるBundlerやJavaScriptにおけるnpmと同様のもの

参考

手順

composerをインストールして、通常パスが通ってるディレクトリに移動させます

$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer

疎通確認

$ composer                                                                                                           
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.2.2 2016-11-03 17:43:15

Composerのアップデート

$ composer self-update