くりにっき

フルスタックキュアエンジニアです

CircleCI 2.1 previewのcommandsが便利だった

https://github.com/CircleCI-Public/config-preview-sdk を見てたら commands が便利そうだったのでためしに個人プロダクトに入れてみた。

github.com

準備

Advanced Settingsの「Enable build processing (preview)」で有効にしないと使えないので注意

f:id:sue445:20180930092753p:plain

Before (CircleCI 2.0)

Ruby製のアプリのCIを構築してると

  • restore_cache でbundle installのキャッシュをリストア
  • bundle install
  • save_cache でキャッシュを保存

というのが頻出すぎてリファクタリングしたかった。

実際に使ってた設定を抜粋したのが下記です。

version: 2

save_bundle_cache_option: &save_bundle_cache_option
  key: v2-bundle-{{ checksum "Gemfile.lock" }}
  paths:
    - ~/app/vendor/bundle

restore_bundle_cache_option: &restore_bundle_cache_option
  keys:
    - v2-bundle-{{ checksum "Gemfile.lock" }}
    - v2-bundle

jobs:
  rspec:
    <<: *default

    steps:
      - checkout
      - restore_cache:
          <<: *restore_bundle_cache_option
      - run: ./.circleci/setup_bundle.sh
      - save_cache:
          <<: *save_bundle_cache_option
      - run: ./.circleci/setup_database.sh

      - run: bundle exec rspec
      - run: bundle exec codeclimate-test-reporter

https://github.com/sue445/chatwork_mention_task/blob/bd6d7ec65d4bc0a8e38d55e082e740539737e538/.circleci/config.yml

rspec以外にrubocopとかの実行でもbundle installが必要なのでDRYにするためにrestore_cachesave_cache のオプションを抽出したり、bundle installの処理をsetup_bundle.sh に抽出しています。

#!/bin/bash -xe

bundle install --jobs=4 --retry=3 --path vendor/bundle
bundle clean

# Resolve bundler version difference between Gemfile.lock and pre-installed in CI
gem install restore_bundled_with --no-document
restore-bundled-with

https://github.com/sue445/chatwork_mention_task/blob/bd6d7ec65d4bc0a8e38d55e082e740539737e538/.circleci/setup_bundle.sh

After (CircleCI 2.1 preview)

restore_cache , bundle install, save_cache の組み合わせを setup_bundle というcommandとして抽出できたので再利用しやすくなりました。

version: 2.1

commands:
  setup_bundle:
    description: "Setup bundle with cache"
    steps:
      - restore_cache:
          keys:
          - v2-bundle-{{ checksum "Gemfile.lock" }}
          - v2-bundle
      - run:
          name: Setup bundle
          command: |
            set -xe
            bundle install --jobs=4 --retry=3 --path vendor/bundle
            bundle clean

            # Resolve bundler version difference between Gemfile.lock and pre-installed in CI
            gem install restore_bundled_with --no-document
            restore-bundled-with

      - save_cache:
          key: v2-bundle-{{ checksum "Gemfile.lock" }}
          paths:
          - ~/app/vendor/bundle

jobs:
  rspec:
    <<: *default

    steps:
      - checkout
      - setup_bundle
      - run: ./.circleci/setup_database.sh

      - run: bundle exec rspec
      - run: bundle exec codeclimate-test-reporter

https://github.com/sue445/chatwork_mention_task/blob/654e5137002bd611f44674d2e69b8ae1b4d31102/.circleci/config.yml

bundle install系の処理が1つになったことでBeforeにあったsave_bundle_cache_optionsetup_bundle.sh にような工夫が不要になったのが嬉しい

所感

今回はcommandsしか使ってないですが他の機能を利用することでリファクタリングの余地が増えそう。

まだpreviewで色々変更ありそうなので個人プロダクト全部には投入しづらいけど、正式版になったら上のような修正を全部に入れたい。

作業PR

https://github.com/sue445/chatwork_mention_task/pull/184/files

apple_system_status v1.0.0を出した

自分以外使ってなさそうだけど一応アナウンス

github.com

このgemについては過去のエントリを参照

