@tama-create/sf-tools 0.0.0-stage → 1.4.0-dev.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +24 -0
- package/README.md +212 -2
- package/bin/sft.js +22 -0
- package/dist/cli.js +35 -0
- package/dist/commands/admin.js +46 -0
- package/dist/commands/check.js +20 -0
- package/dist/commands/ci.js +123 -0
- package/dist/commands/deploy.js +31 -0
- package/dist/commands/doctor.js +11 -0
- package/dist/commands/dryrun.js +17 -0
- package/dist/commands/hook.js +46 -0
- package/dist/commands/index.js +36 -0
- package/dist/commands/job.js +31 -0
- package/dist/commands/launcher.js +40 -0
- package/dist/commands/log.js +58 -0
- package/dist/commands/metasync.js +21 -0
- package/dist/commands/pr.js +32 -0
- package/dist/commands/project.js +93 -0
- package/dist/commands/push.js +16 -0
- package/dist/commands/release.js +19 -0
- package/dist/commands/start.js +19 -0
- package/dist/commands/switch-org.js +12 -0
- package/dist/commands/update-cli.js +11 -0
- package/dist/commands/version.js +18 -0
- package/dist/core/admin-clone.js +79 -0
- package/dist/core/ai-answers.js +40 -0
- package/dist/core/branches.js +88 -0
- package/dist/core/check-targets.js +170 -0
- package/dist/core/check.js +34 -0
- package/dist/core/ci-check-branch.js +150 -0
- package/dist/core/ci-login-steps.js +73 -0
- package/dist/core/ci-release-steps.js +193 -0
- package/dist/core/ci-sequence-steps.js +188 -0
- package/dist/core/ci-validate-steps.js +140 -0
- package/dist/core/command.js +25 -0
- package/dist/core/create-connect.js +519 -0
- package/dist/core/create-setup.js +314 -0
- package/dist/core/create-state.js +82 -0
- package/dist/core/delegate.js +45 -0
- package/dist/core/deploy.js +66 -0
- package/dist/core/diag.js +200 -0
- package/dist/core/doctor.js +211 -0
- package/dist/core/eca.js +259 -0
- package/dist/core/exit-codes.js +12 -0
- package/dist/core/gh-identity.js +46 -0
- package/dist/core/hook-install.js +130 -0
- package/dist/core/hook.js +116 -0
- package/dist/core/identity.js +48 -0
- package/dist/core/job.js +243 -0
- package/dist/core/launcher-status.js +184 -0
- package/dist/core/lines.js +25 -0
- package/dist/core/log-auto.js +56 -0
- package/dist/core/log-box.js +85 -0
- package/dist/core/log-collector.js +370 -0
- package/dist/core/log-format.js +44 -0
- package/dist/core/log-pull.js +565 -0
- package/dist/core/log-select.js +126 -0
- package/dist/core/log-session.js +362 -0
- package/dist/core/log-start.js +421 -0
- package/dist/core/log-status.js +223 -0
- package/dist/core/log-summarize.js +127 -0
- package/dist/core/logger.js +71 -0
- package/dist/core/login-url.js +50 -0
- package/dist/core/manifest-build.js +118 -0
- package/dist/core/mask.js +70 -0
- package/dist/core/metadata-types.js +79 -0
- package/dist/core/metasync.js +684 -0
- package/dist/core/org.js +115 -0
- package/dist/core/origin.js +62 -0
- package/dist/core/package-xml.js +157 -0
- package/dist/core/pr.js +199 -0
- package/dist/core/project-create.js +733 -0
- package/dist/core/project-reconnect.js +126 -0
- package/dist/core/project-status.js +235 -0
- package/dist/core/project-tier.js +374 -0
- package/dist/core/project-verify.js +517 -0
- package/dist/core/promote-check.js +380 -0
- package/dist/core/promote.js +141 -0
- package/dist/core/push.js +152 -0
- package/dist/core/release.js +256 -0
- package/dist/core/scoped-box.js +29 -0
- package/dist/core/start.js +155 -0
- package/dist/core/switch-org.js +49 -0
- package/dist/core/test-types.js +212 -0
- package/dist/core/toolbox.js +4 -0
- package/dist/core/update-cli.js +156 -0
- package/dist/core/update-secret.js +301 -0
- package/dist/core/update-workflows.js +132 -0
- package/dist/core/verify-gh.js +286 -0
- package/dist/core/verify-samples.js +152 -0
- package/dist/core/windows-shim.js +21 -0
- package/dist/io/capability-file.js +11 -0
- package/dist/io/create-deps.js +68 -0
- package/dist/io/doctor-deps.js +32 -0
- package/dist/io/hook-install-deps.js +11 -0
- package/dist/io/job-deps.js +31 -0
- package/dist/io/key-file.js +17 -0
- package/dist/io/keys.js +161 -0
- package/dist/io/log-deps.js +106 -0
- package/dist/io/metasync-deps.js +21 -0
- package/dist/io/open-browser.js +21 -0
- package/dist/io/package-version.js +10 -0
- package/dist/io/pinned-project.js +111 -0
- package/dist/io/promote-deps.js +27 -0
- package/dist/io/push-refs.js +20 -0
- package/dist/io/resolve.js +74 -0
- package/dist/io/run.js +201 -0
- package/dist/io/switch-org-deps.js +10 -0
- package/dist/io/temp-dir.js +10 -0
- package/dist/io/tool-versions.js +14 -0
- package/dist/io/toolbox.js +68 -0
- package/dist/io/update-cli-deps.js +75 -0
- package/dist/io/update-secret-deps.js +27 -0
- package/dist/launch.js +152 -0
- package/dist/ui/confirm.js +18 -0
- package/dist/ui/launcher.js +132 -0
- package/dist/ui/prompt.js +66 -0
- package/package.json +37 -4
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// sft log: 手元の目録(logs/apex/.index)から、要約に入れるログを選ぶ部分(phases/log/pull.sh の 8 章の「選び方」・phases/log/fetch.sh の index_* の移し先)。
|
|
2
|
+
// 外との出入りはしない(純粋な関数だけ)。目録は 1 行 1 ログのタブ区切りで、2 列目が ID、3 列目が開始時刻(UTC)
|
|
3
|
+
export const ID_RE = /^[A-Za-z0-9]{15}([A-Za-z0-9]{3})?$/;
|
|
4
|
+
export const ISO_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/;
|
|
5
|
+
/** 目録の写し。改行で終わる行だけを使う(最後が改行で終わらなければ、書きかけの行として除く) */
|
|
6
|
+
export function parseIndex(text) {
|
|
7
|
+
if (text === "")
|
|
8
|
+
return [];
|
|
9
|
+
const lines = text.split("\n");
|
|
10
|
+
lines.pop(); // 最後の要素は、改行のあとの残り(空、または書きかけの行)
|
|
11
|
+
return lines;
|
|
12
|
+
}
|
|
13
|
+
function col(line, n) {
|
|
14
|
+
return line.split("\t")[n - 1] ?? "";
|
|
15
|
+
}
|
|
16
|
+
/** 目録の行数と、最後の行のログの ID(要約のしおり。空なら 0 と空) */
|
|
17
|
+
export function indexMark(lines) {
|
|
18
|
+
const last = lines[lines.length - 1];
|
|
19
|
+
return last === undefined ? { line: 0, id: "" } : { line: lines.length, id: col(last, 2) };
|
|
20
|
+
}
|
|
21
|
+
/** キー=値 のファイルから、値を取る(なければ空。行末の CR は除く。先に出たものが勝つ) */
|
|
22
|
+
export function kvGet(text, key) {
|
|
23
|
+
for (const raw of text.split("\n")) {
|
|
24
|
+
const line = raw.endsWith("\r") ? raw.slice(0, -1) : raw;
|
|
25
|
+
if (line.startsWith(`${key}=`))
|
|
26
|
+
return line.slice(key.length + 1);
|
|
27
|
+
}
|
|
28
|
+
return "";
|
|
29
|
+
}
|
|
30
|
+
/** 指定のキーだけを差し替える(なければ末尾に足す)。ほかの行は、そのまま残す */
|
|
31
|
+
export function kvSet(text, updates) {
|
|
32
|
+
const pending = new Map(Object.entries(updates));
|
|
33
|
+
const out = [];
|
|
34
|
+
for (const raw of text === "" ? [] : text.replace(/\n$/, "").split("\n")) {
|
|
35
|
+
const eq = raw.indexOf("=");
|
|
36
|
+
const key = eq > 0 ? raw.slice(0, eq) : "";
|
|
37
|
+
if (key !== "" && pending.has(key)) {
|
|
38
|
+
out.push(`${key}=${pending.get(key)}`);
|
|
39
|
+
pending.delete(key);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
out.push(raw);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
for (const [k, v] of pending)
|
|
46
|
+
out.push(`${k}=${v}`);
|
|
47
|
+
return `${out.join("\n")}\n`;
|
|
48
|
+
}
|
|
49
|
+
/** C ロケールの並び(開始時刻 → ID)。バイト順 */
|
|
50
|
+
function byStartThenId(a, b) {
|
|
51
|
+
const ka = [col(a, 3), col(a, 2)];
|
|
52
|
+
const kb = [col(b, 3), col(b, 2)];
|
|
53
|
+
for (let i = 0; i < 2; i++) {
|
|
54
|
+
const x = ka[i];
|
|
55
|
+
const y = kb[i];
|
|
56
|
+
if (x !== y)
|
|
57
|
+
return x < y ? -1 : 1;
|
|
58
|
+
}
|
|
59
|
+
return 0;
|
|
60
|
+
}
|
|
61
|
+
function minusSec(iso, sec) {
|
|
62
|
+
return new Date(Date.parse(iso) - sec * 1000).toISOString().replace(/\.\d{3}Z$/, "Z");
|
|
63
|
+
}
|
|
64
|
+
const OLD_ISO_RE = /^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$/;
|
|
65
|
+
/**
|
|
66
|
+
* 古い .state(LAST_READ = 最後に読んだログの開始時刻・LAST_READ_IDS = その秒に読んだ ID の並び)を、新しいしおり(目録の行)に移す
|
|
67
|
+
* (シェル版の phases/log/pull.sh の 9.2 と同じ)。開始が LAST_READ より前の行と、同じ秒で LAST_READ_IDS にある行を「見た」とし、
|
|
68
|
+
* 最初の見ていない行の手前までを読んだとする。LAST_READ の形が違えば null
|
|
69
|
+
*/
|
|
70
|
+
export function migrateOldMark(lines, lastRead, lastReadIds) {
|
|
71
|
+
if (!OLD_ISO_RE.test(lastRead))
|
|
72
|
+
return null;
|
|
73
|
+
const ids = new Set(lastReadIds.split(",").filter((x) => x !== ""));
|
|
74
|
+
const seen = (l) => {
|
|
75
|
+
const start = col(l, 3);
|
|
76
|
+
return start < lastRead || (start === lastRead && ids.has(col(l, 2)));
|
|
77
|
+
};
|
|
78
|
+
const i = lines.findIndex((l) => !seen(l));
|
|
79
|
+
const line = i < 0 ? lines.length : i;
|
|
80
|
+
return { line, id: line > 0 ? col(lines[line - 1], 2) : "" };
|
|
81
|
+
}
|
|
82
|
+
/** 要約に入れるログを選ぶ。開始時刻ではなく、目録に足された順(しおりの行より後)で選ぶ */
|
|
83
|
+
export function selectLogs(input) {
|
|
84
|
+
const { lines, lastReadLine, lastReadId } = input;
|
|
85
|
+
const notes = [];
|
|
86
|
+
const warnings = [];
|
|
87
|
+
let mode = "time";
|
|
88
|
+
let base = 0;
|
|
89
|
+
let since = "";
|
|
90
|
+
if (input.sinceMin !== null) {
|
|
91
|
+
since = minusSec(input.now, input.sinceMin * 60);
|
|
92
|
+
}
|
|
93
|
+
else if (lastReadLine === "0" && lastReadId === "") {
|
|
94
|
+
mode = "line";
|
|
95
|
+
}
|
|
96
|
+
else if (/^[0-9]+$/.test(lastReadLine) && ID_RE.test(lastReadId)) {
|
|
97
|
+
const n = Number(lastReadLine);
|
|
98
|
+
if (col(lines[n - 1] ?? "", 2) === lastReadId) {
|
|
99
|
+
mode = "line";
|
|
100
|
+
base = n;
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
const found = lines.findIndex((l) => col(l, 2) === lastReadId);
|
|
104
|
+
if (found >= 0) {
|
|
105
|
+
warnings.push(`目録の行がずれていたため、要約のしおりの行を探し直しました(${lastReadLine} → ${found + 1} 行目)。`);
|
|
106
|
+
mode = "line";
|
|
107
|
+
base = found + 1;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (mode === "time") {
|
|
112
|
+
if (input.sinceMin === null && `${lastReadLine}${lastReadId}` !== "") {
|
|
113
|
+
warnings.push("前回の要約のしおりが使えないため、直近 30 分に始まったログを対象にします。");
|
|
114
|
+
}
|
|
115
|
+
if (since === "")
|
|
116
|
+
since = minusSec(input.now, 1800);
|
|
117
|
+
}
|
|
118
|
+
let picked = mode === "line" ? lines.slice(base) : lines.filter((l) => col(l, 3) >= since);
|
|
119
|
+
picked = [...picked].sort(byStartThenId);
|
|
120
|
+
if (picked.length > input.max) {
|
|
121
|
+
const omitted = picked.length - input.max;
|
|
122
|
+
notes.push(`入れなかった古いログ: ${omitted} 本(${col(picked[0], 3)}〜${col(picked[omitted - 1], 3)}。UTC)。入れるには sf-log.sh pull --since <分> --max <本数>`);
|
|
123
|
+
picked = picked.slice(omitted);
|
|
124
|
+
}
|
|
125
|
+
return { entries: picked, mode, notes, warnings, advance: input.sinceMin === null };
|
|
126
|
+
}
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
// sft の起動からの自動のデバッグログ(sf-log.sh begin・start --auto・leave の移し先。phases/log/status.sh 10b 章・start.sh 6 章・stop.sh 9b 章)。
|
|
2
|
+
// begin(ランチャーを開く前。sf を呼ばない):
|
|
3
|
+
// 1. GitHub Actions の上・予約名の組織では取らない
|
|
4
|
+
// 2. 答え(logs/apex/.consent の AUTO・ORG・VERSION)があれば、それに従う。なければ注意を表示して聞く(Y/N/q。端末でなければ、
|
|
5
|
+
// 今回は取らず、答えも保存しない)
|
|
6
|
+
// 3. 持ち主の一覧(.owners)に自分の印(PID:起動の時刻:node)を足す。同じロックの中で、別の環境(シェル版)が扱っていないかを
|
|
7
|
+
// 確かめ、名乗り(.claim)を書く。後始末の最中(CLOSING)なら、終わるまで最大 60 秒待つ
|
|
8
|
+
// 4. .status を starting にして、裏(同じプロセスの中)で準備と取得役を始める
|
|
9
|
+
// 裏の準備(start --auto): 本番・種類を確認できない組織は取らない。答えの組織 ID を照らし合わせる(違えば答えを消す)。
|
|
10
|
+
// DebugLevel・TraceFlag(自動の規則: log-start.ts の ensureTraceFlag の auto)・.state・.status。
|
|
11
|
+
// 取らない判断は STATE=skipped、失敗は 30 秒後に 1 回だけやり直し、だめなら STATE=error(どちらも REASON に理由)。
|
|
12
|
+
// 準備ができたら取得役(log-collector.ts)になる。同じ組織の取得役が、別の sft で動いていれば、30 秒ごとに入れ替わりを待つ
|
|
13
|
+
// leave(ランチャーを閉じたあと): 裏を止めて待ち、持ち主の一覧から自分を外す。生きている持ち主が残れば何もしない。
|
|
14
|
+
// 誰もいなければ、CLOSING を書いて、TraceFlag を消し・.state の TRACE_FLAG_ID を空に・.status を stopped にし、印・名乗りを消す
|
|
15
|
+
// 裏の表示は、画面に出さず、sf-tools/logs/sf-log.log に書く(ランチャーの画面を崩さない)
|
|
16
|
+
// 裏の準備の前に、sf の更新(sft update-cli・sf-update-cli.sh)が動いていれば、終わるまで最大 10 分待つ(sf の入れ替えの途中に、sf を呼ばない。
|
|
17
|
+
// シェル版の wait_sf_upgrade と同じ。.status の WARNING に、待っていることを書く。待つあいだに中断されたら、準備をやめる)
|
|
18
|
+
// 組織の切り替え(ランチャーの中の sft switch-org)は、start.ts が、leave のあと新しい組織で beginLog し直す
|
|
19
|
+
import { isReservedOrgAlias } from "./branches.js";
|
|
20
|
+
import { CONSENT_VERSION } from "./log-auto.js";
|
|
21
|
+
import { claimCollector, runCollector, TICK_MS } from "./log-collector.js";
|
|
22
|
+
import { fmtZ, note, RUNTIME_SELF, read, stateSet } from "./log-pull.js";
|
|
23
|
+
import { ID_RE, kvGet, kvSet } from "./log-select.js";
|
|
24
|
+
import { ensureDebugLevel, ensureTraceFlag, errName, isGone, NOTICE_TEXT, recordStart, Stop, sf } from "./log-start.js";
|
|
25
|
+
import { readOrgUser } from "./log-status.js";
|
|
26
|
+
import { orgLocalReleaseCheck } from "./org.js";
|
|
27
|
+
const APEX = "logs/apex";
|
|
28
|
+
const STATE_F = `${APEX}/.state`;
|
|
29
|
+
const STATUS_F = `${APEX}/.status`;
|
|
30
|
+
const CONSENT_F = `${APEX}/.consent`;
|
|
31
|
+
const OWNERS_F = `${APEX}/.owners`;
|
|
32
|
+
const CLAIM_F = `${APEX}/.claim`;
|
|
33
|
+
const COLLECTOR_F = `${APEX}/.collector`;
|
|
34
|
+
const LOCK_DIR = `${APEX}/.lock`;
|
|
35
|
+
const MIX_STALE = 1800;
|
|
36
|
+
const CLOSING_TRIES = 300; // 0.2 秒ごと・60 秒
|
|
37
|
+
const RETRY_MS = 30_000;
|
|
38
|
+
const STANDBY_MS = 30_000;
|
|
39
|
+
const nowSec = (box) => Math.floor(box.clock.now().getTime() / 1000);
|
|
40
|
+
/** 持ち主の印が生きているか(node の印は PID の生死。別の環境の印は、生きていないとみなす) */
|
|
41
|
+
function markAlive(mark, deps) {
|
|
42
|
+
const [pid = "", , rt = ""] = mark.split(":");
|
|
43
|
+
if (rt !== RUNTIME_SELF || !/^[0-9]+$/.test(pid))
|
|
44
|
+
return false;
|
|
45
|
+
return Number(pid) === deps.pid || deps.pidAlive(Number(pid));
|
|
46
|
+
}
|
|
47
|
+
/** 持ち主の一覧の行(CLOSING の行を除く)と、CLOSING の印 */
|
|
48
|
+
function readOwners(box) {
|
|
49
|
+
const lines = read(box, OWNERS_F)
|
|
50
|
+
.split("\n")
|
|
51
|
+
.map((l) => l.replace(/\r$/, ""))
|
|
52
|
+
.filter(Boolean);
|
|
53
|
+
const closing = lines.find((l) => l.startsWith("CLOSING="))?.slice(8) ?? "";
|
|
54
|
+
return { marks: lines.filter((l) => !l.startsWith("CLOSING=")), closing };
|
|
55
|
+
}
|
|
56
|
+
function writeOwners(box, marks, closing) {
|
|
57
|
+
const lines = [...(closing !== "" ? [`CLOSING=${closing}`] : []), ...marks];
|
|
58
|
+
box.files.writeText(OWNERS_F, lines.length > 0 ? `${lines.join("\n")}\n` : "");
|
|
59
|
+
}
|
|
60
|
+
/** 自動で取るか(手元の答え。なければ聞く)。聞けない・q・N は取らない */
|
|
61
|
+
async function consentLocal(box, deps, org) {
|
|
62
|
+
const c = read(box, CONSENT_F);
|
|
63
|
+
if (kvGet(c, "AUTO") !== "" && kvGet(c, "ORG") === org && kvGet(c, "VERSION") === CONSENT_VERSION)
|
|
64
|
+
return kvGet(c, "AUTO") === "on";
|
|
65
|
+
const err = (l) => box.output.err(l);
|
|
66
|
+
err(`[INFO] ランチャーの起動中、この組織(${org})のデバッグログを、裏で自動で手元に保存できます。`);
|
|
67
|
+
err(`[INFO] ${NOTICE_TEXT}`);
|
|
68
|
+
err("[INFO] あとから sft log auto on|off で変えられます。");
|
|
69
|
+
if (!deps.interactive) {
|
|
70
|
+
err("[INFO] 確認できない(端末ではない)ため、今回は取りません(答えは保存しません)。");
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
let on = false;
|
|
74
|
+
for (;;) {
|
|
75
|
+
const k = await box.input.readKey(" 自動で取りますか? [Y/N/q]: ");
|
|
76
|
+
if (k === null || k === "\u0003")
|
|
77
|
+
return false; // 入力の終わり・Ctrl+C: 今回は取らない(答えは保存しない)
|
|
78
|
+
const a = k.trim().toLowerCase();
|
|
79
|
+
if (a === "y" || a === "n" || a === "q") {
|
|
80
|
+
on = a === "y";
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
box.files.makeDir(APEX);
|
|
85
|
+
box.files.writeText(CONSENT_F, kvSet(c, { AUTO: on ? "on" : "off", ORG: org, ORG_ID: "", VERSION: CONSENT_VERSION, ANSWERED_AT: fmtZ(box.clock.now().getTime()) }));
|
|
86
|
+
return on;
|
|
87
|
+
}
|
|
88
|
+
/** 持ち主の一覧に自分の印を足す。ok / 別の環境(説明)/ 後始末が終わらない */
|
|
89
|
+
async function registerOwner(box, deps, mark) {
|
|
90
|
+
for (let i = 0;; i++) {
|
|
91
|
+
if (!(await deps.lock.take(LOCK_DIR, 50)))
|
|
92
|
+
return "busy";
|
|
93
|
+
try {
|
|
94
|
+
const claim = read(box, CLAIM_F);
|
|
95
|
+
const rt = kvGet(claim, "RUNTIME");
|
|
96
|
+
const at = kvGet(claim, "AT");
|
|
97
|
+
if (claim !== "" && rt !== RUNTIME_SELF && (!/^[0-9]+$/.test(at) || nowSec(box) - Number(at) <= MIX_STALE))
|
|
98
|
+
return { other: rt || "不明" };
|
|
99
|
+
const o = readOwners(box);
|
|
100
|
+
if (o.closing === "" || !markAlive(o.closing, deps)) {
|
|
101
|
+
box.files.makeDir(APEX);
|
|
102
|
+
const marks = o.marks.includes(mark) ? o.marks : [...o.marks, mark];
|
|
103
|
+
writeOwners(box, marks, "");
|
|
104
|
+
box.files.writeText(CLAIM_F, kvSet(claim, { RUNTIME: RUNTIME_SELF, OWNER: mark, AT: String(nowSec(box)) }));
|
|
105
|
+
return "ok";
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
finally {
|
|
109
|
+
deps.lock.drop(LOCK_DIR);
|
|
110
|
+
}
|
|
111
|
+
if (i >= CLOSING_TRIES)
|
|
112
|
+
return "busy";
|
|
113
|
+
await box.clock.sleep(200);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/** 画面に出さず、sf-tools/logs/sf-log.log に書く箱(裏の準備用) */
|
|
117
|
+
function quietBox(box) {
|
|
118
|
+
const toLog = (line) => {
|
|
119
|
+
const m = /^\[(INFO|SUCCESS|WARNING|ERROR)\] (.*)$/.exec(line);
|
|
120
|
+
note(box, m?.[1] === "WARNING" || m?.[1] === "ERROR" ? "WARNING" : "INFO", `自動の取り始め: ${m ? m[2] : line}`);
|
|
121
|
+
};
|
|
122
|
+
return {
|
|
123
|
+
...box,
|
|
124
|
+
output: { out: toLog, err: toLog },
|
|
125
|
+
input: { readKey: async () => null, readLine: async () => null, readSecret: async () => null },
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/** 裏の準備(start --auto)。成功 = true。取らない判断・失敗は .status に書いて false */
|
|
129
|
+
async function autoStart(box, deps, org, ownerPresent) {
|
|
130
|
+
try {
|
|
131
|
+
const kind = await orgLocalReleaseCheck(box, org);
|
|
132
|
+
if (kind === 1)
|
|
133
|
+
throw new Stop(10, `${org} は本番組織のため`);
|
|
134
|
+
if (kind === 2)
|
|
135
|
+
throw new Stop(1, `${org} の組織の種類を確認できないため`);
|
|
136
|
+
const user = await readOrgUser(box, org);
|
|
137
|
+
if (typeof user === "string")
|
|
138
|
+
throw new Stop(1, `ユーザーの情報を取得できません(${user})`);
|
|
139
|
+
// 答えの照らし合わせ: 同じエイリアスで別の組織に入り直していたら、答えを消す(同意のない組織のログを取らない)
|
|
140
|
+
const c = read(box, CONSENT_F);
|
|
141
|
+
if (kvGet(c, "AUTO") !== "on" || kvGet(c, "ORG") !== org || kvGet(c, "VERSION") !== CONSENT_VERSION)
|
|
142
|
+
throw new Stop(10, "この組織で自動で取るかの答えがないため(sft の起動で聞きます)");
|
|
143
|
+
const oid = kvGet(c, "ORG_ID");
|
|
144
|
+
if (oid !== "" && oid !== user.orgId) {
|
|
145
|
+
box.files.writeText(CONSENT_F, kvSet(c, { AUTO: "", ORG_ID: "" }));
|
|
146
|
+
throw new Stop(10, "同じエイリアスで、別の組織に入り直したため(次の sft の起動で、もう一度聞きます)");
|
|
147
|
+
}
|
|
148
|
+
if (oid === "")
|
|
149
|
+
box.files.writeText(CONSENT_F, kvSet(c, { ORG_ID: user.orgId }));
|
|
150
|
+
const state = read(box, STATE_F);
|
|
151
|
+
const dlId = await ensureDebugLevel(box, org, "standard");
|
|
152
|
+
const tf = await ensureTraceFlag(box, async () => "no", {
|
|
153
|
+
org,
|
|
154
|
+
userId: user.userId,
|
|
155
|
+
dlId,
|
|
156
|
+
hours: 2,
|
|
157
|
+
yes: true,
|
|
158
|
+
auto: { ownerPresent },
|
|
159
|
+
});
|
|
160
|
+
await recordStart(box, deps, { org, userId: user.userId, state, tf });
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
catch (e) {
|
|
164
|
+
if (!(e instanceof Stop))
|
|
165
|
+
throw e;
|
|
166
|
+
if (e.code === 10) {
|
|
167
|
+
note(box, "INFO", `自動の取り始め: デバッグログは取りません(${e.message})`);
|
|
168
|
+
await stateSet({ box, deps }, STATUS_F, { STATE: "skipped", REASON: e.message, ORG: org });
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
note(box, "WARNING", `自動の取り始め: デバッグログを取れませんでした(${e.message})`);
|
|
172
|
+
await stateSet({ box, deps }, STATUS_F, { STATE: "error", REASON: e.message, ORG: org });
|
|
173
|
+
}
|
|
174
|
+
throw e;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const UPGRADE_WAIT_MS = 600_000;
|
|
178
|
+
/** sf の更新が終わるのを待つ(最大 10 分)。中断されたら false */
|
|
179
|
+
async function waitSfUpgrade(box, deps, org, signal) {
|
|
180
|
+
const running = () => deps.upgradeRunning?.() ?? false;
|
|
181
|
+
if (!running())
|
|
182
|
+
return true;
|
|
183
|
+
note(box, "INFO", `sf の更新が終わるのを待ちます(最大 ${UPGRADE_WAIT_MS / 1000} 秒)...`);
|
|
184
|
+
await stateSet({ box, deps }, STATUS_F, { STATE: "starting", ORG: org, WARNING: "sf の更新が終わるのを待っています" });
|
|
185
|
+
let waited = 0;
|
|
186
|
+
while (running()) {
|
|
187
|
+
if (signal.aborted)
|
|
188
|
+
return false;
|
|
189
|
+
if (waited >= UPGRADE_WAIT_MS) {
|
|
190
|
+
note(box, "WARNING", `sf の更新が ${UPGRADE_WAIT_MS / 1000} 秒たっても終わりません。待たずに進みます。`);
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
await deps.wait(TICK_MS, signal);
|
|
194
|
+
waited += TICK_MS;
|
|
195
|
+
}
|
|
196
|
+
if (signal.aborted)
|
|
197
|
+
return false;
|
|
198
|
+
note(box, "INFO", `sf の更新が終わりました(${waited / 1000} 秒待ちました)。`);
|
|
199
|
+
await stateSet({ box, deps }, STATUS_F, { WARNING: "" });
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
/** 裏: sf の更新の待ち → 準備(失敗は 1 回だけやり直す)→ 取得役。中断の合図で止まる */
|
|
203
|
+
async function background(box, deps, org, signal) {
|
|
204
|
+
const present = () => !signal.aborted;
|
|
205
|
+
if (!(await waitSfUpgrade(box, deps, org, signal)))
|
|
206
|
+
return;
|
|
207
|
+
for (let attempt = 0;; attempt++) {
|
|
208
|
+
try {
|
|
209
|
+
await autoStart(box, deps, org, present);
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
catch (e) {
|
|
213
|
+
if (!(e instanceof Stop)) {
|
|
214
|
+
note(box, "WARNING", `自動の取り始め: 思わぬ失敗(${e instanceof Error ? e.message : String(e)})`);
|
|
215
|
+
await stateSet({ box, deps }, STATUS_F, { STATE: "error", REASON: "思わぬ失敗のため", ORG: org });
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (e.code === 10 || attempt >= 1 || signal.aborted)
|
|
219
|
+
return;
|
|
220
|
+
await stateSet({ box, deps }, STATUS_F, {
|
|
221
|
+
STATE: "starting",
|
|
222
|
+
ORG: org,
|
|
223
|
+
REASON: "",
|
|
224
|
+
WARNING: `準備に失敗しました(${e.message})。やり直しています`,
|
|
225
|
+
});
|
|
226
|
+
await deps.wait(RETRY_MS, signal);
|
|
227
|
+
if (signal.aborted)
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
// 取得役になる。同じ組織の取得役が別の sft で動いていれば、入れ替わり(その sft の終了)を待つ
|
|
232
|
+
while (!signal.aborted) {
|
|
233
|
+
const c = await claimCollector(box, deps);
|
|
234
|
+
if (typeof c !== "string") {
|
|
235
|
+
await runCollector(c, signal, () => deps.wait(TICK_MS, signal));
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
note(box, "INFO", `取得役: 起動しません(${c})`);
|
|
239
|
+
if (!c.includes("すでに動いています"))
|
|
240
|
+
return;
|
|
241
|
+
await deps.wait(STANDBY_MS, signal);
|
|
242
|
+
if (!ID_RE.test(kvGet(read(box, STATE_F), "TRACE_FLAG_ID")))
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/** 後始末(leave の最後の 1 人)。TraceFlag を消し、手元の記録を stopped にする */
|
|
247
|
+
async function cleanup(box, deps) {
|
|
248
|
+
const state = read(box, STATE_F);
|
|
249
|
+
const tf = kvGet(state, "TRACE_FLAG_ID");
|
|
250
|
+
const org = kvGet(state, "ORG");
|
|
251
|
+
if (ID_RE.test(tf) && org !== "") {
|
|
252
|
+
const d = await sf(box, [
|
|
253
|
+
"data",
|
|
254
|
+
"delete",
|
|
255
|
+
"record",
|
|
256
|
+
"--use-tooling-api",
|
|
257
|
+
"--sobject",
|
|
258
|
+
"TraceFlag",
|
|
259
|
+
"--record-id",
|
|
260
|
+
tf,
|
|
261
|
+
"--target-org",
|
|
262
|
+
org,
|
|
263
|
+
"--json",
|
|
264
|
+
]);
|
|
265
|
+
if (d.ok)
|
|
266
|
+
note(box, "INFO", `TraceFlag を消しました(${tf}・組織: ${org})`);
|
|
267
|
+
else if (isGone(errName(d.json)))
|
|
268
|
+
note(box, "WARNING", `TraceFlag(${tf})は、すでにありません(${errName(d.json)})`);
|
|
269
|
+
else {
|
|
270
|
+
note(box, "WARNING", `TraceFlag(${tf})を消せませんでした(${errName(d.json)})。最大 2 時間で切れます`);
|
|
271
|
+
await stateSet({ box, deps }, STATUS_F, { STATE: "error", REASON: `TraceFlag を消せませんでした(${errName(d.json)})` });
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
await stateSet({ box, deps }, STATE_F, { TRACE_FLAG_ID: "" });
|
|
276
|
+
await stateSet({ box, deps }, STATUS_F, {
|
|
277
|
+
STATE: "stopped",
|
|
278
|
+
EXPIRES: "",
|
|
279
|
+
EXPIRES_AT: "",
|
|
280
|
+
REASON: "すべての sft が終了したため",
|
|
281
|
+
BACKLOG: "",
|
|
282
|
+
});
|
|
283
|
+
if (kvGet(read(box, COLLECTOR_F), "RUNTIME") === RUNTIME_SELF)
|
|
284
|
+
box.files.removeFile(COLLECTOR_F);
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
/** 持ち主の一覧から自分を外し、誰もいなければ後始末する(ランチャーの終了を止めない: WARNING だけ) */
|
|
288
|
+
async function leave(box, deps, mark) {
|
|
289
|
+
const err = (l) => box.output.err(l);
|
|
290
|
+
if (!(await deps.lock.take(LOCK_DIR, 50))) {
|
|
291
|
+
err("[WARNING] 手元の状態のロックを取れませんでした(デバッグログの後始末をしません)。");
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
let rest = 0;
|
|
295
|
+
try {
|
|
296
|
+
const o = readOwners(box);
|
|
297
|
+
if (!o.marks.includes(mark))
|
|
298
|
+
return; // 登録していない: ほかの人・手動の start の TraceFlag を消さない
|
|
299
|
+
const alive = o.marks.filter((m) => m !== mark && markAlive(m, deps));
|
|
300
|
+
rest = alive.length;
|
|
301
|
+
writeOwners(box, alive, rest > 0 ? o.closing : mark);
|
|
302
|
+
}
|
|
303
|
+
finally {
|
|
304
|
+
deps.lock.drop(LOCK_DIR);
|
|
305
|
+
}
|
|
306
|
+
if (rest > 0) {
|
|
307
|
+
err(`[INFO] ほかの sft が起動中(残り ${rest} 件)のため、デバッグログの取得は止めません。`);
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
const ok = await cleanup(box, deps);
|
|
311
|
+
box.files.removeFile(OWNERS_F);
|
|
312
|
+
if (ok && kvGet(read(box, CLAIM_F), "RUNTIME") === RUNTIME_SELF)
|
|
313
|
+
box.files.removeFile(CLAIM_F);
|
|
314
|
+
if (ok)
|
|
315
|
+
err("[INFO] デバッグログの取得を止めました(組織のログの設定を消しました。手元のログは残します)。");
|
|
316
|
+
else
|
|
317
|
+
err("[WARNING] デバッグログの後始末が、最後まで終わりませんでした(sf-tools/logs/sf-log.log を参照)。");
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* ランチャーを開く前に呼ぶ。取るなら、裏で準備と取得役を始め、終わりに呼ぶ leave を返す。取らないなら null。
|
|
321
|
+
* 表示は画面に出す(答えを聞くため)。裏の準備の表示は、sf-log.log に書く
|
|
322
|
+
*/
|
|
323
|
+
export async function beginLog(box, deps, org) {
|
|
324
|
+
const err = (l) => box.output.err(l);
|
|
325
|
+
if (box.env("GITHUB_ACTIONS") === "true")
|
|
326
|
+
return null;
|
|
327
|
+
if (isReservedOrgAlias(org)) {
|
|
328
|
+
err(`[INFO] デバッグログは取りません(${org} は共有環境(予約名)のため)。`);
|
|
329
|
+
return null;
|
|
330
|
+
}
|
|
331
|
+
if (!(await consentLocal(box, deps, org))) {
|
|
332
|
+
await stateSet({ box, deps }, STATUS_F, { STATE: "skipped", REASON: "このプロジェクトでは、自動で取らない設定のため", ORG: org });
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
const mark = `${deps.pid}:${nowSec(box)}:${RUNTIME_SELF}`;
|
|
336
|
+
const r = await registerOwner(box, deps, mark);
|
|
337
|
+
if (r !== "ok") {
|
|
338
|
+
err(r === "busy"
|
|
339
|
+
? "[INFO] デバッグログは取りません(前の後始末が終わらないため)。"
|
|
340
|
+
: `[INFO] 別の環境(${r.other})で、デバッグログを取っています。この環境では取りません。`);
|
|
341
|
+
return null;
|
|
342
|
+
}
|
|
343
|
+
await stateSet({ box, deps }, STATUS_F, {
|
|
344
|
+
STATE: "starting",
|
|
345
|
+
ORG: org,
|
|
346
|
+
REASON: "",
|
|
347
|
+
WARNING: "",
|
|
348
|
+
EXPIRES: "",
|
|
349
|
+
EXPIRES_AT: "",
|
|
350
|
+
BACKLOG: "",
|
|
351
|
+
});
|
|
352
|
+
err(`[INFO] デバッグログの準備を、裏で始めました(組織: ${org})。状態は、ランチャーに表示します。`);
|
|
353
|
+
const ctl = new AbortController();
|
|
354
|
+
const bg = background(quietBox(box), deps, org, ctl.signal).catch((e) => note(box, "WARNING", `自動の取り始め: 思わぬ失敗(${e instanceof Error ? e.message : String(e)})`));
|
|
355
|
+
return {
|
|
356
|
+
leave: async () => {
|
|
357
|
+
ctl.abort();
|
|
358
|
+
await bg;
|
|
359
|
+
await leave(box, deps, mark);
|
|
360
|
+
},
|
|
361
|
+
};
|
|
362
|
+
}
|