2024年6月28日金曜日

Blackhole-2ch の brew upgrade でエラー

Blackhole-2ch の brew upgrade でエラー

概要

Could not kickstart service "com.apple.audio.coreaudiod": 1: Operation not permitted

というエラーが出てアップグレードできなかったので対策方法を紹介します

環境

  • macOS 14.5
  • blackhole-2ch 0.6.0

対応方法

  • rm -rfi /opt/homebrew/Caskroom/blackhole-2ch/
  • brew reinstall blackhole-2ch

参考サイト

2024年6月27日木曜日

docker scout を Mac にインストールしてイメージのスキャンをしてみる

docker scout を Mac にインストールしてイメージのスキャンをしてみる

概要

docker scout はイメージの脆弱性をスキャンしてくれるツールです
docker のバージョンがある程度最新であればデフォルトで使えます
また dockerhub でもフリープランであれば3イメージまで scout スキャンを設定することができます

今回は Mac 上で動かして脆弱性に対応してみました

環境

  • macOS 11.7.10
  • docker 24.0.2

とりあえず使う

バージョン24であれば特にインストールは不要でいきなり使えます

  • docker scout quickview ruby:3.1.2

結果は以下の通りです

INFO New version 1.9.3 available (installed version is 0.16.1)
    ✓ Pulled
    ✓ Image stored for indexing
    ✓ Indexed 630 packages

  Your image  ruby:3.1.2                         │    7C    69H   134M   136L    11?   
  Base image  buildpack-deps:bullseye            │    6C    64H   132M   136L    11?   
  Refreshed base image  buildpack-deps:bullseye  │    1C     2H     3M   157L     3?   
                                                 │    -5    -62   -129    +21     -8   
  Updated base image  buildpack-deps:stable      │    1C     2H     3M   103L     3?   
                                                 │    -5    -62   -129    -33     -8   

What's Next?
  Learn more about vulnerabilities → docker scout cves ruby:3.1.2
  Learn more about base image update recommendations → docker scout recommendations ruby:3.1.2

脆弱性に対応してみる

  • docker scout recommendations ruby:3.1.2

これを実行すると対応方法を教えてくれます
基本はベースイメージを変更することで脆弱性に対応できるので dockerhub でベースイメージの別タグを探すと良いです

https://hub.docker.com/_/ruby/tags?page=&page_size=&ordering=&name=3.1.2 例えば 3.1.2 であれば 3.1.2-alpine3.14 にするだけでかなり減ります

  • docker scout quickview ruby:3.1.2-alpine3.14
INFO New version 1.9.3 available (installed version is 0.16.1)
    ✓ Pulled
    ✓ Image stored for indexing
    ✓ Indexed 128 packages

  Your image  ruby:3.1.2-alpine3.14  │    2C     8H     8M     0L          
  Base image  alpine:3.14            │    1C     2H     5M     0L          
  Updated base image  alpine:3.20    │    0C     0H     2M     0L     1?   
                                     │    -1     -2     -3            +1   

What's Next?
  Learn more about vulnerabilities → docker scout cves ruby:3.1.2-alpine3.14
  Learn more about base image update recommendations → docker scout recommendations ruby:3.1.2-alpine3.14

ただベースイメージを変更するとイメージ内で使用するコマンドも変わってくるのでベースイメージを変更した場合は Dockerfile などを書き換える必要がでてきます

Ruby のバージョンを上げることも検討する

3.1.2 -> 3.2.4 にすることで更に脆弱性を減らせます

  • docker scout quickview ruby:3.2.4-alpine3.20
INFO New version 1.9.3 available (installed version is 0.16.1)
    ✓ Provenance obtained from attestation
    ✓ SBOM obtained from attestation, 127 packages indexed
    ✓ Pulled

  Your image  ruby:3.2.4-alpine3.20  │    0C     0H     3M     0L     1?   
  Base image  alpine:3.20            │    0C     0H     2M     0L     1?   

What's Next?
  Learn more about vulnerabilities → docker scout cves ruby:3.2.4-alpine3.20

最後に

docker scout を試してみました
基本的に脆弱性が出たらベースイメージを変更することを検討しましょう

またイメージで使われているツールやソフトウェアの CVE もチェックできるのでソフトの脆弱性をチェックしたい場合は以下のコマンドを使いましょう

  • docker scout cves ruby:3.2.4-alpine3.20

参考サイト

2024年6月26日水曜日

Skype for mac で録音データが再生もダウンロードもできない場合の対処方法

Skype for mac で録音データが再生もダウンロードもできない場合の対処方法

概要

答え: Skype for cloud からダウンロードしましょう
Mac、iPhone の Skype では再生もダウンロードもできない状態です

環境

  • macOS 11.7.10
  • Skype for mac 8.122.0.205
  • Skype for cloud (2024/06/19時点)

状況

なぜか Mac 上だと再生もダウンロードもできません

ダウンロードしようとすると「ダウンロードに失敗しました」と表示されます

Skype for cloud

