Trvis CI

どうやら失敗したようです。

0.55s$ git clone --depth=50 --branch=chat https://github.com/githubix/bengoshi_chat.git githubix/bengoshi_chat
Cloning into 'githubix/bengoshi_chat'...
remote: Counting objects: 50, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 50 (delta 0), reused 2 (delta 0), pack-reused 47
Unpacking objects: 100% (50/50), done.
$ cd githubix/bengoshi_chat
$ git checkout -qf db05fb4b37219f42856c6148d96e13a6fbde87a5
Disabling Gradle daemon
$ mkdir -p ~/.gradle && echo "org.gradle.daemon=false" >> ~/.gradle/gradle.properties
rvm
4.10s$ rvm use default
Using /home/travis/.rvm/gems/ruby-2.4.1
** Updating RubyGems to the latest version for security reasons. **
** If you need an older version, you can downgrade with 'gem update --system OLD_VERSION'. **
ruby.versions
$ ruby --version
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
$ rvm --version
rvm 1.29.3 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
$ bundle --version
Bundler version 1.16.2
$ gem --version
2.7.7
No Gemfile found, skipping bundle install
0.15s$ rake
rake aborted!
No Rakefile found (looking for: rakefile, Rakefile, rakefile.rb, Rakefile.rb)
/home/travis/.rvm/gems/ruby-2.4.1@global/gems/rake-12.3.0/exe/rake:27:in `'
(See full trace by running task with --trace)
The command "rake" exited with 1.
Done. Your build exited with 1.

.travis.ymlがnullだったからでしょうか。

yamlを書きます。

language: php

php:
  - 5.6
  - 5.7
  - hhvm

before_script:
  - curl -s http://getcomposer.org/installer | php
  - php composer.phar install --dev --prefer-source

script:
  - mkdir -p build/logs
  - phpunit --coverage-clover build/logs/clover.xml --configuration tests/phpunit.xml tests

after_script:
  - php vendor/bin/coveralls -v

notifications:
  slack:
    rooms:
      secure: your_token_key

TravisCI 画面

なるほど、こういうことか。テスト内容をyamlに書いて、rubyが実行するのね。それをslackなどに送付することも可能。

CloudFormation

what?

AWS CloudFormation gives developers and systems administrators an easy way to create and manage a collection of related AWS resources, provisioning and updating them in an orderly and predictable fashion.

You can use AWS CloudFormation’s sample templates or create your own templates to describe the AWS resources, and any associated dependencies or runtime parameters, required to run your application. You don’t need to figure out the order for provisioning AWS services or the subtleties of making those dependencies work. CloudFormation takes care of this for you. After the AWS resources are deployed, you can modify and update them in a controlled and predictable way, in effect applying version control to your AWS infrastructure the same way you do with your software.

VPSにMackerelをいれる

Mackerelのyumリポジトリを登録
[root@hoge ~]# curl -fsSL https://mackerel.io/file/script/setup-yum.sh | sh
This script requires superuser authority to configure Mackerel yum repository:
+ command -v curl
/usr/bin/curl
+ http_get=’curl -LfsS’
+ ‘[‘ ‘curl -LfsS’ = ” ‘]’
++ mktemp
+ gpgkey_path=/tmp/tmp.KrBsOelQWe
+ curl -LfsS https://mackerel.io/file/cert/GPG-KEY-mackerel
+ rpm –import /tmp/tmp.KrBsOelQWe
+ rm /tmp/tmp.KrBsOelQWe
+ cat
done
To install mackerel-agent type: sudo yum install mackerel-agent

その後、yumコマンドを用いてインストール
# sudo yum install -y mackerel-agent
Loaded plugins: fastestmirror, security
Setting up Install Process
Determining fastest mirrors
epel/metalink | 8.7 kB 00:00
* base: ftp.iij.ad.jp
* epel: mirror.dmmlabs.jp
* extras: ftp.iij.ad.jp
* remi-safe: ftp.riken.jp
* updates: ftp.iij.ad.jp
base | 3.7 kB 00:00
base/primary_db | 4.7 MB 00:00
epel | 3.2 kB 00:00
epel/primary | 3.2 MB 00:00
epel 12521/12521
extras | 3.4 kB 00:00
extras/primary_db | 25 kB 00:00
groonga | 2.9 kB 00:00
groonga/primary_db | 604 kB 00:00
mackerel | 2.5 kB 00:00
mackerel/primary_db | 5.8 kB 00:00
mongodb-org-3.4 | 2.5 kB 00:00
mongodb-org-3.4/primary_db | 73 kB 00:00
remi-safe | 2.9 kB 00:00
remi-safe/primary_db | 1.0 MB 00:00
updates | 3.4 kB 00:00
updates/primary_db | 1.2 MB 00:00
Resolving Dependencies
There are unfinished transactions remaining. You might consider running yum-complete-transaction first to finish them.
–> Running transaction check
—> Package mackerel-agent.noarch 0:0.56.1-1 will be installed
–> Finished Dependency Resolution

Dependencies Resolved

================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
mackerel-agent noarch 0.56.1-1 mackerel 1.9 M

Transaction Summary
================================================================================
Install 1 Package(s)

Total download size: 1.9 M
Installed size: 4.6 M
Downloading Packages:
mackerel-agent-0.56.1-1.noarch.rpm | 1.9 MB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : mackerel-agent-0.56.1-1.noarch 1/1
Verifying : mackerel-agent-0.56.1-1.noarch 1/1

Installed:
mackerel-agent.noarch 0:0.56.1-1

Complete!

[root@hoge ~]# sudo mackerel-agent init -apikey=””
You have mail in /var/spool/mail/root
[root@hoge ~]# sudo /etc/init.d/mackerel-agent start
Starting mackerel-agent: [ OK ]

すげ、きた

これすげ

Mackerel(マカレル)

はてなが開発した新世代のサーバ管理・監視サービス。
仮想サーバなどクラウドサービスをMackerelで統合管理および監視ができる。

-> box listを表示
MyCentOS>vagrant box list
bento/centos-6.7 (virtualbox, 2.2.7)
bento/centos-6.8 (virtualbox, 2.3.0)
trusty64 (virtualbox, 0)
ubuntu/trusty64 (virtualbox, 20161207.0.0)

Mackerelは入っていない。当然か。

これ打っていいのか。

vagrant box repackage mackerel/centos66 virtualbox 0.0.2

ちゃうちゃう、hatenaのサイトから無料トライアル

apacheとnginxの比較

まず、nginx
[vagrant@localhost local]$ nginx -v
nginx version: nginx/1.14.0

あれ、入ってますね。
起動してみます。
[vagrant@localhost local]$ sudo service nginx start
nginx を起動中: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[失敗]
あ、ポート80はapacheが使ってるからか。

比較
Apacheは、プロセス駆動アーキテクチャでマルチプロセス。これは、各リクエストをプロセスに割り当てて処理を行う。リクエストが大量に来た際、プロセスが同時に起動するのでオーバーヘッドが非常に大きくなるというデメリットがある。

nginxは、イベント駆動アーキテクチャ、シングルスレッドモデル。 シングルスレッドでループ処理をまわし、キューに溜まったイベントを処理していく処理方式(イベントループ方式)(node.jsなどでも採用)。プロセス数はCPUコア数と基本的には同じに設定。

=>リクエストが多い場合は、単純にマルチプロセルの方が速そうだが。
>nginxの方が、処理が軽く、大量のリクエストを処理するのに向いている。
なに?なぜだ?

>nginxは、CPUリソースがたくさん必要な処理には向いていない。処理時間が長くなる処理を実行した際、そこでプロセスがブロックされてしまい処理能力が落ちてしまう。
これはシングルだから、納得できますね。

>NGINXは大量処理、スピード重視を徹底的に追求
同時アクセスはnginxの方が良いと書かれている。
う~ん、イマイチ納得いかない。

コマンドを変えてみるが、
[vagrant@localhost local]$ sudo nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

あかん

MySQLにHTMLタグを保存する

とりあえずdatabaseから作ります。

mysql> create database if not exists send_mail;
Query OK, 1 row affected (0.30 sec)

続いて、table

mysql> create table send_mail.html(
    ->  'id' int AUTO_INCREMENT,
    ->  'html' MEDIUMTEXT,
    -> );
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''id' int AUTO_INCREMENT,
 'html' MEDIUMTEXT,
)' at line 2

あれ、シングルクオテーションは必要ないか。。

sql文を編集します。

create table send_mail.html(
 id int AUTO_INCREMENT NOT NULL PRIMARY KEY,
 html MEDIUMTEXT
);

OK! insertしていきましょう♪

mysql> INSERT INTO send_mail.html (html)
    ->   VALUES ('タイトルが入ります');
Query OK, 1 row affected (0.46 sec)

mysql> select * from html;
+----+--------------------------------------------+
| id | html                                       |
+----+--------------------------------------------+
|  1 | タイトルが入ります          |
+----+--------------------------------------------+
1 row in set (0.12 sec)

うお!きたきたきたきた!

titleタグ以外のタグを入れてみます。

mysql> INSERT INTO send_mail.html (html)
    ->   VALUES ('');
Query OK, 1 row affected (0.53 sec)

mysql> select * from html;
+----+-----------------------------------------------+
| id | html                                          |
+----+-----------------------------------------------+
|  1 | タイトルが入ります             |
|  2 |  |
+----+-----------------------------------------------+
2 rows in set (0.09 sec)

おうおう。OKだ。

pdoでmysqlからfetchする。

<?php

try {
$pdo = new PDO('mysql:host=localhost;dbname=send_mail;charset=utf8','root','',
	array(PDO::ATTR_EMULATE_PREPARES => false));
} catch (PDOException $e) {
 exit('データベース接続失敗。'.$e->getMessage());
}
$stmt = $pdo->query("SELECT * FROM html where id = 2");
while($row = $stmt -> fetch(PDO::FETCH_ASSOC)){
	$img = $row["html"];
	echo $img;
}

おいおいおい!!!!
それでは、ひっぱてきたhtmlタグをmb_send_mailで送信したいと思います。

<?php

try {
$pdo = new PDO('mysql:host=localhost;dbname=send_mail;charset=utf8','root','',
	array(PDO::ATTR_EMULATE_PREPARES => false));
} catch (PDOException $e) {
 exit('データベース接続失敗。'.$e->getMessage());
}
$stmt = $pdo->query("SELECT * FROM html where id = 2");
while($row = $stmt -> fetch(PDO::FETCH_ASSOC)){
	$img = $row["html"];
}

mb_language('Japanese');
mb_internal_encoding("UTF-8");
$header_info="From: admin@example.com"."\nContent-Type: text/html;charset=ISO-2022-JP\nX-Mailer: PHP/".phpversion();

$insert = "差し込み文章";

$body = <<< EOM
<!DOCTYPE>
<html lang="ja">
<head>
</head>
<body>
Look!<br>
<strong style="color:red;">apple</strong><br>
{$img}<br>
{$insert}が入ります。
</body>
</html>
EOM;
print $body;
mb_send_mail("hogehoge@hoge.com",'test',$body,$header_info);
?>

おおおおおおお、簡単だが、おおよその仕組みはわかった。

次は実際に届いてるHTMLメールのソースコードを見てみよう。