diff --git a/.gitignore b/.gitignore index 99e2f804..26e1e9e6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,7 @@ erl_crash.dump *.ez /tmp /persist + +# Generated assets +/priv/static/assets/ +/priv/static/cache_manifest.json diff --git a/Dockerfile b/Dockerfile index 246f35d3..bfac0cdc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -32,6 +32,8 @@ RUN mix deps.compile # build project COPY priv priv COPY lib lib +COPY assets assets +RUN mix assets.deploy RUN mix compile # build release diff --git a/assets/css/app.css b/assets/css/app.css new file mode 100644 index 00000000..0abcd742 --- /dev/null +++ b/assets/css/app.css @@ -0,0 +1,1221 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; + +:root { + --color-primary-50: #f7f5f9; + --color-primary-100: #f0edf4; + --color-primary-200: #e1d9e8; + --color-primary-300: #c3b4d1; + --color-primary-500: #8769a4; + --color-primary-600: #69438d; + --color-primary-700: #523079; + --color-primary-800: #3d2165; + --color-grey-50: #f0f5f9; + --color-grey-100: #e1e8f0; + --color-grey-200: #cad5e0; + --color-grey-300: #91a4b7; + --color-grey-400: #61758a; + --color-grey-500: #445668; + --color-grey-600: #304254; + --color-grey-700: #1c2a3a; + --color-grey-800: #0d1829; + --color-grey-900: #030913; + --color-green-50: #f3f8f5; + --color-green-200: #cfe3d8; + --color-green-500: #419164; + --color-green-600: #11753d; + --color-green-700: #0c643c; + --color-blue-50: #f3f6fd; + --color-blue-200: #cfdef7; + --color-blue-500: #3f7ae0; + --color-blue-600: #0f59d8; + --color-blue-700: #0a44b9; + --color-yellow-50: #fefbf5; + --color-yellow-200: #fcf0d8; + --color-yellow-700: #cd942a; + --color-red-50: #faf4f5; + --color-red-200: #eed5d9; + --color-red-500: #bb5665; + --color-red-600: #aa2c3f; + --color-red-700: #92203c; + --font-sans: "Inter", ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, sans-serif; + --font-mono: "JetBrains Mono", ui-monospace, "SF Mono", Menlo, Monaco, Consolas, monospace; + --shadow-lg: 0 12px 32px rgba(13, 24, 41, 0.12); +} + +html, +body { + margin: 0; + min-height: 100%; + background: var(--color-grey-50); + color: var(--color-grey-700); + font-family: var(--font-sans); + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +*, +*::before, +*::after { + box-sizing: border-box; +} + +a { + text-decoration: none; +} + +code, +pre, +.mono { + font-family: var(--font-mono); +} + +.flash { + margin: 10px auto 0; + max-width: 1240px; + border-radius: 8px; + background: var(--color-grey-800); + color: #fff; + padding: 8px 12px; + font-size: 13px; +} + +.bob { + min-height: 100vh; + display: flex; + flex-direction: column; + color: var(--color-grey-700); + background: var(--color-grey-50); +} + +.bob-nav { + position: sticky; + top: 0; + z-index: 50; + height: 64px; + background: var(--color-grey-900); + border-bottom: 1px solid rgba(255, 255, 255, 0.06); +} + +.bob-nav__inner { + max-width: 1240px; + height: 100%; + margin: 0 auto; + padding: 0 28px; + display: flex; + align-items: center; + gap: 30px; +} + +.bob-brand { + display: flex; + align-items: center; + gap: 10px; + flex: none; +} + +.bob-brand img { + display: block; +} + +.bob-brand__name { + color: #fff; + font-size: 21px; + font-weight: 800; + letter-spacing: 0; +} + +.bob-brand__tag { + color: var(--color-grey-400); + font-size: 12px; + font-weight: 500; + padding-left: 11px; + border-left: 1px solid var(--color-grey-700); +} + +.bob-nav__tabs { + display: flex; + align-items: center; + gap: 4px; +} + +.bob-nav__tab { + display: inline-flex; + align-items: center; + gap: 8px; + height: 34px; + padding: 0 14px; + border-radius: 8px; + color: var(--color-grey-300); + font-size: 14px; + font-weight: 500; + transition: background 0.15s, color 0.15s; +} + +.bob-nav__tab:hover { + color: #fff; + background: rgba(255, 255, 255, 0.06); +} + +.bob-nav__tab svg { + color: var(--color-grey-400); + transition: color 0.15s; +} + +.bob-nav__tab:hover svg { + color: var(--color-grey-200); +} + +.bob-nav__tab--active { + color: #fff; + background: rgba(255, 255, 255, 0.1); +} + +.bob-nav__tab--active svg { + color: var(--color-primary-300); +} + +.bob-nav__right { + margin-left: auto; + display: flex; + align-items: center; + gap: 14px; +} + +.bob-menu { + display: none; + position: relative; + margin-left: auto; +} + +.bob-menu__button { + display: inline-flex; + align-items: center; + gap: 8px; + height: 36px; + border: 1px solid rgba(255, 255, 255, 0.12); + border-radius: 8px; + background: rgba(255, 255, 255, 0.06); + color: #fff; + padding: 0 12px; + font-size: 14px; + font-weight: 600; + cursor: pointer; + list-style: none; +} + +.bob-menu__button::-webkit-details-marker { + display: none; +} + +.bob-menu__button::marker { + content: ""; +} + +.bob-menu__button:hover, +.bob-menu[open] .bob-menu__button { + background: rgba(255, 255, 255, 0.1); +} + +.bob-menu__panel { + position: absolute; + top: calc(100% + 10px); + right: 0; + z-index: 80; + display: grid; + min-width: 220px; + padding: 8px; + border: 1px solid var(--color-grey-200); + border-radius: 8px; + background: #fff; + box-shadow: 0 18px 40px rgba(12, 20, 33, 0.2); +} + +.bob-menu__item { + display: flex; + align-items: center; + gap: 10px; + min-height: 38px; + border-radius: 6px; + color: var(--color-grey-600); + padding: 0 10px; + font-size: 14px; + font-weight: 600; +} + +.bob-menu__item svg { + color: var(--color-grey-400); +} + +.bob-menu__item:hover, +.bob-menu__item--active { + background: var(--color-grey-100); + color: var(--color-grey-800); +} + +.bob-menu__item--active svg { + color: var(--color-primary-600); +} + +.bob-menu__item--external { + margin-top: 6px; + border-top: 1px solid var(--color-grey-100); + padding-top: 6px; +} + +.nav-ext { + display: inline-flex; + align-items: center; + gap: 7px; + height: 32px; + padding: 0 12px; + border-radius: 8px; + border: 1px solid rgba(255, 255, 255, 0.1); + color: var(--color-grey-300); + font-family: var(--font-mono); + font-size: 12.5px; + transition: background 0.15s, color 0.15s; +} + +.nav-ext:hover { + color: #fff; + background: rgba(255, 255, 255, 0.08); +} + +.nav-ext svg { + color: var(--color-grey-400); +} + +.bob-main { + flex: 1; +} + +.bob-wrap { + max-width: 1240px; + margin: 0 auto; + padding: 28px 28px 90px; +} + +.page-head { + display: flex; + align-items: flex-end; + gap: 16px; + margin-bottom: 22px; +} + +.page-head__main { + flex: 1; + min-width: 0; +} + +.page-head h1 { + margin: 0 0 6px; + color: var(--color-grey-700); + font-size: 26px; + font-weight: 700; + line-height: 1.1; + letter-spacing: 0; +} + +.page-head__sub { + color: var(--color-grey-500); + font-size: 14px; +} + +.page-head__live { + display: inline-flex; + align-items: center; + gap: 8px; + flex: none; + padding-bottom: 3px; + color: var(--color-grey-500); + font-size: 13px; +} + +.live-dot { + position: relative; + width: 8px; + height: 8px; + border-radius: 50%; + background: var(--color-green-500); +} + +.live-dot::after { + content: ""; + position: absolute; + inset: -4px; + border-radius: 50%; + border: 1.5px solid var(--color-green-500); + opacity: 0; + animation: ping 2.4s ease-out infinite; +} + +@keyframes ping { + 0% { + transform: scale(0.55); + opacity: 0.8; + } + + 80%, + 100% { + transform: scale(1.9); + opacity: 0; + } +} + +.sec { + overflow: hidden; + margin-bottom: 18px; + border: 1px solid var(--color-grey-200); + border-radius: 14px; + background: #fff; +} + +.sec__head { + display: flex; + align-items: center; + gap: 12px; + padding: 15px 20px; + border-bottom: 1px solid var(--color-grey-100); +} + +.sec__title { + display: flex; + align-items: center; + gap: 9px; + color: var(--color-grey-700); + font-size: 15px; + font-weight: 600; +} + +.sec__count { + min-width: 22px; + border-radius: 999px; + background: var(--color-grey-100); + color: var(--color-grey-600); + padding: 1px 9px; + text-align: center; + font-size: 12px; + font-variant-numeric: tabular-nums; + font-weight: 600; +} + +.sec__count--live { + background: var(--color-blue-50); + color: var(--color-blue-700); +} + +.icon-blue { + color: var(--color-blue-600); +} + +.icon-muted { + color: var(--color-grey-400); +} + +.qsizes { + padding: 14px 20px 10px; +} + +.qsizes__label { + margin-bottom: 4px; + color: var(--color-grey-400); + font-size: 11px; + font-weight: 600; + letter-spacing: 0.06em; + text-transform: uppercase; +} + +.qsizes__grid { + display: grid; + grid-template-columns: 1fr 1fr; + gap: 0 36px; +} + +.qsize { + display: flex; + align-items: center; + justify-content: space-between; + gap: 16px; + padding: 7px 0; + border-bottom: 1px dashed var(--color-grey-100); +} + +.qsize__l { + display: flex; + align-items: center; + gap: 8px; + min-width: 0; +} + +.qsize__mod { + overflow: hidden; + color: var(--color-grey-600); + font-family: var(--font-mono); + font-size: 12.5px; + text-overflow: ellipsis; + white-space: nowrap; +} + +.qsize__count { + flex: none; + color: var(--color-grey-700); + font-size: 13px; + font-variant-numeric: tabular-nums; + font-weight: 700; +} + +.filter-bar { + display: flex; + align-items: center; + gap: 12px; + padding: 14px 20px; + border-bottom: 1px solid var(--color-grey-100); +} + +.filter-bar--wrap { + flex-wrap: wrap; +} + +.filter-bar__meta { + margin-left: auto; + color: var(--color-grey-400); + font-size: 13px; + white-space: nowrap; +} + +.search-wrap { + position: relative; + flex: 1; + min-width: 220px; + max-width: 380px; +} + +.search-box { + display: flex; + align-items: center; + gap: 8px; + width: 100%; + height: 36px; + padding: 0 10px 0 12px; + border: 1px solid var(--color-grey-200); + border-radius: 9px; + background: var(--color-grey-50); + transition: border-color 0.15s, box-shadow 0.15s, background 0.15s; +} + +.search-box:focus-within { + border-color: var(--color-blue-500); + background: #fff; + box-shadow: 0 0 0 3px rgba(15, 89, 216, 0.12); +} + +.search-box svg { + flex: none; + color: var(--color-grey-400); +} + +.search-box input { + flex: 1; + min-width: 0; + border: 0; + outline: 0; + background: transparent; + color: var(--color-grey-700); + padding: 0; + font: inherit; + font-size: 14px; +} + +.search-box input::placeholder { + color: var(--color-grey-400); +} + +.fsel { + position: relative; + display: inline-flex; + align-items: center; + gap: 7px; +} + +.fsel__label { + color: var(--color-grey-400); + font-size: 12px; +} + +.fsel select { + height: 36px; + appearance: none; + -webkit-appearance: none; + border: 1px solid var(--color-grey-200); + border-radius: 8px; + background: #fff; + color: var(--color-grey-700); + padding: 0 28px 0 11px; + font: inherit; + font-size: 13px; + cursor: pointer; + transition: border-color 0.15s; +} + +.fsel select:hover { + border-color: var(--color-grey-300); +} + +.fsel select:focus { + outline: none; + border-color: var(--color-blue-500); + box-shadow: 0 0 0 3px rgba(15, 89, 216, 0.12); +} + +.fsel svg { + position: absolute; + right: 10px; + color: var(--color-grey-400); + pointer-events: none; +} + +.fsel--off { + opacity: 0.5; +} + +.fsel--off select { + background: var(--color-grey-50); + cursor: not-allowed; +} + +.finput { + position: relative; + display: inline-flex; + align-items: center; +} + +.finput input { + width: 122px; + height: 36px; + border: 1px solid var(--color-grey-200); + border-radius: 8px; + background: #fff; + color: var(--color-grey-700); + padding: 0 11px; + font: inherit; + font-size: 13px; + transition: border-color 0.15s, box-shadow 0.15s; +} + +.finput input::placeholder { + color: var(--color-grey-400); +} + +.finput input:hover { + border-color: var(--color-grey-300); +} + +.finput input:focus { + outline: none; + border-color: var(--color-blue-500); + box-shadow: 0 0 0 3px rgba(15, 89, 216, 0.12); +} + +.finput--off input { + opacity: 0.5; + background: var(--color-grey-50); + cursor: not-allowed; +} + +.fgroup { + display: inline-flex; + align-items: center; + gap: 8px; + padding: 5px; + border: 1px dashed var(--color-grey-200); + border-radius: 10px; + transition: opacity 0.15s; +} + +.fgroup--off { + opacity: 0.5; +} + +.f-or { + color: var(--color-grey-400); + padding: 0 1px; + font-size: 12px; + font-style: italic; +} + +.f-or--off { + opacity: 0.45; +} + +.dk-filters { + align-items: stretch; +} + +.chip-row { + display: flex; + align-items: center; + gap: 6px; +} + +.chip { + display: inline-flex; + align-items: center; + gap: 7px; + height: 32px; + padding: 0 12px; + border: 1px solid var(--color-grey-200); + border-radius: 999px; + background: #fff; + color: var(--color-grey-500); + font-family: inherit; + font-size: 13px; + font-weight: 500; +} + +.chip--active { + border-color: var(--color-grey-700); + background: var(--color-grey-700); + color: #fff; +} + +.chip__n { + font-size: 11px; + font-variant-numeric: tabular-nums; + font-weight: 600; + opacity: 0.75; +} + +.chip__dot { + width: 8px; + height: 8px; + border-radius: 50%; + flex: none; +} + +.chip__dot--done { + background: var(--color-green-600); +} + +.chip__dot--failed { + background: var(--color-red-600); +} + +.tbl-wrap { + max-width: 100%; + overflow-x: auto; +} + +.jt { + min-width: 720px; + width: 100%; + border-collapse: collapse; +} + +.jt thead th { + border-bottom: 1px solid var(--color-grey-200); + background: var(--color-grey-50); + color: var(--color-grey-400); + padding: 10px 16px; + text-align: left; + font-size: 11px; + font-weight: 600; + letter-spacing: 0.06em; + text-transform: uppercase; + white-space: nowrap; +} + +.jt thead th:first-child { + padding-left: 20px; +} + +.jt thead th:last-child { + padding-right: 20px; +} + +.jt tbody td { + border-bottom: 1px solid var(--color-grey-100); + color: var(--color-grey-600); + padding: 11px 16px; + vertical-align: middle; + font-size: 13px; +} + +.jt tbody td:first-child { + padding-left: 20px; +} + +.jt tbody td:last-child { + padding-right: 20px; +} + +.jt tbody tr:last-child td { + border-bottom: 0; +} + +.jt tbody tr:hover { + background: var(--color-grey-50); +} + +.jt tr.row--failed td:first-child { + box-shadow: inset 3px 0 0 var(--color-red-500); +} + +.col-state { + width: 1%; + white-space: nowrap; +} + +.col-time { + width: 190px; + min-width: 190px; + white-space: nowrap; +} + +.col-args { + width: 40%; +} + +.c-state { + display: flex; + align-items: center; + gap: 9px; +} + +.run-label { + color: var(--color-blue-700); + font-size: 12px; + font-weight: 600; +} + +.c-mod { + display: flex; + align-items: center; + gap: 9px; + min-width: 0; +} + +.mod-name { + color: var(--color-grey-700); + font-family: var(--font-mono); + font-size: 13px; + font-weight: 500; + white-space: nowrap; +} + +.c-args { + display: block; + overflow: hidden; + max-width: 460px; + color: var(--color-grey-400); + font-family: var(--font-mono); + font-size: 12px; + text-overflow: ellipsis; + white-space: nowrap; +} + +.c-time { + display: block; + overflow: hidden; + color: var(--color-grey-600); + font-size: 13px; + font-variant-numeric: tabular-nums; + text-overflow: ellipsis; + white-space: nowrap; +} + +.c-dur { + display: block; + overflow: hidden; + color: var(--color-grey-500); + font-size: 13px; + font-variant-numeric: tabular-nums; + text-overflow: ellipsis; + white-space: nowrap; +} + +.c-elapsed { + display: block; + overflow: hidden; + color: var(--color-blue-700); + font-size: 13px; + font-variant-numeric: tabular-nums; + font-weight: 600; + text-overflow: ellipsis; + white-space: nowrap; +} + +.badge { + display: inline-flex; + align-items: center; + gap: 6px; + border: 1px solid transparent; + border-radius: 4px; + padding: 3px 8px; + font-size: 11px; + font-weight: 600; + letter-spacing: 0.01em; + white-space: nowrap; +} + +.badge--neutral { + border-color: var(--color-grey-200); + background: var(--color-grey-100); + color: var(--color-grey-600); +} + +.badge--success { + border-color: var(--color-green-200); + background: var(--color-green-50); + color: var(--color-green-700); +} + +.badge--danger { + border-color: var(--color-red-200); + background: var(--color-red-50); + color: var(--color-red-700); +} + +.badge--running { + border-color: var(--color-blue-200); + background: var(--color-blue-50); + color: var(--color-blue-700); +} + +.badge .badge__dot { + width: 6px; + height: 6px; + border-radius: 50%; + background: currentColor; + opacity: 0.9; +} + +.state-dot { + position: relative; + display: inline-block; + flex: none; + width: 9px; + height: 9px; + border-radius: 50%; +} + +.state-dot--running { + background: var(--color-blue-600); + color: var(--color-blue-600); +} + +.state-dot--done { + background: var(--color-green-600); + color: var(--color-green-600); +} + +.state-dot--failed { + background: var(--color-red-600); + color: var(--color-red-600); +} + +.state-dot--pulse::after { + content: ""; + position: absolute; + inset: -3px; + border: 1.5px solid currentColor; + border-radius: 50%; + opacity: 0; + animation: ping 1.8s ease-out infinite; +} + +.cat-glyph { + display: inline-flex; + align-items: center; + flex: none; +} + +.cat-glyph--check { + color: var(--color-grey-500); +} + +.cat-glyph--build { + color: var(--color-primary-600); +} + +.cat-glyph--docker { + color: var(--color-blue-600); +} + +.mono-cell { + color: var(--color-grey-600); + font-family: var(--font-mono); + font-size: 12.5px; + white-space: nowrap; +} + +.mono-cell--dim { + color: var(--color-grey-400); +} + +.mono-cell--name { + color: var(--color-grey-700); + font-weight: 500; +} + +.kind-badge { + border: 1px solid var(--color-primary-200); + border-radius: 5px; + background: var(--color-primary-50); + color: var(--color-primary-700); + padding: 1px 7px; + font-size: 11px; + font-weight: 700; + letter-spacing: 0.04em; + text-transform: uppercase; +} + +.arch-tag { + border: 1px solid var(--color-grey-200); + border-radius: 5px; + background: var(--color-grey-100); + color: var(--color-grey-600); + padding: 1px 7px; + font-family: var(--font-mono); + font-size: 11.5px; + font-weight: 600; +} + +.os-cell { + color: var(--color-grey-600); + font-family: var(--font-mono); + font-size: 12.5px; + white-space: nowrap; +} + +.arch-list { + display: flex; + gap: 5px; +} + +.dk-repo { + display: flex; + align-items: center; + gap: 8px; +} + +.jt--dk { + min-width: 1040px; + table-layout: fixed; +} + +.col-dk-repo { + width: 180px; +} + +.col-dk-tag { + width: 510px; +} + +.col-dk-archs { + width: 130px; +} + +.dk-tag-code { + display: block; + overflow: hidden; + width: 100%; + color: var(--color-grey-600); + font-family: var(--font-mono); + font-size: 12.5px; + text-overflow: ellipsis; + white-space: nowrap; +} + +.pager { + display: flex; + align-items: center; + justify-content: space-between; + padding: 14px 20px; + border-top: 1px solid var(--color-grey-100); +} + +.pager__info { + color: var(--color-grey-500); + font-size: 13px; + font-variant-numeric: tabular-nums; +} + +.pager__info b { + color: var(--color-grey-700); + font-weight: 600; +} + +.pager__btns { + display: flex; + gap: 8px; +} + +.pg-btn { + display: inline-flex; + align-items: center; + gap: 6px; + height: 34px; + border: 1px solid var(--color-grey-200); + border-radius: 8px; + background: #fff; + color: var(--color-grey-600); + padding: 0 13px; + font-family: inherit; + font-size: 13px; + font-weight: 500; + cursor: pointer; + transition: border-color 0.14s, color 0.14s; +} + +.pg-btn:hover:not(:disabled) { + border-color: var(--color-grey-400); + color: var(--color-grey-800); +} + +.pg-btn:disabled { + opacity: 0.45; + cursor: not-allowed; +} + +.empty-mini { + padding: 22px 20px; + color: var(--color-grey-400); + font-size: 13.5px; +} + +.empty-mini b { + color: var(--color-grey-600); + font-weight: 600; +} + +.fade-in { + animation: fade 0.25s ease; +} + +@keyframes fade { + from { + opacity: 0; + transform: translateY(4px); + } + + to { + opacity: 1; + transform: none; + } +} + +@media (max-width: 820px) { + .bob-brand__tag, + .nav-ext { + display: none; + } + + .bob-nav__inner { + max-width: none; + width: 100%; + margin: 0; + gap: 16px; + padding: 0 16px; + } + + .bob-nav__tabs { + display: none; + } + + .bob-nav__right { + display: none; + } + + .bob-menu { + display: block; + } + + .bob-wrap { + padding: 20px 16px 80px; + } + + .page-head { + flex-direction: column; + align-items: flex-start; + gap: 8px; + } + + .c-args { + max-width: 200px; + } + + .filter-bar__meta { + width: 100%; + margin-left: 0; + } + + .filter-bar.dk-filters { + display: grid; + grid-template-columns: repeat(2, minmax(0, 1fr)); + align-items: stretch; + gap: 10px; + padding: 12px; + } + + .dk-filters > .finput, + .dk-filters > .fgroup, + .dk-filters > .f-or { + grid-column: 1 / -1; + } + + .dk-filters > .fsel:first-child { + grid-column: 1 / -1; + } + + .dk-filters .fgroup { + display: grid; + grid-template-columns: repeat(2, minmax(0, 1fr)); + gap: 8px; + padding: 8px; + } + + .dk-filters .fsel, + .dk-filters .finput { + width: 100%; + min-width: 0; + } + + .dk-filters .fsel { + display: grid; + grid-template-columns: auto minmax(0, 1fr); + } + + .dk-filters .fsel select, + .dk-filters .finput input { + width: 100%; + min-width: 0; + } + + .dk-filters .f-or { + display: flex; + align-items: center; + gap: 8px; + padding: 0; + } + + .dk-filters .f-or::before, + .dk-filters .f-or::after { + content: ""; + height: 1px; + flex: 1; + background: var(--color-grey-200); + } + + .dk-filters .filter-bar__meta { + align-self: center; + justify-self: end; + width: auto; + text-align: right; + } + + .qsizes__grid { + grid-template-columns: 1fr; + } +} diff --git a/assets/js/app.js b/assets/js/app.js new file mode 100644 index 00000000..71e58668 --- /dev/null +++ b/assets/js/app.js @@ -0,0 +1,9 @@ +import "phoenix_html" +import {Socket} from "phoenix" +import {LiveSocket} from "phoenix_live_view" + +let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content") +let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}}) + +liveSocket.connect() +window.liveSocket = liveSocket diff --git a/assets/tailwind.config.js b/assets/tailwind.config.js new file mode 100644 index 00000000..cacb0d73 --- /dev/null +++ b/assets/tailwind.config.js @@ -0,0 +1,9 @@ +module.exports = { + content: [ + "./js/**/*.js", + "../lib/bob_web.ex", + "../lib/bob_web/**/*.*ex" + ], + theme: {extend: {}}, + plugins: [] +} diff --git a/config/config.exs b/config/config.exs index 96ccb41e..8cc60f70 100644 --- a/config/config.exs +++ b/config/config.exs @@ -64,4 +64,32 @@ config :sentry, client: Bob.SentryClient config :logger, :default_formatter, format: "$metadata[$level] $message\n" +config :bob, BobWeb.Endpoint, + url: [host: "localhost"], + adapter: Bandit.PhoenixAdapter, + render_errors: [ + formats: [html: BobWeb.ErrorHTML, json: BobWeb.ErrorJSON], + layout: false + ], + pubsub_server: Bob.PubSub, + live_view: [signing_salt: "u8xQ2rPspeARxr1n"] + +config :phoenix, :json_library, JSON + +config :esbuild, + version: "0.21.5", + bob: [ + args: ~w(js/app.js --bundle --target=es2017 --outdir=../priv/static/assets), + cd: Path.expand("../assets", __DIR__), + env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)} + ] + +config :tailwind, + version: "3.4.3", + bob: [ + args: + ~w(--config=tailwind.config.js --input=css/app.css --output=../priv/static/assets/app.css), + cd: Path.expand("../assets", __DIR__) + ] + import_config "#{Mix.env()}.exs" diff --git a/config/dev.exs b/config/dev.exs index 592d8eec..5e7f6895 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -10,3 +10,20 @@ config :bob, Bob.Repo, hostname: "localhost", database: "bob_dev", pool_size: 10 + +config :bob, BobWeb.Endpoint, + http: [ip: {127, 0, 0, 1}, port: 4003], + check_origin: false, + code_reloader: true, + debug_errors: true, + secret_key_base: "kQv8mZr2hP0sJX4bN6wL9cT1dY3fA5gH7uE2iO4kR6mS8tV0xZ2bC4dF6hJ8lN09", + watchers: [ + esbuild: {Esbuild, :install_and_run, [:bob, ~w(--sourcemap=inline --watch)]}, + tailwind: {Tailwind, :install_and_run, [:bob, ~w(--watch)]} + ], + live_reload: [ + patterns: [ + ~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$", + ~r"lib/bob_web/(controllers|live|components)/.*(ex|heex)$" + ] + ] diff --git a/config/runtime.exs b/config/runtime.exs index 63ca057f..3566302d 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -29,4 +29,13 @@ if config_env() == :prod do bob_who: System.fetch_env!("BOB_WHO"), bob_hostname: System.fetch_env!("BOB_HOSTNAME") } + + bob_host = System.fetch_env!("BOB_HOST") + + config :bob, BobWeb.Endpoint, + server: true, + url: [host: bob_host, port: 443, scheme: "https"], + http: [ip: {0, 0, 0, 0}, port: String.to_integer(System.get_env("BOB_PORT") || "4003")], + secret_key_base: System.fetch_env!("SECRET_KEY_BASE"), + check_origin: ["https://#{bob_host}"] end diff --git a/config/test.exs b/config/test.exs index 581ebfe9..f43b4b78 100644 --- a/config/test.exs +++ b/config/test.exs @@ -18,3 +18,8 @@ config :bob, Bob.Repo, database: "bob_test", pool: Ecto.Adapters.SQL.Sandbox, pool_size: 10 + +config :bob, BobWeb.Endpoint, + http: [ip: {127, 0, 0, 1}, port: 4002], + secret_key_base: "tT9wQ2eR5yU8iO1pA4sD7fG0hJ3kL6zX9cV2bN5mQ8wE1rT4yU7iO0pA3sD6fG90", + server: false diff --git a/lib/bob/application.ex b/lib/bob/application.ex index 54bbe1c4..65b26aab 100644 --- a/lib/bob/application.ex +++ b/lib/bob/application.ex @@ -16,11 +16,12 @@ defmodule Bob.Application do [ {Finch, name: Bob.Finch}, {Task.Supervisor, [name: Bob.Tasks]}, + {Phoenix.PubSub, name: Bob.PubSub}, Bob.DockerHub.RateLimiter, Bob.DockerHub.Auth, runner_spec(), {Bob.Schedule, [schedule()]}, - {Plug.Cowboy, scheme: :http, plug: Bob.Router, options: [port: port(), compress: true]} + BobWeb.Endpoint ] opts = [strategy: :one_for_one, name: Bob.Supervisor] @@ -41,14 +42,6 @@ defmodule Bob.Application do defp maintenance_children(), do: [Bob.Queue.Maintenance] end - defp port() do - if port = System.get_env("BOB_PORT") do - String.to_integer(port) - else - 4003 - end - end - defp setup_docker() do if config = System.get_env("BOB_DOCKER_CONFIG") do config = fix_env_newlines(config) diff --git a/lib/bob/artifacts.ex b/lib/bob/artifacts.ex index 7a7b62b7..cfec31f3 100644 --- a/lib/bob/artifacts.ex +++ b/lib/bob/artifacts.ex @@ -2,7 +2,7 @@ defmodule Bob.Artifacts do import Ecto.Query alias Bob.Repo - alias Bob.Artifacts.{Artifact, DockerTag, BaseImageTag, DockerTagStaging} + alias Bob.Artifacts.{Artifact, BaseImageTag, DockerTag, DockerTagSearch, DockerTagStaging} @builds_txt_lock 4_771_002 @@ -27,29 +27,30 @@ defmodule Bob.Artifacts do def add_docker_tag(repo, tag, archs, built_at \\ DateTime.utc_now()) do now = NaiveDateTime.utc_now() built_at = dump_utc_datetime(built_at) + search = DockerTagSearch.metadata(repo, tag) Repo.query!( """ - INSERT INTO docker_tags (repo, tag, archs, built_at, inserted_at, updated_at) - VALUES ($1, $2, $3, $4, $5, $5) + INSERT INTO docker_tags (repo, tag, archs, search, built_at, inserted_at, updated_at) + VALUES ($1, $2, $3, $4, $5, $6, $6) ON CONFLICT (repo, tag) DO UPDATE SET archs = ( SELECT array_agg(DISTINCT a ORDER BY a) FROM unnest(docker_tags.archs || EXCLUDED.archs) AS a ), + search = EXCLUDED.search, built_at = EXCLUDED.built_at, updated_at = EXCLUDED.updated_at """, - [repo, tag, archs, built_at, now] + [repo, tag, archs, search, built_at, now] ) :ok end @doc """ - Inserts a page of `{tag, archs, built_at}` tuples into the staging table under - `token`. + Inserts a page of `{tag, archs, built_at}` tuples into the staging table under `token`. Reconcile streams Docker Hub a page at a time into staging so the full tag list (up to ~1M for hexpm/elixir) is never held in memory and no connection is @@ -59,12 +60,20 @@ defmodule Bob.Artifacts do """ def stage_docker_tags(_token, _repo, []), do: :ok - def stage_docker_tags(token, repo, tag_archs) do + def stage_docker_tags(token, repo, tag_archs_built_at) do now = NaiveDateTime.utc_now() - tag_archs + tag_archs_built_at |> Enum.map(fn {tag, archs, built_at} -> - %{token: token, repo: repo, tag: tag, archs: archs, built_at: built_at, inserted_at: now} + %{ + token: token, + repo: repo, + tag: tag, + archs: archs, + search: DockerTagSearch.metadata(repo, tag), + built_at: built_at, + inserted_at: now + } end) |> Enum.chunk_every(@staging_chunk) |> Enum.each(&Repo.insert_all(DockerTagStaging, &1)) @@ -88,15 +97,19 @@ defmodule Bob.Artifacts do fn -> Repo.query!( """ - INSERT INTO docker_tags (repo, tag, archs, built_at, inserted_at, updated_at) - SELECT DISTINCT ON (repo, tag) repo, tag, archs, built_at, $3, $3 + INSERT INTO docker_tags (repo, tag, archs, search, built_at, inserted_at, updated_at) + SELECT DISTINCT ON (repo, tag) repo, tag, archs, search, built_at, $3, $3 FROM docker_tags_staging WHERE token = $1 AND repo = $2 ORDER BY repo, tag, built_at DESC ON CONFLICT (repo, tag) - DO UPDATE SET archs = EXCLUDED.archs, built_at = EXCLUDED.built_at, updated_at = EXCLUDED.updated_at + DO UPDATE SET + archs = EXCLUDED.archs, + search = EXCLUDED.search, + built_at = EXCLUDED.built_at, + updated_at = EXCLUDED.updated_at WHERE docker_tags.archs IS DISTINCT FROM EXCLUDED.archs - -- TODO: Remove this built_at comparison after production Docker tag rows are corrected. + OR docker_tags.search IS DISTINCT FROM EXCLUDED.search OR docker_tags.built_at IS DISTINCT FROM EXCLUDED.built_at """, [token, repo, now], @@ -194,6 +207,178 @@ defmodule Bob.Artifacts do num_rows end + def search_artifacts(filters \\ %{}, limit \\ 100, offset \\ 0) do + filters + |> artifact_search_query() + |> order_by([a], desc: a.built_at, desc: a.id) + |> limit(^limit) + |> offset(^offset) + |> Repo.all() + end + + def count_artifacts(filters \\ %{}) do + filters + |> artifact_search_query() + |> Repo.aggregate(:count, :id) + end + + def search_docker_tags(filters \\ %{}, limit \\ 100, offset \\ 0) do + filters + |> docker_tag_search_query() + |> order_by([d], desc: d.built_at, desc: d.id) + |> limit(^limit) + |> offset(^offset) + |> Repo.all() + end + + def count_docker_tags(filters \\ %{}) do + filters + |> docker_tag_search_query() + |> Repo.aggregate(:count, :id) + end + + def distinct_kinds(), do: distinct_values(Artifact, :kind) + def distinct_arches(), do: distinct_values(Artifact, :arch) + def distinct_oses(), do: distinct_values(Artifact, :os) + def distinct_repos(), do: distinct_values(DockerTag, :repo) + + def docker_tag_filter_options() do + %{ + repos: DockerTagSearch.repos(), + arches: DockerTagSearch.arches(), + oses: DockerTagSearch.oses() + } + end + + def distinct_docker_arches(), do: DockerTagSearch.arches() + + def distinct_docker_oses(), do: DockerTagSearch.oses() + + defp distinct_values(schema, field) do + Repo.all( + from(s in schema, distinct: true, select: field(s, ^field), order_by: field(s, ^field)) + ) + end + + defp artifact_search_query(filters) do + query = blank_to_nil(Map.get(filters, :query)) + + Artifact + |> text_filter(query, [:name, :ref]) + |> eq_filter(:kind, Map.get(filters, :kind)) + |> eq_filter(:arch, Map.get(filters, :arch)) + |> eq_filter(:os, Map.get(filters, :os)) + end + + defp docker_tag_search_query(filters) do + DockerTag + |> prefix_filter(:repo, Map.get(filters, :repo)) + |> prefix_filter(:tag, Map.get(filters, :tag)) + |> arch_prefix_filter(Map.get(filters, :arch)) + |> search_metadata_prefix_filter(:elixir_version, Map.get(filters, :elixir_version)) + |> search_metadata_prefix_filter(:erlang_version, Map.get(filters, :erlang_version)) + |> search_metadata_prefix_filter(:os, Map.get(filters, :os)) + |> search_metadata_prefix_filter(:os_version, Map.get(filters, :os_version)) + end + + defp blank_to_nil(nil), do: nil + defp blank_to_nil(""), do: nil + defp blank_to_nil(value), do: value + + defp dump_utc_datetime(%DateTime{} = datetime) do + datetime + |> DateTime.to_naive() + |> NaiveDateTime.truncate(:microsecond) + end + + defp dump_utc_datetime(%NaiveDateTime{} = datetime) do + NaiveDateTime.truncate(datetime, :microsecond) + end + + defp eq_filter(query, _field, nil), do: query + defp eq_filter(query, _field, ""), do: query + defp eq_filter(query, field, value), do: from(s in query, where: field(s, ^field) == ^value) + + defp prefix_filter(query, _field, nil), do: query + defp prefix_filter(query, _field, ""), do: query + + defp prefix_filter(query, field, value) do + pattern = prefix_pattern(value) + from(s in query, where: fragment("? LIKE ? ESCAPE '\\'", field(s, ^field), ^pattern)) + end + + defp arch_prefix_filter(query, nil), do: query + defp arch_prefix_filter(query, ""), do: query + + defp arch_prefix_filter(query, value) when value in ["amd64", "arm64"] do + from(d in query, where: fragment("? @> ?", d.archs, ^[value])) + end + + defp arch_prefix_filter(query, value) do + pattern = prefix_pattern(value) + + from(d in query, + where: + fragment( + "EXISTS (SELECT 1 FROM unnest(?) AS arch(value) WHERE value LIKE ? ESCAPE '\\')", + d.archs, + ^pattern + ) + ) + end + + defp search_metadata_prefix_filter(query, _field, nil), do: query + defp search_metadata_prefix_filter(query, _field, ""), do: query + + defp search_metadata_prefix_filter(query, :elixir_version, value) do + pattern = prefix_pattern(value) + + from(d in query, + where: fragment("?->>'elixir_version' LIKE ? ESCAPE '\\'", d.search, ^pattern) + ) + end + + defp search_metadata_prefix_filter(query, :erlang_version, value) do + pattern = prefix_pattern(value) + + from(d in query, + where: fragment("?->>'erlang_version' LIKE ? ESCAPE '\\'", d.search, ^pattern) + ) + end + + defp search_metadata_prefix_filter(query, :os, value) do + pattern = prefix_pattern(value) + from(d in query, where: fragment("?->>'os' LIKE ? ESCAPE '\\'", d.search, ^pattern)) + end + + defp search_metadata_prefix_filter(query, :os_version, value) do + pattern = prefix_pattern(value) + + from(d in query, + where: fragment("?->>'os_version' LIKE ? ESCAPE '\\'", d.search, ^pattern) + ) + end + + defp prefix_pattern(value), do: escape_like(value) <> "%" + + defp text_filter(query, nil, _fields), do: query + + defp text_filter(query, text, [first_field | rest_fields]) do + pattern = "%" <> escape_like(text) <> "%" + + conditions = + Enum.reduce(rest_fields, dynamic([s], ilike(field(s, ^first_field), ^pattern)), fn field, + acc -> + dynamic([s], ^acc or ilike(field(s, ^field), ^pattern)) + end) + + from(s in query, where: ^conditions) + end + + defp escape_like(string) do + String.replace(string, ~r/([\\%_])/, "\\\\\\1") + end + def docker_tags(repo) do Repo.all( from(d in DockerTag, @@ -326,14 +511,4 @@ defmodule Bob.Artifacts do defp format_date(built_at) do Calendar.strftime(built_at, "%Y-%m-%dT%H:%M:%SZ") end - - defp dump_utc_datetime(%DateTime{} = datetime) do - datetime - |> DateTime.to_naive() - |> NaiveDateTime.truncate(:microsecond) - end - - defp dump_utc_datetime(%NaiveDateTime{} = datetime) do - NaiveDateTime.truncate(datetime, :microsecond) - end end diff --git a/lib/bob/artifacts/docker_tag.ex b/lib/bob/artifacts/docker_tag.ex index 57369b19..ab5a8806 100644 --- a/lib/bob/artifacts/docker_tag.ex +++ b/lib/bob/artifacts/docker_tag.ex @@ -5,6 +5,7 @@ defmodule Bob.Artifacts.DockerTag do field(:repo, :string) field(:tag, :string) field(:archs, {:array, :string}) + field(:search, :map, default: %{}) field(:built_at, :utc_datetime_usec) timestamps(type: :utc_datetime_usec) end diff --git a/lib/bob/artifacts/docker_tag_search.ex b/lib/bob/artifacts/docker_tag_search.ex new file mode 100644 index 00000000..ddda1c9f --- /dev/null +++ b/lib/bob/artifacts/docker_tag_search.ex @@ -0,0 +1,55 @@ +defmodule Bob.Artifacts.DockerTagSearch do + @archs ~w(amd64 arm64) + @erlang_repos ~w(hexpm/erlang hexpm/erlang-amd64 hexpm/erlang-arm64) + @elixir_repos ~w(hexpm/elixir hexpm/elixir-amd64 hexpm/elixir-arm64) + @oses ~w(alpine debian ubuntu) + + @erlang_tag_regex ~r/^(.+)-(alpine|ubuntu|debian)-(.+)$/ + @elixir_tag_regex ~r/^(.+)-erlang-(.+)-(alpine|ubuntu|debian)-(.+)$/ + @version_regex ~r/^\d+(?:\.\d+)*(?:-[0-9A-Za-z][0-9A-Za-z.-]*)?$/ + + def repos(), do: Enum.sort(@erlang_repos ++ @elixir_repos) + def arches(), do: @archs + def oses(), do: @oses + + def metadata(repo, tag) when repo in @erlang_repos do + case Regex.run(@erlang_tag_regex, tag, capture: :all_but_first) do + [erlang_version, os, os_version] -> + if version?(erlang_version) do + %{ + "erlang_version" => erlang_version, + "os" => os, + "os_version" => os_version + } + else + %{} + end + + _other -> + %{} + end + end + + def metadata(repo, tag) when repo in @elixir_repos do + case Regex.run(@elixir_tag_regex, tag, capture: :all_but_first) do + [elixir_version, erlang_version, os, os_version] -> + if version?(elixir_version) and version?(erlang_version) do + %{ + "elixir_version" => elixir_version, + "erlang_version" => erlang_version, + "os" => os, + "os_version" => os_version + } + else + %{} + end + + _other -> + %{} + end + end + + def metadata(_repo, _tag), do: %{} + + defp version?(version), do: Regex.match?(@version_regex, version) +end diff --git a/lib/bob/artifacts/docker_tag_staging.ex b/lib/bob/artifacts/docker_tag_staging.ex index 668843e8..a096f283 100644 --- a/lib/bob/artifacts/docker_tag_staging.ex +++ b/lib/bob/artifacts/docker_tag_staging.ex @@ -7,6 +7,7 @@ defmodule Bob.Artifacts.DockerTagStaging do field(:repo, :string) field(:tag, :string) field(:archs, {:array, :string}) + field(:search, :map, default: %{}) field(:built_at, :utc_datetime_usec) field(:inserted_at, :naive_datetime_usec) end diff --git a/lib/bob/docker_hub.ex b/lib/bob/docker_hub.ex index e5a83a2a..553cdb41 100644 --- a/lib/bob/docker_hub.ex +++ b/lib/bob/docker_hub.ex @@ -18,8 +18,8 @@ defmodule Bob.DockerHub do @doc """ Pages every tag of `repo` from Docker Hub, invoking `on_page` with each page's - `{tag, archs, built_at}` list as it arrives. Returns `:ok` once the full set is - fetched or `:error` if any page failed, so the caller can avoid applying a + `{tag, archs, built_at}` list as it arrives. Returns `:ok` once the full set + is fetched or `:error` if any page failed, so the caller can avoid applying a partial set. Pages stream through `on_page` rather than accumulating, so the response set is never held in memory in full. """ @@ -102,24 +102,27 @@ defmodule Bob.DockerHub do |> Enum.max_by(&DateTime.to_unix(&1, :microsecond), fn -> nil end) end - defp parse_timestamp(value) when value in [nil, ""], do: nil - defp parse_timestamp(value) when is_binary(value) do - with {:error, _reason} <- DateTime.from_iso8601(value) do - parse_naive_timestamp(value) - else - {:ok, datetime, _offset} -> force_microsecond_precision(datetime) + case DateTime.from_iso8601(value) do + {:ok, datetime, _offset} -> + force_microsecond_precision(datetime) + + {:error, _reason} -> + parse_naive_timestamp(value) end end + defp parse_timestamp(_value), do: nil + defp parse_naive_timestamp(value) do - with {:ok, datetime} <- NaiveDateTime.from_iso8601(value) do - datetime - |> NaiveDateTime.truncate(:microsecond) - |> DateTime.from_naive!("Etc/UTC") - |> force_microsecond_precision() - else - {:error, _reason} -> nil + case NaiveDateTime.from_iso8601(value) do + {:ok, datetime} -> + datetime + |> DateTime.from_naive!("Etc/UTC") + |> force_microsecond_precision() + + {:error, _reason} -> + nil end end diff --git a/lib/bob/queue.ex b/lib/bob/queue.ex index 3b72349f..946c8f2b 100644 --- a/lib/bob/queue.ex +++ b/lib/bob/queue.ex @@ -59,6 +59,7 @@ defmodule Bob.Queue do ) end) + broadcast() :ok end @@ -87,6 +88,7 @@ defmodule Bob.Queue do [[id, args_binary]] -> args = Term.decode(args_binary) Logger.info("STARTING #{inspect(key)} #{inspect(args)}") + broadcast() {:ok, {id, args}} [] -> @@ -111,6 +113,7 @@ defmodule Bob.Queue do end end) + broadcast() :ok end @@ -126,6 +129,7 @@ defmodule Bob.Queue do end end) + broadcast() :ok end @@ -150,6 +154,41 @@ defmodule Bob.Queue do ) end + def running() do + Repo.all( + from(j in Job, + where: j.state == "running", + order_by: [desc: j.started_at, desc: j.id] + ) + ) + end + + def queued_listing(limit \\ 100, offset \\ 0) do + Repo.all( + from(j in Job, + where: j.state == "queued", + order_by: [j.inserted_at, j.id], + limit: ^limit, + offset: ^offset + ) + ) + end + + def finished_count() do + Repo.aggregate(from(j in Job, where: j.state in ["done", "failed"]), :count, :id) + end + + def recent(limit \\ 50, offset \\ 0) do + Repo.all( + from(j in Job, + where: j.state in ["done", "failed"], + order_by: [desc: j.finished_at, desc: j.id], + limit: ^limit, + offset: ^offset + ) + ) + end + defp finish(id, state) do now = DateTime.utc_now() @@ -224,4 +263,8 @@ defmodule Bob.Queue do defp backoff_seconds(count) do min(@backoff_base * Integer.pow(2, min(count - 1, 12)), @backoff_max) end + + defp broadcast() do + Phoenix.PubSub.broadcast(Bob.PubSub, "jobs", :jobs_changed) + end end diff --git a/lib/bob/remote_queue.ex b/lib/bob/remote_queue.ex index 3864d306..29f21772 100644 --- a/lib/bob/remote_queue.ex +++ b/lib/bob/remote_queue.ex @@ -1,10 +1,10 @@ defmodule Bob.RemoteQueue do def add(module, args) do - request("/queue/add", %{module: module, args: args}) + request("/api/queue/add", %{module: module, args: args}) end def docker_add(repo, tag, archs) do - request("/docker/add", %{repo: repo, tag: tag, archs: archs}) + request("/api/docker/add", %{repo: repo, tag: tag, archs: archs}) end def success({:local, id}) do @@ -81,7 +81,7 @@ defmodule Bob.RemoteQueue do defp apply_job(module, fun, args), do: apply(module, fun, args) defp done_request(type, id) do - request("/queue/#{type}", %{id: id}) + request("/api/queue/#{type}", %{id: id}) :ok end @@ -91,7 +91,7 @@ defmodule Bob.RemoteQueue do defp start_request(remote_jobs, max_weight, weights) do {:ok, %{jobs: jobs}} = - request("/queue/start", %{jobs: remote_jobs, max_weight: max_weight, weights: weights}) + request("/api/queue/start", %{jobs: remote_jobs, max_weight: max_weight, weights: weights}) jobs end diff --git a/lib/bob/router.ex b/lib/bob/router.ex deleted file mode 100644 index 275376c6..00000000 --- a/lib/bob/router.ex +++ /dev/null @@ -1,86 +0,0 @@ -defmodule Bob.Router do - use Plug.Router - use Sentry.PlugCapture - - import Plug.Conn - - plug(Bob.Plug.Forwarded) - plug(Bob.Plug.Status) - # TODO: SSL? - - plug(Plug.RequestId) - plug(Logster.Plugs.Logger, excludes: [:params]) - - plug(:secret) - - plug(Plug.Parsers, - pass: ["application/json", "application/vnd.bob+erlang"], - parsers: [:json, Bob.Plug.Parser], - json_decoder: JSON - ) - - plug(Sentry.PlugContext) - plug(:match) - plug(:dispatch) - - post "queue/start" do - jobs = - conn.params.jobs - |> Bob.RemoteQueue.prioritize() - |> Bob.RemoteQueue.start_jobs(conn.params.max_weight, conn.params.weights) - - conn - |> put_resp_header("content-type", "application/vnd.bob+erlang") - |> send_resp(200, Bob.Plug.ErlangFormat.encode_to_iodata!(%{jobs: jobs})) - end - - post "queue/success" do - Bob.Queue.success(conn.params.id) - send_resp(conn, 204, "") - end - - post "queue/failure" do - Bob.Queue.failure(conn.params.id) - send_resp(conn, 204, "") - end - - post "queue/add" do - Bob.Queue.add(conn.params.module, conn.params.args) - send_resp(conn, 204, "") - end - - post "artifacts/add" do - Bob.Artifacts.add(%{ - kind: conn.params["kind"], - arch: conn.params["arch"], - os: conn.params["os"], - name: conn.params["name"], - ref: conn.params["ref"], - sha256: conn.params["sha256"], - built_at: conn.params["date"] - }) - - send_resp(conn, 204, "") - end - - post "docker/add" do - Bob.Artifacts.add_docker_tag(conn.params.repo, conn.params.tag, conn.params.archs) - send_resp(conn, 204, "") - end - - match _ do - send_resp(conn, 404, "") - end - - defp secret(conn, _opts) do - secret = Application.get_env(:bob, :agent_secret) - - if get_req_header(conn, "authorization") == [secret] do - conn - else - conn - |> send_resp(401, "") - |> halt() - end - end -end diff --git a/lib/bob_web.ex b/lib/bob_web.ex new file mode 100644 index 00000000..c79bb7f9 --- /dev/null +++ b/lib/bob_web.ex @@ -0,0 +1,59 @@ +defmodule BobWeb do + def static_paths, do: ~w(assets favicon.ico images) + + def router do + quote do + use Phoenix.Router, helpers: false + import Plug.Conn + import Phoenix.Controller + import Phoenix.LiveView.Router + end + end + + def controller do + quote do + use Phoenix.Controller, + formats: [:html, :json], + layouts: [html: BobWeb.Layouts] + + import Plug.Conn + unquote(verified_routes()) + end + end + + def live_view do + quote do + use Phoenix.LiveView, layout: {BobWeb.Layouts, :app} + unquote(html_helpers()) + end + end + + def html do + quote do + use Phoenix.Component + import Phoenix.Controller, only: [get_csrf_token: 0] + unquote(html_helpers()) + end + end + + defp html_helpers do + quote do + import BobWeb.CoreComponents + alias Phoenix.LiveView.JS + unquote(verified_routes()) + end + end + + def verified_routes do + quote do + use Phoenix.VerifiedRoutes, + endpoint: BobWeb.Endpoint, + router: BobWeb.Router, + statics: BobWeb.static_paths() + end + end + + defmacro __using__(which) when is_atom(which) do + apply(__MODULE__, which, []) + end +end diff --git a/lib/bob_web/components/core_components.ex b/lib/bob_web/components/core_components.ex new file mode 100644 index 00000000..9c0c8355 --- /dev/null +++ b/lib/bob_web/components/core_components.ex @@ -0,0 +1,308 @@ +defmodule BobWeb.CoreComponents do + use Phoenix.Component + + attr(:flash, :map, default: %{}) + + def flash_group(assigns) do + ~H""" +
+ <%= kind %>: <%= message %> +
+ """ + end + + attr(:name, :string, required: true) + attr(:size, :integer, default: 16) + attr(:class, :any, default: nil) + + def icon(assigns) do + assigns = assign(assigns, :paths, icon_paths(assigns.name)) + + ~H""" + + """ + end + + attr(:title, :string, required: true) + attr(:count, :integer, default: nil) + attr(:icon, :string, default: nil) + attr(:icon_class, :string, default: nil) + attr(:live, :boolean, default: false) + slot(:inner_block, required: true) + + def section(assigns) do + ~H""" +
+
+
+ <.icon :if={@icon} name={@icon} class={@icon_class} /> + <%= @title %> +
+ + <%= @count %> + +
+ <%= render_slot(@inner_block) %> +
+ """ + end + + attr(:rows, :list, required: true) + attr(:class, :string, default: nil) + + slot :col, required: true do + attr(:label, :string) + attr(:class, :string) + end + + def table(assigns) do + ~H""" +
+ + + + + + + + + + + +
<%= col[:label] %>
<%= render_slot(col, row) %>
+
+ """ + end + + attr(:event, :string, required: true) + attr(:offset, :integer, required: true) + attr(:count, :integer, required: true) + attr(:page, :integer, required: true) + attr(:unit, :string, default: nil) + attr(:total, :integer, default: nil) + + def pager(assigns) do + assigns = + assigns + |> assign(:from, if(assigns.count == 0, do: 0, else: assigns.offset + 1)) + |> assign(:to, assigns.offset + assigns.count) + |> assign(:range_unit, format_unit(assigns.unit, assigns.count)) + |> assign(:next_disabled, next_disabled?(assigns)) + + ~H""" +
+
+ Nothing to show + 0}> + Showing <%= @from %>-<%= @to %><%= if @range_unit, + do: " " <> @range_unit %> + <%= total_text(@total, @unit) %> + +
+
+ + +
+
+ """ + end + + def format_count(count) do + count + |> Integer.to_string() + |> String.replace(~r/\B(?=(\d{3})+(?!\d))/, ",") + end + + def format_unit(nil, _count), do: nil + def format_unit(unit, 1), do: singular_unit(unit) + def format_unit(unit, _count), do: unit + + defp next_disabled?(%{count: count, page: page}) when count < page, do: true + + defp next_disabled?(%{total: total, offset: offset, count: count}) when not is_nil(total), + do: offset + count >= total + + defp next_disabled?(_assigns), do: false + + defp total_text(nil, _unit), do: nil + + defp total_text(total, unit) do + unit = if unit, do: " " <> format_unit(unit, total), else: "" + " of #{format_count(total)}#{unit}" + end + + defp singular_unit(unit) do + if String.ends_with?(unit, "s") do + String.trim_trailing(unit, "s") + else + unit + end + end + + attr(:state, :any, required: true) + + def state_badge(assigns) do + assigns = assign(assigns, :state_name, to_string(assigns.state)) + + ~H""" + + + <%= @state_name %> + + """ + end + + attr(:state, :any, required: true) + + def state_dot(assigns) do + assigns = assign(assigns, :state_name, to_string(assigns.state)) + + ~H""" + + + """ + end + + attr(:cat, :string, required: true) + attr(:size, :integer, default: 15) + + def cat_glyph(assigns) do + assigns = assign(assigns, :icon, cat_icon(assigns.cat)) + + ~H""" + + <.icon name={@icon} size={@size} /> + + """ + end + + attr(:cat, :string, required: true) + attr(:module, :string, required: true) + + def module_cell(assigns) do + ~H""" +
+ <.cat_glyph cat={@cat} /> + <%= @module %> +
+ """ + end + + attr(:name, :string, required: true) + attr(:value, :string, default: "") + attr(:placeholder, :string, required: true) + + def search_box(assigns) do + ~H""" +
+ +
+ """ + end + + attr(:name, :string, required: true) + attr(:label, :string, required: true) + attr(:value, :string, default: "") + attr(:options, :list, required: true) + attr(:disabled, :boolean, default: false) + + def filter_select(assigns) do + ~H""" + + """ + end + + attr(:name, :string, required: true) + attr(:value, :string, default: "") + attr(:placeholder, :string, required: true) + attr(:disabled, :boolean, default: false) + + def filter_text(assigns) do + ~H""" +
+ +
+ """ + end + + defp icon_paths("search"), do: ["M21 21l-4.34-4.34M19 11a8 8 0 11-16 0 8 8 0 0116 0z"] + defp icon_paths("check"), do: ["M4.5 12.75l6 6 9-13.5"] + defp icon_paths("x"), do: ["M6 18L18 6M6 6l12 12"] + defp icon_paths("bolt"), do: ["M3.75 13.5l10.5-11.25-1.5 9h6.75L9 22.5l1.5-9h-6.75z"] + defp icon_paths("clock"), do: ["M12 6v6l4 2m6-2a9 9 0 11-18 0 9 9 0 0118 0z"] + defp icon_paths("menu"), do: icon_paths("queue") + defp icon_paths("queue"), do: ["M3.75 6.75h16.5M3.75 12h16.5M3.75 17.25h16.5"] + + defp icon_paths("box"), + do: [ + "M21 7.5l-9-5.25L3 7.5m18 0l-9 5.25m9-5.25v9l-9 5.25M3 7.5l9 5.25M3 7.5v9l9 5.25m0-9v9" + ] + + defp icon_paths("cube"), do: icon_paths("box") + + defp icon_paths("docker"), + do: [ + "M3 13.5h18M5.25 13.5V9.75h3v3.75m1.5 0V6.75h3v6.75m1.5 0V9.75h3v3.75M3 13.5c0 4 3 6.75 7.5 6.75 6 0 9-3.75 9.75-6.75" + ] + + defp icon_paths("refresh"), + do: [ + "M16.023 9.348h4.992V4.356M3.75 9.348a8.25 8.25 0 0113.803-3.7L20.25 8.4M3.985 14.652a8.25 8.25 0 0013.803 3.7L20.25 15.6m0 4.8v-4.99h-4.992" + ] + + defp icon_paths("chevL"), do: ["M15.75 19.5L8.25 12l7.5-7.5"] + defp icon_paths("chevR"), do: ["M8.25 4.5l7.5 7.5-7.5 7.5"] + defp icon_paths("chevD"), do: ["M19.5 8.25l-7.5 7.5-7.5-7.5"] + + defp icon_paths("external"), + do: [ + "M13.5 6H5.25A2.25 2.25 0 003 8.25v10.5A2.25 2.25 0 005.25 21h10.5A2.25 2.25 0 0018 18.75V10.5m-10.5 6L21 3m0 0h-5.25M21 3v5.25" + ] + + defp icon_paths(_name), do: [] + + defp row_class(%{state: "failed"}), do: "row--failed" + defp row_class(_row), do: nil + + defp state_badge_class("done"), do: "badge--success" + defp state_badge_class("failed"), do: "badge--danger" + defp state_badge_class("running"), do: "badge--running" + defp state_badge_class(_state), do: "badge--neutral" + + defp cat_icon("build"), do: "cube" + defp cat_icon("docker"), do: "docker" + defp cat_icon(_cat), do: "refresh" +end diff --git a/lib/bob_web/components/layouts.ex b/lib/bob_web/components/layouts.ex new file mode 100644 index 00000000..d15516c1 --- /dev/null +++ b/lib/bob_web/components/layouts.ex @@ -0,0 +1,24 @@ +defmodule BobWeb.Layouts do + use BobWeb, :html + + def nav_class(current_path, target_path) do + [ + "bob-nav__tab", + nav_active?(current_path, target_path) && "bob-nav__tab--active" + ] + end + + def menu_class(current_path, target_path) do + [ + "bob-menu__item", + nav_active?(current_path, target_path) && "bob-menu__item--active" + ] + end + + defp nav_active?("/", "/"), do: true + defp nav_active?("/artifacts", "/artifacts"), do: true + defp nav_active?("/docker", "/docker"), do: true + defp nav_active?(_current_path, _target_path), do: false + + embed_templates("layouts/*") +end diff --git a/lib/bob_web/components/layouts/app.html.heex b/lib/bob_web/components/layouts/app.html.heex new file mode 100644 index 00000000..46dced18 --- /dev/null +++ b/lib/bob_web/components/layouts/app.html.heex @@ -0,0 +1,2 @@ + +<%= @inner_content %> diff --git a/lib/bob_web/components/layouts/root.html.heex b/lib/bob_web/components/layouts/root.html.heex new file mode 100644 index 00000000..2c16711c --- /dev/null +++ b/lib/bob_web/components/layouts/root.html.heex @@ -0,0 +1,83 @@ + + + + + + + Bob + + + + + + + +
+
+
+ <.link href={~p"/"} class="bob-brand"> + + Bob + Hex build server + + + + + + +
+ + <.icon name="menu" /> + Menu + + +
+
+
+ +
+
+ <%= @inner_content %> +
+
+
+ + diff --git a/lib/bob_web/controllers/artifact_controller.ex b/lib/bob_web/controllers/artifact_controller.ex new file mode 100644 index 00000000..d06deb84 --- /dev/null +++ b/lib/bob_web/controllers/artifact_controller.ex @@ -0,0 +1,22 @@ +defmodule BobWeb.ArtifactController do + use BobWeb, :controller + + def add(conn, params) do + Bob.Artifacts.add(%{ + kind: params["kind"], + arch: params["arch"], + os: params["os"], + name: params["name"], + ref: params["ref"], + sha256: params["sha256"], + built_at: params["date"] + }) + + send_resp(conn, 204, "") + end + + def add_docker(conn, params) do + Bob.Artifacts.add_docker_tag(params.repo, params.tag, params.archs) + send_resp(conn, 204, "") + end +end diff --git a/lib/bob_web/controllers/error_html.ex b/lib/bob_web/controllers/error_html.ex new file mode 100644 index 00000000..03a99172 --- /dev/null +++ b/lib/bob_web/controllers/error_html.ex @@ -0,0 +1,7 @@ +defmodule BobWeb.ErrorHTML do + use BobWeb, :html + + def render(template, _assigns) do + Phoenix.Controller.status_message_from_template(template) + end +end diff --git a/lib/bob_web/controllers/error_json.ex b/lib/bob_web/controllers/error_json.ex new file mode 100644 index 00000000..71d816d7 --- /dev/null +++ b/lib/bob_web/controllers/error_json.ex @@ -0,0 +1,5 @@ +defmodule BobWeb.ErrorJSON do + def render(template, _assigns) do + %{errors: %{detail: Phoenix.Controller.status_message_from_template(template)}} + end +end diff --git a/lib/bob_web/controllers/queue_controller.ex b/lib/bob_web/controllers/queue_controller.ex new file mode 100644 index 00000000..151a065a --- /dev/null +++ b/lib/bob_web/controllers/queue_controller.ex @@ -0,0 +1,29 @@ +defmodule BobWeb.QueueController do + use BobWeb, :controller + + def start(conn, params) do + jobs = + params.jobs + |> Bob.RemoteQueue.prioritize() + |> Bob.RemoteQueue.start_jobs(params.max_weight, params.weights) + + conn + |> put_resp_header("content-type", "application/vnd.bob+erlang") + |> send_resp(200, Bob.Plug.ErlangFormat.encode_to_iodata!(%{jobs: jobs})) + end + + def success(conn, params) do + Bob.Queue.success(params.id) + send_resp(conn, 204, "") + end + + def failure(conn, params) do + Bob.Queue.failure(params.id) + send_resp(conn, 204, "") + end + + def add(conn, params) do + Bob.Queue.add(params.module, params.args) + send_resp(conn, 204, "") + end +end diff --git a/lib/bob_web/endpoint.ex b/lib/bob_web/endpoint.ex new file mode 100644 index 00000000..ccc55703 --- /dev/null +++ b/lib/bob_web/endpoint.ex @@ -0,0 +1,47 @@ +defmodule BobWeb.Endpoint do + use Phoenix.Endpoint, otp_app: :bob + use Sentry.PlugCapture + + @session_options [ + store: :cookie, + key: "_bob_key", + signing_salt: "9kLm2Qx7", + same_site: "Lax" + ] + + socket("/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]) + + plug(Bob.Plug.Forwarded) + plug(Bob.Plug.Status) + + plug(Plug.Static, + at: "/", + from: :bob, + gzip: Mix.env() != :dev, + only: BobWeb.static_paths() + ) + + if Mix.env() == :dev do + plug(Tidewave) + end + + if code_reloading? do + socket("/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket) + plug(Phoenix.LiveReloader) + plug(Phoenix.CodeReloader) + end + + plug(Plug.RequestId) + plug(Logster.Plugs.Logger, excludes: [:params]) + plug(BobWeb.Plugs.Secret, api_only: true) + + plug(Plug.Parsers, + parsers: [:json, Bob.Plug.Parser], + pass: ["application/json", "application/vnd.bob+erlang"], + json_decoder: JSON + ) + + plug(Sentry.PlugContext) + plug(Plug.Session, @session_options) + plug(BobWeb.Router) +end diff --git a/lib/bob_web/live/artifacts_live.ex b/lib/bob_web/live/artifacts_live.ex new file mode 100644 index 00000000..656c52df --- /dev/null +++ b/lib/bob_web/live/artifacts_live.ex @@ -0,0 +1,156 @@ +defmodule BobWeb.ArtifactsLive do + use BobWeb, :live_view + + @page 100 + + @impl true + def mount(_params, _session, socket) do + socket = + socket + |> assign( + query: "", + kind: "", + arch: "", + os: "", + offset: 0, + page: @page, + kinds: [], + arches: [], + oses: [], + total: nil, + loading: true, + results: [] + ) + + socket = if connected?(socket), do: socket |> load_options() |> load(), else: socket + + {:ok, socket} + end + + @impl true + def handle_event("search", params, socket) do + socket = + socket + |> assign( + query: params["query"] || "", + kind: params["kind"] || "", + arch: params["arch"] || "", + os: params["os"] || "", + offset: 0 + ) + |> load() + + {:noreply, socket} + end + + def handle_event("page", %{"dir" => dir}, socket) do + offset = max(socket.assigns.offset + step(dir), 0) + {:noreply, socket |> assign(offset: offset) |> load()} + end + + defp step("next"), do: @page + defp step("prev"), do: -@page + + defp load_options(socket) do + assign(socket, + kinds: Bob.Artifacts.distinct_kinds(), + arches: Bob.Artifacts.distinct_arches(), + oses: Bob.Artifacts.distinct_oses() + ) + end + + defp load(socket) do + %{query: q, kind: kind, arch: arch, os: os, offset: offset} = socket.assigns + + filters = %{query: q, kind: kind, arch: arch, os: os} + + results = + Bob.Artifacts.search_artifacts(filters, @page, offset) + + assign(socket, + results: results, + total: Bob.Artifacts.count_artifacts(filters), + loading: false + ) + end + + defp fmt(nil), do: "—" + defp fmt(datetime), do: Calendar.strftime(datetime, "%Y-%m-%d %H:%M:%S") + + defp truncate(nil, _length), do: nil + + defp truncate(value, length) when byte_size(value) > length do + binary_part(value, 0, length) <> "..." + end + + defp truncate(value, _length), do: value + + @impl true + def render(assigns) do + ~H""" +
+
+
+

