mirror of
https://github.com/moghtech/komodo.git
synced 2026-10-29 17:47:30 -05:00
Compare commits
207
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
37a684dc03 | ||
|
|
a5a828f99b | ||
|
|
a2ab0fafa2 | ||
|
|
e33e92c7fd | ||
|
|
33f0911912 | ||
|
|
b283791bc2 | ||
|
|
bea3e794ed | ||
|
|
1eed826bfb | ||
|
|
b9e5c3567e | ||
|
|
a4162d057e | ||
|
|
8465a3103c | ||
|
|
6e3dce5ef9 | ||
|
|
817f44dc36 | ||
|
|
fc035dce35 | ||
|
|
702988aeef | ||
|
|
f0c381ee7b | ||
|
|
f54c85d856 | ||
|
|
53c1644bcb | ||
|
|
5a02486f4d | ||
|
|
f7c0e310a2 | ||
|
|
2f0fded27a | ||
|
|
61c2d192fc | ||
|
|
1439d4d612 | ||
|
|
a3fe153767 | ||
|
|
d313ddfdf5 | ||
|
|
a6fa581976 | ||
|
|
d21f36f8bc | ||
|
|
7ddda61ad9 | ||
|
|
3e4437659a | ||
|
|
031f0a68cf | ||
|
|
6a58c69523 | ||
|
|
6d86b3fe22 | ||
|
|
6af4e38cb1 | ||
|
|
641928efa1 | ||
|
|
2e8b2c1340 | ||
|
|
c16ddce8cb | ||
|
|
5caab1fc05 | ||
|
|
118a779921 | ||
|
|
27abc428f7 | ||
|
|
46e9f01235 | ||
|
|
aa88bb6ba4 | ||
|
|
36a78c5cd5 | ||
|
|
d6f5990375 | ||
|
|
d4393a2485 | ||
|
|
d968523053 | ||
|
|
9c0a256c55 | ||
|
|
cebe78affd | ||
|
|
4978d0e2fa | ||
|
|
179bbe6871 | ||
|
|
2b45867fae | ||
|
|
f54daeba8f | ||
|
|
3265908ba7 | ||
|
|
4c3727b203 | ||
|
|
f6e4d5779a | ||
|
|
023e597e12 | ||
|
|
6e78e696b7 | ||
|
|
275c76ee71 | ||
|
|
bd460f4cce | ||
|
|
8c2d8ddc57 | ||
|
|
904e1b95f9 | ||
|
|
e1aece9938 | ||
|
|
fd754c787a | ||
|
|
304ea58bbc | ||
|
|
f3f774a571 | ||
|
|
29d3073b9c | ||
|
|
339b1b440a | ||
|
|
af6aca3c0a | ||
|
|
1f86b8b016 | ||
|
|
70296caa1c | ||
|
|
f974ab6c09 | ||
|
|
a71c481705 | ||
|
|
64700236ae | ||
|
|
95b104ac4c | ||
|
|
68e594377f | ||
|
|
89d384dcf2 | ||
|
|
f81f26f9e3 | ||
|
|
0275a0869a | ||
|
|
fec7343ebc | ||
|
|
50bc22b053 | ||
|
|
3736aff68f | ||
|
|
629cca3951 | ||
|
|
0bed758b4a | ||
|
|
efc7269232 | ||
|
|
be45961c4c | ||
|
|
ed122917b3 | ||
|
|
9ec5a93a54 | ||
|
|
fb3c7c2728 | ||
|
|
3465d14a1c | ||
|
|
0ca90ad103 | ||
|
|
20b4702530 | ||
|
|
63215db1e7 | ||
|
|
92a4f8436f | ||
|
|
a1582065e3 | ||
|
|
5f75fc1573 | ||
|
|
809219aa02 | ||
|
|
7dee1f551a | ||
|
|
b2db42bd82 | ||
|
|
97a567b76d | ||
|
|
51f8d04bad | ||
|
|
c253e8fc1a | ||
|
|
436e510d1c | ||
|
|
6ddade4973 | ||
|
|
e95c7529b3 | ||
|
|
6700fe0415 | ||
|
|
59e79b05be | ||
|
|
d783c511c4 | ||
|
|
c9ce07aa80 | ||
|
|
71b22a4e61 | ||
|
|
62d2700d19 | ||
|
|
102fd7ec77 | ||
|
|
f16b8c93d5 | ||
|
|
e3107d0d61 | ||
|
|
0b7c0e4c55 | ||
|
|
b17d32ce6c | ||
|
|
4cb9057dea | ||
|
|
ce98c3e455 | ||
|
|
739ad9ea26 | ||
|
|
d768558642 | ||
|
|
2241da02d8 | ||
|
|
29cc9bcc02 | ||
|
|
b014dc31c2 | ||
|
|
df3d8a18d0 | ||
|
|
35f7937ffd | ||
|
|
15ea08846d | ||
|
|
88cd356992 | ||
|
|
4440903211 | ||
|
|
5751e79651 | ||
|
|
0897b6a950 | ||
|
|
ab9e7b334c | ||
|
|
36a3c1ffb2 | ||
|
|
9b5713df2d | ||
|
|
a0484efe14 | ||
|
|
2437c72db1 | ||
|
|
3ced7d87c1 | ||
|
|
26d081265a | ||
|
|
beab18e4f8 | ||
|
|
06a51e7ec1 | ||
|
|
cc0cc31ac6 | ||
|
|
b4dcd21ecc | ||
|
|
a0dd329001 | ||
|
|
380d529be1 | ||
|
|
f7289decbc | ||
|
|
afc3f7be86 | ||
|
|
6ee8133026 | ||
|
|
1ac0b86b32 | ||
|
|
b255363be7 | ||
|
|
c2dad4e344 | ||
|
|
f1db5352d8 | ||
|
|
f3cc559a63 | ||
|
|
c1bb3028fd | ||
|
|
79e250058d | ||
|
|
f5c7e02d24 | ||
|
|
1020531fb6 | ||
|
|
e176fde86e | ||
|
|
dcf7be9e57 | ||
|
|
4e5fceaa7e | ||
|
|
c2b9fb73e3 | ||
|
|
84d17411f3 | ||
|
|
da2d823f95 | ||
|
|
3fe503ced2 | ||
|
|
c3f85d6f05 | ||
|
|
d99d793fdc | ||
|
|
db1810bab3 | ||
|
|
ff1171cd58 | ||
|
|
df307ea8ff | ||
|
|
c5f0fc4350 | ||
|
|
159b5a4c10 | ||
|
|
3e27bbb830 | ||
|
|
bd5faf6381 | ||
|
|
2fbd94c134 | ||
|
|
1025ce8502 | ||
|
|
117e1111eb | ||
|
|
879de614b3 | ||
|
|
0f4c8517b3 | ||
|
|
867ded524e | ||
|
|
cf43b21a9b | ||
|
|
dbeb6c54fe | ||
|
|
3ac5f0277f | ||
|
|
5b9678e09b | ||
|
|
38e94fb71a | ||
|
|
38f09603f9 | ||
|
|
8eb0e98e94 | ||
|
|
8f0c1eece4 | ||
|
|
ce0ab80337 | ||
|
|
8038d93a11 | ||
|
|
fad8a5084f | ||
|
|
5b1e7f9c21 | ||
|
|
c9135c9329 | ||
|
|
84298b5289 | ||
|
|
dda9fa1cb9 | ||
|
|
b2f4fd4a9e | ||
|
|
1f9ad78ace | ||
|
|
44ac268066 | ||
|
|
581fd997c5 | ||
|
|
19944f6f41 | ||
|
|
c7a01b311e | ||
|
|
b793554276 | ||
|
|
51025542bd | ||
|
|
a1602d8449 | ||
|
|
a76a9cd741 | ||
|
|
77c061acd2 | ||
|
|
f3b6bb1f04 | ||
|
|
a6ceb1859e | ||
|
|
6fefad4585 | ||
|
|
5d70678b41 | ||
|
|
966dfeb83a | ||
|
|
fceed18a58 |
@@ -1,4 +0,0 @@
|
||||
/target
|
||||
/config_example
|
||||
config.*
|
||||
.env
|
||||
-12
@@ -1,12 +0,0 @@
|
||||
target
|
||||
/frontend/build
|
||||
node_modules
|
||||
dist
|
||||
.env
|
||||
.env.development
|
||||
|
||||
repos
|
||||
config.json
|
||||
config.toml
|
||||
secrets.json
|
||||
secrets.toml
|
||||
Vendored
-64
@@ -1,64 +0,0 @@
|
||||
{
|
||||
"component": {
|
||||
"scope": "typescriptreact,javascriptreact",
|
||||
"prefix": "comp",
|
||||
"body": [
|
||||
"import { Component } from \"solid-js\";",
|
||||
"",
|
||||
"const ${1:$TM_FILENAME_BASE}: Component<{}> = (p) => {",
|
||||
"\treturn (",
|
||||
"\t\t<div>",
|
||||
"\t\t\t${0}",
|
||||
"\t\t</div>",
|
||||
"\t);",
|
||||
"}",
|
||||
"",
|
||||
"export default ${1:$TM_FILENAME_BASE};"
|
||||
]
|
||||
},
|
||||
"component-with-css": {
|
||||
"scope": "typescriptreact,javascriptreact",
|
||||
"prefix": "css-comp",
|
||||
"body": [
|
||||
"import { Component } from \"solid-js\";",
|
||||
"import s from \"./${1:$TM_FILENAME_BASE}.module.scss\";",
|
||||
"",
|
||||
"const ${2:$TM_FILENAME_BASE}: Component<{}> = (p) => {",
|
||||
"\treturn (",
|
||||
"\t\t<div class={s.${2:$TM_FILENAME_BASE}} >",
|
||||
"\t\t\t${0}",
|
||||
"\t\t</div>",
|
||||
"\t);",
|
||||
"}",
|
||||
"",
|
||||
"export default ${2:$TM_FILENAME_BASE};"
|
||||
]
|
||||
},
|
||||
"context": {
|
||||
"scope": "typescriptreact,javascriptreact",
|
||||
"prefix": "provider",
|
||||
"body": [
|
||||
"import { ParentComponent, createContext, useContext } from \"solid-js\";",
|
||||
"",
|
||||
"const value = () => {",
|
||||
"\treturn {};",
|
||||
"}",
|
||||
"",
|
||||
"export type Value = ReturnType<typeof value>;",
|
||||
"",
|
||||
"const context = createContext<Value>();",
|
||||
"",
|
||||
"export const Provider: ParentComponent<{}> = (p) => {",
|
||||
"\treturn (",
|
||||
"\t\t<context.Provider value={value()}>",
|
||||
"\t\t\t{p.children}",
|
||||
"\t\t</context.Provider>",
|
||||
"\t);",
|
||||
"}",
|
||||
"",
|
||||
"export function useValue() {",
|
||||
"\treturn useContext(context) as Value;",
|
||||
"}"
|
||||
]
|
||||
}
|
||||
}
|
||||
Vendored
-164
@@ -1,164 +0,0 @@
|
||||
{
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"type": "cargo",
|
||||
"command": "build",
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
},
|
||||
"label": "rust: cargo build"
|
||||
},
|
||||
{
|
||||
"type": "cargo",
|
||||
"command": "fmt",
|
||||
"label": "rust: cargo fmt"
|
||||
},
|
||||
{
|
||||
"type": "cargo",
|
||||
"command": "check",
|
||||
"label": "rust: cargo check"
|
||||
},
|
||||
{
|
||||
"label": "start dev",
|
||||
"dependsOn": [
|
||||
"run core",
|
||||
"yarn: start frontend"
|
||||
],
|
||||
"problemMatcher": []
|
||||
},
|
||||
{
|
||||
"type": "shell",
|
||||
"command": "yarn start",
|
||||
"label": "yarn: start frontend",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/frontend"
|
||||
},
|
||||
"presentation": {
|
||||
"group": "start"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "cargo",
|
||||
"command": "run",
|
||||
"label": "run core",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/core"
|
||||
},
|
||||
"presentation": {
|
||||
"group": "start"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "cargo",
|
||||
"command": "run",
|
||||
"label": "run periphery",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/periphery"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "shell",
|
||||
"command": "cargo install --path . && if pgrep periphery; then pkill periphery; fi && periphery --daemon --config-path ~/.monitor/local.periphery.config.toml",
|
||||
"label": "run periphery daemon",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/periphery"
|
||||
},
|
||||
"problemMatcher": []
|
||||
},
|
||||
{
|
||||
"type": "cargo",
|
||||
"command": "run",
|
||||
"label": "run cli",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/cli"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "cargo",
|
||||
"command": "run",
|
||||
"label": "run tests",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/tests"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "cargo",
|
||||
"command": "publish",
|
||||
"args": ["--allow-dirty"],
|
||||
"label": "publish monitor types",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/lib/types"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "cargo",
|
||||
"command": "publish",
|
||||
"label": "publish monitor client",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/lib/monitor_client"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "shell",
|
||||
"command": "docker compose up -d",
|
||||
"label": "docker compose up",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/tests"
|
||||
},
|
||||
"problemMatcher": []
|
||||
},
|
||||
{
|
||||
"type": "shell",
|
||||
"command": "docker compose down",
|
||||
"label": "docker compose down",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/tests"
|
||||
},
|
||||
"problemMatcher": []
|
||||
},
|
||||
{
|
||||
"type": "shell",
|
||||
"command": "docker compose build",
|
||||
"label": "docker compose build",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/tests"
|
||||
},
|
||||
"problemMatcher": []
|
||||
},
|
||||
{
|
||||
"type": "shell",
|
||||
"command": "docker compose down && docker compose up -d",
|
||||
"label": "docker compose restart",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/tests"
|
||||
},
|
||||
"problemMatcher": []
|
||||
},
|
||||
{
|
||||
"type": "shell",
|
||||
"command": "docker compose build && docker compose down && docker compose up -d",
|
||||
"label": "docker compose build and restart",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/tests"
|
||||
},
|
||||
"problemMatcher": []
|
||||
},
|
||||
{
|
||||
"type": "shell",
|
||||
"command": "docker compose build periphery",
|
||||
"label": "docker compose build periphery",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/tests"
|
||||
},
|
||||
"problemMatcher": []
|
||||
},
|
||||
{
|
||||
"type": "shell",
|
||||
"command": "typeshare ./lib/types --lang=typescript --output-file=./frontend/src/types.ts && typeshare ./core --lang=typescript --output-file=./frontend/src/util/client_types.ts",
|
||||
"label": "generate typescript types",
|
||||
"problemMatcher": []
|
||||
}
|
||||
]
|
||||
}
|
||||
Generated
-3821
File diff suppressed because it is too large
Load Diff
-14
@@ -1,14 +0,0 @@
|
||||
[workspace]
|
||||
|
||||
members = [
|
||||
"cli",
|
||||
"core",
|
||||
"periphery",
|
||||
"tests",
|
||||
"lib/axum_oauth2",
|
||||
"lib/db_client",
|
||||
"lib/helpers",
|
||||
"lib/periphery_client",
|
||||
"lib/types",
|
||||
"lib/monitor_client"
|
||||
]
|
||||
@@ -1,674 +0,0 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1 @@
|
||||
"use strict";(self.webpackChunkdocsite=self.webpackChunkdocsite||[]).push([[583],{7143:(e,t,s)=>{s.r(t),s.d(t,{default:()=>g});var n=s(4164),o=s(8774),i=s(4586),r=s(1656),c=s(1107);const l={features:"features_t9lD",featureSvg:"featureSvg_GfXr"};var a=s(4848);const d=[{title:"Automated builds \ud83d\udee0\ufe0f",description:(0,a.jsx)(a.Fragment,{children:"Build auto versioned docker images from git repos, trigger builds on git push"})},{title:"Deploy docker containers \ud83d\ude80",description:(0,a.jsx)(a.Fragment,{children:"Deploy containers, deploy docker compose, see uptime and logs across all your servers"})},{title:"Powered by Rust \ud83e\udd80",description:(0,a.jsx)(a.Fragment,{children:"The core API and periphery agent are written in Rust"})}];function h({title:e,description:t}){return(0,a.jsx)("div",{className:(0,n.A)("col col--4"),children:(0,a.jsxs)("div",{className:"text--center padding-horiz--md",children:[(0,a.jsx)(c.A,{as:"h3",children:e}),(0,a.jsx)("p",{children:t})]})})}function u(){return(0,a.jsx)("section",{className:l.features,children:(0,a.jsx)("div",{className:"container",children:(0,a.jsx)("div",{className:"row",children:d.map((e,t)=>(0,a.jsx)(h,{...e},t))})})})}const m={heroBanner:"heroBanner_qdFl",buttons:"buttons_AeoN"};s(6540);function x({width:e="4rem"}){return(0,a.jsx)("img",{style:{width:e,height:"auto",opacity:.7},src:"img/monitor-lizard.png",alt:"monitor-lizard"})}function p(){const{siteConfig:e}=(0,i.A)();return(0,a.jsx)("header",{className:(0,n.A)("hero hero--primary",m.heroBanner),children:(0,a.jsxs)("div",{className:"container",children:[(0,a.jsx)("div",{style:{display:"flex",gap:"1rem",justifyContent:"center"},children:(0,a.jsxs)("div",{style:{position:"relative"},children:[(0,a.jsx)(x,{width:"600px"}),(0,a.jsx)("h1",{className:"hero__title",style:{margin:0,position:"absolute",top:"40%",left:"50%",transform:"translate(-50%, -50%)"},children:"Komodo"})]})}),(0,a.jsx)("p",{className:"hero__subtitle",children:e.tagline}),(0,a.jsx)("div",{style:{display:"flex",justifyContent:"center"},children:(0,a.jsxs)("div",{className:m.buttons,children:[(0,a.jsx)(o.A,{className:"button button--secondary button--lg",to:"/docs/intro",children:"Docs"}),(0,a.jsx)(o.A,{className:"button button--secondary button--lg",to:"https://github.com/moghtech/komodo",children:"Github"}),(0,a.jsx)(o.A,{className:"button button--secondary button--lg",to:"https://github.com/moghtech/komodo#screenshots",style:{width:"100%",boxSizing:"border-box",gridColumn:"span 2"},children:"Screenshots"}),(0,a.jsx)(o.A,{className:"button button--secondary button--lg",to:"https://demo.komo.do",style:{width:"100%",boxSizing:"border-box",gridColumn:"span 2"},children:"Demo"})]})})]})})}function g(){const{siteConfig:e}=(0,i.A)();return(0,a.jsxs)(r.A,{title:"Home",description:e.tagline,children:[(0,a.jsx)(p,{}),(0,a.jsx)("main",{children:(0,a.jsx)(u,{})})]})}}}]);
|
||||
@@ -0,0 +1 @@
|
||||
"use strict";(self.webpackChunkdocsite=self.webpackChunkdocsite||[]).push([[237],{2237:(e,t,i)=>{i.r(t),i.d(t,{default:()=>d});i(6540);var n=i(1312),o=i(5500),s=i(1656),r=i(3363),a=i(4848);function d(){const e=(0,n.T)({id:"theme.NotFound.title",message:"Page Not Found"});return(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(o.be,{title:e}),(0,a.jsx)(s.A,{children:(0,a.jsx)(r.A,{})})]})}},3363:(e,t,i)=>{i.d(t,{A:()=>a});i(6540);var n=i(4164),o=i(1312),s=i(1107),r=i(4848);function a({className:e}){return(0,r.jsx)("main",{className:(0,n.A)("container margin-vert--xl",e),children:(0,r.jsx)("div",{className:"row",children:(0,r.jsxs)("div",{className:"col col--6 col--offset-3",children:[(0,r.jsx)(s.A,{as:"h1",className:"hero__title",children:(0,r.jsx)(o.A,{id:"theme.NotFound.title",description:"The title of the 404 page",children:"Page Not Found"})}),(0,r.jsx)("p",{children:(0,r.jsx)(o.A,{id:"theme.NotFound.p1",description:"The first paragraph of the 404 page",children:"We could not find what you were looking for."})}),(0,r.jsx)("p",{children:(0,r.jsx)(o.A,{id:"theme.NotFound.p2",description:"The 2nd paragraph of the 404 page",children:"Please contact the owner of the site that linked you to the original URL and let them know their link is broken."})})]})})})}}}]);
|
||||
@@ -0,0 +1 @@
|
||||
"use strict";(self.webpackChunkdocsite=self.webpackChunkdocsite||[]).push([[345],{4689:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>c,contentTitle:()=>a,default:()=>u,frontMatter:()=>r,metadata:()=>s,toc:()=>d});const s=JSON.parse('{"id":"resources/build-images/versioning","title":"Image Versioning","description":"Komodo uses a major.minor.patch versioning scheme to Build versioning. By default, every RunBuild will auto increment the Build\'s version patch number, and push the image to docker hub with the version tag, as well as the latest tag. A tag containing the latest short commit hash at the time the repo was cloned will also be created.","source":"@site/docs/resources/build-images/versioning.md","sourceDirName":"resources/build-images","slug":"/resources/build-images/versioning","permalink":"/docs/resources/build-images/versioning","draft":false,"unlisted":false,"editUrl":"https://github.com/moghtech/komodo/tree/main/docsite/docs/resources/build-images/versioning.md","tags":[],"version":"current","frontMatter":{},"sidebar":"docs","previous":{"title":"Builders","permalink":"/docs/resources/build-images/builders"},"next":{"title":"Deploy Containers","permalink":"/docs/resources/deploy-containers/"}}');var i=n(4848),o=n(8453);const r={},a="Image Versioning",c={},d=[];function l(e){const t={code:"code",h1:"h1",header:"header",p:"p",...(0,o.R)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(t.header,{children:(0,i.jsx)(t.h1,{id:"image-versioning",children:"Image Versioning"})}),"\n",(0,i.jsxs)(t.p,{children:["Komodo uses a major.minor.patch versioning scheme to Build versioning. By default, every RunBuild will auto increment the Build's version patch number, and push the image to docker hub with the version tag, as well as the ",(0,i.jsx)(t.code,{children:"latest"})," tag. A tag containing the latest short commit hash at the time the repo was cloned will also be created."]}),"\n",(0,i.jsxs)(t.p,{children:['You can also turn off the auto incrementing feature, and manage the version yourself. In addition, you can configure a "version tag" on the build. This will postfix the version tag / commit hash tag with a custom label. For example, an image tag of ',(0,i.jsx)(t.code,{children:"dev"})," will produce tags like ",(0,i.jsx)(t.code,{children:"image_name:1.1.1-dev"})," and ",(0,i.jsx)(t.code,{children:"image_name:h3c87c-dev"}),"."]})]})}function u(e={}){const{wrapper:t}={...(0,o.R)(),...e.components};return t?(0,i.jsx)(t,{...e,children:(0,i.jsx)(l,{...e})}):l(e)}},8453:(e,t,n)=>{n.d(t,{R:()=>r,x:()=>a});var s=n(6540);const i={},o=s.createContext(i);function r(e){const t=s.useContext(o);return s.useMemo(function(){return"function"==typeof e?e(t):{...t,...e}},[t,e])}function a(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:r(e.components),s.createElement(o.Provider,{value:t},e.children)}}}]);
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1 @@
|
||||
"use strict";(self.webpackChunkdocsite=self.webpackChunkdocsite||[]).push([[771],{483:(e,t,o)=>{o.r(t),o.d(t,{assets:()=>c,contentTitle:()=>i,default:()=>u,frontMatter:()=>a,metadata:()=>n,toc:()=>l});const n=JSON.parse('{"id":"resources/auto-update","title":"Automatic Updates","description":"Starting from v1.19.0, new Komodo installs will automatically create the","source":"@site/docs/resources/auto-update.md","sourceDirName":"resources","slug":"/resources/auto-update","permalink":"/docs/resources/auto-update","draft":false,"unlisted":false,"editUrl":"https://github.com/moghtech/komodo/tree/main/docsite/docs/resources/auto-update.md","tags":[],"version":"current","frontMatter":{},"sidebar":"docs","previous":{"title":"Docker Compose","permalink":"/docs/resources/docker-compose"},"next":{"title":"Variables and Secrets","permalink":"/docs/resources/variables"}}');var s=o(4848),r=o(8453);const a={},i="Automatic Updates",c={},l=[{value:"How does it work?",id:"how-does-it-work",level:3}];function d(e){const t={a:"a",admonition:"admonition",code:"code",em:"em",h1:"h1",h3:"h3",header:"header",p:"p",pre:"pre",strong:"strong",...(0,r.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(t.header,{children:(0,s.jsx)(t.h1,{id:"automatic-updates",children:"Automatic Updates"})}),"\n",(0,s.jsxs)(t.p,{children:["Starting from ",(0,s.jsx)(t.strong,{children:"v1.19.0"}),", new Komodo installs will automatically create the\n",(0,s.jsx)(t.strong,{children:"Global Auto Update"})," ",(0,s.jsx)(t.a,{href:"../resources/procedures#procedures",children:"Procedure"}),", scheduled daily.\nIf you don't have it, this is the Toml:"]}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-toml",children:'[[procedure]]\nname = "Global Auto Update"\ndescription = "Pulls and auto updates Stacks and Deployments using \'poll_for_updates\' or \'auto_update\'."\ntags = ["system"]\nconfig.schedule = "Every day at 03:00"\n\n[[procedure.config.stage]]\nname = "Stage 1"\nenabled = true\nexecutions = [\n { execution.type = "GlobalAutoUpdate", execution.params = {}, enabled = true }\n]\n'})}),"\n",(0,s.jsx)(t.admonition,{type:"info",children:(0,s.jsxs)(t.p,{children:["You are also able to integrate ",(0,s.jsx)(t.code,{children:"GlobalAutoUpdate"})," into other Procedures\nto coordinate the timing with other processes, such as backup. There is\nnothing special about this Procedure, it's just created by default for\nguidance / convenience."]})}),"\n",(0,s.jsx)(t.h3,{id:"how-does-it-work",children:"How does it work?"}),"\n",(0,s.jsxs)(t.p,{children:["Both Stacks and Deployments allow you to configure ",(0,s.jsx)(t.strong,{children:"Poll for Updates"})," or ",(0,s.jsx)(t.strong,{children:"Auto Update"}),".\nWhen ",(0,s.jsx)(t.a,{href:"https://docs.rs/komodo_client/latest/komodo_client/api/execute/struct.GlobalAutoUpdate.html",children:(0,s.jsx)(t.strong,{children:"GlobalAutoUpdate"})}),"\nis run, Komodo will loop through all the resources with either of these options enabled,\nand run ",(0,s.jsx)(t.a,{href:"https://docs.rs/komodo_client/latest/komodo_client/api/execute/struct.PullStack.html",children:(0,s.jsx)(t.strong,{children:"PullStack"})})," / ",(0,s.jsx)(t.a,{href:"https://docs.rs/komodo_client/latest/komodo_client/api/execute/struct.PullDeployment.html",children:(0,s.jsx)(t.strong,{children:"PullDeployment"})}),"\nin order to pick up any newer images ",(0,s.jsx)(t.strong,{children:"at the same tag"}),'.\nNote that in order to work, it requires use of a "Rolling" image tag, such as ',(0,s.jsx)(t.code,{children:":latest"}),"."]}),"\n",(0,s.jsx)(t.admonition,{type:"info",children:(0,s.jsxs)(t.p,{children:["If you use git sources Stacks and want to automatically update image tags, check out\n",(0,s.jsx)(t.a,{href:"https://github.com/renovatebot/renovate?tab=readme-ov-file#what-is-the-mend-renovate-cli",children:"Renovate"})]})}),"\n",(0,s.jsxs)(t.p,{children:["For resources with ",(0,s.jsx)(t.strong,{children:"Poll for Updates"})," enabled and an Alerter configured, it will\nsend an alert that a newer image is available, and display the update available indicator in the UI"]}),"\n",(0,s.jsxs)(t.p,{children:["For resource with ",(0,s.jsx)(t.strong,{children:"Auto Update"})," enabled, it will go ahead and Redeploy ",(0,s.jsx)(t.em,{children:"just the services"})," with\nnewer images (by default). If an Alerter is configured, it will also send an alert that this occured."]})]})}function u(e={}){const{wrapper:t}={...(0,r.R)(),...e.components};return t?(0,s.jsx)(t,{...e,children:(0,s.jsx)(d,{...e})}):d(e)}},8453:(e,t,o)=>{o.d(t,{R:()=>a,x:()=>i});var n=o(6540);const s={},r=n.createContext(s);function a(e){const t=n.useContext(r);return n.useMemo(function(){return"function"==typeof e?e(t):{...t,...e}},[t,e])}function i(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:a(e.components),n.createElement(r.Provider,{value:t},e.children)}}}]);
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1 @@
|
||||
"use strict";(self.webpackChunkdocsite=self.webpackChunkdocsite||[]).push([[122],{8453:(e,s,n)=>{n.d(s,{R:()=>t,x:()=>c});var o=n(6540);const r={},i=o.createContext(r);function t(e){const s=o.useContext(i);return o.useMemo(function(){return"function"==typeof e?e(s):{...s,...e}},[s,e])}function c(e){let s;return s=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:t(e.components),o.createElement(i.Provider,{value:s},e.children)}},9749:(e,s,n)=>{n.r(s),n.d(s,{assets:()=>d,contentTitle:()=>c,default:()=>a,frontMatter:()=>t,metadata:()=>o,toc:()=>l});const o=JSON.parse('{"id":"resources/build-images/pre-build","title":"Pre-build command","description":"Sometimes a command needs to be run before running `docker build`, you can configure this in the pre build section.","source":"@site/docs/resources/build-images/pre-build.md","sourceDirName":"resources/build-images","slug":"/resources/build-images/pre-build","permalink":"/docs/resources/build-images/pre-build","draft":false,"unlisted":false,"editUrl":"https://github.com/moghtech/komodo/tree/main/docsite/docs/resources/build-images/pre-build.md","tags":[],"version":"current","frontMatter":{},"sidebar":"docs","previous":{"title":"Configuration","permalink":"/docs/resources/build-images/configuration"},"next":{"title":"Builders","permalink":"/docs/resources/build-images/builders"}}');var r=n(4848),i=n(8453);const t={},c="Pre-build command",d={},l=[];function u(e){const s={code:"code",em:"em",h1:"h1",header:"header",p:"p",...(0,i.R)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(s.header,{children:(0,r.jsx)(s.h1,{id:"pre-build-command",children:"Pre-build command"})}),"\n",(0,r.jsxs)(s.p,{children:["Sometimes a command needs to be run before running ",(0,r.jsx)(s.code,{children:"docker build"}),", you can configure this in the ",(0,r.jsx)(s.em,{children:"pre build"})," section."]}),"\n",(0,r.jsxs)(s.p,{children:["There are two fields to pass for ",(0,r.jsx)(s.em,{children:"pre build"}),". the first is ",(0,r.jsx)(s.em,{children:"path"}),", which changes the working directory. To run the command in the root of the repo, just pass ",(0,r.jsx)(s.code,{children:"."}),". The second field is ",(0,r.jsx)(s.em,{children:"command"}),", this is the shell command to be executed after the repo is cloned."]}),"\n",(0,r.jsxs)(s.p,{children:["For example, say your repo had a folder in it called ",(0,r.jsx)(s.code,{children:"scripts"})," with a shell script ",(0,r.jsx)(s.code,{children:"on-clone.sh"}),". You would give ",(0,r.jsx)(s.em,{children:"path"})," as ",(0,r.jsx)(s.code,{children:"scripts"})," and command as ",(0,r.jsx)(s.code,{children:"sh on-clone.sh"}),". Or you could make ",(0,r.jsx)(s.em,{children:"path"})," just ",(0,r.jsx)(s.code,{children:"."})," and then the command would be ",(0,r.jsx)(s.code,{children:"sh scripts/on-clone.sh"}),". Either way works fine."]})]})}function a(e={}){const{wrapper:s}={...(0,i.R)(),...e.components};return s?(0,r.jsx)(s,{...e,children:(0,r.jsx)(u,{...e})}):u(e)}}}]);
|
||||
@@ -0,0 +1 @@
|
||||
"use strict";(self.webpackChunkdocsite=self.webpackChunkdocsite||[]).push([[647],{7121:(e,s,r)=>{r.r(s),r.d(s,{default:()=>i});r(6540);var c=r(4164),t=r(7559),u=r(5500),a=r(2831),d=r(1656),n=r(4848);function i(e){return(0,n.jsx)(u.e3,{className:(0,c.A)(t.G.wrapper.docsPages),children:(0,n.jsx)(d.A,{children:(0,a.v)(e.route.routes)})})}}}]);
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1 @@
|
||||
"use strict";(self.webpackChunkdocsite=self.webpackChunkdocsite||[]).push([[464],{185:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>c,contentTitle:()=>s,default:()=>p,frontMatter:()=>r,metadata:()=>o,toc:()=>l});const o=JSON.parse('{"id":"resources/deploy-containers/lifetime-management","title":"Container Management","description":"The lifetime of a docker container is more like a virtual machine. They can be created, started, stopped, and destroyed. Komodo will display the state of the container and provides an API to manage all your container\'s lifetimes.","source":"@site/docs/resources/deploy-containers/lifetime-management.md","sourceDirName":"resources/deploy-containers","slug":"/resources/deploy-containers/lifetime-management","permalink":"/docs/resources/deploy-containers/lifetime-management","draft":false,"unlisted":false,"editUrl":"https://github.com/moghtech/komodo/tree/main/docsite/docs/resources/deploy-containers/lifetime-management.md","tags":[],"version":"current","frontMatter":{},"sidebar":"docs","previous":{"title":"Configuration","permalink":"/docs/resources/deploy-containers/configuration"},"next":{"title":"Docker Compose","permalink":"/docs/resources/docker-compose"}}');var i=n(4848),a=n(8453);const r={},s="Container Management",c={},l=[{value:"Stopping a Container",id:"stopping-a-container",level:3},{value:"Container Redeploy",id:"container-redeploy",level:3}];function d(e){const t={em:"em",h1:"h1",h3:"h3",header:"header",p:"p",...(0,a.R)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(t.header,{children:(0,i.jsx)(t.h1,{id:"container-management",children:"Container Management"})}),"\n",(0,i.jsx)(t.p,{children:"The lifetime of a docker container is more like a virtual machine. They can be created, started, stopped, and destroyed. Komodo will display the state of the container and provides an API to manage all your container's lifetimes."}),"\n",(0,i.jsx)(t.p,{children:"This is achieved internally by running the appropriate docker command for the requested action (docker stop, docker start, etc)."}),"\n",(0,i.jsx)(t.h3,{id:"stopping-a-container",children:"Stopping a Container"}),"\n",(0,i.jsxs)(t.p,{children:["Sometimes you want to stop a running application but preserve its logs and configuration, either to be restarted later or to view the logs at a later time. It is more like ",(0,i.jsx)(t.em,{children:"pausing"})," the application with its current config, as no configuration (like environment variable, volume mounts, etc.) will be changed when the container is started again."]}),"\n",(0,i.jsxs)(t.p,{children:["Note that in order to restart an application with updated configuration, it must be ",(0,i.jsx)(t.em,{children:"redeployed"}),". stopping and starting a container again will keep all configuration as it was when the container was initially created."]}),"\n",(0,i.jsx)(t.h3,{id:"container-redeploy",children:"Container Redeploy"}),"\n",(0,i.jsx)(t.p,{children:"Redeploying is the action of destroying a container and recreating it. If you update deployment config, these changes will not take effect until the container is redeployed. Just note this will destroy the previous containers logs along with the container itself."})]})}function p(e={}){const{wrapper:t}={...(0,a.R)(),...e.components};return t?(0,i.jsx)(t,{...e,children:(0,i.jsx)(d,{...e})}):d(e)}},8453:(e,t,n)=>{n.d(t,{R:()=>r,x:()=>s});var o=n(6540);const i={},a=o.createContext(i);function r(e){const t=o.useContext(a);return o.useMemo(function(){return"function"==typeof e?e(t):{...t,...e}},[t,e])}function s(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:r(e.components),o.createElement(a.Provider,{value:t},e.children)}}}]);
|
||||
@@ -0,0 +1 @@
|
||||
"use strict";(self.webpackChunkdocsite=self.webpackChunkdocsite||[]).push([[339],{4191:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>r,contentTitle:()=>a,default:()=>p,frontMatter:()=>i,metadata:()=>s,toc:()=>d});const s=JSON.parse('{"id":"ecosystem/api","title":"API and Clients","description":"Komodo Core exposes an RPC-like HTTP API to read data, write configuration, and execute actions.","source":"@site/docs/ecosystem/api.md","sourceDirName":"ecosystem","slug":"/ecosystem/api","permalink":"/docs/ecosystem/api","draft":false,"unlisted":false,"editUrl":"https://github.com/moghtech/komodo/tree/main/docsite/docs/ecosystem/api.md","tags":[],"version":"current","frontMatter":{},"sidebar":"docs","previous":{"title":"Komodo CLI","permalink":"/docs/ecosystem/cli"},"next":{"title":"Community","permalink":"/docs/ecosystem/community"}}');var o=n(4848),c=n(8453);const i={},a="API and Clients",r={},d=[{value:"Rust Client",id:"rust-client",level:2},{value:"Typescript Client",id:"typescript-client",level:2}];function l(e){const t={a:"a",code:"code",h1:"h1",h2:"h2",header:"header",p:"p",pre:"pre",strong:"strong",...(0,c.R)(),...e.components};return(0,o.jsxs)(o.Fragment,{children:[(0,o.jsx)(t.header,{children:(0,o.jsx)(t.h1,{id:"api-and-clients",children:"API and Clients"})}),"\n",(0,o.jsxs)(t.p,{children:["Komodo Core exposes an RPC-like HTTP API to read data, write configuration, and execute actions.\nThere are typesafe clients available in\n",(0,o.jsx)(t.a,{href:"/docs/ecosystem/api#rust-client",children:(0,o.jsx)(t.strong,{children:"Rust"})})," and ",(0,o.jsx)(t.a,{href:"/docs/ecosystem/api#typescript-client",children:(0,o.jsx)(t.strong,{children:"Typescript"})}),"."]}),"\n",(0,o.jsxs)(t.p,{children:["The full API documentation is ",(0,o.jsx)(t.a,{href:"https://docs.rs/komodo_client/latest/komodo_client/api/index.html",children:(0,o.jsx)(t.strong,{children:"available here"})}),"."]}),"\n",(0,o.jsx)(t.h2,{id:"rust-client",children:"Rust Client"}),"\n",(0,o.jsxs)(t.p,{children:["The Rust client is published to crates.io at ",(0,o.jsx)(t.a,{href:"https://crates.io/crates/komodo_client",children:"komodo_client"}),"."]}),"\n",(0,o.jsx)(t.pre,{children:(0,o.jsx)(t.code,{className:"language-rust",children:'let komodo = KomodoClient::new("https://demo.komo.do", "your_key", "your_secret")\n .with_healthcheck()\n .await?;\n\nlet stacks = komodo.read(ListStacks::default()).await?;\n\nlet update = komodo\n .execute(DeployStack {\n stack: stacks[0].name.clone(),\n stop_time: None\n })\n .await?;\n'})}),"\n",(0,o.jsx)(t.h2,{id:"typescript-client",children:"Typescript Client"}),"\n",(0,o.jsxs)(t.p,{children:["The Typescript client is published to NPM at ",(0,o.jsx)(t.a,{href:"https://www.npmjs.com/package/komodo_client",children:"komodo_client"}),"."]}),"\n",(0,o.jsx)(t.pre,{children:(0,o.jsx)(t.code,{className:"language-ts",children:'import { KomodoClient, Types } from "komodo_client";\n\nconst komodo = KomodoClient("https://demo.komo.do", {\n type: "api-key",\n params: {\n key: "your_key",\n secret: "your secret",\n },\n});\n\n// Inferred as Types.StackListItem[]\nconst stacks = await komodo.read("ListStacks", {});\n\n// Inferred as Types.Update\nconst update = await komodo.execute("DeployStack", {\n stack: stacks[0].name,\n});\n'})})]})}function p(e={}){const{wrapper:t}={...(0,c.R)(),...e.components};return t?(0,o.jsx)(t,{...e,children:(0,o.jsx)(l,{...e})}):l(e)}},8453:(e,t,n)=>{n.d(t,{R:()=>i,x:()=>a});var s=n(6540);const o={},c=s.createContext(o);function i(e){const t=s.useContext(c);return s.useMemo(function(){return"function"==typeof e?e(t):{...t,...e}},[t,e])}function a(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(o):e.components||o:i(e.components),s.createElement(c.Provider,{value:t},e.children)}}}]);
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1 @@
|
||||
"use strict";(self.webpackChunkdocsite=self.webpackChunkdocsite||[]).push([[235],{8552:e=>{e.exports=JSON.parse('{"name":"docusaurus-plugin-content-pages","id":"default"}')}}]);
|
||||
@@ -0,0 +1 @@
|
||||
"use strict";(self.webpackChunkdocsite=self.webpackChunkdocsite||[]).push([[98],{1723:(n,e,s)=>{s.r(e),s.d(e,{default:()=>l});s(6540);var r=s(5500);function o(n,e){return`docs-${n}-${e}`}var t=s(3025),c=s(2831),i=s(1463),u=s(4848);function a(n){const{version:e}=n;return(0,u.jsxs)(u.Fragment,{children:[(0,u.jsx)(i.A,{version:e.version,tag:o(e.pluginId,e.version)}),(0,u.jsx)(r.be,{children:e.noIndex&&(0,u.jsx)("meta",{name:"robots",content:"noindex, nofollow"})})]})}function d(n){const{version:e,route:s}=n;return(0,u.jsx)(r.e3,{className:e.className,children:(0,u.jsx)(t.n,{version:e,children:(0,c.v)(s.routes)})})}function l(n){return(0,u.jsxs)(u.Fragment,{children:[(0,u.jsx)(a,{...n}),(0,u.jsx)(d,{...n})]})}}}]);
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1 @@
|
||||
"use strict";(self.webpackChunkdocsite=self.webpackChunkdocsite||[]).push([[742],{7093:e=>{e.exports=JSON.parse('{"name":"docusaurus-plugin-content-docs","id":"default"}')}}]);
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1 @@
|
||||
"use strict";(self.webpackChunkdocsite=self.webpackChunkdocsite||[]).push([[39],{3021:(e,t,s)=>{s.d(t,{A:()=>b});var n=s(6540),r=s(4164),o=s(6972),c=s(8774),i=s(4586);const a=["zero","one","two","few","many","other"];function l(e){return a.filter(t=>e.includes(t))}const u={locale:"en",pluralForms:l(["one","other"]),select:e=>1===e?"one":"other"};function d(){const{i18n:{currentLocale:e}}=(0,i.A)();return(0,n.useMemo)(()=>{try{return function(e){const t=new Intl.PluralRules(e);return{locale:e,pluralForms:l(t.resolvedOptions().pluralCategories),select:e=>t.select(e)}}(e)}catch(t){return console.error(`Failed to use Intl.PluralRules for locale "${e}".\nDocusaurus will fallback to the default (English) implementation.\nError: ${t.message}\n`),u}},[e])}function m(){const e=d();return{selectMessage:(t,s)=>function(e,t,s){const n=e.split("|");if(1===n.length)return n[0];n.length>s.pluralForms.length&&console.error(`For locale=${s.locale}, a maximum of ${s.pluralForms.length} plural forms are expected (${s.pluralForms.join(",")}), but the message contains ${n.length}: ${e}`);const r=s.select(t),o=s.pluralForms.indexOf(r);return n[Math.min(o,n.length-1)]}(s,t,e)}}var h=s(6654),f=s(1312),p=s(1107);const x={cardContainer:"cardContainer_fWXF",cardTitle:"cardTitle_rnsV",cardDescription:"cardDescription_PWke"};var g=s(4848);function y({className:e,href:t,children:s}){return(0,g.jsx)(c.A,{href:t,className:(0,r.A)("card padding--lg",x.cardContainer,e),children:s})}function j({className:e,href:t,icon:s,title:n,description:o}){return(0,g.jsxs)(y,{href:t,className:e,children:[(0,g.jsxs)(p.A,{as:"h2",className:(0,r.A)("text--truncate",x.cardTitle),title:n,children:[s," ",n]}),o&&(0,g.jsx)("p",{className:(0,r.A)("text--truncate",x.cardDescription),title:o,children:o})]})}function N({item:e}){const t=(0,o.Nr)(e),s=function(){const{selectMessage:e}=m();return t=>e(t,(0,f.T)({message:"1 item|{count} items",id:"theme.docs.DocCard.categoryDescription.plurals",description:"The default description for a category card in the generated index about how many items this category includes"},{count:t}))}();return t?(0,g.jsx)(j,{className:e.className,href:t,icon:"\ud83d\uddc3\ufe0f",title:e.label,description:e.description??s(e.items.length)}):null}function C({item:e}){const t=(0,h.A)(e.href)?"\ud83d\udcc4\ufe0f":"\ud83d\udd17",s=(0,o.cC)(e.docId??void 0);return(0,g.jsx)(j,{className:e.className,href:e.href,icon:t,title:e.label,description:e.description??s?.description})}function w({item:e}){switch(e.type){case"link":return(0,g.jsx)(C,{item:e});case"category":return(0,g.jsx)(N,{item:e});default:throw new Error(`unknown item type ${JSON.stringify(e)}`)}}const k={docCardListItem:"docCardListItem_W1sv"};function v({className:e}){const t=(0,o.a4)();return(0,g.jsx)(b,{items:t,className:e})}function A({item:e}){return(0,g.jsx)("article",{className:(0,r.A)(k.docCardListItem,"col col--6"),children:(0,g.jsx)(w,{item:e})})}function b(e){const{items:t,className:s}=e;if(!t)return(0,g.jsx)(v,{...e});const n=(0,o.d1)(t);return(0,g.jsx)("section",{className:(0,r.A)("row",s),children:n.map((e,t)=>(0,g.jsx)(A,{item:e},t))})}},7350:(e,t,s)=>{s.r(t),s.d(t,{assets:()=>l,contentTitle:()=>a,default:()=>m,frontMatter:()=>i,metadata:()=>n,toc:()=>u});const n=JSON.parse('{"id":"ecosystem/index","title":"Ecosystem","description":"","source":"@site/docs/ecosystem/index.mdx","sourceDirName":"ecosystem","slug":"/ecosystem","permalink":"/docs/ecosystem","draft":false,"unlisted":false,"editUrl":"https://github.com/moghtech/komodo/tree/main/docsite/docs/ecosystem/index.mdx","tags":[],"version":"current","frontMatter":{"slug":"/ecosystem"},"sidebar":"docs","previous":{"title":"Permissioning","permalink":"/docs/resources/permissioning"},"next":{"title":"Komodo CLI","permalink":"/docs/ecosystem/cli"}}');var r=s(4848),o=s(8453),c=s(3021);const i={slug:"/ecosystem"},a="Ecosystem",l={},u=[];function d(e){const t={h1:"h1",header:"header",...(0,o.R)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(t.header,{children:(0,r.jsx)(t.h1,{id:"ecosystem",children:"Ecosystem"})}),"\n","\n",(0,r.jsx)(c.A,{})]})}function m(e={}){const{wrapper:t}={...(0,o.R)(),...e.components};return t?(0,r.jsx)(t,{...e,children:(0,r.jsx)(d,{...e})}):d(e)}},8453:(e,t,s)=>{s.d(t,{R:()=>c,x:()=>i});var n=s(6540);const r={},o=n.createContext(r);function c(e){const t=n.useContext(o);return n.useMemo(function(){return"function"==typeof e?e(t):{...t,...e}},[t,e])}function i(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:c(e.components),n.createElement(o.Provider,{value:t},e.children)}}}]);
|
||||
@@ -0,0 +1 @@
|
||||
"use strict";(self.webpackChunkdocsite=self.webpackChunkdocsite||[]).push([[847],{4817:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>c,contentTitle:()=>a,default:()=>u,frontMatter:()=>i,metadata:()=>s,toc:()=>d});const s=JSON.parse('{"id":"setup/version-upgrades","title":"Version Upgrades","description":"Most version upgrades only require a redeployment of the Core container after pulling the latest version, and are fully backward compatible with the periphery clients, which may be updated later on as convenient. This is the default, and will be the case unless specifically mentioned in the version release notes.","source":"@site/docs/setup/version-upgrades.md","sourceDirName":"setup","slug":"/setup/version-upgrades","permalink":"/docs/setup/version-upgrades","draft":false,"unlisted":false,"editUrl":"https://github.com/moghtech/komodo/tree/main/docsite/docs/setup/version-upgrades.md","tags":[],"version":"current","frontMatter":{},"sidebar":"docs","previous":{"title":"Advanced Configuration","permalink":"/docs/setup/advanced"},"next":{"title":"Resources","permalink":"/docs/resources/"}}');var r=n(4848),o=n(8453);const i={},a="Version Upgrades",c={},d=[];function l(e){const t={a:"a",h1:"h1",header:"header",p:"p",...(0,o.R)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(t.header,{children:(0,r.jsx)(t.h1,{id:"version-upgrades",children:"Version Upgrades"})}),"\n",(0,r.jsxs)(t.p,{children:["Most version upgrades only require a redeployment of the Core container after pulling the latest version, and are fully backward compatible with the periphery clients, which may be updated later on as convenient. This is the default, and will be the case unless specifically mentioned in the ",(0,r.jsx)(t.a,{href:"https://github.com/moghtech/komodo/releases",children:"version release notes"}),"."]}),"\n",(0,r.jsx)(t.p,{children:"Some Core API upgrades may change behavior such as building / cloning, and require updating the Periphery binaries to match the Core version before this functionality can be restored. This will be specifically mentioned in the release notes."})]})}function u(e={}){const{wrapper:t}={...(0,o.R)(),...e.components};return t?(0,r.jsx)(t,{...e,children:(0,r.jsx)(l,{...e})}):l(e)}},8453:(e,t,n)=>{n.d(t,{R:()=>i,x:()=>a});var s=n(6540);const r={},o=s.createContext(r);function i(e){const t=s.useContext(o);return s.useMemo(function(){return"function"==typeof e?e(t):{...t,...e}},[t,e])}function a(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:i(e.components),s.createElement(o.Provider,{value:t},e.children)}}}]);
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,61 @@
|
||||
/* NProgress, (c) 2013, 2014 Rico Sta. Cruz - http://ricostacruz.com/nprogress
|
||||
* @license MIT */
|
||||
|
||||
/**
|
||||
* @license React
|
||||
* react-dom-client.production.js
|
||||
*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @license React
|
||||
* react-dom.production.js
|
||||
*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @license React
|
||||
* react-jsx-runtime.production.js
|
||||
*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @license React
|
||||
* react.production.js
|
||||
*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @license React
|
||||
* scheduler.production.js
|
||||
*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
/** @license React v16.13.1
|
||||
* react-is.production.min.js
|
||||
*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
File diff suppressed because one or more lines are too long
@@ -1,26 +0,0 @@
|
||||
[package]
|
||||
name = "monitor_cli"
|
||||
version = "0.3.0"
|
||||
edition = "2021"
|
||||
authors = ["MoghTech"]
|
||||
description = "monitor cli | tools to setup monitor system"
|
||||
license = "GPL-3.0-or-later"
|
||||
|
||||
[[bin]]
|
||||
name = "monitor"
|
||||
path = "src/main.rs"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
monitor_types = { path = "../lib/types" }
|
||||
clap = "4.0"
|
||||
async_timing_util = "0.1.14"
|
||||
rand = "0.8"
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
toml = "0.7"
|
||||
run_command = "0.0.5"
|
||||
colored = "2"
|
||||
strum = "0.24"
|
||||
strum_macros = "0.24"
|
||||
@@ -1,621 +0,0 @@
|
||||
use std::{
|
||||
env,
|
||||
fs::{self, File},
|
||||
io::{Read, Write},
|
||||
net::IpAddr,
|
||||
path::PathBuf,
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
use clap::ArgMatches;
|
||||
use colored::Colorize;
|
||||
use monitor_types::{CoreConfig, MongoConfig, PeripheryConfig, RestartMode, Timelength};
|
||||
use rand::{distributions::Alphanumeric, Rng};
|
||||
use run_command::run_command_pipe_to_terminal;
|
||||
use serde::Serialize;
|
||||
|
||||
const CORE_IMAGE_NAME: &str = "mbecker2020/monitor_core";
|
||||
const PERIPHERY_IMAGE_NAME: &str = "mbecker2020/monitor_periphery";
|
||||
const PERIPHERY_CRATE: &str = "monitor_periphery";
|
||||
|
||||
pub fn gen_core_config(sub_matches: &ArgMatches) {
|
||||
let host = sub_matches
|
||||
.get_one::<String>("host")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("http://localhost:9000")
|
||||
.to_string();
|
||||
|
||||
let path = sub_matches
|
||||
.get_one::<String>("path")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("~/.monitor/core.config.toml")
|
||||
.to_string();
|
||||
|
||||
let port = sub_matches
|
||||
.get_one::<String>("port")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("9000")
|
||||
.parse::<u16>()
|
||||
.expect("invalid port");
|
||||
|
||||
let mongo_uri = sub_matches
|
||||
.get_one::<String>("mongo-uri")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("mongodb://monitor-mongo")
|
||||
.to_string();
|
||||
|
||||
let mongo_db_name = sub_matches
|
||||
.get_one::<String>("mongo-db-name")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("monitor")
|
||||
.to_string();
|
||||
|
||||
let jwt_valid_for = sub_matches
|
||||
.get_one::<String>("jwt-valid-for")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("1-wk")
|
||||
.parse()
|
||||
.expect("invalid jwt-valid-for");
|
||||
|
||||
let slack_url = sub_matches
|
||||
.get_one::<String>("slack-url")
|
||||
.map(|p| p.to_owned());
|
||||
|
||||
let config = CoreConfig {
|
||||
title: String::from("monitor"),
|
||||
host,
|
||||
port,
|
||||
jwt_valid_for,
|
||||
monitoring_interval: Timelength::OneMinute,
|
||||
daily_offset_hours: 0,
|
||||
keep_stats_for_days: 120,
|
||||
slack_url,
|
||||
local_auth: true,
|
||||
github_oauth: Default::default(),
|
||||
google_oauth: Default::default(),
|
||||
aws: Default::default(),
|
||||
docker_organizations: Default::default(),
|
||||
mongo: MongoConfig {
|
||||
uri: mongo_uri,
|
||||
db_name: mongo_db_name,
|
||||
app_name: "monitor".to_string(),
|
||||
},
|
||||
jwt_secret: generate_secret(40),
|
||||
github_webhook_secret: generate_secret(30),
|
||||
github_webhook_base_url: None,
|
||||
passkey: generate_secret(30),
|
||||
};
|
||||
|
||||
write_to_toml(&path, &config);
|
||||
|
||||
println!(
|
||||
"\n✅ {} has been generated at {path} ✅\n",
|
||||
"core config".bold()
|
||||
);
|
||||
}
|
||||
|
||||
pub fn start_mongo(sub_matches: &ArgMatches) {
|
||||
let username = sub_matches.get_one::<String>("username");
|
||||
let password = sub_matches.get_one::<String>("password");
|
||||
|
||||
if (username.is_some() && password.is_none()) {
|
||||
println!(
|
||||
"\n❌ must provide {} if username is provided ❌\n",
|
||||
"--password".bold()
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (username.is_none() && password.is_some()) {
|
||||
println!(
|
||||
"\n❌ must provide {} if password is provided ❌\n",
|
||||
"--username".bold()
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let skip_enter = *sub_matches.get_one::<bool>("yes").unwrap_or(&false);
|
||||
|
||||
let name = sub_matches
|
||||
.get_one::<String>("name")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("monitor-mongo");
|
||||
|
||||
let port = sub_matches
|
||||
.get_one::<String>("port")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("27017")
|
||||
.parse::<u16>()
|
||||
.expect("invalid port");
|
||||
|
||||
let network = sub_matches
|
||||
.get_one::<String>("network")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("bridge");
|
||||
|
||||
let mount = sub_matches
|
||||
.get_one::<String>("mount")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("~/.monitor/db");
|
||||
|
||||
let restart = sub_matches
|
||||
.get_one::<String>("restart")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("unless-stopped")
|
||||
.parse::<RestartMode>()
|
||||
.expect("invalid restart mode");
|
||||
|
||||
let env = if let (Some(username), Some(password)) = (username, password) {
|
||||
format!(" --env MONGO_INITDB_ROOT_USERNAME={username} --env MONGO_INITDB_ROOT_PASSWORD={password}")
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
println!(
|
||||
"\n====================\n {} \n====================\n",
|
||||
"mongo config".bold()
|
||||
);
|
||||
if let Some(username) = username {
|
||||
println!("{}: {username}", "mongo username".dimmed());
|
||||
}
|
||||
println!("{}: {name}", "container name".dimmed());
|
||||
println!("{}: {port}", "port".dimmed());
|
||||
println!("{}: {mount}", "mount".dimmed());
|
||||
println!("{}: {network}", "network".dimmed());
|
||||
println!("{}: {restart}", "restart".dimmed());
|
||||
|
||||
if !skip_enter {
|
||||
println!(
|
||||
"\npress {} to start {}. {}",
|
||||
"ENTER".green().bold(),
|
||||
"MongoDB".bold(),
|
||||
"(ctrl-c to cancel)".dimmed()
|
||||
);
|
||||
|
||||
let buffer = &mut [0u8];
|
||||
let res = std::io::stdin().read_exact(buffer);
|
||||
|
||||
if res.is_err() {
|
||||
println!("pressed another button, exiting");
|
||||
}
|
||||
}
|
||||
|
||||
let stop =
|
||||
run_command_pipe_to_terminal(&format!("docker stop {name} && docker container rm {name}"));
|
||||
|
||||
let command = format!("docker run -d --name {name} -p {port}:27017 --network {network} -v {mount}:/data/db{env} --restart {restart} --log-opt max-size=15m --log-opt max-file=3 mongo --quiet");
|
||||
|
||||
let output = run_command_pipe_to_terminal(&command);
|
||||
|
||||
if output.success() {
|
||||
println!("\n✅ {} has been started up ✅\n", "monitor mongo".bold())
|
||||
} else {
|
||||
eprintln!("\n❌ there was some {} on startup ❌\n", "error".red())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_core(sub_matches: &ArgMatches) {
|
||||
let skip_enter = *sub_matches.get_one::<bool>("yes").unwrap_or(&false);
|
||||
|
||||
let config_path = sub_matches
|
||||
.get_one::<String>("config-path")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("~/.monitor/core.config.toml")
|
||||
.to_string();
|
||||
|
||||
let name = sub_matches
|
||||
.get_one::<String>("name")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("monitor-core");
|
||||
|
||||
let port = sub_matches
|
||||
.get_one::<String>("port")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("9000")
|
||||
.parse::<u16>()
|
||||
.expect("invalid port");
|
||||
|
||||
let network = sub_matches
|
||||
.get_one::<String>("network")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("bridge");
|
||||
|
||||
let restart = sub_matches
|
||||
.get_one::<String>("restart")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("unless-stopped")
|
||||
.parse::<RestartMode>()
|
||||
.expect("invalid restart mode");
|
||||
|
||||
let add_host = sub_matches
|
||||
.get_one::<bool>("add-internal-host")
|
||||
.map(|p| *p)
|
||||
.unwrap_or(true);
|
||||
|
||||
println!(
|
||||
"\n===================\n {} \n===================\n",
|
||||
"core config".bold()
|
||||
);
|
||||
println!("{}: {name}", "container name".dimmed());
|
||||
println!("{}: {config_path}", "config path".dimmed());
|
||||
println!("{}: {port}", "port".dimmed());
|
||||
println!("{}: {network}", "network".dimmed());
|
||||
println!("{}: {restart}", "restart".dimmed());
|
||||
println!("{}: {add_host}", "add internal host".dimmed());
|
||||
|
||||
if !skip_enter {
|
||||
println!(
|
||||
"\npress {} to start {}. {}",
|
||||
"ENTER".green().bold(),
|
||||
"monitor core".bold(),
|
||||
"(ctrl-c to cancel)".dimmed()
|
||||
);
|
||||
|
||||
let buffer = &mut [0u8];
|
||||
let res = std::io::stdin().read_exact(buffer);
|
||||
|
||||
if res.is_err() {
|
||||
println!("pressed another button, exiting");
|
||||
}
|
||||
}
|
||||
|
||||
println!("\nstarting monitor core container...\n");
|
||||
|
||||
let _ = run_command_pipe_to_terminal(&format!("docker pull {CORE_IMAGE_NAME}"));
|
||||
|
||||
let _ =
|
||||
run_command_pipe_to_terminal(&format!("docker stop {name} && docker container rm {name}"));
|
||||
|
||||
let add_host = if add_host {
|
||||
" --add-host host.docker.internal:host-gateway"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
||||
let command = format!("docker run -d --name {name} -p {port}:9000 --network {network} -v {config_path}:/config/config.toml --restart {restart}{add_host} {CORE_IMAGE_NAME}");
|
||||
|
||||
let output = run_command_pipe_to_terminal(&command);
|
||||
|
||||
if output.success() {
|
||||
println!("\n✅ {} has been started up ✅\n", "monitor core".bold())
|
||||
} else {
|
||||
eprintln!("\n❌ there was some {} on startup ❌\n", "error".red())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gen_periphery_config(sub_matches: &ArgMatches) {
|
||||
let path = sub_matches
|
||||
.get_one::<String>("path")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("~/.monitor/periphery.config.toml")
|
||||
.to_string();
|
||||
|
||||
let port = sub_matches
|
||||
.get_one::<String>("port")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("8000")
|
||||
.parse::<u16>()
|
||||
.expect("invalid port");
|
||||
|
||||
let stats_polling_rate = sub_matches
|
||||
.get_one::<String>("stats-polling-rate")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("5-sec")
|
||||
.parse::<Timelength>()
|
||||
.expect("invalid timelength");
|
||||
|
||||
let allowed_ips = sub_matches
|
||||
.get_one::<String>("allowed-ips")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("")
|
||||
.split(",")
|
||||
.filter(|ip| ip.len() > 0)
|
||||
.map(|ip| {
|
||||
ip.parse()
|
||||
.expect("given allowed ip address is not valid ip")
|
||||
})
|
||||
.collect::<Vec<IpAddr>>();
|
||||
|
||||
let repo_dir = sub_matches
|
||||
.get_one::<String>("repo-dir")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("~/.monitor/repos")
|
||||
.to_string()
|
||||
.replace("~", env::var("HOME").unwrap().as_str())
|
||||
.parse()
|
||||
.expect("failed to parse --repo_dir as path");
|
||||
|
||||
let config = PeripheryConfig {
|
||||
port,
|
||||
repo_dir,
|
||||
stats_polling_rate,
|
||||
allowed_ips,
|
||||
passkeys: vec![],
|
||||
secrets: Default::default(),
|
||||
github_accounts: Default::default(),
|
||||
docker_accounts: Default::default(),
|
||||
};
|
||||
|
||||
write_to_toml(&path, &config);
|
||||
|
||||
println!(
|
||||
"\n✅ {} generated at {path} ✅\n",
|
||||
"periphery config".bold()
|
||||
);
|
||||
}
|
||||
|
||||
pub fn start_periphery_systemd(sub_matches: &ArgMatches) {
|
||||
let skip_enter = *sub_matches.get_one::<bool>("yes").unwrap_or(&false);
|
||||
|
||||
let install = *sub_matches.get_one::<bool>("install").unwrap_or(&false);
|
||||
|
||||
let config_path = sub_matches
|
||||
.get_one::<String>("config-path")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("~/.monitor/periphery.config.toml")
|
||||
.to_string();
|
||||
|
||||
println!(
|
||||
"\n========================\n {} \n========================\n",
|
||||
"periphery config".bold()
|
||||
);
|
||||
println!("{}: systemd", "run with".dimmed());
|
||||
println!("{}: {config_path}", "config path".dimmed());
|
||||
|
||||
if !skip_enter {
|
||||
println!(
|
||||
"\npress {} to start {}. {}",
|
||||
"ENTER".green().bold(),
|
||||
"monitor periphery".bold(),
|
||||
"(ctrl-c to cancel)".dimmed()
|
||||
);
|
||||
|
||||
let buffer = &mut [0u8];
|
||||
let res = std::io::stdin().read_exact(buffer);
|
||||
|
||||
if res.is_err() {
|
||||
println!("pressed another button, exiting");
|
||||
}
|
||||
}
|
||||
|
||||
if install {
|
||||
install_periphery_from_crates_io();
|
||||
}
|
||||
|
||||
gen_periphery_service_file(&config_path);
|
||||
|
||||
let user = env::var("USER").expect("failed to find $USER env var");
|
||||
|
||||
let command =
|
||||
format!("systemctl --user daemon-reload && systemctl --user enable --now periphery && loginctl enable-linger {user}");
|
||||
|
||||
let output = run_command_pipe_to_terminal(&command);
|
||||
|
||||
if output.success() {
|
||||
println!(
|
||||
"\n✅ {} has been started up ✅\n",
|
||||
"monitor periphery".bold()
|
||||
)
|
||||
} else {
|
||||
eprintln!("\n❌ there was some {} on startup ❌\n", "error".red())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_periphery_daemon(sub_matches: &ArgMatches) {
|
||||
let skip_enter = *sub_matches.get_one::<bool>("yes").unwrap_or(&false);
|
||||
|
||||
let install = *sub_matches.get_one::<bool>("install").unwrap_or(&false);
|
||||
|
||||
let config_path = sub_matches
|
||||
.get_one::<String>("config-path")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("~/.monitor/periphery.config.toml")
|
||||
.to_string();
|
||||
|
||||
let stdout = sub_matches
|
||||
.get_one::<String>("stdout")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("~/.monitor/periphery.log.out")
|
||||
.to_string();
|
||||
|
||||
let stderr = sub_matches
|
||||
.get_one::<String>("stderr")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("~/.monitor/periphery.log.err")
|
||||
.to_string();
|
||||
|
||||
println!(
|
||||
"\n========================\n {} \n========================\n",
|
||||
"periphery config".bold()
|
||||
);
|
||||
println!("{}: daemon", "run as".dimmed());
|
||||
println!("{}: {config_path}", "config path".dimmed());
|
||||
println!("{}: {stdout}", "stdout".dimmed());
|
||||
println!("{}: {stderr}", "stderr".dimmed());
|
||||
|
||||
if !skip_enter {
|
||||
println!(
|
||||
"\npress {} to start {}. {}",
|
||||
"ENTER".green().bold(),
|
||||
"monitor periphery".bold(),
|
||||
"(ctrl-c to cancel)".dimmed()
|
||||
);
|
||||
|
||||
let buffer = &mut [0u8];
|
||||
let res = std::io::stdin().read_exact(buffer);
|
||||
|
||||
if res.is_err() {
|
||||
println!("pressed another button, exiting");
|
||||
}
|
||||
}
|
||||
|
||||
if install {
|
||||
install_periphery_from_crates_io();
|
||||
}
|
||||
|
||||
let command = format!("if pgrep periphery; then pkill periphery; fi && periphery --daemon --config-path {config_path} --stdout {stdout} --stderr {stderr}");
|
||||
|
||||
let output = run_command_pipe_to_terminal(&command);
|
||||
|
||||
if output.success() {
|
||||
println!(
|
||||
"\n✅ {} has been started up ✅\n",
|
||||
"monitor periphery".bold()
|
||||
)
|
||||
} else {
|
||||
eprintln!("\n❌ there was some {} on startup ❌\n", "error".red())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_periphery_container(sub_matches: &ArgMatches) {
|
||||
let skip_enter = *sub_matches.get_one::<bool>("yes").unwrap_or(&false);
|
||||
|
||||
let config_path = sub_matches
|
||||
.get_one::<String>("config-path")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("~/.monitor/periphery.config.toml")
|
||||
.to_string();
|
||||
|
||||
let repo_dir = sub_matches
|
||||
.get_one::<String>("repo-dir")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("~/.monitor/repos")
|
||||
.to_string();
|
||||
|
||||
let name = sub_matches
|
||||
.get_one::<String>("name")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("monitor-periphery");
|
||||
|
||||
let port = sub_matches
|
||||
.get_one::<String>("port")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("8000")
|
||||
.parse::<u16>()
|
||||
.expect("invalid port");
|
||||
|
||||
let network = sub_matches
|
||||
.get_one::<String>("network")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("bridge");
|
||||
|
||||
let restart = sub_matches
|
||||
.get_one::<String>("restart")
|
||||
.map(|p| p.as_str())
|
||||
.unwrap_or("unless-stopped")
|
||||
.parse::<RestartMode>()
|
||||
.expect("invalid restart mode");
|
||||
|
||||
println!(
|
||||
"\n========================\n {} \n========================\n",
|
||||
"periphery config".bold()
|
||||
);
|
||||
println!("{}: container", "run as".dimmed());
|
||||
println!("{}: {name}", "container name".dimmed());
|
||||
println!("{}: {config_path}", "config path".dimmed());
|
||||
println!("{}: {repo_dir}", "repo folder".dimmed());
|
||||
println!("{}: {port}", "port".dimmed());
|
||||
println!("{}: {network}", "network".dimmed());
|
||||
println!("{}: {restart}", "restart".dimmed());
|
||||
|
||||
if !skip_enter {
|
||||
println!(
|
||||
"\npress {} to start {}. {}",
|
||||
"ENTER".green().bold(),
|
||||
"monitor periphery".bold(),
|
||||
"(ctrl-c to cancel)".dimmed()
|
||||
);
|
||||
|
||||
let buffer = &mut [0u8];
|
||||
let res = std::io::stdin().read_exact(buffer);
|
||||
|
||||
if res.is_err() {
|
||||
println!("pressed another button, exiting");
|
||||
}
|
||||
}
|
||||
|
||||
println!("\nstarting monitor periphery container...\n");
|
||||
|
||||
let _ = run_command_pipe_to_terminal(&format!("docker pull {PERIPHERY_IMAGE_NAME}"));
|
||||
|
||||
let _ =
|
||||
run_command_pipe_to_terminal(&format!("docker stop {name} && docker container rm {name}"));
|
||||
|
||||
let command = format!("docker run -d --name {name} -p {port}:8000 --network {network} -v {config_path}:/config/config.toml -v {repo_dir}:/repos -v /var/run/docker.sock:/var/run/docker.sock --restart {restart} {PERIPHERY_IMAGE_NAME}");
|
||||
|
||||
let output = run_command_pipe_to_terminal(&command);
|
||||
|
||||
if output.success() {
|
||||
println!(
|
||||
"\n✅ {} has been started up ✅\n",
|
||||
"monitor periphery".bold()
|
||||
)
|
||||
} else {
|
||||
eprintln!("\n❌ there was some {} on startup ❌\n", "error".red())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gen_periphery_service_file(config_path: &str) {
|
||||
let home = env::var("HOME").expect("failed to find $HOME env var");
|
||||
let _ = std::fs::create_dir_all(format!("{home}/.config/systemd/user"));
|
||||
let mut file = File::create(format!("{home}/.config/systemd/user/periphery.service"))
|
||||
.expect("failed to create user systemd unit file");
|
||||
file.write_all(periphery_unit_file(config_path).as_bytes())
|
||||
.expect("failed to write config file");
|
||||
}
|
||||
|
||||
fn write_to_toml(path: &str, toml: impl Serialize) {
|
||||
let path = PathBuf::from_str(&path.replace("~", &std::env::var("HOME").unwrap()))
|
||||
.expect("not a valid path");
|
||||
let _ = fs::create_dir_all(pop_path(&path));
|
||||
fs::write(
|
||||
path,
|
||||
toml::to_string(&toml).expect("failed to parse config into toml"),
|
||||
)
|
||||
.expect("❌ failed to write toml to file ❌");
|
||||
}
|
||||
|
||||
fn pop_path(path: &PathBuf) -> PathBuf {
|
||||
let mut clone = path.clone();
|
||||
clone.pop();
|
||||
clone
|
||||
}
|
||||
|
||||
fn generate_secret(length: usize) -> String {
|
||||
rand::thread_rng()
|
||||
.sample_iter(&Alphanumeric)
|
||||
.take(length)
|
||||
.map(char::from)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn periphery_unit_file(config_path: &str) -> String {
|
||||
let home = env::var("HOME").expect("failed to find $HOME env var");
|
||||
let user = env::var("USER").expect("failed to find $USER env var");
|
||||
format!(
|
||||
"[Unit]
|
||||
Description=agent to connect with monitor core
|
||||
|
||||
[Service]
|
||||
ExecStart={home}/.monitor/bin/periphery --config-path {config_path} --home-dir {home}
|
||||
Restart=on-failure
|
||||
TimeoutStartSec=0
|
||||
|
||||
[Install]
|
||||
WantedBy=default.target"
|
||||
)
|
||||
}
|
||||
|
||||
fn install_periphery_from_crates_io() {
|
||||
println!("\ninstalling periphery binary...\n");
|
||||
|
||||
let install_output = run_command_pipe_to_terminal(&format!("cargo install {PERIPHERY_CRATE}"));
|
||||
|
||||
if install_output.success() {
|
||||
println!("\ninstallation finished, starting monitor periphery daemon\n");
|
||||
} else {
|
||||
panic!(
|
||||
"\n❌ there was some {} during periphery installation ❌\n",
|
||||
"error".red()
|
||||
)
|
||||
}
|
||||
}
|
||||
-252
@@ -1,252 +0,0 @@
|
||||
#![allow(unused)]
|
||||
|
||||
use clap::{arg, Arg, Command};
|
||||
|
||||
mod helpers;
|
||||
|
||||
use helpers::*;
|
||||
|
||||
fn cli() -> Command {
|
||||
Command::new("monitor")
|
||||
.about("\na cli to set up monitor components, like the periphery client")
|
||||
.version(env!("CARGO_PKG_VERSION"))
|
||||
.subcommand_required(true)
|
||||
.arg_required_else_help(true)
|
||||
.allow_external_subcommands(true)
|
||||
.subcommand(
|
||||
Command::new("core")
|
||||
.about("tools to set up monitor core")
|
||||
.subcommand_required(true)
|
||||
.arg_required_else_help(true)
|
||||
.allow_external_subcommands(true)
|
||||
.subcommand(
|
||||
Command::new("gen-config")
|
||||
.about("generate a core config file")
|
||||
.arg(
|
||||
arg!(--host <HOST> "the host to use with oauth redirect url, whatever host the user hits to access monitor. eg 'https://monitor.mogh.tech'")
|
||||
.required(true)
|
||||
)
|
||||
.arg(
|
||||
arg!(--path <PATH> "sets path of generated config file. default is '~/.monitor/core.config.toml'")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--port <PORT> "sets port core will run on. default is 9000. if running in docker, keep this port as is, set the external port when running core start command")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--"mongo-uri" <URI> "sets the mongo uri to use. default is 'mongodb://monitor-mongo'")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--"mongo-db-name" <NAME> "sets the db name to use. default is 'monitor'")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--"jwt-valid-for" <TIMELENGTH> "sets the length of time jwt stays valid for. default is 1-wk (one week)")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--"slack-url" <URL> "sets the slack url to use for slack notifications")
|
||||
.required(false)
|
||||
),
|
||||
)
|
||||
.subcommand(
|
||||
Command::new("start-mongo")
|
||||
.about("start up a local mongo container for monitor core")
|
||||
.arg(
|
||||
arg!(--yes "used in scripts to skip 'enter to continue' step")
|
||||
)
|
||||
.arg(
|
||||
arg!(--name <NAME> "specify the name of the mongo container. default is monitor-mongo")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--username <USERNAME> "specify the admin username for mongo. default is mongo with no auth")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--password <PASSWORD> "specify the admin password for mongo. default is mongo with no auth")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--port <PORT> "sets port mongo will run on. default is 27017")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--mount <PATH> "sets the path the mongo data is mounted into. default is ~/.monitor/db")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--network <NETWORK> "sets docker network of mongo container. default is bridge")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--restart <RESTART> "sets docker restart mode of mongo container. default is unless-stopped")
|
||||
)
|
||||
)
|
||||
.subcommand(
|
||||
Command::new("start")
|
||||
.about("start up monitor core in container")
|
||||
.arg(
|
||||
arg!(--yes "used in scripts to skip 'enter to continue' step")
|
||||
)
|
||||
.arg(
|
||||
arg!(--name <NAME> "specify the name of the monitor core container. default is monitor-core")
|
||||
)
|
||||
.arg(
|
||||
arg!(--"config-path" <PATH> "specify the file path to use for config. default is ~/.monitor/core.config.toml")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--port <PORT> "sets port monitor core will run on. default is 9000")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--network <NETWORK> "sets docker network of monitor core container. default is bridge")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--restart <RESTART> "sets docker restart mode of monitor core container. default is unless-stopped")
|
||||
)
|
||||
.arg(
|
||||
arg!(--"add-internal-host" "adds the docker flag '--add-host=host.docker.internal:host-gateway'. default is true")
|
||||
)
|
||||
),
|
||||
)
|
||||
.subcommand(
|
||||
Command::new("periphery")
|
||||
.about("tools to set up monitor periphery")
|
||||
.subcommand_required(true)
|
||||
.arg_required_else_help(true)
|
||||
.allow_external_subcommands(true)
|
||||
.subcommand(
|
||||
Command::new("gen-config")
|
||||
.about("generate a periphery config file")
|
||||
.arg(
|
||||
arg!(--path <PATH> "sets path of generated config file. default is '~/.monitor/periphery.config.toml'")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--port <PORT> "sets port periphery will run on. default is 8000. if running in docker, keep this port as is, set the external port when running periphery start command")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--"stats-polling-rate" <INTERVAL> "sets stats polling rate to control granularity of system stats returned. default is 5-sec. options: 1-sec, 5-sec, 10-sec, 30-sec, 1-min")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--"allowed-ips" <IPS> "used to only accept requests from known ips. give ips as comma seperated list, like '--allowed_ips 127.0.0.1,10.20.30.43'. default is empty, which will not block any ip.")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--"repo-dir" <PATH> "if running in container, this should be '/repos'. default is ~/.monitor/repos").required(false)
|
||||
)
|
||||
)
|
||||
.subcommand(
|
||||
Command::new("start")
|
||||
.about("tools to start periphery as daemon or container")
|
||||
.subcommand(
|
||||
Command::new("systemd")
|
||||
.about("manage periphery with systemd running under current user")
|
||||
.arg(
|
||||
arg!(--yes "used in scripts to skip 'enter to continue' step")
|
||||
)
|
||||
.arg(
|
||||
arg!(--install "specify this to install periphery from crates.io")
|
||||
)
|
||||
.arg(
|
||||
arg!(--"config-path" <PATH> "specify the file path to use for config. default is ~/.monitor/periphery.config.toml")
|
||||
.required(false)
|
||||
)
|
||||
)
|
||||
.subcommand(
|
||||
Command::new("daemon")
|
||||
.about("start up monitor periphery daemon")
|
||||
.arg(
|
||||
arg!(--yes "used in scripts to skip 'enter to continue' step")
|
||||
)
|
||||
.arg(
|
||||
arg!(--install "specify this to install periphery from crates.io")
|
||||
)
|
||||
.arg(
|
||||
arg!(--"config-path" <PATH> "specify the file path to use for config. default is ~/.monitor/periphery.config.toml")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--stdout <PATH> "specify the file path for periphery to log stdout to. default is ~/.monitor/periphery.log.out")
|
||||
.required(false)
|
||||
)
|
||||
.arg(
|
||||
arg!(--stderr <PATH> "specify the file path for periphery to log stderr to. default is ~/.monitor/periphery.log.err")
|
||||
.required(false)
|
||||
)
|
||||
)
|
||||
// .subcommand(
|
||||
// Command::new("container")
|
||||
// .about("start up monitor periphery in docker container")
|
||||
// .arg(
|
||||
// arg!(--yes "used in scripts to skip 'enter to continue' step")
|
||||
// )
|
||||
// .arg(
|
||||
// arg!(--name <NAME> "specify the name of the monitor periphery container. default is monitor-periphery")
|
||||
// )
|
||||
// .arg(
|
||||
// arg!(--"config-path" <PATH> "specify the file path to use for config. default is ~/.monitor/periphery.config.toml")
|
||||
// .required(false)
|
||||
// )
|
||||
// .arg(arg!(--"repo-dir" <PATH> "specify the folder on host to clone repos into. default is ~/.monitor/repos").required(false))
|
||||
// .arg(
|
||||
// arg!(--port <PORT> "sets port monitor periphery will run on. default is 8000")
|
||||
// .required(false)
|
||||
// )
|
||||
// .arg(
|
||||
// arg!(--network <NETWORK> "sets docker network of monitor periphery container. default is bridge")
|
||||
// .required(false)
|
||||
// )
|
||||
// .arg(
|
||||
// arg!(--restart <RESTART> "sets docker restart mode of monitor periphery container. default is unless-stopped")
|
||||
// )
|
||||
// )
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let matches = cli().get_matches();
|
||||
|
||||
match matches.subcommand() {
|
||||
Some(("core", sub_matches)) => {
|
||||
let core_command = sub_matches.subcommand().expect("\n❌ invalid call, should be 'monitor core <gen-config, start-mongo, start> <flags>' ❌\n");
|
||||
match core_command {
|
||||
("gen-config", sub_matches) => gen_core_config(sub_matches),
|
||||
("start-mongo", sub_matches) => start_mongo(sub_matches),
|
||||
("start", sub_matches) => start_core(sub_matches),
|
||||
_ => {
|
||||
println!("\n❌ invalid call, should be 'monitor core <gen-config, start-mongo, start> <flags>' ❌\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(("periphery", sub_matches)) => {
|
||||
let periphery_command = sub_matches.subcommand().expect(
|
||||
"\n❌ invalid call, should be 'monitor periphery <gen-config, start> <flags>' ❌\n",
|
||||
);
|
||||
match periphery_command {
|
||||
("gen-config", sub_matches) => gen_periphery_config(sub_matches),
|
||||
("start", sub_matches) => {
|
||||
let periphery_start_command = sub_matches.subcommand().expect("\n❌ invalid call, should be 'monitor periphery start <daemon, container> <flags>' ❌\n");
|
||||
match periphery_start_command {
|
||||
("systemd", sub_matches) => start_periphery_systemd(sub_matches),
|
||||
("daemon", sub_matches) => start_periphery_daemon(sub_matches),
|
||||
// ("container", sub_matches) => start_periphery_container(sub_matches),
|
||||
_ => println!("\n❌ invalid call, should be 'monitor periphery start <daemon, container> <flags>' ❌\n")
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
println!("\n❌ invalid call, should be 'monitor periphery <gen-config, start>...' ❌\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => println!("\n❌ invalid call, should be 'monitor <core, periphery> ...' ❌\n"),
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
# optional. this will be the document title on the web page (shows up as text in the browser tab). default is 'monitor'
|
||||
title = "monitor"
|
||||
|
||||
# this should be the url used to access monitor in browser, potentially behind DNS, eg https://monitor.mogh.tech or http://12.34.56.78:9000
|
||||
host = "https://monitor.mogh.tech"
|
||||
|
||||
# the port the core system will run on. if running core in docker container, leave as this port as 9000 and use port bind eg. -p 9001:9000
|
||||
port = 9000
|
||||
|
||||
# daily utc offset in hours to send daily update. eg 8:00 eastern time is 13:00 UTC, so offset should be 13. default of 0 runs at UTC midnight.
|
||||
daily_offset_hours = 13
|
||||
|
||||
# number of days to keep stats around, or 0 to disable pruning. stats older than this number of days are deleted daily
|
||||
keep_stats_for_days = 120
|
||||
|
||||
# secret used to generate the jwt. should be some randomly generated hash.
|
||||
jwt_secret = "your_jwt_secret"
|
||||
|
||||
# can be 1-hr, 12-hr, 1-day, 3-day, 1-wk, 2-wk, 30-day
|
||||
jwt_valid_for = "1-wk"
|
||||
|
||||
# webhook url given by slack app
|
||||
slack_url = "your_slack_app_webhook_url"
|
||||
|
||||
# token that has to be given to github during webhook config as the secret
|
||||
github_webhook_secret = "your_random_webhook_secret"
|
||||
|
||||
# optional. an alternate base url that is used to recieve github webhook requests. if not provided, will use 'host' address as base
|
||||
github_webhook_base_url = "https://monitor-github-webhook.mogh.tech"
|
||||
|
||||
# token used to authenticate core requests to periphery
|
||||
passkey = "your_random_passkey"
|
||||
|
||||
# can be 30-sec, 1-min, 2-min, 5-min
|
||||
monitoring_interval = "1-min"
|
||||
|
||||
# allow or deny user login with username / password
|
||||
local_auth = true
|
||||
|
||||
# these will be given in the GUI to attach to builds. New build docker orgs will default to first org (or none if empty).
|
||||
docker_organizations = ["your_docker_org1", "your_docker_org_2"]
|
||||
|
||||
[aws]
|
||||
access_key_id = "your_aws_key_id"
|
||||
secret_access_key = "your_aws_secret_key"
|
||||
default_region = "us-east-1"
|
||||
default_ami_id = "your_periphery_ami"
|
||||
default_key_pair_name = "your_default_key_pair_name"
|
||||
default_instance_type = "m5.2xlarge"
|
||||
default_volume_gb = 8
|
||||
default_subnet_id = "your_default_subnet_id"
|
||||
default_security_group_ids = ["sg_id_1", "sg_id_2"]
|
||||
default_assign_public_ip = false
|
||||
|
||||
[aws.available_ami_accounts]
|
||||
your_periphery_ami = { name = "default ami", github = ["github_username"], docker = ["docker_username"] }
|
||||
|
||||
[github_oauth]
|
||||
enabled = true
|
||||
id = "your_github_client_id"
|
||||
secret = "your_github_client_secret"
|
||||
|
||||
[google_oauth]
|
||||
enabled = true
|
||||
id = "your_google_client_id"
|
||||
secret = "your_google_client_secret"
|
||||
|
||||
[mongo]
|
||||
uri = "your_mongo_uri"
|
||||
app_name = "monitor_core"
|
||||
db_name = "monitor"
|
||||
@@ -1 +0,0 @@
|
||||
CONFIG_PATH=../config/core.config.example.toml # optional, default is /config/config.toml. this is usually bind mounted into the container
|
||||
@@ -1,16 +0,0 @@
|
||||
port = 8000 # optional. 8000 is default
|
||||
repo_dir = "/repos" # optional. /repos is default. no reason to change if running the docker container, just mount your desired repo dir to /repos in the container
|
||||
stats_polling_rate = "5-sec" # optional. 5-sec is default. can use 1-sec, 5-sec, 10-sec, 30-sec, 1-min. controls granularity of system stats recorded
|
||||
allowed_ips = ["127.0.0.1"] # optional. default is empty, which will not block any request by ip.
|
||||
passkeys = ["abcdefghijk"] # optional. default is empty, which will not require any passkey to be passed by core.
|
||||
|
||||
[secrets] # optional. can inject these values into your deployments configuration.
|
||||
secret_variable = "secret_value"
|
||||
|
||||
[github_accounts] # optional
|
||||
github_username1 = "github_token1"
|
||||
github_username2 = "github_token2"
|
||||
|
||||
[docker_accounts] # optional
|
||||
docker_username1 = "docker_token1"
|
||||
docker_username2 = "docker_token2"
|
||||
@@ -1 +0,0 @@
|
||||
CONFIG_PATH=../config/periphery.config.example.toml # optional, default is /config/config.toml. this is usually bind mounted into the container
|
||||
@@ -1,40 +0,0 @@
|
||||
[package]
|
||||
name = "core"
|
||||
version = "0.3.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
helpers = { package = "monitor_helpers", path = "../lib/helpers" }
|
||||
types = { package = "monitor_types", path = "../lib/types" }
|
||||
db = { package = "db_client", path = "../lib/db_client" }
|
||||
periphery = { package = "periphery_client", path = "../lib/periphery_client" }
|
||||
axum_oauth2 = { path = "../lib/axum_oauth2" }
|
||||
tokio = { version = "1.26", features = ["full"] }
|
||||
tokio-tungstenite = { version = "0.18", features=["native-tls"] }
|
||||
tokio-util = "0.7"
|
||||
axum = { version = "0.6", features = ["ws", "json"] }
|
||||
axum-extra = { version = "0.5.0", features = ["spa"] }
|
||||
tower = { version = "0.4", features = ["full"] }
|
||||
tower-http = { version = "0.4.0", features = ["cors"] }
|
||||
slack = { package = "slack_client_rs", version = "0.0.8" }
|
||||
mungos = "0.3.14"
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
serde_json = "1.0"
|
||||
dotenv = "0.15"
|
||||
envy = "0.4"
|
||||
anyhow = "1.0"
|
||||
bcrypt = "0.14"
|
||||
jwt = "0.16"
|
||||
hmac = "0.12"
|
||||
sha2 = "0.10"
|
||||
async_timing_util = "0.1.14"
|
||||
futures-util = "0.3"
|
||||
diff-struct = "0.5"
|
||||
typeshare = "1.0.0"
|
||||
hex = "0.4"
|
||||
aws-config = "0.55"
|
||||
aws-sdk-ec2 = "0.26"
|
||||
merge_config_files = "0.1.3"
|
||||
@@ -1,23 +0,0 @@
|
||||
FROM rust:latest as builder
|
||||
WORKDIR /builder
|
||||
|
||||
COPY ./core ./core
|
||||
|
||||
COPY ./lib/types ./lib/types
|
||||
COPY ./lib/helpers ./lib/helpers
|
||||
|
||||
COPY ./lib/db_client ./lib/db_client
|
||||
COPY ./lib/periphery_client ./lib/periphery_client
|
||||
COPY ./lib/axum_oauth2 ./lib/axum_oauth2
|
||||
|
||||
RUN cd core && cargo build --release
|
||||
|
||||
FROM gcr.io/distroless/cc
|
||||
|
||||
COPY ./frontend/build /frontend
|
||||
|
||||
COPY --from=builder /builder/core/target/release/core /
|
||||
|
||||
EXPOSE 9000
|
||||
|
||||
CMD ["./core"]
|
||||
@@ -1,610 +0,0 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{anyhow, Context};
|
||||
use aws_sdk_ec2::Client;
|
||||
use diff::Diff;
|
||||
use helpers::{all_logs_success, to_monitor_name};
|
||||
use mungos::{doc, to_bson};
|
||||
use types::{
|
||||
monitor_timestamp,
|
||||
traits::{Busy, Permissioned},
|
||||
AwsBuilderBuildConfig, Build, Log, Operation, PermissionLevel, Update, UpdateStatus,
|
||||
UpdateTarget, Version,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
auth::RequestUser,
|
||||
cloud::aws::{
|
||||
create_ec2_client, create_instance_with_ami, terminate_ec2_instance, Ec2Instance,
|
||||
},
|
||||
helpers::empty_or_only_spaces,
|
||||
state::State,
|
||||
};
|
||||
|
||||
const BUILDER_POLL_RATE_SECS: u64 = 2;
|
||||
const BUILDER_POLL_MAX_TRIES: usize = 30;
|
||||
|
||||
impl State {
|
||||
pub async fn get_build_check_permissions(
|
||||
&self,
|
||||
build_id: &str,
|
||||
user: &RequestUser,
|
||||
permission_level: PermissionLevel,
|
||||
) -> anyhow::Result<Build> {
|
||||
let build = self.db.get_build(build_id).await?;
|
||||
let permissions = build.get_user_permissions(&user.id);
|
||||
if user.is_admin || permissions >= permission_level {
|
||||
Ok(build)
|
||||
} else {
|
||||
Err(anyhow!(
|
||||
"user does not have required permissions on this build"
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn build_busy(&self, id: &str) -> bool {
|
||||
match self.build_action_states.lock().await.get(id) {
|
||||
Some(a) => a.busy(),
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_build(&self, name: &str, user: &RequestUser) -> anyhow::Result<Build> {
|
||||
if !user.is_admin && !user.create_build_permissions {
|
||||
return Err(anyhow!("user does not have permission to create builds"));
|
||||
}
|
||||
let start_ts = monitor_timestamp();
|
||||
let build = Build {
|
||||
name: to_monitor_name(name),
|
||||
docker_organization: self
|
||||
.config
|
||||
.docker_organizations
|
||||
.get(0)
|
||||
.map(|d| d.to_string()),
|
||||
aws_config: Some(AwsBuilderBuildConfig::default()),
|
||||
permissions: [(user.id.clone(), PermissionLevel::Update)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
last_built_at: "never".to_string(),
|
||||
created_at: start_ts.clone(),
|
||||
updated_at: start_ts.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
let build_id = self
|
||||
.db
|
||||
.builds
|
||||
.create_one(build)
|
||||
.await
|
||||
.context("failed at adding build to db")?;
|
||||
let build = self.db.get_build(&build_id).await?;
|
||||
let update = Update {
|
||||
target: UpdateTarget::Build(build_id),
|
||||
operation: Operation::CreateBuild,
|
||||
start_ts,
|
||||
end_ts: Some(monitor_timestamp()),
|
||||
operator: user.id.clone(),
|
||||
success: true,
|
||||
..Default::default()
|
||||
};
|
||||
self.add_update(update).await?;
|
||||
Ok(build)
|
||||
}
|
||||
|
||||
pub async fn create_full_build(
|
||||
&self,
|
||||
mut build: Build,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Build> {
|
||||
build.id = self.create_build(&build.name, user).await?.id;
|
||||
let build = self.update_build(build, user).await?;
|
||||
Ok(build)
|
||||
}
|
||||
|
||||
pub async fn copy_build(
|
||||
&self,
|
||||
target_id: &str,
|
||||
new_name: String,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Build> {
|
||||
let mut build = self
|
||||
.get_build_check_permissions(target_id, user, PermissionLevel::Update)
|
||||
.await?;
|
||||
build.name = new_name;
|
||||
build.version = Version::default();
|
||||
let build = self.create_full_build(build, user).await?;
|
||||
Ok(build)
|
||||
}
|
||||
|
||||
pub async fn delete_build(&self, build_id: &str, user: &RequestUser) -> anyhow::Result<Build> {
|
||||
if self.build_busy(build_id).await {
|
||||
return Err(anyhow!("build busy"));
|
||||
}
|
||||
let build = self
|
||||
.get_build_check_permissions(build_id, user, PermissionLevel::Update)
|
||||
.await?;
|
||||
let start_ts = monitor_timestamp();
|
||||
self.db.builds.delete_one(build_id).await?;
|
||||
let update = Update {
|
||||
target: UpdateTarget::Build(build_id.to_string()),
|
||||
operation: Operation::DeleteBuild,
|
||||
start_ts,
|
||||
end_ts: Some(monitor_timestamp()),
|
||||
operator: user.id.clone(),
|
||||
logs: vec![Log::simple(
|
||||
"delete build",
|
||||
format!("deleted build {}", build.name),
|
||||
)],
|
||||
success: true,
|
||||
..Default::default()
|
||||
};
|
||||
self.add_update(update).await?;
|
||||
Ok(build)
|
||||
}
|
||||
|
||||
pub async fn update_build(
|
||||
&self,
|
||||
new_build: Build,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Build> {
|
||||
if self.build_busy(&new_build.id).await {
|
||||
return Err(anyhow!("build busy"));
|
||||
}
|
||||
let id = new_build.id.clone();
|
||||
{
|
||||
let mut lock = self.build_action_states.lock().await;
|
||||
let entry = lock.entry(id.clone()).or_default();
|
||||
entry.updating = true;
|
||||
}
|
||||
let res = self.update_build_inner(new_build, user).await;
|
||||
{
|
||||
let mut lock = self.build_action_states.lock().await;
|
||||
let entry = lock.entry(id).or_default();
|
||||
entry.updating = false;
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
async fn update_build_inner(
|
||||
&self,
|
||||
mut new_build: Build,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Build> {
|
||||
let start_ts = monitor_timestamp();
|
||||
let current_build = self
|
||||
.get_build_check_permissions(&new_build.id, user, PermissionLevel::Update)
|
||||
.await?;
|
||||
|
||||
if let Some(new_server_id) = &new_build.server_id {
|
||||
if current_build.server_id.is_none()
|
||||
|| new_server_id != current_build.server_id.as_ref().unwrap()
|
||||
{
|
||||
self.get_server_check_permissions(new_server_id, user, PermissionLevel::Update)
|
||||
.await
|
||||
.context("user does not have permission to attach build to this server")?;
|
||||
}
|
||||
}
|
||||
|
||||
// none of these should be changed through this method
|
||||
new_build.name = current_build.name.clone();
|
||||
new_build.permissions = current_build.permissions.clone();
|
||||
new_build.last_built_at = current_build.last_built_at.clone();
|
||||
new_build.created_at = current_build.created_at.clone();
|
||||
new_build.updated_at = start_ts.clone();
|
||||
|
||||
// filter out any build args that contain empty strings
|
||||
// these could only happen by accident
|
||||
new_build.docker_build_args = new_build.docker_build_args.map(|mut args| {
|
||||
args.build_args = args
|
||||
.build_args
|
||||
.into_iter()
|
||||
.filter(|a| !empty_or_only_spaces(&a.variable) && !empty_or_only_spaces(&a.value))
|
||||
.collect();
|
||||
args
|
||||
});
|
||||
|
||||
self.db
|
||||
.builds
|
||||
.update_one(&new_build.id, mungos::Update::Regular(new_build.clone()))
|
||||
.await
|
||||
.context("failed at update one build")?;
|
||||
|
||||
let diff = current_build.diff(&new_build);
|
||||
|
||||
let update = Update {
|
||||
operation: Operation::UpdateBuild,
|
||||
target: UpdateTarget::Build(new_build.id.clone()),
|
||||
start_ts,
|
||||
status: UpdateStatus::Complete,
|
||||
logs: vec![Log::simple(
|
||||
"build update",
|
||||
serde_json::to_string_pretty(&diff).unwrap(),
|
||||
)],
|
||||
operator: user.id.clone(),
|
||||
end_ts: Some(monitor_timestamp()),
|
||||
success: true,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// update.id = self.add_update(update.clone()).await?;
|
||||
|
||||
// if any_option_diff_is_some(&[&diff.repo, &diff.branch, &diff.github_account])
|
||||
// || option_diff_is_some(&diff.on_clone)
|
||||
// {
|
||||
// let server = self.db.get_server(¤t_build.server_id).await?;
|
||||
// match self.periphery.clone_repo(&server, &new_build).await {
|
||||
// Ok(clone_logs) => {
|
||||
// update.logs.extend(clone_logs);
|
||||
// }
|
||||
// Err(e) => update
|
||||
// .logs
|
||||
// .push(Log::error("cloning repo", format!("{e:#?}"))),
|
||||
// }
|
||||
// }
|
||||
|
||||
// update.end_ts = Some(monitor_timestamp());
|
||||
// update.success = all_logs_success(&update.logs);
|
||||
// update.status = UpdateStatus::Complete;
|
||||
|
||||
self.add_update(update).await?;
|
||||
|
||||
Ok(new_build)
|
||||
}
|
||||
|
||||
pub async fn build(&self, build_id: &str, user: &RequestUser) -> anyhow::Result<Update> {
|
||||
if self.build_busy(build_id).await {
|
||||
return Err(anyhow!("build busy"));
|
||||
}
|
||||
{
|
||||
let mut lock = self.build_action_states.lock().await;
|
||||
let entry = lock.entry(build_id.to_string()).or_default();
|
||||
entry.building = true;
|
||||
}
|
||||
let res = self.build_inner(build_id, user).await;
|
||||
{
|
||||
let mut lock = self.build_action_states.lock().await;
|
||||
let entry = lock.entry(build_id.to_string()).or_default();
|
||||
entry.building = false;
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
async fn build_inner(&self, build_id: &str, user: &RequestUser) -> anyhow::Result<Update> {
|
||||
let mut build = self
|
||||
.get_build_check_permissions(build_id, user, PermissionLevel::Execute)
|
||||
.await?;
|
||||
build.version.increment();
|
||||
let mut update = Update {
|
||||
target: UpdateTarget::Build(build_id.to_string()),
|
||||
operation: Operation::BuildBuild,
|
||||
start_ts: monitor_timestamp(),
|
||||
status: UpdateStatus::InProgress,
|
||||
operator: user.id.clone(),
|
||||
success: true,
|
||||
version: build.version.clone().into(),
|
||||
..Default::default()
|
||||
};
|
||||
update.id = self.add_update(update.clone()).await?;
|
||||
|
||||
let (server, aws_client) = if let Some(server_id) = &build.server_id {
|
||||
let server = self.db.get_server(server_id).await;
|
||||
if let Err(e) = server {
|
||||
update.status = UpdateStatus::Complete;
|
||||
update.end_ts = Some(monitor_timestamp());
|
||||
update.success = false;
|
||||
update
|
||||
.logs
|
||||
.push(Log::error("get build server", format!("{e:#?}")));
|
||||
self.update_update(update.clone()).await?;
|
||||
return Err(e);
|
||||
}
|
||||
let server = Ec2Instance {
|
||||
instance_id: String::new(),
|
||||
server: server.unwrap(),
|
||||
};
|
||||
(server, None)
|
||||
} else if build.aws_config.is_some() {
|
||||
let start_ts = monitor_timestamp();
|
||||
let res = self.create_ec2_instance_for_build(&build).await;
|
||||
if let Err(e) = res {
|
||||
update.status = UpdateStatus::Complete;
|
||||
update.end_ts = Some(monitor_timestamp());
|
||||
update.success = false;
|
||||
update.logs.push(Log {
|
||||
stage: "start build server".to_string(),
|
||||
stderr: format!("{e:#?}"),
|
||||
success: false,
|
||||
start_ts,
|
||||
end_ts: monitor_timestamp(),
|
||||
..Default::default()
|
||||
});
|
||||
self.update_update(update).await?;
|
||||
return Err(e);
|
||||
}
|
||||
let (server, aws_client, logs) = res.unwrap();
|
||||
update.logs.extend(logs);
|
||||
self.update_update(update.clone()).await?;
|
||||
(server, aws_client)
|
||||
} else {
|
||||
update.status = UpdateStatus::Complete;
|
||||
update.end_ts = Some(monitor_timestamp());
|
||||
update.success = false;
|
||||
update.logs.push(Log::error(
|
||||
"start build",
|
||||
"build has neither server_id nor aws_config attached".to_string(),
|
||||
));
|
||||
self.update_update(update).await?;
|
||||
return Err(anyhow!(
|
||||
"build has neither server_id or aws_config attached"
|
||||
));
|
||||
};
|
||||
|
||||
let clone_success = match self.periphery.clone_repo(&server.server, &build).await {
|
||||
Ok(clone_logs) => {
|
||||
update.logs.extend(clone_logs);
|
||||
all_logs_success(&update.logs)
|
||||
}
|
||||
Err(e) => {
|
||||
update
|
||||
.logs
|
||||
.push(Log::error("clone repo", format!("{e:#?}")));
|
||||
false
|
||||
}
|
||||
};
|
||||
|
||||
if !clone_success {
|
||||
let _ = self
|
||||
.periphery
|
||||
.delete_repo(&server.server, &build.name)
|
||||
.await;
|
||||
if let Some(aws_client) = aws_client {
|
||||
self.terminate_ec2_instance(aws_client, &server, &mut update)
|
||||
.await;
|
||||
}
|
||||
update.status = UpdateStatus::Complete;
|
||||
update.end_ts = Some(monitor_timestamp());
|
||||
update.success = false;
|
||||
self.update_update(update.clone()).await?;
|
||||
return Ok(update);
|
||||
}
|
||||
|
||||
self.update_update(update.clone()).await?;
|
||||
|
||||
let build_logs = match self
|
||||
.periphery
|
||||
.build(&server.server, &build)
|
||||
.await
|
||||
.context("failed at call to periphery to build")
|
||||
{
|
||||
Ok(logs) => logs,
|
||||
Err(e) => Some(vec![Log::error("build", format!("{e:#?}"))]),
|
||||
};
|
||||
|
||||
match build_logs {
|
||||
Some(logs) => {
|
||||
let success = all_logs_success(&logs);
|
||||
update.logs.extend(logs);
|
||||
if success {
|
||||
let _ = self
|
||||
.db
|
||||
.builds
|
||||
.update_one::<Build>(
|
||||
build_id,
|
||||
mungos::Update::Set(doc! {
|
||||
"version": to_bson(&build.version)
|
||||
.context("failed at converting version to bson")?,
|
||||
"last_built_at": monitor_timestamp(),
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
None => {
|
||||
update
|
||||
.logs
|
||||
.push(Log::error("build", "builder busy".to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
let _ = self
|
||||
.periphery
|
||||
.delete_repo(&server.server, &build.name)
|
||||
.await;
|
||||
|
||||
if let Some(aws_client) = aws_client {
|
||||
self.terminate_ec2_instance(aws_client, &server, &mut update)
|
||||
.await;
|
||||
}
|
||||
|
||||
update.success = all_logs_success(&update.logs);
|
||||
update.status = UpdateStatus::Complete;
|
||||
update.end_ts = Some(monitor_timestamp());
|
||||
|
||||
self.update_update(update.clone()).await?;
|
||||
|
||||
Ok(update)
|
||||
}
|
||||
|
||||
async fn create_ec2_instance_for_build(
|
||||
&self,
|
||||
build: &Build,
|
||||
) -> anyhow::Result<(Ec2Instance, Option<Client>, Vec<Log>)> {
|
||||
if build.aws_config.is_none() {
|
||||
return Err(anyhow!("build has no aws_config attached"));
|
||||
}
|
||||
let start_instance_ts = monitor_timestamp();
|
||||
let aws_config = build.aws_config.as_ref().unwrap();
|
||||
let region = aws_config
|
||||
.region
|
||||
.as_ref()
|
||||
.unwrap_or(&self.config.aws.default_region)
|
||||
.to_string();
|
||||
let aws_client = create_ec2_client(
|
||||
region,
|
||||
&self.config.aws.access_key_id,
|
||||
self.config.aws.secret_access_key.clone(),
|
||||
)
|
||||
.await;
|
||||
let ami_name = aws_config
|
||||
.ami_name
|
||||
.as_ref()
|
||||
.unwrap_or(&self.config.aws.default_ami_name);
|
||||
let ami_id = &self
|
||||
.config
|
||||
.aws
|
||||
.available_ami_accounts
|
||||
.get(ami_name)
|
||||
.ok_or(anyhow!("no ami id associated with ami name {ami_name}"))?
|
||||
.ami_id;
|
||||
let instance_type = aws_config
|
||||
.instance_type
|
||||
.as_ref()
|
||||
.unwrap_or(&self.config.aws.default_instance_type);
|
||||
let subnet_id = aws_config
|
||||
.subnet_id
|
||||
.as_ref()
|
||||
.unwrap_or(&self.config.aws.default_subnet_id);
|
||||
let security_group_ids = aws_config
|
||||
.security_group_ids
|
||||
.as_ref()
|
||||
.unwrap_or(&self.config.aws.default_security_group_ids)
|
||||
.to_owned();
|
||||
let readable_sec_group_ids = security_group_ids.join(", ");
|
||||
let volume_size_gb = *aws_config
|
||||
.volume_gb
|
||||
.as_ref()
|
||||
.unwrap_or(&self.config.aws.default_volume_gb);
|
||||
let key_pair_name = aws_config
|
||||
.key_pair_name
|
||||
.as_ref()
|
||||
.unwrap_or(&self.config.aws.default_key_pair_name);
|
||||
let assign_public_ip = *aws_config
|
||||
.assign_public_ip
|
||||
.as_ref()
|
||||
.unwrap_or(&self.config.aws.default_assign_public_ip);
|
||||
let instance = create_instance_with_ami(
|
||||
&aws_client,
|
||||
&format!("BUILDER-{}-v{}", build.name, build.version.to_string()),
|
||||
ami_id,
|
||||
instance_type,
|
||||
subnet_id,
|
||||
security_group_ids,
|
||||
volume_size_gb,
|
||||
key_pair_name,
|
||||
assign_public_ip,
|
||||
)
|
||||
.await?;
|
||||
let instance_id = &instance.instance_id;
|
||||
let start_log = Log {
|
||||
stage: "start build instance".to_string(),
|
||||
success: true,
|
||||
stdout: format!("instance id: {instance_id}\nami id: {ami_id}\ninstance type: {instance_type}\nvolume size: {volume_size_gb} GB\nsubnet id: {subnet_id}\nsecurity groups: {readable_sec_group_ids}"),
|
||||
start_ts: start_instance_ts,
|
||||
end_ts: monitor_timestamp(),
|
||||
..Default::default()
|
||||
};
|
||||
let start_connect_ts = monitor_timestamp();
|
||||
let mut res = Ok(String::new());
|
||||
for _ in 0..BUILDER_POLL_MAX_TRIES {
|
||||
let version = self.periphery.get_version(&instance.server).await;
|
||||
if let Ok(version) = version {
|
||||
let connect_log = Log {
|
||||
stage: "build instance connected".to_string(),
|
||||
success: true,
|
||||
stdout: format!("established contact with periphery on builder\nperiphery version: v{version}"),
|
||||
start_ts: start_connect_ts,
|
||||
end_ts: monitor_timestamp(),
|
||||
..Default::default()
|
||||
};
|
||||
return Ok((instance, Some(aws_client), vec![start_log, connect_log]));
|
||||
}
|
||||
res = version;
|
||||
tokio::time::sleep(Duration::from_secs(BUILDER_POLL_RATE_SECS)).await;
|
||||
}
|
||||
let _ = terminate_ec2_instance(&aws_client, &instance.instance_id).await;
|
||||
Err(anyhow!(
|
||||
"unable to reach periphery agent on build server\n{res:#?}"
|
||||
))
|
||||
}
|
||||
|
||||
async fn terminate_ec2_instance(
|
||||
&self,
|
||||
aws_client: Client,
|
||||
server: &Ec2Instance,
|
||||
update: &mut Update,
|
||||
) {
|
||||
let res = terminate_ec2_instance(&aws_client, &server.instance_id).await;
|
||||
if let Err(e) = res {
|
||||
update
|
||||
.logs
|
||||
.push(Log::error("terminate instance", format!("{e:#?}")))
|
||||
} else {
|
||||
update.logs.push(Log::simple(
|
||||
"terminate instance",
|
||||
format!("terminate instance id {}", server.instance_id),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
// pub async fn reclone_build(
|
||||
// &self,
|
||||
// build_id: &str,
|
||||
// user: &RequestUser,
|
||||
// ) -> anyhow::Result<Update> {
|
||||
// if self.build_busy(build_id).await {
|
||||
// return Err(anyhow!("build busy"));
|
||||
// }
|
||||
// {
|
||||
// let mut lock = self.build_action_states.lock().await;
|
||||
// let entry = lock.entry(build_id.to_string()).or_default();
|
||||
// entry.recloning = true;
|
||||
// }
|
||||
// let res = self.reclone_build_inner(build_id, user).await;
|
||||
// {
|
||||
// let mut lock = self.build_action_states.lock().await;
|
||||
// let entry = lock.entry(build_id.to_string()).or_default();
|
||||
// entry.recloning = false;
|
||||
// }
|
||||
// res
|
||||
// }
|
||||
|
||||
// async fn reclone_build_inner(
|
||||
// &self,
|
||||
// build_id: &str,
|
||||
// user: &RequestUser,
|
||||
// ) -> anyhow::Result<Update> {
|
||||
// let build = self
|
||||
// .get_build_check_permissions(build_id, user, PermissionLevel::Update)
|
||||
// .await?;
|
||||
// let server = self.db.get_server(&build.server_id).await?;
|
||||
// let mut update = Update {
|
||||
// target: UpdateTarget::Build(build_id.to_string()),
|
||||
// operation: Operation::RecloneBuild,
|
||||
// start_ts: monitor_timestamp(),
|
||||
// status: UpdateStatus::InProgress,
|
||||
// operator: user.id.clone(),
|
||||
// success: true,
|
||||
// ..Default::default()
|
||||
// };
|
||||
// update.id = self.add_update(update.clone()).await?;
|
||||
|
||||
// update.success = match self.periphery.clone_repo(&server, &build).await {
|
||||
// Ok(clone_logs) => {
|
||||
// update.logs.extend(clone_logs);
|
||||
// true
|
||||
// }
|
||||
// Err(e) => {
|
||||
// update
|
||||
// .logs
|
||||
// .push(Log::error("clone repo", format!("{e:#?}")));
|
||||
// false
|
||||
// }
|
||||
// };
|
||||
|
||||
// update.status = UpdateStatus::Complete;
|
||||
// update.end_ts = Some(monitor_timestamp());
|
||||
|
||||
// self.update_update(update.clone()).await?;
|
||||
|
||||
// Ok(update)
|
||||
// }
|
||||
}
|
||||
@@ -1,844 +0,0 @@
|
||||
use anyhow::{anyhow, Context};
|
||||
use diff::Diff;
|
||||
use helpers::{all_logs_success, to_monitor_name};
|
||||
use mungos::doc;
|
||||
use types::{
|
||||
monitor_timestamp,
|
||||
traits::{Busy, Permissioned},
|
||||
Deployment, DeploymentWithContainerState, DockerContainerState, Log, Operation,
|
||||
PermissionLevel, ServerStatus, ServerWithStatus, Update, UpdateStatus, UpdateTarget,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
auth::RequestUser,
|
||||
helpers::{any_option_diff_is_some, empty_or_only_spaces, get_image_name, option_diff_is_some},
|
||||
state::State,
|
||||
};
|
||||
|
||||
impl State {
|
||||
pub async fn get_deployment_check_permissions(
|
||||
&self,
|
||||
deployment_id: &str,
|
||||
user: &RequestUser,
|
||||
permission_level: PermissionLevel,
|
||||
) -> anyhow::Result<Deployment> {
|
||||
let deployment = self.db.get_deployment(deployment_id).await?;
|
||||
let permissions = deployment.get_user_permissions(&user.id);
|
||||
if user.is_admin || permissions >= permission_level {
|
||||
Ok(deployment)
|
||||
} else {
|
||||
Err(anyhow!(
|
||||
"user does not have required permissions on this deployment"
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn deployment_busy(&self, id: &str) -> bool {
|
||||
match self.deployment_action_states.lock().await.get(id) {
|
||||
Some(a) => a.busy(),
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_deployment(
|
||||
&self,
|
||||
name: &str,
|
||||
server_id: String,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Deployment> {
|
||||
self.get_server_check_permissions(&server_id, user, PermissionLevel::Update)
|
||||
.await?;
|
||||
let start_ts = monitor_timestamp();
|
||||
let deployment = Deployment {
|
||||
name: to_monitor_name(name),
|
||||
server_id,
|
||||
permissions: [(user.id.clone(), PermissionLevel::Update)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
created_at: start_ts.clone(),
|
||||
updated_at: start_ts.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
let deployment_id = self
|
||||
.db
|
||||
.deployments
|
||||
.create_one(deployment)
|
||||
.await
|
||||
.context("failed to add deployment to db")?;
|
||||
let deployment = self.db.get_deployment(&deployment_id).await?;
|
||||
let update = Update {
|
||||
target: UpdateTarget::Deployment(deployment_id),
|
||||
operation: Operation::CreateDeployment,
|
||||
start_ts,
|
||||
end_ts: Some(monitor_timestamp()),
|
||||
operator: user.id.clone(),
|
||||
success: true,
|
||||
..Default::default()
|
||||
};
|
||||
self.add_update(update).await?;
|
||||
Ok(deployment)
|
||||
}
|
||||
|
||||
pub async fn create_full_deployment(
|
||||
&self,
|
||||
mut deployment: Deployment,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Deployment> {
|
||||
deployment.id = self
|
||||
.create_deployment(&deployment.name, deployment.server_id.clone(), user)
|
||||
.await?
|
||||
.id;
|
||||
let deployment = self.update_deployment(deployment, user).await?;
|
||||
Ok(deployment)
|
||||
}
|
||||
|
||||
pub async fn copy_deployment(
|
||||
&self,
|
||||
target_id: &str,
|
||||
new_name: String,
|
||||
new_server_id: String,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Deployment> {
|
||||
let mut deployment = self
|
||||
.get_deployment_check_permissions(target_id, user, PermissionLevel::Update)
|
||||
.await?;
|
||||
deployment.name = new_name;
|
||||
deployment.server_id = new_server_id;
|
||||
let deployment = self.create_full_deployment(deployment, user).await?;
|
||||
Ok(deployment)
|
||||
}
|
||||
|
||||
pub async fn delete_deployment(
|
||||
&self,
|
||||
deployment_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Deployment> {
|
||||
if self.deployment_busy(deployment_id).await {
|
||||
return Err(anyhow!("deployment busy"));
|
||||
}
|
||||
let deployment = self
|
||||
.get_deployment_check_permissions(deployment_id, user, PermissionLevel::Update)
|
||||
.await?;
|
||||
let start_ts = monitor_timestamp();
|
||||
let server = self.db.get_server(&deployment.server_id).await?;
|
||||
let log = match self
|
||||
.periphery
|
||||
.container_remove(&server, &deployment.name)
|
||||
.await
|
||||
{
|
||||
Ok(log) => log,
|
||||
Err(e) => Log::error("destroy container", format!("{e:#?}")),
|
||||
};
|
||||
self.db
|
||||
.deployments
|
||||
.delete_one(deployment_id)
|
||||
.await
|
||||
.context(format!(
|
||||
"failed at deleting deployment at {deployment_id} from mongo"
|
||||
))?;
|
||||
let update = Update {
|
||||
target: UpdateTarget::Deployment(deployment_id.to_string()),
|
||||
operation: Operation::DeleteDeployment,
|
||||
start_ts,
|
||||
end_ts: Some(monitor_timestamp()),
|
||||
operator: user.id.clone(),
|
||||
logs: vec![
|
||||
log,
|
||||
Log::simple(
|
||||
"delete deployment",
|
||||
format!(
|
||||
"deleted deployment {} on server {}",
|
||||
deployment.name, server.name
|
||||
),
|
||||
),
|
||||
],
|
||||
success: true,
|
||||
..Default::default()
|
||||
};
|
||||
self.add_update(update).await?;
|
||||
Ok(deployment)
|
||||
}
|
||||
|
||||
pub async fn update_deployment(
|
||||
&self,
|
||||
new_deployment: Deployment,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Deployment> {
|
||||
if self.deployment_busy(&new_deployment.id).await {
|
||||
return Err(anyhow!("deployment busy"));
|
||||
}
|
||||
let id = new_deployment.id.clone();
|
||||
{
|
||||
let mut lock = self.deployment_action_states.lock().await;
|
||||
let entry = lock.entry(id.clone()).or_default();
|
||||
entry.updating = true;
|
||||
}
|
||||
let res = self.update_deployment_inner(new_deployment, user).await;
|
||||
{
|
||||
let mut lock = self.deployment_action_states.lock().await;
|
||||
let entry = lock.entry(id).or_default();
|
||||
entry.updating = false;
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
async fn update_deployment_inner(
|
||||
&self,
|
||||
mut new_deployment: Deployment,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Deployment> {
|
||||
let current_deployment = self
|
||||
.get_deployment_check_permissions(&new_deployment.id, user, PermissionLevel::Update)
|
||||
.await?;
|
||||
let start_ts = monitor_timestamp();
|
||||
|
||||
// none of these should be changed through this method
|
||||
new_deployment.name = current_deployment.name.clone();
|
||||
new_deployment.permissions = current_deployment.permissions.clone();
|
||||
new_deployment.server_id = current_deployment.server_id.clone();
|
||||
new_deployment.created_at = current_deployment.created_at.clone();
|
||||
new_deployment.updated_at = start_ts.clone();
|
||||
|
||||
// filter out any volumes, ports, env vars, extra args which are or contain empty strings
|
||||
// these could only happen by accident
|
||||
new_deployment.docker_run_args.volumes = new_deployment
|
||||
.docker_run_args
|
||||
.volumes
|
||||
.into_iter()
|
||||
.filter(|v| !empty_or_only_spaces(&v.local) && !empty_or_only_spaces(&v.container))
|
||||
.collect();
|
||||
new_deployment.docker_run_args.ports = new_deployment
|
||||
.docker_run_args
|
||||
.ports
|
||||
.into_iter()
|
||||
.filter(|p| !empty_or_only_spaces(&p.local) && !empty_or_only_spaces(&p.container))
|
||||
.collect();
|
||||
new_deployment.docker_run_args.environment = new_deployment
|
||||
.docker_run_args
|
||||
.environment
|
||||
.into_iter()
|
||||
.filter(|e| !empty_or_only_spaces(&e.variable) && !empty_or_only_spaces(&e.value))
|
||||
.collect();
|
||||
new_deployment.docker_run_args.extra_args = new_deployment
|
||||
.docker_run_args
|
||||
.extra_args
|
||||
.into_iter()
|
||||
.filter(|a| a.len() != 0)
|
||||
.collect();
|
||||
|
||||
self.db
|
||||
.deployments
|
||||
.update_one(
|
||||
&new_deployment.id,
|
||||
mungos::Update::Regular(new_deployment.clone()),
|
||||
)
|
||||
.await
|
||||
.context("failed at update one deployment")?;
|
||||
|
||||
let diff = current_deployment.diff(&new_deployment);
|
||||
|
||||
let mut update = Update {
|
||||
operation: Operation::UpdateDeployment,
|
||||
target: UpdateTarget::Deployment(new_deployment.id.clone()),
|
||||
start_ts,
|
||||
status: UpdateStatus::InProgress,
|
||||
logs: vec![Log::simple(
|
||||
"deployment update",
|
||||
serde_json::to_string_pretty(&diff).unwrap(),
|
||||
)],
|
||||
operator: user.id.clone(),
|
||||
success: true,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
update.id = self.add_update(update.clone()).await?;
|
||||
|
||||
if any_option_diff_is_some(&[&diff.repo, &diff.branch, &diff.github_account])
|
||||
|| option_diff_is_some(&diff.on_clone)
|
||||
{
|
||||
let server = self.db.get_server(¤t_deployment.server_id).await?;
|
||||
match self.periphery.clone_repo(&server, &new_deployment).await {
|
||||
Ok(clone_logs) => {
|
||||
update.logs.extend(clone_logs);
|
||||
}
|
||||
Err(e) => update
|
||||
.logs
|
||||
.push(Log::error("cloning repo", format!("{e:#?}"))),
|
||||
}
|
||||
}
|
||||
|
||||
update.end_ts = Some(monitor_timestamp());
|
||||
update.success = all_logs_success(&update.logs);
|
||||
update.status = UpdateStatus::Complete;
|
||||
|
||||
self.update_update(update).await?;
|
||||
|
||||
Ok(new_deployment)
|
||||
}
|
||||
|
||||
pub async fn rename_deployment(
|
||||
&self,
|
||||
deployment_id: &str,
|
||||
new_name: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
if self.deployment_busy(&deployment_id).await {
|
||||
return Err(anyhow!("deployment busy"));
|
||||
}
|
||||
{
|
||||
let mut lock = self.deployment_action_states.lock().await;
|
||||
let entry = lock.entry(deployment_id.to_string()).or_default();
|
||||
entry.renaming = true;
|
||||
}
|
||||
let res = self
|
||||
.rename_deployment_inner(deployment_id, new_name, user)
|
||||
.await;
|
||||
{
|
||||
let mut lock = self.deployment_action_states.lock().await;
|
||||
let entry = lock.entry(deployment_id.to_string()).or_default();
|
||||
entry.renaming = false;
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
async fn rename_deployment_inner(
|
||||
&self,
|
||||
deployment_id: &str,
|
||||
new_name: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
let start_ts = monitor_timestamp();
|
||||
let deployment = self
|
||||
.get_deployment_check_permissions(deployment_id, user, PermissionLevel::Update)
|
||||
.await?;
|
||||
let mut update = Update {
|
||||
target: UpdateTarget::Deployment(deployment_id.to_string()),
|
||||
operation: Operation::RenameDeployment,
|
||||
start_ts,
|
||||
status: UpdateStatus::InProgress,
|
||||
operator: user.id.to_string(),
|
||||
success: true,
|
||||
..Default::default()
|
||||
};
|
||||
update.id = self.add_update(update.clone()).await?;
|
||||
let server_with_status = self.get_server(&deployment.server_id, user).await;
|
||||
if server_with_status.is_err() {
|
||||
update.logs.push(Log::error(
|
||||
"get server",
|
||||
format!(
|
||||
"failed to get server info: {:?}",
|
||||
server_with_status.as_ref().err().unwrap()
|
||||
),
|
||||
));
|
||||
update.status = UpdateStatus::Complete;
|
||||
update.end_ts = monitor_timestamp().into();
|
||||
update.success = false;
|
||||
self.update_update(update).await?;
|
||||
return Err(server_with_status.err().unwrap());
|
||||
}
|
||||
let ServerWithStatus { server, status } = server_with_status.unwrap();
|
||||
if status != ServerStatus::Ok {
|
||||
update.logs.push(Log::error(
|
||||
"check server status",
|
||||
String::from("cannot rename deployment when periphery is disabled or unreachable"),
|
||||
));
|
||||
update.status = UpdateStatus::Complete;
|
||||
update.end_ts = monitor_timestamp().into();
|
||||
update.success = false;
|
||||
self.update_update(update).await?;
|
||||
return Err(anyhow!(
|
||||
"cannot rename deployment when periphery is disabled or unreachable"
|
||||
));
|
||||
}
|
||||
let deployment_state = self
|
||||
.get_deployment_with_container_state(user, deployment_id)
|
||||
.await;
|
||||
if deployment_state.is_err() {
|
||||
update.logs.push(Log::error(
|
||||
"check deployment status",
|
||||
format!(
|
||||
"could not get current state of deployment: {:?}",
|
||||
deployment_state.as_ref().err().unwrap()
|
||||
),
|
||||
));
|
||||
update.status = UpdateStatus::Complete;
|
||||
update.end_ts = monitor_timestamp().into();
|
||||
update.success = false;
|
||||
self.update_update(update).await?;
|
||||
return Err(deployment_state.err().unwrap());
|
||||
}
|
||||
let DeploymentWithContainerState { state, .. } = deployment_state.unwrap();
|
||||
if state != DockerContainerState::NotDeployed {
|
||||
let log = self
|
||||
.periphery
|
||||
.container_rename(&server, &deployment.name, new_name)
|
||||
.await;
|
||||
if log.is_err() {
|
||||
update.logs.push(Log::error(
|
||||
"rename container",
|
||||
format!("{:?}", log.as_ref().err().unwrap()),
|
||||
));
|
||||
update.status = UpdateStatus::Complete;
|
||||
update.end_ts = monitor_timestamp().into();
|
||||
update.success = false;
|
||||
self.update_update(update).await?;
|
||||
return Err(log.err().unwrap());
|
||||
}
|
||||
let log = log.unwrap();
|
||||
if !log.success {
|
||||
update.logs.push(log);
|
||||
update.status = UpdateStatus::Complete;
|
||||
update.end_ts = monitor_timestamp().into();
|
||||
update.success = false;
|
||||
self.update_update(update).await?;
|
||||
return Err(anyhow!("rename container on periphery not successful"));
|
||||
}
|
||||
update.logs.push(log);
|
||||
}
|
||||
let res = self
|
||||
.db
|
||||
.deployments
|
||||
.update_one(
|
||||
deployment_id,
|
||||
mungos::Update::<()>::Set(
|
||||
doc! { "name": to_monitor_name(new_name), "updated_at": monitor_timestamp() },
|
||||
),
|
||||
)
|
||||
.await
|
||||
.context("failed to update deployment name on mongo");
|
||||
|
||||
if let Err(e) = res {
|
||||
update
|
||||
.logs
|
||||
.push(Log::error("mongo update", format!("{e:?}")));
|
||||
} else {
|
||||
update.logs.push(Log::simple(
|
||||
"mongo update",
|
||||
String::from("updated name on mongo"),
|
||||
))
|
||||
}
|
||||
|
||||
update.end_ts = monitor_timestamp().into();
|
||||
update.status = UpdateStatus::Complete;
|
||||
update.success = all_logs_success(&update.logs);
|
||||
|
||||
self.update_update(update.clone()).await?;
|
||||
|
||||
Ok(update)
|
||||
}
|
||||
|
||||
pub async fn reclone_deployment(
|
||||
&self,
|
||||
deployment_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
if self.deployment_busy(deployment_id).await {
|
||||
return Err(anyhow!("deployment busy"));
|
||||
}
|
||||
{
|
||||
let mut lock = self.deployment_action_states.lock().await;
|
||||
let entry = lock.entry(deployment_id.to_string()).or_default();
|
||||
entry.recloning = true;
|
||||
}
|
||||
let res = self.reclone_deployment_inner(deployment_id, user).await;
|
||||
{
|
||||
let mut lock = self.deployment_action_states.lock().await;
|
||||
let entry = lock.entry(deployment_id.to_string()).or_default();
|
||||
entry.recloning = false;
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
async fn reclone_deployment_inner(
|
||||
&self,
|
||||
deployment_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
let deployment = self
|
||||
.get_deployment_check_permissions(deployment_id, user, PermissionLevel::Execute)
|
||||
.await?;
|
||||
let server = self.db.get_server(&deployment.server_id).await?;
|
||||
let mut update = Update {
|
||||
target: UpdateTarget::Deployment(deployment_id.to_string()),
|
||||
operation: Operation::RecloneDeployment,
|
||||
start_ts: monitor_timestamp(),
|
||||
status: UpdateStatus::InProgress,
|
||||
operator: user.id.clone(),
|
||||
success: true,
|
||||
..Default::default()
|
||||
};
|
||||
update.id = self.add_update(update.clone()).await?;
|
||||
|
||||
update.success = match self.periphery.clone_repo(&server, &deployment).await {
|
||||
Ok(clone_logs) => {
|
||||
update.logs.extend(clone_logs);
|
||||
true
|
||||
}
|
||||
Err(e) => {
|
||||
update
|
||||
.logs
|
||||
.push(Log::error("clone repo", format!("{e:#?}")));
|
||||
false
|
||||
}
|
||||
};
|
||||
|
||||
update.status = UpdateStatus::Complete;
|
||||
update.end_ts = Some(monitor_timestamp());
|
||||
|
||||
self.update_update(update.clone()).await?;
|
||||
|
||||
Ok(update)
|
||||
}
|
||||
|
||||
pub async fn deploy_container(
|
||||
&self,
|
||||
deployment_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
if self.deployment_busy(deployment_id).await {
|
||||
return Err(anyhow!("deployment busy"));
|
||||
}
|
||||
{
|
||||
let mut lock = self.deployment_action_states.lock().await;
|
||||
let entry = lock.entry(deployment_id.to_string()).or_default();
|
||||
entry.deploying = true;
|
||||
}
|
||||
let res = self.deploy_container_inner(deployment_id, user).await;
|
||||
{
|
||||
let mut lock = self.deployment_action_states.lock().await;
|
||||
let entry = lock.entry(deployment_id.to_string()).or_default();
|
||||
entry.deploying = false;
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
async fn deploy_container_inner(
|
||||
&self,
|
||||
deployment_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
let start_ts = monitor_timestamp();
|
||||
let mut deployment = self
|
||||
.get_deployment_check_permissions(deployment_id, user, PermissionLevel::Execute)
|
||||
.await?;
|
||||
let version = if let Some(build_id) = &deployment.build_id {
|
||||
let build = self.db.get_build(build_id).await?;
|
||||
let image = get_image_name(&build);
|
||||
if deployment.docker_run_args.docker_account.is_none() {
|
||||
if let Some(docker_account) = &build.docker_account {
|
||||
deployment.docker_run_args.docker_account = Some(docker_account.to_string())
|
||||
};
|
||||
}
|
||||
let version = if let Some(version) = &deployment.build_version {
|
||||
version.clone()
|
||||
} else {
|
||||
build.version.clone()
|
||||
};
|
||||
deployment.docker_run_args.image = format!("{image}:{}", version.to_string());
|
||||
Some(version)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let server = self.db.get_server(&deployment.server_id).await?;
|
||||
let mut update = Update {
|
||||
target: UpdateTarget::Deployment(deployment_id.to_string()),
|
||||
operation: Operation::DeployContainer,
|
||||
start_ts,
|
||||
status: UpdateStatus::InProgress,
|
||||
operator: user.id.clone(),
|
||||
success: true,
|
||||
version,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
update.id = self.add_update(update.clone()).await?;
|
||||
|
||||
let deploy_log = match self.periphery.deploy(&server, &deployment).await {
|
||||
Ok(log) => log,
|
||||
Err(e) => Log::error("deploy container", format!("{e:#?}")),
|
||||
};
|
||||
|
||||
update.success = deploy_log.success;
|
||||
update.logs.push(deploy_log);
|
||||
update.status = UpdateStatus::Complete;
|
||||
update.end_ts = Some(monitor_timestamp());
|
||||
|
||||
self.update_update(update.clone()).await?;
|
||||
|
||||
Ok(update)
|
||||
}
|
||||
|
||||
pub async fn start_container(
|
||||
&self,
|
||||
deployment_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
if self.deployment_busy(deployment_id).await {
|
||||
return Err(anyhow!("deployment busy"));
|
||||
}
|
||||
{
|
||||
let mut lock = self.deployment_action_states.lock().await;
|
||||
let entry = lock.entry(deployment_id.to_string()).or_default();
|
||||
entry.starting = true;
|
||||
}
|
||||
let res = self.start_container_inner(deployment_id, user).await;
|
||||
{
|
||||
let mut lock = self.deployment_action_states.lock().await;
|
||||
let entry = lock.entry(deployment_id.to_string()).or_default();
|
||||
entry.starting = false;
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
async fn start_container_inner(
|
||||
&self,
|
||||
deployment_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
let start_ts = monitor_timestamp();
|
||||
let deployment = self
|
||||
.get_deployment_check_permissions(deployment_id, user, PermissionLevel::Execute)
|
||||
.await?;
|
||||
let server = self.db.get_server(&deployment.server_id).await?;
|
||||
let mut update = Update {
|
||||
target: UpdateTarget::Deployment(deployment_id.to_string()),
|
||||
operation: Operation::StartContainer,
|
||||
start_ts,
|
||||
status: UpdateStatus::InProgress,
|
||||
success: true,
|
||||
operator: user.id.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
update.id = self.add_update(update.clone()).await?;
|
||||
|
||||
let log = self
|
||||
.periphery
|
||||
.container_start(&server, &deployment.name)
|
||||
.await;
|
||||
|
||||
update.success = match log {
|
||||
Ok(log) => {
|
||||
let success = log.success;
|
||||
update.logs.push(log);
|
||||
success
|
||||
}
|
||||
Err(e) => {
|
||||
update
|
||||
.logs
|
||||
.push(Log::error("start container", format!("{e:#?}")));
|
||||
false
|
||||
}
|
||||
};
|
||||
|
||||
update.end_ts = Some(monitor_timestamp());
|
||||
update.status = UpdateStatus::Complete;
|
||||
|
||||
self.update_update(update.clone()).await?;
|
||||
|
||||
Ok(update)
|
||||
}
|
||||
|
||||
pub async fn stop_container(
|
||||
&self,
|
||||
deployment_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
if self.deployment_busy(deployment_id).await {
|
||||
return Err(anyhow!("deployment busy"));
|
||||
}
|
||||
{
|
||||
let mut lock = self.deployment_action_states.lock().await;
|
||||
let entry = lock.entry(deployment_id.to_string()).or_default();
|
||||
entry.stopping = true;
|
||||
}
|
||||
let res = self.stop_container_inner(deployment_id, user).await;
|
||||
{
|
||||
let mut lock = self.deployment_action_states.lock().await;
|
||||
let entry = lock.entry(deployment_id.to_string()).or_default();
|
||||
entry.stopping = false;
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
async fn stop_container_inner(
|
||||
&self,
|
||||
deployment_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
let start_ts = monitor_timestamp();
|
||||
let deployment = self
|
||||
.get_deployment_check_permissions(deployment_id, user, PermissionLevel::Execute)
|
||||
.await?;
|
||||
let server = self.db.get_server(&deployment.server_id).await?;
|
||||
let mut update = Update {
|
||||
target: UpdateTarget::Deployment(deployment_id.to_string()),
|
||||
operation: Operation::StopContainer,
|
||||
start_ts,
|
||||
status: UpdateStatus::InProgress,
|
||||
success: true,
|
||||
operator: user.id.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
update.id = self.add_update(update.clone()).await?;
|
||||
|
||||
let log = self
|
||||
.periphery
|
||||
.container_stop(&server, &deployment.name)
|
||||
.await;
|
||||
|
||||
update.success = match log {
|
||||
Ok(log) => {
|
||||
let success = log.success;
|
||||
update.logs.push(log);
|
||||
success
|
||||
}
|
||||
Err(e) => {
|
||||
update
|
||||
.logs
|
||||
.push(Log::error("stop container", format!("{e:#?}")));
|
||||
false
|
||||
}
|
||||
};
|
||||
|
||||
update.end_ts = Some(monitor_timestamp());
|
||||
update.status = UpdateStatus::Complete;
|
||||
|
||||
self.update_update(update.clone()).await?;
|
||||
|
||||
Ok(update)
|
||||
}
|
||||
|
||||
pub async fn remove_container(
|
||||
&self,
|
||||
deployment_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
if self.deployment_busy(deployment_id).await {
|
||||
return Err(anyhow!("deployment busy"));
|
||||
}
|
||||
{
|
||||
let mut lock = self.deployment_action_states.lock().await;
|
||||
let entry = lock.entry(deployment_id.to_string()).or_default();
|
||||
entry.removing = true;
|
||||
}
|
||||
let res = self.remove_container_inner(deployment_id, user).await;
|
||||
{
|
||||
let mut lock = self.deployment_action_states.lock().await;
|
||||
let entry = lock.entry(deployment_id.to_string()).or_default();
|
||||
entry.removing = false;
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
async fn remove_container_inner(
|
||||
&self,
|
||||
deployment_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
let start_ts = monitor_timestamp();
|
||||
let deployment = self
|
||||
.get_deployment_check_permissions(deployment_id, user, PermissionLevel::Execute)
|
||||
.await?;
|
||||
let server = self.db.get_server(&deployment.server_id).await?;
|
||||
let mut update = Update {
|
||||
target: UpdateTarget::Deployment(deployment_id.to_string()),
|
||||
operation: Operation::RemoveContainer,
|
||||
start_ts,
|
||||
status: UpdateStatus::InProgress,
|
||||
success: true,
|
||||
operator: user.id.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
update.id = self.add_update(update.clone()).await?;
|
||||
|
||||
let log = self
|
||||
.periphery
|
||||
.container_remove(&server, &deployment.name)
|
||||
.await;
|
||||
|
||||
update.success = match log {
|
||||
Ok(log) => {
|
||||
let success = log.success;
|
||||
update.logs.push(log);
|
||||
success
|
||||
}
|
||||
Err(e) => {
|
||||
update
|
||||
.logs
|
||||
.push(Log::error("remove container", format!("{e:#?}")));
|
||||
false
|
||||
}
|
||||
};
|
||||
|
||||
update.end_ts = Some(monitor_timestamp());
|
||||
update.status = UpdateStatus::Complete;
|
||||
|
||||
self.update_update(update.clone()).await?;
|
||||
|
||||
Ok(update)
|
||||
}
|
||||
|
||||
pub async fn pull_deployment_repo(
|
||||
&self,
|
||||
deployment_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
if self.deployment_busy(deployment_id).await {
|
||||
return Err(anyhow!("deployment busy"));
|
||||
}
|
||||
{
|
||||
let mut lock = self.deployment_action_states.lock().await;
|
||||
let entry = lock.entry(deployment_id.to_string()).or_default();
|
||||
entry.pulling = true;
|
||||
}
|
||||
let res = self.pull_deployment_repo_inner(deployment_id, user).await;
|
||||
{
|
||||
let mut lock = self.deployment_action_states.lock().await;
|
||||
let entry = lock.entry(deployment_id.to_string()).or_default();
|
||||
entry.pulling = false;
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
async fn pull_deployment_repo_inner(
|
||||
&self,
|
||||
deployment_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
let start_ts = monitor_timestamp();
|
||||
let deployment = self
|
||||
.get_deployment_check_permissions(deployment_id, user, PermissionLevel::Execute)
|
||||
.await?;
|
||||
let server = self.db.get_server(&deployment.server_id).await?;
|
||||
let mut update = Update {
|
||||
target: UpdateTarget::Deployment(deployment_id.to_string()),
|
||||
operation: Operation::PullDeployment,
|
||||
start_ts,
|
||||
status: UpdateStatus::InProgress,
|
||||
operator: user.id.clone(),
|
||||
success: true,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
update.id = self.add_update(update.clone()).await?;
|
||||
|
||||
let logs = self
|
||||
.periphery
|
||||
.pull_repo(
|
||||
&server,
|
||||
&deployment.name,
|
||||
&deployment.branch,
|
||||
&deployment.on_pull,
|
||||
)
|
||||
.await?;
|
||||
|
||||
update.success = all_logs_success(&logs);
|
||||
update.logs.extend(logs);
|
||||
update.end_ts = Some(monitor_timestamp());
|
||||
update.status = UpdateStatus::Complete;
|
||||
|
||||
self.update_update(update.clone()).await?;
|
||||
|
||||
Ok(update)
|
||||
}
|
||||
}
|
||||
@@ -1,141 +0,0 @@
|
||||
use anyhow::{anyhow, Context};
|
||||
use diff::Diff;
|
||||
use helpers::to_monitor_name;
|
||||
use types::{
|
||||
monitor_timestamp, traits::Permissioned, Group, Log, Operation, PermissionLevel, Update,
|
||||
UpdateStatus, UpdateTarget,
|
||||
};
|
||||
|
||||
use crate::{auth::RequestUser, state::State};
|
||||
|
||||
impl State {
|
||||
pub async fn get_group_check_permissions(
|
||||
&self,
|
||||
group_id: &str,
|
||||
user: &RequestUser,
|
||||
permission_level: PermissionLevel,
|
||||
) -> anyhow::Result<Group> {
|
||||
let group = self.db.get_group(group_id).await?;
|
||||
let permissions = group.get_user_permissions(&user.id);
|
||||
if user.is_admin || permissions >= permission_level {
|
||||
Ok(group)
|
||||
} else {
|
||||
Err(anyhow!(
|
||||
"user does not have required permissions on this group"
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_group(&self, name: &str, user: &RequestUser) -> anyhow::Result<Group> {
|
||||
let start_ts = monitor_timestamp();
|
||||
let group = Group {
|
||||
name: to_monitor_name(name),
|
||||
permissions: [(user.id.clone(), PermissionLevel::Update)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
created_at: start_ts.clone(),
|
||||
updated_at: start_ts.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
let group_id = self
|
||||
.db
|
||||
.groups
|
||||
.create_one(group)
|
||||
.await
|
||||
.context("failed to add group to db")?;
|
||||
let group = self.db.get_group(&group_id).await?;
|
||||
let update = Update {
|
||||
target: UpdateTarget::Group(group_id),
|
||||
operation: Operation::CreateGroup,
|
||||
start_ts,
|
||||
end_ts: Some(monitor_timestamp()),
|
||||
operator: user.id.clone(),
|
||||
success: true,
|
||||
..Default::default()
|
||||
};
|
||||
self.add_update(update).await?;
|
||||
Ok(group)
|
||||
}
|
||||
|
||||
pub async fn create_full_group(
|
||||
&self,
|
||||
mut full_group: Group,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Group> {
|
||||
let group = self.create_group(&full_group.name, user).await?;
|
||||
full_group.id = group.id;
|
||||
let group = self.update_group(full_group, user).await?;
|
||||
Ok(group)
|
||||
}
|
||||
|
||||
pub async fn delete_group(&self, id: &str, user: &RequestUser) -> anyhow::Result<Group> {
|
||||
let group = self
|
||||
.get_group_check_permissions(id, user, PermissionLevel::Update)
|
||||
.await?;
|
||||
let start_ts = monitor_timestamp();
|
||||
self.db
|
||||
.groups
|
||||
.delete_one(id)
|
||||
.await
|
||||
.context(format!("failed at deleting group at {id} from mongo"))?;
|
||||
let update = Update {
|
||||
target: UpdateTarget::Group(id.to_string()),
|
||||
operation: Operation::DeleteGroup,
|
||||
start_ts,
|
||||
end_ts: Some(monitor_timestamp()),
|
||||
operator: user.id.clone(),
|
||||
logs: vec![Log::simple(
|
||||
"delete group",
|
||||
format!("deleted group {}", group.name),
|
||||
)],
|
||||
success: true,
|
||||
..Default::default()
|
||||
};
|
||||
self.add_update(update).await?;
|
||||
Ok(group)
|
||||
}
|
||||
|
||||
pub async fn update_group(
|
||||
&self,
|
||||
mut new_group: Group,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Group> {
|
||||
let current_group = self
|
||||
.get_group_check_permissions(&new_group.id, user, PermissionLevel::Update)
|
||||
.await?;
|
||||
let start_ts = monitor_timestamp();
|
||||
|
||||
// none of these should be changed through this method
|
||||
new_group.name = current_group.name.clone();
|
||||
new_group.permissions = current_group.permissions.clone();
|
||||
new_group.created_at = current_group.created_at.clone();
|
||||
new_group.updated_at = start_ts.clone();
|
||||
|
||||
self.db
|
||||
.groups
|
||||
.update_one(&new_group.id, mungos::Update::Regular(new_group.clone()))
|
||||
.await
|
||||
.context("failed at update one group")?;
|
||||
|
||||
let diff = current_group.diff(&new_group);
|
||||
|
||||
let update = Update {
|
||||
operation: Operation::UpdateGroup,
|
||||
target: UpdateTarget::Group(new_group.id.clone()),
|
||||
end_ts: Some(start_ts.clone()),
|
||||
start_ts,
|
||||
status: UpdateStatus::Complete,
|
||||
logs: vec![Log::simple(
|
||||
"group update",
|
||||
serde_json::to_string_pretty(&diff).unwrap(),
|
||||
)],
|
||||
operator: user.id.clone(),
|
||||
success: true,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
self.add_update(update).await?;
|
||||
|
||||
Ok(new_group)
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
use anyhow::Context;
|
||||
use types::Update;
|
||||
|
||||
use crate::state::State;
|
||||
|
||||
mod build;
|
||||
mod deployment;
|
||||
mod group;
|
||||
mod procedure;
|
||||
mod server;
|
||||
|
||||
impl State {
|
||||
pub async fn send_update(&self, update: Update) -> anyhow::Result<()> {
|
||||
self.update.sender.lock().await.send(update)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn add_update(&self, mut update: Update) -> anyhow::Result<String> {
|
||||
update.id = self
|
||||
.db
|
||||
.updates
|
||||
.create_one(update.clone())
|
||||
.await
|
||||
.context("failed to insert update into db")?
|
||||
.to_string();
|
||||
let id = update.id.clone();
|
||||
let _ = self.send_update(update).await;
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub async fn update_update(&self, mut update: Update) -> anyhow::Result<()> {
|
||||
let mut update_id = String::new();
|
||||
std::mem::swap(&mut update.id, &mut update_id);
|
||||
self.db
|
||||
.updates
|
||||
.update_one(&update_id, mungos::Update::Regular(update.clone()))
|
||||
.await
|
||||
.context("failed to update the update on db. the update build process was deleted")?;
|
||||
std::mem::swap(&mut update.id, &mut update_id);
|
||||
let _ = self.send_update(update).await;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,295 +0,0 @@
|
||||
use anyhow::{anyhow, Context};
|
||||
use diff::Diff;
|
||||
use helpers::to_monitor_name;
|
||||
use types::{
|
||||
monitor_timestamp, traits::Permissioned, Log, Operation, PermissionLevel, Procedure,
|
||||
ProcedureOperation::*, ProcedureStage, Update, UpdateStatus, UpdateTarget,
|
||||
};
|
||||
|
||||
use crate::{auth::RequestUser, state::State};
|
||||
|
||||
impl State {
|
||||
pub async fn get_procedure_check_permissions(
|
||||
&self,
|
||||
procedure_id: &str,
|
||||
user: &RequestUser,
|
||||
permission_level: PermissionLevel,
|
||||
) -> anyhow::Result<Procedure> {
|
||||
let procedure = self.db.get_procedure(procedure_id).await?;
|
||||
let permissions = procedure.get_user_permissions(&user.id);
|
||||
if user.is_admin || permissions >= permission_level {
|
||||
Ok(procedure)
|
||||
} else {
|
||||
Err(anyhow!(
|
||||
"user does not have required permissions on this procedure"
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_procedure(
|
||||
&self,
|
||||
name: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Procedure> {
|
||||
let start_ts = monitor_timestamp();
|
||||
let procedure = Procedure {
|
||||
name: to_monitor_name(name),
|
||||
permissions: [(user.id.clone(), PermissionLevel::Update)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
created_at: start_ts.clone(),
|
||||
updated_at: start_ts.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
let procedure_id = self
|
||||
.db
|
||||
.procedures
|
||||
.create_one(procedure)
|
||||
.await
|
||||
.context("failed to add procedure to db")?;
|
||||
let procedure = self.db.get_procedure(&procedure_id).await?;
|
||||
let update = Update {
|
||||
target: UpdateTarget::Procedure(procedure_id),
|
||||
operation: Operation::CreateProcedure,
|
||||
start_ts,
|
||||
end_ts: Some(monitor_timestamp()),
|
||||
operator: user.id.clone(),
|
||||
success: true,
|
||||
..Default::default()
|
||||
};
|
||||
self.add_update(update).await?;
|
||||
Ok(procedure)
|
||||
}
|
||||
|
||||
pub async fn create_full_procedure(
|
||||
&self,
|
||||
mut full_procedure: Procedure,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Procedure> {
|
||||
let procedure = self.create_procedure(&full_procedure.name, user).await?;
|
||||
full_procedure.id = procedure.id;
|
||||
let procedure = self.update_procedure(full_procedure, user).await?;
|
||||
Ok(procedure)
|
||||
}
|
||||
|
||||
pub async fn delete_procedure(
|
||||
&self,
|
||||
id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Procedure> {
|
||||
let procedure = self
|
||||
.get_procedure_check_permissions(id, user, PermissionLevel::Update)
|
||||
.await?;
|
||||
let start_ts = monitor_timestamp();
|
||||
self.db
|
||||
.procedures
|
||||
.delete_one(id)
|
||||
.await
|
||||
.context(format!("failed at deleting procedure at {id} from mongo"))?;
|
||||
let update = Update {
|
||||
target: UpdateTarget::Procedure(id.to_string()),
|
||||
operation: Operation::DeleteProcedure,
|
||||
start_ts,
|
||||
end_ts: Some(monitor_timestamp()),
|
||||
operator: user.id.clone(),
|
||||
logs: vec![Log::simple(
|
||||
"delete procedure",
|
||||
format!("deleted procedure {}", procedure.name),
|
||||
)],
|
||||
success: true,
|
||||
..Default::default()
|
||||
};
|
||||
self.add_update(update).await?;
|
||||
Ok(procedure)
|
||||
}
|
||||
|
||||
pub async fn update_procedure(
|
||||
&self,
|
||||
mut new_procedure: Procedure,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Procedure> {
|
||||
let current_procedure = self
|
||||
.get_procedure_check_permissions(&new_procedure.id, user, PermissionLevel::Update)
|
||||
.await?;
|
||||
let start_ts = monitor_timestamp();
|
||||
|
||||
// none of these should be changed through this method
|
||||
new_procedure.name = current_procedure.name.clone();
|
||||
new_procedure.permissions = current_procedure.permissions.clone();
|
||||
new_procedure.created_at = current_procedure.created_at.clone();
|
||||
new_procedure.updated_at = start_ts.clone();
|
||||
|
||||
for ProcedureStage {
|
||||
operation,
|
||||
target_id,
|
||||
} in &new_procedure.stages
|
||||
{
|
||||
match operation {
|
||||
BuildBuild => {
|
||||
self.get_build_check_permissions(&target_id, user, PermissionLevel::Execute)
|
||||
.await?;
|
||||
}
|
||||
DeployContainer | StartContainer | StopContainer | RemoveContainer
|
||||
| PullDeployment | RecloneDeployment => {
|
||||
self.get_deployment_check_permissions(
|
||||
target_id,
|
||||
user,
|
||||
PermissionLevel::Execute,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
PruneImagesServer | PruneContainersServer | PruneNetworksServer => {
|
||||
self.get_server_check_permissions(target_id, user, PermissionLevel::Execute)
|
||||
.await?;
|
||||
}
|
||||
RunProcedure => {
|
||||
self.get_procedure_check_permissions(target_id, user, PermissionLevel::Execute)
|
||||
.await?;
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
|
||||
self.db
|
||||
.procedures
|
||||
.update_one(
|
||||
&new_procedure.id,
|
||||
mungos::Update::Regular(new_procedure.clone()),
|
||||
)
|
||||
.await
|
||||
.context("failed at update one procedure")?;
|
||||
|
||||
let diff = current_procedure.diff(&new_procedure);
|
||||
|
||||
let update = Update {
|
||||
operation: Operation::UpdateProcedure,
|
||||
target: UpdateTarget::Procedure(new_procedure.id.clone()),
|
||||
end_ts: Some(start_ts.clone()),
|
||||
start_ts,
|
||||
status: UpdateStatus::Complete,
|
||||
logs: vec![Log::simple(
|
||||
"procedure update",
|
||||
serde_json::to_string_pretty(&diff).unwrap(),
|
||||
)],
|
||||
operator: user.id.clone(),
|
||||
success: true,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
self.add_update(update).await?;
|
||||
|
||||
Ok(new_procedure)
|
||||
}
|
||||
|
||||
pub async fn run_procedure(
|
||||
&self,
|
||||
procedure_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Vec<Update>> {
|
||||
let procedure = self
|
||||
.get_procedure_check_permissions(procedure_id, user, PermissionLevel::Execute)
|
||||
.await?;
|
||||
let mut updates = Vec::new();
|
||||
for ProcedureStage {
|
||||
operation,
|
||||
target_id,
|
||||
} in procedure.stages
|
||||
{
|
||||
match operation {
|
||||
None => {}
|
||||
// deployment
|
||||
StartContainer => {
|
||||
let update = self
|
||||
.start_container(&target_id, user)
|
||||
.await
|
||||
.context(format!(
|
||||
"failed at start container for deployment (id: {target_id})"
|
||||
))?;
|
||||
updates.push(update);
|
||||
}
|
||||
StopContainer => {
|
||||
let update = self
|
||||
.stop_container(&target_id, user)
|
||||
.await
|
||||
.context(format!(
|
||||
"failed at stop container for deployment (id: {target_id})"
|
||||
))?;
|
||||
updates.push(update);
|
||||
}
|
||||
RemoveContainer => {
|
||||
let update = self
|
||||
.remove_container(&target_id, user)
|
||||
.await
|
||||
.context(format!(
|
||||
"failed at remove container for deployment (id: {target_id})"
|
||||
))?;
|
||||
updates.push(update);
|
||||
}
|
||||
DeployContainer => {
|
||||
let update = self
|
||||
.deploy_container(&target_id, user)
|
||||
.await
|
||||
.context(format!(
|
||||
"failed at deploy container for deployment (id: {target_id})"
|
||||
))?;
|
||||
updates.push(update);
|
||||
}
|
||||
RecloneDeployment => {
|
||||
let update = self
|
||||
.reclone_deployment(&target_id, user)
|
||||
.await
|
||||
.context(format!("failed at reclone deployment (id: {target_id})"))?;
|
||||
updates.push(update);
|
||||
}
|
||||
PullDeployment => {
|
||||
// implement this one
|
||||
// let update = self.pull
|
||||
}
|
||||
// build
|
||||
BuildBuild => {
|
||||
let update = self
|
||||
.build(&target_id, user)
|
||||
.await
|
||||
.context(format!("failed at build (id: {target_id})"))?;
|
||||
updates.push(update);
|
||||
}
|
||||
// server
|
||||
PruneImagesServer => {
|
||||
let update = self.prune_images(&target_id, user).await.context(format!(
|
||||
"failed at prune images on server (id: {target_id})"
|
||||
))?;
|
||||
updates.push(update);
|
||||
}
|
||||
PruneContainersServer => {
|
||||
let update = self
|
||||
.prune_containers(&target_id, user)
|
||||
.await
|
||||
.context(format!(
|
||||
"failed at prune containers on server (id: {target_id})"
|
||||
))?;
|
||||
updates.push(update);
|
||||
}
|
||||
PruneNetworksServer => {
|
||||
let update = self
|
||||
.prune_networks(&target_id, user)
|
||||
.await
|
||||
.context(format!(
|
||||
"failed at prune networks on servers (id: {target_id})"
|
||||
))?;
|
||||
updates.push(update);
|
||||
}
|
||||
// procedure
|
||||
RunProcedure => {
|
||||
// need to figure out async recursion
|
||||
// need to guard against infinite procedure loops when they are updated
|
||||
// let proc_updates = self
|
||||
// .run_procedure(&target_id, user)
|
||||
// .await
|
||||
// .context(format!("failed to run nested procedure (id: {target_id})"))?;
|
||||
// updates.extend(proc_updates);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(updates)
|
||||
}
|
||||
}
|
||||
@@ -1,393 +0,0 @@
|
||||
use anyhow::{anyhow, Context};
|
||||
use diff::Diff;
|
||||
use futures_util::future::join_all;
|
||||
use mungos::doc;
|
||||
use types::{
|
||||
monitor_timestamp,
|
||||
traits::{Busy, Permissioned},
|
||||
Log, Operation, PermissionLevel, Server, Update, UpdateStatus, UpdateTarget,
|
||||
};
|
||||
|
||||
use crate::{auth::RequestUser, state::State};
|
||||
|
||||
impl State {
|
||||
pub async fn get_server_check_permissions(
|
||||
&self,
|
||||
server_id: &str,
|
||||
user: &RequestUser,
|
||||
permission_level: PermissionLevel,
|
||||
) -> anyhow::Result<Server> {
|
||||
let server = self.db.get_server(server_id).await?;
|
||||
let permissions = server.get_user_permissions(&user.id);
|
||||
if user.is_admin || permissions >= permission_level {
|
||||
Ok(server)
|
||||
} else {
|
||||
Err(anyhow!(
|
||||
"user does not have required permissions on this server"
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn server_busy(&self, id: &str) -> bool {
|
||||
match self.server_action_states.lock().await.get(id) {
|
||||
Some(a) => a.busy(),
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_server(
|
||||
&self,
|
||||
name: &str,
|
||||
address: String,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Server> {
|
||||
if !user.is_admin && !user.create_server_permissions {
|
||||
return Err(anyhow!(
|
||||
"user does not have permissions to add server (not admin)"
|
||||
));
|
||||
}
|
||||
let start_ts = monitor_timestamp();
|
||||
let server = Server {
|
||||
name: name.to_string(),
|
||||
address,
|
||||
permissions: [(user.id.clone(), PermissionLevel::Update)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
created_at: start_ts.clone(),
|
||||
updated_at: start_ts.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
let server_id = self
|
||||
.db
|
||||
.servers
|
||||
.create_one(server)
|
||||
.await
|
||||
.context("failed to add server to db")?;
|
||||
let server = self.db.get_server(&server_id).await?;
|
||||
let update = Update {
|
||||
target: UpdateTarget::Server(server_id),
|
||||
operation: Operation::CreateServer,
|
||||
start_ts,
|
||||
end_ts: Some(monitor_timestamp()),
|
||||
operator: user.id.clone(),
|
||||
success: true,
|
||||
..Default::default()
|
||||
};
|
||||
self.add_update(update).await?;
|
||||
|
||||
Ok(server)
|
||||
}
|
||||
|
||||
pub async fn create_full_server(
|
||||
&self,
|
||||
mut server: Server,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Server> {
|
||||
server.id = self
|
||||
.create_server(&server.name, server.address.clone(), user)
|
||||
.await?
|
||||
.id;
|
||||
let server = self.update_server(server, user).await?;
|
||||
Ok(server)
|
||||
}
|
||||
|
||||
pub async fn delete_server(
|
||||
&self,
|
||||
server_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Server> {
|
||||
if self.server_busy(server_id).await {
|
||||
return Err(anyhow!("server busy"));
|
||||
}
|
||||
let server = self
|
||||
.get_server_check_permissions(server_id, user, PermissionLevel::Update)
|
||||
.await?;
|
||||
let start_ts = monitor_timestamp();
|
||||
let mut update = Update {
|
||||
target: UpdateTarget::Server(server_id.to_string()),
|
||||
operation: Operation::DeleteServer,
|
||||
start_ts,
|
||||
operator: user.id.clone(),
|
||||
success: true,
|
||||
status: UpdateStatus::InProgress,
|
||||
..Default::default()
|
||||
};
|
||||
update.id = self.add_update(update.clone()).await?;
|
||||
|
||||
let res = {
|
||||
let delete_deployments = self
|
||||
.db
|
||||
.deployments
|
||||
.get_some(doc! { "server_id": server_id }, None)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|d| async move { self.delete_deployment(&d.id, user).await });
|
||||
let delete_builds = self
|
||||
.db
|
||||
.builds
|
||||
.get_some(doc! { "server_id": server_id }, None)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|d| async move { self.delete_deployment(&d.id, user).await });
|
||||
let update_groups = self
|
||||
.db
|
||||
.groups
|
||||
.update_many(doc! {}, doc! { "$pull": { "servers": server_id } });
|
||||
let (dep_res, build_res, group_res) = tokio::join!(
|
||||
join_all(delete_deployments),
|
||||
join_all(delete_builds),
|
||||
update_groups
|
||||
);
|
||||
dep_res.into_iter().collect::<anyhow::Result<Vec<_>>>()?;
|
||||
build_res.into_iter().collect::<anyhow::Result<Vec<_>>>()?;
|
||||
group_res?;
|
||||
self.db.servers.delete_one(&server_id).await?;
|
||||
anyhow::Ok(())
|
||||
};
|
||||
|
||||
let log = match res {
|
||||
Ok(_) => Log::simple("delete server", format!("deleted server {}", server.name)),
|
||||
Err(e) => Log::error("delete server", format!("failed to delete server\n{e:#?}")),
|
||||
};
|
||||
|
||||
update.end_ts = Some(monitor_timestamp());
|
||||
update.status = UpdateStatus::Complete;
|
||||
update.success = log.success;
|
||||
update.logs.push(log);
|
||||
|
||||
self.update_update(update).await?;
|
||||
Ok(server)
|
||||
}
|
||||
|
||||
pub async fn update_server(
|
||||
&self,
|
||||
mut new_server: Server,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Server> {
|
||||
if self.server_busy(&new_server.id).await {
|
||||
return Err(anyhow!("server busy"));
|
||||
}
|
||||
let current_server = self
|
||||
.get_server_check_permissions(&new_server.id, user, PermissionLevel::Update)
|
||||
.await?;
|
||||
let start_ts = monitor_timestamp();
|
||||
|
||||
new_server.permissions = current_server.permissions.clone();
|
||||
new_server.created_at = current_server.created_at.clone();
|
||||
new_server.updated_at = start_ts.clone();
|
||||
|
||||
let diff = current_server.diff(&new_server);
|
||||
|
||||
self.db
|
||||
.servers
|
||||
.update_one(&new_server.id, mungos::Update::Regular(new_server.clone()))
|
||||
.await
|
||||
.context("failed at update one server")?;
|
||||
|
||||
let update = Update {
|
||||
operation: Operation::UpdateServer,
|
||||
target: UpdateTarget::Server(new_server.id.clone()),
|
||||
start_ts,
|
||||
end_ts: Some(monitor_timestamp()),
|
||||
status: UpdateStatus::Complete,
|
||||
logs: vec![Log::simple(
|
||||
"server update",
|
||||
serde_json::to_string_pretty(&diff).unwrap(),
|
||||
)],
|
||||
operator: user.id.clone(),
|
||||
success: true,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
self.add_update(update).await?;
|
||||
Ok(new_server)
|
||||
}
|
||||
|
||||
pub async fn prune_networks(
|
||||
&self,
|
||||
server_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
if self.server_busy(server_id).await {
|
||||
return Err(anyhow!("server busy"));
|
||||
}
|
||||
{
|
||||
let mut lock = self.server_action_states.lock().await;
|
||||
let entry = lock.entry(server_id.to_string()).or_default();
|
||||
entry.pruning_networks = true;
|
||||
}
|
||||
let res = self.prune_networks_inner(server_id, user).await;
|
||||
{
|
||||
let mut lock = self.server_action_states.lock().await;
|
||||
let entry = lock.entry(server_id.to_string()).or_default();
|
||||
entry.pruning_networks = false;
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
async fn prune_networks_inner(
|
||||
&self,
|
||||
server_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
let server = self
|
||||
.get_server_check_permissions(server_id, user, PermissionLevel::Execute)
|
||||
.await?;
|
||||
|
||||
let start_ts = monitor_timestamp();
|
||||
let mut update = Update {
|
||||
target: UpdateTarget::Server(server_id.to_owned()),
|
||||
operation: Operation::PruneNetworksServer,
|
||||
start_ts,
|
||||
status: UpdateStatus::InProgress,
|
||||
success: true,
|
||||
operator: user.id.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
update.id = self.add_update(update.clone()).await?;
|
||||
|
||||
let log = match self.periphery.network_prune(&server).await.context(format!(
|
||||
"failed to prune networks on server {}",
|
||||
server.name
|
||||
)) {
|
||||
Ok(log) => log,
|
||||
Err(e) => Log::error("prune networks", format!("{e:#?}")),
|
||||
};
|
||||
|
||||
update.success = log.success;
|
||||
update.status = UpdateStatus::Complete;
|
||||
update.end_ts = Some(monitor_timestamp());
|
||||
update.logs.push(log);
|
||||
|
||||
self.update_update(update.clone()).await?;
|
||||
|
||||
Ok(update)
|
||||
}
|
||||
|
||||
pub async fn prune_images(
|
||||
&self,
|
||||
server_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
if self.server_busy(server_id).await {
|
||||
return Err(anyhow!("server busy"));
|
||||
}
|
||||
{
|
||||
let mut lock = self.server_action_states.lock().await;
|
||||
let entry = lock.entry(server_id.to_string()).or_default();
|
||||
entry.pruning_images = true;
|
||||
}
|
||||
let res = self.prune_images_inner(server_id, user).await;
|
||||
{
|
||||
let mut lock = self.server_action_states.lock().await;
|
||||
let entry = lock.entry(server_id.to_string()).or_default();
|
||||
entry.pruning_images = false;
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
async fn prune_images_inner(
|
||||
&self,
|
||||
server_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
let server = self
|
||||
.get_server_check_permissions(server_id, user, PermissionLevel::Execute)
|
||||
.await?;
|
||||
let start_ts = monitor_timestamp();
|
||||
let mut update = Update {
|
||||
target: UpdateTarget::Server(server_id.to_owned()),
|
||||
operation: Operation::PruneImagesServer,
|
||||
start_ts,
|
||||
status: UpdateStatus::InProgress,
|
||||
success: true,
|
||||
operator: user.id.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
update.id = self.add_update(update.clone()).await?;
|
||||
|
||||
let log = match self
|
||||
.periphery
|
||||
.image_prune(&server)
|
||||
.await
|
||||
.context(format!("failed to prune images on server {}", server.name))
|
||||
{
|
||||
Ok(log) => log,
|
||||
Err(e) => Log::error("prune images", format!("{e:#?}")),
|
||||
};
|
||||
|
||||
update.success = log.success;
|
||||
update.status = UpdateStatus::Complete;
|
||||
update.end_ts = Some(monitor_timestamp());
|
||||
update.logs.push(log);
|
||||
|
||||
self.update_update(update.clone()).await?;
|
||||
|
||||
Ok(update)
|
||||
}
|
||||
|
||||
pub async fn prune_containers(
|
||||
&self,
|
||||
server_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
if self.server_busy(server_id).await {
|
||||
return Err(anyhow!("server busy"));
|
||||
}
|
||||
{
|
||||
let mut lock = self.server_action_states.lock().await;
|
||||
let entry = lock.entry(server_id.to_string()).or_default();
|
||||
entry.pruning_containers = true;
|
||||
}
|
||||
let res = self.prune_containers_inner(server_id, user).await;
|
||||
{
|
||||
let mut lock = self.server_action_states.lock().await;
|
||||
let entry = lock.entry(server_id.to_string()).or_default();
|
||||
entry.pruning_containers = false;
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
async fn prune_containers_inner(
|
||||
&self,
|
||||
server_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Update> {
|
||||
let server = self
|
||||
.get_server_check_permissions(server_id, user, PermissionLevel::Execute)
|
||||
.await?;
|
||||
|
||||
let start_ts = monitor_timestamp();
|
||||
let mut update = Update {
|
||||
target: UpdateTarget::Server(server_id.to_owned()),
|
||||
operation: Operation::PruneContainersServer,
|
||||
start_ts,
|
||||
status: UpdateStatus::InProgress,
|
||||
success: true,
|
||||
operator: user.id.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
update.id = self.add_update(update.clone()).await?;
|
||||
|
||||
let log = match self
|
||||
.periphery
|
||||
.container_prune(&server)
|
||||
.await
|
||||
.context(format!(
|
||||
"failed to prune containers on server {}",
|
||||
server.name
|
||||
)) {
|
||||
Ok(log) => log,
|
||||
Err(e) => Log::error("prune containers", format!("{e:#?}")),
|
||||
};
|
||||
|
||||
update.success = log.success;
|
||||
update.status = UpdateStatus::Complete;
|
||||
update.end_ts = Some(monitor_timestamp());
|
||||
update.logs.push(log);
|
||||
|
||||
self.update_update(update.clone()).await?;
|
||||
|
||||
Ok(update)
|
||||
}
|
||||
}
|
||||
@@ -1,320 +0,0 @@
|
||||
use anyhow::Context;
|
||||
use axum::{
|
||||
extract::{Path, Query},
|
||||
routing::{delete, get, patch, post},
|
||||
Extension, Json, Router,
|
||||
};
|
||||
use helpers::handle_anyhow_error;
|
||||
use mungos::{doc, Deserialize, Document, FindOptions, Serialize};
|
||||
use types::{
|
||||
traits::Permissioned, AwsBuilderConfig, Build, BuildActionState, BuildVersionsReponse,
|
||||
Operation, PermissionLevel, UpdateStatus,
|
||||
};
|
||||
use typeshare::typeshare;
|
||||
|
||||
const NUM_VERSIONS_PER_PAGE: u64 = 10;
|
||||
|
||||
use crate::{
|
||||
auth::{RequestUser, RequestUserExtension},
|
||||
response,
|
||||
state::{State, StateExtension},
|
||||
};
|
||||
|
||||
use super::spawn_request_action;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct BuildId {
|
||||
id: String,
|
||||
}
|
||||
|
||||
#[typeshare]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct CreateBuildBody {
|
||||
name: String,
|
||||
}
|
||||
|
||||
#[typeshare]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct CopyBuildBody {
|
||||
name: String,
|
||||
}
|
||||
|
||||
#[typeshare]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct BuildVersionsQuery {
|
||||
#[serde(default)]
|
||||
page: u32,
|
||||
major: Option<i32>,
|
||||
minor: Option<i32>,
|
||||
patch: Option<i32>,
|
||||
}
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.route(
|
||||
"/:id",
|
||||
get(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(build_id): Path<BuildId>| async move {
|
||||
let build = state
|
||||
.get_build_check_permissions(&build_id.id, &user, PermissionLevel::Read)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(build))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/list",
|
||||
get(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Query(query): Query<Document>| async move {
|
||||
let builds = state
|
||||
.list_builds(&user, query)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(builds))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/create",
|
||||
post(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Json(build): Json<CreateBuildBody>| async move {
|
||||
let build = state
|
||||
.create_build(&build.name, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(build))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/create_full",
|
||||
post(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Json(build): Json<Build>| async move {
|
||||
let build = spawn_request_action(async move {
|
||||
state
|
||||
.create_full_build(build, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
})
|
||||
.await??;
|
||||
response!(Json(build))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/copy",
|
||||
post(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(BuildId { id }): Path<BuildId>,
|
||||
Json(build): Json<CopyBuildBody>| async move {
|
||||
let build = spawn_request_action(async move {
|
||||
state
|
||||
.copy_build(&id, build.name, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
})
|
||||
.await??;
|
||||
response!(Json(build))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/delete",
|
||||
delete(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(build_id): Path<BuildId>| async move {
|
||||
let build = spawn_request_action(async move {
|
||||
state
|
||||
.delete_build(&build_id.id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
})
|
||||
.await??;
|
||||
response!(Json(build))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/update",
|
||||
patch(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Json(build): Json<Build>| async move {
|
||||
let build = spawn_request_action(async move {
|
||||
state
|
||||
.update_build(build, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
})
|
||||
.await??;
|
||||
response!(Json(build))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/build",
|
||||
post(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(build_id): Path<BuildId>| async move {
|
||||
let update = spawn_request_action(async move {
|
||||
state
|
||||
.build(&build_id.id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
})
|
||||
.await??;
|
||||
response!(Json(update))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/action_state",
|
||||
get(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(BuildId { id }): Path<BuildId>| async move {
|
||||
let action_state = state
|
||||
.get_build_action_states(id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(action_state))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/versions",
|
||||
get(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(BuildId { id }),
|
||||
Query(query): Query<BuildVersionsQuery>| async move {
|
||||
let versions = state
|
||||
.get_build_versions(&id, &user, query)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(versions))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/aws_builder_defaults",
|
||||
get(|Extension(state): StateExtension| async move {
|
||||
Json(AwsBuilderConfig {
|
||||
access_key_id: String::new(),
|
||||
secret_access_key: String::new(),
|
||||
..state.config.aws.clone()
|
||||
})
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/docker_organizations",
|
||||
get(|Extension(state): StateExtension| async move {
|
||||
Json(state.config.docker_organizations.clone())
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
impl State {
|
||||
async fn list_builds(
|
||||
&self,
|
||||
user: &RequestUser,
|
||||
query: impl Into<Option<Document>>,
|
||||
) -> anyhow::Result<Vec<Build>> {
|
||||
let builds: Vec<Build> = self
|
||||
.db
|
||||
.builds
|
||||
.get_some(query, None)
|
||||
.await
|
||||
.context("failed at get all builds query")?
|
||||
.into_iter()
|
||||
.filter(|s| {
|
||||
if user.is_admin {
|
||||
true
|
||||
} else {
|
||||
let permissions = s.get_user_permissions(&user.id);
|
||||
permissions != PermissionLevel::None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
Ok(builds)
|
||||
}
|
||||
|
||||
async fn get_build_action_states(
|
||||
&self,
|
||||
id: String,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<BuildActionState> {
|
||||
self.get_build_check_permissions(&id, &user, PermissionLevel::Read)
|
||||
.await?;
|
||||
let action_state = self
|
||||
.build_action_states
|
||||
.lock()
|
||||
.await
|
||||
.entry(id)
|
||||
.or_default()
|
||||
.clone();
|
||||
Ok(action_state)
|
||||
}
|
||||
|
||||
pub async fn get_build_versions(
|
||||
&self,
|
||||
id: &str,
|
||||
user: &RequestUser,
|
||||
query: BuildVersionsQuery,
|
||||
) -> anyhow::Result<Vec<BuildVersionsReponse>> {
|
||||
self.get_build_check_permissions(&id, user, PermissionLevel::Read)
|
||||
.await?;
|
||||
let mut filter = doc! {
|
||||
"target": {
|
||||
"type": "Build",
|
||||
"id": id
|
||||
},
|
||||
"operation": Operation::BuildBuild.to_string(),
|
||||
"status": UpdateStatus::Complete.to_string(),
|
||||
"success": true
|
||||
};
|
||||
if let Some(major) = query.major {
|
||||
filter.insert("version.major", major);
|
||||
}
|
||||
if let Some(minor) = query.minor {
|
||||
filter.insert("version.minor", minor);
|
||||
}
|
||||
if let Some(patch) = query.patch {
|
||||
filter.insert("version.patch", patch);
|
||||
}
|
||||
let versions = self
|
||||
.db
|
||||
.updates
|
||||
.get_some(
|
||||
filter,
|
||||
FindOptions::builder()
|
||||
.sort(doc! { "_id": -1 })
|
||||
.limit(NUM_VERSIONS_PER_PAGE as i64)
|
||||
.skip(query.page as u64 * NUM_VERSIONS_PER_PAGE)
|
||||
.build(),
|
||||
)
|
||||
.await
|
||||
.context("failed to pull versions from mongo")?
|
||||
.into_iter()
|
||||
.map(|u| (u.version, u.start_ts))
|
||||
.filter(|(v, _)| v.is_some())
|
||||
.map(|(v, ts)| BuildVersionsReponse {
|
||||
version: v.unwrap(),
|
||||
ts,
|
||||
})
|
||||
.collect();
|
||||
Ok(versions)
|
||||
}
|
||||
}
|
||||
@@ -1,534 +0,0 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use anyhow::Context;
|
||||
use axum::{
|
||||
extract::{Path, Query},
|
||||
routing::{delete, get, patch, post},
|
||||
Extension, Json, Router,
|
||||
};
|
||||
use futures_util::future::join_all;
|
||||
use helpers::handle_anyhow_error;
|
||||
use mungos::{doc, options::FindOneOptions, Deserialize, Document, Serialize};
|
||||
use types::{
|
||||
traits::Permissioned, Deployment, DeploymentActionState, DeploymentWithContainerState,
|
||||
DockerContainerState, DockerContainerStats, Log, Operation, PermissionLevel, Server,
|
||||
UpdateStatus,
|
||||
};
|
||||
use typeshare::typeshare;
|
||||
|
||||
use crate::{
|
||||
auth::{RequestUser, RequestUserExtension},
|
||||
response,
|
||||
state::{State, StateExtension},
|
||||
};
|
||||
|
||||
use super::spawn_request_action;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct DeploymentId {
|
||||
id: String,
|
||||
}
|
||||
|
||||
#[typeshare]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CreateDeploymentBody {
|
||||
name: String,
|
||||
server_id: String,
|
||||
}
|
||||
|
||||
#[typeshare]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CopyDeploymentBody {
|
||||
name: String,
|
||||
server_id: String,
|
||||
}
|
||||
|
||||
#[typeshare]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct RenameDeploymentBody {
|
||||
new_name: String,
|
||||
}
|
||||
|
||||
#[typeshare]
|
||||
#[derive(Deserialize)]
|
||||
pub struct GetContainerLogQuery {
|
||||
tail: Option<u32>,
|
||||
}
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.route(
|
||||
"/:id",
|
||||
get(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(deployment_id): Path<DeploymentId>| async move {
|
||||
let res = state
|
||||
.get_deployment_with_container_state(&user, &deployment_id.id)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(res))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/list",
|
||||
get(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Query(query): Query<Document>| async move {
|
||||
let deployments = state
|
||||
.list_deployments_with_container_state(&user, query)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(deployments))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/create",
|
||||
post(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Json(deployment): Json<CreateDeploymentBody>| async move {
|
||||
let deployment = state
|
||||
.create_deployment(&deployment.name, deployment.server_id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(deployment))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/create_full",
|
||||
post(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Json(full_deployment): Json<Deployment>| async move {
|
||||
let deployment = spawn_request_action(async move {
|
||||
state
|
||||
.create_full_deployment(full_deployment, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
})
|
||||
.await??;
|
||||
response!(Json(deployment))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/copy",
|
||||
post(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(DeploymentId { id }): Path<DeploymentId>,
|
||||
Json(deployment): Json<CopyDeploymentBody>| async move {
|
||||
let deployment = spawn_request_action(async move {
|
||||
state
|
||||
.copy_deployment(&id, deployment.name, deployment.server_id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
})
|
||||
.await??;
|
||||
response!(Json(deployment))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/delete",
|
||||
delete(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(deployment_id): Path<DeploymentId>| async move {
|
||||
let deployment = spawn_request_action(async move {
|
||||
state
|
||||
.delete_deployment(&deployment_id.id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
})
|
||||
.await??;
|
||||
response!(Json(deployment))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/update",
|
||||
patch(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Json(deployment): Json<Deployment>| async move {
|
||||
let deployment = spawn_request_action(async move {
|
||||
state
|
||||
.update_deployment(deployment, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
})
|
||||
.await??;
|
||||
response!(Json(deployment))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/rename",
|
||||
patch(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
deployment: Path<DeploymentId>,
|
||||
body: Json<RenameDeploymentBody>| async move {
|
||||
let update = spawn_request_action(async move {
|
||||
state
|
||||
.rename_deployment(&deployment.id, &body.new_name, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
})
|
||||
.await??;
|
||||
response!(Json(update))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/reclone",
|
||||
post(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(deployment_id): Path<DeploymentId>| async move {
|
||||
let update = spawn_request_action(async move {
|
||||
state
|
||||
.reclone_deployment(&deployment_id.id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
})
|
||||
.await??;
|
||||
response!(Json(update))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/deploy",
|
||||
post(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(deployment_id): Path<DeploymentId>| async move {
|
||||
let update = spawn_request_action(async move {
|
||||
state
|
||||
.deploy_container(&deployment_id.id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
})
|
||||
.await??;
|
||||
response!(Json(update))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/start_container",
|
||||
post(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(deployment_id): Path<DeploymentId>| async move {
|
||||
let update = spawn_request_action(async move {
|
||||
state
|
||||
.start_container(&deployment_id.id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
})
|
||||
.await??;
|
||||
response!(Json(update))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/stop_container",
|
||||
post(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(deployment_id): Path<DeploymentId>| async move {
|
||||
let update = spawn_request_action(async move {
|
||||
state
|
||||
.stop_container(&deployment_id.id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
})
|
||||
.await??;
|
||||
response!(Json(update))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/remove_container",
|
||||
post(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(deployment_id): Path<DeploymentId>| async move {
|
||||
let update = spawn_request_action(async move {
|
||||
state
|
||||
.remove_container(&deployment_id.id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
})
|
||||
.await??;
|
||||
response!(Json(update))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/pull",
|
||||
post(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(deployment_id): Path<DeploymentId>| async move {
|
||||
let update = spawn_request_action(async move {
|
||||
state
|
||||
.pull_deployment_repo(&deployment_id.id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
})
|
||||
.await??;
|
||||
response!(Json(update))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/action_state",
|
||||
get(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(DeploymentId { id }): Path<DeploymentId>| async move {
|
||||
let action_state = state
|
||||
.get_deployment_action_states(id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(action_state))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/log",
|
||||
get(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(deployment_id): Path<DeploymentId>,
|
||||
Query(query): Query<GetContainerLogQuery>| async move {
|
||||
let log = state
|
||||
.get_deployment_container_log(&deployment_id.id, &user, query.tail)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(log))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/stats",
|
||||
get(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(DeploymentId { id })| async move {
|
||||
let stats = state
|
||||
.get_deployment_container_stats(&id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(stats))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/deployed_version",
|
||||
get(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(DeploymentId { id })| async move {
|
||||
let version = state
|
||||
.get_deployment_deployed_version(&id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(version)
|
||||
},
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub async fn get_deployment_with_container_state(
|
||||
&self,
|
||||
user: &RequestUser,
|
||||
id: &str,
|
||||
) -> anyhow::Result<DeploymentWithContainerState> {
|
||||
let deployment = self
|
||||
.get_deployment_check_permissions(id, user, PermissionLevel::Read)
|
||||
.await?;
|
||||
let server = self.db.get_server(&deployment.server_id).await?;
|
||||
let (state, container) = match self.periphery.container_list(&server).await {
|
||||
Ok(containers) => match containers.into_iter().find(|c| c.name == deployment.name) {
|
||||
Some(container) => (container.state, Some(container)),
|
||||
None => (DockerContainerState::NotDeployed, None),
|
||||
},
|
||||
Err(_) => (DockerContainerState::Unknown, None),
|
||||
};
|
||||
Ok(DeploymentWithContainerState {
|
||||
deployment,
|
||||
state,
|
||||
container,
|
||||
})
|
||||
}
|
||||
|
||||
async fn list_deployments_with_container_state(
|
||||
&self,
|
||||
user: &RequestUser,
|
||||
query: impl Into<Option<Document>>,
|
||||
) -> anyhow::Result<Vec<DeploymentWithContainerState>> {
|
||||
let deployments: Vec<Deployment> = self
|
||||
.db
|
||||
.deployments
|
||||
.get_some(query, None)
|
||||
.await
|
||||
.context("failed at get all deployments query")?
|
||||
.into_iter()
|
||||
.filter(|s| {
|
||||
if user.is_admin {
|
||||
true
|
||||
} else {
|
||||
let permissions = s.get_user_permissions(&user.id);
|
||||
permissions != PermissionLevel::None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let mut servers: Vec<Server> = Vec::new();
|
||||
for d in &deployments {
|
||||
if servers.iter().find(|s| s.id == d.server_id).is_none() {
|
||||
servers.push(self.db.get_server(&d.server_id).await?)
|
||||
}
|
||||
}
|
||||
let containers_futures = servers
|
||||
.into_iter()
|
||||
.map(|server| async { (self.periphery.container_list(&server).await, server.id) });
|
||||
|
||||
let containers = join_all(containers_futures)
|
||||
.await
|
||||
.into_iter()
|
||||
.map(|(container, server_id)| (server_id, container.ok()))
|
||||
.collect::<HashMap<_, _>>();
|
||||
let deployments_with_containers = deployments
|
||||
.into_iter()
|
||||
.map(|deployment| {
|
||||
let (state, container) = match containers.get(&deployment.server_id).unwrap() {
|
||||
Some(container) => {
|
||||
match container
|
||||
.iter()
|
||||
.find(|c| c.name == deployment.name)
|
||||
.map(|c| c.to_owned())
|
||||
{
|
||||
Some(container) => (container.state, Some(container)),
|
||||
None => (DockerContainerState::NotDeployed, None),
|
||||
}
|
||||
}
|
||||
None => (DockerContainerState::Unknown, None),
|
||||
};
|
||||
DeploymentWithContainerState {
|
||||
container,
|
||||
deployment,
|
||||
state,
|
||||
}
|
||||
})
|
||||
.collect::<Vec<DeploymentWithContainerState>>();
|
||||
Ok(deployments_with_containers)
|
||||
}
|
||||
|
||||
async fn get_deployment_action_states(
|
||||
&self,
|
||||
id: String,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<DeploymentActionState> {
|
||||
self.get_deployment_check_permissions(&id, &user, PermissionLevel::Read)
|
||||
.await?;
|
||||
let action_state = self
|
||||
.deployment_action_states
|
||||
.lock()
|
||||
.await
|
||||
.entry(id)
|
||||
.or_default()
|
||||
.clone();
|
||||
Ok(action_state)
|
||||
}
|
||||
|
||||
async fn get_deployment_container_log(
|
||||
&self,
|
||||
id: &str,
|
||||
user: &RequestUser,
|
||||
tail: Option<u32>,
|
||||
) -> anyhow::Result<Log> {
|
||||
let deployment = self
|
||||
.get_deployment_check_permissions(&id, &user, PermissionLevel::Read)
|
||||
.await?;
|
||||
let server = self.db.get_server(&deployment.server_id).await?;
|
||||
let log = self
|
||||
.periphery
|
||||
.container_log(&server, &deployment.name, tail)
|
||||
.await?;
|
||||
Ok(log)
|
||||
}
|
||||
|
||||
async fn get_deployment_container_stats(
|
||||
&self,
|
||||
id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<DockerContainerStats> {
|
||||
let deployment = self
|
||||
.get_deployment_check_permissions(&id, &user, PermissionLevel::Read)
|
||||
.await?;
|
||||
let server = self.db.get_server(&deployment.server_id).await?;
|
||||
let stats = self
|
||||
.periphery
|
||||
.container_stats(&server, &deployment.name)
|
||||
.await?;
|
||||
Ok(stats)
|
||||
}
|
||||
|
||||
async fn get_deployment_deployed_version(
|
||||
&self,
|
||||
id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<String> {
|
||||
let deployment = self
|
||||
.get_deployment_check_permissions(&id, &user, PermissionLevel::Read)
|
||||
.await?;
|
||||
if deployment.build_id.is_some() {
|
||||
let latest_deploy_update = self
|
||||
.db
|
||||
.updates
|
||||
.find_one(
|
||||
doc! {
|
||||
"target": {
|
||||
"type": "Deployment",
|
||||
"id": id
|
||||
},
|
||||
"operation": Operation::DeployContainer.to_string(),
|
||||
"status": UpdateStatus::Complete.to_string(),
|
||||
"success": true,
|
||||
},
|
||||
FindOneOptions::builder().sort(doc! { "_id": -1 }).build(),
|
||||
)
|
||||
.await
|
||||
.context("failed at query to get latest deploy update from mongo")?;
|
||||
if let Some(update) = latest_deploy_update {
|
||||
if let Some(version) = update.version {
|
||||
Ok(version.to_string())
|
||||
} else {
|
||||
Ok("unknown".to_string())
|
||||
}
|
||||
} else {
|
||||
Ok("unknown".to_string())
|
||||
}
|
||||
} else {
|
||||
let split = deployment
|
||||
.docker_run_args
|
||||
.image
|
||||
.split(':')
|
||||
.collect::<Vec<&str>>();
|
||||
if let Some(version) = split.get(1) {
|
||||
Ok(version.to_string())
|
||||
} else {
|
||||
Ok("unknown".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,176 +0,0 @@
|
||||
use anyhow::{anyhow, Context};
|
||||
use axum::{extract::Path, http::HeaderMap, routing::post, Router};
|
||||
use axum_oauth2::random_duration;
|
||||
use helpers::handle_anyhow_error;
|
||||
use hex::ToHex;
|
||||
use hmac::{Hmac, Mac};
|
||||
use mungos::Deserialize;
|
||||
use sha2::Sha256;
|
||||
use types::GITHUB_WEBHOOK_USER_ID;
|
||||
|
||||
use crate::{
|
||||
auth::RequestUser,
|
||||
state::{State, StateExtension},
|
||||
};
|
||||
|
||||
use super::spawn_request_action;
|
||||
|
||||
type HmacSha256 = Hmac<Sha256>;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Id {
|
||||
id: String,
|
||||
}
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.route(
|
||||
"/build/:id",
|
||||
post(|state: StateExtension, Path(Id { id }), headers: HeaderMap, body: String| async move {
|
||||
spawn_request_action(async move {
|
||||
state.handle_build_webhook(&id, headers, body).await.map_err(handle_anyhow_error)
|
||||
}).await?
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/deployment/:id",
|
||||
post(|state: StateExtension, Path(Id { id }), headers: HeaderMap, body: String| async move {
|
||||
spawn_request_action(async move {
|
||||
state.handle_deployment_webhook(&id, headers, body).await.map_err(handle_anyhow_error)
|
||||
}).await?
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/procedure/:id",
|
||||
post(|state: StateExtension, Path(Id { id }), headers: HeaderMap, body: String| async move {
|
||||
spawn_request_action(async move {
|
||||
state.handle_procedure_webhook(&id, headers, body).await.map_err(handle_anyhow_error)
|
||||
}).await?
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
impl State {
|
||||
async fn handle_build_webhook(
|
||||
&self,
|
||||
id: &str,
|
||||
headers: HeaderMap,
|
||||
body: String,
|
||||
) -> anyhow::Result<()> {
|
||||
self.verify_gh_signature(headers, &body).await?;
|
||||
let request_branch = extract_branch(&body)?;
|
||||
let expected_branch = self
|
||||
.db
|
||||
.get_build(id)
|
||||
.await?
|
||||
.branch
|
||||
.ok_or(anyhow!("build has no branch attached"))?;
|
||||
if request_branch != expected_branch {
|
||||
return Err(anyhow!("request branch does not match expected"));
|
||||
}
|
||||
self.build(
|
||||
id,
|
||||
&RequestUser {
|
||||
id: String::from(GITHUB_WEBHOOK_USER_ID),
|
||||
is_admin: true,
|
||||
create_server_permissions: false,
|
||||
create_build_permissions: false,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_deployment_webhook(
|
||||
&self,
|
||||
id: &str,
|
||||
headers: HeaderMap,
|
||||
body: String,
|
||||
) -> anyhow::Result<()> {
|
||||
self.verify_gh_signature(headers, &body).await?;
|
||||
let request_branch = extract_branch(&body)?;
|
||||
let expected_branch = self
|
||||
.db
|
||||
.get_deployment(id)
|
||||
.await?
|
||||
.branch
|
||||
.ok_or(anyhow!("deployment has no branch attached"))?;
|
||||
if request_branch != expected_branch {
|
||||
return Err(anyhow!("request branch does not match expected"));
|
||||
}
|
||||
self.pull_deployment_repo(
|
||||
id,
|
||||
&RequestUser {
|
||||
id: String::from(GITHUB_WEBHOOK_USER_ID),
|
||||
is_admin: true,
|
||||
create_server_permissions: false,
|
||||
create_build_permissions: false,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_procedure_webhook(
|
||||
&self,
|
||||
id: &str,
|
||||
headers: HeaderMap,
|
||||
body: String,
|
||||
) -> anyhow::Result<()> {
|
||||
self.verify_gh_signature(headers, &body).await?;
|
||||
let request_branch = extract_branch(&body)?;
|
||||
let expected_branches = self.db.get_procedure(id).await?.webhook_branches;
|
||||
if !expected_branches.contains(&request_branch) {
|
||||
return Err(anyhow!("request branch does not match expected"));
|
||||
}
|
||||
self.run_procedure(
|
||||
id,
|
||||
&RequestUser {
|
||||
id: String::from(GITHUB_WEBHOOK_USER_ID),
|
||||
is_admin: true,
|
||||
create_server_permissions: false,
|
||||
create_build_permissions: false,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn verify_gh_signature(&self, headers: HeaderMap, body: &str) -> anyhow::Result<()> {
|
||||
// wait random amount of time
|
||||
tokio::time::sleep(random_duration(0, 500)).await;
|
||||
|
||||
let signature = headers.get("x-hub-signature-256");
|
||||
if signature.is_none() {
|
||||
return Err(anyhow!("no signature in headers"));
|
||||
}
|
||||
let signature = signature.unwrap().to_str();
|
||||
if signature.is_err() {
|
||||
return Err(anyhow!("failed to unwrap signature"));
|
||||
}
|
||||
let signature = signature.unwrap().replace("sha256=", "");
|
||||
let mut mac = HmacSha256::new_from_slice(self.config.github_webhook_secret.as_bytes())
|
||||
.expect("github webhook | failed to create hmac sha256");
|
||||
mac.update(body.as_bytes());
|
||||
let expected = mac.finalize().into_bytes().encode_hex::<String>();
|
||||
if signature == expected {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("signature does not equal expected"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct GithubWebhookBody {
|
||||
#[serde(rename = "ref")]
|
||||
branch: String,
|
||||
}
|
||||
|
||||
fn extract_branch(body: &str) -> anyhow::Result<String> {
|
||||
let branch = serde_json::from_str::<GithubWebhookBody>(body)
|
||||
.context("failed to parse github request body")?
|
||||
.branch
|
||||
.replace("refs/heads/", "");
|
||||
Ok(branch)
|
||||
}
|
||||
@@ -1,142 +0,0 @@
|
||||
use anyhow::Context;
|
||||
use axum::{
|
||||
extract::{Path, Query},
|
||||
routing::{delete, get, patch, post},
|
||||
Extension, Json, Router,
|
||||
};
|
||||
use helpers::handle_anyhow_error;
|
||||
use mungos::{Deserialize, Document, Serialize};
|
||||
use types::{traits::Permissioned, Group, PermissionLevel};
|
||||
use typeshare::typeshare;
|
||||
|
||||
use crate::{
|
||||
auth::{RequestUser, RequestUserExtension},
|
||||
response,
|
||||
state::{State, StateExtension},
|
||||
};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct GroupId {
|
||||
id: String,
|
||||
}
|
||||
|
||||
#[typeshare]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CreateGroupBody {
|
||||
name: String,
|
||||
}
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.route(
|
||||
"/:id",
|
||||
get(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(group_id): Path<GroupId>| async move {
|
||||
let group = state
|
||||
.get_group_check_permissions(&group_id.id, &user, PermissionLevel::Read)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(group))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/list",
|
||||
get(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Query(query): Query<Document>| async move {
|
||||
let groups = state
|
||||
.list_groups(&user, query)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(groups))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/create",
|
||||
post(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Json(group): Json<CreateGroupBody>| async move {
|
||||
let group = state
|
||||
.create_group(&group.name, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(group))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/create_full",
|
||||
post(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Json(group): Json<Group>| async move {
|
||||
let group = state
|
||||
.create_full_group(group, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(group))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/delete",
|
||||
delete(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(group_id): Path<GroupId>| async move {
|
||||
let group = state
|
||||
.delete_group(&group_id.id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(group))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/update",
|
||||
patch(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Json(group): Json<Group>| async move {
|
||||
let group = state
|
||||
.update_group(group, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(group))
|
||||
},
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
impl State {
|
||||
async fn list_groups(
|
||||
&self,
|
||||
user: &RequestUser,
|
||||
query: impl Into<Option<Document>>,
|
||||
) -> anyhow::Result<Vec<Group>> {
|
||||
let groups: Vec<Group> = self
|
||||
.db
|
||||
.groups
|
||||
.get_some(query, None)
|
||||
.await
|
||||
.context("failed at get all groups query")?
|
||||
.into_iter()
|
||||
.filter(|s| {
|
||||
if user.is_admin {
|
||||
true
|
||||
} else {
|
||||
let permissions = s.get_user_permissions(&user.id);
|
||||
permissions != PermissionLevel::None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
// groups.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));
|
||||
Ok(groups)
|
||||
}
|
||||
}
|
||||
@@ -1,233 +0,0 @@
|
||||
use anyhow::{anyhow, Context};
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::Path,
|
||||
http::{Request, StatusCode},
|
||||
middleware,
|
||||
routing::{get, post},
|
||||
Extension, Json, Router,
|
||||
};
|
||||
use futures_util::Future;
|
||||
use helpers::handle_anyhow_error;
|
||||
use mungos::{doc, Deserialize};
|
||||
use types::{PermissionLevel, UpdateTarget, User};
|
||||
use typeshare::typeshare;
|
||||
|
||||
use crate::{
|
||||
auth::{auth_request, JwtExtension, RequestUser, RequestUserExtension},
|
||||
response,
|
||||
state::{State, StateExtension},
|
||||
ResponseResult,
|
||||
};
|
||||
|
||||
pub mod build;
|
||||
pub mod deployment;
|
||||
mod github_listener;
|
||||
pub mod group;
|
||||
pub mod permissions;
|
||||
pub mod procedure;
|
||||
pub mod secret;
|
||||
pub mod server;
|
||||
pub mod update;
|
||||
|
||||
#[typeshare]
|
||||
#[derive(Deserialize)]
|
||||
struct UpdateDescriptionBody {
|
||||
target: UpdateTarget,
|
||||
description: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct UserId {
|
||||
id: String,
|
||||
}
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.route(
|
||||
"/title",
|
||||
get(|state: StateExtension| async move { state.config.title.clone() }),
|
||||
)
|
||||
.route("/user", get(get_request_user))
|
||||
.nest("/listener", github_listener::router())
|
||||
.nest(
|
||||
"/",
|
||||
Router::new()
|
||||
.route("/user/:id", get(get_user_at_id))
|
||||
.route(
|
||||
"/username/:id",
|
||||
get(|state: StateExtension, Path(UserId { id })| async move {
|
||||
let user = state
|
||||
.db
|
||||
.get_user(&id)
|
||||
.await
|
||||
.context("failed to find user at id")
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(user.username))
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/github_webhook_base_url",
|
||||
get(|state: StateExtension| async move {
|
||||
state
|
||||
.config
|
||||
.github_webhook_base_url
|
||||
.as_ref()
|
||||
.unwrap_or(&state.config.host)
|
||||
.to_string()
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/update_description",
|
||||
post(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
body: Json<UpdateDescriptionBody>| async move {
|
||||
state
|
||||
.update_description(&body.target, &body.description, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
},
|
||||
),
|
||||
)
|
||||
.route("/users", get(get_users))
|
||||
.nest("/build", build::router())
|
||||
.nest("/deployment", deployment::router())
|
||||
.nest("/server", server::router())
|
||||
.nest("/procedure", procedure::router())
|
||||
.nest("/group", group::router())
|
||||
.nest("/update", update::router())
|
||||
.nest("/permissions", permissions::router())
|
||||
.nest("/secret", secret::router())
|
||||
.layer(middleware::from_fn(auth_request)),
|
||||
)
|
||||
}
|
||||
|
||||
async fn get_request_user(
|
||||
Extension(jwt): JwtExtension,
|
||||
req: Request<Body>,
|
||||
) -> ResponseResult<Json<User>> {
|
||||
let mut user = jwt.authenticate(&req).await.map_err(handle_anyhow_error)?;
|
||||
user.password = None;
|
||||
for secret in &mut user.secrets {
|
||||
secret.hash = String::new();
|
||||
}
|
||||
Ok(Json(user))
|
||||
}
|
||||
|
||||
async fn get_users(
|
||||
state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
) -> ResponseResult<Json<Vec<User>>> {
|
||||
if user.is_admin {
|
||||
let users = state
|
||||
.db
|
||||
.users
|
||||
.get_some(None, None)
|
||||
.await
|
||||
.context("failed to get users from db")
|
||||
.map_err(handle_anyhow_error)?
|
||||
.into_iter()
|
||||
.map(|u| User {
|
||||
password: None,
|
||||
secrets: vec![],
|
||||
..u
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
Ok(Json(users))
|
||||
} else {
|
||||
Err((StatusCode::UNAUTHORIZED, "user is not admin".to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_user_at_id(
|
||||
state: StateExtension,
|
||||
Path(UserId { id }): Path<UserId>,
|
||||
user: RequestUserExtension,
|
||||
) -> ResponseResult<Json<User>> {
|
||||
if user.is_admin {
|
||||
let mut user = state
|
||||
.db
|
||||
.users
|
||||
.find_one_by_id(&id)
|
||||
.await
|
||||
.context("failed at query to get user from mongo")
|
||||
.map_err(handle_anyhow_error)?
|
||||
.ok_or(anyhow!(""))
|
||||
.map_err(handle_anyhow_error)?;
|
||||
user.password = None;
|
||||
for secret in &mut user.secrets {
|
||||
secret.hash = String::new();
|
||||
}
|
||||
Ok(Json(user))
|
||||
} else {
|
||||
Err((StatusCode::UNAUTHORIZED, "user is not admin".to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
// need to run requested actions in here to prevent them being dropped mid action when user disconnects prematurely
|
||||
pub async fn spawn_request_action<A>(action: A) -> ResponseResult<A::Output>
|
||||
where
|
||||
A: Future + Send + 'static,
|
||||
A::Output: Send + 'static,
|
||||
{
|
||||
let res = tokio::spawn(action)
|
||||
.await
|
||||
.context("failure at action thread spawn")
|
||||
.map_err(handle_anyhow_error)?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub async fn update_description(
|
||||
&self,
|
||||
target: &UpdateTarget,
|
||||
description: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<()> {
|
||||
match target {
|
||||
UpdateTarget::Build(id) => {
|
||||
self.get_build_check_permissions(id, user, PermissionLevel::Update)
|
||||
.await?;
|
||||
self.db
|
||||
.builds
|
||||
.update_one::<()>(id, mungos::Update::Set(doc! { "description": description }))
|
||||
.await?;
|
||||
}
|
||||
UpdateTarget::Deployment(id) => {
|
||||
self.get_deployment_check_permissions(id, user, PermissionLevel::Update)
|
||||
.await?;
|
||||
self.db
|
||||
.deployments
|
||||
.update_one::<()>(id, mungos::Update::Set(doc! { "description": description }))
|
||||
.await?;
|
||||
}
|
||||
UpdateTarget::Server(id) => {
|
||||
self.get_server_check_permissions(id, user, PermissionLevel::Update)
|
||||
.await?;
|
||||
self.db
|
||||
.servers
|
||||
.update_one::<()>(id, mungos::Update::Set(doc! { "description": description }))
|
||||
.await?;
|
||||
}
|
||||
UpdateTarget::Group(id) => {
|
||||
self.get_group_check_permissions(id, user, PermissionLevel::Update)
|
||||
.await?;
|
||||
self.db
|
||||
.groups
|
||||
.update_one::<()>(id, mungos::Update::Set(doc! { "description": description }))
|
||||
.await?;
|
||||
}
|
||||
UpdateTarget::Procedure(id) => {
|
||||
self.get_procedure_check_permissions(id, user, PermissionLevel::Update)
|
||||
.await?;
|
||||
self.db
|
||||
.procedures
|
||||
.update_one::<()>(id, mungos::Update::Set(doc! { "description": description }))
|
||||
.await?;
|
||||
}
|
||||
_ => return Err(anyhow!("invalid target: {target:?}")),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,409 +0,0 @@
|
||||
use anyhow::{anyhow, Context};
|
||||
use axum::{routing::post, Extension, Json, Router};
|
||||
use helpers::handle_anyhow_error;
|
||||
use mungos::{doc, Deserialize, Document, Serialize};
|
||||
use types::{
|
||||
monitor_timestamp, Build, Deployment, Group, Log, Operation, PermissionLevel,
|
||||
PermissionsTarget, Procedure, Server, Update, UpdateStatus, UpdateTarget,
|
||||
};
|
||||
use typeshare::typeshare;
|
||||
|
||||
use crate::{auth::RequestUserExtension, response, state::StateExtension};
|
||||
|
||||
#[typeshare]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct PermissionsUpdateBody {
|
||||
user_id: String,
|
||||
permission: PermissionLevel,
|
||||
target_type: PermissionsTarget,
|
||||
target_id: String,
|
||||
}
|
||||
|
||||
#[typeshare]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct ModifyUserEnabledBody {
|
||||
user_id: String,
|
||||
enabled: bool,
|
||||
}
|
||||
|
||||
#[typeshare]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct ModifyUserCreateServerBody {
|
||||
user_id: String,
|
||||
create_server_permissions: bool,
|
||||
}
|
||||
|
||||
#[typeshare]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct ModifyUserCreateBuildBody {
|
||||
user_id: String,
|
||||
create_build_permissions: bool,
|
||||
}
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.route(
|
||||
"/update",
|
||||
post(|state, user, update| async {
|
||||
let update = update_permissions(state, user, update)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(update))
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/modify_enabled",
|
||||
post(|state, user, body| async {
|
||||
let update = modify_user_enabled(state, user, body)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(update))
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/modify_create_server",
|
||||
post(|state, user, body| async {
|
||||
let update = modify_user_create_server_permissions(state, user, body)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(update))
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/modify_create_build",
|
||||
post(|state, user, body| async {
|
||||
let update = modify_user_create_build_permissions(state, user, body)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(update))
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
async fn update_permissions(
|
||||
Extension(state): StateExtension,
|
||||
Extension(req_user): RequestUserExtension,
|
||||
Json(permission_update): Json<PermissionsUpdateBody>,
|
||||
) -> anyhow::Result<Update> {
|
||||
if !req_user.is_admin {
|
||||
return Err(anyhow!(
|
||||
"user not authorized for this action (is not admin)"
|
||||
));
|
||||
}
|
||||
let target_user = state
|
||||
.db
|
||||
.users
|
||||
.find_one_by_id(&permission_update.user_id)
|
||||
.await
|
||||
.context("failed at find target user query")?
|
||||
.ok_or(anyhow!(
|
||||
"failed to find a user with id {}",
|
||||
permission_update.user_id
|
||||
))?;
|
||||
if !target_user.enabled {
|
||||
return Err(anyhow!("target user not enabled"));
|
||||
}
|
||||
let mut update = Update {
|
||||
operation: Operation::ModifyUserPermissions,
|
||||
start_ts: monitor_timestamp(),
|
||||
success: true,
|
||||
operator: req_user.id.clone(),
|
||||
status: UpdateStatus::Complete,
|
||||
..Default::default()
|
||||
};
|
||||
let log_text = match permission_update.target_type {
|
||||
PermissionsTarget::Server => {
|
||||
let server = state
|
||||
.db
|
||||
.servers
|
||||
.find_one_by_id(&permission_update.target_id)
|
||||
.await
|
||||
.context("failed at find server query")?
|
||||
.ok_or(anyhow!(
|
||||
"failed to find a server with id {}",
|
||||
permission_update.target_id
|
||||
))?;
|
||||
state
|
||||
.db
|
||||
.servers
|
||||
.update_one::<Server>(
|
||||
&permission_update.target_id,
|
||||
mungos::Update::Set(doc! {
|
||||
format!("permissions.{}", permission_update.user_id): permission_update.permission.to_string()
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
update.target = UpdateTarget::Server(server.id);
|
||||
format!(
|
||||
"user {} given {} permissions on server {}",
|
||||
target_user.username, permission_update.permission, server.name
|
||||
)
|
||||
}
|
||||
PermissionsTarget::Deployment => {
|
||||
let deployment = state
|
||||
.db
|
||||
.deployments
|
||||
.find_one_by_id(&permission_update.target_id)
|
||||
.await
|
||||
.context("failed at find deployment query")?
|
||||
.ok_or(anyhow!(
|
||||
"failed to find a deployment with id {}",
|
||||
permission_update.target_id
|
||||
))?;
|
||||
state
|
||||
.db
|
||||
.deployments
|
||||
.update_one::<Deployment>(
|
||||
&permission_update.target_id,
|
||||
mungos::Update::Set(doc! {
|
||||
format!("permissions.{}", permission_update.user_id): permission_update.permission.to_string()
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
update.target = UpdateTarget::Deployment(deployment.id);
|
||||
format!(
|
||||
"user {} (id: {}) given {} permissions on deployment {}",
|
||||
target_user.username, target_user.id, permission_update.permission, deployment.name
|
||||
)
|
||||
}
|
||||
PermissionsTarget::Build => {
|
||||
let build = state
|
||||
.db
|
||||
.builds
|
||||
.find_one_by_id(&permission_update.target_id)
|
||||
.await
|
||||
.context("failed at find build query")?
|
||||
.ok_or(anyhow!(
|
||||
"failed to find a build with id {}",
|
||||
permission_update.target_id
|
||||
))?;
|
||||
state
|
||||
.db
|
||||
.builds
|
||||
.update_one::<Build>(
|
||||
&permission_update.target_id,
|
||||
mungos::Update::Set(doc! {
|
||||
format!("permissions.{}", permission_update.user_id): permission_update.permission.to_string()
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
update.target = UpdateTarget::Build(build.id);
|
||||
format!(
|
||||
"user {} given {} permissions on build {}",
|
||||
target_user.username, permission_update.permission, build.name
|
||||
)
|
||||
}
|
||||
PermissionsTarget::Procedure => {
|
||||
let procedure = state
|
||||
.db
|
||||
.procedures
|
||||
.find_one_by_id(&permission_update.target_id)
|
||||
.await
|
||||
.context("failed at find procedure query")?
|
||||
.ok_or(anyhow!(
|
||||
"failed to find a procedure with id {}",
|
||||
permission_update.target_id
|
||||
))?;
|
||||
state
|
||||
.db
|
||||
.procedures
|
||||
.update_one::<Procedure>(
|
||||
&permission_update.target_id,
|
||||
mungos::Update::Set(doc! {
|
||||
format!("permissions.{}", permission_update.user_id): permission_update.permission.to_string()
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
update.target = UpdateTarget::Procedure(procedure.id);
|
||||
format!(
|
||||
"user {} given {} permissions on procedure {}",
|
||||
target_user.username, permission_update.permission, procedure.name
|
||||
)
|
||||
}
|
||||
PermissionsTarget::Group => {
|
||||
let group = state
|
||||
.db
|
||||
.groups
|
||||
.find_one_by_id(&permission_update.target_id)
|
||||
.await
|
||||
.context("failed at find group query")?
|
||||
.ok_or(anyhow!(
|
||||
"failed to find a group with id {}",
|
||||
permission_update.target_id
|
||||
))?;
|
||||
state
|
||||
.db
|
||||
.groups
|
||||
.update_one::<Group>(
|
||||
&permission_update.target_id,
|
||||
mungos::Update::Set(doc! {
|
||||
format!("permissions.{}", permission_update.user_id): permission_update.permission.to_string()
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
update.target = UpdateTarget::Group(group.id);
|
||||
format!(
|
||||
"user {} given {} permissions on group {}",
|
||||
target_user.username, permission_update.permission, group.name
|
||||
)
|
||||
}
|
||||
};
|
||||
update
|
||||
.logs
|
||||
.push(Log::simple("modify permissions", log_text));
|
||||
update.end_ts = Some(monitor_timestamp());
|
||||
update.id = state.add_update(update.clone()).await?;
|
||||
Ok(update)
|
||||
}
|
||||
|
||||
async fn modify_user_enabled(
|
||||
Extension(state): StateExtension,
|
||||
Extension(req_user): RequestUserExtension,
|
||||
Json(ModifyUserEnabledBody { user_id, enabled }): Json<ModifyUserEnabledBody>,
|
||||
) -> anyhow::Result<Update> {
|
||||
if !req_user.is_admin {
|
||||
return Err(anyhow!(
|
||||
"user does not have permissions for this action (not admin)"
|
||||
));
|
||||
}
|
||||
let user = state
|
||||
.db
|
||||
.users
|
||||
.find_one_by_id(&user_id)
|
||||
.await
|
||||
.context("failed at mongo query to find target user")?
|
||||
.ok_or(anyhow!("did not find any user with user_id {user_id}"))?;
|
||||
state
|
||||
.db
|
||||
.users
|
||||
.update_one::<Document>(&user_id, mungos::Update::Set(doc! { "enabled": enabled }))
|
||||
.await?;
|
||||
let update_type = if enabled { "enabled" } else { "disabled" };
|
||||
let ts = monitor_timestamp();
|
||||
let mut update = Update {
|
||||
target: UpdateTarget::System,
|
||||
operation: Operation::ModifyUserEnabled,
|
||||
logs: vec![Log::simple(
|
||||
"modify user enabled",
|
||||
format!("{update_type} {} (id: {})", user.username, user.id),
|
||||
)],
|
||||
start_ts: ts.clone(),
|
||||
end_ts: Some(ts),
|
||||
status: UpdateStatus::Complete,
|
||||
success: true,
|
||||
operator: req_user.id.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
update.id = state.add_update(update.clone()).await?;
|
||||
Ok(update)
|
||||
}
|
||||
|
||||
async fn modify_user_create_server_permissions(
|
||||
Extension(state): StateExtension,
|
||||
Extension(req_user): RequestUserExtension,
|
||||
Json(ModifyUserCreateServerBody {
|
||||
user_id,
|
||||
create_server_permissions,
|
||||
}): Json<ModifyUserCreateServerBody>,
|
||||
) -> anyhow::Result<Update> {
|
||||
if !req_user.is_admin {
|
||||
return Err(anyhow!(
|
||||
"user does not have permissions for this action (not admin)"
|
||||
));
|
||||
}
|
||||
let target_user = state
|
||||
.db
|
||||
.users
|
||||
.find_one_by_id(&user_id)
|
||||
.await
|
||||
.context("failed at mongo query to find target user")?
|
||||
.ok_or(anyhow!("did not find any user with user_id {user_id}"))?;
|
||||
state
|
||||
.db
|
||||
.users
|
||||
.update_one::<Document>(
|
||||
&user_id,
|
||||
mungos::Update::Set(doc! { "create_server_permissions": create_server_permissions }),
|
||||
)
|
||||
.await?;
|
||||
let update_type = if create_server_permissions {
|
||||
"enabled"
|
||||
} else {
|
||||
"disabled"
|
||||
};
|
||||
let ts = monitor_timestamp();
|
||||
let mut update = Update {
|
||||
target: UpdateTarget::System,
|
||||
operation: Operation::ModifyUserCreateServerPermissions,
|
||||
logs: vec![Log::simple(
|
||||
"modify user create server permissions",
|
||||
format!(
|
||||
"{update_type} create server permissions for {} (id: {})",
|
||||
target_user.username, target_user.id
|
||||
),
|
||||
)],
|
||||
start_ts: ts.clone(),
|
||||
end_ts: Some(ts),
|
||||
status: UpdateStatus::Complete,
|
||||
success: true,
|
||||
operator: req_user.id.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
update.id = state.add_update(update.clone()).await?;
|
||||
Ok(update)
|
||||
}
|
||||
|
||||
async fn modify_user_create_build_permissions(
|
||||
Extension(state): StateExtension,
|
||||
Extension(req_user): RequestUserExtension,
|
||||
Json(ModifyUserCreateBuildBody {
|
||||
user_id,
|
||||
create_build_permissions,
|
||||
}): Json<ModifyUserCreateBuildBody>,
|
||||
) -> anyhow::Result<Update> {
|
||||
if !req_user.is_admin {
|
||||
return Err(anyhow!(
|
||||
"user does not have permissions for this action (not admin)"
|
||||
));
|
||||
}
|
||||
let target_user = state
|
||||
.db
|
||||
.users
|
||||
.find_one_by_id(&user_id)
|
||||
.await
|
||||
.context("failed at mongo query to find target user")?
|
||||
.ok_or(anyhow!("did not find any user with user_id {user_id}"))?;
|
||||
state
|
||||
.db
|
||||
.users
|
||||
.update_one::<Document>(
|
||||
&user_id,
|
||||
mungos::Update::Set(doc! { "create_build_permissions": create_build_permissions }),
|
||||
)
|
||||
.await?;
|
||||
let update_type = if create_build_permissions {
|
||||
"enabled"
|
||||
} else {
|
||||
"disabled"
|
||||
};
|
||||
let ts = monitor_timestamp();
|
||||
let mut update = Update {
|
||||
target: UpdateTarget::System,
|
||||
operation: Operation::ModifyUserCreateBuildPermissions,
|
||||
logs: vec![Log::simple(
|
||||
"modify user create build permissions",
|
||||
format!(
|
||||
"{update_type} create build permissions for {} (id: {})",
|
||||
target_user.username, target_user.id
|
||||
),
|
||||
)],
|
||||
start_ts: ts.clone(),
|
||||
end_ts: Some(ts),
|
||||
status: UpdateStatus::Complete,
|
||||
success: true,
|
||||
operator: req_user.id.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
update.id = state.add_update(update.clone()).await?;
|
||||
Ok(update)
|
||||
}
|
||||
@@ -1,165 +0,0 @@
|
||||
use anyhow::Context;
|
||||
use axum::{
|
||||
extract::{Path, Query},
|
||||
routing::{delete, get, patch, post},
|
||||
Extension, Json, Router,
|
||||
};
|
||||
use helpers::handle_anyhow_error;
|
||||
use mungos::{Deserialize, Document, Serialize};
|
||||
use types::{traits::Permissioned, PermissionLevel, Procedure};
|
||||
use typeshare::typeshare;
|
||||
|
||||
use crate::{
|
||||
auth::{RequestUser, RequestUserExtension},
|
||||
response,
|
||||
state::{State, StateExtension},
|
||||
};
|
||||
|
||||
use super::spawn_request_action;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ProcedureId {
|
||||
id: String,
|
||||
}
|
||||
|
||||
#[typeshare]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CreateProcedureBody {
|
||||
name: String,
|
||||
}
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.route(
|
||||
"/:id",
|
||||
get(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(procedure_id): Path<ProcedureId>| async move {
|
||||
let procedure = state
|
||||
.get_procedure_check_permissions(
|
||||
&procedure_id.id,
|
||||
&user,
|
||||
PermissionLevel::Read,
|
||||
)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(procedure))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/list",
|
||||
get(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Query(query): Query<Document>| async move {
|
||||
let procedures = state
|
||||
.list_procedures(&user, query)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(procedures))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/create",
|
||||
post(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Json(procedure): Json<CreateProcedureBody>| async move {
|
||||
let procedure = state
|
||||
.create_procedure(&procedure.name, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(procedure))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/create_full",
|
||||
post(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Json(procedure): Json<Procedure>| async move {
|
||||
let procedure = state
|
||||
.create_full_procedure(procedure, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(procedure))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/delete",
|
||||
delete(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(procedure_id): Path<ProcedureId>| async move {
|
||||
let procedure = state
|
||||
.delete_procedure(&procedure_id.id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(procedure))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/update",
|
||||
patch(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Json(procedure): Json<Procedure>| async move {
|
||||
let procedure = state
|
||||
.update_procedure(procedure, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(procedure))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/run",
|
||||
post(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(procedure_id): Path<ProcedureId>| async move {
|
||||
let update = spawn_request_action(async move {
|
||||
state
|
||||
.run_procedure(&procedure_id.id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
})
|
||||
.await??;
|
||||
response!(Json(update))
|
||||
},
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
impl State {
|
||||
async fn list_procedures(
|
||||
&self,
|
||||
user: &RequestUser,
|
||||
query: impl Into<Option<Document>>,
|
||||
) -> anyhow::Result<Vec<Procedure>> {
|
||||
let procedures: Vec<Procedure> = self
|
||||
.db
|
||||
.procedures
|
||||
.get_some(query, None)
|
||||
.await
|
||||
.context("failed at get all procedures query")?
|
||||
.into_iter()
|
||||
.filter(|s| {
|
||||
if user.is_admin {
|
||||
true
|
||||
} else {
|
||||
let permissions = s.get_user_permissions(&user.id);
|
||||
permissions != PermissionLevel::None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
// procedures.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));
|
||||
Ok(procedures)
|
||||
}
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
use anyhow::{anyhow, Context};
|
||||
use axum::{
|
||||
extract::Path,
|
||||
routing::{delete, post},
|
||||
Extension, Json, Router,
|
||||
};
|
||||
use helpers::{generate_secret, handle_anyhow_error};
|
||||
use mungos::{doc, to_bson, Deserialize, Document, Serialize, Update};
|
||||
use types::{monitor_timestamp, ApiSecret};
|
||||
use typeshare::typeshare;
|
||||
|
||||
use crate::{auth::RequestUserExtension, state::StateExtension};
|
||||
|
||||
const SECRET_LENGTH: usize = 40;
|
||||
const BCRYPT_COST: u32 = 10;
|
||||
|
||||
#[typeshare]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct CreateSecretBody {
|
||||
name: String,
|
||||
expires: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct DeleteSecretPath {
|
||||
name: String,
|
||||
}
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.route(
|
||||
"/create",
|
||||
post(|state, user, secret| async {
|
||||
create(state, user, secret)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/delete/:name",
|
||||
delete(|state, user, secret_id| async {
|
||||
delete_one(state, user, secret_id)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
impl Into<ApiSecret> for CreateSecretBody {
|
||||
fn into(self) -> ApiSecret {
|
||||
ApiSecret {
|
||||
name: self.name,
|
||||
expires: self.expires,
|
||||
created_at: monitor_timestamp(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn create(
|
||||
Extension(state): StateExtension,
|
||||
Extension(req_user): RequestUserExtension,
|
||||
Json(secret): Json<CreateSecretBody>,
|
||||
) -> anyhow::Result<String> {
|
||||
let user = state.db.get_user(&req_user.id).await?;
|
||||
for s in &user.secrets {
|
||||
if s.name == secret.name {
|
||||
return Err(anyhow!("secret with name {} already exists", secret.name));
|
||||
}
|
||||
}
|
||||
let mut secret: ApiSecret = secret.into();
|
||||
let secret_str = generate_secret(SECRET_LENGTH);
|
||||
secret.hash =
|
||||
bcrypt::hash(&secret_str, BCRYPT_COST).context("failed at hashing secret string")?;
|
||||
state
|
||||
.db
|
||||
.users
|
||||
.update_one::<Document>(
|
||||
&req_user.id,
|
||||
Update::Custom(doc! {
|
||||
"$push": {
|
||||
"secrets": to_bson(&secret).context("failed at converting secret to bson")?
|
||||
}
|
||||
}),
|
||||
)
|
||||
.await
|
||||
.context("failed at mongo update query")?;
|
||||
Ok(secret_str)
|
||||
}
|
||||
|
||||
async fn delete_one(
|
||||
Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Path(DeleteSecretPath { name }): Path<DeleteSecretPath>,
|
||||
) -> anyhow::Result<()> {
|
||||
state
|
||||
.db
|
||||
.users
|
||||
.update_one::<Document>(
|
||||
&user.id,
|
||||
Update::Custom(doc! {
|
||||
"$pull": {
|
||||
"secrets": {
|
||||
"name": name
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
.await
|
||||
.context("failed at mongo update query")?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,688 +0,0 @@
|
||||
use anyhow::{anyhow, Context};
|
||||
use async_timing_util::{get_timelength_in_ms, unix_timestamp_ms};
|
||||
use axum::{
|
||||
extract::{ws::Message as AxumMessage, Path, Query, WebSocketUpgrade},
|
||||
response::IntoResponse,
|
||||
routing::{delete, get, patch, post},
|
||||
Json, Router,
|
||||
};
|
||||
use futures_util::{future::join_all, SinkExt, StreamExt};
|
||||
use helpers::handle_anyhow_error;
|
||||
use mungos::{doc, Deserialize, Document, FindOptions};
|
||||
use tokio::select;
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use types::{
|
||||
traits::Permissioned, BasicContainerInfo, HistoricalStatsQuery, ImageSummary, Network,
|
||||
PermissionLevel, Server, ServerActionState, ServerStatus, ServerWithStatus, SystemInformation,
|
||||
SystemStats, SystemStatsQuery, SystemStatsRecord,
|
||||
};
|
||||
use typeshare::typeshare;
|
||||
|
||||
const MAX_HISTORICAL_STATS_LIMIT: i64 = 500;
|
||||
|
||||
use crate::{
|
||||
auth::{RequestUser, RequestUserExtension},
|
||||
response,
|
||||
state::{State, StateExtension},
|
||||
};
|
||||
|
||||
use super::spawn_request_action;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ServerId {
|
||||
id: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Ts {
|
||||
ts: i64,
|
||||
}
|
||||
|
||||
#[typeshare]
|
||||
#[derive(Deserialize)]
|
||||
pub struct CreateServerBody {
|
||||
name: String,
|
||||
address: String,
|
||||
}
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.route(
|
||||
"/:id",
|
||||
get(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Path(server_id): Path<ServerId>| async move {
|
||||
let server = state
|
||||
.get_server(&server_id.id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(server))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/list",
|
||||
get(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Query(query): Query<Document>| async move {
|
||||
let servers = state
|
||||
.list_servers(&user, query)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(servers))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/create",
|
||||
post(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Json(server): Json<CreateServerBody>| async move {
|
||||
let server = state
|
||||
.create_server(&server.name, server.address, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(server))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/create_full",
|
||||
post(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Json(server): Json<Server>| async move {
|
||||
let server = state
|
||||
.create_full_server(server, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(server))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/delete",
|
||||
delete(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Path(server): Path<ServerId>| async move {
|
||||
let server = state
|
||||
.delete_server(&server.id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(server))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/update",
|
||||
patch(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Json(server): Json<Server>| async move {
|
||||
let server = state
|
||||
.update_server(server, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(server))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/version",
|
||||
get(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Path(ServerId { id })| async move {
|
||||
let stats = state
|
||||
.get_server_version(&id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(stats))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/system_information",
|
||||
get(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Path(ServerId { id })| async move {
|
||||
let stats = state
|
||||
.get_server_system_info(&id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(stats))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/stats",
|
||||
get(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Path(ServerId { id }),
|
||||
query: Query<SystemStatsQuery>| async move {
|
||||
let stats = state
|
||||
.get_server_stats(&id, &user, &query)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(stats))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/stats/history",
|
||||
get(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Path(ServerId { id }),
|
||||
query: Query<HistoricalStatsQuery>| async move {
|
||||
let stats = state
|
||||
.get_historical_stats(&id, &user, &query)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(stats))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/stats/at_ts",
|
||||
get(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Path(ServerId { id }),
|
||||
Query(Ts { ts })| async move {
|
||||
let stats = state
|
||||
.get_stats_at_ts(&id, &user, ts)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(stats))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/stats/ws",
|
||||
get(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Path(ServerId { id }),
|
||||
Query(query): Query<SystemStatsQuery>,
|
||||
ws: WebSocketUpgrade| async move {
|
||||
let connection = state
|
||||
.subscribe_to_stats_ws(&id, &user, &query, ws)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(connection)
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/networks",
|
||||
get(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Path(ServerId { id })| async move {
|
||||
let stats = state
|
||||
.get_networks(&id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(stats))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/networks/prune",
|
||||
post(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Path(ServerId { id })| async move {
|
||||
let stats = spawn_request_action(async move {
|
||||
state
|
||||
.prune_networks(&id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
}).await??;
|
||||
response!(Json(stats))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/images",
|
||||
get(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Path(ServerId { id })| async move {
|
||||
let stats = state
|
||||
.get_images(&id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(stats))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/images/prune",
|
||||
post(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Path(ServerId { id })| async move {
|
||||
let stats = spawn_request_action(async move {
|
||||
state
|
||||
.prune_images(&id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
}).await??;
|
||||
response!(Json(stats))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/containers",
|
||||
get(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Path(ServerId { id })| async move {
|
||||
let stats = state
|
||||
.get_containers(&id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(stats))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/containers/prune",
|
||||
post(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Path(ServerId { id })| async move {
|
||||
let stats = spawn_request_action(async move {
|
||||
state
|
||||
.prune_containers(&id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
}).await??;
|
||||
response!(Json(stats))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/github_accounts",
|
||||
get(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Path(ServerId { id })| async move {
|
||||
let github_accounts = state
|
||||
.get_github_accounts(&id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(github_accounts))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/docker_accounts",
|
||||
get(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Path(ServerId { id })| async move {
|
||||
let docker_accounts = state
|
||||
.get_docker_accounts(&id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(docker_accounts))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/secrets",
|
||||
get(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Path(ServerId { id })| async move {
|
||||
let vars = state
|
||||
.get_available_secrets(&id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(vars))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/:id/action_state",
|
||||
get(
|
||||
|state: StateExtension,
|
||||
user: RequestUserExtension,
|
||||
Path(ServerId { id })| async move {
|
||||
let action_state = state
|
||||
.get_server_action_states(id, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(action_state))
|
||||
},
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub async fn get_server(
|
||||
&self,
|
||||
id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<ServerWithStatus> {
|
||||
let server = self
|
||||
.get_server_check_permissions(id, user, PermissionLevel::Read)
|
||||
.await?;
|
||||
let status = if server.enabled {
|
||||
let res = self.periphery.health_check(&server).await;
|
||||
match res {
|
||||
Ok(_) => ServerStatus::Ok,
|
||||
Err(_) => ServerStatus::NotOk,
|
||||
}
|
||||
} else {
|
||||
ServerStatus::Disabled
|
||||
};
|
||||
Ok(ServerWithStatus { server, status })
|
||||
}
|
||||
|
||||
async fn list_servers(
|
||||
&self,
|
||||
user: &RequestUser,
|
||||
query: impl Into<Option<Document>>,
|
||||
) -> anyhow::Result<Vec<ServerWithStatus>> {
|
||||
let futures = self
|
||||
.db
|
||||
.servers
|
||||
.get_some(query, None)
|
||||
.await
|
||||
.context("failed at get all servers query")?
|
||||
.into_iter()
|
||||
.filter(|s| {
|
||||
if user.is_admin {
|
||||
true
|
||||
} else {
|
||||
let permissions = s.get_user_permissions(&user.id);
|
||||
permissions != PermissionLevel::None
|
||||
}
|
||||
})
|
||||
.map(|server| async {
|
||||
let status = if server.enabled {
|
||||
let res = self.periphery.health_check(&server).await;
|
||||
match res {
|
||||
Ok(_) => ServerStatus::Ok,
|
||||
Err(_) => ServerStatus::NotOk,
|
||||
}
|
||||
} else {
|
||||
ServerStatus::Disabled
|
||||
};
|
||||
|
||||
ServerWithStatus { server, status }
|
||||
});
|
||||
Ok(join_all(futures).await)
|
||||
}
|
||||
|
||||
async fn get_server_version(
|
||||
&self,
|
||||
server_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<String> {
|
||||
let server = self
|
||||
.get_server_check_permissions(server_id, user, PermissionLevel::Read)
|
||||
.await?;
|
||||
let version = self.periphery.get_version(&server).await.context(format!(
|
||||
"failed to get system information from server {}",
|
||||
server.name
|
||||
))?;
|
||||
Ok(version)
|
||||
}
|
||||
|
||||
async fn get_server_system_info(
|
||||
&self,
|
||||
server_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<SystemInformation> {
|
||||
let server = self
|
||||
.get_server_check_permissions(server_id, user, PermissionLevel::Read)
|
||||
.await?;
|
||||
let stats = self
|
||||
.periphery
|
||||
.get_system_information(&server)
|
||||
.await
|
||||
.context(format!(
|
||||
"failed to get system information from server {}",
|
||||
server.name
|
||||
))?;
|
||||
Ok(stats)
|
||||
}
|
||||
|
||||
async fn get_server_stats(
|
||||
&self,
|
||||
server_id: &str,
|
||||
user: &RequestUser,
|
||||
query: &SystemStatsQuery,
|
||||
) -> anyhow::Result<SystemStats> {
|
||||
let server = self
|
||||
.get_server_check_permissions(server_id, user, PermissionLevel::Read)
|
||||
.await?;
|
||||
let stats = self
|
||||
.periphery
|
||||
.get_system_stats(&server, query)
|
||||
.await
|
||||
.context(format!("failed to get stats from server {}", server.name))?;
|
||||
Ok(stats)
|
||||
}
|
||||
|
||||
async fn get_historical_stats(
|
||||
&self,
|
||||
server_id: &str,
|
||||
user: &RequestUser,
|
||||
query: &HistoricalStatsQuery,
|
||||
) -> anyhow::Result<Vec<SystemStatsRecord>> {
|
||||
self.get_server_check_permissions(server_id, user, PermissionLevel::Read)
|
||||
.await?;
|
||||
|
||||
let mut projection = doc! { "processes": 0, "disk.disks": 0 };
|
||||
if !query.networks {
|
||||
projection.insert("networks", 0);
|
||||
}
|
||||
if !query.components {
|
||||
projection.insert("components", 0);
|
||||
}
|
||||
let limit = if query.limit as i64 > MAX_HISTORICAL_STATS_LIMIT {
|
||||
MAX_HISTORICAL_STATS_LIMIT
|
||||
} else {
|
||||
query.limit as i64
|
||||
};
|
||||
let interval = get_timelength_in_ms(query.interval.to_string().parse().unwrap()) as i64;
|
||||
let mut ts_vec = Vec::<i64>::new();
|
||||
let curr_ts = unix_timestamp_ms() as i64;
|
||||
let mut curr_ts = curr_ts - curr_ts % interval - interval * limit * query.page as i64;
|
||||
for _ in 0..limit {
|
||||
ts_vec.push(curr_ts);
|
||||
curr_ts -= interval;
|
||||
}
|
||||
self.db
|
||||
.stats
|
||||
.get_some(
|
||||
doc! {
|
||||
"server_id": server_id,
|
||||
"ts": { "$in": ts_vec }
|
||||
},
|
||||
FindOptions::builder()
|
||||
.sort(doc! { "ts": 1 })
|
||||
.projection(projection)
|
||||
.build(),
|
||||
)
|
||||
.await
|
||||
.context("failed at mongo query to get stats")
|
||||
}
|
||||
|
||||
async fn get_stats_at_ts(
|
||||
&self,
|
||||
server_id: &str,
|
||||
user: &RequestUser,
|
||||
ts: i64,
|
||||
) -> anyhow::Result<SystemStatsRecord> {
|
||||
self.get_server_check_permissions(server_id, user, PermissionLevel::Read)
|
||||
.await?;
|
||||
self.db
|
||||
.stats
|
||||
.find_one(doc! { "server_id": server_id, "ts": ts }, None)
|
||||
.await
|
||||
.context("failed at mongo query to get full stat entry")?
|
||||
.ok_or(anyhow!("did not find entry for server at time"))
|
||||
}
|
||||
|
||||
async fn subscribe_to_stats_ws(
|
||||
&self,
|
||||
server_id: &str,
|
||||
user: &RequestUser,
|
||||
query: &SystemStatsQuery,
|
||||
ws: WebSocketUpgrade,
|
||||
) -> anyhow::Result<impl IntoResponse> {
|
||||
let server = self
|
||||
.get_server_check_permissions(server_id, user, PermissionLevel::Read)
|
||||
.await?;
|
||||
let mut stats_reciever = self.periphery.subscribe_to_stats_ws(&server, query).await?;
|
||||
let upgrade = ws.on_upgrade(|socket| async move {
|
||||
let (mut ws_sender, mut ws_recv) = socket.split();
|
||||
let cancel = CancellationToken::new();
|
||||
let cancel_clone = cancel.clone();
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
let stats = select! {
|
||||
_ = cancel_clone.cancelled() => break,
|
||||
stats = stats_reciever.next() => stats
|
||||
};
|
||||
if let Some(Ok(Message::Text(msg))) = stats {
|
||||
let _ = ws_sender.send(AxumMessage::Text(msg)).await;
|
||||
}
|
||||
}
|
||||
});
|
||||
while let Some(msg) = ws_recv.next().await {
|
||||
match msg {
|
||||
Ok(msg) => match msg {
|
||||
AxumMessage::Close(_) => {
|
||||
cancel.cancel();
|
||||
return;
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Err(_) => {
|
||||
cancel.cancel();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
Ok(upgrade)
|
||||
}
|
||||
|
||||
async fn get_networks(
|
||||
&self,
|
||||
server_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Vec<Network>> {
|
||||
let server = self
|
||||
.get_server_check_permissions(server_id, user, PermissionLevel::Read)
|
||||
.await?;
|
||||
let stats = self.periphery.network_list(&server).await.context(format!(
|
||||
"failed to get networks from server {}",
|
||||
server.name
|
||||
))?;
|
||||
Ok(stats)
|
||||
}
|
||||
|
||||
async fn get_images(
|
||||
&self,
|
||||
server_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Vec<ImageSummary>> {
|
||||
let server = self
|
||||
.get_server_check_permissions(server_id, user, PermissionLevel::Read)
|
||||
.await?;
|
||||
let images = self
|
||||
.periphery
|
||||
.image_list(&server)
|
||||
.await
|
||||
.context(format!("failed to get images from server {}", server.name))?;
|
||||
Ok(images)
|
||||
}
|
||||
|
||||
async fn get_containers(
|
||||
&self,
|
||||
server_id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Vec<BasicContainerInfo>> {
|
||||
let server = self
|
||||
.get_server_check_permissions(server_id, user, PermissionLevel::Read)
|
||||
.await?;
|
||||
let containers = self
|
||||
.periphery
|
||||
.container_list(&server)
|
||||
.await
|
||||
.context(format!(
|
||||
"failed to get containers from server {}",
|
||||
server.name
|
||||
))?;
|
||||
Ok(containers)
|
||||
}
|
||||
|
||||
async fn get_github_accounts(
|
||||
&self,
|
||||
id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Vec<String>> {
|
||||
let server = self
|
||||
.get_server_check_permissions(id, user, PermissionLevel::Read)
|
||||
.await?;
|
||||
let github_accounts = self.periphery.get_github_accounts(&server).await?;
|
||||
Ok(github_accounts)
|
||||
}
|
||||
|
||||
async fn get_docker_accounts(
|
||||
&self,
|
||||
id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Vec<String>> {
|
||||
let server = self
|
||||
.get_server_check_permissions(id, user, PermissionLevel::Read)
|
||||
.await?;
|
||||
let docker_accounts = self.periphery.get_docker_accounts(&server).await?;
|
||||
Ok(docker_accounts)
|
||||
}
|
||||
|
||||
async fn get_available_secrets(
|
||||
&self,
|
||||
id: &str,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Vec<String>> {
|
||||
let server = self
|
||||
.get_server_check_permissions(id, user, PermissionLevel::Read)
|
||||
.await?;
|
||||
let vars = self.periphery.get_available_secrets(&server).await?;
|
||||
Ok(vars)
|
||||
}
|
||||
|
||||
async fn get_server_action_states(
|
||||
&self,
|
||||
id: String,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<ServerActionState> {
|
||||
self.get_server_check_permissions(&id, &user, PermissionLevel::Read)
|
||||
.await?;
|
||||
let action_state = self
|
||||
.server_action_states
|
||||
.lock()
|
||||
.await
|
||||
.entry(id)
|
||||
.or_default()
|
||||
.clone();
|
||||
Ok(action_state)
|
||||
}
|
||||
}
|
||||
@@ -1,219 +0,0 @@
|
||||
use anyhow::{anyhow, Context};
|
||||
use axum::{extract::Query, routing::get, Extension, Json, Router};
|
||||
use helpers::handle_anyhow_error;
|
||||
use mungos::{doc, to_bson};
|
||||
use serde_json::Value;
|
||||
use types::{PermissionLevel, Update, UpdateTarget};
|
||||
|
||||
use crate::{
|
||||
auth::{RequestUser, RequestUserExtension},
|
||||
helpers::parse_comma_seperated_list,
|
||||
response,
|
||||
state::{State, StateExtension},
|
||||
};
|
||||
|
||||
const NUM_UPDATES_PER_PAGE: usize = 20;
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new().route(
|
||||
"/list",
|
||||
get(
|
||||
|Extension(state): StateExtension,
|
||||
Extension(user): RequestUserExtension,
|
||||
Query(value): Query<Value>| async move {
|
||||
let offset = value
|
||||
.get("offset")
|
||||
.map(|v| v.as_str().unwrap_or("0").parse().unwrap_or(0))
|
||||
.unwrap_or(0);
|
||||
let target = serde_json::from_str::<UpdateTarget>(&value.to_string()).ok();
|
||||
let show_builds = value
|
||||
.get("show_builds")
|
||||
.map(|b| {
|
||||
b.as_str()
|
||||
.unwrap_or("false")
|
||||
.parse::<bool>()
|
||||
.unwrap_or_default()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
let operations = value
|
||||
.get("operations")
|
||||
.map(|o| {
|
||||
let o = o.as_str().unwrap_or_default();
|
||||
if o.len() == 0 {
|
||||
return None;
|
||||
}
|
||||
parse_comma_seperated_list::<String>(o).ok()
|
||||
})
|
||||
.flatten();
|
||||
let updates = state
|
||||
.list_updates(target, offset, show_builds, operations, &user)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(Json(updates))
|
||||
},
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
impl State {
|
||||
async fn permission_on_update_target(
|
||||
&self,
|
||||
update_target: &UpdateTarget,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<()> {
|
||||
if user.is_admin {
|
||||
Ok(())
|
||||
} else {
|
||||
match update_target {
|
||||
UpdateTarget::System => {
|
||||
if user.is_admin {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("user must be admin to see system updates"))
|
||||
}
|
||||
}
|
||||
UpdateTarget::Build(id) => self
|
||||
.get_build_check_permissions(id, user, PermissionLevel::Read)
|
||||
.await
|
||||
.map(|_| ()),
|
||||
UpdateTarget::Deployment(id) => self
|
||||
.get_deployment_check_permissions(id, user, PermissionLevel::Read)
|
||||
.await
|
||||
.map(|_| ()),
|
||||
UpdateTarget::Server(id) => self
|
||||
.get_server_check_permissions(id, user, PermissionLevel::Read)
|
||||
.await
|
||||
.map(|_| ()),
|
||||
UpdateTarget::Procedure(id) => self
|
||||
.get_procedure_check_permissions(id, user, PermissionLevel::Read)
|
||||
.await
|
||||
.map(|_| ()),
|
||||
UpdateTarget::Group(id) => self
|
||||
.get_group_check_permissions(id, user, PermissionLevel::Read)
|
||||
.await
|
||||
.map(|_| ()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn list_updates(
|
||||
&self,
|
||||
target: Option<UpdateTarget>,
|
||||
offset: u64,
|
||||
show_builds: bool,
|
||||
operations: Option<Vec<String>>,
|
||||
user: &RequestUser,
|
||||
) -> anyhow::Result<Vec<Update>> {
|
||||
let mut filter = match target {
|
||||
Some(target) => {
|
||||
if let (UpdateTarget::Deployment(id), true) = (&target, show_builds) {
|
||||
let deployment = self
|
||||
.get_deployment_check_permissions(id, user, PermissionLevel::Read)
|
||||
.await?;
|
||||
if let Some(build_id) = &deployment.build_id {
|
||||
let build = self
|
||||
.get_build_check_permissions(build_id, user, PermissionLevel::Read)
|
||||
.await;
|
||||
if let Ok(_) = build {
|
||||
doc! {
|
||||
"$or": [
|
||||
{"target": to_bson(&target).unwrap()},
|
||||
{"target": { "type": "Build", "id": build_id }, "operation": "build_build"}
|
||||
],
|
||||
}
|
||||
} else {
|
||||
doc! {
|
||||
"target": to_bson(&target).unwrap()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
doc! {
|
||||
"target": to_bson(&target).unwrap()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.permission_on_update_target(&target, user).await?;
|
||||
doc! {
|
||||
"target": to_bson(&target).unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
if user.is_admin {
|
||||
doc! {}
|
||||
} else {
|
||||
let permissions_field = format!("permissions.{}", user.id);
|
||||
let target_filter = doc! {
|
||||
"$or": [
|
||||
{ &permissions_field: "update" },
|
||||
{ &permissions_field: "execute" },
|
||||
{ &permissions_field: "read" },
|
||||
]
|
||||
};
|
||||
let build_ids = self
|
||||
.db
|
||||
.builds
|
||||
.get_some(target_filter.clone(), None)
|
||||
.await
|
||||
.context("failed at query to get users builds")?
|
||||
.into_iter()
|
||||
.map(|e| e.id)
|
||||
.collect::<Vec<_>>();
|
||||
let deployment_ids = self
|
||||
.db
|
||||
.deployments
|
||||
.get_some(target_filter.clone(), None)
|
||||
.await
|
||||
.context("failed at query to get users deployments")?
|
||||
.into_iter()
|
||||
.map(|e| e.id)
|
||||
.collect::<Vec<_>>();
|
||||
let server_ids = self
|
||||
.db
|
||||
.servers
|
||||
.get_some(target_filter.clone(), None)
|
||||
.await
|
||||
.context("failed at query to get users servers")?
|
||||
.into_iter()
|
||||
.map(|e| e.id)
|
||||
.collect::<Vec<_>>();
|
||||
let procedure_ids = self
|
||||
.db
|
||||
.procedures
|
||||
.get_some(target_filter, None)
|
||||
.await
|
||||
.context("failed at query to get users procedures")?
|
||||
.into_iter()
|
||||
.map(|e| e.id)
|
||||
.collect::<Vec<_>>();
|
||||
let filter = doc! {
|
||||
"$or": [
|
||||
{ "target.type": "Build", "target.id": { "$in": &build_ids } },
|
||||
{ "target.type": "Deployment", "target.id": { "$in": &deployment_ids } },
|
||||
{ "target.type": "Server", "target.id": { "$in": &server_ids } },
|
||||
{ "target.type": "Procedure", "target.id": { "$in": &procedure_ids } }
|
||||
]
|
||||
};
|
||||
filter
|
||||
}
|
||||
}
|
||||
};
|
||||
if let Some(operations) = operations {
|
||||
filter.insert("operation", doc! { "$in": operations });
|
||||
}
|
||||
let mut updates = self
|
||||
.db
|
||||
.updates
|
||||
.get_most_recent(
|
||||
"start_ts",
|
||||
NUM_UPDATES_PER_PAGE as i64,
|
||||
offset,
|
||||
filter,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.context("mongo get most recent updates query failed")?;
|
||||
updates.reverse();
|
||||
Ok(updates)
|
||||
}
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::{anyhow, Context};
|
||||
use axum::{extract::Query, response::Redirect, routing::get, Extension, Router};
|
||||
use axum_oauth2::github::{GithubOauthClient, GithubOauthExtension};
|
||||
use helpers::handle_anyhow_error;
|
||||
use mungos::{doc, Deserialize};
|
||||
use types::{monitor_timestamp, CoreConfig, User};
|
||||
|
||||
use crate::{response, state::StateExtension};
|
||||
|
||||
use super::JwtExtension;
|
||||
|
||||
pub fn router(config: &CoreConfig) -> Router {
|
||||
let client = GithubOauthClient::new(
|
||||
config.github_oauth.id.clone(),
|
||||
config.github_oauth.secret.clone(),
|
||||
format!("{}/auth/github/callback", config.host),
|
||||
&[],
|
||||
"monitor".to_string(),
|
||||
);
|
||||
Router::new()
|
||||
.route(
|
||||
"/login",
|
||||
get(|Extension(client): GithubOauthExtension| async move {
|
||||
Redirect::to(&client.get_login_redirect_url())
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/callback",
|
||||
get(|client, jwt, state, query| async {
|
||||
let redirect = callback(client, jwt, state, query)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(redirect)
|
||||
}),
|
||||
)
|
||||
.layer(Extension(Arc::new(client)))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct CallbackQuery {
|
||||
state: String,
|
||||
code: String,
|
||||
}
|
||||
|
||||
async fn callback(
|
||||
Extension(client): GithubOauthExtension,
|
||||
Extension(jwt_client): JwtExtension,
|
||||
Extension(state): StateExtension,
|
||||
Query(query): Query<CallbackQuery>,
|
||||
) -> anyhow::Result<Redirect> {
|
||||
if !client.check_state(&query.state) {
|
||||
return Err(anyhow!("state mismatch"));
|
||||
}
|
||||
let token = client.get_access_token(&query.code).await?;
|
||||
let github_user = client.get_github_user(&token.access_token).await?;
|
||||
let github_id = github_user.id.to_string();
|
||||
let user = state
|
||||
.db
|
||||
.users
|
||||
.find_one(doc! { "github_id": &github_id }, None)
|
||||
.await
|
||||
.context("failed at find user query from mongo")?;
|
||||
let jwt = match user {
|
||||
Some(user) => jwt_client
|
||||
.generate(user.id)
|
||||
.context("failed to generate jwt")?,
|
||||
None => {
|
||||
let ts = monitor_timestamp();
|
||||
let user = User {
|
||||
username: github_user.login,
|
||||
avatar: github_user.avatar_url.into(),
|
||||
github_id: github_id.into(),
|
||||
created_at: ts.clone(),
|
||||
updated_at: ts,
|
||||
..Default::default()
|
||||
};
|
||||
let user_id = state
|
||||
.db
|
||||
.users
|
||||
.create_one(user)
|
||||
.await
|
||||
.context("failed to create user on mongo")?;
|
||||
jwt_client
|
||||
.generate(user_id)
|
||||
.context("failed to generate jwt")?
|
||||
}
|
||||
};
|
||||
let exchange_token = jwt_client.create_exchange_token(jwt);
|
||||
Ok(Redirect::to(&format!(
|
||||
"{}?token={exchange_token}",
|
||||
state.config.host
|
||||
)))
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::{anyhow, Context};
|
||||
use axum::{extract::Query, response::Redirect, routing::get, Extension, Router};
|
||||
use axum_oauth2::google::{GoogleOauthClient, GoogleOauthExtension};
|
||||
use helpers::handle_anyhow_error;
|
||||
use mungos::{doc, Deserialize};
|
||||
use types::{monitor_timestamp, CoreConfig, User};
|
||||
|
||||
use crate::{response, state::StateExtension};
|
||||
|
||||
use super::JwtExtension;
|
||||
|
||||
pub fn router(config: &CoreConfig) -> Router {
|
||||
let client = GoogleOauthClient::new(
|
||||
config.google_oauth.id.clone(),
|
||||
config.google_oauth.secret.clone(),
|
||||
format!("{}/auth/google/callback", config.host),
|
||||
&[
|
||||
"https://www.googleapis.com/auth/userinfo.profile",
|
||||
"https://www.googleapis.com/auth/userinfo.email",
|
||||
],
|
||||
"monitor".to_string(),
|
||||
);
|
||||
Router::new()
|
||||
.route(
|
||||
"/login",
|
||||
get(|Extension(client): GoogleOauthExtension| async move {
|
||||
Redirect::to(&client.get_login_redirect_url())
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/callback",
|
||||
get(|client, jwt, state, query| async {
|
||||
let redirect = callback(client, jwt, state, query)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
response!(redirect)
|
||||
}),
|
||||
)
|
||||
.layer(Extension(Arc::new(client)))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct CallbackQuery {
|
||||
state: Option<String>,
|
||||
code: Option<String>,
|
||||
error: Option<String>,
|
||||
}
|
||||
|
||||
async fn callback(
|
||||
Extension(client): GoogleOauthExtension,
|
||||
Extension(jwt_client): JwtExtension,
|
||||
Extension(state): StateExtension,
|
||||
Query(query): Query<CallbackQuery>,
|
||||
) -> anyhow::Result<Redirect> {
|
||||
if let Some(error) = query.error {
|
||||
return Err(anyhow!("auth error from google: {error}"));
|
||||
}
|
||||
if !client.check_state(
|
||||
&query
|
||||
.state
|
||||
.ok_or(anyhow!("callback query does not contain state"))?,
|
||||
) {
|
||||
return Err(anyhow!("state mismatch"));
|
||||
}
|
||||
let token = client
|
||||
.get_access_token(
|
||||
&query
|
||||
.code
|
||||
.ok_or(anyhow!("callback query does not contain code"))?,
|
||||
)
|
||||
.await?;
|
||||
let google_user = client.get_google_user(&token.id_token)?;
|
||||
let google_id = google_user.id.to_string();
|
||||
let user = state
|
||||
.db
|
||||
.users
|
||||
.find_one(doc! { "google_id": &google_id }, None)
|
||||
.await
|
||||
.context("failed at find user query from mongo")?;
|
||||
let jwt = match user {
|
||||
Some(user) => jwt_client
|
||||
.generate(user.id)
|
||||
.context("failed to generate jwt")?,
|
||||
None => {
|
||||
let ts = monitor_timestamp();
|
||||
let user = User {
|
||||
username: google_user
|
||||
.email
|
||||
.split("@")
|
||||
.collect::<Vec<&str>>()
|
||||
.get(0)
|
||||
.unwrap()
|
||||
.to_string(),
|
||||
avatar: google_user.picture.into(),
|
||||
google_id: google_id.into(),
|
||||
created_at: ts.clone(),
|
||||
updated_at: ts,
|
||||
..Default::default()
|
||||
};
|
||||
let user_id = state
|
||||
.db
|
||||
.users
|
||||
.create_one(user)
|
||||
.await
|
||||
.context("failed to create user on mongo")?;
|
||||
jwt_client
|
||||
.generate(user_id)
|
||||
.context("failed to generate jwt")?
|
||||
}
|
||||
};
|
||||
let exchange_token = jwt_client.create_exchange_token(jwt);
|
||||
Ok(Redirect::to(&format!(
|
||||
"{}?token={exchange_token}",
|
||||
state.config.host
|
||||
)))
|
||||
}
|
||||
@@ -1,187 +0,0 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use anyhow::{anyhow, Context};
|
||||
use async_timing_util::{get_timelength_in_ms, unix_timestamp_ms, Timelength};
|
||||
use axum::{body::Body, http::Request, Extension};
|
||||
use axum_oauth2::random_string;
|
||||
use hmac::{Hmac, Mac};
|
||||
use jwt::{SignWithKey, VerifyWithKey};
|
||||
use mungos::{Deserialize, Serialize};
|
||||
use sha2::Sha256;
|
||||
use types::{CoreConfig, User};
|
||||
|
||||
use crate::state::State;
|
||||
|
||||
pub type JwtExtension = Extension<Arc<JwtClient>>;
|
||||
pub type RequestUserExtension = Extension<Arc<RequestUser>>;
|
||||
|
||||
type ExchangeTokenMap = Mutex<HashMap<String, (String, u128)>>;
|
||||
|
||||
pub struct RequestUser {
|
||||
pub id: String,
|
||||
pub is_admin: bool,
|
||||
pub create_server_permissions: bool,
|
||||
pub create_build_permissions: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct JwtClaims {
|
||||
pub id: String,
|
||||
pub iat: u128,
|
||||
pub exp: u128,
|
||||
}
|
||||
|
||||
pub struct JwtClient {
|
||||
key: Hmac<Sha256>,
|
||||
valid_for_ms: u128,
|
||||
exchange_tokens: ExchangeTokenMap,
|
||||
}
|
||||
|
||||
impl JwtClient {
|
||||
pub fn extension(config: &CoreConfig) -> JwtExtension {
|
||||
let key = Hmac::new_from_slice(config.jwt_secret.as_bytes())
|
||||
.expect("failed at taking HmacSha256 of jwt secret");
|
||||
let client = JwtClient {
|
||||
key,
|
||||
valid_for_ms: get_timelength_in_ms(config.jwt_valid_for.to_string().parse().unwrap()),
|
||||
exchange_tokens: Default::default(),
|
||||
};
|
||||
Extension(Arc::new(client))
|
||||
}
|
||||
|
||||
pub fn generate(&self, user_id: String) -> anyhow::Result<String> {
|
||||
let iat = unix_timestamp_ms();
|
||||
let exp = iat + self.valid_for_ms;
|
||||
let claims = JwtClaims {
|
||||
id: user_id,
|
||||
iat,
|
||||
exp,
|
||||
};
|
||||
let jwt = claims
|
||||
.sign_with_key(&self.key)
|
||||
.context("failed at signing claim")?;
|
||||
Ok(jwt)
|
||||
}
|
||||
|
||||
pub async fn authenticate_check_enabled(
|
||||
&self,
|
||||
req: &Request<Body>,
|
||||
) -> anyhow::Result<Arc<RequestUser>> {
|
||||
let jwt = req
|
||||
.headers()
|
||||
.get("authorization")
|
||||
.ok_or(anyhow!(
|
||||
"no authorization header provided. must be Bearer <jwt_token>"
|
||||
))?
|
||||
.to_str()?
|
||||
.replace("Bearer ", "")
|
||||
.replace("bearer ", "");
|
||||
let state = req
|
||||
.extensions()
|
||||
.get::<Arc<State>>()
|
||||
.ok_or(anyhow!("failed at getting state handle"))?;
|
||||
let user = self
|
||||
.auth_jwt_check_enabled(&jwt, &state)
|
||||
.await
|
||||
.context("failed to authenticate jwt")?;
|
||||
Ok(Arc::new(user))
|
||||
}
|
||||
|
||||
pub async fn auth_jwt_check_enabled(
|
||||
&self,
|
||||
jwt: &str,
|
||||
state: &State,
|
||||
) -> anyhow::Result<RequestUser> {
|
||||
let claims: JwtClaims = jwt
|
||||
.verify_with_key(&self.key)
|
||||
.context("failed to verify claims")?;
|
||||
if claims.exp > unix_timestamp_ms() {
|
||||
let user = state
|
||||
.db
|
||||
.users
|
||||
.find_one_by_id(&claims.id)
|
||||
.await?
|
||||
.ok_or(anyhow!("did not find user with id {}", claims.id))?;
|
||||
if user.enabled {
|
||||
let user = RequestUser {
|
||||
id: claims.id,
|
||||
is_admin: user.admin,
|
||||
create_server_permissions: user.create_server_permissions,
|
||||
create_build_permissions: user.create_build_permissions,
|
||||
};
|
||||
Ok(user)
|
||||
} else {
|
||||
Err(anyhow!("user not enabled"))
|
||||
}
|
||||
} else {
|
||||
Err(anyhow!("token has expired"))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn authenticate(&self, req: &Request<Body>) -> anyhow::Result<User> {
|
||||
let jwt = req
|
||||
.headers()
|
||||
.get("authorization")
|
||||
.ok_or(anyhow!(
|
||||
"no authorization header provided. must be Bearer <jwt_token>"
|
||||
))?
|
||||
.to_str()?
|
||||
.replace("Bearer ", "")
|
||||
.replace("bearer ", "");
|
||||
let state = req
|
||||
.extensions()
|
||||
.get::<Arc<State>>()
|
||||
.ok_or(anyhow!("failed at getting state handle"))?;
|
||||
let user = self
|
||||
.auth_jwt(&jwt, &state)
|
||||
.await
|
||||
.context("failed to authenticate jwt")?;
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
pub async fn auth_jwt(&self, jwt: &str, state: &State) -> anyhow::Result<User> {
|
||||
let claims: JwtClaims = jwt
|
||||
.verify_with_key(&self.key)
|
||||
.context("failed to verify claims")?;
|
||||
if claims.exp > unix_timestamp_ms() {
|
||||
let user = state
|
||||
.db
|
||||
.users
|
||||
.find_one_by_id(&claims.id)
|
||||
.await?
|
||||
.ok_or(anyhow!("did not find user with id {}", claims.id))?;
|
||||
Ok(user)
|
||||
} else {
|
||||
Err(anyhow!("token has expired"))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_exchange_token(&self, jwt: String) -> String {
|
||||
let exchange_token = random_string(40);
|
||||
self.exchange_tokens.lock().unwrap().insert(
|
||||
exchange_token.clone(),
|
||||
(
|
||||
jwt,
|
||||
unix_timestamp_ms() + get_timelength_in_ms(Timelength::OneMinute),
|
||||
),
|
||||
);
|
||||
exchange_token
|
||||
}
|
||||
|
||||
pub fn redeem_exchange_token(&self, exchange_token: &str) -> anyhow::Result<String> {
|
||||
let (jwt, valid_until) = self
|
||||
.exchange_tokens
|
||||
.lock()
|
||||
.unwrap()
|
||||
.remove(exchange_token)
|
||||
.ok_or(anyhow!("invalid exchange token: unrecognized"))?;
|
||||
if unix_timestamp_ms() < valid_until {
|
||||
Ok(jwt)
|
||||
} else {
|
||||
Err(anyhow!("invalid exchange token: expired"))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
use anyhow::{anyhow, Context};
|
||||
use axum::{extract::Json, routing::post, Extension, Router};
|
||||
use helpers::handle_anyhow_error;
|
||||
use mungos::doc;
|
||||
use types::{monitor_timestamp, User, UserCredentials};
|
||||
|
||||
use crate::state::StateExtension;
|
||||
|
||||
use super::jwt::JwtExtension;
|
||||
|
||||
const BCRYPT_COST: u32 = 10;
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.route(
|
||||
"/create_user",
|
||||
post(|db, jwt, body| async {
|
||||
create_user_handler(db, jwt, body)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/login",
|
||||
post(|db, jwt, body| async {
|
||||
login_handler(db, jwt, body)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
async fn create_user_handler(
|
||||
Extension(state): StateExtension,
|
||||
Extension(jwt): JwtExtension,
|
||||
Json(UserCredentials { username, password }): Json<UserCredentials>,
|
||||
) -> anyhow::Result<String> {
|
||||
let password = bcrypt::hash(password, BCRYPT_COST).context("failed to hash password")?;
|
||||
|
||||
let no_users_exist = state.db.users.find_one(None, None).await?.is_none();
|
||||
|
||||
let ts = monitor_timestamp();
|
||||
|
||||
let user = User {
|
||||
username,
|
||||
password: Some(password),
|
||||
enabled: no_users_exist,
|
||||
admin: no_users_exist,
|
||||
create_server_permissions: no_users_exist,
|
||||
created_at: ts.clone(),
|
||||
updated_at: ts,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let user_id = state
|
||||
.db
|
||||
.users
|
||||
.create_one(user)
|
||||
.await
|
||||
.context("failed to create user")?;
|
||||
|
||||
let jwt = jwt
|
||||
.generate(user_id)
|
||||
.context("failed to generate jwt for user")?;
|
||||
|
||||
Ok(jwt)
|
||||
}
|
||||
|
||||
async fn login_handler(
|
||||
Extension(state): StateExtension,
|
||||
Extension(jwt): JwtExtension,
|
||||
Json(UserCredentials { username, password }): Json<UserCredentials>,
|
||||
) -> anyhow::Result<String> {
|
||||
let user = state
|
||||
.db
|
||||
.users
|
||||
.find_one(doc! { "username": &username }, None)
|
||||
.await
|
||||
.context("failed at mongo query")?
|
||||
.ok_or(anyhow!("did not find user with username {username}"))?;
|
||||
|
||||
let user_pw_hash = user
|
||||
.password
|
||||
.ok_or(anyhow!("invalid login, user does not have password login"))?;
|
||||
|
||||
let verified = bcrypt::verify(password, &user_pw_hash).context("failed at verify password")?;
|
||||
|
||||
if !verified {
|
||||
return Err(anyhow!("invalid credentials"));
|
||||
}
|
||||
|
||||
let jwt = jwt
|
||||
.generate(user.id)
|
||||
.context("failed at generating jwt for user")?;
|
||||
|
||||
Ok(jwt)
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::{
|
||||
body::Body,
|
||||
http::{Request, StatusCode},
|
||||
middleware::Next,
|
||||
response::Response,
|
||||
routing::{get, post},
|
||||
Extension, Json, Router,
|
||||
};
|
||||
use helpers::handle_anyhow_error;
|
||||
use mungos::{Deserialize, Serialize};
|
||||
use types::CoreConfig;
|
||||
use typeshare::typeshare;
|
||||
|
||||
mod github;
|
||||
mod google;
|
||||
mod jwt;
|
||||
mod local;
|
||||
mod secret;
|
||||
|
||||
use crate::state::StateExtension;
|
||||
|
||||
pub use self::jwt::{JwtClaims, JwtClient, JwtExtension, RequestUser, RequestUserExtension};
|
||||
|
||||
#[typeshare]
|
||||
#[derive(Serialize)]
|
||||
struct LoginOptions {
|
||||
local: bool,
|
||||
github: bool,
|
||||
google: bool,
|
||||
}
|
||||
|
||||
pub fn router(config: &CoreConfig) -> Router {
|
||||
let mut router = Router::new()
|
||||
.route(
|
||||
"/options",
|
||||
get(|Extension(state): StateExtension| async move {
|
||||
Json(LoginOptions {
|
||||
local: state.config.local_auth,
|
||||
github: state.config.github_oauth.enabled
|
||||
&& state.config.github_oauth.id.len() > 0
|
||||
&& state.config.github_oauth.secret.len() > 0,
|
||||
google: state.config.google_oauth.enabled
|
||||
&& state.config.google_oauth.id.len() > 0
|
||||
&& state.config.google_oauth.secret.len() > 0,
|
||||
})
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/exchange",
|
||||
post(|jwt, body| async {
|
||||
exchange_for_jwt(jwt, body)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)
|
||||
}),
|
||||
)
|
||||
.nest("/secret", secret::router());
|
||||
|
||||
if config.local_auth {
|
||||
router = router.nest("/local", local::router());
|
||||
}
|
||||
|
||||
if config.github_oauth.enabled
|
||||
&& config.github_oauth.id.len() > 0
|
||||
&& config.github_oauth.secret.len() > 0
|
||||
{
|
||||
router = router.nest("/github", github::router(config));
|
||||
}
|
||||
|
||||
if config.google_oauth.enabled
|
||||
&& config.google_oauth.id.len() > 0
|
||||
&& config.google_oauth.secret.len() > 0
|
||||
{
|
||||
router = router.nest("/google", google::router(config));
|
||||
}
|
||||
|
||||
router
|
||||
}
|
||||
|
||||
#[typeshare]
|
||||
#[derive(Deserialize)]
|
||||
struct TokenExchangeBody {
|
||||
token: String,
|
||||
}
|
||||
|
||||
async fn exchange_for_jwt(
|
||||
Extension(jwt): JwtExtension,
|
||||
Json(body): Json<TokenExchangeBody>,
|
||||
) -> anyhow::Result<String> {
|
||||
let jwt = jwt.redeem_exchange_token(&body.token)?;
|
||||
Ok(jwt)
|
||||
}
|
||||
|
||||
pub async fn auth_request(
|
||||
mut req: Request<Body>,
|
||||
next: Next<Body>,
|
||||
) -> Result<Response, (StatusCode, String)> {
|
||||
let jwt_client = req.extensions().get::<Arc<JwtClient>>().ok_or((
|
||||
StatusCode::UNAUTHORIZED,
|
||||
"failed to get jwt client extension".to_string(),
|
||||
))?;
|
||||
let user = jwt_client
|
||||
.authenticate_check_enabled(&req)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::UNAUTHORIZED, format!("{e:#?}")))?;
|
||||
req.extensions_mut().insert(user);
|
||||
Ok(next.run(req).await)
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
use anyhow::{anyhow, Context};
|
||||
use async_timing_util::unix_timestamp_ms;
|
||||
use axum::{routing::post, Extension, Json, Router};
|
||||
use helpers::handle_anyhow_error;
|
||||
use mungos::{doc, Deserialize, Document, Update};
|
||||
use types::unix_from_monitor_ts;
|
||||
|
||||
use crate::state::StateExtension;
|
||||
|
||||
use super::JwtExtension;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct SecretLoginBody {
|
||||
username: String,
|
||||
secret: String,
|
||||
}
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new().route(
|
||||
"/login",
|
||||
post(|db, jwt, body| async { login(db, jwt, body).await.map_err(handle_anyhow_error) }),
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn login(
|
||||
Extension(state): StateExtension,
|
||||
Extension(jwt): JwtExtension,
|
||||
Json(SecretLoginBody { username, secret }): Json<SecretLoginBody>,
|
||||
) -> anyhow::Result<String> {
|
||||
let user = state
|
||||
.db
|
||||
.users
|
||||
.find_one(doc! { "username": &username }, None)
|
||||
.await
|
||||
.context("failed at mongo query")?
|
||||
.ok_or(anyhow!("did not find user with username {username}"))?;
|
||||
let ts = unix_timestamp_ms() as i64;
|
||||
for s in user.secrets {
|
||||
if let Some(expires) = s.expires {
|
||||
let expires = unix_from_monitor_ts(&expires)?;
|
||||
if expires < ts {
|
||||
state
|
||||
.db
|
||||
.users
|
||||
.update_one::<Document>(
|
||||
&user.id,
|
||||
Update::Custom(doc! { "$pull": { "secrets": { "name": s.name } } }),
|
||||
)
|
||||
.await
|
||||
.context("failed to remove expired secret")?;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if bcrypt::verify(&secret, &s.hash).context("failed at verifying hash")? {
|
||||
let jwt = jwt
|
||||
.generate(user.id)
|
||||
.context("failed at generating jwt for user")?;
|
||||
return Ok(jwt);
|
||||
}
|
||||
}
|
||||
Err(anyhow!("invalid secret"))
|
||||
}
|
||||
@@ -1,199 +0,0 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{anyhow, Context};
|
||||
use aws_sdk_ec2::{
|
||||
config::Region,
|
||||
types::{
|
||||
BlockDeviceMapping, EbsBlockDevice, InstanceNetworkInterfaceSpecification,
|
||||
InstanceStateChange, InstanceStateName, InstanceStatus, InstanceType, ResourceType, Tag,
|
||||
TagSpecification,
|
||||
},
|
||||
Client,
|
||||
};
|
||||
use types::Server;
|
||||
|
||||
pub async fn create_ec2_client(
|
||||
region: String,
|
||||
access_key_id: &str,
|
||||
secret_access_key: String,
|
||||
) -> Client {
|
||||
// There may be a better way to pass these keys to client
|
||||
std::env::set_var("AWS_ACCESS_KEY_ID", access_key_id);
|
||||
std::env::set_var("AWS_SECRET_ACCESS_KEY", secret_access_key);
|
||||
let region = Region::new(region);
|
||||
let config = aws_config::from_env().region(region).load().await;
|
||||
let client = Client::new(&config);
|
||||
client
|
||||
}
|
||||
|
||||
pub struct Ec2Instance {
|
||||
pub instance_id: String,
|
||||
pub server: Server,
|
||||
}
|
||||
|
||||
const POLL_RATE_SECS: u64 = 2;
|
||||
const MAX_POLL_TRIES: usize = 30;
|
||||
|
||||
/// this will only resolve after the instance is running
|
||||
/// should still poll the periphery agent after creation
|
||||
pub async fn create_instance_with_ami(
|
||||
client: &Client,
|
||||
instance_name: &str,
|
||||
ami_id: &str,
|
||||
instance_type: &str,
|
||||
subnet_id: &str,
|
||||
security_group_ids: Vec<String>,
|
||||
volume_size_gb: i32,
|
||||
key_pair_name: &str,
|
||||
assign_public_ip: bool,
|
||||
) -> anyhow::Result<Ec2Instance> {
|
||||
let instance_type = InstanceType::from(instance_type);
|
||||
if let InstanceType::Unknown(t) = instance_type {
|
||||
return Err(anyhow!("unknown instance type {t:?}"));
|
||||
}
|
||||
let res = client
|
||||
.run_instances()
|
||||
.image_id(ami_id)
|
||||
.instance_type(instance_type)
|
||||
.block_device_mappings(
|
||||
BlockDeviceMapping::builder()
|
||||
.set_device_name(String::from("/dev/sda1").into())
|
||||
.set_ebs(
|
||||
EbsBlockDevice::builder()
|
||||
.volume_size(volume_size_gb)
|
||||
.build()
|
||||
.into(),
|
||||
)
|
||||
.build(),
|
||||
)
|
||||
.network_interfaces(
|
||||
InstanceNetworkInterfaceSpecification::builder()
|
||||
.subnet_id(subnet_id)
|
||||
.associate_public_ip_address(assign_public_ip)
|
||||
.set_groups(security_group_ids.into())
|
||||
.device_index(0)
|
||||
.build(),
|
||||
)
|
||||
.key_name(key_pair_name)
|
||||
.tag_specifications(
|
||||
TagSpecification::builder()
|
||||
.tags(Tag::builder().key("Name").value(instance_name).build())
|
||||
.resource_type(ResourceType::Instance)
|
||||
.build(),
|
||||
)
|
||||
.min_count(1)
|
||||
.max_count(1)
|
||||
.send()
|
||||
.await
|
||||
.context("failed to start builder ec2 instance")?;
|
||||
let instance = res
|
||||
.instances()
|
||||
.ok_or(anyhow!("got None for created instances"))?
|
||||
.get(0)
|
||||
.ok_or(anyhow!("instances array is empty"))?;
|
||||
let instance_id = instance
|
||||
.instance_id()
|
||||
.ok_or(anyhow!("instance does not have instance_id"))?
|
||||
.to_string();
|
||||
for _ in 0..MAX_POLL_TRIES {
|
||||
let state_name = get_ec2_instance_state_name(&client, &instance_id).await?;
|
||||
if state_name == Some(InstanceStateName::Running) {
|
||||
let ip = if assign_public_ip {
|
||||
get_ec2_instance_public_ip(client, &instance_id).await?
|
||||
} else {
|
||||
instance
|
||||
.private_ip_address()
|
||||
.ok_or(anyhow!("instance does not have private ip"))?
|
||||
.to_string()
|
||||
};
|
||||
let server = Server {
|
||||
address: format!("http://{ip}:8000"),
|
||||
..Default::default()
|
||||
};
|
||||
return Ok(Ec2Instance {
|
||||
instance_id,
|
||||
server,
|
||||
});
|
||||
}
|
||||
tokio::time::sleep(Duration::from_secs(POLL_RATE_SECS)).await;
|
||||
}
|
||||
Err(anyhow!("instance not running after polling"))
|
||||
}
|
||||
|
||||
pub async fn get_ec2_instance_status(
|
||||
client: &Client,
|
||||
instance_id: &str,
|
||||
) -> anyhow::Result<Option<InstanceStatus>> {
|
||||
let status = client
|
||||
.describe_instance_status()
|
||||
.instance_ids(instance_id)
|
||||
.send()
|
||||
.await
|
||||
.context("failed to get instance status from aws")?
|
||||
.instance_statuses()
|
||||
.ok_or(anyhow!("instance statuses is None"))?
|
||||
.get(0)
|
||||
.map(|s| s.to_owned());
|
||||
Ok(status)
|
||||
}
|
||||
|
||||
pub async fn get_ec2_instance_state_name(
|
||||
client: &Client,
|
||||
instance_id: &str,
|
||||
) -> anyhow::Result<Option<InstanceStateName>> {
|
||||
let status = get_ec2_instance_status(client, instance_id).await?;
|
||||
if status.is_none() {
|
||||
return Ok(None);
|
||||
}
|
||||
let state = status
|
||||
.unwrap()
|
||||
.instance_state()
|
||||
.ok_or(anyhow!("instance state is None"))?
|
||||
.name()
|
||||
.ok_or(anyhow!("instance state name is None"))?
|
||||
.to_owned();
|
||||
Ok(Some(state))
|
||||
}
|
||||
|
||||
pub async fn get_ec2_instance_public_ip(
|
||||
client: &Client,
|
||||
instance_id: &str,
|
||||
) -> anyhow::Result<String> {
|
||||
let ip = client
|
||||
.describe_instances()
|
||||
.instance_ids(instance_id)
|
||||
.send()
|
||||
.await
|
||||
.context("failed to get instance status from aws")?
|
||||
.reservations()
|
||||
.ok_or(anyhow!("instance reservations is None"))?
|
||||
.get(0)
|
||||
.ok_or(anyhow!("instance reservations is empty"))?
|
||||
.instances()
|
||||
.ok_or(anyhow!("instances is None"))?
|
||||
.get(0)
|
||||
.ok_or(anyhow!("instances is empty"))?
|
||||
.public_ip_address()
|
||||
.ok_or(anyhow!("instance has no public ip"))?
|
||||
.to_string();
|
||||
|
||||
Ok(ip)
|
||||
}
|
||||
|
||||
pub async fn terminate_ec2_instance(
|
||||
client: &Client,
|
||||
instance_id: &str,
|
||||
) -> anyhow::Result<InstanceStateChange> {
|
||||
let res = client
|
||||
.terminate_instances()
|
||||
.instance_ids(instance_id)
|
||||
.send()
|
||||
.await
|
||||
.context("failed to terminate instance from aws")?
|
||||
.terminating_instances()
|
||||
.ok_or(anyhow!("terminating instances is None"))?
|
||||
.get(0)
|
||||
.ok_or(anyhow!("terminating instances is empty"))?
|
||||
.to_owned();
|
||||
Ok(res)
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
pub mod aws;
|
||||
@@ -1,29 +0,0 @@
|
||||
use axum_extra::routing::SpaRouter;
|
||||
use dotenv::dotenv;
|
||||
use merge_config_files::parse_config_file;
|
||||
use mungos::Deserialize;
|
||||
use types::CoreConfig;
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct Env {
|
||||
#[serde(default = "default_config_path")]
|
||||
pub config_path: String,
|
||||
#[serde(default = "default_frontend_path")]
|
||||
pub frontend_path: String,
|
||||
}
|
||||
|
||||
pub fn load() -> (CoreConfig, SpaRouter) {
|
||||
dotenv().ok();
|
||||
let env: Env = envy::from_env().expect("failed to parse environment variables");
|
||||
let config = parse_config_file(env.config_path).expect("failed to parse config");
|
||||
let spa_router = SpaRouter::new("/assets", env.frontend_path);
|
||||
(config, spa_router)
|
||||
}
|
||||
|
||||
pub fn default_config_path() -> String {
|
||||
"/config/config.toml".to_string()
|
||||
}
|
||||
|
||||
fn default_frontend_path() -> String {
|
||||
"/frontend".to_string()
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use diff::{Diff, OptionDiff};
|
||||
use helpers::to_monitor_name;
|
||||
use types::Build;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! response {
|
||||
($x:expr) => {
|
||||
Ok::<_, (axum::http::StatusCode, String)>($x)
|
||||
};
|
||||
}
|
||||
|
||||
pub fn option_diff_is_some<T: Diff>(diff: &OptionDiff<T>) -> bool
|
||||
where
|
||||
<T as Diff>::Repr: PartialEq,
|
||||
{
|
||||
diff != &OptionDiff::NoChange && diff != &OptionDiff::None
|
||||
}
|
||||
|
||||
pub fn any_option_diff_is_some<T: Diff>(diffs: &[&OptionDiff<T>]) -> bool
|
||||
where
|
||||
<T as Diff>::Repr: PartialEq,
|
||||
{
|
||||
for diff in diffs {
|
||||
if diff != &&OptionDiff::NoChange && diff != &&OptionDiff::None {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
pub fn parse_comma_seperated_list<T: FromStr>(comma_sep_list: &str) -> anyhow::Result<Vec<T>> {
|
||||
comma_sep_list
|
||||
.split(",")
|
||||
.filter(|item| item.len() > 0)
|
||||
.map(|item| {
|
||||
let item = item
|
||||
.parse()
|
||||
.map_err(|_| anyhow!("error parsing string {item} into type T"))?;
|
||||
Ok::<T, anyhow::Error>(item)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn get_image_name(build: &Build) -> String {
|
||||
let name = to_monitor_name(&build.name);
|
||||
match &build.docker_organization {
|
||||
Some(org) => format!("{org}/{name}"),
|
||||
None => match &build.docker_account {
|
||||
Some(acct) => format!("{acct}/{name}"),
|
||||
None => name,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn empty_or_only_spaces(word: &str) -> bool {
|
||||
if word.len() == 0 {
|
||||
return true;
|
||||
}
|
||||
for char in word.chars() {
|
||||
if char != ' ' {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
// #![allow(unused)]
|
||||
|
||||
use ::helpers::get_socket_addr;
|
||||
use auth::JwtClient;
|
||||
use axum::{http::StatusCode, Router};
|
||||
use state::State;
|
||||
use tower_http::cors::{Any, CorsLayer};
|
||||
|
||||
mod actions;
|
||||
mod api;
|
||||
mod auth;
|
||||
mod cloud;
|
||||
mod config;
|
||||
mod helpers;
|
||||
mod monitoring;
|
||||
mod state;
|
||||
mod ws;
|
||||
|
||||
type ResponseResult<T> = Result<T, (StatusCode, String)>;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let (config, spa_router) = config::load();
|
||||
|
||||
println!("starting monitor core on port {}...", config.port);
|
||||
|
||||
let app = Router::new()
|
||||
.merge(spa_router)
|
||||
.nest("/api", api::router())
|
||||
.nest("/auth", auth::router(&config))
|
||||
.nest("/ws", ws::router())
|
||||
.layer(JwtClient::extension(&config))
|
||||
.layer(State::extension(config.clone()).await)
|
||||
.layer(
|
||||
CorsLayer::new()
|
||||
.allow_origin(Any)
|
||||
.allow_methods(Any)
|
||||
.allow_headers(Any),
|
||||
);
|
||||
|
||||
println!("started monitor core on port {}", config.port);
|
||||
|
||||
axum::Server::bind(&get_socket_addr(config.port))
|
||||
.serve(app.into_make_service())
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,529 +0,0 @@
|
||||
use std::{cmp::Ordering, collections::HashMap, path::PathBuf};
|
||||
|
||||
use async_timing_util::{
|
||||
unix_timestamp_ms, wait_until_timelength, Timelength, ONE_DAY_MS, ONE_HOUR_MS,
|
||||
};
|
||||
use futures_util::future::join_all;
|
||||
use mungos::doc;
|
||||
use slack::types::Block;
|
||||
use types::{Server, SystemStats, SystemStatsQuery, SystemStatsRecord};
|
||||
|
||||
use crate::state::State;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct AlertStatus {
|
||||
cpu_alert: bool,
|
||||
mem_alert: bool,
|
||||
disk_alert: HashMap<PathBuf, bool>,
|
||||
component_alert: bool,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub async fn collect_server_stats(&self) {
|
||||
loop {
|
||||
let ts = wait_until_timelength(
|
||||
self.config.monitoring_interval.to_string().parse().unwrap(),
|
||||
0,
|
||||
)
|
||||
.await as i64;
|
||||
let servers = self.get_enabled_servers_with_stats().await;
|
||||
if let Err(e) = servers {
|
||||
eprintln!("failed to get server list from db: {e:?}");
|
||||
continue;
|
||||
}
|
||||
for (server, res) in servers.unwrap() {
|
||||
if let Err(_) = res {
|
||||
if let Some(slack) = &self.slack {
|
||||
let (header, info) = generate_unreachable_message(&server);
|
||||
let res = slack.send_message_with_header(&header, info.clone()).await;
|
||||
if let Err(e) = res {
|
||||
eprintln!("failed to send message to slack: {e} | header: {header} | info: {info:?}")
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let stats = res.unwrap();
|
||||
self.check_server_stats(&server, &stats).await;
|
||||
let res = self
|
||||
.db
|
||||
.stats
|
||||
.create_one(SystemStatsRecord::from_stats(server.id, ts, stats))
|
||||
.await;
|
||||
if let Err(e) = res {
|
||||
eprintln!("failed to insert stats into mongo | {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn prune_stats_on_mongo(&self) {
|
||||
let days_ago_ms = self.config.keep_stats_for_days as u128 * ONE_DAY_MS;
|
||||
loop {
|
||||
let ts = wait_until_timelength(Timelength::OneDay, 0).await;
|
||||
let delete_before_ts = ts - days_ago_ms;
|
||||
let res = self
|
||||
.db
|
||||
.stats
|
||||
.delete_many(doc! { "ts": { "$lte": delete_before_ts as i64 } })
|
||||
.await;
|
||||
if let Err(e) = res {
|
||||
eprintln!("{ts} | failed to delete old stats | {e:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_enabled_servers_with_stats(
|
||||
&self,
|
||||
) -> anyhow::Result<Vec<(Server, anyhow::Result<SystemStats>)>> {
|
||||
let servers = self
|
||||
.db
|
||||
.servers
|
||||
.get_some(doc! { "enabled": true }, None)
|
||||
.await?;
|
||||
|
||||
let futures = servers.into_iter().map(|server| async move {
|
||||
let stats = self
|
||||
.periphery
|
||||
.get_system_stats(&server, &SystemStatsQuery::all())
|
||||
.await;
|
||||
(server, stats)
|
||||
});
|
||||
|
||||
Ok(join_all(futures).await)
|
||||
}
|
||||
|
||||
async fn check_server_stats(&self, server: &Server, stats: &SystemStats) {
|
||||
self.check_cpu(server, stats).await;
|
||||
self.check_mem(server, stats).await;
|
||||
self.check_disk(server, stats).await;
|
||||
self.check_components(server, stats).await;
|
||||
}
|
||||
|
||||
async fn check_cpu(&self, server: &Server, stats: &SystemStats) {
|
||||
let server_alert_status = self.server_alert_status.lock().await;
|
||||
if self.slack.is_none()
|
||||
|| server_alert_status
|
||||
.get(&server.id)
|
||||
.map(|s| s.cpu_alert)
|
||||
.unwrap_or(false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
drop(server_alert_status);
|
||||
if stats.cpu_perc > server.cpu_alert {
|
||||
let region = if let Some(region) = &server.region {
|
||||
format!(" ({region})")
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
let mut top_procs = stats.processes.clone();
|
||||
top_procs.sort_by(|a, b| {
|
||||
if a.cpu_perc > b.cpu_perc {
|
||||
Ordering::Less
|
||||
} else {
|
||||
Ordering::Greater
|
||||
}
|
||||
});
|
||||
let top_procs = top_procs
|
||||
.into_iter()
|
||||
.take(3)
|
||||
.enumerate()
|
||||
.map(|(i, p)| {
|
||||
format!(
|
||||
"\n{}. *{}* | *{:.1}%* CPU | *{:.1} GiB* MEM",
|
||||
i + 1,
|
||||
p.name,
|
||||
p.cpu_perc,
|
||||
p.mem_mb / 1024.0,
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("");
|
||||
let mut blocks = vec![
|
||||
Block::header("WARNING 🚨"),
|
||||
Block::section(format!(
|
||||
"*{}*{region} has high *CPU usage* 📈 🚨",
|
||||
server.name
|
||||
)),
|
||||
Block::section(format!("cpu: *{:.1}%*", stats.cpu_perc)),
|
||||
Block::section(format!("*top cpu processes*{top_procs}",)),
|
||||
];
|
||||
|
||||
if let Some(to_notify) = generate_to_notify(server) {
|
||||
blocks.push(Block::section(to_notify))
|
||||
}
|
||||
|
||||
let res = self
|
||||
.slack
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.send_message(
|
||||
format!(
|
||||
"WARNING 🚨 | *{}*{region} has high *CPU usage* 📈 🚨",
|
||||
server.name
|
||||
),
|
||||
blocks,
|
||||
)
|
||||
.await;
|
||||
if let Err(e) = res {
|
||||
eprintln!(
|
||||
"failed to send message to slack | high cpu usage on {} | usage: {:.1}% | {e:?}",
|
||||
server.name, stats.cpu_perc
|
||||
)
|
||||
} else {
|
||||
let mut lock = self.server_alert_status.lock().await;
|
||||
let entry = lock.entry(server.id.clone()).or_default();
|
||||
entry.cpu_alert = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn check_mem(&self, server: &Server, stats: &SystemStats) {
|
||||
let server_alert_status = self.server_alert_status.lock().await;
|
||||
if self.slack.is_none()
|
||||
|| server_alert_status
|
||||
.get(&server.id)
|
||||
.map(|s| s.mem_alert)
|
||||
.unwrap_or(false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
drop(server_alert_status);
|
||||
let usage_perc = (stats.mem_used_gb / stats.mem_total_gb) * 100.0;
|
||||
if usage_perc > server.mem_alert {
|
||||
let region = if let Some(region) = &server.region {
|
||||
format!(" ({region})")
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
let mut top_procs = stats.processes.clone();
|
||||
top_procs.sort_by(|a, b| {
|
||||
if a.mem_mb > b.mem_mb {
|
||||
Ordering::Less
|
||||
} else {
|
||||
Ordering::Greater
|
||||
}
|
||||
});
|
||||
let top_procs = top_procs
|
||||
.into_iter()
|
||||
.take(3)
|
||||
.enumerate()
|
||||
.map(|(i, p)| {
|
||||
format!(
|
||||
"\n{}. *{}* | *{:.1}%* CPU | *{:.1} GiB* MEM",
|
||||
i + 1,
|
||||
p.name,
|
||||
p.cpu_perc,
|
||||
p.mem_mb / 1024.0,
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("");
|
||||
let mut blocks = vec![
|
||||
Block::header("WARNING 🚨"),
|
||||
Block::section(format!(
|
||||
"*{}*{region} has high *memory usage* 💾 🚨",
|
||||
server.name
|
||||
)),
|
||||
Block::section(format!(
|
||||
"memory: used *{:.2} GB* of *{:.2} GB* (*{:.1}%*)",
|
||||
stats.mem_used_gb, stats.mem_total_gb, usage_perc
|
||||
)),
|
||||
Block::section(format!("*top mem processes*{top_procs}",)),
|
||||
];
|
||||
|
||||
if let Some(to_notify) = generate_to_notify(server) {
|
||||
blocks.push(Block::section(to_notify))
|
||||
}
|
||||
|
||||
let res = self
|
||||
.slack
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.send_message(
|
||||
format!(
|
||||
"WARNING 🚨 | *{}*{region} has high *memory usage* 💾 🚨",
|
||||
server.name
|
||||
),
|
||||
blocks,
|
||||
)
|
||||
.await;
|
||||
if let Err(e) = res {
|
||||
eprintln!(
|
||||
"failed to send message to slack | high mem usage on {} | usage: {:.2}GB of {:.2}GB | {e:?}",
|
||||
server.name, stats.mem_used_gb, stats.mem_total_gb,
|
||||
)
|
||||
} else {
|
||||
let mut lock = self.server_alert_status.lock().await;
|
||||
let entry = lock.entry(server.id.clone()).or_default();
|
||||
entry.mem_alert = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn check_disk(&self, server: &Server, stats: &SystemStats) {
|
||||
for disk in &stats.disk.disks {
|
||||
let server_alert_status = self.server_alert_status.lock().await;
|
||||
if self.slack.is_none()
|
||||
|| server_alert_status
|
||||
.get(&server.id)
|
||||
.map(|s| *s.disk_alert.get(&disk.mount).unwrap_or(&false))
|
||||
.unwrap_or(false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
drop(server_alert_status);
|
||||
let usage_perc = (disk.used_gb / disk.total_gb) * 100.0;
|
||||
if usage_perc > server.disk_alert {
|
||||
let region = if let Some(region) = &server.region {
|
||||
format!(" ({region})")
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
let mut blocks = vec![
|
||||
Block::header("WARNING 🚨"),
|
||||
Block::section(format!(
|
||||
"*{}*{region} has high *disk usage* (mount point *{}*) 💿 🚨",
|
||||
server.name,
|
||||
disk.mount.display()
|
||||
)),
|
||||
Block::section(format!(
|
||||
"disk: used *{:.2} GB* of *{:.2} GB* (*{:.1}%*)",
|
||||
disk.used_gb, disk.total_gb, usage_perc
|
||||
)),
|
||||
];
|
||||
|
||||
if let Some(to_notify) = generate_to_notify(server) {
|
||||
blocks.push(Block::section(to_notify))
|
||||
}
|
||||
|
||||
let res = self
|
||||
.slack
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.send_message(
|
||||
format!(
|
||||
"WARNING 🚨 | *{}*{region} has high *disk usage* 💿 🚨",
|
||||
server.name
|
||||
),
|
||||
blocks,
|
||||
)
|
||||
.await;
|
||||
if let Err(e) = res {
|
||||
eprintln!(
|
||||
"failed to send message to slack | high disk usage on {} | usage: {:.2}GB of {:.2}GB | {e:?}",
|
||||
server.name, stats.disk.used_gb, stats.disk.total_gb,
|
||||
)
|
||||
} else {
|
||||
let mut lock = self.server_alert_status.lock().await;
|
||||
let entry = lock.entry(server.id.clone()).or_default();
|
||||
entry.disk_alert.insert(disk.mount.clone(), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn check_components(&self, server: &Server, stats: &SystemStats) {
|
||||
let lock = self.server_alert_status.lock().await;
|
||||
if self.slack.is_none()
|
||||
|| lock
|
||||
.get(&server.id)
|
||||
.map(|s| s.component_alert)
|
||||
.unwrap_or(false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
drop(lock);
|
||||
let info = stats
|
||||
.components
|
||||
.iter()
|
||||
.map(|c| {
|
||||
if let Some(critical) = c.critical {
|
||||
if c.temp / critical > 0.85 {
|
||||
format!(
|
||||
"{}: *{:.1}°* (*{:.1}%* to critical) 🌡️",
|
||||
c.label,
|
||||
c.temp,
|
||||
(c.temp / critical) * 100.0
|
||||
)
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
})
|
||||
.filter(|s| !s.is_empty())
|
||||
.collect::<Vec<_>>();
|
||||
if info.len() > 0 {
|
||||
let region = if let Some(region) = &server.region {
|
||||
format!(" ({region})")
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
let mut blocks = vec![
|
||||
Block::header("WARNING 🚨"),
|
||||
Block::section(format!(
|
||||
"*{}*{region} has high *tempurature* 🌡️ 🚨",
|
||||
server.name
|
||||
)),
|
||||
Block::section(info.join("\n")),
|
||||
];
|
||||
|
||||
if let Some(to_notify) = generate_to_notify(server) {
|
||||
blocks.push(Block::section(to_notify))
|
||||
}
|
||||
|
||||
let res = self
|
||||
.slack
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.send_message(
|
||||
format!(
|
||||
"WARNING 🚨 | *{}*{region} has high *tempurature* 🌡️ 🚨",
|
||||
server.name
|
||||
),
|
||||
blocks,
|
||||
)
|
||||
.await;
|
||||
if let Err(e) = res {
|
||||
eprintln!(
|
||||
"failed to send message to slack | high tempurature on {} | {} | {e:?}",
|
||||
server.name,
|
||||
info.join(" | "),
|
||||
)
|
||||
} else {
|
||||
let mut lock = self.server_alert_status.lock().await;
|
||||
let entry = lock.entry(server.id.clone()).or_default();
|
||||
entry.component_alert = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn daily_update(&self) {
|
||||
let offset = self.config.daily_offset_hours as u128 * ONE_HOUR_MS;
|
||||
loop {
|
||||
wait_until_timelength(Timelength::OneDay, offset).await;
|
||||
let servers = self.get_enabled_servers_with_stats().await;
|
||||
if let Err(e) = &servers {
|
||||
eprintln!(
|
||||
"{} | failed to get servers with stats for daily update | {e:#?}",
|
||||
unix_timestamp_ms()
|
||||
);
|
||||
continue;
|
||||
}
|
||||
let servers = servers.unwrap();
|
||||
if servers.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let mut blocks = vec![Block::header("INFO | daily update"), Block::divider()];
|
||||
for (server, stats) in servers {
|
||||
let region = if let Some(region) = &server.region {
|
||||
format!(" | {region}")
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
if let Ok(stats) = stats {
|
||||
let cpu_warning = if stats.cpu_perc > server.cpu_alert {
|
||||
" 🚨"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let mem_warning =
|
||||
if (stats.mem_used_gb / stats.mem_total_gb) * 100.0 > server.mem_alert {
|
||||
" 🚨"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let disk_warning =
|
||||
if (stats.disk.used_gb / stats.disk.total_gb) * 100.0 > server.disk_alert {
|
||||
" 🚨"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let status = if !cpu_warning.is_empty()
|
||||
|| !mem_warning.is_empty()
|
||||
|| !disk_warning.is_empty()
|
||||
{
|
||||
"*WARNING* 🚨"
|
||||
} else {
|
||||
"*OK* ✅"
|
||||
};
|
||||
let name_line = format!("*{}*{region} | {status}", server.name);
|
||||
let cpu_line = format!("CPU: *{:.1}%*{cpu_warning}", stats.cpu_perc);
|
||||
let mem_line = format!(
|
||||
"MEM: *{:.1}%* ({:.2} GB of {:.2} GB){mem_warning}",
|
||||
(stats.mem_used_gb / stats.mem_total_gb) * 100.0,
|
||||
stats.mem_used_gb,
|
||||
stats.mem_total_gb,
|
||||
);
|
||||
let disk_line = format!(
|
||||
"DISK: *{:.1}%* ({:.2} GB of {:.2} GB){disk_warning}",
|
||||
(stats.disk.used_gb / stats.disk.total_gb) * 100.0,
|
||||
stats.disk.used_gb,
|
||||
stats.disk.total_gb,
|
||||
);
|
||||
blocks.push(Block::section(format!(
|
||||
"{name_line}\n{cpu_line}\n{mem_line}\n{disk_line}",
|
||||
)));
|
||||
} else {
|
||||
blocks.push(Block::section(format!(
|
||||
"*{}*{region} | *UNREACHABLE* ❌",
|
||||
server.name
|
||||
)));
|
||||
}
|
||||
blocks.push(Block::divider())
|
||||
}
|
||||
let res = self
|
||||
.slack
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.send_message(format!("INFO | daily update"), blocks)
|
||||
.await;
|
||||
if let Err(e) = res {
|
||||
eprintln!(
|
||||
"{} | failed to send daily update message | {e:?}",
|
||||
unix_timestamp_ms()
|
||||
);
|
||||
}
|
||||
{
|
||||
self.server_alert_status.lock().await.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_unreachable_message(server: &Server) -> (String, Option<String>) {
|
||||
let region = match &server.region {
|
||||
Some(region) => format!(" ({region})"),
|
||||
None => String::new(),
|
||||
};
|
||||
let header = format!("WARNING 🚨 | {}{region} is unreachable ❌", server.name);
|
||||
let to_notify = server
|
||||
.to_notify
|
||||
.iter()
|
||||
.map(|u| format!("<@{u}>"))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ");
|
||||
let info = if to_notify.len() > 0 {
|
||||
Some(to_notify)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
(header, info)
|
||||
}
|
||||
|
||||
fn generate_to_notify(server: &Server) -> Option<String> {
|
||||
if server.to_notify.len() > 0 {
|
||||
Some(
|
||||
server
|
||||
.to_notify
|
||||
.iter()
|
||||
.map(|u| format!("<@{u}>"))
|
||||
.collect::<Vec<String>>()
|
||||
.join(" "),
|
||||
)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use async_timing_util::{unix_timestamp_ms, wait_until_timelength, Timelength, ONE_HOUR_MS};
|
||||
use axum::Extension;
|
||||
use db::DbClient;
|
||||
use futures_util::future::join_all;
|
||||
use mungos::doc;
|
||||
use periphery::PeripheryClient;
|
||||
use tokio::sync::Mutex;
|
||||
use types::{BuildActionState, CoreConfig, DeploymentActionState, ServerActionState};
|
||||
|
||||
use crate::{monitoring::AlertStatus, ws::update::UpdateWsChannel};
|
||||
|
||||
pub type StateExtension = Extension<Arc<State>>;
|
||||
|
||||
pub type ActionStateMap<T> = Mutex<HashMap<String, T>>;
|
||||
|
||||
pub struct State {
|
||||
pub config: CoreConfig,
|
||||
pub db: DbClient,
|
||||
pub update: UpdateWsChannel,
|
||||
pub periphery: PeripheryClient,
|
||||
pub slack: Option<slack::Client>,
|
||||
pub build_action_states: ActionStateMap<BuildActionState>,
|
||||
pub deployment_action_states: ActionStateMap<DeploymentActionState>,
|
||||
pub server_action_states: ActionStateMap<ServerActionState>,
|
||||
pub server_alert_status: Mutex<HashMap<String, AlertStatus>>, // (server_id, AlertStatus)
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub async fn new(config: CoreConfig) -> Arc<State> {
|
||||
let state = State {
|
||||
db: DbClient::new(config.mongo.clone()).await,
|
||||
slack: config.slack_url.clone().map(|url| slack::Client::new(&url)),
|
||||
periphery: PeripheryClient::new(config.passkey.clone()),
|
||||
config,
|
||||
update: UpdateWsChannel::new(),
|
||||
build_action_states: Default::default(),
|
||||
deployment_action_states: Default::default(),
|
||||
server_action_states: Default::default(),
|
||||
server_alert_status: Default::default(),
|
||||
};
|
||||
let state = Arc::new(state);
|
||||
let state_clone = state.clone();
|
||||
tokio::spawn(async move { state_clone.collect_server_stats().await });
|
||||
let state_clone = state.clone();
|
||||
tokio::spawn(async move { state_clone.daily_image_prune().await });
|
||||
if state.slack.is_some() {
|
||||
let state_clone = state.clone();
|
||||
tokio::spawn(async move { state_clone.daily_update().await });
|
||||
}
|
||||
if state.config.keep_stats_for_days != 0 {
|
||||
let state_clone = state.clone();
|
||||
tokio::spawn(async move { state_clone.prune_stats_on_mongo().await });
|
||||
}
|
||||
state
|
||||
}
|
||||
|
||||
pub async fn extension(config: CoreConfig) -> StateExtension {
|
||||
Extension(State::new(config).await)
|
||||
}
|
||||
|
||||
async fn daily_image_prune(&self) {
|
||||
let offset = self.config.daily_offset_hours as u128 * ONE_HOUR_MS;
|
||||
loop {
|
||||
wait_until_timelength(Timelength::OneDay, offset).await;
|
||||
let servers = self
|
||||
.db
|
||||
.servers
|
||||
.get_some(doc! { "enabled": true, "auto_prune": true }, None)
|
||||
.await;
|
||||
if let Err(e) = &servers {
|
||||
eprintln!(
|
||||
"{} | failed to get servers for daily prune | {e:#?}",
|
||||
unix_timestamp_ms()
|
||||
);
|
||||
continue;
|
||||
}
|
||||
let futures = servers.unwrap().into_iter().map(|server| async move {
|
||||
let _ = self.periphery.image_prune(&server).await;
|
||||
});
|
||||
join_all(futures).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
use axum::{
|
||||
extract::ws::{Message, WebSocket},
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
auth::{JwtClient, RequestUser},
|
||||
state::State,
|
||||
};
|
||||
|
||||
mod stats;
|
||||
pub mod update;
|
||||
|
||||
pub fn router() -> Router {
|
||||
Router::new()
|
||||
.route("/update", get(update::ws_handler))
|
||||
.route("/stats/:id", get(stats::ws_handler))
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub async fn ws_login(
|
||||
&self,
|
||||
mut socket: WebSocket,
|
||||
jwt_client: &JwtClient,
|
||||
) -> Option<(WebSocket, RequestUser)> {
|
||||
if let Some(jwt) = socket.recv().await {
|
||||
match jwt {
|
||||
Ok(jwt) => match jwt {
|
||||
Message::Text(jwt) => {
|
||||
match jwt_client.auth_jwt_check_enabled(&jwt, self).await {
|
||||
Ok(user) => {
|
||||
let _ = socket.send(Message::Text("LOGGED_IN".to_string())).await;
|
||||
Some((socket, user))
|
||||
}
|
||||
Err(e) => {
|
||||
let _ = socket
|
||||
.send(Message::Text(format!(
|
||||
"failed to authenticate user | {e:#?}"
|
||||
)))
|
||||
.await;
|
||||
let _ = socket.close().await;
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
msg => {
|
||||
let _ = socket
|
||||
.send(Message::Text(format!("invalid login msg: {msg:#?}")))
|
||||
.await;
|
||||
let _ = socket.close().await;
|
||||
None
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
let _ = socket
|
||||
.send(Message::Text(format!("failed to get jwt message: {e:#?}")))
|
||||
.await;
|
||||
let _ = socket.close().await;
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let _ = socket
|
||||
.send(Message::Text(format!("failed to get jwt message")))
|
||||
.await;
|
||||
let _ = socket.close().await;
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::{
|
||||
extract::{ws::Message as AxumMessage, Path, Query, WebSocketUpgrade},
|
||||
response::IntoResponse,
|
||||
};
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use helpers::handle_anyhow_error;
|
||||
use mungos::Deserialize;
|
||||
use tokio::select;
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use types::{traits::Permissioned, PermissionLevel, SystemStatsQuery};
|
||||
|
||||
use crate::{auth::JwtExtension, state::StateExtension, ResponseResult};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct ServerId {
|
||||
id: String,
|
||||
}
|
||||
|
||||
pub async fn ws_handler(
|
||||
state: StateExtension,
|
||||
jwt_client: JwtExtension,
|
||||
path: Path<ServerId>,
|
||||
query: Query<SystemStatsQuery>,
|
||||
ws: WebSocketUpgrade,
|
||||
) -> ResponseResult<impl IntoResponse> {
|
||||
let server = state
|
||||
.db
|
||||
.get_server(&path.id)
|
||||
.await
|
||||
.map_err(handle_anyhow_error)?;
|
||||
let query = Arc::new(query);
|
||||
let upgrade = ws.on_upgrade(|socket| async move {
|
||||
let login_res = state.ws_login(socket, &jwt_client).await;
|
||||
if login_res.is_none() {
|
||||
return;
|
||||
}
|
||||
let (mut socket, user) = login_res.unwrap();
|
||||
if !user.is_admin && server.get_user_permissions(&user.id) < PermissionLevel::Read {
|
||||
let _ = socket
|
||||
.send(AxumMessage::Text(
|
||||
"permission denied. user must have at least read permissions on this server"
|
||||
.to_string(),
|
||||
))
|
||||
.await;
|
||||
return;
|
||||
}
|
||||
let (mut ws_sender, mut ws_reciever) = socket.split();
|
||||
let res = state.periphery.subscribe_to_stats_ws(&server, &query).await;
|
||||
if let Err(e) = &res {
|
||||
let _ = ws_sender
|
||||
.send(AxumMessage::Text(format!("ERROR: {e}")))
|
||||
.await;
|
||||
return;
|
||||
}
|
||||
let mut stats_recv = res.unwrap();
|
||||
let cancel = CancellationToken::new();
|
||||
let cancel_clone = cancel.clone();
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
let stats = select! {
|
||||
_ = cancel_clone.cancelled() => {
|
||||
let _ = stats_recv.close(None).await;
|
||||
break
|
||||
},
|
||||
stats = stats_recv.next() => stats,
|
||||
};
|
||||
if let Some(Ok(Message::Text(msg))) = stats {
|
||||
let _ = ws_sender.send(AxumMessage::Text(msg)).await;
|
||||
} else {
|
||||
let _ = stats_recv.close(None).await;
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
while let Some(msg) = ws_reciever.next().await {
|
||||
match msg {
|
||||
Ok(msg) => match msg {
|
||||
AxumMessage::Close(_) => {
|
||||
// println!("CLOSE");
|
||||
cancel.cancel();
|
||||
return;
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Err(_) => {
|
||||
// println!("CLOSE FROM ERR");
|
||||
cancel.cancel();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
Ok(upgrade)
|
||||
}
|
||||
@@ -1,152 +0,0 @@
|
||||
use anyhow::anyhow;
|
||||
use axum::{
|
||||
extract::{ws::Message, WebSocketUpgrade},
|
||||
response::IntoResponse,
|
||||
};
|
||||
use db::DbClient;
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use serde_json::json;
|
||||
use tokio::{
|
||||
select,
|
||||
sync::{
|
||||
broadcast::{self, Receiver, Sender},
|
||||
Mutex,
|
||||
},
|
||||
};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use types::{PermissionLevel, Update, UpdateTarget, User};
|
||||
|
||||
use crate::{auth::JwtExtension, state::StateExtension};
|
||||
|
||||
pub type UpdateWsSender = Mutex<Sender<Update>>;
|
||||
|
||||
pub type UpdateWsReciever = Receiver<Update>;
|
||||
pub struct UpdateWsChannel {
|
||||
pub sender: UpdateWsSender,
|
||||
pub reciever: UpdateWsReciever,
|
||||
}
|
||||
|
||||
impl UpdateWsChannel {
|
||||
pub fn new() -> UpdateWsChannel {
|
||||
let (sender, reciever) = broadcast::channel(16);
|
||||
UpdateWsChannel {
|
||||
sender: Mutex::new(sender),
|
||||
reciever,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn ws_handler(
|
||||
jwt_client: JwtExtension,
|
||||
state: StateExtension,
|
||||
ws: WebSocketUpgrade,
|
||||
) -> impl IntoResponse {
|
||||
let mut reciever = state.update.reciever.resubscribe();
|
||||
ws.on_upgrade(|socket| async move {
|
||||
let login_res = state.ws_login(socket, &jwt_client).await;
|
||||
if login_res.is_none() {
|
||||
return;
|
||||
}
|
||||
let (socket, user) = login_res.unwrap();
|
||||
let (mut ws_sender, mut ws_reciever) = socket.split();
|
||||
let cancel = CancellationToken::new();
|
||||
let cancel_clone = cancel.clone();
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
let update = select! {
|
||||
_ = cancel_clone.cancelled() => break,
|
||||
update = reciever.recv() => {update.expect("failed to recv update msg")}
|
||||
};
|
||||
let user = state.db.users.find_one_by_id(&user.id).await;
|
||||
if user.is_err()
|
||||
|| user.as_ref().unwrap().is_none()
|
||||
|| !user.as_ref().unwrap().as_ref().unwrap().enabled
|
||||
{
|
||||
let _ = ws_sender
|
||||
.send(Message::Text(json!({ "type": "INVALID_USER" }).to_string()))
|
||||
.await;
|
||||
let _ = ws_sender.close().await;
|
||||
return;
|
||||
}
|
||||
let user = user.unwrap().unwrap(); // already handle cases where this panics in the above early return
|
||||
match user_can_see_update(&user, &user.id, &update.target, &state.db).await {
|
||||
Ok(_) => {
|
||||
let _ = ws_sender
|
||||
.send(Message::Text(serde_json::to_string(&update).unwrap()))
|
||||
.await;
|
||||
}
|
||||
Err(_) => {
|
||||
// make these error visible in some way
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
while let Some(msg) = ws_reciever.next().await {
|
||||
match msg {
|
||||
Ok(msg) => match msg {
|
||||
Message::Close(_) => {
|
||||
cancel.cancel();
|
||||
return;
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Err(_) => {
|
||||
cancel.cancel();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async fn user_can_see_update(
|
||||
user: &User,
|
||||
user_id: &str,
|
||||
update_target: &UpdateTarget,
|
||||
db_client: &DbClient,
|
||||
) -> anyhow::Result<()> {
|
||||
if user.admin {
|
||||
return Ok(());
|
||||
}
|
||||
let (permissions, target) = match update_target {
|
||||
UpdateTarget::Server(server_id) => {
|
||||
let permissions = db_client
|
||||
.get_user_permission_on_server(user_id, server_id)
|
||||
.await?;
|
||||
(permissions, "server")
|
||||
}
|
||||
UpdateTarget::Deployment(deployment_id) => {
|
||||
let permissions = db_client
|
||||
.get_user_permission_on_deployment(user_id, deployment_id)
|
||||
.await?;
|
||||
(permissions, "deployment")
|
||||
}
|
||||
UpdateTarget::Build(build_id) => {
|
||||
let permissions = db_client
|
||||
.get_user_permission_on_build(user_id, build_id)
|
||||
.await?;
|
||||
(permissions, "build")
|
||||
}
|
||||
UpdateTarget::Procedure(procedure_id) => {
|
||||
let permissions = db_client
|
||||
.get_user_permission_on_procedure(user_id, procedure_id)
|
||||
.await?;
|
||||
(permissions, "procedure")
|
||||
}
|
||||
UpdateTarget::Group(group_id) => {
|
||||
let permissions = db_client
|
||||
.get_user_permission_on_group(user_id, group_id)
|
||||
.await?;
|
||||
(permissions, "group")
|
||||
}
|
||||
UpdateTarget::System => {
|
||||
return Err(anyhow!("user not admin, can't recieve system updates"))
|
||||
}
|
||||
};
|
||||
if permissions != PermissionLevel::None {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("user does not have permissions on {target}"))
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user