地方エンジニアの学習日記

興味ある技術の雑なメモだったりを書いてくブログ。たまに日記とガジェット紹介。

【Ruby】入門した

Rubyへ入門し始めたので感想とかをつらつらと。入門の動機は仕事で使う機会が出てきそうだから。がっつりアプリケーションコードを書く職種でもないけど社内の多くツールがRubyで書かれていたりアプリケーションを触る機会が出るとしたら知っておいて損ないよねくらいで入門。中期的に読んだり簡単な改修で困らないレベルまで持っていく予定

ざっくり感想

とても学びやすそうと感じました。CでいうポインタやPerlでいうコンテキストみたいな入門時に理解しづらいみたいなのが特になく入れたかなと思いました。「ん?」って思ったのは以下

  • 定数の考え方
  • 真偽値
  • 変数は常にオブジェクトへの参照

くらいかなと。これも数をこなして書いていけばすぐになれそうと言った感じでした。

書籍

https://www.amazon.co.jp/dp/B077Q8BXHC/ref=dp-kindle-redirect?_encoding=UTF8&btkr=1

この本で入門しました。内容だけじゃなく構成も素晴らしくて構文の話から実践的な開発で使えるようなテクニックや言語使用なんかも言及していたりととても読み応えのある一冊でした。

www.ruby-lang.org

あとは公式にある「他言語からのRuby入門」と言うページがわかりやすかったので良かったです。言語ごとに比較が書いてあって理解の助けになりました。

このあとは

とりあえず家で使ってるシェルスクリプトやらを再実装しつつ学んで行く予定。

【Go】SIGPIPEをハンドリングする

func main() {
        signal_chan := make(chan os.Signal, 1)
        signal.Notify(signal_chan, syscall.SIGPIPE)

        go func() {
                select {
                case <-signal_chan:
                        os.Exit(1)
                }
        }()

        run()
}

func run() {
        for {
                _, err := fmt.Println("run!")
                if err != nil {
                        if errors.Is(err, syscall.EPIPE) {
                                fmt.Fprintln(os.Stderr, "catch broken pipe error")
                        } else {
                                fmt.Fprintln(os.Stderr, err)
                        }
                }
        }
}

【Serverspec】入門する

f:id:ryuichi1208:20210728191031j:plain

背景

Serverspecを見る機会があったのでこの機会に入門する。IaCのテストってすごく難しいと思っていてどこからどこまでやるかみたいな線引きがよく分からないとかもある。記事的には具体的なServerspecの使い方よりも各プロダクトでどんな使われ方してるのかを見てまとめられれば良いかなといった感じ。

概要

Serverspecとは

Serverspec(サーバスペック)とは、サーバ状態のテスト自動化フレームワークUNIX/LinuxサーバとWindowsサーバに対応している。構築したサーバ環境が意図した通りに構成されているかについて自動的に確認作業を行えたりする。IaCの先のフェーズとかで出てくるイメージ。個人的な感覚ではあるがいままで手動で行なう必要があったサーバ構成の確認(テスト)をスクリプトにより自動化することができるツールぐらいのイメージ。

Serverspecは、Rubyで実装されていてRubyがある環境ならwindowsでも動くらしい。windows -> linuxのテストみたいなことも可能とのこと。リリース頻度自体はそこまで高くは無いもののメンテナンスはされている模様。作者は宮下 剛輔という日本の方(https://twitter.com/gosukenator)

github.com

Serverspec is not ServerSpec.ってツイートもあるがこれは間違えそうというかちょっと調べているだけでもこう書かれている記事が散見されたので結構間違ってる人いるんだろうなと感じた。(ElasticSearch的な感じかな)

公式サイトもある

serverspec.org

導入するとできること

Serverspec自体は主に以下のような目的で使われる

  • テスト駆動によるインフラのコード開発
  • サーバ構築後の確認作業の自動化
  • 稼働しているサーバの監視
  • サーバの再起動時の状態確認

サーバーにエージェントなどをインストールする必要がないのがとても便利そうでした。

セットアップ/サンプル

以下でインストールできる。

$ gem install serverspec

serverspec-initで初期化

$ serverspec-init

Select OS type:

  1) UN*X
  2) Windows

Select number: 1

Select a backend type:

  1) SSH
  2) Exec (local)

Select number: 2

Vagrant instance y/n: n
Input target host name: www.example.jp
 + spec/
 + spec/www.example.jp/
 + spec/www.example.jp/sample_spec.rb
 + spec/spec_helper.rb
 + Rakefile
 + .rspec

sample_spec.rbにテストを記述する

require 'spec_helper'

describe "gitについてのテスト" do
  context "環境設定" do
    describe package('git'), :if => os[:family] == 'redhat' do
      it "インストールされている" do
        expect be_installed
      end
    end
  end
end