Build artifacts

+
+
+ +
+
+ <.search_box name="query" value={@query} placeholder="Search name or ref..." /> + <.filter_select name="kind" label="kind" value={@kind} options={@kinds} /> + <.filter_select name="arch" label="arch" value={@arch} options={@arches} /> + <.filter_select name="os" label="os" value={@os} options={@oses} /> + <%= artifact_count_label(@total) %> +
+ +
Loading artifacts...
+ + <.table :if={!@loading and @results != []} rows={@results} class="jt--art"> + <:col :let={a} label="kind"> + <%= a.kind %> + + <:col :let={a} label="arch"> + <%= a.arch %> + + <:col :let={a} label="os"> + <%= a.os %> + + <:col :let={a} label="name"> + <%= a.name %> + + <:col :let={a} label="ref"> + <%= truncate(a.ref, 12) %> + + <:col :let={a} label="sha256"> + + <%= truncate(a.sha256, 14) %> + + — + + <:col :let={a} label="built" class="col-time"> + <%= fmt(a.built_at) %> + + +
No matching artifacts.
+ + <.pager + :if={!@loading} + event="page" + offset={@offset} + count={length(@results)} + page={@page} + unit="artifacts" + total={@total} + /> +
+
+ """ + end + + defp artifact_count_label(nil), do: "Loading artifacts" + + defp artifact_count_label(count) do + "#{format_count(count)} #{format_unit("artifacts", count)}" + end +end diff --git a/lib/bob_web/live/docker_tags_live.ex b/lib/bob_web/live/docker_tags_live.ex new file mode 100644 index 00000000..adf865c1 --- /dev/null +++ b/lib/bob_web/live/docker_tags_live.ex @@ -0,0 +1,195 @@ +defmodule BobWeb.DockerTagsLive do + use BobWeb, :live_view + + @page 100 + + @impl true + def mount(_params, _session, socket) do + options = Bob.Artifacts.docker_tag_filter_options() + + socket = + socket + |> assign( + repo: "", + tag: "", + arch: "", + elixir_version: "", + erlang_version: "", + os: "", + os_version: "", + offset: 0, + page: @page, + repos: options.repos, + arches: options.arches, + oses: options.oses, + total: nil, + loading: true, + results: [] + ) + + socket = if connected?(socket), do: load(socket), else: socket + + {:ok, socket} + end + + @impl true + def handle_event("search", params, socket) do + tag = params["tag"] || "" + + socket = + socket + |> assign( + repo: params["repo"] || "", + tag: tag, + arch: params["arch"] || "", + elixir_version: structured_param(params, "elixir_version", tag), + erlang_version: structured_param(params, "erlang_version", tag), + os: structured_param(params, "os", tag), + os_version: structured_param(params, "os_version", tag), + offset: 0 + ) + |> load() + + {:noreply, socket} + end + + def handle_event("page", %{"dir" => dir}, socket) do + offset = max(socket.assigns.offset + step(dir), 0) + {:noreply, socket |> assign(offset: offset) |> load()} + end + + defp step("next"), do: @page + defp step("prev"), do: -@page + + defp structured_param(_params, _key, tag) when tag != "", do: "" + defp structured_param(params, key, _tag), do: params[key] || "" + + defp load(socket) do + %{ + repo: repo, + tag: tag, + arch: arch, + elixir_version: elixir_version, + erlang_version: erlang_version, + os: os, + os_version: os_version, + offset: offset + } = socket.assigns + + filters = %{ + repo: repo, + tag: tag, + arch: arch, + elixir_version: elixir_version, + erlang_version: erlang_version, + os: os, + os_version: os_version + } + + results = + Bob.Artifacts.search_docker_tags(filters, @page, offset) + + total = + if offset == 0 and results == [] do + 0 + else + filters + |> Bob.Artifacts.count_docker_tags() + # The page and count are separate queries, so keep the pager coherent if + # tags change between them. + |> max(offset + length(results)) + end + + assign(socket, results: results, total: total, loading: false) + end + + defp fmt(nil), do: "—" + defp fmt(datetime), do: Calendar.strftime(datetime, "%Y-%m-%d %H:%M:%S") + + defp count_label(nil), do: "Loading tags" + defp count_label(0), do: "0 tags" + defp count_label(count), do: "#{format_count(count)} #{format_unit("tags", count)}" + + @impl true + def render(assigns) do + ~H""" +
+
+
+