ここhttps://web.skype.com/?openPstnPage=true にログインするとブラウザで Skype が使えます

ここからだとダウンロードも再生もできました

最後に

Skype for Mac だとうまくダウンロードできない場合には Skype for cloud を試してみてください

それでもダメな場合は Windows クライアントなども試す価値はありそうです

2024年6月25日火曜日

Python で指定の cron が一日何回実行されるかカウントするスクリプト

Python で指定の cron が一日何回実行されるかカウントするスクリプト

概要

前回指定の日時にcronが実行されるかどうかチェックするスクリプトを作成しました
今回は更にそれを応用して指定の cron が一日に何回実行されるのかカウントしてみました

環境

  • macOS 11.7.10
  • Python 3.11.7
  • croniter 2.0.5

インストール

  • pipenv install croniter

サンプルスクリプト

from datetime import datetime, timedelta

from croniter import croniter


def count_run_today(cron_expression, target_date: str):
    # 指定の日付の開始、指定の日付の明日の開始時刻を取得
    target_datetime = datetime.strptime(target_date, "%Y/%m/%d")
    today_start = datetime(
        target_datetime.year, target_datetime.month, target_datetime.day
    ) - timedelta(seconds=1)
    tomorrow_start = today_start + timedelta(days=1) + timedelta(seconds=1)
    # cron オブジェクトの作成
    cron = croniter(cron_expression, today_start)
    # 当日内に実行される日時の分だけカウントする
    run_count = 0
    while True:
        # 次に実行される日時を取得
        next_run = cron.get_next(datetime)
        if today_start < next_run < tomorrow_start:
            run_count += 1
        else:
            break
    return run_count


# テスト正常系
print("正常系テスト")
print(count_run_today("0 12 * * *", "2024/06/24"))  # => 1
print(count_run_today("0 12 24 6 *", "2024/06/24"))  # => 1
print(count_run_today("*/1 * * * *", "2024/06/24"))  # => 1440
print(count_run_today("*/5 * * * *", "2024/06/24"))  # => 288
print(count_run_today("*/30 * * * *", "2024/06/24"))  # => 48
print(count_run_today("*/30 1 * * *", "2024/06/24"))  # => 2

最後に

当日分の cron が正しく全件実行されるか確認したいときに使えるスクリプトです

2024年6月24日月曜日

Python で指定の cron が当日実行されるかチェックするスクリプト

Python で指定の cron が当日実行されるかチェックするスクリプト

概要

例えば */5 * * * * は本日実行される cron で 30 12 22 6 * は本日実行されない cron というのを判定することができるスクリプトを紹介します

環境

  • macOS 11.7.10
  • Python 3.11.7
  • croniter 2.0.5

インストール

  • pipenv install croniter

サンプルスクリプト

from datetime import datetime, timedelta

from croniter import croniter


def will_run_today(cron_expression):
    # 今日の日付と今日の開始、明日の開始時刻を取得
    now = datetime.now()
    today_start = datetime(now.year, now.month, now.day) - timedelta(seconds=1)
    tomorrow_start = today_start + timedelta(days=1) + timedelta(seconds=1)
    # cron オブジェクトの作成
    cron = croniter(cron_expression, today_start)
    # 次に実行される日時を取得
    next_run = cron.get_next(datetime)
    # 次に実行される日時が今日中であればTrueを返す
    return today_start < next_run < tomorrow_start


# テスト正常系(2024/06/21(木)に実行した想定)
print("正常系テスト")
print(will_run_today("0 12 * * *"))
print(will_run_today("*/5 * * * *"))
print(will_run_today("*/1 * * * *"))
print(will_run_today("*/30 * * * *"))
print(will_run_today("*/30 5 * * *"))
print(will_run_today("*/30 5 * 6 *"))
print(will_run_today("*/30 5 * 6 *"))
print(will_run_today("59 23 21 6 *"))
print(will_run_today("59 23 * * 5"))
print(will_run_today("59 23 21 6 5"))
print(will_run_today("1 0 21 6 5"))
print(will_run_today("0 0 * * *"))
print(will_run_today("0 0 21 6 5"))
print(will_run_today("*/30 * * * 1-5"))

# テスト異常系
print("異常系テスト")
print(will_run_today("0 12 3 2 *"))
print(will_run_today("0 12 22 6 *"))
print(will_run_today("*/5 * 22 6 *"))
print(will_run_today("*/1 * * * 4"))
print(will_run_today("*/1 * * * 0,1,2,3,4,6"))
print(will_run_today("30 12 22 * *"))

おまけ: 指定の日付に実行されたかどうかチェックするように修正する

from datetime import datetime, timedelta

from croniter import croniter


def will_run_today(cron_expression, target_date: str):
    # 指定の日付の開始、指定の日付の明日の開始時刻を取得
    target_datetime = datetime.strptime(target_date, "%Y/%m/%d")
    today_start = datetime(
        target_datetime.year, target_datetime.month, target_datetime.day
    ) - timedelta(seconds=1)
    tomorrow_start = today_start + timedelta(days=1) + timedelta(seconds=1)
    # cron オブジェクトの作成
    cron = croniter(cron_expression, today_start)
    # 次に実行される日時を取得
    next_run = cron.get_next(datetime)
    # 次に実行される日時が指定日時内であればTrueを返す
    return today_start < next_run < tomorrow_start