sue445.hatenablog.com

大きな変更点

https://github.com/sue445/apple_system_status/blob/master/CHANGELOG.md#v100-2018-09-22 からピックアップ

phantomjsに依存してたのでHeadless Chromeを使うようにした

今更ですがHeadless Chromeに移行。

実は結構前に実装自体は終わっててローカルだとサクッと動いたのですが、CI周りで苦戦してました

簡単に書くと

  • 最初TravisCIでやろうかと思ったらうまく動かない
  • Docker Hubの circleci/ruby のbrowsersタグがついてるイメージならHeadless Chromeがインストール済だから大丈夫だろうと思ったらうまく動かない
    • ChromeにはつながるんだけどDOM取得がうまくいかない
  • 3ヶ月くらい経ってふとnode-browsersタグを使ったらCI通った

って感じ

https://github.com/sue445/apple_system_status/pull/25

activesupport への依存を外した

若気の至りで blank?present? のためだけにactivesupport を使ってたので依存を外した

https://github.com/sue445/apple_system_status/pull/27

Ruby 2.1以下をサポート外にした

最近のcapybara v3.0.0以降だとxpath v3系に依存してるけど *1 xpath v3系はruby 2.2以降でしかインストールできないので*2、メジャーバージョン上げるついでにサポートを明示的に切った

https://github.com/sue445/apple_system_status/pull/28

プリッカソン#5に参加した #prickathon

プリパラを見ていた時期に面白そうなハッカソンがあったので参加しました。*1

prickathon.connpass.com

第一印象

成果物

プリティーオールフレンズ誕生日カレンダー

f:id:sue445:20180924131200p:plain

github.com

下記のプリティーシリーズ版です。

sue445.hatenablog.com

発表資料

esa-pages.io

雑にスライド作るのにesa便利。*2

*1:現時点ではプリティーリズム、キンプリ、プリパラ全て視聴済

*2:Qiitaでもいいんだけど技術以外のことは書きづらい

fog-aws v3.3.0が出た

しばらくv3.0.0で止まっていたのですが、ここ数日で立て続けにv3.1.0, v3.2.0, v3.3.0と出たのでメモ

前提

fog-core v2.1.1のリリースにより Fog::Compute::AWS って書いてると「Fog::AWS::Compute を使え」ってdeprecation warningが出るようになった。

https://github.com/fog/fog-core/blob/master/changelog.md#211-09042018

fog-coreでdeprecation warningを出すようになったのでfogファミリー全部に対応が必要になった。

自分のアプリでも出るようになったんですが、rake叩く度にむっちゃwarning出るようになってノイズになってつらかったので直した。

v3.1.0

https://github.com/fog/fog-aws/compare/v3.0.0...v3.1.0

今年の4月以降リリースされずに溜まってたPRが一気にリリースされた感じ。

deprecation warning対応以外で自分のアプリで影響範囲調べるのならこのdiff見るのがよさそう

v3.2.0

https://github.com/fog/fog-aws/compare/v3.1.0...v3.2.0

私ですo^