Docker tags

+
+
+ +
+
+ <.filter_select name="repo" label="repo" value={@repo} options={@repos} /> + <.filter_text name="tag" value={@tag} placeholder="tag prefix..." /> + or +
+ <.filter_text + name="elixir_version" + value={@elixir_version} + placeholder="Elixir" + disabled={@tag != ""} + /> + <.filter_text + name="erlang_version" + value={@erlang_version} + placeholder="Erlang" + disabled={@tag != ""} + /> + <.filter_select + name="os" + label="os" + value={@os} + options={@oses} + disabled={@tag != ""} + /> + <.filter_text + name="os_version" + value={@os_version} + placeholder="OS version" + disabled={@tag != ""} + /> +
+ <.filter_select name="arch" label="arch" value={@arch} options={@arches} /> + <%= count_label(@total) %> +
+ +
Loading tags...
+ + <.table :if={!@loading and @results != []} rows={@results} class="jt--dk"> + <:col :let={d} label="repo" class="col-dk-repo"> +
+ <.icon name="docker" class="icon-blue" /> + <%= d.repo %> +
+ + <:col :let={d} label="tag" class="col-dk-tag"> + <%= d.tag %> + + <:col :let={d} label="archs" class="col-dk-archs"> +
+ <%= arch %> +
+ + <:col :let={d} label="built" class="col-time"> + <%= fmt(d.built_at) %> + + +
No matching tags.
+ + <.pager + :if={!@loading} + event="page" + offset={@offset} + count={length(@results)} + page={@page} + unit="tags" + total={@total} + /> +
+
+ """ + end +end diff --git a/lib/bob_web/live/jobs_live.ex b/lib/bob_web/live/jobs_live.ex new file mode 100644 index 00000000..e81e7fe6 --- /dev/null +++ b/lib/bob_web/live/jobs_live.ex @@ -0,0 +1,266 @@ +defmodule BobWeb.JobsLive do + use BobWeb, :live_view + + @queued_page 100 + @past_page 50 + @debounce_ms 250 + @tick_ms 1000 + + @impl true + def mount(_params, _session, socket) do + if connected?(socket), do: Phoenix.PubSub.subscribe(Bob.PubSub, "jobs") + + socket = + socket + |> assign( + queued_offset: 0, + past_offset: 0, + refresh_scheduled: false, + queued_page: @queued_page, + past_page: @past_page, + loading: true, + now: DateTime.utc_now(), + tick_scheduled: false, + running: [], + queue_sizes: [], + queue_total: 0, + queued: [], + past: [], + past_total: 0 + ) + + socket = if connected?(socket), do: load(socket), else: socket + socket = if connected?(socket), do: schedule_tick(socket), else: socket + + {:ok, socket} + end + + @impl true + def handle_info(:jobs_changed, %{assigns: %{refresh_scheduled: true}} = socket) do + {:noreply, socket} + end + + def handle_info(:jobs_changed, socket) do + Process.send_after(self(), :refresh, @debounce_ms) + {:noreply, assign(socket, refresh_scheduled: true)} + end + + def handle_info(:refresh, socket) do + {:noreply, socket |> assign(refresh_scheduled: false) |> load() |> schedule_tick()} + end + + def handle_info(:tick, socket) do + socket = + socket + |> assign(now: DateTime.utc_now(), tick_scheduled: false) + |> schedule_tick() + + {:noreply, socket} + end + + @impl true + def handle_event("queued_page", %{"dir" => dir}, socket) do + offset = max(socket.assigns.queued_offset + step(dir, @queued_page), 0) + {:noreply, socket |> assign(queued_offset: offset) |> load()} + end + + def handle_event("past_page", %{"dir" => dir}, socket) do + offset = max(socket.assigns.past_offset + step(dir, @past_page), 0) + {:noreply, socket |> assign(past_offset: offset) |> load()} + end + + defp step("next", page), do: page + defp step("prev", page), do: -page + + defp load(socket) do + queue_sizes = + Bob.Queue.queue_sizes() + |> Enum.sort_by(fn {_mod, count} -> count end, :desc) + + assign(socket, + running: Bob.Queue.running(), + now: DateTime.utc_now(), + queue_sizes: queue_sizes, + queue_total: Enum.sum(Enum.map(queue_sizes, fn {_mod, count} -> count end)), + queued: Bob.Queue.queued_listing(@queued_page, socket.assigns.queued_offset), + past: Bob.Queue.recent(@past_page, socket.assigns.past_offset), + past_total: Bob.Queue.finished_count(), + loading: false + ) + end + + defp schedule_tick(%{assigns: %{running: [], tick_scheduled: false}} = socket), do: socket + defp schedule_tick(%{assigns: %{tick_scheduled: true}} = socket), do: socket + + defp schedule_tick(socket) do + Process.send_after(self(), :tick, @tick_ms) + assign(socket, tick_scheduled: true) + end + + defp fmt(nil), do: "—" + defp fmt(datetime), do: Calendar.strftime(datetime, "%Y-%m-%d %H:%M:%S") + + defp fmt_duration(nil), do: "—" + + defp fmt_duration(seconds) when seconds < 60, do: "#{seconds}s" + + defp fmt_duration(seconds) when seconds < 3600 do + minutes = div(seconds, 60) + seconds = rem(seconds, 60) + "#{minutes}m #{seconds}s" + end + + defp fmt_duration(seconds) do + hours = div(seconds, 3600) + minutes = div(rem(seconds, 3600), 60) + "#{hours}h #{minutes}m" + end + + defp elapsed(datetime, now), do: duration(datetime, now) + + defp duration(%{started_at: started_at, finished_at: finished_at}), + do: duration(started_at, finished_at) + + defp duration(nil, _finished_at), do: nil + defp duration(_started_at, nil), do: nil + + defp duration(started_at, finished_at) do + NaiveDateTime.diff(to_naive(finished_at), to_naive(started_at)) + |> max(0) + end + + defp to_naive(%DateTime{} = datetime), do: DateTime.to_naive(datetime) + defp to_naive(%NaiveDateTime{} = datetime), do: datetime + + defp module_name(module_key), do: inspect(module_key) + defp args_text(args), do: inspect(args) + + defp job_cat(module_key) do + module = inspect(module_key) + + cond do + String.contains?(module, "Docker") -> "docker" + String.contains?(module, "Build") -> "build" + true -> "check" + end + end + + @impl true + def render(assigns) do + ~H""" +
+
+
+