describe "git-coreについてのテスト" do
  context "環境設定" do
    describe package('git-core'), :if => os[:family] == 'redhat' do
      it "インストールされている" do
        expect be_installed
      end
    end
  end
end

describe "httpdについてのテスト" do
  context "環境設定" do
    describe package('httpd'), :if => os[:family] == 'redhat' do
      it "インストールされている" do
        expect be_installed
      end

      it "enabledされている" do
        expect be_enabled
      end

      it "runningされている" do
        expect be_running
      end
    end
  end
end

describe "portについてのテスト" do
  context "環境設定" do
    describe port(80) do
      it "listeningされている" do
        expect be_listening 
      end
    end
  end
end

rake specで実行できる

$ rake spec

まとめ

参考記事

【rpmbuild】入門する

背景

rpmを作って配布してみたいなことをやっているのをみる機会があったので、どうやってんだろうと思って調べてみた。rpmbuild自体は存在も知っていたしうまくラップされたツールを使ったことがあったがオプションとか多くて驚いた。

rpmbuildとは

rpmbuild は、バイナリパッケージとソースパッケージの両方のビルド(作成)に利用される。 パッケージ はファイルのアーカイブと、アーカイブされたファイルの インストール・アンインストールに使われるメタデータから構成される。 メタデータは補助スクリプト、ファイル属性、 パッケージの説明に関する情報からなる。 パッケージ には 2 種類あり、 インストールするためのソフトウェアをカプセル化するのに使われるバイナリ パッケージと、バイナリパッケージを作成するのに必要なレシピとソースコード からなるソースパッケージとがある。

参考は以下あたり

manpages.debian.org

やってみる

yum install rpm-build

ざっと作っちゃいます。

# 作業パス生成
$ mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}

# .rpmmacros生成
$ echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros

# 移動
cd ~/rpmbuild/SRPMS/

# src.rpmをゲット
wget http://foo.var.com/foovar-1.0.src.rpm

# ビルドする
rpmbuild --rebulid foovar-1.0.src.rpm

とりあえずこれでRPM自体は出来上がる。

Usage

Usage: rpmbuild [OPTION...]

Build options with [ <specfile> | <tarball> | <source package> ]:
  -bp                           build through %prep (unpack sources and apply
                                patches) from <specfile>
  -bc                           build through %build (%prep, then compile)
                                from <specfile>
  -bi                           build through %install (%prep, %build, then
                                install) from <specfile>
  -bl                           verify %files section from <specfile>
  -ba                           build source and binary packages from
                                <specfile>
  -bb                           build binary package only from <specfile>
  -bs                           build source package only from <specfile>
  -tp                           build through %prep (unpack sources and apply
                                patches) from <tarball>
  -tc                           build through %build (%prep, then compile)
                                from <tarball>
  -ti                           build through %install (%prep, %build, then
                                install) from <tarball>
  -ta                           build source and binary packages from <tarball>
  -tb                           build binary package only from <tarball>
  -ts                           build source package only from <tarball>
  --rebuild                     build binary package from <source package>
  --recompile                   build through %install (%prep, %build, then
                                install) from <source package>
  --buildroot=DIRECTORY         override build root
  --clean                       remove build tree when done
  --nobuild                     do not execute any stages of the build
  --nodeps                      do not verify build dependencies
  --nodirtokens                 generate package header(s) compatible with
                                (legacy) rpm v3 packaging
  --noclean                     do not execute %clean stage of the build
  --nocheck                     do not execute %check stage of the build
  --rmsource                    remove sources when done
  --rmspec                      remove specfile when done
  --short-circuit               skip straight to specified stage (only for c,i)
  --target=CPU-VENDOR-OS        override target platform

Common options for all rpm modes and executables:
  -D, --define='MACRO EXPR'     define MACRO with value EXPR
  --undefine=MACRO              undefine MACRO
  -E, --eval='EXPR'             print macro expansion of EXPR
  --macros=<FILE:...>           read <FILE:...> instead of default file(s)
  --noplugins                   don't enable any plugins
  --nodigest                    don't verify package digest(s)
  --nosignature                 don't verify package signature(s)
  --rcfile=<FILE:...>           read <FILE:...> instead of default file(s)
  -r, --root=ROOT               use ROOT as top level directory (default: "/")
  --dbpath=DIRECTORY            use database in DIRECTORY
  --querytags                   display known query tags
  --showrc                      display final rpmrc and macro configuration
  --quiet                       provide less detailed output
  -v, --verbose                 provide more detailed output
  --version                     print the version of rpm being used

Options implemented via popt alias/exec:
  --with=<option>               enable configure <option> for build
  --without=<option>            disable configure <option> for build
  --buildpolicy=<policy>        set buildroot <policy> (e.g. compress man
                                pages)
  --sign                        generate GPG signature (deprecated, use
                                command rpmsign instead)

Help options:
  -?, --help                    Show this help message
  --usage                       Display brief usage message

まとめ