コミットログ見ると本当にきれいなsed芸ですね(熱い自画自賛

github.com

github.com

github.com

ちなみにこの辺のPRはOSSパッチ会での成果でした。(PR出せたのは3連休だけど)

blog.agile.esm.co.jp

本当はこのバージョンでdeprecation warningが解決されてるはずだったんだけど、僕のPRより前に出てた https://github.com/fog/fog-aws/pull/467 がマージされ忘れてたのでdeprecation warning対応は不完全。

v3.3.0

https://github.com/fog/fog-aws/compare/v3.2.0...v3.3.0

https://github.com/fog/fog-aws/issues/466#issuecomment-422165748https://github.com/fog/fog-aws/pull/467 のマージし忘れを指摘されてv3.3.0ががリリースされた。

ここでようやくdeprecation warning完全対応。

#技術書典 5で「Packer with mitamae」という本を出します

技術書典5 まで残り1ヶ月切りましたが一通り執筆終わったので告知です

スペース

か75

techbookfest.org

f:id:sue445:20180911081340p:plain:w300

本の内容について

これはPackerのビルドを行う時にmItamaeでレシピを適用し、Serverspecでテストを実行するための本です。

本書のゴールはmitamaeでプロビジョニングしたサーバに対してServerspecでテストを行い、PackerでAMIを作成することです。 AMI作成だけでなくVagrantやCircleCIの設定を整えることでmItamaeを利用した実践的なレシピ開発も視野に入れています。

おそらく日本初のmitamae本じゃないかと思ってます。(要出典)

サンプル

表紙と目次です

f:id:sue445:20180908231114p:plain

f:id:sue445:20180908231123p:plain

f:id:sue445:20180908231129p:plain

目次だけですが何について書いているかはだいたい分かるかと思います。

2018/9/16追記:「はじめに」の章を公開

f:id:sue445:20180916192826p:plain

キーワード

画像だとググラビリティがないので使っている技術に関してざっとキーワードを挙げておきます

  • Itamae
  • mitamae
  • Vagrant
  • Packer
  • Serverspec
  • AWS
  • CircleCI
  • mitamaeとServerspecを用いたインフラTDD

FAQ

Q: 進捗どうですか?

A: 進捗最高です!(もう一通り執筆は終わって、今は知り合いにレビュー依頼している)

Q: 頒布形式は?

A: 当日の頒布物はPDFをDLするためのシリアルコードのみです。

見本用に1〜2部印刷するかもしれないですが、基本は電子版のみです。

Q: 技術書典に行かないと買えないの?

A: 後日 BOOTH で電子版の販売を予定しています。

その他

当日は プリ☆チャン のフォロチケ交換も受け付けていますぷり。

ChromeでTwitterを開くと重すぎてまともに見れない事象

表題の件についてググっても同じ事象が出てこなかったのでメモ

tl;dr;

Chrome拡張のNorton Safe Webが悪さしてた

事象

  • Chromehttps://twitter.com/ を開いたらくっそ重くてまともに閲覧できない。
    • どれだけ重いのかというとスクロールはできるんだけどクリックがなんも反応しない
    • デベロッパーツールで見ても重すぎて通信されない
  • WindowsMacの両方で再現
  • 重かったサイトは自分が見た範囲ではTwitterのみ
  • 他ブラウザでTwitterを開くのは問題なし

Chromeのタスクマネージャで見るとTwitterのタブが常時CPU 100%に張り付いてる('A`)

f:id:sue445:20180905000715p:plain

調査方法

Chromeの拡張のどれかが悪さしてるだろうとあたりをつけて、試しに拡張を全部無効にしてTwitterを開くと激軽。

それから1つずつ拡張を有効にしていったらNorton Safe Webにぶつかった。

f:id:sue445:20180905001502p:plain

再度Norton Safe Webをオフにした時のタスクマネージャ

f:id:sue445:20180905001522p:plain

rspec-time_stop を作った

github.com

モチベーション

前職の spec/support/ によくあったテスト系の便利ヘルパを現職でも使いたくなったので記憶からサルベージしてgem化しました

使い方

itdescribe:time_stop をつけるだけで現在時刻を止めた状態でテストを実行します

実装はたったこれだけです

https://github.com/sue445/rspec-time_stop/blob/v0.1.0/lib/rspec/time_stop.rb

READMEからコピペ

RSpec.describe Rspec::TimeStop do
  context "with :time_stop", :time_stop do
    it "current time is freezed" do
      before_time = Time.now
      sleep 1
      after_time = Time.now

      expect(after_time).to eq before_time
    end
  end

  context "without :time_stop" do
    it "current time is not freezed" do
      before_time = Time.now
      sleep 1
      after_time = Time.now

      expect(after_time).to be > before_time
    end

    it "current time is freezed when :time_stop is granted", :time_stop do
      before_time = Time.now
      sleep 1
      after_time = Time.now

      expect(after_time).to eq before_time
    end
  end
end

備考

原状対応してるのは ActiveSupport::Testing::TimeHelpers だけです。

(timecop対応も一応考えたんだけどCI込みだと両方対応するのがちょい面倒だった)