DELAEMON BLOG

Live as if you were to die tomorrow. Learn as if you were to live forever.

Hugo SSL/TLS(Let's Encrypt) Renew

Systemdで動かしている Hugo サイトのSSL/TLS(Let's Encrypt)を更新した。

更新しようとしたらエラー

$ ./certbot-auto renew
Requesting root privileges to run certbot...
  /home/dela/.local/share/letsencrypt/bin/letsencrypt renew

-------------------------------------------------------------------------------
Processing <domain>.conf
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
The program hugo (process ID 786) is already listening on TCP port 80. This will
prevent us from binding to that port. Please stop the hugo program temporarily
and then try again. For automated renewal, you may want to use a script that
stops and starts your webserver. You can find an example at
https://letsencrypt.org/howitworks/#writing-your-own-renewal-script.
Alternatively you can use the webroot plugin to renew without needing to stop
and start your webserver.
-------------------------------------------------------------------------------
2016-09-28 22:22:53,632:WARNING:certbot.renewal:Attempting to renew cert from <domain>.conf produced an unexpected error: At least one of the (possibly) required ports is already taken.. Skipping.

All renewal attempts failed. The following certs could not be renewed:
 <domain>/fullchain.pem (failure)
1 renew failure(s), 0 parse failure(s)

80portを使ってるサービスを停止

$ sudo systemctl stop hugo

再度、更新しようとしたらエラー

$ ./certbot-auto renew
Requesting root privileges to run certbot...
  /home/dela/.local/share/letsencrypt/bin/letsencrypt renew

-------------------------------------------------------------------------------
Processing <domain>.conf
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
The program hugo-https (process ID 14806) is already listening on TCP port 443.
This will prevent us from binding to that port. Please stop the hugo-https
program temporarily and then try again. For automated renewal, you may want to
use a script that stops and starts your webserver. You can find an example at
https://letsencrypt.org/howitworks/#writing-your-own-renewal-script.
Alternatively you can use the webroot plugin to renew without needing to stop
and start your webserver.
-------------------------------------------------------------------------------
2016-09-28 22:26:37,520:WARNING:certbot.renewal:Attempting to renew cert from <domain>.conf produced an unexpected error: At least one of the (possibly) required ports is already taken.. Skipping.

All renewal attempts failed. The following certs could not be renewed:
  <domain>/fullchain.pem (failure)
1 renew failure(s), 0 parse failure(s)

443portを使ってるサービスを停止

$ sudo systemctl stop hugos

再々更新

$ ./certbot-auto renew
Requesting root privileges to run certbot...
  /home/dela/.local/share/letsencrypt/bin/letsencrypt renew

-------------------------------------------------------------------------------
Processing <domain>.conf
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
new certificate deployed without reload, fullchain is
<domain>/fullchain.pem
-------------------------------------------------------------------------------

Congratulations, all renewals succeeded. The following certs have been renewed:
  <domain>/fullchain.pem (success)

停止したサービスを起動

$ sudo systemctl start hugos
$ sudo systemctl start hugo

簡単だけど、サービス無停止で更新する方法を考える

AWS Certified Solution Architect Associate 対策

1軍サービス

制限、実践的な構成、トラブルシーティングなど詳細に把握しておく必要あり

  • VPC (Multi-AZ, Internet Gateway, Route Table, NetworkACL, Security Group)
  • IAM
  • EC2 (+ Auto Scaling)
  • EBS
  • S3
  • CloudFront
  • Route53

2軍サービス

サービス概要・基本的な使い方まで把握しておけばOK

  • CloudFormation
  • RDS
  • ElastiCache
  • DynamoDB
  • DirectConnect
  • Trusted Advisor
  • CloudTrail
  • CloudWatch
  • SNS
  • SQS
  • OpsWorks
  • KMS / STS

※1軍7割、2軍3割くらいの力配分

役立つドキュメント

※1軍2軍共にブラックベルトの資料を読むよりは、マニュアルを読む方が役立つ印象

その他

  • AWS用語覚える
  • 模擬試験・サンプル問題などで問題文の文章になれる

※ 最初の感覚を掴むためにEC2+RDS+ElasCticache+S3+VPC+EIPでWebサイトを立てて、AWS ConsoleからCloudwatchLogs, TrustedAdvisorみたり、IAM Role, SecurityGroupいじるくらいやっておくと良さそう

http/2@Hugo & Let's Encrypt

Let's Encryptで発行した証明書を使って、GolangのHugoで動いてるWebサイトをhttp/2にする

環境

OS: Fedora22
Go: 1.6.2

準備

firewalldのhttps/443番ポートを有効にしておく

$ sudo firewall-cmd --list-all
$ sudo firewall-cmd --permanent --add-service=https
$ sudo firewall-cmd --permanent --add-port=443/tcp
$ sudo systemctl restart firewalld
$ sudo firewall-cmd --list-all

80番 ポートで動いてるサービスは止めておく
Certbotクライアントが疎通確認をする場合に既に80番ポートが使われていると、メッセージが表示されて、先に進めない

SSL/TLS証明書発行

ここ読めば簡単
Let's Encrypt の使い方 - Let's Encrypt 総合ポータル

Let's Encryptで証明書を発行するツールのcertbotをダウンロード

$ git clone https://github.com/certbot/certbot

Let's Encryptで証明証を発行する

./certbot-auto certonly --standalone -d <domain>

Certbotクライアントの問いに答えてく。連絡先のメアドとか、同意しますかとか

生成されたファイルの保存場所と期限。期限は3ヶ月と短い

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/<domain>/fullchain.pem. Your cert
   will expire on <有効期限>.

でも更新はこれだけで良いので簡単。毎日更新すると怒られる

$ ./certbot-auto renew

Hugo

自分のレポジトリにcloneしてコード変更

commands/server.go

-       err = http.ListenAndServe(endpoint, nil)
+       err = http.ListenAndServeTLS(endpoint, "/etc/letsencrypt/live/<domain>/fullchain.pem", "/etc/letsencrypt/live/<domain>/privkey.pem

main.go

-       "github.com/spf13/hugo/commands"
+       "github.com/delaemon/hugo-https/commands"

go build して $GOPATH/bin/ に hugo-httpsとして置いておく

Systemd

新しいサービスとして設定ファイルを追加
web socket使ってるところをwssにするのは面倒だったのでExecStart の hugo-https serverコマンドに

    • disableLiveReloadを付けた

付けえない場合は以下のエラーがブラウザで吐かれていた

livereload.js:1 Mixed Content: The page at 'https://<domain>' was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint 'ws://<domain>:443/livereload'. This request has been blocked; this endpoint must be available over WSS.n.Connector.e.connect @ livereload.js:1
livereload.js:1 Uncaught SecurityError: Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS.

Go

当初サーバーにインストールされてたGoはdnf installしたgo1.5.3 linux/amd64だったが、
これではhttps通信になっても、http/2にはならなかった。1.6.2をダウンロードして配置したら、http/2になった

curl HTTP/2@Mac

HTTP/2用サーバーを立てるなら、debug用途でcurlでもHTTP/2を使いたい。

環境: OS X EI Capitan 10.11.3

普通にやると

$ curl -I google.com
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.co.jp/?gfe_rd=cr&ei=mshtV9S9BvLC8geyqZ7QAg
Content-Length: 261
Date: Fri, 24 Jun 2016 23:56:10 GMT

http2オプションつければ・・・

$curl -I --http2 google.com
curl: (1) Unsupported protocol

バージョン確認すると対応してるプロトコルや機能が見える

$ curl -V
curl 7.43.0 (x86_64-apple-darwin15.0) libcurl/7.43.0 SecureTransport zlib/1.2.5
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz UnixSockets

ビルドオプションにnghttp2を指定して、curlを再インストール

$ brew reinstall curl --  --with-nghttp2
$ brew link curl --force
$ hash -r

FeaturesにHTTP2が追加されてる

$ curl -V 
curl 7.48.0 (x86_64-apple-darwin15.3.0) libcurl/7.48.0 OpenSSL/1.0.2h zlib/1.2.5 nghttp2/1.10.0
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets

HTTP/2が使われる

$ curl -I --http2 https://google.com
HTTP/2.0 302
cache-control:private
content-type:text/html; charset=UTF-8
location:https://www.google.co.jp/?gfe_rd=cr&ei=ncxtV9W1GbDK8geS5JegAg
content-length:262
date:Sat, 25 Jun 2016 00:13:17 GMT
alternate-protocol:443:quic
alt-svc:quic=":443"; ma=2592000; v="34,33,32,31,30,29,28,27,26,25"

https://じゃないとHTTP/2は使われない

$ curl -I --http2 google.com
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.co.jp/?gfe_rd=cr&ei=ksxtV6X0MKfK8geE5LWgBA
Content-Length: 261
Date: Sat, 25 Jun 2016 00:13:06 GMT

Mac OSX EI Capitan Wifi Commands

Mac Bookのネットワーク接続(Wifi/Proxy)をターミナルからコマンドから操作したい。

OS version

OS X EI Capitan

Wifiの操作
# On
$ networksetup -setairportpower en0 on

# Off
$ networksetup -setairportpower en0 off

# Info
$ networksetup -getairportnetwork en0

# SSIDの一覧表示
$ sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s

# 任意のネットワークに接続
$ sudo networksetup -setairportnetwork en0 <ssid> <password>
Proxyの操作
# On
$ sudo networksetup -setwebproxystate Wi-Fi on; sudo networksetup -setsecurewebproxystate Wi-Fi on

# Off
$ sudo networksetup -setwebproxystate Wi-Fi off; sudo networksetup -setsecurewebproxystate Wi-Fi off

# Info
$ networksetup -getwebproxy Wi-Fi; networksetup -getsecurewebproxy Wi-Fi
エイリアスに設定
alias wifi-on='networksetup -setairportpower en0 on'
alias wifi-off='networksetup -setairportpower en0 off'
alias wifi-info='networksetup -getairportnetwork en0'
alias proxy-on='sudo networksetup -setwebproxystate Wi-Fi on; sudo networksetup -setsecurewebproxystate Wi-Fi on'
alias proxy-off='sudo networksetup -setwebproxystate Wi-Fi off; sudo networksetup -setsecurewebproxystate Wi-Fi off'
alias proxy-info='networksetup -getwebproxy Wi-Fi; networksetup -getsecurewebproxy Wi-Fi'
alias airport='sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport'
実際に打ってるコマンド
$ proxy-on && wifi-on
$ proxy-off && wifi-off
$ proxy-info && wifi-info

LLVM build on Fedora23(VirtualBox)

VirtualBox上のFedora23でLLVMをビルドした。

インストール手順

1. 必要パッケージをインストール

$ sudo dnf install make gcc-c++ texinfo m4 autoconf automake libtool graphviz python-xdot

2. ソースコードをダウンロード・解凍・配置

$ mkdir llvm
$ cd llvm/
$ wget http://llvm.org/releases/3.8.0/llvm-3.8.0.src.tar.xz
$ wget http://llvm.org/releases/3.8.0/cfe-3.8.0.src.tar.xz
$ wget http://llvm.org/releases/3.8.0/compiler-rt-3.8.0.src.tar.xz

xz -dv llvm-3.8.0.src.tar.xz
tar -xvf llvm-3.8.0.src.tar
xz -dv cfe-3.8.0.src.tar.xz
tar xvf cfe-3.8.0.src.tar
mv cfe-3.8.0.src llvm-3.8.0.src/tools/clang
xz -dv compiler-rt-3.8.0.src.tar.xz
tar xvf compiler-rt-3.8.0.src.tar
mv compiler-rt-3.8.0.src llvm-3.8.0.src/projects/compiler-rt

3. ディレクトリを作成して、ビルド

$ mkdir llvm-build
$ cd llvm-build/
$ source ~/.bashrc
../llvm-3.8.0.src/configure --prefix=/usr/local/llvm --enable-optimized
$ make -j2
$ make check
$ sudo make install

かなり時間かかった。度々落ちるけど、メモリ不足だったので、make を繰り返せば終わる

基本操作
$ llvm
ll                llvm-as           llvm-diff         llvm-extract      llvm-nm           llvm-readobj      llvm-symbolizer
llc               llvm-bcanalyzer   llvm-dis          llvm-link         llvm-objdump      llvm-rtdyld       llvm-tblgen
lli               llvm-config       llvm-dsymutil     llvm-lto          llvm-pdbdump      llvm-size
lli-child-target  llvm-cov          llvm-dwarfdump    llvm-mc           llvm-profdata     llvm-split
llvm-ar           llvm-cxxdump      llvm-dwp          llvm-mcmarkup     llvm-ranlib       llvm-stress
$ clang -o hoge.o hoge.c
$ ./hoge.o
hoge
$ clang -emit-llvm -c -o hoge.bc hoge.c #LLVM ビットコードを出力
$ lli -force-interpreter hoge.bc #インタプリタで実行する
$ clang -emit-llvm -S -o hoge.ll hoge.c #LLVM アセンブリを出力
$ llvm-as -o hoge.bc hoge.ll #LLVM アセンブリをLLVM ビットコードへ変換
$ lli hoge.bc #実行
$ llc -o hoge.s hoge.bc #LLVM アセンブリを経由してアセンブリ出力
$ llc -filetype=obj -o hoge.o hoge.ll #オブジェクト出力
$ opt -O3 -o hoge.bc hoge.ll #最適化
$ lli hoge.bc #実行
$ llvm-objdump -d hoge.o #ディスアセンブル

hoge.o:	file format ELF64-x86-64

Disassembly of section .text:
main:
       0:	55 	pushq	%rbp
       1:	48 89 e5 	movq	%rsp, %rbp
       4:	bf 00 00 00 00 	movl	$0, %edi
       9:	31 c0 	xorl	%eax, %eax
       b:	e8 00 00 00 00 	callq	0 <main+10>
      10:	31 c0 	xorl	%eax, %eax
      12:	5d 	popq	%rbp

Clojure Leiningen

Clojureことはじめ

"Leiningen is the easiest way to use Clojure. With a focus on project automation and declarative configuration, it gets out of your way and lets you focus on your code."
Leiningen

leiningen をインストール

$ brew install leiningen

プロジェクトを作成

$ lein new hello

ディレクトリ構成は以下

$ tree
.
├── CHANGELOG.md
├── LICENSE
├── README.md
├── dev-resources
├── doc
│   └── intro.md
├── project.clj
├── resources
├── src
│   └── hello
│       └── core.clj
├── target
│   ├── classes
│   │   └── META-INF
│   │       └── maven
│   │           └── hello
│   │               └── hello
│   │                   └── pom.properties
│   └── stale
│       └── extract-native.dependencies
└── test
    └── hello
        └── core_test.clj

14 directories, 9 files

project.cljを書き換え

cat project.clj
(defproject hello "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.7.0"]]
  :main hello.core)


lein repl で 実行。lein --help すると "Start a repl session either with the current project or standalone."

$ lein repl
nREPL server started on port 52522 on host 127.0.0.1 - nrepl://127.0.0.1:52522
REPL-y 0.3.7, nREPL 0.2.10
Clojure 1.7.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_65-b17
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
    Exit: Control+D or (exit) or (quit)
 Results: Stored in vars *1, *2, *3, an exception in *e

hello.core=> fo #Tabで候補が表示される
foo      for      force    format
hello.core=> (foo "delaemon")
delaemon Hello, World!
nil