sendmailのラッパーをGo言語で作ってみた話

Cbffd4d28c61bc35c2e9a7a5aca632c3?s=47 Hiromi Kimura
December 15, 2016

 sendmailのラッパーをGo言語で作ってみた話

Cbffd4d28c61bc35c2e9a7a5aca632c3?s=128

Hiromi Kimura

December 15, 2016
Tweet

Transcript

  1. ໦ଜ޿ւ(.01FQBCP *OD ϗεςΟϯάࣄۀ෦5FDI.5( TFOENBJMͷϥούʔΛ (PݴޠͰ࡞ͬͯΈͨ࿩

  2. ΤϯδχΞ ໦ଜ޿ւ!LJNSPNJ ϗεςΟϯάࣄۀ෦ϜʔϜʔυϝΠϯνʔϜ

  3. ຖि༵͓ۚனͷ (P-VODI

  4. ͳΜ͔ॻ͍ͯΈ͍ͨͳ͊

  5. None
  6. ॻ͍ͯΈͨʂ

  7. TFOENBJMMPHHFS IUUQTHJUIVCDPNLJNSPNJTFOENBJMMPHHFS ͍ͨͩ·W

  8. TFOENBJMMPHHFSʁ ϜʔϜʔυϝΠϯ 1)1ɾ3VCZ "QQMJDBUJPO TFOENBJM Ϣʔβʔ ɾϢʔβʔొ࿥ ɾυϝΠϯऔಘɺߋ৽ ɾظݶ੾ΕؒۙͷҊ಺ ɹɹɹɹɹɹͳͲͷϝʔϧ

  9. TFOENBJMMPHHFSʁ ϜʔϜʔυϝΠϯ 1)1ɾ3VCZ "QQMJDBUJPO TFOENBJMMPHHFS Ϣʔβʔ ɾϢʔβʔొ࿥ ɾυϝΠϯऔಘɺߋ৽ ɾظݶ੾ΕؒۙͷҊ಺ ɹɹɹɹɹɹͳͲͷϝʔϧ

    TFOENBJM NBJM MPH
  10. TFOENBJMMPHHFSʁ ϜʔϜʔυϝΠϯ 1)1ɾ3VCZ "QQMJDBUJPO TFOENBJMMPHHFS Ϣʔβʔ ɾϢʔβʔొ࿥ ɾυϝΠϯऔಘɺߋ৽ ɾظݶ੾ΕؒۙͷҊ಺ ɹɹɹɹɹɹͳͲͷϝʔϧ

    TFOENBJM NBJM MPH UBJM
  11. Ͳ͏͍͍ͷʁ w ސ٬؅ཧ͔Βϝʔϧૹ৴ঢ়گΛӾཡͰ͖Δ ظݶ੾Εલʹϝʔϧ͕͜ͳ͔ͬͨΜͰ͚͢ͲʂʁʹରԠ͠΍͍͢ w ֬ೝ༻ͷϝʔϧΞυϨε΁ͷϝʔϧૹ৴͕ෆཁʹ Ϣʔβʔ΁̍௨ɺ֬ೝ༻ϝʔϧΞυϨε΁̍௨ͷ̎௨ૹ৴͍ͯ͠Δɾɾ

  12. Πϯετʔϧ logfile: /var/log/sendmail.log $ wget -O /usr/local/bin/sendmail-logger https:// github.com/kimromi/sendmail-logger/releases/download/ v0.0.2/sendmail-logger_linux_amd64

    $ chmod 755 /usr/local/bin/sendmail-logger ઃఆϑΝΠϧFUDTFOENBJMMPHHFSDPOpHZBNM ྫ $FOU04CJU όΠφϦΛ(JUIVCʹஔ͍͍ͯΔͷͰ 04ʹ͋ͬͨ΋ͷΛμ΢ϯϩʔυ͢Δ͚ͩ
  13. ઃఆํ๏ 1)1 sendmail_path = /usr/sbin/sendmail -t -i sendmail_path = /usr/local/bin/sendmail-logger

    QIQJOJ sendmail_path = sendmail-logger # path௨ͬͯΕ͹
  14. ઃఆํ๏ 3VCZ"DUJWF.BJMFSPO3BJMT Rails.application.configure do .. config.action_mailer.delivery_method = :sendmail config.action_mailer.sendmail_settings =

    { location: "/usr/local/bin/sendmail-logger" } end DPOpHFOWJSPONFOUT3"*-4@&/7SC
  15. ࣮૷͔Β഑෍·ͰͷಓͷΓ w ࣮૷ w ΫϩείϯύΠϧ w όΠφϦ഑෍ ҙ֎ͱ͢ΜͳΓ͍͚ͨΫϚʔ

  16. ࣮૷ 1.

  17. TFOENBJMMPHHFSͰ΍͍ͬͯΔ͜ͱ w ࣮ߦϩάग़ྗ w ඪ४ೖྗಡΈࠐΈ w ZBNMϑΝΠϧಡΈࠐΈ w ϝʔϧϩάϑΝΠϧग़ྗ w

    TFOENBJMίϚϯυ࣮ߦ
  18. ࣮ߦϩάग़ྗ w MPH(Pඪ४ͷMPHϥΠϒϥϦ package main import ( "log" "os" )

    func init() { f, _ := os.OpenFile("out.log", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666) log.SetOutput(f) } func main() { log.Print("loglog") } ¥ 3FBEBOE8SJUF ϑΝΠϧͳ͔ͬͨΒ࡞Δ ຤ඌʹςΩετ௥Ճ
  19. ඪ४ೖྗಡΈࠐΈ w CVpP(Pඪ४ͷ*0όοϑΝϦϯά package main import ( "bufio" "fmt" "os"

    ) func main() { body := "" scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { body += scanner.Text() + "\n" } if err := scanner.Err(); err != nil { fmt.Print(err) } } ෳ਺ߦͷೖྗ͕͋Δͱ͖͸ GPSͰશߦऔಘ͢Δ
  20. ZBNMϑΝΠϧಡΈࠐΈ w HPZBNMZBNMZBNMಡΈࠐΈϥΠϒϥϦ IUUQTHJUIVCDPNHPZBNMZBNM package main import ( "fmt" "io/ioutil"

    yaml "gopkg.in/yaml.v2" ) type Config struct { LogFile string `yaml:"logfile"` } func main() { file, _ := ioutil.ReadFile("/etc/sendmail-logger/config.yaml") conf := Config{} yaml.Unmarshal([]byte(file), &conf) fmt.Print(conf) } ZBNMϑΝΠϧͰͷઃఆ໊Λ TUSVDUͰఆ͓ٛͯ͘͠
  21. ϝʔϧϩάϑΝΠϧग़ྗ package main import ( "fmt" "os" ) func main()

    { file, err := os.OpenFile("mail.log", os.O_WRONLY | os.O_CREATE | os.O_APPEND, 0664) if err != nil { fmt.Print(err) } defer file.Close() file.Write(([]byte)("write")) } w PT(Pඪ४ͷ04ػೳ΁ͷΠϯλʔϑΣʔεύοέʔδ 8SJUF0OMZ EFGFSͰ࣮ߦ׬ྃ࣌ʹ ϑΝΠϧΫϩʔζ͢Δ
  22. TFOENBJMίϚϯυ࣮ߦ w PTFYFD(Pඪ४ͷ֎෦ίϚϯυ࣮ߦύοέʔδ package main import ( "fmt" "os/exec" )

    func main() { out, err := exec.Command("ls", "-la").Output() if err != nil { fmt.Print(err) } fmt.Print(string(out)) } ίϚϯυ࣮ߦͭͭ͠ ඪ४ग़ྗΛม਺ʹऔΓࠐΉ
  23. ΫϩείϯύΠϧ 2.

  24. HPY w ΫϩείϯύΠϧ༻ͷύοέʔδ $ go get github.com/mitchellh/gox $ gox Number

    of parallel builds: 3 --> linux/arm: _/Users/PMAC243S/repos/github.com/kimromi/sendmail-logger --> darwin/386: _/Users/PMAC243S/repos/github.com/kimromi/sendmail-logger --> linux/386: _/Users/PMAC243S/repos/github.com/kimromi/sendmail-logger --> netbsd/arm: _/Users/PMAC243S/repos/github.com/kimromi/sendmail-logger ɾɾɾ IUUQTHJUIVCDPNNJUDIFMMIHPY ίϚϯυൃͰ ֤04༻ͷόΠφϦΛ࡞੒ HPHFUͰΠϯετʔϧ
  25. όΠφϦ഑෍ 3.

  26. HIS w HJUIVCDPNʹWFSTJPOUBH෇͖ͰϦϦʔε IUUQTHJUIVCDPNUDOLTNHIS $ go get github.com/tcnksm/ghr $ gox

    -output "pkg/{{.Dir}}_{{.OS}}_{{.Arch}}" $ ghr v0.0.2 pkg/ WFSTJPOUBHΛ෇༩ͯ͠ϦϦʔε όΠφϦμ΢ϯϩʔυͰ͖Δ HPHFUͰΠϯετʔϧ
  27. ͓ΘΓɻ؆୯ɻ

  28. ͳΜ͔ॻ͍ͯΈ·͠ΐʂ ๻͕Ͱ͖͔ͨΒΈΜͳͰ͖Δ

  29. ࠓޙ w ςετॻ͍ͯͳ͍ɾɾɾ w TNUQͷରԠͱ͔ w qVFOUEͱ࿈ܞͯ͠5SFBTVSF%BUB΁ʢݕূதʣ w ސ٬؅ཧ͔Βϝʔϧ಺༰ΛݟΕΔΑ͏ʹ w

    ֬ೝ༻ϝʔϧΞυϨε΁ͷૹ৴Λ๾໓