Skip to content

Commit 3a9044e

Browse files
authored
Merge pull request #18 from THE-POWERNEWS/develop
1.4.0
2 parents 8238dba + a7a6a2b commit 3a9044e

File tree

7 files changed

+191
-1
lines changed

7 files changed

+191
-1
lines changed

CLAUDE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ VPS上で実行するサーバー管理ユーティリティ集。FreeBSD / Ubun
2525
- テスト: `bin/wb help` で動作確認可能
2626
- GitHub CLI: `gh`
2727

28+
## Mastodon関連ツールの共通パターン
29+
30+
- `config/application.yaml``mastodon`セクション(user, rails_env, dir)を共通設定として参照
31+
- tootctlの実行は`sudo -u``CommandLine`で、`RAILS_ENV`環境変数とディレクトリを設定
32+
- 現在FreeBSD専用(Ubuntu対応は不要)
33+
2834
## 注意点
2935

3036
- 外部gemのrequireは不要(`Bundler.require`で自動ロード)

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ rake uninstall # cronスクリプトをアンインストール
4343
| --- | --- |
4444
| access_log_compress | 指定日数が経過したログファイルをgzip圧縮します。 |
4545
| help | ツール一覧を表示します。 |
46+
| mastodon_follow | 全ユーザーに指定アカウントを強制フォローさせます。 |
47+
| mastodon_maintenance | Mastodonのメンテナンスコマンドを実行します。 |
48+
| mastodon_media_cleanup | Mastodonの古いメディアファイルを削除します。 |
4649
| mysql_dump | MySQLのダンプファイルを作成します。 |
4750
| postgresql_dump | PostgreSQLのダンプファイルを作成します。 |
4851
| postgresql_snapshot | PostgreSQLのZFSスナップショットを作成・管理します。 |
4952
| reboot_required | システムに再起動が必要かを判定します。 |
53+
| service_restart | 設定されたサービスを再起動します。 |
5054

5155
## 設定
5256

@@ -91,6 +95,38 @@ rake uninstall # cronスクリプトをアンインストール
9195
| days | スナップショット保管日数 | 3 |
9296
| dsn | PostgreSQL接続文字列 | postgres://postgres@localhost/mastodon |
9397

98+
### mastodon(共通設定)
99+
100+
| キー | 説明 | デフォルト |
101+
| --- | --- | --- |
102+
| user | 実行ユーザー | mastodon |
103+
| rails_env | RAILS_ENV環境変数 | production |
104+
| dir | Mastodonインストールディレクトリ | /home/mastodon/repos/mastodon |
105+
106+
### service_restart
107+
108+
| キー | 説明 | デフォルト |
109+
| --- | --- | --- |
110+
| services | 再起動するサービス名の配列 | [mastodon-sidekiq] |
111+
112+
### mastodon_maintenance
113+
114+
| キー | 説明 | デフォルト |
115+
| --- | --- | --- |
116+
| commands | 実行するtootctlサブコマンドの配列 | [cache recount accounts, accounts cull] |
117+
118+
### mastodon_follow
119+
120+
| キー | 説明 | デフォルト |
121+
| --- | --- | --- |
122+
| account | 強制フォローするアカウント名 | info |
123+
124+
### mastodon_media_cleanup
125+
126+
| キー | 説明 | デフォルト |
127+
| --- | --- | --- |
128+
| commands | 実行するtootctlサブコマンドの配列 | [media remove-orphans, ...] |
129+
94130
## ライセンス
95131