# テスト正常系(2024/06/21(木)に実行した想定)
print("正常系テスト")
print(will_run_today("0 12 * * *", "2024/06/21"))
print(will_run_today("0 12 3 2 *", "2024/02/03"))
print(will_run_today("*/1 * * * *", "2024/01/01"))
print(will_run_today("0 0 * * *", "2024/01/01"))

# テスト異常系
print("異常系テスト")
print(will_run_today("0 12 22 6 *", "2024/06/21"))

最後に

cron が大量にありその中で本日実行されるのが何個あるのかチェックしたりするのに使えます

2024年6月21日金曜日

Slidev を docker で動かす

Slidev を docker で動かす

概要

Slidev は markdown でスライドが作成できるツールです
今回は簡単な使い方を紹介します

環境

  • macOS 11.7.10
  • docker 24.0.2
  • slidev 0.49.10

markdown 作成

適当な markdown を作成しましょう
ファイル名は slides.md にします

  • vim slides.md
# Slidevへようこそ!

開発者のためのプレゼンテーションスライド

---

# Page 2

- 📄 スライドを1つのMarkdownファイルで
- 🌈 テーマ、コードブロック、インタラクティブなコンポーネント
- 😎 詳しくはドキュメントをご覧ください!|

起動

docker run --name slidev --rm -it \
    --user node \
    -v ${PWD}:/slidev \
    -p 3030:3030 \
    -e NPM_MIRROR="https://registry.npmmirror.com" \
    tangramor/slidev:latest

マウントしているカレントに node_modules なども作成されるので注意しましょう
次回以降同じディレクトリで動作しないと再度作成されてしまいます

動作確認

http://localhost:3030/ にアクセスするとスライドが確認できます

セクションが長すぎると画面をはみ出てしまうのではみ出る場合はセクション内の文章を短くしましょう

最後に

少し記法が特殊な部分があるのでそこは対応が必要です
YAML ヘッダなどは使えます

参考サイト

2024年6月20日木曜日

Github Actions で rubygems に自動で push する (2要素認証対応あり)

Github Actions で rubygems に自動で push する (2要素認証対応あり)

概要

前回 dockerhub に push するアクションを作成しました
今回は rubygems に push するアクションを作成しました

環境

  • macOS 11.7.10
  • GithubActions v3
  • rubygems (2024/06/18時点)

gem.yml

  • vim .github/workflows/gem.yml
name: ci

on:
  workflow_dispatch:
    inputs:
      rubygems-otp-code:
        description: RubyGems OTP code
        required: true

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: 3.2.2
      - run: bundle install

      - name: Setup git config
        run: |
          git config --global user.email "your-github-email-address@xxx.com"
          git config --global user.name "your-github-name"

      - name: git
        run: |
          git status
          git diff

      - name: Publish to RubyGems
        run: bundle exec rake release
    env:
      GEM_HOST_API_KEY: ${{ secrets.RUBYGEMS_API_KEY }}

Rubygems の API キーの取得

設定 -> API キー

で作成できます
rubygems に push できる権限を扶養しましょう

RUBYGEMS_API_KEY の登録

リポジトリのシークレットに登録します

自分のプロジェクト -> Settings -> Secrets and variables -> Actions -> Repository secrets -> New repository secret

トラブルシューティング

There are files that need to be committed first.

ローカルの開発環境が Mac の場合 nokogiri など darwin 用のパッケージがインストールされます

     nokogiri (1.16.6-x86_64-darwin)
       racc (~> 1.4)

この状態の Gemfile.lock で Github Actions 上の Ubuntu で bundle install を実行すると linux 用の nokogiri がインストールされます

+    nokogiri (1.16.6-x86_64-linux)
+      racc (~> 1.4)

Github Actions では必ずこれがインストールされてしまうので事前に Mac 上で Linux 用のパッケージも追加してあげる必要があります

  • bundle lock --add-platform linux

You have enabled multi-factor authentication.

Rubygems で 2auth を有効にしている場合はワンタイムパスワードを入力する必要があります

env に GEM_HOST_OTP_CODE: ${{ github.event.inputs.rubygems-otp-code }} を追加することでアクション実行時にワンタイムパスワードを入力することができるのでこれで対応します

動作確認

version.rb や bundle update をして何かしら更新しましょう
そして Github に push します

今回ワークフローは手動でなので手動実行します
さらにワンタイムパスワードを入力しましょう

するとワークフローが実行されて version.rb に記載した新しいバージョンの gem が公開されていることが確認できると思います

最後に

Github Actions で rubygems に自動で push してみました
master に push されると自動で実行されます

Rubygems の API キーには有効期限があるので切れた場合は再度 Github に登録してあげましょう

参考サイト