Jobs

+
+
+ + Live - updates automatically +
+
+ + <.section + title="Running" + count={length(@running)} + live={@running != []} + icon="bolt" + icon_class={if @running == [], do: "icon-muted", else: "icon-blue"} + > +
Loading jobs...
+ + <.table :if={!@loading and @running != []} rows={@running}> + <:col :let={_j} label="State" class="col-state"> +
+ <.state_dot state="running" /> + running +
+ + <:col :let={j} label="Module"> + <.module_cell cat={job_cat(j.module_key)} module={module_name(j.module_key)} /> + + <:col :let={j} label="Args" class="col-args"> + <%= args_text(j.args) %> + + <:col :let={j} label="Started" class="col-time"> + <%= fmt(j.started_at) %> + + <:col :let={j} label="Elapsed" class="col-time"> + <%= fmt_duration(elapsed(j.started_at, @now)) %> + + +
Nothing running.
+ + + <.section title="Queue" count={@queue_total} icon="queue"> +
Loading queue...
+ +
+
Queued by job type
+
+
+ + <.cat_glyph cat={job_cat(mod)} size={14} /> + <%= module_name(mod) %> + + <%= count %> +
+
+
+ <.table :if={!@loading and @queued != []} rows={@queued}> + <:col :let={j} label="Module"> + <.module_cell cat={job_cat(j.module_key)} module={module_name(j.module_key)} /> + + <:col :let={j} label="Args" class="col-args"> + <%= args_text(j.args) %> + + <:col :let={j} label="Queued" class="col-time"> + <%= fmt(j.inserted_at) %> + + +
Queue is empty.
+ <.pager + :if={!@loading} + event="queued_page" + offset={@queued_offset} + count={length(@queued)} + page={@queued_page} + unit="queued" + total={@queue_total} + /> + + + <.section title="Past" count={@past_total} icon="clock"> +
Loading finished jobs...
+ + <.table :if={!@loading and @past != []} rows={@past}> + <:col :let={j} label="State" class="col-state"> + <.state_badge state={j.state} /> + + <:col :let={j} label="Module"> + <.module_cell cat={job_cat(j.module_key)} module={module_name(j.module_key)} /> + + <:col :let={j} label="Args" class="col-args"> + <%= args_text(j.args) %> + + <:col :let={j} label="Duration" class="col-time"> + <%= fmt_duration(duration(j)) %> + + <:col :let={j} label="Finished" class="col-time"> + <%= fmt(j.finished_at) %> + + +
No finished jobs.
+ <.pager + :if={!@loading} + event="past_page" + offset={@past_offset} + count={length(@past)} + page={@past_page} + unit="jobs" + total={@past_total} + /> + +
+ """ + end +end diff --git a/lib/bob_web/plugs/secret.ex b/lib/bob_web/plugs/secret.ex new file mode 100644 index 00000000..58fb79f1 --- /dev/null +++ b/lib/bob_web/plugs/secret.ex @@ -0,0 +1,37 @@ +defmodule BobWeb.Plugs.Secret do + import Plug.Conn + + def init(opts), do: opts + + def call(%{path_info: ["api" | _]} = conn, _opts) do + authenticate(conn) + end + + def call(conn, opts) when is_list(opts) do + if Keyword.get(opts, :api_only, false) do + conn + else + authenticate(conn) + end + end + + defp authenticate(conn) do + secret = Application.get_env(:bob, :agent_secret) + + if authorized?(get_req_header(conn, "authorization"), secret) do + conn + else + conn + |> send_resp(401, "") + |> halt() + end + end + + defp authorized?([authorization], secret) + when is_binary(authorization) and is_binary(secret) and byte_size(secret) > 0 and + byte_size(authorization) == byte_size(secret) do + Plug.Crypto.secure_compare(authorization, secret) + end + + defp authorized?(_headers, _secret), do: false +end diff --git a/lib/bob_web/router.ex b/lib/bob_web/router.ex new file mode 100644 index 00000000..075c92c3 --- /dev/null +++ b/lib/bob_web/router.ex @@ -0,0 +1,29 @@ +defmodule BobWeb.Router do + use BobWeb, :router + + pipeline :browser do + plug(:accepts, ["html"]) + plug(:fetch_session) + plug(:fetch_live_flash) + plug(:put_root_layout, html: {BobWeb.Layouts, :root}) + plug(:protect_from_forgery) + plug(:put_secure_browser_headers) + end + + scope "/api", BobWeb do + post("/queue/start", QueueController, :start) + post("/queue/success", QueueController, :success) + post("/queue/failure", QueueController, :failure) + post("/queue/add", QueueController, :add) + post("/artifacts/add", ArtifactController, :add) + post("/docker/add", ArtifactController, :add_docker) + end + + scope "/", BobWeb do + pipe_through(:browser) + + live("/", JobsLive) + live("/artifacts", ArtifactsLive) + live("/docker", DockerTagsLive) + end +end diff --git a/mix.exs b/mix.exs index 12f6d9e9..1526aa62 100644 --- a/mix.exs +++ b/mix.exs @@ -8,6 +8,7 @@ defmodule Bob.Mixfile do elixir: "~> 1.18", start_permanent: Mix.env() == :prod, elixirc_paths: elixirc_paths(Mix.env()), + listeners: [Phoenix.CodeReloader], releases: releases(), aliases: aliases(), deps: deps() @@ -26,24 +27,37 @@ defmodule Bob.Mixfile do defp deps() do [ + {:tidewave, "~> 0.5", only: [:dev]}, {:ecto_sql, "~> 3.12"}, {:ex_aws_s3, "~> 2.0"}, {:finch, "~> 0.19"}, {:req, "~> 0.5"}, - {:plug_cowboy, "~> 2.0"}, {:porcelain, "~> 2.0"}, {:postgrex, "~> 0.19"}, {:sentry, "~> 10.2"}, {:sweet_xml, "~> 0.5"}, {:logster, "~> 1.0"}, - {:observer_cli, "~> 1.7"} + {:observer_cli, "~> 1.7"}, + {:phoenix_pubsub, "~> 2.1"}, + {:phoenix, "~> 1.7"}, + {:phoenix_html, "~> 4.1"}, + {:phoenix_live_view, "~> 1.0"}, + {:phoenix_live_reload, "~> 1.5", only: :dev}, + {:bandit, "~> 1.5"}, + {:esbuild, "~> 0.8", runtime: Mix.env() == :dev}, + {:tailwind, "~> 0.2", runtime: Mix.env() == :dev}, + {:igniter, "~> 0.8.1", only: [:dev, :test], runtime: false}, + {:lazy_html, ">= 0.1.0", only: :test} ] end defp aliases() do [ "ecto.setup": ["ecto.create", "ecto.migrate"], - "ecto.reset": ["ecto.drop", "ecto.setup"] + "ecto.reset": ["ecto.drop", "ecto.setup"], + "assets.setup": ["tailwind.install --if-missing", "esbuild.install --if-missing"], + "assets.build": ["tailwind bob", "esbuild bob"], + "assets.deploy": ["tailwind bob --minify", "esbuild bob --minify", "phx.digest"] ] end diff --git a/mix.lock b/mix.lock index e3d79562..cbbcc990 100644 --- a/mix.lock +++ b/mix.lock @@ -1,16 +1,24 @@ %{ - "cowboy": {:hex, :cowboy, "2.13.0", "09d770dd5f6a22cc60c071f432cd7cb87776164527f205c5a6b0f24ff6b38990", [:make, :rebar3], [{:cowlib, ">= 2.14.0 and < 3.0.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, ">= 1.8.0 and < 3.0.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "e724d3a70995025d654c1992c7b11dbfea95205c047d86ff9bf1cda92ddc5614"}, - "cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"}, - "cowlib": {:hex, :cowlib, "2.15.0", "3c97a318a933962d1c12b96ab7c1d728267d2c523c25a5b57b0f93392b6e9e25", [:make, :rebar3], [], "hexpm", "4f00c879a64b4fe7c8fcb42a4281925e9ffdb928820b03c3ad325a617e857532"}, + "bandit": {:hex, :bandit, "1.12.0", "6c5214daa2469644ac4ab0113b98abc24f75e348378e6a974c6343b3e5da22ef", [:mix], [{:hpax, "~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.18", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:thousand_island, "~> 1.5", [hex: :thousand_island, repo: "hexpm", optional: false]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "45dac82dc86f45cf4a196dee9cc5a8b791d9c9469d996055f055e6ee36c66e20"}, + "cc_precompiler": {:hex, :cc_precompiler, "0.1.11", "8c844d0b9fb98a3edea067f94f616b3f6b29b959b6b3bf25fee94ffe34364768", [:mix], [{:elixir_make, "~> 0.7", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "3427232caf0835f94680e5bcf082408a70b48ad68a5f5c0b02a3bea9f3a075b9"}, + "circular_buffer": {:hex, :circular_buffer, "1.0.1", "01f0e3d5fe945080692cf6521c0988e9dbb5dc312831cefe77e5b63a4e658160", [:mix], [], "hexpm", "7d4ece3137d49c1f8dd0b3e0aa7c484f4d83a0be5d4b516c282085c1d5f2d7b9"}, "db_connection": {:hex, :db_connection, "2.10.1", "d5465f6bcc125c1b8981c1dbf23c193ca16f446ec0b25832dc174f74f18be510", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "18ed94c6e627b4bf452dbd4df61b69a35a1e768525140bc1917b7a685026a6a3"}, - "decimal": {:hex, :decimal, "2.4.1", "6c0fbede12fb122ba685e9ab41c6a40c129e322b3aa192f9e072e61f3a6ffaf2", [:mix], [], "hexpm", "7e618897933a8455f19a727d7c5e50a2c071a544b700e5e724298ecb4340187f"}, + "decimal": {:hex, :decimal, "3.1.1", "430d87b04011ce6cbd4fd205be758311a81f87d552d40904abd00f015935b1d0", [:mix], [], "hexpm", "c5f25f2ced74a0587d03e6023f595db8e924c9d3922c8c8ffd9edfc4498cf1f6"}, "ecto": {:hex, :ecto, "3.13.6", "352135b474f91d1ab99a1b502171d207e9db60421c9e3d0ecab4c7ab96b24d14", [:mix], [{:decimal, "~> 2.0 or ~> 3.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "8afa059bc16cd2c94739ec0a11e3e5df69d828125119109bef35f20a21a76af2"}, "ecto_sql": {:hex, :ecto_sql, "3.13.5", "2f8282b2ad97bf0f0d3217ea0a6fff320ead9e2f8770f810141189d182dc304e", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.13.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.7", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.19 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "aa36751f4e6a2b56ae79efb0e088042e010ff4935fc8684e74c23b1f49e25fdc"}, + "elixir_make": {:hex, :elixir_make, "0.9.0", "6484b3cd8c0cee58f09f05ecaf1a140a8c97670671a6a0e7ab4dc326c3109726", [:mix], [], "hexpm", "db23d4fd8b757462ad02f8aa73431a426fe6671c80b200d9710caf3d1dd0ffdb"}, + "esbuild": {:hex, :esbuild, "0.10.0", "b0aa3388a1c23e727c5a3e7427c932d89ee791746b0081bbe56103e9ef3d291f", [:mix], [{:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "468489cda427b974a7cc9f03ace55368a83e1a7be12fba7e30969af78e5f8c70"}, + "ex_ast": {:hex, :ex_ast, "0.12.0", "052ad63711da41b7efbfb3490dbf3d757bb67caec17d02f6deb0db4a0363e5f6", [:mix], [{:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.7", [hex: :sourceror, repo: "hexpm", optional: false]}], "hexpm", "66b4797f157d32f0a63c6da227515f78816c0ac8f621f6d7a2b22108e7b4dd85"}, "ex_aws": {:hex, :ex_aws, "2.5.10", "d3f8ca8959dad6533a2a934dfdf380df1b1bef425feeb215a47a5176dee8736c", [:mix], [{:configparser_ex, "~> 5.0", [hex: :configparser_ex, repo: "hexpm", optional: true]}, {:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}, {:jsx, "~> 2.8 or ~> 3.0", [hex: :jsx, repo: "hexpm", optional: true]}, {:mime, "~> 1.2 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:req, "~> 0.5.10 or ~> 0.6 or ~> 1.0", [hex: :req, repo: "hexpm", optional: true]}, {:sweet_xml, "~> 0.7", [hex: :sweet_xml, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "88fcd9cc1b2e0fcea65106bdaa8340ac56c6e29bf72f46cf7ef174027532d3da"}, "ex_aws_s3": {:hex, :ex_aws_s3, "2.5.7", "e571424d2f345299753382f3a01b005c422b1a460a8bc3ed47659b3d3ef91e9e", [:mix], [{:ex_aws, "~> 2.0", [hex: :ex_aws, repo: "hexpm", optional: false]}, {:sweet_xml, ">= 0.0.0", [hex: :sweet_xml, repo: "hexpm", optional: true]}], "hexpm", "858e51241e50181e29aa2bc128fef548873a3a9cd580471f57eda5b64dec937f"}, + "file_system": {:hex, :file_system, "1.1.1", "31864f4685b0148f25bd3fbef2b1228457c0c89024ad67f7a81a3ffbc0bbad3a", [:mix], [], "hexpm", "7a15ff97dfe526aeefb090a7a9d3d03aa907e100e262a0f8f7746b78f8f87a5d"}, "finch": {:hex, :finch, "0.22.0", "5c48fa6f9706a78eb9036cacb67b8b996b4e66d111c543f4c29bb0f879a6806b", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.8", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 1.1", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "b94e83c47780fc6813f746a1f1a34ee65cda42da4c5ea26a68f0acc4498e23dc"}, + "fine": {:hex, :fine, "0.1.6", "4bf7151493443c454aac9f2fa2f34f5fefd0346a83fb5586a016c4a135c63247", [:mix], [], "hexpm", "5638eb4495488e885ebec167fa57973e5c35e1a50c344eb7666c90ec1c4e3b12"}, + "glob_ex": {:hex, :glob_ex, "0.1.11", "cb50d3f1ef53f6ca04d6252c7fde09fd7a1cf63387714fe96f340a1349e62c93", [:mix], [], "hexpm", "342729363056e3145e61766b416769984c329e4378f1d558b63e341020525de4"}, "hpax": {:hex, :hpax, "1.0.3", "ed67ef51ad4df91e75cc6a1494f851850c0bd98ebc0be6e81b026e765ee535aa", [:mix], [], "hexpm", "8eab6e1cfa8d5918c2ce4ba43588e894af35dbd8e91e6e55c817bca5847df34a"}, - "jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"}, + "igniter": {:hex, :igniter, "0.8.1", "3c6ea47f3a6031015e29da8b4ba5c685f0a2e409facf63041fd83e982ca3aa89", [:mix], [{:ex_ast, "~> 0.5", [hex: :ex_ast, repo: "hexpm", optional: false]}, {:glob_ex, "~> 0.1.7", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:jason, "~> 1.4.5", [hex: :jason, repo: "hexpm", optional: false]}, {:owl, "~> 0.11", [hex: :owl, repo: "hexpm", optional: false]}, {:phx_new, "~> 1.7", [hex: :phx_new, repo: "hexpm", optional: true]}, {:req, "~> 0.5", [hex: :req, repo: "hexpm", optional: false]}, {:rewrite, ">= 1.1.1 and < 2.0.0-0", [hex: :rewrite, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.4", [hex: :sourceror, repo: "hexpm", optional: false]}, {:spitfire, ">= 0.1.3 and < 1.0.0-0", [hex: :spitfire, repo: "hexpm", optional: false]}], "hexpm", "d99472e6daf3bfc3675d699c6c7ace9196f377207aab83e09d7b95e9d90e8ae8"}, + "jason": {:hex, :jason, "1.4.5", "2e3a008590b0b8d7388c20293e9dcc9cf3e5d642fd2a114e4cbbb52e595d940a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0 or ~> 3.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "b0c823996102bcd0239b3c2444eb00409b72f6a140c1950bc8b457d836b30684"}, + "lazy_html": {:hex, :lazy_html, "0.1.11", "136c8e9cd616b4f4e9c1562daa683880891120b759606dc4c3b6b18058ba5d79", [:make, :mix], [{:cc_precompiler, "~> 0.1", [hex: :cc_precompiler, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.9.0", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:fine, "~> 0.1.0", [hex: :fine, repo: "hexpm", optional: false]}], "hexpm", "3b1be592929c31eca1a21673d25696e5c14cddfe922d9d1a3e3b48be4163883b"}, "logster": {:hex, :logster, "1.1.1", "d6fddac540dd46adde0c894024500867fe63b0043713f842c62da5815e21db10", [:mix], [{:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "d18e852c430812ad1c9756998ebe46ec814c724e6eb551a512d7e3f8dee24cef"}, "mime": {:hex, :mime, "2.0.7", "b8d739037be7cd402aee1ba0306edfdef982687ee7e9859bee6198c1e7e2f128", [:mix], [], "hexpm", "6171188e399ee16023ffc5b76ce445eb6d9672e2e241d2df6050f3c771e80ccd"}, "mint": {:hex, :mint, "1.9.0", "d6f534c2a3e98b2a8cc749b4796eb77e9e3af79a76f96e4c74035a827de0d318", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1 or ~> 0.2.0 or ~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "007154c7d8c43916aed3c93afd1f11aebbaa9c5ff4b7ba55ebe0d17ee0296042"}, @@ -18,15 +26,29 @@ "nimble_ownership": {:hex, :nimble_ownership, "1.0.1", "f69fae0cdd451b1614364013544e66e4f5d25f36a2056a9698b793305c5aa3a6", [:mix], [], "hexpm", "3825e461025464f519f3f3e4a1f9b68c47dc151369611629ad08b636b73bb22d"}, "nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"}, "observer_cli": {:hex, :observer_cli, "1.8.5", "6771e73560432049891b7737b0edce67ef28aa7973a8dd2b39b21da8c1227386", [:mix, :rebar3], [{:recon, "~> 2.5.6", [hex: :recon, repo: "hexpm", optional: false]}], "hexpm", "4252f4af00b48b51718ef8ccd9da5ce879cdfc8da66debaa9d471fc9bf099f59"}, - "plug": {:hex, :plug, "1.18.1", "5067f26f7745b7e31bc3368bc1a2b818b9779faa959b49c934c17730efc911cf", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "57a57db70df2b422b564437d2d33cf8d33cd16339c1edb190cd11b1a3a546cc2"}, - "plug_cowboy": {:hex, :plug_cowboy, "2.7.4", "729c752d17cf364e2b8da5bdb34fb5804f56251e88bb602aff48ae0bd8673d11", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "9b85632bd7012615bae0a5d70084deb1b25d2bcbb32cab82d1e9a1e023168aa3"}, + "owl": {:hex, :owl, "0.13.1", "1ec4a5dea170465f0e90c502c203079224516bc0cbd599281c8667b3c6ef8848", [:mix], [{:ucwidth, "~> 0.2", [hex: :ucwidth, repo: "hexpm", optional: true]}], "hexpm", "351e768af8f2edc575cdaab1a5a2f6d6381be591758a026c701c703145508a0c"}, + "phoenix": {:hex, :phoenix, "1.8.7", "d8d755b4ff4b449f610223dd706b4ae64155cb720d3dc09c706c079ecea189e4", [:mix], [{:bandit, "~> 1.0", [hex: :bandit, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.7", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:websock_adapter, "~> 0.5.3", [hex: :websock_adapter, repo: "hexpm", optional: false]}], "hexpm", "47352f72d6ab31009ef77516b1b3a14745be97b54061fd458031b9d8294869d5"}, + "phoenix_html": {:hex, :phoenix_html, "4.3.0", "d3577a5df4b6954cd7890c84d955c470b5310bb49647f0a114a6eeecc850f7ad", [:mix], [], "hexpm", "3eaa290a78bab0f075f791a46a981bbe769d94bc776869f4f3063a14f30497ad"}, + "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.6.2", "b18b0773a1ba77f28c52decbb0f10fd1ac4d3ae5b8632399bbf6986e3b665f62", [:mix], [{:file_system, "~> 0.2.10 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "d1f89c18114c50d394721365ffb428cce24f1c13de0467ffa773e2ff4a30d5b9"}, + "phoenix_live_view": {:hex, :phoenix_live_view, "1.1.31", "c45c85df509dd79c917bc530e26c71299e3920850f65ea52ab6a19ccee66875a", [:mix], [{:igniter, ">= 0.6.16 and < 1.0.0-0", [hex: :igniter, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:lazy_html, "~> 0.1.0", [hex: :lazy_html, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.6.15 or ~> 1.7.0 or ~> 1.8.0-rc", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 3.3 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.15", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2f53cc6a9e149f30449341c2775990819d97e3b22338fe719c4d30342e6f9638"}, + "phoenix_pubsub": {:hex, :phoenix_pubsub, "2.2.0", "ff3a5616e1bed6804de7773b92cbccfc0b0f473faf1f63d7daf1206c7aeaaa6f", [:mix], [], "hexpm", "adc313a5bf7136039f63cfd9668fde73bba0765e0614cba80c06ac9460ff3e96"}, + "phoenix_template": {:hex, :phoenix_template, "1.0.4", "e2092c132f3b5e5b2d49c96695342eb36d0ed514c5b252a77048d5969330d639", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "2c0c81f0e5c6753faf5cca2f229c9709919aba34fab866d3bc05060c9c444206"}, + "plug": {:hex, :plug, "1.19.2", "e4950525b22c6789dfb38a3f95d47171ba159da3fc5a33be9643b43d5e8adb98", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "b6fce20a56af5e60fa5dfecf3f907bb98ec981be43c79a3809a499bc3d133de0"}, "plug_crypto": {:hex, :plug_crypto, "2.1.1", "19bda8184399cb24afa10be734f84a16ea0a2bc65054e23a62bb10f06bc89491", [:mix], [], "hexpm", "6470bce6ffe41c8bd497612ffde1a7e4af67f36a15eea5f921af71cf3e11247c"}, "porcelain": {:hex, :porcelain, "2.0.3", "2d77b17d1f21fed875b8c5ecba72a01533db2013bd2e5e62c6d286c029150fdc", [:mix], [], "hexpm", "dc996ab8fadbc09912c787c7ab8673065e50ea1a6245177b0c24569013d23620"}, "postgrex": {:hex, :postgrex, "0.22.2", "4aec14df2a72722aee92492566edbeeb44e233ecb86b1915d03136297ef1385d", [:mix], [{:db_connection, "~> 2.9", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0 or ~> 3.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "8946382ddb06294f56026ac4278b3cc212bac8a2c82ed68b4087819ed1abc53b"}, - "ranch": {:hex, :ranch, "2.2.0", "25528f82bc8d7c6152c57666ca99ec716510fe0925cb188172f41ce93117b1b0", [:make, :rebar3], [], "hexpm", "fa0b99a1780c80218a4197a59ea8d3bdae32fbff7e88527d7d8a4787eff4f8e7"}, "recon": {:hex, :recon, "2.5.6", "9052588e83bfedfd9b72e1034532aee2a5369d9d9343b61aeb7fbce761010741", [:mix, :rebar3], [], "hexpm", "96c6799792d735cc0f0fd0f86267e9d351e63339cbe03df9d162010cefc26bb0"}, - "req": {:hex, :req, "0.6.0", "85dec26488ea23e29b4b516dc1fb9615b2f6f32fe824980d1adc9d79d44a83fd", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.21.0 or ~> 0.22.0", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 2.0.6 or ~> 2.1", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "f90e15671bfcc0c40ba26aaf825c47aec3f8b4f14f54a41bdd14e255fb67118e"}, + "req": {:hex, :req, "0.6.1", "7b904c8b42d0e08136a5c6aba024fd12fc79a1ed8856e7a3522b0917f7e75113", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.21.0 or ~> 0.22.0", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 2.0.6 or ~> 2.1", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "aaf11c9c80f2df2364630b3594e1857fe610d8ea7cb994e1ce3dcb55f204ff1c"}, + "rewrite": {:hex, :rewrite, "1.3.0", "67448ba7975690b35ba7e7f35717efcce317dbd5963cb0577aa7325c1923121a", [:mix], [{:glob_ex, "~> 0.1", [hex: :glob_ex, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.0", [hex: :sourceror, repo: "hexpm", optional: false]}, {:text_diff, "~> 0.1", [hex: :text_diff, repo: "hexpm", optional: false]}], "hexpm", "d111ac7ff3a58a802ef4f193bbd1831e00a9c57b33276e5068e8390a212714a5"}, "sentry": {:hex, :sentry, "10.10.0", "d058b635f3796947545c8057a42996f6dbefd12152da947209b56d16af41b161", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}, {:nimble_options, "~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_ownership, "~> 0.3.0 or ~> 1.0", [hex: :nimble_ownership, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.6", [hex: :phoenix, repo: "hexpm", optional: true]}, {:phoenix_live_view, "~> 0.20 or ~> 1.0", [hex: :phoenix_live_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.6", [hex: :plug, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "7c7ddd3cfdd63fcee53b1e28f9a653037e6927b2b1dbd300b7aeee9687c7a8f6"}, + "sourceror": {:hex, :sourceror, "1.12.0", "da354c5f35aad3cc1132f5d5b0d8437d865e2661c263260480bab51b5eedb437", [:mix], [], "hexpm", "755703683bd014ebcd5de9acc24b68fb874a660a568d1d63f8f98cd8a6ef9cd0"}, + "spitfire": {:hex, :spitfire, "0.3.13", "edd207b065eaec57acc5484097d0aa3e97fe4246168c54e67a9af040b8dee4c1", [:mix], [], "hexpm", "3601be88ceed4967b584e96444de3e1d12d6555ae0864a7390b9cd5332d134b4"}, "sweet_xml": {:hex, :sweet_xml, "0.7.5", "803a563113981aaac202a1dbd39771562d0ad31004ddbfc9b5090bdcd5605277", [:mix], [], "hexpm", "193b28a9b12891cae351d81a0cead165ffe67df1b73fe5866d10629f4faefb12"}, - "telemetry": {:hex, :telemetry, "1.3.0", "fedebbae410d715cf8e7062c96a1ef32ec22e764197f70cda73d82778d61e7a2", [:rebar3], [], "hexpm", "7015fc8919dbe63764f4b4b87a95b7c0996bd539e0d499be6ec9d7f3875b79e6"}, + "tailwind": {:hex, :tailwind, "0.4.1", "e7bcc222fe96a1e55f948e76d13dd84a1a7653fb051d2a167135db3b4b08d3e9", [:mix], [], "hexpm", "6249d4f9819052911120dbdbe9e532e6bd64ea23476056adb7f730aa25c220d1"}, + "telemetry": {:hex, :telemetry, "1.4.2", "a0cb522801dffb1c49fe6e30561badffc7b6d0e180db1300df759faa22062855", [:rebar3], [], "hexpm", "928f6495066506077862c0d1646609eed891a4326bee3126ba54b60af61febb1"}, + "text_diff": {:hex, :text_diff, "0.1.0", "1caf3175e11a53a9a139bc9339bd607c47b9e376b073d4571c031913317fecaa", [:mix], [], "hexpm", "d1ffaaecab338e49357b6daa82e435f877e0649041ace7755583a0ea3362dbd7"}, + "thousand_island": {:hex, :thousand_island, "1.5.0", "f50a213cac97262b6d5ebb85745aa2c00fec1413191e6e66834788d45425cecb", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "708923d40523e43cf99041ab37a0d4b0ec426ac6438fa3716ab23d919eaeb412"}, + "tidewave": {:hex, :tidewave, "0.5.6", "91f35540b5599640443f1d3a1c6166bf506e202840261a6344e384e8813c1f64", [:mix], [{:circular_buffer, "~> 0.4 or ~> 1.0", [hex: :circular_buffer, repo: "hexpm", optional: false]}, {:igniter, "~> 0.6", [hex: :igniter, repo: "hexpm", optional: true]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:phoenix_live_reload, ">= 1.6.1", [hex: :phoenix_live_reload, repo: "hexpm", optional: true]}, {:plug, "~> 1.17", [hex: :plug, repo: "hexpm", optional: false]}, {:req, "~> 0.5", [hex: :req, repo: "hexpm", optional: false]}], "hexpm", "dc82d52b8b6ffc04680544b17cd340c7d4166bb0d63999eb960850526866b533"}, + "websock": {:hex, :websock, "0.5.3", "2f69a6ebe810328555b6fe5c831a851f485e303a7c8ce6c5f675abeb20ebdadc", [:mix], [], "hexpm", "6105453d7fac22c712ad66fab1d45abdf049868f253cf719b625151460b8b453"}, + "websock_adapter": {:hex, :websock_adapter, "0.5.9", "43dc3ba6d89ef5dec5b1d0a39698436a1e856d000d84bf31a3149862b01a287f", [:mix], [{:bandit, ">= 0.6.0", [hex: :bandit, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.6", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "5534d5c9adad3c18a0f58a9371220d75a803bf0b9a3d87e6fe072faaeed76a08"}, } diff --git a/priv/repo/migrations/20260608120000_add_docker_tag_search_metadata.exs b/priv/repo/migrations/20260608120000_add_docker_tag_search_metadata.exs new file mode 100644 index 00000000..3c0be36b --- /dev/null +++ b/priv/repo/migrations/20260608120000_add_docker_tag_search_metadata.exs @@ -0,0 +1,90 @@ +defmodule Bob.Repo.Migrations.AddDockerTagSearchMetadata do + use Ecto.Migration + + @disable_ddl_transaction true + @disable_migration_lock true + @version_regex "^[0-9]+(\\.[0-9]+)*(-[0-9A-Za-z][0-9A-Za-z.-]*)?$" + + def up do + execute("ALTER TABLE docker_tags ADD COLUMN IF NOT EXISTS search jsonb NOT NULL DEFAULT '{}'::jsonb") + + execute( + "ALTER TABLE docker_tags_staging ADD COLUMN IF NOT EXISTS search jsonb NOT NULL DEFAULT '{}'::jsonb" + ) + + execute(""" + UPDATE docker_tags d + SET search = jsonb_build_object( + 'erlang_version', (parsed.m)[1], + 'os', (parsed.m)[2], + 'os_version', (parsed.m)[3] + ) + FROM ( + SELECT id, regexp_match(tag, '^(.+)-(alpine|ubuntu|debian)-(.+)$') AS m + FROM docker_tags + WHERE repo IN ('hexpm/erlang', 'hexpm/erlang-amd64', 'hexpm/erlang-arm64') + ) AS parsed + WHERE d.id = parsed.id + AND parsed.m IS NOT NULL + AND (parsed.m)[1] ~ '#{@version_regex}' + """) + + execute(""" + UPDATE docker_tags d + SET search = jsonb_build_object( + 'elixir_version', (parsed.m)[1], + 'erlang_version', (parsed.m)[2], + 'os', (parsed.m)[3], + 'os_version', (parsed.m)[4] + ) + FROM ( + SELECT id, regexp_match(tag, '^(.+)-erlang-(.+)-(alpine|ubuntu|debian)-(.+)$') AS m + FROM docker_tags + WHERE repo IN ('hexpm/elixir', 'hexpm/elixir-amd64', 'hexpm/elixir-arm64') + ) AS parsed + WHERE d.id = parsed.id + AND parsed.m IS NOT NULL + AND (parsed.m)[1] ~ '#{@version_regex}' + AND (parsed.m)[2] ~ '#{@version_regex}' + """) + + execute( + "CREATE INDEX CONCURRENTLY IF NOT EXISTS docker_tags_repo_prefix_index ON docker_tags (repo text_pattern_ops)" + ) + + execute( + "CREATE INDEX CONCURRENTLY IF NOT EXISTS docker_tags_tag_prefix_index ON docker_tags (tag text_pattern_ops)" + ) + + execute(""" + CREATE INDEX CONCURRENTLY IF NOT EXISTS docker_tags_elixir_version_prefix_index + ON docker_tags ((search->>'elixir_version') text_pattern_ops) + """) + + execute(""" + CREATE INDEX CONCURRENTLY IF NOT EXISTS docker_tags_erlang_version_prefix_index + ON docker_tags ((search->>'erlang_version') text_pattern_ops) + """) + + execute(""" + CREATE INDEX CONCURRENTLY IF NOT EXISTS docker_tags_os_prefix_index + ON docker_tags ((search->>'os') text_pattern_ops) + """) + + execute(""" + CREATE INDEX CONCURRENTLY IF NOT EXISTS docker_tags_os_version_prefix_index + ON docker_tags ((search->>'os_version') text_pattern_ops) + """) + end + + def down do + execute("DROP INDEX CONCURRENTLY IF EXISTS docker_tags_os_version_prefix_index") + execute("DROP INDEX CONCURRENTLY IF EXISTS docker_tags_os_prefix_index") + execute("DROP INDEX CONCURRENTLY IF EXISTS docker_tags_erlang_version_prefix_index") + execute("DROP INDEX CONCURRENTLY IF EXISTS docker_tags_elixir_version_prefix_index") + execute("DROP INDEX CONCURRENTLY IF EXISTS docker_tags_tag_prefix_index") + execute("DROP INDEX CONCURRENTLY IF EXISTS docker_tags_repo_prefix_index") + execute("ALTER TABLE docker_tags_staging DROP COLUMN IF EXISTS search") + execute("ALTER TABLE docker_tags DROP COLUMN IF EXISTS search") + end +end diff --git a/priv/repo/migrations/20260609014923_index_docker_tags_by_built_at.exs b/priv/repo/migrations/20260609014923_index_docker_tags_by_built_at.exs new file mode 100644 index 00000000..018ee3b9 --- /dev/null +++ b/priv/repo/migrations/20260609014923_index_docker_tags_by_built_at.exs @@ -0,0 +1,17 @@ +defmodule Bob.Repo.Migrations.IndexDockerTagsByBuiltAt do + use Ecto.Migration + + @disable_ddl_transaction true + @disable_migration_lock true + + def up do + execute(""" + CREATE INDEX CONCURRENTLY IF NOT EXISTS docker_tags_built_at_id_desc_index + ON docker_tags (built_at DESC, id DESC) + """) + end + + def down do + execute("DROP INDEX CONCURRENTLY IF EXISTS docker_tags_built_at_id_desc_index") + end +end diff --git a/priv/repo/migrations/20260609023000_index_docker_tags_archs.exs b/priv/repo/migrations/20260609023000_index_docker_tags_archs.exs new file mode 100644 index 00000000..1f509a2f --- /dev/null +++ b/priv/repo/migrations/20260609023000_index_docker_tags_archs.exs @@ -0,0 +1,14 @@ +defmodule Bob.Repo.Migrations.IndexDockerTagsArchs do + use Ecto.Migration + + @disable_ddl_transaction true + @disable_migration_lock true + + def up do + execute("CREATE INDEX CONCURRENTLY IF NOT EXISTS docker_tags_archs_gin_index ON docker_tags USING GIN (archs)") + end + + def down do + execute("DROP INDEX CONCURRENTLY IF EXISTS docker_tags_archs_gin_index") + end +end diff --git a/priv/scripts/otp/otp.sh b/priv/scripts/otp/otp.sh index f92c6d56..0e778813 100755 --- a/priv/scripts/otp/otp.sh +++ b/priv/scripts/otp/otp.sh @@ -49,4 +49,4 @@ curl \ -H "authorization: ${BOB_AGENT_SECRET}" \ -H "content-type: application/json" \ -d "{\"kind\":\"otp\",\"arch\":\"${arch}\",\"os\":\"${linux}\",\"name\":\"${ref_name}\",\"ref\":\"${ref}\",\"sha256\":\"${build_sha256}\",\"date\":\"${date}\"}" \ - "${BOB_MASTER_URL}/artifacts/add" + "${BOB_MASTER_URL}/api/artifacts/add" diff --git a/priv/static/favicon.ico b/priv/static/favicon.ico new file mode 100644 index 00000000..4ad97cd9 Binary files /dev/null and b/priv/static/favicon.ico differ diff --git a/priv/static/images/favicon-160.png b/priv/static/images/favicon-160.png new file mode 100644 index 00000000..cf537fa5 Binary files /dev/null and b/priv/static/images/favicon-160.png differ diff --git a/priv/static/images/favicon-64.png b/priv/static/images/favicon-64.png new file mode 100644 index 00000000..def74c2e Binary files /dev/null and b/priv/static/images/favicon-64.png differ diff --git a/test/bob/artifacts/docker_tag_search_test.exs b/test/bob/artifacts/docker_tag_search_test.exs new file mode 100644 index 00000000..21f9f272 --- /dev/null +++ b/test/bob/artifacts/docker_tag_search_test.exs @@ -0,0 +1,109 @@ +defmodule Bob.Artifacts.DockerTagSearchTest do + use ExUnit.Case, async: true + + alias Bob.Artifacts.DockerTagSearch + + test "returns known Docker tag dimensions" do + assert DockerTagSearch.repos() == [ + "hexpm/elixir", + "hexpm/elixir-amd64", + "hexpm/elixir-arm64", + "hexpm/erlang", + "hexpm/erlang-amd64", + "hexpm/erlang-arm64" + ] + + assert DockerTagSearch.arches() == ["amd64", "arm64"] + assert DockerTagSearch.oses() == ["alpine", "debian", "ubuntu"] + end + + test "parses per-arch Erlang tags" do + assert DockerTagSearch.metadata( + "hexpm/erlang-amd64", + "27.0-ubuntu-noble-20250101" + ) == %{ + "erlang_version" => "27.0", + "os" => "ubuntu", + "os_version" => "noble-20250101" + } + end + + test "parses Erlang manifest tags" do + assert DockerTagSearch.metadata( + "hexpm/erlang", + "26.2-debian-bookworm-20250113-slim" + ) == %{ + "erlang_version" => "26.2", + "os" => "debian", + "os_version" => "bookworm-20250113-slim" + } + end + + test "parses major-only Erlang version tags" do + assert DockerTagSearch.metadata( + "hexpm/erlang", + "17-ubuntu-jammy-20250101" + ) == %{ + "erlang_version" => "17", + "os" => "ubuntu", + "os_version" => "jammy-20250101" + } + end + + test "parses Erlang prerelease version tags" do + assert DockerTagSearch.metadata( + "hexpm/erlang", + "26.0-rc1-ubuntu-noble-20250101" + ) == %{ + "erlang_version" => "26.0-rc1", + "os" => "ubuntu", + "os_version" => "noble-20250101" + } + end + + test "parses per-arch Elixir tags" do + assert DockerTagSearch.metadata( + "hexpm/elixir-arm64", + "1.18.0-erlang-27.0-ubuntu-noble-20250101" + ) == %{ + "elixir_version" => "1.18.0", + "erlang_version" => "27.0", + "os" => "ubuntu", + "os_version" => "noble-20250101" + } + end + + test "parses Elixir manifest tags" do + assert DockerTagSearch.metadata( + "hexpm/elixir", + "1.17.3-erlang-26.2-alpine-3.22.1" + ) == %{ + "elixir_version" => "1.17.3", + "erlang_version" => "26.2", + "os" => "alpine", + "os_version" => "3.22.1" + } + end + + test "parses Elixir prerelease version tags" do + assert DockerTagSearch.metadata( + "hexpm/elixir", + "1.18.0-rc.0-erlang-27.0-ubuntu-noble-20250101" + ) == %{ + "elixir_version" => "1.18.0-rc.0", + "erlang_version" => "27.0", + "os" => "ubuntu", + "os_version" => "noble-20250101" + } + end + + test "returns empty metadata for unknown repos and malformed tags" do + assert DockerTagSearch.metadata("library/alpine", "3.22.1") == %{} + assert DockerTagSearch.metadata("hexpm/erlang-amd64", "not-a-known-shape") == %{} + assert DockerTagSearch.metadata("hexpm/erlang", "not-ubuntu-shape") == %{} + assert DockerTagSearch.metadata("hexpm/elixir-amd64", "1.18.0-otp-27") == %{} + + assert DockerTagSearch.metadata("hexpm/elixir", "anything-erlang-anything-debian-shape") == + %{} + end +end diff --git a/test/bob/artifacts_test.exs b/test/bob/artifacts_test.exs index 29b70bb7..847312a2 100644 --- a/test/bob/artifacts_test.exs +++ b/test/bob/artifacts_test.exs @@ -188,6 +188,23 @@ defmodule Bob.ArtifactsTest do [{"27.0-ubuntu-noble-20250101", ["amd64"]}] end + test "stores parsed search metadata" do + Artifacts.add_docker_tag("hexpm/elixir-amd64", "1.18.0-erlang-27.0-ubuntu-noble-20250101", [ + "amd64" + ]) + + assert [ + %DockerTag{ + search: %{ + "elixir_version" => "1.18.0", + "erlang_version" => "27.0", + "os" => "ubuntu", + "os_version" => "noble-20250101" + } + } + ] = Repo.all(DockerTag) + end + test "stores the supplied Docker Hub timestamp as built_at" do Artifacts.add_docker_tag( "hexpm/erlang-amd64", @@ -228,20 +245,35 @@ defmodule Bob.ArtifactsTest do end describe "stage_docker_tags/3 + swap_docker_tags/2" do - test "stages the Docker Hub timestamp for each tag" do + test "stages and swaps parsed search metadata" do token = Ecto.UUID.generate() Artifacts.stage_docker_tags(token, "hexpm/erlang-amd64", [ - docker_tag("27.0-ubuntu-noble-20250101", ["amd64"], @docker_built_at) + docker_tag("27.0-ubuntu-noble-20250101", ["amd64"]) ]) assert [ %DockerTagStaging{ - tag: "27.0-ubuntu-noble-20250101", - archs: ["amd64"], - built_at: @docker_built_at + built_at: @docker_built_at, + search: %{ + "erlang_version" => "27.0", + "os" => "ubuntu", + "os_version" => "noble-20250101" + } } ] = Repo.all(DockerTagStaging) + + assert Artifacts.swap_docker_tags(token, "hexpm/erlang-amd64") == :ok + + assert [ + %DockerTag{ + search: %{ + "erlang_version" => "27.0", + "os" => "ubuntu", + "os_version" => "noble-20250101" + } + } + ] = Repo.all(DockerTag) end test "swaps the staged Docker Hub timestamp into built_at" do @@ -432,7 +464,7 @@ defmodule Bob.ArtifactsTest do stale = Ecto.UUID.generate() - Repo.insert_all(DockerTagStaging, [ + Repo.insert_all(Bob.Artifacts.DockerTagStaging, [ %{ token: stale, repo: "hexpm/erlang-amd64", @@ -510,6 +542,222 @@ defmodule Bob.ArtifactsTest do end end + describe "search" do + setup do + Bob.Artifacts.upsert(%{ + kind: "otp", + arch: "amd64", + os: "ubuntu-24.04", + name: "OTP-27.0", + ref: "aaa", + built_at: ~U[2026-01-01 00:00:00Z] + }) + + Bob.Artifacts.upsert(%{ + kind: "otp", + arch: "arm64", + os: "ubuntu-22.04", + name: "OTP-26.2", + ref: "bbb", + built_at: ~U[2026-02-01 00:00:00Z] + }) + + :ok + end + + test "search_artifacts/1 with blank filters returns newest first" do + assert [%{name: "OTP-26.2"}, %{name: "OTP-27.0"}] = Bob.Artifacts.search_artifacts(%{}) + end + + test "search_docker_tags/1 with blank filters returns newest first" do + Bob.Artifacts.add_docker_tag( + "hexpm/erlang", + "old-ubuntu-noble-20250101", + ["amd64"], + ~U[2025-01-01 00:00:00Z] + ) + + Bob.Artifacts.add_docker_tag( + "hexpm/erlang", + "new-ubuntu-noble-20250101", + ["amd64"], + ~U[2025-02-01 00:00:00Z] + ) + + assert [%{tag: "new-ubuntu-noble-20250101"}, %{tag: "old-ubuntu-noble-20250101"}] = + Bob.Artifacts.search_docker_tags(%{}) + end + + test "search_artifacts/1 filters by free-text on name" do + assert [%{name: "OTP-27.0"}] = Bob.Artifacts.search_artifacts(%{query: "27.0"}) + end + + test "search_artifacts/1 filters by arch" do + assert [%{arch: "arm64"}] = Bob.Artifacts.search_artifacts(%{arch: "arm64"}) + end + + test "count_artifacts/1 counts exact matching artifacts" do + assert Bob.Artifacts.count_artifacts(%{}) == 2 + assert Bob.Artifacts.count_artifacts(%{query: "27.0"}) == 1 + assert Bob.Artifacts.count_artifacts(%{arch: "arm64"}) == 1 + assert Bob.Artifacts.count_artifacts(%{os: "missing"}) == 0 + end + + test "distinct value helpers" do + assert Bob.Artifacts.distinct_kinds() == ["otp"] + assert Bob.Artifacts.distinct_arches() == ["amd64", "arm64"] + assert Bob.Artifacts.distinct_oses() == ["ubuntu-22.04", "ubuntu-24.04"] + end + + test "distinct Docker filter helpers" do + assert Bob.Artifacts.distinct_docker_arches() == ["amd64", "arm64"] + assert Bob.Artifacts.distinct_docker_oses() == ["alpine", "debian", "ubuntu"] + end + + test "distinct Docker repositories" do + Bob.Artifacts.add_docker_tag("hexpm/erlang", "27.0-ubuntu-noble-20250101", [ + "amd64", + "arm64" + ]) + + Bob.Artifacts.add_docker_tag( + "hexpm/elixir", + "1.17.3-erlang-26.2-debian-bookworm-20250113-slim", + ["amd64"] + ) + + assert Bob.Artifacts.distinct_repos() == ["hexpm/elixir", "hexpm/erlang"] + end + + test "search_docker_tags/1 filters by structured prefixes" do + Bob.Artifacts.add_docker_tag("hexpm/erlang", "27.0-ubuntu-noble-20250101", [ + "amd64", + "arm64" + ]) + + Bob.Artifacts.add_docker_tag("hexpm/erlang", "27.1-ubuntu-noble-20250101", [ + "amd64" + ]) + + Bob.Artifacts.add_docker_tag("hexpm/erlang", "26.2-debian-bookworm-20250113-slim", [ + "amd64" + ]) + + Bob.Artifacts.add_docker_tag( + "hexpm/elixir", + "1.18.0-erlang-27.0-ubuntu-noble-20250101", + ["amd64", "arm64"] + ) + + Bob.Artifacts.add_docker_tag( + "hexpm/elixir", + "1.17.3-erlang-26.2-debian-bookworm-20250113-slim", + ["amd64"] + ) + + assert [%{tag: "27.0-ubuntu-noble-20250101"}] = + Bob.Artifacts.search_docker_tags(%{ + repo: "hexpm/erl", + tag: "27", + arch: "arm", + erlang_version: "27", + os: "ub", + os_version: "noble" + }) + + assert [%{tag: "27.0-ubuntu-noble-20250101"}] = + Bob.Artifacts.search_docker_tags(%{ + repo: "hexpm/erl", + tag: "27", + arch: "arm", + os: "ub", + os_version: "noble" + }) + + assert [ + "1.17.3-erlang-26.2-debian-bookworm-20250113-slim", + "1.18.0-erlang-27.0-ubuntu-noble-20250101" + ] = + Bob.Artifacts.search_docker_tags(%{elixir_version: "1"}) + |> Enum.map(& &1.tag) + |> Enum.sort() + + assert [ + "1.18.0-erlang-27.0-ubuntu-noble-20250101", + "27.0-ubuntu-noble-20250101", + "27.1-ubuntu-noble-20250101" + ] = + Bob.Artifacts.search_docker_tags(%{os: "ub"}) + |> Enum.map(& &1.tag) + |> Enum.sort() + + assert [%{tag: "1.18.0-erlang-27.0-ubuntu-noble-20250101"}] = + Bob.Artifacts.search_docker_tags(%{ + elixir_version: "1", + os: "ub" + }) + + assert [%{tag: "1.18.0-erlang-27.0-ubuntu-noble-20250101"}] = + Bob.Artifacts.search_docker_tags(%{ + repo: "hexpm/elixir", + elixir_version: "1.18", + erlang_version: "27.0", + os: "ubuntu", + os_version: "noble-2025" + }) + + assert [%{tag: "1.17.3-erlang-26.2-debian-bookworm-20250113-slim"}] = + Bob.Artifacts.search_docker_tags(%{ + elixir_version: "1.17", + os: "deb", + os_version: "bookworm" + }) + end + + test "count_docker_tags/1 counts exact matching tags" do + Bob.Artifacts.add_docker_tag("hexpm/erlang", "27.0-ubuntu-noble-20250101", [ + "amd64", + "arm64" + ]) + + Bob.Artifacts.add_docker_tag("hexpm/erlang", "27.1-ubuntu-noble-20250101", ["arm64"]) + + count = + Bob.Artifacts.count_docker_tags(%{ + repo: "hexpm/erl", + tag: "27", + arch: "arm", + erlang_version: "27", + os: "ub", + os_version: "noble" + }) + + assert count == 2 + assert Bob.Artifacts.count_docker_tags(%{arch: "amd64"}) == 1 + assert Bob.Artifacts.count_docker_tags(%{arch: "arm64"}) == 2 + assert Bob.Artifacts.count_docker_tags(%{tag: "missing"}) == 0 + end + + test "search_artifacts/1 does not treat % as a LIKE wildcard" do + assert Bob.Artifacts.search_artifacts(%{query: "%"}) == [] + end + + test "search_docker_tags/1 does not treat % or _ as LIKE wildcards" do + Bob.Artifacts.add_docker_tag("hexpm/erlang", "27.0-ubuntu-noble-20250101", ["amd64"]) + + assert Bob.Artifacts.search_docker_tags(%{tag: "%"}) == [] + assert Bob.Artifacts.search_docker_tags(%{repo: "hexpm/_"}) == [] + assert Bob.Artifacts.search_docker_tags(%{arch: "%"}) == [] + assert Bob.Artifacts.search_docker_tags(%{erlang_version: "%"}) == [] + assert Bob.Artifacts.search_docker_tags(%{erlang_version: "27_"}) == [] + end + + test "search_artifacts/3 respects limit and offset" do + assert [%{name: "OTP-26.2"}] = Bob.Artifacts.search_artifacts(%{}, 1, 0) + assert [%{name: "OTP-27.0"}] = Bob.Artifacts.search_artifacts(%{}, 1, 1) + end + end + defp replace(repo, tag_archs) do token = Ecto.UUID.generate() Artifacts.stage_docker_tags(token, repo, Enum.map(tag_archs, &docker_tag_tuple/1)) diff --git a/test/bob/mixfile_test.exs b/test/bob/mixfile_test.exs new file mode 100644 index 00000000..0896c371 --- /dev/null +++ b/test/bob/mixfile_test.exs @@ -0,0 +1,7 @@ +defmodule Bob.MixfileTest do + use ExUnit.Case, async: true + + test "configures Phoenix code reloader as a Mix listener" do + assert Phoenix.CodeReloader in Keyword.fetch!(Bob.Mixfile.project(), :listeners) + end +end diff --git a/test/bob/queue_test.exs b/test/bob/queue_test.exs index d69fcb07..26295e61 100644 --- a/test/bob/queue_test.exs +++ b/test/bob/queue_test.exs @@ -204,4 +204,89 @@ defmodule Bob.QueueTest do assert Queue.queued() == [{TestJob, [:a]}, {{TestJob, :variant}, [:b, :c]}] end + + describe "pubsub broadcasts" do + setup do + Phoenix.PubSub.subscribe(Bob.PubSub, "jobs") + :ok + end + + test "add broadcasts :jobs_changed" do + Bob.Queue.add(Bob.Job.OTPChecker, [:a]) + assert_receive :jobs_changed + end + + test "start broadcasts :jobs_changed" do + Bob.Queue.add(Bob.Job.OTPChecker, [:a]) + assert_receive :jobs_changed + {:ok, _} = Bob.Queue.start(Bob.Job.OTPChecker) + assert_receive :jobs_changed + end + + test "success and failure broadcast :jobs_changed" do + Bob.Queue.add(Bob.Job.OTPChecker, [:a]) + assert_receive :jobs_changed + {:ok, {id, _}} = Bob.Queue.start(Bob.Job.OTPChecker) + assert_receive :jobs_changed + Bob.Queue.success(id) + assert_receive :jobs_changed + + Bob.Queue.add(Bob.Job.OTPChecker, [:b]) + assert_receive :jobs_changed + {:ok, {id2, _}} = Bob.Queue.start(Bob.Job.OTPChecker) + assert_receive :jobs_changed + Bob.Queue.failure(id2) + assert_receive :jobs_changed + end + end + + describe "read functions" do + test "running/0 returns running jobs newest-started first" do + Bob.Queue.add(Bob.Job.OTPChecker, [:a]) + Bob.Queue.add(Bob.Job.OTPChecker, [:b]) + {:ok, _} = Bob.Queue.start(Bob.Job.OTPChecker) + {:ok, _} = Bob.Queue.start(Bob.Job.OTPChecker) + + running = Bob.Queue.running() + assert length(running) == 2 + assert Enum.all?(running, &(&1.state == "running")) + assert hd(running).started_at >= List.last(running).started_at + end + + test "queued_listing/2 returns queued jobs oldest-first with limit/offset" do + for n <- 1..3, do: Bob.Queue.add(Bob.Job.OTPChecker, [n]) + + assert [a, b] = Bob.Queue.queued_listing(2, 0) + assert a.inserted_at <= b.inserted_at + assert [_c] = Bob.Queue.queued_listing(2, 2) + end + + test "recent/2 returns done and failed jobs newest-finished first" do + Bob.Queue.add(Bob.Job.OTPChecker, [:done]) + {:ok, {id1, _}} = Bob.Queue.start(Bob.Job.OTPChecker) + Bob.Queue.success(id1) + + Bob.Queue.add(Bob.Job.OTPChecker, [:fail]) + {:ok, {id2, _}} = Bob.Queue.start(Bob.Job.OTPChecker) + Bob.Queue.failure(id2) + + recent = Bob.Queue.recent(50, 0) + assert Enum.map(recent, & &1.state) |> Enum.sort() == ["done", "failed"] + assert hd(recent).finished_at >= List.last(recent).finished_at + end + + test "finished_count/0 counts done and failed jobs" do + Bob.Queue.add(Bob.Job.OTPChecker, [:done]) + {:ok, {done_id, _}} = Bob.Queue.start(Bob.Job.OTPChecker) + Bob.Queue.success(done_id) + + Bob.Queue.add(Bob.Job.OTPChecker, [:failed]) + {:ok, {failed_id, _}} = Bob.Queue.start(Bob.Job.OTPChecker) + Bob.Queue.failure(failed_id) + + Bob.Queue.add(Bob.Job.OTPChecker, [:queued]) + + assert Bob.Queue.finished_count() == 2 + end + end end diff --git a/test/bob/router_test.exs b/test/bob_web/controllers/api_test.exs similarity index 52% rename from test/bob/router_test.exs rename to test/bob_web/controllers/api_test.exs index 9ce23348..72a24a10 100644 --- a/test/bob/router_test.exs +++ b/test/bob_web/controllers/api_test.exs @@ -1,14 +1,9 @@ -defmodule Bob.RouterTest do - use Bob.DataCase - - import Plug.Test - import Plug.Conn +defmodule BobWeb.ApiTest do + use BobWeb.ConnCase alias Bob.Artifacts alias Bob.Artifacts.Artifact - @opts Bob.Router.init([]) - setup do Bob.FakeHttpClient.reset() Application.put_env(:bob, :agent_secret, "secret") @@ -16,7 +11,7 @@ defmodule Bob.RouterTest do :ok end - defp body() do + defp artifact_body() do JSON.encode!(%{ kind: "otp", arch: "amd64", @@ -28,7 +23,7 @@ defmodule Bob.RouterTest do }) end - test "POST /artifacts/add upserts an artifact and returns 204" do + test "POST /api/artifacts/add upserts an artifact and returns 204", %{conn: conn} do Bob.FakeHttpClient.stub( :put, "https://s3.amazonaws.com/s3.hex.pm/builds/otp/amd64/ubuntu-24.04/builds.txt", @@ -37,10 +32,10 @@ defmodule Bob.RouterTest do ) conn = - conn(:post, "/artifacts/add", body()) + conn |> put_req_header("content-type", "application/json") |> put_req_header("authorization", "secret") - |> Bob.Router.call(@opts) + |> post(~p"/api/artifacts/add", artifact_body()) assert conn.status == 204 @@ -48,17 +43,28 @@ defmodule Bob.RouterTest do Repo.all(Artifact) end - test "POST /artifacts/add rejects a missing/wrong secret with 401" do + test "POST /api/artifacts/add rejects a missing secret with 401", %{conn: conn} do conn = - conn(:post, "/artifacts/add", body()) + conn |> put_req_header("content-type", "application/json") - |> Bob.Router.call(@opts) + |> post(~p"/api/artifacts/add", artifact_body()) assert conn.status == 401 assert Repo.all(Artifact) == [] end - test "POST /docker/add upserts a docker tag and returns 204" do + test "POST /api/artifacts/add rejects an invalid secret before parsing the body", %{conn: conn} do + conn = + conn + |> put_req_header("content-type", "application/json") + |> put_req_header("authorization", "public") + |> post(~p"/api/artifacts/add", "{") + + assert conn.status == 401 + assert Repo.all(Artifact) == [] + end + + test "POST /api/docker/add upserts a docker tag and returns 204", %{conn: conn} do body = Bob.Plug.ErlangFormat.encode_to_iodata!(%{ repo: "hexpm/erlang-amd64", @@ -67,10 +73,10 @@ defmodule Bob.RouterTest do }) conn = - conn(:post, "/docker/add", body) + conn |> put_req_header("content-type", "application/vnd.bob+erlang") |> put_req_header("authorization", "secret") - |> Bob.Router.call(@opts) + |> post(~p"/api/docker/add", body) assert conn.status == 204 @@ -78,7 +84,7 @@ defmodule Bob.RouterTest do [{"27.0-ubuntu-noble-20250101", ["amd64"]}] end - test "POST /docker/add rejects a missing/wrong secret with 401" do + test "POST /api/docker/add rejects a missing secret with 401", %{conn: conn} do body = Bob.Plug.ErlangFormat.encode_to_iodata!(%{ repo: "hexpm/erlang-amd64", @@ -87,11 +93,33 @@ defmodule Bob.RouterTest do }) conn = - conn(:post, "/docker/add", body) + conn |> put_req_header("content-type", "application/vnd.bob+erlang") - |> Bob.Router.call(@opts) + |> post(~p"/api/docker/add", body) assert conn.status == 401 assert Artifacts.docker_tags("hexpm/erlang-amd64") == [] end + + test "POST /api/queue/add with erlang body enqueues a job (atom-keyed params)", %{conn: conn} do + body = + Bob.Plug.ErlangFormat.encode_to_iodata!(%{ + module: Bob.Job.OTPChecker, + args: [:tags] + }) + + conn = + conn + |> put_req_header("content-type", "application/vnd.bob+erlang") + |> put_req_header("authorization", "secret") + |> post(~p"/api/queue/add", body) + + assert conn.status == 204 + assert [{Bob.Job.OTPChecker, [:tags]}] = Bob.Queue.queued() + end + + test "GET /status still returns 200 at the root", %{conn: conn} do + conn = get(conn, "/status") + assert conn.status == 200 + end end diff --git a/test/bob_web/endpoint_config_test.exs b/test/bob_web/endpoint_config_test.exs new file mode 100644 index 00000000..e94ec2f3 --- /dev/null +++ b/test/bob_web/endpoint_config_test.exs @@ -0,0 +1,72 @@ +defmodule BobWeb.EndpointConfigTest do + use ExUnit.Case, async: false + + @config_dir Path.expand("../../config", __DIR__) + @prod_runtime_env %{ + "BOB_AGENT_SECRET" => "secret", + "BOB_ENV" => "prod", + "BOB_GITHUB_TOKEN" => "github-token", + "BOB_GITHUB_USER" => "hexbob", + "BOB_HOST" => "bob.hex.pm", + "BOB_HOSTNAME" => "gce", + "BOB_LOCAL_JOBS" => "[]", + "BOB_MASTER_URL" => "https://bob.hex.pm", + "BOB_PARALLEL_JOBS" => "10", + "BOB_PORT" => "4003", + "BOB_REMOTE_JOBS" => "[]", + "BOB_S3_ACCESS_KEY" => "s3-key", + "BOB_S3_SECRET_KEY" => "s3-secret", + "BOB_SENTRY_DSN" => "https://public@example.com/1", + "BOB_WHO" => "master", + "SECRET_KEY_BASE" => String.duplicate("a", 64) + } + + test "configured cookie session secret keys satisfy Plug's minimum length" do + for env <- [:dev, :test] do + endpoint_config = + @config_dir + |> Path.join("#{env}.exs") + |> Config.Reader.read!(env: env) + |> get_in([:bob, BobWeb.Endpoint]) + + secret_key_base = Keyword.fetch!(endpoint_config, :secret_key_base) + + assert byte_size(secret_key_base) >= 64 + end + end + + test "favicon assets are served as static paths" do + assert "favicon.ico" in BobWeb.static_paths() + assert "images" in BobWeb.static_paths() + end + + test "prod runtime endpoint config uses a schemed LiveView origin" do + with_env(@prod_runtime_env, fn -> + endpoint_config = + @config_dir + |> Path.join("runtime.exs") + |> Config.Reader.read!(env: :prod) + |> get_in([:bob, BobWeb.Endpoint]) + + assert Keyword.fetch!(endpoint_config, :url)[:host] == "bob.hex.pm" + assert Keyword.fetch!(endpoint_config, :check_origin) == ["https://bob.hex.pm"] + end) + end + + defp with_env(env, fun) do + previous = Map.new(Map.keys(env), &{&1, System.get_env(&1)}) + + try do + System.put_env(env) + fun.() + after + for {key, value} <- previous do + if value do + System.put_env(key, value) + else + System.delete_env(key) + end + end + end + end +end diff --git a/test/bob_web/live/artifacts_live_test.exs b/test/bob_web/live/artifacts_live_test.exs new file mode 100644 index 00000000..6f59a3e0 --- /dev/null +++ b/test/bob_web/live/artifacts_live_test.exs @@ -0,0 +1,53 @@ +defmodule BobWeb.ArtifactsLiveTest do + use BobWeb.ConnCase + + import Phoenix.LiveViewTest + + setup do + Bob.Artifacts.upsert(%{ + kind: "otp", + arch: "amd64", + os: "ubuntu-24.04", + name: "OTP-27.0", + ref: "aaa", + built_at: ~U[2026-01-01 00:00:00Z] + }) + + Bob.Artifacts.upsert(%{ + kind: "otp", + arch: "arm64", + os: "ubuntu-22.04", + name: "OTP-26.2", + ref: "bbb", + built_at: ~U[2026-02-01 00:00:00Z] + }) + + :ok + end + + test "lists artifacts and filters by free-text", %{conn: conn} do + {:ok, view, html} = live(conn, ~p"/artifacts") + assert html =~ "OTP-27.0" + assert html =~ "OTP-26.2" + assert html =~ "2 artifacts" + assert html =~ ~r/Showing\s*1<\/b>\s*-\s*2<\/b>\s*artifacts\s*of\s*2 artifacts/ + + html = + render_change(view, "search", %{"query" => "27.0", "kind" => "", "arch" => "", "os" => ""}) + + assert html =~ "OTP-27.0" + refute html =~ "OTP-26.2" + assert html =~ "1 artifact" + assert html =~ ~r/Showing\s*1<\/b>\s*-\s*1<\/b>\s*artifact\s*of\s*1 artifact/ + end + + test "filters by arch dropdown", %{conn: conn} do + {:ok, view, _html} = live(conn, ~p"/artifacts") + + html = + render_change(view, "search", %{"query" => "", "kind" => "", "arch" => "arm64", "os" => ""}) + + assert html =~ "OTP-26.2" + refute html =~ "OTP-27.0" + end +end diff --git a/test/bob_web/live/docker_tags_live_test.exs b/test/bob_web/live/docker_tags_live_test.exs new file mode 100644 index 00000000..5035809c --- /dev/null +++ b/test/bob_web/live/docker_tags_live_test.exs @@ -0,0 +1,143 @@ +defmodule BobWeb.DockerTagsLiveTest do + use BobWeb.ConnCase + + import Phoenix.LiveViewTest + + setup do + Bob.Artifacts.add_docker_tag("hexpm/erlang", "27.0-ubuntu-noble-20250101", [ + "amd64", + "arm64" + ]) + + Bob.Artifacts.add_docker_tag( + "hexpm/elixir", + "1.18.0-erlang-27.0-ubuntu-noble-20250101", + ["amd64", "arm64"] + ) + + Bob.Artifacts.add_docker_tag( + "hexpm/elixir", + "1.18.1-erlang-27.0-ubuntu-noble-20250101", + ["arm64"] + ) + + Bob.Artifacts.add_docker_tag( + "hexpm/elixir", + "1.17.3-erlang-26.2-debian-bookworm-20250113-slim", + ["amd64"] + ) + + :ok + end + + test "lists tags and filters by tag prefix", %{conn: conn} do + {:ok, view, html} = live(conn, ~p"/docker") + assert html =~ "27.0-ubuntu-noble-20250101" + assert html =~ "1.18.0-erlang-27.0-ubuntu-noble-20250101" + assert html =~ "4 tags" + assert html =~ ~r/Showing\s*1<\/b>\s*-\s*4<\/b>\s*tags\s*of\s*4 tags/ + + html = + render_change(view, "search", %{ + "repo" => "", + "tag" => "1.18", + "arch" => "", + "elixir_version" => "", + "erlang_version" => "", + "os" => "", + "os_version" => "" + }) + + assert docker_tag?(html, "1.18.0-erlang-27.0-ubuntu-noble-20250101") + refute docker_tag?(html, "27.0-ubuntu-noble-20250101") + end + + test "filters by structured inputs", %{conn: conn} do + {:ok, view, _html} = live(conn, ~p"/docker") + + html = + render_change(view, "search", %{ + "repo" => "hexpm/elixir", + "tag" => "", + "arch" => "arm", + "elixir_version" => "1.18", + "erlang_version" => "27", + "os" => "ub", + "os_version" => "noble" + }) + + assert docker_tag?(html, "1.18.0-erlang-27.0-ubuntu-noble-20250101") + refute docker_tag?(html, "1.17.3-erlang-26.2-debian-bookworm-20250113-slim") + refute docker_tag?(html, "27.0-ubuntu-noble-20250101") + end + + test "tag prefix keeps arch filtering and disables parsed tag filters", %{conn: conn} do + {:ok, view, _html} = live(conn, ~p"/docker") + + html = + render_change(view, "search", %{ + "repo" => "hexpm/elixir", + "tag" => "1.18", + "arch" => "amd64", + "elixir_version" => "1.17", + "erlang_version" => "26", + "os" => "debian", + "os_version" => "bookworm" + }) + + assert docker_tag?(html, "1.18.0-erlang-27.0-ubuntu-noble-20250101") + refute docker_tag?(html, "1.18.1-erlang-27.0-ubuntu-noble-20250101") + refute docker_tag?(html, "1.17.3-erlang-26.2-debian-bookworm-20250113-slim") + + refute control_html(html, "arch") =~ "disabled" + + for name <- ~w(elixir_version erlang_version os os_version) do + assert control_html(html, name) =~ "disabled" + end + end + + test "renders every Docker search input", %{conn: conn} do + {:ok, _view, html} = live(conn, ~p"/docker") + + assert control_html(html, "repo") =~ "hexpm/elixir) + assert html =~ ~s() + assert html =~ ~s() + assert html =~ ~s() + assert html =~ ~s() + assert html =~ ~s() + end + + test "renders arch filter after the parsed tag filters", %{conn: conn} do + {:ok, _view, html} = live(conn, ~p"/docker") + + assert control_position(html, "repo") < control_position(html, "tag") + assert control_position(html, "tag") < control_position(html, "elixir_version") + assert control_position(html, "elixir_version") < control_position(html, "erlang_version") + assert control_position(html, "erlang_version") < control_position(html, "os") + assert control_position(html, "os") < control_position(html, "os_version") + assert control_position(html, "os_version") < control_position(html, "arch") + end + + defp control_html(html, name) do + Regex.run(~r/<(?:input|select)[^>]*name="#{name}"[^>]*>/, html) + |> List.first() + end + + defp control_position(html, name) do + {position, _length} = :binary.match(html, ~s(name="#{name}")) + position + end + + defp docker_tag?(html, tag) do + html =~ + ~r/]*>#{Regex.escape(tag)}<\/code><\/td>/ + end +end diff --git a/test/bob_web/live/jobs_live_test.exs b/test/bob_web/live/jobs_live_test.exs new file mode 100644 index 00000000..436c2b22 --- /dev/null +++ b/test/bob_web/live/jobs_live_test.exs @@ -0,0 +1,66 @@ +defmodule BobWeb.JobsLiveTest do + use BobWeb.ConnCase + + import Ecto.Query + import Phoenix.LiveViewTest + + @debounce_pause 400 + + test "renders running, queued, and past jobs", %{conn: conn} do + Bob.Queue.add(Bob.Job.OTPChecker, [:queued]) + + Bob.Queue.add(Bob.Job.OTPChecker, [:running]) + {:ok, _} = Bob.Queue.start(Bob.Job.OTPChecker) + + Bob.Queue.add(Bob.Job.OTPChecker, [:done]) + {:ok, {id, _}} = Bob.Queue.start(Bob.Job.OTPChecker) + Bob.Queue.success(id) + + {:ok, _view, html} = live(conn, ~p"/") + + assert html =~ "Running" + assert html =~ "Queue" + assert html =~ "Past" + assert html =~ "OTPChecker" + assert html =~ "done" + assert html =~ ~r/Showing\s*1<\/b>\s*-\s*1<\/b>\s*queued\s*of\s*1 queued/ + assert html =~ ~r/Showing\s*1<\/b>\s*-\s*1<\/b>\s*job\s*of\s*1 job/ + end + + test "root layout links favicon assets", %{conn: conn} do + {:ok, _view, html} = live(conn, ~p"/") + + assert html =~ ~s(href="/favicon.ico") + assert html =~ ~s(href="/images/favicon-64.png") + assert html =~ ~s(href="/images/favicon-160.png") + end + + test "refreshes when a :jobs_changed broadcast arrives", %{conn: conn} do + {:ok, view, _html} = live(conn, ~p"/") + refute render(view) =~ ":refreshed" + + Bob.Queue.add(Bob.Job.OTPChecker, [:refreshed]) + {:ok, _} = Bob.Queue.start(Bob.Job.OTPChecker) + + # The add/start broadcasts :jobs_changed; the view reloads after the debounce window. + Process.sleep(@debounce_pause) + assert render(view) =~ ":refreshed" + end + + test "ticks elapsed time for running jobs", %{conn: conn} do + Bob.Queue.add(Bob.Job.OTPChecker, [:elapsed_tick]) + {:ok, {id, _args}} = Bob.Queue.start(Bob.Job.OTPChecker) + + started_at = DateTime.add(DateTime.utc_now(), -10, :second) + + Bob.Repo.update_all( + from(j in Bob.Queue.Job, where: j.id == ^id), + set: [started_at: started_at, updated_at: started_at] + ) + + {:ok, view, _html} = live(conn, ~p"/") + + Process.sleep(1600) + assert render(view) =~ ~r/1[1-3]s/ + end +end diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex new file mode 100644 index 00000000..21ef3ed2 --- /dev/null +++ b/test/support/conn_case.ex @@ -0,0 +1,28 @@ +defmodule BobWeb.ConnCase do + use ExUnit.CaseTemplate + + using do + quote do + use BobWeb, :verified_routes + + import Plug.Conn + import Phoenix.ConnTest + import BobWeb.ConnCase + + alias Bob.Repo + + @endpoint BobWeb.Endpoint + end + end + + setup tags do + pid = Ecto.Adapters.SQL.Sandbox.start_owner!(Bob.Repo, shared: not tags[:async]) + on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end) + + conn = + Phoenix.ConnTest.build_conn() + |> Map.put(:secret_key_base, BobWeb.Endpoint.config(:secret_key_base)) + + {:ok, conn: conn} + end +end