96132
MIT
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module WritersBase
2+
class MastodonFollowTool < Tool
3+
def exec(args = {})
4+
result = {success: [], failure: []}
5+
logger.info(tool: underscore, account:, message: 'フォロー実行開始')
6+
tootctl_command(['account', 'follow', account])
7+
result[:success].push(account)
8+
return result
9+
rescue => e
10+
logger.error(tool: underscore, account:, error: e.message.strip)
11+
result[:failure].push(account:, error: e.message.strip)
12+
return result
13+
end
14+
15+
def description
16+
return '全ユーザーに指定アカウントを強制フォローさせます。'
17+
end
18+
19+
private
20+
21+
def tootctl_command(args)
22+
command = CommandLine.new(
23+
['sudo', '-u', mastodon_user, 'bundle', 'exec', 'bin/tootctl', *args],
24+
)
25+
command.env = {'RAILS_ENV' => mastodon_rails_env}
26+
command.dir = mastodon_dir
27+
command.exec unless test?
28+
return command
29+
end
30+
31+
def mastodon_user = config['/mastodon/user']
32+
def mastodon_rails_env = config['/mastodon/rails_env']
33+
def mastodon_dir = config['/mastodon/dir']
34+
end
35+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module WritersBase
2+
class MastodonMaintenanceTool < Tool
3+
def exec(args = {})
4+
result = {success: [], failure: []}
5+
commands.each do |cmd|
6+
tootctl_args = cmd.split(/\s+/)
7+
logger.info(tool: underscore, command: cmd, message: '実行開始')
8+
tootctl_command(tootctl_args)
9+
result[:success].push(cmd)
10+
rescue => e
11+
logger.error(tool: underscore, command: cmd, error: e.message.strip)
12+
result[:failure].push(command: cmd, error: e.message.strip)
13+
end
14+
return result
15+
end
16+
17+
def description
18+
return 'Mastodonのメンテナンスコマンドを実行します。'
19+
end
20+
21+
private
22+
23+
def tootctl_command(args)
24+
command = CommandLine.new(
25+
['sudo', '-u', mastodon_user, 'bundle', 'exec', 'bin/tootctl', *args],
26+
)
27+
command.env = {'RAILS_ENV' => mastodon_rails_env}
28+
command.dir = mastodon_dir
29+
command.exec unless test?
30+
return command
31+
end
32+
33+
def mastodon_user = config['/mastodon/user']
34+
def mastodon_rails_env = config['/mastodon/rails_env']
35+
def mastodon_dir = config['/mastodon/dir']
36+
end
37+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module WritersBase
2+
class MastodonMediaCleanupTool < Tool
3+
def exec(args = {})
4+
result = {success: [], failure: []}
5+
commands.each do |cmd|
6+
tootctl_args = cmd.split(/\s+/)
7+
logger.info(tool: underscore, command: cmd, message: '実行開始')
8+
tootctl_command(tootctl_args)
9+
result[:success].push(cmd)
10+
rescue => e
11+
logger.error(tool: underscore, command: cmd, error: e.message.strip)
12+
result[:failure].push(command: cmd, error: e.message.strip)
13+
end
14+
return result
15+
end
16+
17+
def description
18+
return 'Mastodonの古いメディアファイルを削除します。'
19+
end
20+
21+
private
22+
23+
def tootctl_command(args)
24+
command = CommandLine.new(
25+
['sudo', '-u', mastodon_user, 'bundle', 'exec', 'bin/tootctl', *args],
26+
)
27+
command.env = {'RAILS_ENV' => mastodon_rails_env}
28+
command.dir = mastodon_dir
29+
command.exec unless test?
30+
return command
31+
end
32+
33+
def mastodon_user = config['/mastodon/user']
34+
def mastodon_rails_env = config['/mastodon/rails_env']
35+
def mastodon_dir = config['/mastodon/dir']
36+
end
37+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module WritersBase
2+
class ServiceRestartTool < Tool
3+
def exec(args = {})
4+
result = {success: [], failure: []}
5+
services.each do |name|
6+
logger.info(tool: underscore, service: name, message: '再起動開始')
7+
command = CommandLine.new(['service', name, 'restart'])
8+
command.exec unless test?
9+
result[:success].push(name)
10+
rescue => e
11+
logger.error(tool: underscore, service: name, error: e.message.strip)
12+
result[:failure].push(service: name, error: e.message.strip)
13+
end
14+
return result
15+
end
16+
17+
def description
18+
return '設定されたサービスを再起動します。'
19+
end
20+
end
21+
end

config/application.yaml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package:
1919
- tkoishi@b-shock.co.jp
2020
license: MIT
2121
url: https://github.com/THE-POWERNEWS/writersbase-tools
22-
version: 1.3.1
22+
version: 1.4.0
2323
ruby:
2424
jit: false
2525
bin: /usr/bin/ruby3.3
@@ -54,3 +54,21 @@ postgresql_snapshot:
5454
target: zroot/postgres
5555
days: 3
5656
dsn: postgres://postgres@localhost/mastodon
57+
mastodon:
58+
user: mastodon
59+
rails_env: production
60+
dir: /home/mastodon/repos/mastodon
61+
service_restart:
62+
services:
63+
- mastodon-sidekiq
64+
mastodon_maintenance:
65+
commands:
66+
- cache recount accounts
67+
- accounts cull
68+
mastodon_follow:
69+
account: info
70+
mastodon_media_cleanup:
71+
commands:
72+
- media remove-orphans
73+
- media remove --remote-headers
74+
- preview_cards remove -c 1

0 commit comments

Comments
 (0)