Compare commits
261 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9907c12eda | ||
|
|
19533a32b5 | ||
|
|
c5a5004f9e | ||
|
|
677cdea99d | ||
|
|
4d7c0ddbce | ||
|
|
81daf10157 | ||
|
|
b3ef4e41bf | ||
|
|
9fbf149717 | ||
|
|
95cb94a039 | ||
|
|
21f7f87716 | ||
|
|
831c7e2c32 | ||
|
|
cc0d04c8b7 | ||
|
|
46be83f8f7 | ||
|
|
28560e2045 | ||
|
|
0df4824a56 | ||
|
|
dbcabc6517 | ||
|
|
69f479b67e | ||
|
|
af75696018 | ||
|
|
80b8f8740f | ||
|
|
71ab325940 | ||
|
|
653c76709a | ||
|
|
83cc1bab38 | ||
|
|
6c8588c019 | ||
|
|
5b00ed2fb2 | ||
|
|
9f66962bfb | ||
|
|
0edba74091 | ||
|
|
1003b49dd9 | ||
|
|
884ba54f96 | ||
|
|
cf2325a2da | ||
|
|
db6972638d | ||
|
|
74e04e81d5 | ||
|
|
7c5d7365c7 | ||
|
|
0dadf3d78a | ||
|
|
e341256627 | ||
|
|
5a3bd3ca67 | ||
|
|
8102e0a468 | ||
|
|
7d55179727 | ||
|
|
bc1a1d1818 | ||
|
|
a8bbb22fe8 | ||
|
|
6b489f71a1 | ||
|
|
f1db088af4 | ||
|
|
6fe12b3fb5 | ||
|
|
dacbf9b68d | ||
|
|
9f5057eac7 | ||
|
|
525cd54921 | ||
|
|
7ac94bbf5f | ||
|
|
b8ff6938df | ||
|
|
2f6c77fba2 | ||
|
|
28a6430778 | ||
|
|
6e4157da35 | ||
|
|
4f420dde05 | ||
|
|
d9601471df | ||
|
|
9941a97e37 | ||
|
|
0a64b08669 | ||
|
|
4d9d0d4548 | ||
|
|
5f6c8545c6 | ||
|
|
ddc335d65a | ||
|
|
9cbaa892d3 | ||
|
|
9531465410 | ||
|
|
c35916fad1 | ||
|
|
bf476a058e | ||
|
|
d4e815a4cb | ||
|
|
0545c4167b | ||
|
|
6838dd02c0 | ||
|
|
14c2fd1edd | ||
|
|
6e503cc79b | ||
|
|
bd4563b699 | ||
|
|
458e115490 | ||
|
|
51369adad1 | ||
|
|
f65c5fb147 | ||
|
|
4150ae7307 | ||
|
|
a87288d519 | ||
|
|
3cf9639e99 | ||
|
|
4490c3ed1a | ||
|
|
fbcb562781 | ||
|
|
b1e035f96a | ||
|
|
11c3a26c23 | ||
|
|
1fbe72b52d | ||
|
|
f4bb066737 | ||
|
|
aaac9cbeeb | ||
|
|
0e68ff6923 | ||
|
|
1c59712cbf | ||
|
|
c2cb1c9168 | ||
|
|
cc8e2e40dd | ||
|
|
e67d97d9da | ||
|
|
d74c2115fd | ||
|
|
70e7ee2d46 | ||
|
|
d11854f4e8 | ||
|
|
4bb553e015 | ||
|
|
0af9af44e5 | ||
|
|
3a0d73f740 | ||
|
|
9b9ff2622d | ||
|
|
a4858be967 | ||
|
|
6fd5623b1f | ||
|
|
66d9c7091c | ||
|
|
525a1e8140 | ||
|
|
64dc47d7e9 | ||
|
|
f3fc7bb91e | ||
|
|
028ef14cc0 | ||
|
|
3e001f9a1c | ||
|
|
33d20ac6d8 | ||
|
|
660554cc45 | ||
|
|
a455324e8c | ||
|
|
cd5e2e1148 | ||
|
|
074da4da19 | ||
|
|
e4e39d820c | ||
|
|
e5dbb214a2 | ||
|
|
91af528ff8 | ||
|
|
18c4e39ea3 | ||
|
|
bda455ce78 | ||
|
|
a07aea1ad3 | ||
|
|
18e2dbf144 | ||
|
|
564a07e62e | ||
|
|
a358135e41 | ||
|
|
6d9be15035 | ||
|
|
b740e0b78a | ||
|
|
9546949945 | ||
|
|
8ff048d055 | ||
|
|
95a1c6e7fb | ||
|
|
0b1a4a0f30 | ||
|
|
22b48e296a | ||
|
|
c696ebf53c | ||
|
|
a0686b7d2b | ||
|
|
8d94be8924 | ||
|
|
e97ac5033f | ||
|
|
44771a0049 | ||
|
|
32aae8f57a | ||
|
|
8207e23cd9 | ||
|
|
a469029698 | ||
|
|
203d866643 | ||
|
|
1488e5ec4d | ||
|
|
af66138a17 | ||
|
|
5f060d60a7 | ||
|
|
73ccbb69ea | ||
|
|
be60440b20 | ||
|
|
837efb78e6 | ||
|
|
4a62a290d8 | ||
|
|
018399cb1f | ||
|
|
646a576358 | ||
|
|
d8e19cd79a | ||
|
|
757cb0cf23 | ||
|
|
7d92ab335a | ||
|
|
46c6d6f656 | ||
|
|
46260749c1 | ||
|
|
50664fe115 | ||
|
|
c480bd94db | ||
|
|
79923a939b | ||
|
|
327b22113a | ||
|
|
12160ab539 | ||
|
|
2462ea0892 | ||
|
|
8be09eadd4 | ||
|
|
98bc96c911 | ||
|
|
b0fce6a80d | ||
|
|
53b8a21d1e | ||
|
|
1346492d72 | ||
|
|
e5bb8d7992 | ||
|
|
49594b8435 | ||
|
|
3bd37a7906 | ||
|
|
e070a85ae0 | ||
|
|
c189278e24 | ||
|
|
2a8606bd98 | ||
|
|
18ea05c837 | ||
|
|
86c3072515 | ||
|
|
fccf508dde | ||
|
|
2da21f90f4 | ||
|
|
bec7f1726f | ||
|
|
74dfb9d88d | ||
|
|
02dddfc227 | ||
|
|
545016b38f | ||
|
|
0ccceaf226 | ||
|
|
a601115650 | ||
|
|
ae6267c906 | ||
|
|
ac142694f5 | ||
|
|
69b0913315 | ||
|
|
421bacd7dc | ||
|
|
573a76eedb | ||
|
|
b7948c7f40 | ||
|
|
2647d09b8f | ||
|
|
57e919d7e5 | ||
|
|
f456aa1407 | ||
|
|
d0d62892c8 | ||
|
|
a981cfa053 | ||
|
|
55290dd1e3 | ||
|
|
9c4e255994 | ||
|
|
f9c7d5f7bc | ||
|
|
49baea5f6a | ||
|
|
6209cf3933 | ||
|
|
d170a523c3 | ||
|
|
be5040e7a8 | ||
|
|
ecbaa5bfc1 | ||
|
|
25e2af7c89 | ||
|
|
605688426d | ||
|
|
0e069f1e75 | ||
|
|
e9adbf18d3 | ||
|
|
610202097a | ||
|
|
8c2c552164 | ||
|
|
b9976cf693 | ||
|
|
3261c405bd | ||
|
|
35d3328e3e | ||
|
|
e96041d76f | ||
|
|
c2034bc0c0 | ||
|
|
e8855f7621 | ||
|
|
bdb8368e89 | ||
|
|
f160db2032 | ||
|
|
de9a32a273 | ||
|
|
6ba7422c3b | ||
|
|
5cbb0ceb80 | ||
|
|
5b29358b37 | ||
|
|
90147f3dfb | ||
|
|
72873abe05 | ||
|
|
de1810ba68 | ||
|
|
7b7c765d78 | ||
|
|
806d4660cf | ||
|
|
5ae5d364bb | ||
|
|
1af67e72d4 | ||
|
|
ed268ad683 | ||
|
|
5bdd2ca02f | ||
|
|
eb59861d4d | ||
|
|
427e46a2aa | ||
|
|
68a8649292 | ||
|
|
2aff8709a5 | ||
|
|
62c3add888 | ||
|
|
3ac878db62 | ||
|
|
c247cd8fea | ||
|
|
b6772b7280 | ||
|
|
807a3df9d1 | ||
|
|
491d60e267 | ||
|
|
4811eafd67 | ||
|
|
8dedbb9620 | ||
|
|
dd8454161f | ||
|
|
9421f2cddd | ||
|
|
d8c4f78ec1 | ||
|
|
54296da647 | ||
|
|
357102fdb5 | ||
|
|
7e15a9e181 | ||
|
|
12e0b2d6f7 | ||
|
|
11b40bf32f | ||
|
|
8d2b53373f | ||
|
|
9ecc49e592 | ||
|
|
4f34f7083b | ||
|
|
2a6df875ec | ||
|
|
51c83116a2 | ||
|
|
74435aac76 | ||
|
|
5dfdb5b5f9 | ||
|
|
ac892a3f3d | ||
|
|
1a2e99f559 | ||
|
|
e97bba0524 | ||
|
|
0538f0c524 | ||
|
|
fc3e35868d | ||
|
|
f1e0cfea1c | ||
|
|
56efef69ba | ||
|
|
668ec8a248 | ||
|
|
60912bd01c | ||
|
|
0b416e44f8 | ||
|
|
ecc4aa09d3 | ||
|
|
b921aabbed | ||
|
|
6ad8ac0b6b | ||
|
|
44e7e0e970 | ||
|
|
45820b4ce3 | ||
|
|
3a098377cb | ||
|
|
35875485ee |
9
.github/ISSUE_TEMPLATE.md
vendored
Normal file
9
.github/ISSUE_TEMPLATE.md
vendored
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
## Versions:
|
||||||
|
|
||||||
|
* mgmt version (eg: `mgmt --version`):
|
||||||
|
|
||||||
|
* operating system/distribution (eg: `uname -a`):
|
||||||
|
|
||||||
|
* golang version (eg: `go version`):
|
||||||
|
|
||||||
|
## Description:
|
||||||
36
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
36
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
## Tips:
|
||||||
|
|
||||||
|
* commit message titles must be in the form:
|
||||||
|
```topic: Capitalized message with no trailing period```
|
||||||
|
or:
|
||||||
|
```topic, topic2: Capitalized message with no trailing period```
|
||||||
|
|
||||||
|
* golang code must be formatted according to the standard, please run:
|
||||||
|
```
|
||||||
|
make gofmt # formats the entire project correctly
|
||||||
|
```
|
||||||
|
or format a single golang file correctly:
|
||||||
|
```
|
||||||
|
gofmt -w yourcode.go
|
||||||
|
```
|
||||||
|
|
||||||
|
* please rebase your patch against current git master:
|
||||||
|
```
|
||||||
|
git checkout master
|
||||||
|
git pull origin master
|
||||||
|
git checkout your-feature
|
||||||
|
git rebase master
|
||||||
|
git push your-remote your-feature
|
||||||
|
hub pull-request # or submit with the github web ui
|
||||||
|
```
|
||||||
|
|
||||||
|
* after a patch review, please ping @purpleidea so we know to re-review:
|
||||||
|
```
|
||||||
|
# make changes based on reviews...
|
||||||
|
git add -p # add new changes
|
||||||
|
git commit --amend # combine with existing commit
|
||||||
|
git push your-remote your-feature -f
|
||||||
|
# now ping @purpleidea in the github PR since it doesn't notify us automatically
|
||||||
|
```
|
||||||
|
|
||||||
|
## Thanks for contributing to mgmt and welcome to the team!
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,6 +2,7 @@
|
|||||||
.omv/
|
.omv/
|
||||||
.ssh/
|
.ssh/
|
||||||
.vagrant/
|
.vagrant/
|
||||||
|
.envrc
|
||||||
old/
|
old/
|
||||||
tmp/
|
tmp/
|
||||||
*_stringer.go
|
*_stringer.go
|
||||||
|
|||||||
9
.gitmodules
vendored
9
.gitmodules
vendored
@@ -13,3 +13,12 @@
|
|||||||
[submodule "vendor/github.com/purpleidea/go-systemd"]
|
[submodule "vendor/github.com/purpleidea/go-systemd"]
|
||||||
path = vendor/github.com/purpleidea/go-systemd
|
path = vendor/github.com/purpleidea/go-systemd
|
||||||
url = https://github.com/purpleidea/go-systemd
|
url = https://github.com/purpleidea/go-systemd
|
||||||
|
[submodule "vendor/honnef.co/go/augeas"]
|
||||||
|
path = vendor/honnef.co/go/augeas
|
||||||
|
url = https://github.com/dominikh/go-augeas/
|
||||||
|
[submodule "vendor/github.com/grpc-ecosystem/go-grpc-prometheus"]
|
||||||
|
path = vendor/github.com/grpc-ecosystem/go-grpc-prometheus
|
||||||
|
url = https://github.com/grpc-ecosystem/go-grpc-prometheus
|
||||||
|
[submodule "vendor/github.com/ugorji/go"]
|
||||||
|
path = vendor/github.com/ugorji/go
|
||||||
|
url = https://github.com/ugorji/go
|
||||||
|
|||||||
14
.travis.yml
14
.travis.yml
@@ -1,18 +1,21 @@
|
|||||||
language: go
|
language: go
|
||||||
go:
|
go:
|
||||||
- 1.6
|
- 1.8.x
|
||||||
- 1.7
|
- 1.9.x
|
||||||
- tip
|
- tip
|
||||||
|
go_import_path: github.com/purpleidea/mgmt
|
||||||
sudo: true
|
sudo: true
|
||||||
dist: trusty
|
dist: trusty
|
||||||
before_install: 'git fetch --unshallow'
|
before_install:
|
||||||
|
- sudo apt update
|
||||||
|
- git fetch --unshallow
|
||||||
install: 'make deps'
|
install: 'make deps'
|
||||||
script: 'make test'
|
script: 'make test'
|
||||||
matrix:
|
matrix:
|
||||||
fast_finish: true
|
fast_finish: true
|
||||||
allow_failures:
|
allow_failures:
|
||||||
- go: tip
|
- go: tip
|
||||||
- go: 1.7
|
- go: 1.9.x
|
||||||
notifications:
|
notifications:
|
||||||
irc:
|
irc:
|
||||||
channels:
|
channels:
|
||||||
@@ -25,4 +28,7 @@ notifications:
|
|||||||
use_notice: false
|
use_notice: false
|
||||||
skip_join: false
|
skip_join: false
|
||||||
email:
|
email:
|
||||||
|
recipients:
|
||||||
- travis-ci@shubin.ca
|
- travis-ci@shubin.ca
|
||||||
|
on_failure: change
|
||||||
|
on_success: change
|
||||||
|
|||||||
2
AUTHORS
2
AUTHORS
@@ -4,5 +4,7 @@ If you appreciate the work of one of the contributors, thank them a beverage!
|
|||||||
For a more exhaustive list please run: git log --format='%aN' | sort -u
|
For a more exhaustive list please run: git log --format='%aN' | sort -u
|
||||||
This list is sorted alphabetically by first name.
|
This list is sorted alphabetically by first name.
|
||||||
|
|
||||||
|
Felix Frank
|
||||||
James Shubin
|
James Shubin
|
||||||
|
Julien Pivotto
|
||||||
Paul Morgan
|
Paul Morgan
|
||||||
|
|||||||
141
COPYING
141
COPYING
@@ -1,5 +1,5 @@
|
|||||||
GNU AFFERO GENERAL PUBLIC LICENSE
|
GNU GENERAL PUBLIC LICENSE
|
||||||
Version 3, 19 November 2007
|
Version 3, 29 June 2007
|
||||||
|
|
||||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||||
Everyone is permitted to copy and distribute verbatim copies
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
@@ -7,15 +7,17 @@
|
|||||||
|
|
||||||
Preamble
|
Preamble
|
||||||
|
|
||||||
The GNU Affero General Public License is a free, copyleft license for
|
The GNU General Public License is a free, copyleft license for
|
||||||
software and other kinds of works, specifically designed to ensure
|
software and other kinds of works.
|
||||||
cooperation with the community in the case of network server software.
|
|
||||||
|
|
||||||
The licenses for most software and other practical works are designed
|
The licenses for most software and other practical works are designed
|
||||||
to take away your freedom to share and change the works. By contrast,
|
to take away your freedom to share and change the works. By contrast,
|
||||||
our General Public Licenses are intended to guarantee your freedom to
|
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
|
share and change all versions of a program--to make sure it remains free
|
||||||
software for all its users.
|
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
|
When we speak of free software, we are referring to freedom, not
|
||||||
price. Our General Public Licenses are designed to make sure that you
|
price. Our General Public Licenses are designed to make sure that you
|
||||||
@@ -24,34 +26,44 @@ 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
|
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.
|
free programs, and that you know you can do these things.
|
||||||
|
|
||||||
Developers that use our General Public Licenses protect your rights
|
To protect your rights, we need to prevent others from denying you
|
||||||
with two steps: (1) assert copyright on the software, and (2) offer
|
these rights or asking you to surrender the rights. Therefore, you have
|
||||||
you this License which gives you legal permission to copy, distribute
|
certain responsibilities if you distribute copies of the software, or if
|
||||||
and/or modify the software.
|
you modify it: responsibilities to respect the freedom of others.
|
||||||
|
|
||||||
A secondary benefit of defending all users' freedom is that
|
For example, if you distribute copies of such a program, whether
|
||||||
improvements made in alternate versions of the program, if they
|
gratis or for a fee, you must pass on to the recipients the same
|
||||||
receive widespread use, become available for other developers to
|
freedoms that you received. You must make sure that they, too, receive
|
||||||
incorporate. Many developers of free software are heartened and
|
or can get the source code. And you must show them these terms so they
|
||||||
encouraged by the resulting cooperation. However, in the case of
|
know their rights.
|
||||||
software used on network servers, this result may fail to come about.
|
|
||||||
The GNU General Public License permits making a modified version and
|
|
||||||
letting the public access it on a server without ever releasing its
|
|
||||||
source code to the public.
|
|
||||||
|
|
||||||
The GNU Affero General Public License is designed specifically to
|
Developers that use the GNU GPL protect your rights with two steps:
|
||||||
ensure that, in such cases, the modified source code becomes available
|
(1) assert copyright on the software, and (2) offer you this License
|
||||||
to the community. It requires the operator of a network server to
|
giving you legal permission to copy, distribute and/or modify it.
|
||||||
provide the source code of the modified version running there to the
|
|
||||||
users of that server. Therefore, public use of a modified version, on
|
|
||||||
a publicly accessible server, gives the public access to the source
|
|
||||||
code of the modified version.
|
|
||||||
|
|
||||||
An older license, called the Affero General Public License and
|
For the developers' and authors' protection, the GPL clearly explains
|
||||||
published by Affero, was designed to accomplish similar goals. This is
|
that there is no warranty for this free software. For both users' and
|
||||||
a different license, not a version of the Affero GPL, but Affero has
|
authors' sake, the GPL requires that modified versions be marked as
|
||||||
released a new version of the Affero GPL which permits relicensing under
|
changed, so that their problems will not be attributed erroneously to
|
||||||
this license.
|
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
|
The precise terms and conditions for copying, distribution and
|
||||||
modification follow.
|
modification follow.
|
||||||
@@ -60,7 +72,7 @@ modification follow.
|
|||||||
|
|
||||||
0. Definitions.
|
0. Definitions.
|
||||||
|
|
||||||
"This License" refers to version 3 of the GNU Affero General Public License.
|
"This License" refers to version 3 of the GNU General Public License.
|
||||||
|
|
||||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||||
works, such as semiconductor masks.
|
works, such as semiconductor masks.
|
||||||
@@ -537,45 +549,35 @@ 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
|
the Program, the only way you could satisfy both those terms and this
|
||||||
License would be to refrain entirely from conveying the Program.
|
License would be to refrain entirely from conveying the Program.
|
||||||
|
|
||||||
13. Remote Network Interaction; Use with the GNU General Public License.
|
13. Use with the GNU Affero General Public License.
|
||||||
|
|
||||||
Notwithstanding any other provision of this License, if you modify the
|
|
||||||
Program, your modified version must prominently offer all users
|
|
||||||
interacting with it remotely through a computer network (if your version
|
|
||||||
supports such interaction) an opportunity to receive the Corresponding
|
|
||||||
Source of your version by providing access to the Corresponding Source
|
|
||||||
from a network server at no charge, through some standard or customary
|
|
||||||
means of facilitating copying of software. This Corresponding Source
|
|
||||||
shall include the Corresponding Source for any work covered by version 3
|
|
||||||
of the GNU General Public License that is incorporated pursuant to the
|
|
||||||
following paragraph.
|
|
||||||
|
|
||||||
Notwithstanding any other provision of this License, you have
|
Notwithstanding any other provision of this License, you have
|
||||||
permission to link or combine any covered work with a work licensed
|
permission to link or combine any covered work with a work licensed
|
||||||
under version 3 of the GNU General Public License into a single
|
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
|
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,
|
License will continue to apply to the part which is the covered work,
|
||||||
but the work with which it is combined will remain governed by version
|
but the special requirements of the GNU Affero General Public License,
|
||||||
3 of the GNU General Public License.
|
section 13, concerning interaction through a network will apply to the
|
||||||
|
combination as such.
|
||||||
|
|
||||||
14. Revised Versions of this License.
|
14. Revised Versions of this License.
|
||||||
|
|
||||||
The Free Software Foundation may publish revised and/or new versions of
|
The Free Software Foundation may publish revised and/or new versions of
|
||||||
the GNU Affero General Public License from time to time. Such new versions
|
the GNU General Public License from time to time. Such new versions will
|
||||||
will be similar in spirit to the present version, but may differ in detail to
|
be similar in spirit to the present version, but may differ in detail to
|
||||||
address new problems or concerns.
|
address new problems or concerns.
|
||||||
|
|
||||||
Each version is given a distinguishing version number. If the
|
Each version is given a distinguishing version number. If the
|
||||||
Program specifies that a certain numbered version of the GNU Affero General
|
Program specifies that a certain numbered version of the GNU General
|
||||||
Public License "or any later version" applies to it, you have the
|
Public License "or any later version" applies to it, you have the
|
||||||
option of following the terms and conditions either of that numbered
|
option of following the terms and conditions either of that numbered
|
||||||
version or of any later version published by the Free Software
|
version or of any later version published by the Free Software
|
||||||
Foundation. If the Program does not specify a version number of the
|
Foundation. If the Program does not specify a version number of the
|
||||||
GNU Affero General Public License, you may choose any version ever published
|
GNU General Public License, you may choose any version ever published
|
||||||
by the Free Software Foundation.
|
by the Free Software Foundation.
|
||||||
|
|
||||||
If the Program specifies that a proxy can decide which future
|
If the Program specifies that a proxy can decide which future
|
||||||
versions of the GNU Affero General Public License can be used, that proxy's
|
versions of the GNU General Public License can be used, that proxy's
|
||||||
public statement of acceptance of a version permanently authorizes you
|
public statement of acceptance of a version permanently authorizes you
|
||||||
to choose that version for the Program.
|
to choose that version for the Program.
|
||||||
|
|
||||||
@@ -633,29 +635,40 @@ the "copyright" line and a pointer to where the full notice is found.
|
|||||||
Copyright (C) <year> <name of author>
|
Copyright (C) <year> <name of author>
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU Affero General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
(at your option) any later version.
|
(at your option) any later version.
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
GNU Affero General Public License for more details.
|
GNU General Public License for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU Affero General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
Also add information on how to contact you by electronic and paper mail.
|
Also add information on how to contact you by electronic and paper mail.
|
||||||
|
|
||||||
If your software can interact with users remotely through a computer
|
If the program does terminal interaction, make it output a short
|
||||||
network, you should also make sure that it provides a way for users to
|
notice like this when it starts in an interactive mode:
|
||||||
get its source. For example, if your program is a web application, its
|
|
||||||
interface could display a "Source" link that leads users to an archive
|
<program> Copyright (C) <year> <name of author>
|
||||||
of the code. There are many ways you could offer source, and different
|
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||||
solutions will be better for different programs; see section 13 for the
|
This is free software, and you are welcome to redistribute it
|
||||||
specific requirements.
|
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,
|
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.
|
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 AGPL, see
|
For more information on this, and how to apply and follow the GNU GPL, see
|
||||||
<http://www.gnu.org/licenses/>.
|
<http://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
|
||||||
|
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
Mgmt
|
Mgmt
|
||||||
Copyright (C) 2013-2016+ James Shubin and the project contributors
|
Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
Written by James Shubin <james@shubin.ca> and the project contributors
|
Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU Affero General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
(at your option) any later version.
|
(at your option) any later version.
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
GNU Affero General Public License for more details.
|
GNU General Public License for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU Affero General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|||||||
23
Makefile
23
Makefile
@@ -1,18 +1,18 @@
|
|||||||
# Mgmt
|
# Mgmt
|
||||||
# Copyright (C) 2013-2016+ James Shubin and the project contributors
|
# Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
# Written by James Shubin <james@shubin.ca> and the project contributors
|
# Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# This program is free software: you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU Affero General Public License as published by
|
# it under the terms of the GNU General Public License as published by
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
# (at your option) any later version.
|
# (at your option) any later version.
|
||||||
#
|
#
|
||||||
# This program is distributed in the hope that it will be useful,
|
# This program is distributed in the hope that it will be useful,
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
# GNU Affero General Public License for more details.
|
# GNU General Public License for more details.
|
||||||
#
|
#
|
||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
SHELL = /usr/bin/env bash
|
SHELL = /usr/bin/env bash
|
||||||
@@ -37,6 +37,11 @@ RPM = rpmbuild/RPMS/$(PROGRAM)-$(VERSION)-$(RELEASE).$(ARCH).rpm
|
|||||||
USERNAME := $(shell cat ~/.config/copr 2>/dev/null | grep username | awk -F '=' '{print $$2}' | tr -d ' ')
|
USERNAME := $(shell cat ~/.config/copr 2>/dev/null | grep username | awk -F '=' '{print $$2}' | tr -d ' ')
|
||||||
SERVER = 'dl.fedoraproject.org'
|
SERVER = 'dl.fedoraproject.org'
|
||||||
REMOTE_PATH = 'pub/alt/$(USERNAME)/$(PROGRAM)'
|
REMOTE_PATH = 'pub/alt/$(USERNAME)/$(PROGRAM)'
|
||||||
|
ifneq ($(GOTAGS),)
|
||||||
|
BUILD_FLAGS = -tags '$(GOTAGS)'
|
||||||
|
endif
|
||||||
|
|
||||||
|
default: build
|
||||||
|
|
||||||
#
|
#
|
||||||
# art
|
# art
|
||||||
@@ -105,9 +110,9 @@ $(PROGRAM): main.go
|
|||||||
@echo "Building: $(PROGRAM), version: $(SVERSION)..."
|
@echo "Building: $(PROGRAM), version: $(SVERSION)..."
|
||||||
ifneq ($(OLDGOLANG),)
|
ifneq ($(OLDGOLANG),)
|
||||||
@# avoid equals sign in old golang versions eg in: -X foo=bar
|
@# avoid equals sign in old golang versions eg in: -X foo=bar
|
||||||
time go build -ldflags "-X main.program $(PROGRAM) -X main.version $(SVERSION)" -o $(PROGRAM);
|
time go build -ldflags "-X main.program $(PROGRAM) -X main.version $(SVERSION)" -o $(PROGRAM) $(BUILD_FLAGS);
|
||||||
else
|
else
|
||||||
time go build -ldflags "-X main.program=$(PROGRAM) -X main.version=$(SVERSION)" -o $(PROGRAM);
|
time go build -i -ldflags "-X main.program=$(PROGRAM) -X main.version=$(SVERSION)" -o $(PROGRAM) $(BUILD_FLAGS);
|
||||||
endif
|
endif
|
||||||
|
|
||||||
$(PROGRAM).static: main.go
|
$(PROGRAM).static: main.go
|
||||||
@@ -115,9 +120,9 @@ $(PROGRAM).static: main.go
|
|||||||
go generate
|
go generate
|
||||||
ifneq ($(OLDGOLANG),)
|
ifneq ($(OLDGOLANG),)
|
||||||
@# avoid equals sign in old golang versions eg in: -X foo=bar
|
@# avoid equals sign in old golang versions eg in: -X foo=bar
|
||||||
go build -a -installsuffix cgo -tags netgo -ldflags '-extldflags "-static" -X main.program $(PROGRAM) -X main.version $(SVERSION)' -o $(PROGRAM).static;
|
go build -a -installsuffix cgo -tags netgo -ldflags '-extldflags "-static" -X main.program $(PROGRAM) -X main.version $(SVERSION)' -o $(PROGRAM).static $(BUILD_FLAGS);
|
||||||
else
|
else
|
||||||
go build -a -installsuffix cgo -tags netgo -ldflags '-extldflags "-static" -X main.program=$(PROGRAM) -X main.version=$(SVERSION)' -o $(PROGRAM).static;
|
go build -a -installsuffix cgo -tags netgo -ldflags '-extldflags "-static" -X main.program=$(PROGRAM) -X main.version=$(SVERSION)' -o $(PROGRAM).static $(BUILD_FLAGS);
|
||||||
endif
|
endif
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@@ -183,7 +188,7 @@ $(SRPM): $(SPEC) $(SOURCE)
|
|||||||
#
|
#
|
||||||
$(SPEC): rpmbuild/ spec.in
|
$(SPEC): rpmbuild/ spec.in
|
||||||
@echo Running templater...
|
@echo Running templater...
|
||||||
#cat spec.in > $(SPEC)
|
cat spec.in > $(SPEC)
|
||||||
sed -e s/__PROGRAM__/$(PROGRAM)/g -e s/__VERSION__/$(VERSION)/g -e s/__RELEASE__/$(RELEASE)/g < spec.in > $(SPEC)
|
sed -e s/__PROGRAM__/$(PROGRAM)/g -e s/__VERSION__/$(VERSION)/g -e s/__RELEASE__/$(RELEASE)/g < spec.in > $(SPEC)
|
||||||
# append a changelog to the .spec file
|
# append a changelog to the .spec file
|
||||||
git log --format="* %cd %aN <%aE>%n- (%h) %s%d%n" --date=local | sed -r 's/[0-9]+:[0-9]+:[0-9]+ //' >> $(SPEC)
|
git log --format="* %cd %aN <%aE>%n- (%h) %s%d%n" --date=local | sed -r 's/[0-9]+:[0-9]+:[0-9]+ //' >> $(SPEC)
|
||||||
|
|||||||
134
README.md
134
README.md
@@ -2,18 +2,20 @@
|
|||||||
|
|
||||||
[](art/)
|
[](art/)
|
||||||
|
|
||||||
[](https://goreportcard.com/report/github.com/purpleidea/mgmt)
|
[](https://goreportcard.com/report/github.com/purpleidea/mgmt)
|
||||||
[](http://travis-ci.org/purpleidea/mgmt)
|
[](http://travis-ci.org/purpleidea/mgmt)
|
||||||
[](docs/documentation.md)
|
[](https://godoc.org/github.com/purpleidea/mgmt)
|
||||||
[](https://godoc.org/github.com/purpleidea/mgmt)
|
[](https://webchat.freenode.net/?channels=#mgmtconfig)
|
||||||
[](https://webchat.freenode.net/?channels=#mgmtconfig)
|
[](https://ci.centos.org/job/purpleidea-mgmt/)
|
||||||
[](https://ci.centos.org/job/purpleidea-mgmt/)
|
|
||||||
[](https://copr.fedoraproject.org/coprs/purpleidea/mgmt/)
|
|
||||||
[](https://aur.archlinux.org/packages/mgmt/)
|
|
||||||
|
|
||||||
## Community:
|
## Community:
|
||||||
Come join us on IRC in [#mgmtconfig](https://webchat.freenode.net/?channels=#mgmtconfig) on Freenode!
|
Come join us in the `mgmt` community!
|
||||||
You may like the [#mgmtconfig](https://twitter.com/hashtag/mgmtconfig) hashtag if you're on [Twitter](https://twitter.com/#!/purpleidea).
|
|
||||||
|
| Medium | Link |
|
||||||
|
|---|---|
|
||||||
|
| IRC | [#mgmtconfig](https://webchat.freenode.net/?channels=#mgmtconfig) on Freenode |
|
||||||
|
| Twitter | [@mgmtconfig](https://twitter.com/mgmtconfig) & [#mgmtconfig](https://twitter.com/hashtag/mgmtconfig) |
|
||||||
|
| Mailing list | [mgmtconfig-list@redhat.com](https://www.redhat.com/mailman/listinfo/mgmtconfig-list) |
|
||||||
|
|
||||||
## Status:
|
## Status:
|
||||||
Mgmt is a fairly new project.
|
Mgmt is a fairly new project.
|
||||||
@@ -21,35 +23,21 @@ We're working towards being minimally useful for production environments.
|
|||||||
We aren't feature complete for what we'd consider a 1.x release yet.
|
We aren't feature complete for what we'd consider a 1.x release yet.
|
||||||
With your help you'll be able to influence our design and get us there sooner!
|
With your help you'll be able to influence our design and get us there sooner!
|
||||||
|
|
||||||
## Questions:
|
|
||||||
Please join the [#mgmtconfig](https://webchat.freenode.net/?channels=#mgmtconfig) IRC community!
|
|
||||||
If you have a well phrased question that might benefit others, consider asking it by sending a patch to the documentation [FAQ](https://github.com/purpleidea/mgmt/blob/master/docs/documentation.md#usage-and-frequently-asked-questions) section. I'll merge your question, and a patch with the answer!
|
|
||||||
|
|
||||||
## Quick start:
|
|
||||||
* Make sure you have golang version 1.6 or greater installed.
|
|
||||||
* If you do not have a GOPATH yet, create one and export it:
|
|
||||||
```
|
|
||||||
mkdir $HOME/gopath
|
|
||||||
export GOPATH=$HOME/gopath
|
|
||||||
```
|
|
||||||
* You might also want to add the GOPATH to your `~/.bashrc` or `~/.profile`.
|
|
||||||
* For more information you can read the [GOPATH documentation](https://golang.org/cmd/go/#hdr-GOPATH_environment_variable).
|
|
||||||
* Next download the mgmt code base, and switch to that directory:
|
|
||||||
```
|
|
||||||
go get -u github.com/purpleidea/mgmt
|
|
||||||
cd $GOPATH/src/github.com/purpleidea/mgmt
|
|
||||||
```
|
|
||||||
* Get the remaining golang deps with `go get ./...`, or run `make deps` if you're comfortable with how we install them.
|
|
||||||
* Run `make build` to get a freshly built `mgmt` binary.
|
|
||||||
* Run `time ./mgmt run --yaml examples/graph0.yaml --converged-timeout=5 --tmp-prefix` to try out a very simple example!
|
|
||||||
* To run continuously in the default mode of operation, omit the `--converged-timeout` option.
|
|
||||||
* Have fun hacking on our future technology!
|
|
||||||
|
|
||||||
## Examples:
|
|
||||||
Please look in the [examples/](examples/) folder for more examples!
|
|
||||||
|
|
||||||
## Documentation:
|
## Documentation:
|
||||||
Please see: the manually created [documentation.md](docs/documentation.md) (also available as [PDF](https://pdfdoc-purpleidea.rhcloud.com/pdf/https://github.com/purpleidea/mgmt/blob/master/docs/documentation.md)) and the automatically generated [GoDoc documentation](https://godoc.org/github.com/purpleidea/mgmt).
|
Please read, enjoy and help improve our documentation!
|
||||||
|
|
||||||
|
| Documentation | Additional Notes |
|
||||||
|
|---|---|
|
||||||
|
| [general documentation](docs/documentation.md) | for everyone |
|
||||||
|
| [quick start guide](docs/quick-start-guide.md) | for mgmt developers |
|
||||||
|
| [resource guide](docs/resource-guide.md) | for mgmt developers |
|
||||||
|
| [godoc API reference](https://godoc.org/github.com/purpleidea/mgmt) | for mgmt developers |
|
||||||
|
| [prometheus guide](docs/prometheus.md) | for everyone |
|
||||||
|
| [puppet guide](docs/puppet-guide.md) | for puppet sysadmins |
|
||||||
|
|
||||||
|
## Questions:
|
||||||
|
Please ask in the [community](#community)!
|
||||||
|
If you have a well phrased question that might benefit others, consider asking it by sending a patch to the documentation [FAQ](https://github.com/purpleidea/mgmt/blob/master/docs/documentation.md#usage-and-frequently-asked-questions) section. I'll merge your question, and a patch with the answer!
|
||||||
|
|
||||||
## Roadmap:
|
## Roadmap:
|
||||||
Please see: [TODO.md](TODO.md) for a list of upcoming work and TODO items.
|
Please see: [TODO.md](TODO.md) for a list of upcoming work and TODO items.
|
||||||
@@ -61,52 +49,38 @@ Please set the `DEBUG` constant in [main.go](https://github.com/purpleidea/mgmt/
|
|||||||
Bonus points if you provide a [shell](https://github.com/purpleidea/mgmt/tree/master/test/shell) or [OMV](https://github.com/purpleidea/mgmt/tree/master/test/omv) reproducible test case.
|
Bonus points if you provide a [shell](https://github.com/purpleidea/mgmt/tree/master/test/shell) or [OMV](https://github.com/purpleidea/mgmt/tree/master/test/omv) reproducible test case.
|
||||||
Feel free to read my article on [debugging golang programs](https://ttboj.wordpress.com/2016/02/15/debugging-golang-programs/).
|
Feel free to read my article on [debugging golang programs](https://ttboj.wordpress.com/2016/02/15/debugging-golang-programs/).
|
||||||
|
|
||||||
## Dependencies:
|
|
||||||
* golang 1.6 or higher (required, available in most distros)
|
|
||||||
* golang libraries (required, available with `go get`)
|
|
||||||
```
|
|
||||||
go get github.com/coreos/etcd/client
|
|
||||||
go get gopkg.in/yaml.v2
|
|
||||||
go get gopkg.in/fsnotify.v1
|
|
||||||
go get github.com/urfave/cli
|
|
||||||
go get github.com/coreos/go-systemd/dbus
|
|
||||||
go get github.com/coreos/go-systemd/util
|
|
||||||
go get github.com/coreos/pkg/capnslog
|
|
||||||
go get github.com/rgbkrk/libvirt-go
|
|
||||||
```
|
|
||||||
* stringer (optional for building), available as a package on some platforms, otherwise via `go get`
|
|
||||||
```
|
|
||||||
go get golang.org/x/tools/cmd/stringer
|
|
||||||
```
|
|
||||||
* pandoc (optional, for building a pdf of the documentation)
|
|
||||||
* graphviz (optional, for building a visual representation of the graph)
|
|
||||||
|
|
||||||
## Patches:
|
## Patches:
|
||||||
We'd love to have your patches! Please send them by email, or as a pull request.
|
We'd love to have your patches! Please send them by email, or as a pull request.
|
||||||
|
|
||||||
## On the web:
|
## On the web:
|
||||||
* James Shubin; blog: [Next generation configuration mgmt](https://ttboj.wordpress.com/2016/01/18/next-generation-configuration-mgmt/)
|
| Author | Format | Subject |
|
||||||
* James Shubin; video: [Introductory recording from DevConf.cz 2016](https://www.youtube.com/watch?v=GVhpPF0j-iE&html5=1)
|
|---|---|---|
|
||||||
* James Shubin; video: [Introductory recording from CfgMgmtCamp.eu 2016](https://www.youtube.com/watch?v=fNeooSiIRnA&html5=1)
|
| James Shubin | blog | [Next generation configuration mgmt](https://ttboj.wordpress.com/2016/01/18/next-generation-configuration-mgmt/) |
|
||||||
* Julian Dunn; video: [On mgmt at CfgMgmtCamp.eu 2016](https://www.youtube.com/watch?v=kfF9IATUask&t=1949&html5=1)
|
| James Shubin | video | [Introductory recording from DevConf.cz 2016](https://www.youtube.com/watch?v=GVhpPF0j-iE&html5=1) |
|
||||||
* Walter Heck; slides: [On mgmt at CfgMgmtCamp.eu 2016](http://www.slideshare.net/olindata/configuration-management-time-for-a-4th-generation/3)
|
| James Shubin | video | [Introductory recording from CfgMgmtCamp.eu 2016](https://www.youtube.com/watch?v=fNeooSiIRnA&html5=1) |
|
||||||
* Marco Marongiu; blog: [On mgmt](http://syslog.me/2016/02/15/leap-or-die/)
|
| Julian Dunn | video | [On mgmt at CfgMgmtCamp.eu 2016](https://www.youtube.com/watch?v=kfF9IATUask&t=1949&html5=1) |
|
||||||
* Felix Frank; blog: [From Catalog To Mgmt (on puppet to mgmt "transpiling")](https://ffrank.github.io/features/2016/02/18/from-catalog-to-mgmt/)
|
| Walter Heck | slides | [On mgmt at CfgMgmtCamp.eu 2016](http://www.slideshare.net/olindata/configuration-management-time-for-a-4th-generation/3) |
|
||||||
* James Shubin; blog: [Automatic edges in mgmt (...and the pkg resource)](https://ttboj.wordpress.com/2016/03/14/automatic-edges-in-mgmt/)
|
| Marco Marongiu | blog | [On mgmt](http://syslog.me/2016/02/15/leap-or-die/) |
|
||||||
* James Shubin; blog: [Automatic grouping in mgmt](https://ttboj.wordpress.com/2016/03/30/automatic-grouping-in-mgmt/)
|
| Felix Frank | blog | [From Catalog To Mgmt (on puppet to mgmt "transpiling")](https://ffrank.github.io/features/2016/02/18/from-catalog-to-mgmt/) |
|
||||||
* John Arundel; tweet: [“Puppet’s days are numbered.”](https://twitter.com/bitfield/status/732157519142002688)
|
| James Shubin | blog | [Automatic edges in mgmt (...and the pkg resource)](https://ttboj.wordpress.com/2016/03/14/automatic-edges-in-mgmt/) |
|
||||||
* Felix Frank; blog: [Puppet, Meet Mgmt (on puppet to mgmt internals)](https://ffrank.github.io/features/2016/06/12/puppet,-meet-mgmt/)
|
| James Shubin | blog | [Automatic grouping in mgmt](https://ttboj.wordpress.com/2016/03/30/automatic-grouping-in-mgmt/) |
|
||||||
* Felix Frank; blog: [Puppet Powered Mgmt (puppet to mgmt tl;dr)](https://ffrank.github.io/features/2016/06/19/puppet-powered-mgmt/)
|
| John Arundel | tweet | [“Puppet’s days are numbered.”](https://twitter.com/bitfield/status/732157519142002688) |
|
||||||
* James Shubin; blog: [Automatic clustering in mgmt](https://ttboj.wordpress.com/2016/06/20/automatic-clustering-in-mgmt/)
|
| Felix Frank | blog | [Puppet, Meet Mgmt (on puppet to mgmt internals)](https://ffrank.github.io/features/2016/06/12/puppet,-meet-mgmt/) |
|
||||||
* James Shubin; video: [Recording from CoreOSFest 2016](https://www.youtube.com/watch?v=KVmDCUA42wc&html5=1)
|
| Felix Frank | blog | [Puppet Powered Mgmt (puppet to mgmt tl;dr)](https://ffrank.github.io/features/2016/06/19/puppet-powered-mgmt/) |
|
||||||
* James Shubin; video: [Recording from DebConf16](http://meetings-archive.debian.net/pub/debian-meetings/2016/debconf16/Next_Generation_Config_Mgmt.webm) ([Slides](https://annex.debconf.org//debconf-share/debconf16/slides/15-next-generation-config-mgmt.pdf))
|
| James Shubin | blog | [Automatic clustering in mgmt](https://ttboj.wordpress.com/2016/06/20/automatic-clustering-in-mgmt/) |
|
||||||
* Felix Frank; blog: [Edging It All In (puppet and mgmt edges)](https://ffrank.github.io/features/2016/07/12/edging-it-all-in/)
|
| James Shubin | video | [Recording from CoreOSFest 2016](https://www.youtube.com/watch?v=KVmDCUA42wc&html5=1) |
|
||||||
* Felix Frank; blog: [Translating All The Things (puppet to mgmt translation warnings)](https://ffrank.github.io/features/2016/08/19/translating-all-the-things/)
|
| James Shubin | video | [Recording from DebConf16](http://meetings-archive.debian.net/pub/debian-meetings/2016/debconf16/Next_Generation_Config_Mgmt.webm) ([Slides](https://annex.debconf.org//debconf-share/debconf16/slides/15-next-generation-config-mgmt.pdf)) |
|
||||||
* James Shubin; video: [Recording from systemd.conf 2016](https://www.youtube.com/watch?v=jB992Zb3nH0&html5=1)
|
| Felix Frank | blog | [Edging It All In (puppet and mgmt edges)](https://ffrank.github.io/features/2016/07/12/edging-it-all-in/) |
|
||||||
* James Shubin; blog: [Remote execution in mgmt](https://ttboj.wordpress.com/2016/10/07/remote-execution-in-mgmt/)
|
| Felix Frank | blog | [Translating All The Things (puppet to mgmt translation warnings)](https://ffrank.github.io/features/2016/08/19/translating-all-the-things/) |
|
||||||
* James Shubin; video: [Recording from High Load Strategy 2016](https://vimeo.com/191493409)
|
| James Shubin | video | [Recording from systemd.conf 2016](https://www.youtube.com/watch?v=jB992Zb3nH0&html5=1) |
|
||||||
* James Shubin; video: [Recording from NLUUG 2016](https://www.youtube.com/watch?v=MmpwOQAb_SE&html5=1)
|
| James Shubin | blog | [Remote execution in mgmt](https://ttboj.wordpress.com/2016/10/07/remote-execution-in-mgmt/) |
|
||||||
* James Shubin; blog: [Send/Recv in mgmt](https://ttboj.wordpress.com/2016/12/07/sendrecv-in-mgmt/)
|
| James Shubin | video | [Recording from High Load Strategy 2016](https://vimeo.com/191493409) |
|
||||||
|
| James Shubin | video | [Recording from NLUUG 2016](https://www.youtube.com/watch?v=MmpwOQAb_SE&html5=1) |
|
||||||
|
| James Shubin | blog | [Send/Recv in mgmt](https://ttboj.wordpress.com/2016/12/07/sendrecv-in-mgmt/) |
|
||||||
|
| James Shubin | blog | [Metaparameters in mgmt](https://ttboj.wordpress.com/2017/03/01/metaparameters-in-mgmt/) |
|
||||||
|
| James Shubin | video | [Recording from Incontro DevOps 2017](https://vimeo.com/212241877) |
|
||||||
|
| Yves Brissaud | blog | [mgmt aux HumanTalks Grenoble (french)](http://log.winsos.net/2017/04/12/mgmt-aux-human-talks-grenoble.html) |
|
||||||
|
| James Shubin | video | [Recording from OSDC Berlin 2017](https://www.youtube.com/watch?v=LkEtBVLfygE&html5=1) |
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|
||||||
|
|||||||
16
TODO.md
16
TODO.md
@@ -1,16 +1,16 @@
|
|||||||
# TODO
|
# TODO
|
||||||
If you're looking for something to do, look here!
|
If you're looking for something to do, look here!
|
||||||
Let us know if you're working on one of the items.
|
Let us know if you're working on one of the items.
|
||||||
|
If you'd like something to work on, ping @purpleidea and I'll create an issue
|
||||||
|
tailored especially for you! Just let me know your approximate golang skill
|
||||||
|
level and how many hours you'd like to spend on the patch.
|
||||||
|
|
||||||
## Package resource
|
## Package resource
|
||||||
- [ ] getfiles support on debian [bug](https://github.com/hughsie/PackageKit/issues/118)
|
- [ ] getfiles support on debian [bug](https://github.com/hughsie/PackageKit/issues/118)
|
||||||
- [ ] directory info on fedora [bug](https://github.com/hughsie/PackageKit/issues/117)
|
- [ ] directory info on fedora [bug](https://github.com/hughsie/PackageKit/issues/117)
|
||||||
- [ ] dnf blocker [bug](https://github.com/hughsie/PackageKit/issues/110)
|
- [ ] dnf blocker [bug](https://github.com/hughsie/PackageKit/issues/110)
|
||||||
- [ ] install signal blocker [bug](https://github.com/hughsie/PackageKit/issues/109)
|
|
||||||
|
|
||||||
## File resource [bug](https://github.com/purpleidea/mgmt/issues/13) [:heart:](https://github.com/purpleidea/mgmt/labels/mgmtlove)
|
## File resource [bug](https://github.com/purpleidea/mgmt/issues/64) [:heart:](https://github.com/purpleidea/mgmt/labels/mgmtlove)
|
||||||
- [ ] chown/chmod support [:heart:](https://github.com/purpleidea/mgmt/labels/mgmtlove)
|
|
||||||
- [ ] user/group support [:heart:](https://github.com/purpleidea/mgmt/labels/mgmtlove)
|
|
||||||
- [ ] recurse limit support [:heart:](https://github.com/purpleidea/mgmt/labels/mgmtlove)
|
- [ ] recurse limit support [:heart:](https://github.com/purpleidea/mgmt/labels/mgmtlove)
|
||||||
- [ ] fanotify support [bug](https://github.com/go-fsnotify/fsnotify/issues/114)
|
- [ ] fanotify support [bug](https://github.com/go-fsnotify/fsnotify/issues/114)
|
||||||
|
|
||||||
@@ -21,7 +21,6 @@ Let us know if you're working on one of the items.
|
|||||||
- [ ] base resource improvements
|
- [ ] base resource improvements
|
||||||
|
|
||||||
## Timer resource
|
## Timer resource
|
||||||
- [ ] reset on recompile
|
|
||||||
- [ ] increment algorithm (linear, exponential, etc...) [:heart:](https://github.com/purpleidea/mgmt/labels/mgmtlove)
|
- [ ] increment algorithm (linear, exponential, etc...) [:heart:](https://github.com/purpleidea/mgmt/labels/mgmtlove)
|
||||||
|
|
||||||
## User/Group resource
|
## User/Group resource
|
||||||
@@ -29,7 +28,7 @@ Let us know if you're working on one of the items.
|
|||||||
- [ ] automatic edges to file resource [:heart:](https://github.com/purpleidea/mgmt/labels/mgmtlove)
|
- [ ] automatic edges to file resource [:heart:](https://github.com/purpleidea/mgmt/labels/mgmtlove)
|
||||||
|
|
||||||
## Virt (libvirt) resource
|
## Virt (libvirt) resource
|
||||||
- [ ] base resource [bug](https://github.com/purpleidea/mgmt/issues/25)
|
- [ ] base resource improvements [:heart:](https://github.com/purpleidea/mgmt/labels/mgmtlove)
|
||||||
|
|
||||||
## Net (systemd-networkd) resource
|
## Net (systemd-networkd) resource
|
||||||
- [ ] base resource
|
- [ ] base resource
|
||||||
@@ -44,7 +43,7 @@ Let us know if you're working on one of the items.
|
|||||||
- [ ] base resource [:heart:](https://github.com/purpleidea/mgmt/labels/mgmtlove)
|
- [ ] base resource [:heart:](https://github.com/purpleidea/mgmt/labels/mgmtlove)
|
||||||
|
|
||||||
## Http resource
|
## Http resource
|
||||||
- [ ] base resource
|
- [ ] base resource [:heart:](https://github.com/purpleidea/mgmt/labels/mgmtlove)
|
||||||
|
|
||||||
## Etcd improvements
|
## Etcd improvements
|
||||||
- [ ] fix embedded etcd master race
|
- [ ] fix embedded etcd master race
|
||||||
@@ -52,6 +51,9 @@ Let us know if you're working on one of the items.
|
|||||||
## Torrent/dht file transfer
|
## Torrent/dht file transfer
|
||||||
- [ ] base plumbing
|
- [ ] base plumbing
|
||||||
|
|
||||||
|
## GPG/Auth improvements
|
||||||
|
- [ ] base plumbing
|
||||||
|
|
||||||
## Language improvements
|
## Language improvements
|
||||||
- [ ] language design
|
- [ ] language design
|
||||||
- [ ] lexer/parser
|
- [ ] lexer/parser
|
||||||
|
|||||||
50
Vagrantfile
vendored
Normal file
50
Vagrantfile
vendored
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
Vagrant.configure(2) do |config|
|
||||||
|
config.ssh.forward_agent = true
|
||||||
|
config.ssh.username = 'vagrant'
|
||||||
|
config.vm.network "private_network", ip: "192.168.219.2"
|
||||||
|
|
||||||
|
config.vm.synced_folder ".", "/vagrant", disabled: true
|
||||||
|
|
||||||
|
config.vm.define "mgmt-dev" do |instance|
|
||||||
|
instance.vm.box = "fedora/26-cloud-base"
|
||||||
|
end
|
||||||
|
|
||||||
|
config.vm.provider "virtualbox" do |v|
|
||||||
|
v.memory = 1536
|
||||||
|
v.cpus = 2
|
||||||
|
end
|
||||||
|
config.vm.provider "libvirt" do |v|
|
||||||
|
v.memory = 2048
|
||||||
|
end
|
||||||
|
|
||||||
|
config.vm.provision "file", source: "vagrant/motd", destination: ".motd"
|
||||||
|
config.vm.provision "shell", inline: "cp ~vagrant/.motd /etc/motd"
|
||||||
|
|
||||||
|
config.vm.provision "file", source: "vagrant/mgmt.bashrc", destination: ".mgmt.bashrc"
|
||||||
|
config.vm.provision "file", source: "~/.gitconfig", destination: ".gitconfig"
|
||||||
|
|
||||||
|
# copied from make-deps.sh (with added git)
|
||||||
|
config.vm.provision "shell", inline: "dnf install -y libvirt-devel golang golang-googlecode-tools-stringer hg git make"
|
||||||
|
|
||||||
|
# set up packagekit
|
||||||
|
config.vm.provision "shell" do |shell|
|
||||||
|
shell.inline = <<-SCRIPT
|
||||||
|
dnf install -y PackageKit
|
||||||
|
systemctl enable packagekit
|
||||||
|
systemctl start packagekit
|
||||||
|
SCRIPT
|
||||||
|
end
|
||||||
|
|
||||||
|
# set up vagrant home
|
||||||
|
script = <<-SCRIPT
|
||||||
|
grep -q 'mgmt\.bashrc' ~/.bashrc || echo '. ~/.mgmt.bashrc' >>~/.bashrc
|
||||||
|
. ~/.mgmt.bashrc
|
||||||
|
go get -u github.com/purpleidea/mgmt
|
||||||
|
cd ~/gopath/src/github.com/purpleidea/mgmt
|
||||||
|
make deps
|
||||||
|
SCRIPT
|
||||||
|
config.vm.provision "shell" do |shell|
|
||||||
|
shell.privileged = false
|
||||||
|
shell.inline = script
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,18 +1,18 @@
|
|||||||
// Mgmt
|
// Mgmt
|
||||||
// Copyright (C) 2013-2016+ James Shubin and the project contributors
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// GNU Affero General Public License for more details.
|
// GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
// Package converger is a facility for reporting the converged state.
|
// Package converger is a facility for reporting the converged state.
|
||||||
@@ -29,24 +29,24 @@ import (
|
|||||||
// TODO: we could make a new function that masks out the state of certain
|
// TODO: we could make a new function that masks out the state of certain
|
||||||
// UID's, but at the moment the new Timer code has obsoleted the need...
|
// UID's, but at the moment the new Timer code has obsoleted the need...
|
||||||
|
|
||||||
// Converger is the general interface for implementing a convergence watcher
|
// Converger is the general interface for implementing a convergence watcher.
|
||||||
type Converger interface { // TODO: need a better name
|
type Converger interface { // TODO: need a better name
|
||||||
Register() ConvergerUID
|
Register() UID
|
||||||
IsConverged(ConvergerUID) bool // is the UID converged ?
|
IsConverged(UID) bool // is the UID converged ?
|
||||||
SetConverged(ConvergerUID, bool) error // set the converged state of the UID
|
SetConverged(UID, bool) error // set the converged state of the UID
|
||||||
Unregister(ConvergerUID)
|
Unregister(UID)
|
||||||
Start()
|
Start()
|
||||||
Pause()
|
Pause()
|
||||||
Loop(bool)
|
Loop(bool)
|
||||||
ConvergedTimer(ConvergerUID) <-chan time.Time
|
ConvergedTimer(UID) <-chan time.Time
|
||||||
Status() map[uint64]bool
|
Status() map[uint64]bool
|
||||||
Timeout() int // returns the timeout that this was created with
|
Timeout() int // returns the timeout that this was created with
|
||||||
SetStateFn(func(bool) error) // sets the stateFn
|
SetStateFn(func(bool) error) // sets the stateFn
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConvergerUID is the interface resources can use to notify with if converged
|
// UID is the interface resources can use to notify with if converged. You'll
|
||||||
// you'll need to use part of the Converger interface to Register initially too
|
// need to use part of the Converger interface to Register initially too.
|
||||||
type ConvergerUID interface {
|
type UID interface {
|
||||||
ID() uint64 // get Id
|
ID() uint64 // get Id
|
||||||
Name() string // get a friendly name
|
Name() string // get a friendly name
|
||||||
SetName(string)
|
SetName(string)
|
||||||
@@ -61,7 +61,7 @@ type ConvergerUID interface {
|
|||||||
StopTimer() error
|
StopTimer() error
|
||||||
}
|
}
|
||||||
|
|
||||||
// converger is an implementation of the Converger interface
|
// converger is an implementation of the Converger interface.
|
||||||
type converger struct {
|
type converger struct {
|
||||||
timeout int // must be zero (instant) or greater seconds to run
|
timeout int // must be zero (instant) or greater seconds to run
|
||||||
stateFn func(bool) error // run on converged state changes with state bool
|
stateFn func(bool) error // run on converged state changes with state bool
|
||||||
@@ -73,18 +73,19 @@ type converger struct {
|
|||||||
status map[uint64]bool
|
status map[uint64]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// convergerUID is an implementation of the ConvergerUID interface
|
// cuid is an implementation of the UID interface.
|
||||||
type convergerUID struct {
|
type cuid struct {
|
||||||
converger Converger
|
converger Converger
|
||||||
id uint64
|
id uint64
|
||||||
name string // user defined, friendly name
|
name string // user defined, friendly name
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
timer chan struct{}
|
timer chan struct{}
|
||||||
running bool // is the above timer running?
|
running bool // is the above timer running?
|
||||||
|
wg sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConverger builds a new converger struct
|
// NewConverger builds a new converger struct.
|
||||||
func NewConverger(timeout int, stateFn func(bool) error) *converger {
|
func NewConverger(timeout int, stateFn func(bool) error) Converger {
|
||||||
return &converger{
|
return &converger{
|
||||||
timeout: timeout,
|
timeout: timeout,
|
||||||
stateFn: stateFn,
|
stateFn: stateFn,
|
||||||
@@ -95,13 +96,13 @@ func NewConverger(timeout int, stateFn func(bool) error) *converger {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register assigns a ConvergerUID to the caller
|
// Register assigns a UID to the caller.
|
||||||
func (obj *converger) Register() ConvergerUID {
|
func (obj *converger) Register() UID {
|
||||||
obj.mutex.Lock()
|
obj.mutex.Lock()
|
||||||
defer obj.mutex.Unlock()
|
defer obj.mutex.Unlock()
|
||||||
obj.lastid++
|
obj.lastid++
|
||||||
obj.status[obj.lastid] = false // initialize as not converged
|
obj.status[obj.lastid] = false // initialize as not converged
|
||||||
return &convergerUID{
|
return &cuid{
|
||||||
converger: obj,
|
converger: obj,
|
||||||
id: obj.lastid,
|
id: obj.lastid,
|
||||||
name: fmt.Sprintf("%d", obj.lastid), // some default
|
name: fmt.Sprintf("%d", obj.lastid), // some default
|
||||||
@@ -110,28 +111,28 @@ func (obj *converger) Register() ConvergerUID {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsConverged gets the converged status of a uid
|
// IsConverged gets the converged status of a uid.
|
||||||
func (obj *converger) IsConverged(uid ConvergerUID) bool {
|
func (obj *converger) IsConverged(uid UID) bool {
|
||||||
if !uid.IsValid() {
|
if !uid.IsValid() {
|
||||||
panic(fmt.Sprintf("Id of ConvergerUID(%s) is nil!", uid.Name()))
|
panic(fmt.Sprintf("the ID of UID(%s) is nil", uid.Name()))
|
||||||
}
|
}
|
||||||
obj.mutex.RLock()
|
obj.mutex.RLock()
|
||||||
isConverged, found := obj.status[uid.ID()] // lookup
|
isConverged, found := obj.status[uid.ID()] // lookup
|
||||||
obj.mutex.RUnlock()
|
obj.mutex.RUnlock()
|
||||||
if !found {
|
if !found {
|
||||||
panic("Id of ConvergerUID is unregistered!")
|
panic("the ID of UID is unregistered")
|
||||||
}
|
}
|
||||||
return isConverged
|
return isConverged
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetConverged updates the converger with the converged state of the UID
|
// SetConverged updates the converger with the converged state of the UID.
|
||||||
func (obj *converger) SetConverged(uid ConvergerUID, isConverged bool) error {
|
func (obj *converger) SetConverged(uid UID, isConverged bool) error {
|
||||||
if !uid.IsValid() {
|
if !uid.IsValid() {
|
||||||
return fmt.Errorf("Id of ConvergerUID(%s) is nil!", uid.Name())
|
return fmt.Errorf("the ID of UID(%s) is nil", uid.Name())
|
||||||
}
|
}
|
||||||
obj.mutex.Lock()
|
obj.mutex.Lock()
|
||||||
if _, found := obj.status[uid.ID()]; !found {
|
if _, found := obj.status[uid.ID()]; !found {
|
||||||
panic("Id of ConvergerUID is unregistered!")
|
panic("the ID of UID is unregistered")
|
||||||
}
|
}
|
||||||
obj.status[uid.ID()] = isConverged // set
|
obj.status[uid.ID()] = isConverged // set
|
||||||
obj.mutex.Unlock() // unlock *before* poke or deadlock!
|
obj.mutex.Unlock() // unlock *before* poke or deadlock!
|
||||||
@@ -143,7 +144,7 @@ func (obj *converger) SetConverged(uid ConvergerUID, isConverged bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// isConverged returns true if *every* registered uid has converged
|
// isConverged returns true if *every* registered uid has converged.
|
||||||
func (obj *converger) isConverged() bool {
|
func (obj *converger) isConverged() bool {
|
||||||
obj.mutex.RLock() // take a read lock
|
obj.mutex.RLock() // take a read lock
|
||||||
defer obj.mutex.RUnlock()
|
defer obj.mutex.RUnlock()
|
||||||
@@ -155,10 +156,10 @@ func (obj *converger) isConverged() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unregister dissociates the ConvergedUID from the converged checking
|
// Unregister dissociates the ConvergedUID from the converged checking.
|
||||||
func (obj *converger) Unregister(uid ConvergerUID) {
|
func (obj *converger) Unregister(uid UID) {
|
||||||
if !uid.IsValid() {
|
if !uid.IsValid() {
|
||||||
panic(fmt.Sprintf("Id of ConvergerUID(%s) is nil!", uid.Name()))
|
panic(fmt.Sprintf("the ID of UID(%s) is nil", uid.Name()))
|
||||||
}
|
}
|
||||||
obj.mutex.Lock()
|
obj.mutex.Lock()
|
||||||
uid.StopTimer() // ignore any errors
|
uid.StopTimer() // ignore any errors
|
||||||
@@ -167,30 +168,30 @@ func (obj *converger) Unregister(uid ConvergerUID) {
|
|||||||
uid.InvalidateID()
|
uid.InvalidateID()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start causes a Converger object to start or resume running
|
// Start causes a Converger object to start or resume running.
|
||||||
func (obj *converger) Start() {
|
func (obj *converger) Start() {
|
||||||
obj.control <- true
|
obj.control <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pause causes a Converger object to stop running temporarily
|
// Pause causes a Converger object to stop running temporarily.
|
||||||
func (obj *converger) Pause() { // FIXME: add a sync ACK on pause before return
|
func (obj *converger) Pause() { // FIXME: add a sync ACK on pause before return
|
||||||
obj.control <- false
|
obj.control <- false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loop is the main loop for a Converger object; it usually runs in a goroutine
|
// Loop is the main loop for a Converger object. It usually runs in a goroutine.
|
||||||
// TODO: we could eventually have each resource tell us as soon as it converges
|
// TODO: we could eventually have each resource tell us as soon as it converges,
|
||||||
// and then keep track of the time delays here, to avoid callers needing select
|
// and then keep track of the time delays here, to avoid callers needing select.
|
||||||
// NOTE: when we have very short timeouts, if we start before all the resources
|
// NOTE: when we have very short timeouts, if we start before all the resources
|
||||||
// have joined the map, then it might appears as if we converged before we did!
|
// have joined the map, then it might appear as if we converged before we did!
|
||||||
func (obj *converger) Loop(startPaused bool) {
|
func (obj *converger) Loop(startPaused bool) {
|
||||||
if obj.control == nil {
|
if obj.control == nil {
|
||||||
panic("Converger not initialized correctly")
|
panic("converger not initialized correctly")
|
||||||
}
|
}
|
||||||
if startPaused { // start paused without racing
|
if startPaused { // start paused without racing
|
||||||
select {
|
select {
|
||||||
case e := <-obj.control:
|
case e := <-obj.control:
|
||||||
if !e {
|
if !e {
|
||||||
panic("Converger expected true!")
|
panic("converger expected true")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -198,13 +199,13 @@ func (obj *converger) Loop(startPaused bool) {
|
|||||||
select {
|
select {
|
||||||
case e := <-obj.control: // expecting "false" which means pause!
|
case e := <-obj.control: // expecting "false" which means pause!
|
||||||
if e {
|
if e {
|
||||||
panic("Converger expected false!")
|
panic("converger expected false")
|
||||||
}
|
}
|
||||||
// now i'm paused...
|
// now i'm paused...
|
||||||
select {
|
select {
|
||||||
case e := <-obj.control:
|
case e := <-obj.control:
|
||||||
if !e {
|
if !e {
|
||||||
panic("Converger expected true!")
|
panic("converger expected true")
|
||||||
}
|
}
|
||||||
// restart
|
// restart
|
||||||
// kick once to refresh the check...
|
// kick once to refresh the check...
|
||||||
@@ -243,9 +244,9 @@ func (obj *converger) Loop(startPaused bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConvergedTimer adds a timeout to a select call and blocks until then
|
// ConvergedTimer adds a timeout to a select call and blocks until then.
|
||||||
// TODO: this means we could eventually have per resource converged timeouts
|
// TODO: this means we could eventually have per resource converged timeouts
|
||||||
func (obj *converger) ConvergedTimer(uid ConvergerUID) <-chan time.Time {
|
func (obj *converger) ConvergedTimer(uid UID) <-chan time.Time {
|
||||||
// be clever: if i'm already converged, this timeout should block which
|
// be clever: if i'm already converged, this timeout should block which
|
||||||
// avoids unnecessary new signals being sent! this avoids fast loops if
|
// avoids unnecessary new signals being sent! this avoids fast loops if
|
||||||
// we have a low timeout, or in particular a timeout == 0
|
// we have a low timeout, or in particular a timeout == 0
|
||||||
@@ -279,63 +280,65 @@ func (obj *converger) SetStateFn(stateFn func(bool) error) {
|
|||||||
obj.stateFn = stateFn
|
obj.stateFn = stateFn
|
||||||
}
|
}
|
||||||
|
|
||||||
// Id returns the unique id of this UID object
|
// ID returns the unique id of this UID object.
|
||||||
func (obj *convergerUID) ID() uint64 {
|
func (obj *cuid) ID() uint64 {
|
||||||
return obj.id
|
return obj.id
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name returns a user defined name for the specific convergerUID.
|
// Name returns a user defined name for the specific cuid.
|
||||||
func (obj *convergerUID) Name() string {
|
func (obj *cuid) Name() string {
|
||||||
return obj.name
|
return obj.name
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetName sets a user defined name for the specific convergerUID.
|
// SetName sets a user defined name for the specific cuid.
|
||||||
func (obj *convergerUID) SetName(name string) {
|
func (obj *cuid) SetName(name string) {
|
||||||
obj.name = name
|
obj.name = name
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsValid tells us if the id is valid or has already been destroyed
|
// IsValid tells us if the id is valid or has already been destroyed.
|
||||||
func (obj *convergerUID) IsValid() bool {
|
func (obj *cuid) IsValid() bool {
|
||||||
return obj.id != 0 // an id of 0 is invalid
|
return obj.id != 0 // an id of 0 is invalid
|
||||||
}
|
}
|
||||||
|
|
||||||
// InvalidateID marks the id as no longer valid
|
// InvalidateID marks the id as no longer valid.
|
||||||
func (obj *convergerUID) InvalidateID() {
|
func (obj *cuid) InvalidateID() {
|
||||||
obj.id = 0 // an id of 0 is invalid
|
obj.id = 0 // an id of 0 is invalid
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsConverged is a helper function to the regular IsConverged method
|
// IsConverged is a helper function to the regular IsConverged method.
|
||||||
func (obj *convergerUID) IsConverged() bool {
|
func (obj *cuid) IsConverged() bool {
|
||||||
return obj.converger.IsConverged(obj)
|
return obj.converger.IsConverged(obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetConverged is a helper function to the regular SetConverged notification
|
// SetConverged is a helper function to the regular SetConverged notification.
|
||||||
func (obj *convergerUID) SetConverged(isConverged bool) error {
|
func (obj *cuid) SetConverged(isConverged bool) error {
|
||||||
return obj.converger.SetConverged(obj, isConverged)
|
return obj.converger.SetConverged(obj, isConverged)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unregister is a helper function to unregister myself
|
// Unregister is a helper function to unregister myself.
|
||||||
func (obj *convergerUID) Unregister() {
|
func (obj *cuid) Unregister() {
|
||||||
obj.converger.Unregister(obj)
|
obj.converger.Unregister(obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConvergedTimer is a helper around the regular ConvergedTimer method
|
// ConvergedTimer is a helper around the regular ConvergedTimer method.
|
||||||
func (obj *convergerUID) ConvergedTimer() <-chan time.Time {
|
func (obj *cuid) ConvergedTimer() <-chan time.Time {
|
||||||
return obj.converger.ConvergedTimer(obj)
|
return obj.converger.ConvergedTimer(obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartTimer runs an invisible timer that automatically converges on timeout.
|
// StartTimer runs an invisible timer that automatically converges on timeout.
|
||||||
func (obj *convergerUID) StartTimer() (func() error, error) {
|
func (obj *cuid) StartTimer() (func() error, error) {
|
||||||
obj.mutex.Lock()
|
obj.mutex.Lock()
|
||||||
if !obj.running {
|
if !obj.running {
|
||||||
obj.timer = make(chan struct{})
|
obj.timer = make(chan struct{})
|
||||||
obj.running = true
|
obj.running = true
|
||||||
} else {
|
} else {
|
||||||
obj.mutex.Unlock()
|
obj.mutex.Unlock()
|
||||||
return obj.StopTimer, fmt.Errorf("Timer already started!")
|
return obj.StopTimer, fmt.Errorf("timer already started")
|
||||||
}
|
}
|
||||||
obj.mutex.Unlock()
|
obj.mutex.Unlock()
|
||||||
|
obj.wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
|
defer obj.wg.Done()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case _, ok := <-obj.timer: // reset signal channel
|
case _, ok := <-obj.timer: // reset signal channel
|
||||||
@@ -359,24 +362,25 @@ func (obj *convergerUID) StartTimer() (func() error, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ResetTimer resets the counter to zero if using a StartTimer internally.
|
// ResetTimer resets the counter to zero if using a StartTimer internally.
|
||||||
func (obj *convergerUID) ResetTimer() error {
|
func (obj *cuid) ResetTimer() error {
|
||||||
obj.mutex.Lock()
|
obj.mutex.Lock()
|
||||||
defer obj.mutex.Unlock()
|
defer obj.mutex.Unlock()
|
||||||
if obj.running {
|
if obj.running {
|
||||||
obj.timer <- struct{}{} // send the reset message
|
obj.timer <- struct{}{} // send the reset message
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return fmt.Errorf("Timer hasn't been started!")
|
return fmt.Errorf("timer hasn't been started")
|
||||||
}
|
}
|
||||||
|
|
||||||
// StopTimer stops the running timer permanently until a StartTimer is run.
|
// StopTimer stops the running timer permanently until a StartTimer is run.
|
||||||
func (obj *convergerUID) StopTimer() error {
|
func (obj *cuid) StopTimer() error {
|
||||||
obj.mutex.Lock()
|
obj.mutex.Lock()
|
||||||
defer obj.mutex.Unlock()
|
defer obj.mutex.Unlock()
|
||||||
if !obj.running {
|
if !obj.running {
|
||||||
return fmt.Errorf("Timer isn't running!")
|
return fmt.Errorf("timer isn't running")
|
||||||
}
|
}
|
||||||
close(obj.timer)
|
close(obj.timer)
|
||||||
|
obj.wg.Wait()
|
||||||
obj.running = false
|
obj.running = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
8
doc.go
8
doc.go
@@ -1,18 +1,18 @@
|
|||||||
// Mgmt
|
// Mgmt
|
||||||
// Copyright (C) 2013-2016+ James Shubin and the project contributors
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// GNU Affero General Public License for more details.
|
// GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
// Package main provides the main entrypoint for using the `mgmt` software.
|
// Package main provides the main entrypoint for using the `mgmt` software.
|
||||||
|
|||||||
1
docs/.gitignore
vendored
1
docs/.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
mgmt-documentation.pdf
|
mgmt-documentation.pdf
|
||||||
|
_build
|
||||||
|
|||||||
20
docs/Makefile
Normal file
20
docs/Makefile
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# Minimal makefile for Sphinx documentation
|
||||||
|
#
|
||||||
|
|
||||||
|
# You can set these variables from the command line.
|
||||||
|
SPHINXOPTS =
|
||||||
|
SPHINXBUILD = sphinx-build
|
||||||
|
SPHINXPROJ = mgmt
|
||||||
|
SOURCEDIR = .
|
||||||
|
BUILDDIR = _build
|
||||||
|
|
||||||
|
# Put it first so that "make" without argument is like "make help".
|
||||||
|
help:
|
||||||
|
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
||||||
|
|
||||||
|
.PHONY: help Makefile
|
||||||
|
|
||||||
|
# Catch-all target: route all unknown targets to Sphinx using the new
|
||||||
|
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
|
||||||
|
%: Makefile
|
||||||
|
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
||||||
158
docs/conf.py
Normal file
158
docs/conf.py
Normal file
@@ -0,0 +1,158 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
#
|
||||||
|
# mgmt documentation build configuration file, created by
|
||||||
|
# sphinx-quickstart on Wed Feb 15 21:34:09 2017.
|
||||||
|
#
|
||||||
|
# This file is execfile()d with the current directory set to its
|
||||||
|
# containing dir.
|
||||||
|
#
|
||||||
|
# Note that not all possible configuration values are present in this
|
||||||
|
# autogenerated file.
|
||||||
|
#
|
||||||
|
# All configuration values have a default; values that are commented out
|
||||||
|
# serve to show the default.
|
||||||
|
|
||||||
|
# If extensions (or modules to document with autodoc) are in another directory,
|
||||||
|
# add these directories to sys.path here. If the directory is relative to the
|
||||||
|
# documentation root, use os.path.abspath to make it absolute, like shown here.
|
||||||
|
#
|
||||||
|
# import os
|
||||||
|
# import sys
|
||||||
|
# sys.path.insert(0, os.path.abspath('.'))
|
||||||
|
|
||||||
|
from recommonmark.parser import CommonMarkParser
|
||||||
|
|
||||||
|
# -- General configuration ------------------------------------------------
|
||||||
|
|
||||||
|
# If your documentation needs a minimal Sphinx version, state it here.
|
||||||
|
#
|
||||||
|
# needs_sphinx = '1.0'
|
||||||
|
|
||||||
|
# Add any Sphinx extension module names here, as strings. They can be
|
||||||
|
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
|
||||||
|
# ones.
|
||||||
|
extensions = []
|
||||||
|
|
||||||
|
# Add any paths that contain templates here, relative to this directory.
|
||||||
|
templates_path = ['_templates']
|
||||||
|
|
||||||
|
# The suffix(es) of source filenames.
|
||||||
|
# You can specify multiple suffix as a list of string:
|
||||||
|
#
|
||||||
|
|
||||||
|
source_parsers = {
|
||||||
|
'.md': CommonMarkParser,
|
||||||
|
}
|
||||||
|
|
||||||
|
source_suffix = ['.rst', '.md']
|
||||||
|
|
||||||
|
# The master toctree document.
|
||||||
|
master_doc = 'index'
|
||||||
|
|
||||||
|
# General information about the project.
|
||||||
|
project = u'mgmt'
|
||||||
|
copyright = u'2013-2017+ James Shubin and the project contributors'
|
||||||
|
author = u'James Shubin'
|
||||||
|
|
||||||
|
# The version info for the project you're documenting, acts as replacement for
|
||||||
|
# |version| and |release|, also used in various other places throughout the
|
||||||
|
# built documents.
|
||||||
|
#
|
||||||
|
# The short X.Y version.
|
||||||
|
version = u''
|
||||||
|
# The full version, including alpha/beta/rc tags.
|
||||||
|
release = u''
|
||||||
|
|
||||||
|
# The language for content autogenerated by Sphinx. Refer to documentation
|
||||||
|
# for a list of supported languages.
|
||||||
|
#
|
||||||
|
# This is also used if you do content translation via gettext catalogs.
|
||||||
|
# Usually you set "language" from the command line for these cases.
|
||||||
|
language = None
|
||||||
|
|
||||||
|
# List of patterns, relative to source directory, that match files and
|
||||||
|
# directories to ignore when looking for source files.
|
||||||
|
# This patterns also effect to html_static_path and html_extra_path
|
||||||
|
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', 'venv']
|
||||||
|
|
||||||
|
# The name of the Pygments (syntax highlighting) style to use.
|
||||||
|
pygments_style = 'sphinx'
|
||||||
|
|
||||||
|
# If true, `todo` and `todoList` produce output, else they produce nothing.
|
||||||
|
todo_include_todos = False
|
||||||
|
|
||||||
|
|
||||||
|
# -- Options for HTML output ----------------------------------------------
|
||||||
|
|
||||||
|
# The theme to use for HTML and HTML Help pages. See the documentation for
|
||||||
|
# a list of builtin themes.
|
||||||
|
#
|
||||||
|
#html_theme = 'alabaster'
|
||||||
|
|
||||||
|
# Theme options are theme-specific and customize the look and feel of a theme
|
||||||
|
# further. For a list of options available for each theme, see the
|
||||||
|
# documentation.
|
||||||
|
#
|
||||||
|
# html_theme_options = {}
|
||||||
|
|
||||||
|
# Add any paths that contain custom static files (such as style sheets) here,
|
||||||
|
# relative to this directory. They are copied after the builtin static files,
|
||||||
|
# so a file named "default.css" will overwrite the builtin "default.css".
|
||||||
|
html_static_path = ['_static']
|
||||||
|
|
||||||
|
|
||||||
|
# -- Options for HTMLHelp output ------------------------------------------
|
||||||
|
|
||||||
|
# Output file base name for HTML help builder.
|
||||||
|
htmlhelp_basename = 'mgmtdoc'
|
||||||
|
|
||||||
|
|
||||||
|
# -- Options for LaTeX output ---------------------------------------------
|
||||||
|
|
||||||
|
latex_elements = {
|
||||||
|
# The paper size ('letterpaper' or 'a4paper').
|
||||||
|
#
|
||||||
|
# 'papersize': 'letterpaper',
|
||||||
|
|
||||||
|
# The font size ('10pt', '11pt' or '12pt').
|
||||||
|
#
|
||||||
|
# 'pointsize': '10pt',
|
||||||
|
|
||||||
|
# Additional stuff for the LaTeX preamble.
|
||||||
|
#
|
||||||
|
# 'preamble': '',
|
||||||
|
|
||||||
|
# Latex figure (float) alignment
|
||||||
|
#
|
||||||
|
# 'figure_align': 'htbp',
|
||||||
|
}
|
||||||
|
|
||||||
|
# Grouping the document tree into LaTeX files. List of tuples
|
||||||
|
# (source start file, target name, title,
|
||||||
|
# author, documentclass [howto, manual, or own class]).
|
||||||
|
latex_documents = [
|
||||||
|
(master_doc, 'mgmt.tex', u'mgmt Documentation',
|
||||||
|
u'James Shubin', 'manual'),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
# -- Options for manual page output ---------------------------------------
|
||||||
|
|
||||||
|
# One entry per manual page. List of tuples
|
||||||
|
# (source start file, name, description, authors, manual section).
|
||||||
|
man_pages = [
|
||||||
|
(master_doc, 'mgmt', u'mgmt Documentation',
|
||||||
|
[author], 1)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
# -- Options for Texinfo output -------------------------------------------
|
||||||
|
|
||||||
|
# Grouping the document tree into Texinfo files. List of tuples
|
||||||
|
# (source start file, target name, title, author,
|
||||||
|
# dir menu entry, description, category)
|
||||||
|
texinfo_documents = [
|
||||||
|
(master_doc, 'mgmt', u'mgmt Documentation',
|
||||||
|
author, 'mgmt', 'A next generation config management prototype!',
|
||||||
|
'Miscellaneous'),
|
||||||
|
]
|
||||||
@@ -1,50 +1,9 @@
|
|||||||
# mgmt
|
# mgmt
|
||||||
|
|
||||||
<!--
|
Available from:
|
||||||
Mgmt
|
[https://github.com/purpleidea/mgmt/](https://github.com/purpleidea/mgmt/)
|
||||||
Copyright (C) 2013-2016+ James Shubin and the project contributors
|
|
||||||
Written by James Shubin <james@shubin.ca> and the project contributors
|
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
This documentation is available in: [Markdown](https://github.com/purpleidea/mgmt/blob/master/docs/documentation.md) or [PDF](https://pdfdoc-purpleidea.rhcloud.com/pdf/https://github.com/purpleidea/mgmt/blob/master/docs/documentation.md) format.
|
||||||
it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Affero General Public License
|
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
-->
|
|
||||||
|
|
||||||
##mgmt by [James](https://ttboj.wordpress.com/)
|
|
||||||
####Available from:
|
|
||||||
####[https://github.com/purpleidea/mgmt/](https://github.com/purpleidea/mgmt/)
|
|
||||||
|
|
||||||
####This documentation is available in: [Markdown](https://github.com/purpleidea/mgmt/blob/master/docs/documentation.md) or [PDF](https://pdfdoc-purpleidea.rhcloud.com/pdf/https://github.com/purpleidea/mgmt/blob/master/docs/documentation.md) format.
|
|
||||||
|
|
||||||
####Table of Contents
|
|
||||||
|
|
||||||
1. [Overview](#overview)
|
|
||||||
2. [Project description - What the project does](#project-description)
|
|
||||||
3. [Setup - Getting started with mgmt](#setup)
|
|
||||||
4. [Features - All things mgmt can do](#features)
|
|
||||||
* [Autoedges - Automatic resource relationships](#autoedges)
|
|
||||||
* [Autogrouping - Automatic resource grouping](#autogrouping)
|
|
||||||
* [Automatic clustering - Automatic cluster management](#automatic-clustering)
|
|
||||||
* [Remote mode - Remote "agent-less" execution](#remote-agent-less-mode)
|
|
||||||
* [Puppet support - write manifest code for mgmt](#puppet-support)
|
|
||||||
5. [Resources - All built-in primitives](#resources)
|
|
||||||
6. [Usage/FAQ - Notes on usage and frequently asked questions](#usage-and-frequently-asked-questions)
|
|
||||||
7. [Reference - Detailed reference](#reference)
|
|
||||||
* [Meta parameters](#meta-parameters)
|
|
||||||
* [Graph definition file](#graph-definition-file)
|
|
||||||
* [Command line](#command-line)
|
|
||||||
8. [Examples - Example configurations](#examples)
|
|
||||||
9. [Development - Background on module development and reporting bugs](#development)
|
|
||||||
10. [Authors - Authors and contact information](#authors)
|
|
||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|
||||||
@@ -63,6 +22,9 @@ For more information, you may like to read some blog posts from the author:
|
|||||||
* [Automatic edges in mgmt](https://ttboj.wordpress.com/2016/03/14/automatic-edges-in-mgmt/)
|
* [Automatic edges in mgmt](https://ttboj.wordpress.com/2016/03/14/automatic-edges-in-mgmt/)
|
||||||
* [Automatic grouping in mgmt](https://ttboj.wordpress.com/2016/03/30/automatic-grouping-in-mgmt/)
|
* [Automatic grouping in mgmt](https://ttboj.wordpress.com/2016/03/30/automatic-grouping-in-mgmt/)
|
||||||
* [Automatic clustering in mgmt](https://ttboj.wordpress.com/2016/06/20/automatic-clustering-in-mgmt/)
|
* [Automatic clustering in mgmt](https://ttboj.wordpress.com/2016/06/20/automatic-clustering-in-mgmt/)
|
||||||
|
* [Remote execution in mgmt](https://ttboj.wordpress.com/2016/10/07/remote-execution-in-mgmt/)
|
||||||
|
* [Send/Recv in mgmt](https://ttboj.wordpress.com/2016/12/07/sendrecv-in-mgmt/)
|
||||||
|
* [Metaparameters in mgmt](https://ttboj.wordpress.com/2017/03/01/metaparameters-in-mgmt/)
|
||||||
|
|
||||||
There is also an [introductory video](http://meetings-archive.debian.net/pub/debian-meetings/2016/debconf16/Next_Generation_Config_Mgmt.webm) available.
|
There is also an [introductory video](http://meetings-archive.debian.net/pub/debian-meetings/2016/debconf16/Next_Generation_Config_Mgmt.webm) available.
|
||||||
Older videos and other material [is available](https://github.com/purpleidea/mgmt/#on-the-web).
|
Older videos and other material [is available](https://github.com/purpleidea/mgmt/#on-the-web).
|
||||||
@@ -216,9 +178,11 @@ meta parameters aren't very useful when combined with certain resources, but
|
|||||||
in general, it should be fairly obvious, such as when combining the `noop` meta
|
in general, it should be fairly obvious, such as when combining the `noop` meta
|
||||||
parameter with the [Noop](#Noop) resource.
|
parameter with the [Noop](#Noop) resource.
|
||||||
|
|
||||||
|
* [Augeas](#Augeas): Manipulate files using augeas.
|
||||||
* [Exec](#Exec): Execute shell commands on the system.
|
* [Exec](#Exec): Execute shell commands on the system.
|
||||||
* [File](#File): Manage files and directories.
|
* [File](#File): Manage files and directories.
|
||||||
* [Hostname](#Hostname): Manages the hostname on the system.
|
* [Hostname](#Hostname): Manages the hostname on the system.
|
||||||
|
* [KV](#KV): Set a key value pair in our shared world database.
|
||||||
* [Msg](#Msg): Send log messages.
|
* [Msg](#Msg): Send log messages.
|
||||||
* [Noop](#Noop): A simple resource that does nothing.
|
* [Noop](#Noop): A simple resource that does nothing.
|
||||||
* [Nspawn](#Nspawn): Manage systemd-machined nspawn containers.
|
* [Nspawn](#Nspawn): Manage systemd-machined nspawn containers.
|
||||||
@@ -228,6 +192,12 @@ parameter with the [Noop](#Noop) resource.
|
|||||||
* [Timer](#Timer): Manage system systemd services.
|
* [Timer](#Timer): Manage system systemd services.
|
||||||
* [Virt](#Virt): Manage virtual machines with libvirt.
|
* [Virt](#Virt): Manage virtual machines with libvirt.
|
||||||
|
|
||||||
|
|
||||||
|
### Augeas
|
||||||
|
|
||||||
|
The augeas resource uses [augeas](http://augeas.net/) commands to manipulate
|
||||||
|
files.
|
||||||
|
|
||||||
### Exec
|
### Exec
|
||||||
|
|
||||||
The exec resource can execute commands on your system.
|
The exec resource can execute commands on your system.
|
||||||
@@ -237,6 +207,15 @@ The exec resource can execute commands on your system.
|
|||||||
The file resource manages files and directories. In `mgmt`, directories are
|
The file resource manages files and directories. In `mgmt`, directories are
|
||||||
identified by a trailing slash in their path name. File have no such slash.
|
identified by a trailing slash in their path name. File have no such slash.
|
||||||
|
|
||||||
|
It has the following properties:
|
||||||
|
|
||||||
|
- `path`: file path (directories have a trailing slash here)
|
||||||
|
- `content`: raw file content
|
||||||
|
- `state`: either `exists` (the default value) or `absent`
|
||||||
|
- `mode`: octal unix file permissions
|
||||||
|
- `owner`: username or uid for the file owner
|
||||||
|
- `group`: group name or gid for the file group
|
||||||
|
|
||||||
#### Path
|
#### Path
|
||||||
|
|
||||||
The path property specifies the file or directory that we are managing.
|
The path property specifies the file or directory that we are managing.
|
||||||
@@ -290,6 +269,30 @@ The pretty hostname is a free-form UTF8 host name for presentation to the user.
|
|||||||
Hostname is the fallback value for all 3 fields above, if only `hostname` is
|
Hostname is the fallback value for all 3 fields above, if only `hostname` is
|
||||||
specified, it will set all 3 fields to this value.
|
specified, it will set all 3 fields to this value.
|
||||||
|
|
||||||
|
### KV
|
||||||
|
|
||||||
|
The KV resource sets a key and value pair in the global world database. This is
|
||||||
|
quite useful for setting a flag after a number of resources have run. It will
|
||||||
|
ignore database updates to the value that are greater in compare order than the
|
||||||
|
requested key if the `SkipLessThan` parameter is set to true. If we receive a
|
||||||
|
refresh, then the stored value will be reset to the requested value even if the
|
||||||
|
stored value is greater.
|
||||||
|
|
||||||
|
#### Key
|
||||||
|
The string key used to store the key.
|
||||||
|
|
||||||
|
#### Value
|
||||||
|
The string value to set. This can also be set via Send/Recv.
|
||||||
|
|
||||||
|
#### SkipLessThan
|
||||||
|
If this parameter is set to `true`, then it will ignore updating the value as
|
||||||
|
long as the database versions are greater than the requested value. The compare
|
||||||
|
operation used is based on the `SkipCmpStyle` parameter.
|
||||||
|
|
||||||
|
#### SkipCmpStyle
|
||||||
|
By default this converts the string values to integers and compares them as you
|
||||||
|
would expect.
|
||||||
|
|
||||||
### Msg
|
### Msg
|
||||||
|
|
||||||
The msg resource sends messages to the main log, or an external service such
|
The msg resource sends messages to the main log, or an external service such
|
||||||
@@ -418,6 +421,7 @@ If you feel that a well used option needs documenting here, please patch it!
|
|||||||
* [Meta parameters](#meta-parameters): List of available resource meta parameters.
|
* [Meta parameters](#meta-parameters): List of available resource meta parameters.
|
||||||
* [Graph definition file](#graph-definition-file): Main graph definition file.
|
* [Graph definition file](#graph-definition-file): Main graph definition file.
|
||||||
* [Command line](#command-line): Command line parameters.
|
* [Command line](#command-line): Command line parameters.
|
||||||
|
* [Compilation options](#compilation-options): Compilation options.
|
||||||
|
|
||||||
### Meta parameters
|
### Meta parameters
|
||||||
These meta parameters are special parameters (or properties) which can apply to
|
These meta parameters are special parameters (or properties) which can apply to
|
||||||
@@ -449,6 +453,47 @@ they could have separate values, I've decided to use the same ones for both
|
|||||||
until there's a proper reason to want to do something differently for the Watch
|
until there's a proper reason to want to do something differently for the Watch
|
||||||
errors.
|
errors.
|
||||||
|
|
||||||
|
#### Poll
|
||||||
|
Integer. Number of seconds to wait between `CheckApply` checks. If this is
|
||||||
|
greater than zero, then the standard event based `Watch` mechanism for this
|
||||||
|
resource is replaced with a simple polling mechanism. In general, this is not
|
||||||
|
recommended, unless you have a very good reason for doing so.
|
||||||
|
|
||||||
|
Please keep in mind that if you have a resource which changes every `I` seconds,
|
||||||
|
and you poll it every `J` seconds, and you've asked for a converged timeout of
|
||||||
|
`K` seconds, and `I <= J <= K`, then your graph will likely never converge.
|
||||||
|
|
||||||
|
When polling, the system detects that a resource is not converged if its
|
||||||
|
`CheckApply` method returns false. This allows a resource which changes every
|
||||||
|
`I` seconds, and which is polled every `J` seconds, and with a converged timeout
|
||||||
|
of `K` seconds to still converge when `J <= K`, as long as `I > J || I > K`,
|
||||||
|
which is another way of saying that if the resource finally settles down to give
|
||||||
|
the graph enough time, it can probably converge.
|
||||||
|
|
||||||
|
#### Limit
|
||||||
|
Float. Maximum rate of `CheckApply` runs started per second. Useful to limit
|
||||||
|
an especially _eventful_ process from causing excessive checks to run. This
|
||||||
|
defaults to `+Infinity` which adds no limiting. If you change this value, you
|
||||||
|
will also need to change the `Burst` value to a non-zero value. Please see the
|
||||||
|
[rate](https://godoc.org/golang.org/x/time/rate) package for more information.
|
||||||
|
|
||||||
|
#### Burst
|
||||||
|
Integer. Burst is the maximum number of runs which can happen without invoking
|
||||||
|
the rate limiter as designated by the `Limit` value. If the `Limit` is not set
|
||||||
|
to `+Infinity`, this must be a non-zero value. Please see the
|
||||||
|
[rate](https://godoc.org/golang.org/x/time/rate) package for more information.
|
||||||
|
|
||||||
|
#### Sema
|
||||||
|
List of string ids. Sema is a P/V style counting semaphore which can be used to
|
||||||
|
limit parallelism during the CheckApply phase of resource execution. Each
|
||||||
|
resource can have `N` different semaphores which share a graph global namespace.
|
||||||
|
Each semaphore has a maximum count associated with it. The default value of the
|
||||||
|
size is 1 (one) if size is unspecified. Each string id is the unique id of the
|
||||||
|
semaphore. If the id contains a trailing colon (:) followed by a positive
|
||||||
|
integer, then that value is the max size for that semaphore. Valid semaphore
|
||||||
|
id's include: `some_id`, `hello:42`, `not:smart:4` and `:13`. It is expected
|
||||||
|
that the last bare example be only used by the engine to add a global semaphore.
|
||||||
|
|
||||||
### Graph definition file
|
### Graph definition file
|
||||||
graph.yaml is the compiled graph definition file. The format is currently
|
graph.yaml is the compiled graph definition file. The format is currently
|
||||||
undocumented, but by looking through the [examples/](https://github.com/purpleidea/mgmt/tree/master/examples)
|
undocumented, but by looking through the [examples/](https://github.com/purpleidea/mgmt/tree/master/examples)
|
||||||
@@ -473,6 +518,15 @@ Globally force all resources into no-op mode. This also disables the export to
|
|||||||
etcd functionality, but does not disable resource collection, however all
|
etcd functionality, but does not disable resource collection, however all
|
||||||
resources that are collected will have their individual noop settings set.
|
resources that are collected will have their individual noop settings set.
|
||||||
|
|
||||||
|
#### `--sema <size>`
|
||||||
|
Globally add a counting semaphore of this size to each resource in the graph.
|
||||||
|
The semaphore will get given an id of `:size`. In other words if you specify a
|
||||||
|
size of 42, you can expect a semaphore if named: `:42`. It is expected that
|
||||||
|
consumers of the semaphore metaparameter always include a prefix to avoid a
|
||||||
|
collision with this globally defined semaphore. The size value must be greater
|
||||||
|
than zero at this time. The traditional non-parallel execution found in config
|
||||||
|
management tools such as `Puppet` can be obtained with `--sema 1`.
|
||||||
|
|
||||||
#### `--remote <graph.yaml>`
|
#### `--remote <graph.yaml>`
|
||||||
Point to a graph file to run on the remote host specified within. This parameter
|
Point to a graph file to run on the remote host specified within. This parameter
|
||||||
can be used multiple times if you'd like to remotely run on multiple hosts in
|
can be used multiple times if you'd like to remotely run on multiple hosts in
|
||||||
@@ -513,6 +567,34 @@ like to try. The canonical example is when running `mgmt` with `--remote` there
|
|||||||
might be a cached copy of the binary in the primary prefix, but in case there's
|
might be a cached copy of the binary in the primary prefix, but in case there's
|
||||||
no binary available continue working in a temporary directory to avoid failure.
|
no binary available continue working in a temporary directory to avoid failure.
|
||||||
|
|
||||||
|
### Compilation options
|
||||||
|
|
||||||
|
You can control some compilation variables by using environment variables.
|
||||||
|
|
||||||
|
#### Disable libvirt support
|
||||||
|
|
||||||
|
If you wish to compile mgmt without libvirt, you can use the following command:
|
||||||
|
|
||||||
|
```
|
||||||
|
GOTAGS=novirt make build
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Disable augeas support
|
||||||
|
|
||||||
|
If you wish to compile mgmt without augeas support, you can use the following command:
|
||||||
|
|
||||||
|
```
|
||||||
|
GOTAGS=noaugeas make build
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Combining compile-time flags
|
||||||
|
|
||||||
|
You can combine multiple tags by using a space-separated list:
|
||||||
|
|
||||||
|
```
|
||||||
|
GOTAGS="noaugeas novirt" make build
|
||||||
|
```
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
For example configurations, please consult the [examples/](https://github.com/purpleidea/mgmt/tree/master/examples) directory in the git
|
For example configurations, please consult the [examples/](https://github.com/purpleidea/mgmt/tree/master/examples) directory in the git
|
||||||
source repository. It is available from:
|
source repository. It is available from:
|
||||||
@@ -553,7 +635,7 @@ To report any bugs, please file a ticket at: [https://github.com/purpleidea/mgmt
|
|||||||
|
|
||||||
## Authors
|
## Authors
|
||||||
|
|
||||||
Copyright (C) 2013-2016+ James Shubin and the project contributors
|
Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
|
|
||||||
Please see the
|
Please see the
|
||||||
[AUTHORS](https://github.com/purpleidea/mgmt/tree/master/AUTHORS) file
|
[AUTHORS](https://github.com/purpleidea/mgmt/tree/master/AUTHORS) file
|
||||||
|
|||||||
17
docs/index.rst
Normal file
17
docs/index.rst
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
.. mgmt documentation master file, created by
|
||||||
|
sphinx-quickstart on Wed Feb 15 21:34:09 2017.
|
||||||
|
You can adapt this file completely to your liking, but it should at least
|
||||||
|
contain the root `toctree` directive.
|
||||||
|
|
||||||
|
Welcome to mgmt's documentation!
|
||||||
|
================================
|
||||||
|
|
||||||
|
.. toctree::
|
||||||
|
:maxdepth: 2
|
||||||
|
:caption: Contents:
|
||||||
|
|
||||||
|
documentation
|
||||||
|
quick-start-guide
|
||||||
|
resource-guide
|
||||||
|
prometheus
|
||||||
|
puppet-guide
|
||||||
66
docs/prometheus.md
Normal file
66
docs/prometheus.md
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
# Prometheus support
|
||||||
|
|
||||||
|
Mgmt comes with a built-in prometheus support. It is disabled by default, and
|
||||||
|
can be enabled with the `--prometheus` command line switch.
|
||||||
|
|
||||||
|
By default, the prometheus instance will listen on [`127.0.0.1:9233`][pd]. You
|
||||||
|
can change this setting by using the `--prometheus-listen` cli option:
|
||||||
|
|
||||||
|
To have mgmt prometheus bind interface on 0.0.0.0:45001, use:
|
||||||
|
`./mgmt r --prometheus --prometheus-listen :45001`
|
||||||
|
|
||||||
|
## Metrics
|
||||||
|
|
||||||
|
Mgmt exposes three kinds of resources: _go_ metrics, _etcd_ metrics and _mgmt_
|
||||||
|
metrics.
|
||||||
|
|
||||||
|
### go metrics
|
||||||
|
|
||||||
|
We use the [prometheus go_collector][pgc] to expose go metrics. Those metrics
|
||||||
|
are mainly useful for debugging and perf testing.
|
||||||
|
|
||||||
|
### etcd metrics
|
||||||
|
|
||||||
|
mgmt exposes etcd metrics. Read more in the [upstream documentation][etcdm]
|
||||||
|
|
||||||
|
### mgmt metrics
|
||||||
|
|
||||||
|
Here is a list of the metrics we provide:
|
||||||
|
|
||||||
|
- `mgmt_resources_total`: The number of resources that mgmt is managing
|
||||||
|
- `mgmt_checkapply_total`: The number of CheckApply's that mgmt has run
|
||||||
|
- `mgmt_failures_total`: The number of resources that have failed
|
||||||
|
- `mgmt_failures_current`: The number of resources that have failed
|
||||||
|
- `mgmt_graph_start_time_seconds`: Start time of the current graph since unix epoch in seconds
|
||||||
|
|
||||||
|
For each metric, you will get some extra labels:
|
||||||
|
|
||||||
|
- `kind`: The kind of mgmt resource
|
||||||
|
|
||||||
|
For `mgmt_checkapply_total`, those extra labels are set:
|
||||||
|
|
||||||
|
- `eventful`: "true" or "false", if the CheckApply triggered some changes
|
||||||
|
- `errorful`: "true" or "false", if the CheckApply reported an error
|
||||||
|
- `apply`: "true" or "false", if the CheckApply ran in apply or noop mode
|
||||||
|
|
||||||
|
## Alerting
|
||||||
|
|
||||||
|
You can use prometheus to alert you upon changes or failures. We do not provide
|
||||||
|
such templates yet, but we plan to provide some examples in this repository.
|
||||||
|
Patches welcome!
|
||||||
|
|
||||||
|
## Grafana
|
||||||
|
|
||||||
|
We do not have grafana dashboards yet. Patches welcome!
|
||||||
|
|
||||||
|
## External resources
|
||||||
|
|
||||||
|
- [prometheus website](https://prometheus.io/)
|
||||||
|
- [prometheus documentation](https://prometheus.io/docs/introduction/overview/)
|
||||||
|
- [prometheus best practices regarding metrics
|
||||||
|
naming](https://prometheus.io/docs/practices/naming/)
|
||||||
|
- [grafana website](http://grafana.org/)
|
||||||
|
|
||||||
|
[pgc]: https://github.com/prometheus/client_golang/blob/master/prometheus/go_collector.go
|
||||||
|
[etcdm]: https://coreos.com/etcd/docs/latest/metrics.html
|
||||||
|
[pd]: https://github.com/prometheus/prometheus/wiki/Default-port-allocation
|
||||||
@@ -1,13 +1,4 @@
|
|||||||
#mgmt Puppet support
|
# Puppet guide
|
||||||
|
|
||||||
1. [Prerequisites](#prerequisites)
|
|
||||||
* [Testing the Puppet side](#testing-the-puppet-side)
|
|
||||||
2. [Writing a suitable manifest](#writing-a-suitable-manifest)
|
|
||||||
* [Unsupported attributes](#unsupported-attributes)
|
|
||||||
* [Unsupported resources](#unsupported-resources)
|
|
||||||
* [Avoiding common warnings](#avoiding-common-warnings)
|
|
||||||
3. [Configuring Puppet](#configuring-puppet)
|
|
||||||
4. [Caveats](#caveats)
|
|
||||||
|
|
||||||
`mgmt` can use Puppet as its source for the configuration graph.
|
`mgmt` can use Puppet as its source for the configuration graph.
|
||||||
This document goes into detail on how this works, and lists
|
This document goes into detail on how this works, and lists
|
||||||
@@ -29,7 +20,9 @@ Any release of Puppet's 3.x and 4.x series should be suitable for use with
|
|||||||
`mgmt`. Most importantly, make sure to install the `ffrank-mgmtgraph` Puppet
|
`mgmt`. Most importantly, make sure to install the `ffrank-mgmtgraph` Puppet
|
||||||
module (referred to below as "the translator module").
|
module (referred to below as "the translator module").
|
||||||
|
|
||||||
|
```
|
||||||
puppet module install ffrank-mgmtgraph
|
puppet module install ffrank-mgmtgraph
|
||||||
|
```
|
||||||
|
|
||||||
Please note that the module is not required on your Puppet master (if you
|
Please note that the module is not required on your Puppet master (if you
|
||||||
use a master/agent setup). It's needed on the machine that runs `mgmt`.
|
use a master/agent setup). It's needed on the machine that runs `mgmt`.
|
||||||
@@ -62,8 +55,10 @@ For example, at the time of writing this, the `file` type in `mgmt` had no
|
|||||||
notion of permissions (the file `mode`) yet. This lead to the following
|
notion of permissions (the file `mode`) yet. This lead to the following
|
||||||
warning (among others that will be discussed below):
|
warning (among others that will be discussed below):
|
||||||
|
|
||||||
|
```
|
||||||
$ puppet mgmtgraph print --code 'file { "/tmp/foo": mode => "0600" }'
|
$ puppet mgmtgraph print --code 'file { "/tmp/foo": mode => "0600" }'
|
||||||
Warning: cannot translate: File[/tmp/foo] { mode => "600" } (attribute is ignored)
|
Warning: cannot translate: File[/tmp/foo] { mode => "600" } (attribute is ignored)
|
||||||
|
```
|
||||||
|
|
||||||
This is a heads-up for the user, because the resulting `mgmt` graph will
|
This is a heads-up for the user, because the resulting `mgmt` graph will
|
||||||
in fact not pass this information to the `/tmp/foo` file resource, and
|
in fact not pass this information to the `/tmp/foo` file resource, and
|
||||||
@@ -98,8 +93,10 @@ the translator module just ignores them. However, there are cases in which
|
|||||||
Puppet will default to convenient behavior that `mgmt` cannot quite replicate.
|
Puppet will default to convenient behavior that `mgmt` cannot quite replicate.
|
||||||
For example, translating a plain `file` resource will lead to a warning message:
|
For example, translating a plain `file` resource will lead to a warning message:
|
||||||
|
|
||||||
|
```
|
||||||
$ puppet mgmtgraph print --code 'file { "/tmp/mgmt-test": }'
|
$ puppet mgmtgraph print --code 'file { "/tmp/mgmt-test": }'
|
||||||
Warning: File[/tmp/mgmt-test] uses the 'puppet' file bucket, which mgmt cannot do. There will be no backup copies!
|
Warning: File[/tmp/mgmt-test] uses the 'puppet' file bucket, which mgmt cannot do. There will be no backup copies!
|
||||||
|
```
|
||||||
|
|
||||||
The reason is that per default, Puppet assumes the following parameter value
|
The reason is that per default, Puppet assumes the following parameter value
|
||||||
(among others)
|
(among others)
|
||||||
@@ -112,7 +109,9 @@ file { "/tmp/mgmt-test":
|
|||||||
|
|
||||||
To avoid this, specify the parameter explicitly:
|
To avoid this, specify the parameter explicitly:
|
||||||
|
|
||||||
|
```
|
||||||
$ puppet mgmtgraph print --code 'file { "/tmp/mgmt-test": backup => false }'
|
$ puppet mgmtgraph print --code 'file { "/tmp/mgmt-test": backup => false }'
|
||||||
|
```
|
||||||
|
|
||||||
This is tedious in a more complex manifest. A good simplification is the
|
This is tedious in a more complex manifest. A good simplification is the
|
||||||
following [resource default](https://docs.puppet.com/puppet/latest/reference/lang_defaults.html)
|
following [resource default](https://docs.puppet.com/puppet/latest/reference/lang_defaults.html)
|
||||||
@@ -143,14 +142,18 @@ control all of them, through its `--puppet-conf` option. It allows
|
|||||||
you to specify which `puppet.conf` file should be used during
|
you to specify which `puppet.conf` file should be used during
|
||||||
translation.
|
translation.
|
||||||
|
|
||||||
|
```
|
||||||
mgmt run --puppet /opt/my-manifest.pp --puppet-conf /etc/mgmt/puppet.conf
|
mgmt run --puppet /opt/my-manifest.pp --puppet-conf /etc/mgmt/puppet.conf
|
||||||
|
```
|
||||||
|
|
||||||
Within this file, you can just specify any needed options in the
|
Within this file, you can just specify any needed options in the
|
||||||
`[main]` section:
|
`[main]` section:
|
||||||
|
|
||||||
|
```
|
||||||
[main]
|
[main]
|
||||||
server=mgmt-master.example.net
|
server=mgmt-master.example.net
|
||||||
vardir=/var/lib/mgmt/puppet
|
vardir=/var/lib/mgmt/puppet
|
||||||
|
```
|
||||||
|
|
||||||
## Caveats
|
## Caveats
|
||||||
|
|
||||||
94
docs/quick-start-guide.md
Normal file
94
docs/quick-start-guide.md
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
# Quick start guide
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
This guide is intended for developers. Once `mgmt` is minimally viable, we'll
|
||||||
|
publish a quick start guide for users too. In the meantime, please contribute!
|
||||||
|
If you're brand new to `mgmt`, it's probably a good idea to start by reading the
|
||||||
|
[introductory article](https://ttboj.wordpress.com/2016/01/18/next-generation-configuration-mgmt/)
|
||||||
|
or to watch an [introductory video](https://github.com/purpleidea/mgmt/#on-the-web).
|
||||||
|
Once you're familiar with the general idea, please start hacking...
|
||||||
|
|
||||||
|
## Vagrant
|
||||||
|
If you would like to avoid doing the following steps manually, we have prepared
|
||||||
|
a [Vagrant](https://www.vagrantup.com/) environment for your convenience. From
|
||||||
|
the project directory, run a `vagrant up`, and then a `vagrant status`. From
|
||||||
|
there, you can `vagrant ssh` into the `mgmt` machine. The MOTD will explain the
|
||||||
|
rest.
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
Software projects have a few different kinds of dependencies. There are _build_
|
||||||
|
dependencies, _runtime_ dependencies, and additionally, a few extra dependencies
|
||||||
|
required for running the _test_ suite.
|
||||||
|
|
||||||
|
### Build
|
||||||
|
* `golang` 1.8 or higher (required, available in some distros and distributed
|
||||||
|
as a binary officially by [golang.org](https://golang.org/dl/))
|
||||||
|
* golang libraries (required, available with `go get ./...`) a partial list includes:
|
||||||
|
```
|
||||||
|
github.com/coreos/etcd/client
|
||||||
|
gopkg.in/yaml.v2
|
||||||
|
gopkg.in/fsnotify.v1
|
||||||
|
github.com/urfave/cli
|
||||||
|
github.com/coreos/go-systemd/dbus
|
||||||
|
github.com/coreos/go-systemd/util
|
||||||
|
github.com/libvirt/libvirt-go
|
||||||
|
```
|
||||||
|
* `stringer` (optional), available as a package on some platforms, otherwise via `go get`
|
||||||
|
```
|
||||||
|
golang.org/x/tools/cmd/stringer
|
||||||
|
```
|
||||||
|
* `pandoc` (optional), for building a pdf of the documentation
|
||||||
|
|
||||||
|
### Runtime
|
||||||
|
A relatively modern GNU/Linux system should be able to run `mgmt` without any
|
||||||
|
problems. Since `mgmt` runs as a single statically compiled binary, all of the
|
||||||
|
library dependencies are included. It is expected, that certain advanced
|
||||||
|
resources require host specific facilities to work. These requirements are
|
||||||
|
listed below:
|
||||||
|
|
||||||
|
| Resource | Dependency | Version |
|
||||||
|
|----------|-------------------|---------|
|
||||||
|
| file | inotify | ? |
|
||||||
|
| hostname | systemd-hostnamed | ? |
|
||||||
|
| nspawn | systemd-nspawn | ? |
|
||||||
|
| pkg | packagekitd | ? |
|
||||||
|
| svc | systemd | ? |
|
||||||
|
| virt | libvirtd | ? |
|
||||||
|
|
||||||
|
For building a visual representation of the graph, `graphviz` is required.
|
||||||
|
|
||||||
|
### Testing
|
||||||
|
* golint `github.com/golang/lint/golint`
|
||||||
|
|
||||||
|
## Quick start
|
||||||
|
* Make sure you have golang version 1.8 or greater installed.
|
||||||
|
* If you do not have a GOPATH yet, create one and export it:
|
||||||
|
```
|
||||||
|
mkdir $HOME/gopath
|
||||||
|
export GOPATH=$HOME/gopath
|
||||||
|
```
|
||||||
|
* You might also want to add the GOPATH to your `~/.bashrc` or `~/.profile`.
|
||||||
|
* For more information you can read the [GOPATH documentation](https://golang.org/cmd/go/#hdr-GOPATH_environment_variable).
|
||||||
|
* Next download the mgmt code base, and switch to that directory:
|
||||||
|
```
|
||||||
|
mkdir -p $GOPATH/src/github.com/purpleidea/
|
||||||
|
cd $GOPATH/src/github.com/purpleidea/
|
||||||
|
git clone --recursive https://github.com/purpleidea/mgmt/
|
||||||
|
cd $GOPATH/src/github.com/purpleidea/mgmt
|
||||||
|
```
|
||||||
|
* Run `make deps` to install system and golang dependencies. Take a look at `misc/make-deps.sh` for details.
|
||||||
|
* Run `make build` to get a freshly built `mgmt` binary.
|
||||||
|
* Run `time ./mgmt run --yaml examples/graph0.yaml --converged-timeout=5 --tmp-prefix` to try out a very simple example!
|
||||||
|
* To run continuously in the default mode of operation, omit the `--converged-timeout` option.
|
||||||
|
* Have fun hacking on our future technology!
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
Please look in the [examples/](../examples/) folder for some examples!
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
Installation of `mgmt` from distribution packages currently needs improvement.
|
||||||
|
At the moment we have:
|
||||||
|
* [COPR](https://copr.fedoraproject.org/coprs/purpleidea/mgmt/)
|
||||||
|
* [Arch](https://aur.archlinux.org/packages/mgmt/)
|
||||||
|
|
||||||
|
Please contribute more! We'd especially like to see a Debian package!
|
||||||
@@ -1,47 +1,4 @@
|
|||||||
#mgmt
|
# Resource guide
|
||||||
|
|
||||||
<!--
|
|
||||||
Mgmt
|
|
||||||
Copyright (C) 2013-2016+ James Shubin and the project contributors
|
|
||||||
Written by James Shubin <james@shubin.ca> and the project contributors
|
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Affero General Public License
|
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
-->
|
|
||||||
|
|
||||||
##mgmt resource guide by [James](https://ttboj.wordpress.com/)
|
|
||||||
####Available from:
|
|
||||||
####[https://github.com/purpleidea/mgmt/](https://github.com/purpleidea/mgmt/)
|
|
||||||
|
|
||||||
####This documentation is available in: [Markdown](https://github.com/purpleidea/mgmt/blob/master/docs/resource-guide.md) or [PDF](https://pdfdoc-purpleidea.rhcloud.com/pdf/https://github.com/purpleidea/mgmt/blob/master/docs/resource-guide.md) format.
|
|
||||||
|
|
||||||
####Table of Contents
|
|
||||||
|
|
||||||
1. [Overview](#overview)
|
|
||||||
2. [Theory - Resource theory in mgmt](#theory)
|
|
||||||
3. [Resource API - Getting started with mgmt](#resource-api)
|
|
||||||
* [Init - Initialize the resource](#init)
|
|
||||||
* [CheckApply - Check and apply resource state](#checkapply)
|
|
||||||
* [Watch - Detect resource changes](#watch)
|
|
||||||
* [Compare - Compare resource with another](#compare)
|
|
||||||
4. [Further considerations - More information about resource writing](#further-considerations)
|
|
||||||
5. [Automatic edges - Adding automatic resources dependencies](#automatic-edges)
|
|
||||||
6. [Automatic grouping - Grouping multiple resources into one](#automatic-grouping)
|
|
||||||
7. [Send/Recv - Communication between resources](#send-recv)
|
|
||||||
8. [Composite resources - Importing code from one resource into another](#composite-resources)
|
|
||||||
9. [FAQ - Frequently asked questions](#frequently-asked-questions)
|
|
||||||
10. [Suggestions - API change suggestions](#suggestions)
|
|
||||||
11. [Authors - Authors and contact information](#authors)
|
|
||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|
||||||
@@ -69,20 +26,60 @@ To implement a resource in `mgmt` it must satisfy the
|
|||||||
interface. What follows are each of the method signatures and a description of
|
interface. What follows are each of the method signatures and a description of
|
||||||
each.
|
each.
|
||||||
|
|
||||||
|
### Default
|
||||||
|
```golang
|
||||||
|
Default() Res
|
||||||
|
```
|
||||||
|
|
||||||
|
This returns a populated resource struct as a `Res`. It shouldn't populate any
|
||||||
|
values which already have the correct default as the golang zero value. In
|
||||||
|
general it is preferable if the zero values make for the correct defaults.
|
||||||
|
|
||||||
|
#### Example
|
||||||
|
```golang
|
||||||
|
// Default returns some sensible defaults for this resource.
|
||||||
|
func (obj *FooRes) Default() Res {
|
||||||
|
return &FooRes{
|
||||||
|
Answer: 42, // sometimes, defaults shouldn't be the zero value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Validate
|
||||||
|
```golang
|
||||||
|
Validate() error
|
||||||
|
```
|
||||||
|
|
||||||
|
This method is used to validate if the populated resource struct is a valid
|
||||||
|
representation of the resource kind. If it does not conform to the resource
|
||||||
|
specifications, it should generate an error. If you notice that this method is
|
||||||
|
quite large, it might be an indication that you should reconsider the parameter
|
||||||
|
list and interface to this resource. This method is called _before_ `Init`.
|
||||||
|
|
||||||
|
#### Example
|
||||||
|
```golang
|
||||||
|
// Validate reports any problems with the struct definition.
|
||||||
|
func (obj *FooRes) Validate() error {
|
||||||
|
if obj.Answer != 42 { // validate whatever you want
|
||||||
|
return fmt.Errorf("expected an answer of 42")
|
||||||
|
}
|
||||||
|
return obj.BaseRes.Validate() // remember to call the base method!
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Init
|
### Init
|
||||||
```golang
|
```golang
|
||||||
Init() error
|
Init() error
|
||||||
```
|
```
|
||||||
|
|
||||||
This is called to initialize the resource. If something goes wrong, it should
|
This is called to initialize the resource. If something goes wrong, it should
|
||||||
return an error. It should set the resource `kind`, do any resource specific
|
return an error. It should do any resource specific work, and finish by calling
|
||||||
work, and finish by calling the `Init` method of the base resource.
|
the `Init` method of the base resource.
|
||||||
|
|
||||||
#### Example
|
#### Example
|
||||||
```golang
|
```golang
|
||||||
// Init initializes the Foo resource.
|
// Init initializes the Foo resource.
|
||||||
func (obj *FooRes) Init() error {
|
func (obj *FooRes) Init() error {
|
||||||
obj.BaseRes.kind = "Foo" // must set capitalized resource kind
|
|
||||||
// run the resource specific initialization, and error if anything fails
|
// run the resource specific initialization, and error if anything fails
|
||||||
if some_error {
|
if some_error {
|
||||||
return err // something went wrong!
|
return err // something went wrong!
|
||||||
@@ -91,6 +88,42 @@ func (obj *FooRes) Init() error {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
This method is always called after `Validate` has run successfully, with the
|
||||||
|
exception that we can't prevent a malicious or buggy `libmgmt` user to not run
|
||||||
|
this. In other words, you should expect `Validate` to have run first, but you
|
||||||
|
shouldn't allow `Init` to dangerously `rm -rf /$the_world` if your code only
|
||||||
|
checks `$the_world` in `Validate`. Remember to always program safely!
|
||||||
|
|
||||||
|
### Close
|
||||||
|
```golang
|
||||||
|
Close() error
|
||||||
|
```
|
||||||
|
|
||||||
|
This is called to cleanup after the resource. It is usually not necessary, but
|
||||||
|
can be useful if you'd like to properly close a persistent connection that you
|
||||||
|
opened in the `Init` method and were using throughout the resource.
|
||||||
|
|
||||||
|
#### Example
|
||||||
|
```golang
|
||||||
|
// Close runs some cleanup code for this resource.
|
||||||
|
func (obj *FooRes) Close() error {
|
||||||
|
err := obj.conn.Close() // close some internal connection
|
||||||
|
|
||||||
|
// call base close, b/c we're overriding
|
||||||
|
if e := obj.BaseRes.Close(); err == nil {
|
||||||
|
err = e
|
||||||
|
} else if e != nil {
|
||||||
|
err = multierr.Append(err, e) // list of errors
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
You should probably check the return errors of your internal methods, and pass
|
||||||
|
on an error if something went wrong. Remember to always call the base `Close`
|
||||||
|
method! If you plan to return early if you hit an internal error, then at least
|
||||||
|
call it with a defer!
|
||||||
|
|
||||||
### CheckApply
|
### CheckApply
|
||||||
```golang
|
```golang
|
||||||
CheckApply(apply bool) (checkOK bool, err error)
|
CheckApply(apply bool) (checkOK bool, err error)
|
||||||
@@ -168,12 +201,12 @@ will likely find the state to now be correct.
|
|||||||
|
|
||||||
### Watch
|
### Watch
|
||||||
```golang
|
```golang
|
||||||
Watch(chan Event) error
|
Watch() error
|
||||||
```
|
```
|
||||||
|
|
||||||
`Watch` is a main loop that runs and sends messages when it detects that the
|
`Watch` is a main loop that runs and sends messages when it detects that the
|
||||||
state of the resource might have changed. To send a message you should write to
|
state of the resource might have changed. To send a message you should write to
|
||||||
the input `Event` channel using the `DoSend` helper method. The Watch function
|
the input event channel using the `Event` helper method. The Watch function
|
||||||
should run continuously until a shutdown message is received. If at any time
|
should run continuously until a shutdown message is received. If at any time
|
||||||
something goes wrong, you should return an error, and the `mgmt` engine will
|
something goes wrong, you should return an error, and the `mgmt` engine will
|
||||||
handle possibly restarting the main loop based on the `retry` meta parameters.
|
handle possibly restarting the main loop based on the `retry` meta parameters.
|
||||||
@@ -200,15 +233,14 @@ The lifetime of most resources `Watch` method should be spent in an infinite
|
|||||||
loop that is bounded by a `select` call. The `select` call is the point where
|
loop that is bounded by a `select` call. The `select` call is the point where
|
||||||
our method hands back control to the engine (and the kernel) so that we can
|
our method hands back control to the engine (and the kernel) so that we can
|
||||||
sleep until something of interest wakes us up. In this loop we must process
|
sleep until something of interest wakes us up. In this loop we must process
|
||||||
events from the engine via the `<-obj.Events()` call, wait for the converged
|
events from the engine via the `<-obj.Events()` call, and receive events for our
|
||||||
timeout with `<-cuid.ConvergedTimer()`, and receive events for our resource
|
resource itself!
|
||||||
itself!
|
|
||||||
|
|
||||||
#### Events
|
#### Events
|
||||||
If we receive an internal event from the `<-obj.Events()` method, we can read it
|
If we receive an internal event from the `<-obj.Events()` method, we can read it
|
||||||
with the ReadEvent helper function. This function tells us if we should shutdown
|
with the ReadEvent helper function. This function tells us if we should shutdown
|
||||||
our resource, and if we should generate an event. When we want to send an event,
|
our resource, and if we should generate an event. When we want to send an event,
|
||||||
we use the `DoSend` helper function. It is also important to mark the resource
|
we use the `Event` helper function. It is also important to mark the resource
|
||||||
state as `dirty` if we believe it might have changed. We do this with the
|
state as `dirty` if we believe it might have changed. We do this with the
|
||||||
`StateOK(false)` function.
|
`StateOK(false)` function.
|
||||||
|
|
||||||
@@ -221,10 +253,10 @@ or from before `mgmt` was running. It does this by calling the `Running` method.
|
|||||||
|
|
||||||
#### Converged
|
#### Converged
|
||||||
The engine might be asked to shutdown when the entire state of the system has
|
The engine might be asked to shutdown when the entire state of the system has
|
||||||
not seen any changes for some duration of time. In order for the engine to be
|
not seen any changes for some duration of time. The engine can determine this
|
||||||
able to make this determination, each resource must report its converged state.
|
automatically, but each resource can block this if it is absolutely necessary.
|
||||||
To do this, the `Watch` method should get the `ConvergedUID` handle that has
|
To do this, the `Watch` method should get the `ConvergedUID` handle that has
|
||||||
been prepared for it by the engine. This is done by calling the `Converger`
|
been prepared for it by the engine. This is done by calling the `ConvergerUID`
|
||||||
method on the resource object. The result can be used to set the converged
|
method on the resource object. The result can be used to set the converged
|
||||||
status with `SetConverged`, and to notify when the particular timeout has been
|
status with `SetConverged`, and to notify when the particular timeout has been
|
||||||
reached by waiting on `ConvergedTimer`.
|
reached by waiting on `ConvergedTimer`.
|
||||||
@@ -233,12 +265,14 @@ Instead of interacting with the `ConvergedUID` with these two methods, we can
|
|||||||
instead use the `StartTimer` and `ResetTimer` methods which accomplish the same
|
instead use the `StartTimer` and `ResetTimer` methods which accomplish the same
|
||||||
thing, but provide a `select`-free interface for different coding situations.
|
thing, but provide a `select`-free interface for different coding situations.
|
||||||
|
|
||||||
|
This particular facility is most likely not required for most resources. It may
|
||||||
|
prove to be useful if a resource wants to start off a long operation, but avoid
|
||||||
|
sending out erroneous `Event` messages to keep things alive until it finishes.
|
||||||
|
|
||||||
#### Example
|
#### Example
|
||||||
```golang
|
```golang
|
||||||
// Watch is the listener and main loop for this resource.
|
// Watch is the listener and main loop for this resource.
|
||||||
func (obj *FooRes) Watch(processChan chan event.Event) error {
|
func (obj *FooRes) Watch() error {
|
||||||
cuid := obj.Converger() // get the converger uid used to report status
|
|
||||||
|
|
||||||
// setup the Foo resource
|
// setup the Foo resource
|
||||||
var err error
|
var err error
|
||||||
if err, obj.foo = OpenFoo(); err != nil {
|
if err, obj.foo = OpenFoo(); err != nil {
|
||||||
@@ -247,46 +281,36 @@ func (obj *FooRes) Watch(processChan chan event.Event) error {
|
|||||||
defer obj.whatever.CloseFoo() // shutdown our
|
defer obj.whatever.CloseFoo() // shutdown our
|
||||||
|
|
||||||
// notify engine that we're running
|
// notify engine that we're running
|
||||||
if err := obj.Running(processChan); err != nil {
|
if err := obj.Running(); err != nil {
|
||||||
return err // bubble up a NACK...
|
return err // bubble up a NACK...
|
||||||
}
|
}
|
||||||
|
|
||||||
var send = false // send event?
|
var send = false // send event?
|
||||||
var exit = false
|
var exit *error
|
||||||
for {
|
for {
|
||||||
obj.SetState(ResStateWatching) // reset
|
|
||||||
select {
|
select {
|
||||||
case event := <-obj.Events():
|
case event := <-obj.Events():
|
||||||
cuid.SetConverged(false)
|
|
||||||
// we avoid sending events on unpause
|
// we avoid sending events on unpause
|
||||||
if exit, send = obj.ReadEvent(&event); exit {
|
if exit, send = obj.ReadEvent(event); exit != nil {
|
||||||
return nil // exit
|
return *exit // exit
|
||||||
}
|
}
|
||||||
|
|
||||||
// the actual events!
|
// the actual events!
|
||||||
case event := <-obj.foo.Events:
|
case event := <-obj.foo.Events:
|
||||||
if is_an_event {
|
if is_an_event {
|
||||||
send = true // used below
|
send = true // used below
|
||||||
cuid.SetConverged(false)
|
|
||||||
obj.StateOK(false) // dirty
|
obj.StateOK(false) // dirty
|
||||||
}
|
}
|
||||||
|
|
||||||
// event errors
|
// event errors
|
||||||
case err := <-obj.foo.Errors:
|
case err := <-obj.foo.Errors:
|
||||||
cuuid.SetConverged(false)
|
|
||||||
return err // will cause a retry or permanent failure
|
return err // will cause a retry or permanent failure
|
||||||
|
|
||||||
case <-cuid.ConvergedTimer():
|
|
||||||
cuid.SetConverged(true) // converged!
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// do all our event sending all together to avoid duplicate msgs
|
// do all our event sending all together to avoid duplicate msgs
|
||||||
if send {
|
if send {
|
||||||
send = false
|
send = false
|
||||||
if exit, err := obj.DoSend(processChan, ""); exit || err != nil {
|
obj.Event() // send the event!
|
||||||
return err // we exit or bubble up a NACK...
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -319,52 +343,42 @@ some way.
|
|||||||
#### Example
|
#### Example
|
||||||
```golang
|
```golang
|
||||||
// Compare two resources and return if they are equivalent.
|
// Compare two resources and return if they are equivalent.
|
||||||
func (obj *FooRes) Compare(res Res) bool {
|
func (obj *FooRes) Compare(r Res) bool {
|
||||||
switch res.(type) {
|
// we can only compare FooRes to others of the same resource kind
|
||||||
case *FooRes: // only compare to other resources of the Foo kind!
|
res, ok := r.(*FooRes)
|
||||||
res := res.(*FileRes)
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
if !obj.BaseRes.Compare(res) { // call base Compare
|
if !obj.BaseRes.Compare(res) { // call base Compare
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if obj.Name != res.Name {
|
if obj.Name != res.Name {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj.whatever != res.whatever {
|
if obj.whatever != res.whatever {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if obj.Flag != res.Flag {
|
if obj.Flag != res.Flag {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
return false // different kind of resource
|
|
||||||
}
|
|
||||||
return true // they must match!
|
return true // they must match!
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
###Validate
|
### UIDs
|
||||||
```golang
|
```golang
|
||||||
Validate() error
|
UIDs() []ResUID
|
||||||
```
|
```
|
||||||
|
|
||||||
This method is used to validate if the populated resource struct is a valid
|
The `UIDs` method returns a list of `ResUID` interfaces that represent the
|
||||||
representation of the resource kind. If it does not conform to the resource
|
|
||||||
specifications, it should generate an error. If you notice that this method is
|
|
||||||
quite large, it might be an indication that you might want to reconsider the
|
|
||||||
parameter list and interface to this resource.
|
|
||||||
|
|
||||||
###GetUIDs
|
|
||||||
```golang
|
|
||||||
GetUIDs() []ResUID
|
|
||||||
```
|
|
||||||
|
|
||||||
The `GetUIDs` method returns a list of `ResUID` interfaces that represent the
|
|
||||||
particular resource uniquely. This is used with the AutoEdges API to determine
|
particular resource uniquely. This is used with the AutoEdges API to determine
|
||||||
if another resource can match a dependency to this one.
|
if another resource can match a dependency to this one.
|
||||||
|
|
||||||
### AutoEdges
|
### AutoEdges
|
||||||
```golang
|
```golang
|
||||||
AutoEdges() AutoEdge
|
AutoEdges() (AutoEdge, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
This returns a struct that implements the `AutoEdge` interface. This struct
|
This returns a struct that implements the `AutoEdge` interface. This struct
|
||||||
@@ -378,6 +392,42 @@ CollectPattern() string
|
|||||||
|
|
||||||
This is currently a stub and will be updated once the DSL is further along.
|
This is currently a stub and will be updated once the DSL is further along.
|
||||||
|
|
||||||
|
### UnmarshalYAML
|
||||||
|
```golang
|
||||||
|
UnmarshalYAML(unmarshal func(interface{}) error) error // optional
|
||||||
|
```
|
||||||
|
|
||||||
|
This is optional, but recommended for any resource that will have a YAML
|
||||||
|
accessible struct. It is not required because to do so would mean that
|
||||||
|
third-party or custom resources (such as those someone writes to use with
|
||||||
|
`libmgmt`) would have to implement this needlessly.
|
||||||
|
|
||||||
|
The signature intentionally matches what is required to satisfy the `go-yaml`
|
||||||
|
[Unmarshaler](https://godoc.org/gopkg.in/yaml.v2#Unmarshaler) interface.
|
||||||
|
|
||||||
|
#### Example
|
||||||
|
```golang
|
||||||
|
// UnmarshalYAML is the custom unmarshal handler for this struct.
|
||||||
|
// It is primarily useful for setting the defaults.
|
||||||
|
func (obj *FooRes) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||||
|
type rawRes FooRes // indirection to avoid infinite recursion
|
||||||
|
|
||||||
|
def := obj.Default() // get the default
|
||||||
|
res, ok := def.(*FooRes) // put in the right format
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("could not convert to FooRes")
|
||||||
|
}
|
||||||
|
raw := rawRes(*res) // convert; the defaults go here
|
||||||
|
|
||||||
|
if err := unmarshal(&raw); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
*obj = FooRes(raw) // restore from indirection with type conversion!
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Further considerations
|
## Further considerations
|
||||||
There is some additional information that any resource writer will need to know.
|
There is some additional information that any resource writer will need to know.
|
||||||
Each issue is listed separately below!
|
Each issue is listed separately below!
|
||||||
@@ -402,32 +452,15 @@ type FooRes struct {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
###YAML
|
### Resource registration
|
||||||
In addition to labelling your resource struct with YAML fields, you must also
|
All resources must be registered with the engine so that they can be found. This
|
||||||
add an entry to the internal `GraphConfig` struct. It is a fairly straight
|
also ensures they can be encoded and decoded. Make sure to include the following
|
||||||
forward one line patch.
|
code snippet for this to work.
|
||||||
|
|
||||||
```golang
|
```golang
|
||||||
type GraphConfig struct {
|
|
||||||
// [snip...]
|
|
||||||
Resources struct {
|
|
||||||
Noop []*resources.NoopRes `yaml:"noop"`
|
|
||||||
File []*resources.FileRes `yaml:"file"`
|
|
||||||
// [snip...]
|
|
||||||
Foo []*resources.FooRes `yaml:"foo"` // tada :)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
###Gob registration
|
|
||||||
All resources must be registered with the `golang` _gob_ module so that they can
|
|
||||||
be encoded and decoded. Make sure to include the following code snippet for this
|
|
||||||
to work.
|
|
||||||
|
|
||||||
```golang
|
|
||||||
import "encoding/gob"
|
|
||||||
func init() { // special golang method that runs once
|
func init() { // special golang method that runs once
|
||||||
gob.Register(&FooRes{}) // substitude your resource here
|
// set your resource kind and struct here (the kind must be lower case)
|
||||||
|
RegisterResource("foo", func() Res { return &FooRes{} })
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -463,7 +496,7 @@ This can _only_ be done inside of the `CheckApply` function!
|
|||||||
```golang
|
```golang
|
||||||
// inside CheckApply, probably near the top
|
// inside CheckApply, probably near the top
|
||||||
if val, exists := obj.Recv["SomeKey"]; exists {
|
if val, exists := obj.Recv["SomeKey"]; exists {
|
||||||
log.Printf("SomeKey was sent to us from: %s[%s].%s", val.Res.Kind(), val.Res.GetName(), val.Key)
|
log.Printf("SomeKey was sent to us from: %s.%s", val.Res, val.Key)
|
||||||
if val.Changed {
|
if val.Changed {
|
||||||
log.Printf("SomeKey was just updated!")
|
log.Printf("SomeKey was just updated!")
|
||||||
// you may want to invalidate some local cache
|
// you may want to invalidate some local cache
|
||||||
@@ -520,15 +553,3 @@ Additional blog posts, videos and other material [is available!](https://github.
|
|||||||
If you have any ideas for API changes or other improvements to resource writing,
|
If you have any ideas for API changes or other improvements to resource writing,
|
||||||
please let us know! We're still pre 1.0 and pre 0.1 and happy to break API in
|
please let us know! We're still pre 1.0 and pre 0.1 and happy to break API in
|
||||||
order to get it right!
|
order to get it right!
|
||||||
|
|
||||||
##Authors
|
|
||||||
|
|
||||||
Copyright (C) 2013-2016+ James Shubin and the project contributors
|
|
||||||
|
|
||||||
Please see the
|
|
||||||
[AUTHORS](https://github.com/purpleidea/mgmt/tree/master/AUTHORS) file
|
|
||||||
for more information.
|
|
||||||
|
|
||||||
* [github](https://github.com/purpleidea/)
|
|
||||||
* [@purpleidea](https://twitter.com/#!/purpleidea)
|
|
||||||
* [https://ttboj.wordpress.com/](https://ttboj.wordpress.com/)
|
|
||||||
|
|||||||
718
etcd/etcd.go
718
etcd/etcd.go
File diff suppressed because it is too large
Load Diff
412
etcd/methods.go
Normal file
412
etcd/methods.go
Normal file
@@ -0,0 +1,412 @@
|
|||||||
|
// Mgmt
|
||||||
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
|
//
|
||||||
|
// 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package etcd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
etcd "github.com/coreos/etcd/clientv3"
|
||||||
|
rpctypes "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
|
||||||
|
etcdtypes "github.com/coreos/etcd/pkg/types"
|
||||||
|
context "golang.org/x/net/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TODO: Could all these Etcd*(obj *EmbdEtcd, ...) functions which deal with the
|
||||||
|
// interface between etcd paths and behaviour be grouped into a single struct ?
|
||||||
|
|
||||||
|
// Nominate nominates a particular client to be a server (peer).
|
||||||
|
func Nominate(obj *EmbdEtcd, hostname string, urls etcdtypes.URLs) error {
|
||||||
|
if obj.flags.Trace {
|
||||||
|
log.Printf("Trace: Etcd: Nominate(%v): %v", hostname, urls.String())
|
||||||
|
defer log.Printf("Trace: Etcd: Nominate(%v): Finished!", hostname)
|
||||||
|
}
|
||||||
|
// nominate someone to be a server
|
||||||
|
nominate := fmt.Sprintf("/%s/nominated/%s", NS, hostname)
|
||||||
|
ops := []etcd.Op{} // list of ops in this txn
|
||||||
|
if urls != nil {
|
||||||
|
ops = append(ops, etcd.OpPut(nominate, urls.String())) // TODO: add a TTL? (etcd.WithLease)
|
||||||
|
|
||||||
|
} else { // delete message if set to erase
|
||||||
|
ops = append(ops, etcd.OpDelete(nominate))
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := obj.Txn(nil, ops, nil); err != nil {
|
||||||
|
return fmt.Errorf("nominate failed") // exit in progress?
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nominated returns a urls map of nominated etcd server volunteers.
|
||||||
|
// NOTE: I know 'nominees' might be more correct, but is less consistent here
|
||||||
|
func Nominated(obj *EmbdEtcd) (etcdtypes.URLsMap, error) {
|
||||||
|
path := fmt.Sprintf("/%s/nominated/", NS)
|
||||||
|
keyMap, err := obj.Get(path, etcd.WithPrefix()) // map[string]string, bool
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("nominated isn't available: %v", err)
|
||||||
|
}
|
||||||
|
nominated := make(etcdtypes.URLsMap)
|
||||||
|
for key, val := range keyMap { // loop through directory of nominated
|
||||||
|
if !strings.HasPrefix(key, path) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
name := key[len(path):] // get name of nominee
|
||||||
|
if val == "" { // skip "erased" values
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
urls, err := etcdtypes.NewURLs(strings.Split(val, ","))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("nominated data format error: %v", err)
|
||||||
|
}
|
||||||
|
nominated[name] = urls // add to map
|
||||||
|
if obj.flags.Debug {
|
||||||
|
log.Printf("Etcd: Nominated(%v): %v", name, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nominated, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Volunteer offers yourself up to be a server if needed.
|
||||||
|
func Volunteer(obj *EmbdEtcd, urls etcdtypes.URLs) error {
|
||||||
|
if obj.flags.Trace {
|
||||||
|
log.Printf("Trace: Etcd: Volunteer(%v): %v", obj.hostname, urls.String())
|
||||||
|
defer log.Printf("Trace: Etcd: Volunteer(%v): Finished!", obj.hostname)
|
||||||
|
}
|
||||||
|
// volunteer to be a server
|
||||||
|
volunteer := fmt.Sprintf("/%s/volunteers/%s", NS, obj.hostname)
|
||||||
|
ops := []etcd.Op{} // list of ops in this txn
|
||||||
|
if urls != nil {
|
||||||
|
// XXX: adding a TTL is crucial! (i think)
|
||||||
|
ops = append(ops, etcd.OpPut(volunteer, urls.String())) // value is usually a peer "serverURL"
|
||||||
|
|
||||||
|
} else { // delete message if set to erase
|
||||||
|
ops = append(ops, etcd.OpDelete(volunteer))
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := obj.Txn(nil, ops, nil); err != nil {
|
||||||
|
return fmt.Errorf("volunteering failed") // exit in progress?
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Volunteers returns a urls map of available etcd server volunteers.
|
||||||
|
func Volunteers(obj *EmbdEtcd) (etcdtypes.URLsMap, error) {
|
||||||
|
if obj.flags.Trace {
|
||||||
|
log.Printf("Trace: Etcd: Volunteers()")
|
||||||
|
defer log.Printf("Trace: Etcd: Volunteers(): Finished!")
|
||||||
|
}
|
||||||
|
path := fmt.Sprintf("/%s/volunteers/", NS)
|
||||||
|
keyMap, err := obj.Get(path, etcd.WithPrefix())
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("volunteers aren't available: %v", err)
|
||||||
|
}
|
||||||
|
volunteers := make(etcdtypes.URLsMap)
|
||||||
|
for key, val := range keyMap { // loop through directory of volunteers
|
||||||
|
if !strings.HasPrefix(key, path) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
name := key[len(path):] // get name of volunteer
|
||||||
|
if val == "" { // skip "erased" values
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
urls, err := etcdtypes.NewURLs(strings.Split(val, ","))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("volunteers data format error: %v", err)
|
||||||
|
}
|
||||||
|
volunteers[name] = urls // add to map
|
||||||
|
if obj.flags.Debug {
|
||||||
|
log.Printf("Etcd: Volunteer(%v): %v", name, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return volunteers, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AdvertiseEndpoints advertises the list of available client endpoints.
|
||||||
|
func AdvertiseEndpoints(obj *EmbdEtcd, urls etcdtypes.URLs) error {
|
||||||
|
if obj.flags.Trace {
|
||||||
|
log.Printf("Trace: Etcd: AdvertiseEndpoints(%v): %v", obj.hostname, urls.String())
|
||||||
|
defer log.Printf("Trace: Etcd: AdvertiseEndpoints(%v): Finished!", obj.hostname)
|
||||||
|
}
|
||||||
|
// advertise endpoints
|
||||||
|
endpoints := fmt.Sprintf("/%s/endpoints/%s", NS, obj.hostname)
|
||||||
|
ops := []etcd.Op{} // list of ops in this txn
|
||||||
|
if urls != nil {
|
||||||
|
// TODO: add a TTL? (etcd.WithLease)
|
||||||
|
ops = append(ops, etcd.OpPut(endpoints, urls.String())) // value is usually a "clientURL"
|
||||||
|
|
||||||
|
} else { // delete message if set to erase
|
||||||
|
ops = append(ops, etcd.OpDelete(endpoints))
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := obj.Txn(nil, ops, nil); err != nil {
|
||||||
|
return fmt.Errorf("endpoint advertising failed") // exit in progress?
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Endpoints returns a urls map of available etcd server endpoints.
|
||||||
|
func Endpoints(obj *EmbdEtcd) (etcdtypes.URLsMap, error) {
|
||||||
|
if obj.flags.Trace {
|
||||||
|
log.Printf("Trace: Etcd: Endpoints()")
|
||||||
|
defer log.Printf("Trace: Etcd: Endpoints(): Finished!")
|
||||||
|
}
|
||||||
|
path := fmt.Sprintf("/%s/endpoints/", NS)
|
||||||
|
keyMap, err := obj.Get(path, etcd.WithPrefix())
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("endpoints aren't available: %v", err)
|
||||||
|
}
|
||||||
|
endpoints := make(etcdtypes.URLsMap)
|
||||||
|
for key, val := range keyMap { // loop through directory of endpoints
|
||||||
|
if !strings.HasPrefix(key, path) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
name := key[len(path):] // get name of volunteer
|
||||||
|
if val == "" { // skip "erased" values
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
urls, err := etcdtypes.NewURLs(strings.Split(val, ","))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("endpoints data format error: %v", err)
|
||||||
|
}
|
||||||
|
endpoints[name] = urls // add to map
|
||||||
|
if obj.flags.Debug {
|
||||||
|
log.Printf("Etcd: Endpoint(%v): %v", name, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return endpoints, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetHostnameConverged sets whether a specific hostname is converged.
|
||||||
|
func SetHostnameConverged(obj *EmbdEtcd, hostname string, isConverged bool) error {
|
||||||
|
if obj.flags.Trace {
|
||||||
|
log.Printf("Trace: Etcd: SetHostnameConverged(%s): %v", hostname, isConverged)
|
||||||
|
defer log.Printf("Trace: Etcd: SetHostnameConverged(%v): Finished!", hostname)
|
||||||
|
}
|
||||||
|
converged := fmt.Sprintf("/%s/converged/%s", NS, hostname)
|
||||||
|
op := []etcd.Op{etcd.OpPut(converged, fmt.Sprintf("%t", isConverged))}
|
||||||
|
if _, err := obj.Txn(nil, op, nil); err != nil { // TODO: do we need a skipConv flag here too?
|
||||||
|
return fmt.Errorf("set converged failed") // exit in progress?
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// HostnameConverged returns a map of every hostname's converged state.
|
||||||
|
func HostnameConverged(obj *EmbdEtcd) (map[string]bool, error) {
|
||||||
|
if obj.flags.Trace {
|
||||||
|
log.Printf("Trace: Etcd: HostnameConverged()")
|
||||||
|
defer log.Printf("Trace: Etcd: HostnameConverged(): Finished!")
|
||||||
|
}
|
||||||
|
path := fmt.Sprintf("/%s/converged/", NS)
|
||||||
|
keyMap, err := obj.ComplexGet(path, true, etcd.WithPrefix()) // don't un-converge
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("converged values aren't available: %v", err)
|
||||||
|
}
|
||||||
|
converged := make(map[string]bool)
|
||||||
|
for key, val := range keyMap { // loop through directory...
|
||||||
|
if !strings.HasPrefix(key, path) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
name := key[len(path):] // get name of key
|
||||||
|
if val == "" { // skip "erased" values
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
b, err := strconv.ParseBool(val)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("converged data format error: %v", err)
|
||||||
|
}
|
||||||
|
converged[name] = b // add to map
|
||||||
|
}
|
||||||
|
return converged, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddHostnameConvergedWatcher adds a watcher with a callback that runs on
|
||||||
|
// hostname state changes.
|
||||||
|
func AddHostnameConvergedWatcher(obj *EmbdEtcd, callbackFn func(map[string]bool) error) (func(), error) {
|
||||||
|
path := fmt.Sprintf("/%s/converged/", NS)
|
||||||
|
internalCbFn := func(re *RE) error {
|
||||||
|
// TODO: get the value from the response, and apply delta...
|
||||||
|
// for now, just run a get operation which is easier to code!
|
||||||
|
m, err := HostnameConverged(obj)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return callbackFn(m) // call my function
|
||||||
|
}
|
||||||
|
return obj.AddWatcher(path, internalCbFn, true, true, etcd.WithPrefix()) // no block and no converger reset
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetClusterSize sets the ideal target cluster size of etcd peers.
|
||||||
|
func SetClusterSize(obj *EmbdEtcd, value uint16) error {
|
||||||
|
if obj.flags.Trace {
|
||||||
|
log.Printf("Trace: Etcd: SetClusterSize(): %v", value)
|
||||||
|
defer log.Printf("Trace: Etcd: SetClusterSize(): Finished!")
|
||||||
|
}
|
||||||
|
key := fmt.Sprintf("/%s/idealClusterSize", NS)
|
||||||
|
|
||||||
|
if err := obj.Set(key, strconv.FormatUint(uint64(value), 10)); err != nil {
|
||||||
|
return fmt.Errorf("function SetClusterSize failed: %v", err) // exit in progress?
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetClusterSize gets the ideal target cluster size of etcd peers.
|
||||||
|
func GetClusterSize(obj *EmbdEtcd) (uint16, error) {
|
||||||
|
key := fmt.Sprintf("/%s/idealClusterSize", NS)
|
||||||
|
keyMap, err := obj.Get(key)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("function GetClusterSize failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
val, exists := keyMap[key]
|
||||||
|
if !exists || val == "" {
|
||||||
|
return 0, fmt.Errorf("function GetClusterSize failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
v, err := strconv.ParseUint(val, 10, 16)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("function GetClusterSize failed: %v", err)
|
||||||
|
}
|
||||||
|
return uint16(v), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MemberAdd adds a member to the cluster.
|
||||||
|
func MemberAdd(obj *EmbdEtcd, peerURLs etcdtypes.URLs) (*etcd.MemberAddResponse, error) {
|
||||||
|
//obj.Connect(false) // TODO: ?
|
||||||
|
ctx := context.Background()
|
||||||
|
var response *etcd.MemberAddResponse
|
||||||
|
var err error
|
||||||
|
for {
|
||||||
|
if obj.exiting { // the exit signal has been sent!
|
||||||
|
return nil, fmt.Errorf("exiting etcd")
|
||||||
|
}
|
||||||
|
obj.rLock.RLock()
|
||||||
|
response, err = obj.client.MemberAdd(ctx, peerURLs.StringSlice())
|
||||||
|
obj.rLock.RUnlock()
|
||||||
|
if err == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if ctx, err = obj.CtxError(ctx, err); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MemberRemove removes a member by mID and returns if it worked, and also
|
||||||
|
// if there was an error. This is because it might have run without error, but
|
||||||
|
// the member wasn't found, for example.
|
||||||
|
func MemberRemove(obj *EmbdEtcd, mID uint64) (bool, error) {
|
||||||
|
//obj.Connect(false) // TODO: ?
|
||||||
|
ctx := context.Background()
|
||||||
|
for {
|
||||||
|
if obj.exiting { // the exit signal has been sent!
|
||||||
|
return false, fmt.Errorf("exiting etcd")
|
||||||
|
}
|
||||||
|
obj.rLock.RLock()
|
||||||
|
_, err := obj.client.MemberRemove(ctx, mID)
|
||||||
|
obj.rLock.RUnlock()
|
||||||
|
if err == nil {
|
||||||
|
break
|
||||||
|
} else if err == rpctypes.ErrMemberNotFound {
|
||||||
|
// if we get this, member already shut itself down :)
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
if ctx, err = obj.CtxError(ctx, err); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Members returns information on cluster membership.
|
||||||
|
// The member ID's are the keys, because an empty names means unstarted!
|
||||||
|
// TODO: consider queueing this through the main loop with CtxError(ctx, err)
|
||||||
|
func Members(obj *EmbdEtcd) (map[uint64]string, error) {
|
||||||
|
//obj.Connect(false) // TODO: ?
|
||||||
|
ctx := context.Background()
|
||||||
|
var response *etcd.MemberListResponse
|
||||||
|
var err error
|
||||||
|
for {
|
||||||
|
if obj.exiting { // the exit signal has been sent!
|
||||||
|
return nil, fmt.Errorf("exiting etcd")
|
||||||
|
}
|
||||||
|
obj.rLock.RLock()
|
||||||
|
if obj.flags.Trace {
|
||||||
|
log.Printf("Trace: Etcd: Members(): Endpoints are: %v", obj.client.Endpoints())
|
||||||
|
}
|
||||||
|
response, err = obj.client.MemberList(ctx)
|
||||||
|
obj.rLock.RUnlock()
|
||||||
|
if err == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if ctx, err = obj.CtxError(ctx, err); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
members := make(map[uint64]string)
|
||||||
|
for _, x := range response.Members {
|
||||||
|
members[x.ID] = x.Name // x.Name will be "" if unstarted!
|
||||||
|
}
|
||||||
|
return members, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Leader returns the current leader of the etcd server cluster.
|
||||||
|
func Leader(obj *EmbdEtcd) (string, error) {
|
||||||
|
//obj.Connect(false) // TODO: ?
|
||||||
|
var err error
|
||||||
|
membersMap := make(map[uint64]string)
|
||||||
|
if membersMap, err = Members(obj); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
addresses := obj.LocalhostClientURLs() // heuristic, but probably correct
|
||||||
|
if len(addresses) == 0 {
|
||||||
|
// probably a programming error...
|
||||||
|
return "", fmt.Errorf("programming error")
|
||||||
|
}
|
||||||
|
endpoint := addresses[0].Host // FIXME: arbitrarily picked the first one
|
||||||
|
|
||||||
|
// part two
|
||||||
|
ctx := context.Background()
|
||||||
|
var response *etcd.StatusResponse
|
||||||
|
for {
|
||||||
|
if obj.exiting { // the exit signal has been sent!
|
||||||
|
return "", fmt.Errorf("exiting etcd")
|
||||||
|
}
|
||||||
|
|
||||||
|
obj.rLock.RLock()
|
||||||
|
response, err = obj.client.Maintenance.Status(ctx, endpoint)
|
||||||
|
obj.rLock.RUnlock()
|
||||||
|
if err == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if ctx, err = obj.CtxError(ctx, err); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// isLeader: response.Header.MemberId == response.Leader
|
||||||
|
for id, name := range membersMap {
|
||||||
|
if id == response.Leader {
|
||||||
|
return name, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", fmt.Errorf("members map is not current") // not found
|
||||||
|
}
|
||||||
181
etcd/resources.go
Normal file
181
etcd/resources.go
Normal file
@@ -0,0 +1,181 @@
|
|||||||
|
// Mgmt
|
||||||
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
|
//
|
||||||
|
// 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package etcd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/purpleidea/mgmt/resources"
|
||||||
|
"github.com/purpleidea/mgmt/util"
|
||||||
|
|
||||||
|
etcd "github.com/coreos/etcd/clientv3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WatchResources returns a channel that outputs events when exported resources
|
||||||
|
// change.
|
||||||
|
// TODO: Filter our watch (on the server side if possible) based on the
|
||||||
|
// collection prefixes and filters that we care about...
|
||||||
|
func WatchResources(obj *EmbdEtcd) chan error {
|
||||||
|
ch := make(chan error, 1) // buffer it so we can measure it
|
||||||
|
path := fmt.Sprintf("/%s/exported/", NS)
|
||||||
|
callback := func(re *RE) error {
|
||||||
|
// TODO: is this even needed? it used to happen on conn errors
|
||||||
|
log.Printf("Etcd: Watch: Path: %v", path) // event
|
||||||
|
if re == nil || re.response.Canceled {
|
||||||
|
return fmt.Errorf("watch is empty") // will cause a CtxError+retry
|
||||||
|
}
|
||||||
|
// we normally need to check if anything changed since the last
|
||||||
|
// event, since a set (export) with no changes still causes the
|
||||||
|
// watcher to trigger and this would cause an infinite loop. we
|
||||||
|
// don't need to do this check anymore because we do the export
|
||||||
|
// transactionally, and only if a change is needed. since it is
|
||||||
|
// atomic, all the changes arrive together which avoids dupes!!
|
||||||
|
if len(ch) == 0 { // send event only if one isn't pending
|
||||||
|
// this check avoids multiple events all queueing up and then
|
||||||
|
// being released continuously long after the changes stopped
|
||||||
|
// do not block!
|
||||||
|
ch <- nil // event
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
_, _ = obj.AddWatcher(path, callback, true, false, etcd.WithPrefix()) // no need to check errors
|
||||||
|
return ch
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetResources exports all of the resources which we pass in to etcd.
|
||||||
|
func SetResources(obj *EmbdEtcd, hostname string, resourceList []resources.Res) error {
|
||||||
|
// key structure is /$NS/exported/$hostname/resources/$uid = $data
|
||||||
|
|
||||||
|
var kindFilter []string // empty to get from everyone
|
||||||
|
hostnameFilter := []string{hostname}
|
||||||
|
// this is not a race because we should only be reading keys which we
|
||||||
|
// set, and there should not be any contention with other hosts here!
|
||||||
|
originals, err := GetResources(obj, hostnameFilter, kindFilter)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(originals) == 0 && len(resourceList) == 0 { // special case of no add or del
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ifs := []etcd.Cmp{} // list matching the desired state
|
||||||
|
ops := []etcd.Op{} // list of ops in this transaction
|
||||||
|
for _, res := range resourceList {
|
||||||
|
if res.GetKind() == "" {
|
||||||
|
log.Fatalf("Etcd: SetResources: Error: Empty kind: %v", res.GetName())
|
||||||
|
}
|
||||||
|
uid := fmt.Sprintf("%s/%s", res.GetKind(), res.GetName())
|
||||||
|
path := fmt.Sprintf("/%s/exported/%s/resources/%s", NS, hostname, uid)
|
||||||
|
if data, err := resources.ResToB64(res); err == nil {
|
||||||
|
ifs = append(ifs, etcd.Compare(etcd.Value(path), "=", data)) // desired state
|
||||||
|
ops = append(ops, etcd.OpPut(path, data))
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("can't convert to B64: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match := func(res resources.Res, resourceList []resources.Res) bool { // helper lambda
|
||||||
|
for _, x := range resourceList {
|
||||||
|
if res.GetKind() == x.GetKind() && res.GetName() == x.GetName() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
hasDeletes := false
|
||||||
|
// delete old, now unused resources here...
|
||||||
|
for _, res := range originals {
|
||||||
|
if res.GetKind() == "" {
|
||||||
|
log.Fatalf("Etcd: SetResources: Error: Empty kind: %v", res.GetName())
|
||||||
|
}
|
||||||
|
uid := fmt.Sprintf("%s/%s", res.GetKind(), res.GetName())
|
||||||
|
path := fmt.Sprintf("/%s/exported/%s/resources/%s", NS, hostname, uid)
|
||||||
|
|
||||||
|
if match(res, resourceList) { // if we match, no need to delete!
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
ops = append(ops, etcd.OpDelete(path))
|
||||||
|
|
||||||
|
hasDeletes = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// if everything is already correct, do nothing, otherwise, run the ops!
|
||||||
|
// it's important to do this in one transaction, and atomically, because
|
||||||
|
// this way, we only generate one watch event, and only when it's needed
|
||||||
|
if hasDeletes { // always run, ifs don't matter
|
||||||
|
_, err = obj.Txn(nil, ops, nil) // TODO: does this run? it should!
|
||||||
|
} else {
|
||||||
|
_, err = obj.Txn(ifs, nil, ops) // TODO: do we need to look at response?
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetResources collects all of the resources which match a filter from etcd.
|
||||||
|
// If the kindfilter or hostnameFilter is empty, then it assumes no filtering...
|
||||||
|
// TODO: Expand this with a more powerful filter based on what we eventually
|
||||||
|
// support in our collect DSL. Ideally a server side filter like WithFilter()
|
||||||
|
// We could do this if the pattern was /$NS/exported/$kind/$hostname/$uid = $data.
|
||||||
|
func GetResources(obj *EmbdEtcd, hostnameFilter, kindFilter []string) ([]resources.Res, error) {
|
||||||
|
// key structure is /$NS/exported/$hostname/resources/$uid = $data
|
||||||
|
path := fmt.Sprintf("/%s/exported/", NS)
|
||||||
|
resourceList := []resources.Res{}
|
||||||
|
keyMap, err := obj.Get(path, etcd.WithPrefix(), etcd.WithSort(etcd.SortByKey, etcd.SortAscend))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("could not get resources: %v", err)
|
||||||
|
}
|
||||||
|
for key, val := range keyMap {
|
||||||
|
if !strings.HasPrefix(key, path) { // sanity check
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
str := strings.Split(key[len(path):], "/")
|
||||||
|
if len(str) != 4 {
|
||||||
|
return nil, fmt.Errorf("unexpected chunk count")
|
||||||
|
}
|
||||||
|
hostname, r, kind, name := str[0], str[1], str[2], str[3]
|
||||||
|
if r != "resources" {
|
||||||
|
return nil, fmt.Errorf("unexpected chunk pattern")
|
||||||
|
}
|
||||||
|
if kind == "" {
|
||||||
|
return nil, fmt.Errorf("unexpected kind chunk")
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: ideally this would be a server side filter instead!
|
||||||
|
if len(hostnameFilter) > 0 && !util.StrInList(hostname, hostnameFilter) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: ideally this would be a server side filter instead!
|
||||||
|
if len(kindFilter) > 0 && !util.StrInList(kind, kindFilter) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if obj, err := resources.B64ToRes(val); err == nil {
|
||||||
|
log.Printf("Etcd: Get: (Hostname, Kind, Name): (%s, %s, %s)", hostname, kind, name)
|
||||||
|
resourceList = append(resourceList, obj)
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("can't convert from B64: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resourceList, nil
|
||||||
|
}
|
||||||
105
etcd/str.go
Normal file
105
etcd/str.go
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
// Mgmt
|
||||||
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
|
//
|
||||||
|
// 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package etcd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
etcd "github.com/coreos/etcd/clientv3"
|
||||||
|
errwrap "github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ErrNotExist is returned when GetStr can not find the requested key.
|
||||||
|
// TODO: https://dave.cheney.net/2016/04/07/constant-errors
|
||||||
|
var ErrNotExist = errors.New("errNotExist")
|
||||||
|
|
||||||
|
// WatchStr returns a channel which spits out events on key activity.
|
||||||
|
// FIXME: It should close the channel when it's done, and spit out errors when
|
||||||
|
// something goes wrong.
|
||||||
|
func WatchStr(obj *EmbdEtcd, key string) chan error {
|
||||||
|
// new key structure is /$NS/strings/$key = $data
|
||||||
|
path := fmt.Sprintf("/%s/strings/%s", NS, key)
|
||||||
|
ch := make(chan error, 1)
|
||||||
|
// FIXME: fix our API so that we get a close event on shutdown.
|
||||||
|
callback := func(re *RE) error {
|
||||||
|
// TODO: is this even needed? it used to happen on conn errors
|
||||||
|
//log.Printf("Etcd: Watch: Path: %v", path) // event
|
||||||
|
if re == nil || re.response.Canceled {
|
||||||
|
return fmt.Errorf("watch is empty") // will cause a CtxError+retry
|
||||||
|
}
|
||||||
|
if len(ch) == 0 { // send event only if one isn't pending
|
||||||
|
ch <- nil // event
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
_, _ = obj.AddWatcher(path, callback, true, false, etcd.WithPrefix()) // no need to check errors
|
||||||
|
return ch
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStr collects the string which matches a global namespace in etcd.
|
||||||
|
func GetStr(obj *EmbdEtcd, key string) (string, error) {
|
||||||
|
// new key structure is /$NS/strings/$key = $data
|
||||||
|
path := fmt.Sprintf("/%s/strings/%s", NS, key)
|
||||||
|
keyMap, err := obj.Get(path, etcd.WithPrefix())
|
||||||
|
if err != nil {
|
||||||
|
return "", errwrap.Wrapf(err, "could not get strings in: %s", key)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(keyMap) == 0 {
|
||||||
|
return "", ErrNotExist
|
||||||
|
}
|
||||||
|
|
||||||
|
if count := len(keyMap); count != 1 {
|
||||||
|
return "", fmt.Errorf("returned %d entries", count)
|
||||||
|
}
|
||||||
|
|
||||||
|
val, exists := keyMap[path]
|
||||||
|
if !exists {
|
||||||
|
return "", fmt.Errorf("path `%s` is missing", path)
|
||||||
|
}
|
||||||
|
|
||||||
|
//log.Printf("Etcd: GetStr(%s): %s", key, val)
|
||||||
|
return val, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetStr sets a key and hostname pair to a certain value. If the value is
|
||||||
|
// nil, then it deletes the key. Otherwise the value should point to a string.
|
||||||
|
// TODO: TTL or delete disconnect?
|
||||||
|
func SetStr(obj *EmbdEtcd, key string, data *string) error {
|
||||||
|
// key structure is /$NS/strings/$key = $data
|
||||||
|
path := fmt.Sprintf("/%s/strings/%s", NS, key)
|
||||||
|
ifs := []etcd.Cmp{} // list matching the desired state
|
||||||
|
ops := []etcd.Op{} // list of ops in this transaction (then)
|
||||||
|
els := []etcd.Op{} // list of ops in this transaction (else)
|
||||||
|
if data == nil { // perform a delete
|
||||||
|
// TODO: use https://github.com/coreos/etcd/pull/7417 if merged
|
||||||
|
//ifs = append(ifs, etcd.KeyExists(path))
|
||||||
|
ifs = append(ifs, etcd.Compare(etcd.Version(path), ">", 0))
|
||||||
|
ops = append(ops, etcd.OpDelete(path))
|
||||||
|
} else {
|
||||||
|
data := *data // get the real value
|
||||||
|
ifs = append(ifs, etcd.Compare(etcd.Value(path), "=", data)) // desired state
|
||||||
|
els = append(els, etcd.OpPut(path, data))
|
||||||
|
}
|
||||||
|
|
||||||
|
// it's important to do this in one transaction, and atomically, because
|
||||||
|
// this way, we only generate one watch event, and only when it's needed
|
||||||
|
_, err := obj.Txn(ifs, ops, els) // TODO: do we need to look at response?
|
||||||
|
return errwrap.Wrapf(err, "could not set strings in: %s", key)
|
||||||
|
}
|
||||||
115
etcd/strmap.go
Normal file
115
etcd/strmap.go
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
// Mgmt
|
||||||
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
|
//
|
||||||
|
// 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package etcd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/purpleidea/mgmt/util"
|
||||||
|
|
||||||
|
etcd "github.com/coreos/etcd/clientv3"
|
||||||
|
errwrap "github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WatchStrMap returns a channel which spits out events on key activity.
|
||||||
|
// FIXME: It should close the channel when it's done, and spit out errors when
|
||||||
|
// something goes wrong.
|
||||||
|
func WatchStrMap(obj *EmbdEtcd, key string) chan error {
|
||||||
|
// new key structure is /$NS/strings/$key/$hostname = $data
|
||||||
|
path := fmt.Sprintf("/%s/strings/%s", NS, key)
|
||||||
|
ch := make(chan error, 1)
|
||||||
|
// FIXME: fix our API so that we get a close event on shutdown.
|
||||||
|
callback := func(re *RE) error {
|
||||||
|
// TODO: is this even needed? it used to happen on conn errors
|
||||||
|
//log.Printf("Etcd: Watch: Path: %v", path) // event
|
||||||
|
if re == nil || re.response.Canceled {
|
||||||
|
return fmt.Errorf("watch is empty") // will cause a CtxError+retry
|
||||||
|
}
|
||||||
|
if len(ch) == 0 { // send event only if one isn't pending
|
||||||
|
ch <- nil // event
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
_, _ = obj.AddWatcher(path, callback, true, false, etcd.WithPrefix()) // no need to check errors
|
||||||
|
return ch
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStrMap collects all of the strings which match a namespace in etcd.
|
||||||
|
func GetStrMap(obj *EmbdEtcd, hostnameFilter []string, key string) (map[string]string, error) {
|
||||||
|
// old key structure is /$NS/strings/$hostname/$key = $data
|
||||||
|
// new key structure is /$NS/strings/$key/$hostname = $data
|
||||||
|
// FIXME: if we have the $key as the last token (old key structure), we
|
||||||
|
// can allow the key to contain the slash char, otherwise we need to
|
||||||
|
// verify that one isn't present in the input string.
|
||||||
|
path := fmt.Sprintf("/%s/strings/%s", NS, key)
|
||||||
|
keyMap, err := obj.Get(path, etcd.WithPrefix(), etcd.WithSort(etcd.SortByKey, etcd.SortAscend))
|
||||||
|
if err != nil {
|
||||||
|
return nil, errwrap.Wrapf(err, "could not get strings in: %s", key)
|
||||||
|
}
|
||||||
|
result := make(map[string]string)
|
||||||
|
for key, val := range keyMap {
|
||||||
|
if !strings.HasPrefix(key, path) { // sanity check
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
str := strings.Split(key[len(path):], "/")
|
||||||
|
if len(str) != 2 {
|
||||||
|
return nil, fmt.Errorf("unexpected chunk count of %d", len(str))
|
||||||
|
}
|
||||||
|
_, hostname := str[0], str[1]
|
||||||
|
|
||||||
|
if hostname == "" {
|
||||||
|
return nil, fmt.Errorf("unexpected chunk length of %d", len(hostname))
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: ideally this would be a server side filter instead!
|
||||||
|
if len(hostnameFilter) > 0 && !util.StrInList(hostname, hostnameFilter) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
//log.Printf("Etcd: GetStr(%s): (Hostname, Data): (%s, %s)", key, hostname, val)
|
||||||
|
result[hostname] = val
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetStrMap sets a key and hostname pair to a certain value. If the value is
|
||||||
|
// nil, then it deletes the key. Otherwise the value should point to a string.
|
||||||
|
// TODO: TTL or delete disconnect?
|
||||||
|
func SetStrMap(obj *EmbdEtcd, hostname, key string, data *string) error {
|
||||||
|
// key structure is /$NS/strings/$key/$hostname = $data
|
||||||
|
path := fmt.Sprintf("/%s/strings/%s/%s", NS, key, hostname)
|
||||||
|
ifs := []etcd.Cmp{} // list matching the desired state
|
||||||
|
ops := []etcd.Op{} // list of ops in this transaction (then)
|
||||||
|
els := []etcd.Op{} // list of ops in this transaction (else)
|
||||||
|
if data == nil { // perform a delete
|
||||||
|
// TODO: use https://github.com/coreos/etcd/pull/7417 if merged
|
||||||
|
//ifs = append(ifs, etcd.KeyExists(path))
|
||||||
|
ifs = append(ifs, etcd.Compare(etcd.Version(path), ">", 0))
|
||||||
|
ops = append(ops, etcd.OpDelete(path))
|
||||||
|
} else {
|
||||||
|
data := *data // get the real value
|
||||||
|
ifs = append(ifs, etcd.Compare(etcd.Value(path), "=", data)) // desired state
|
||||||
|
els = append(els, etcd.OpPut(path, data))
|
||||||
|
}
|
||||||
|
|
||||||
|
// it's important to do this in one transaction, and atomically, because
|
||||||
|
// this way, we only generate one watch event, and only when it's needed
|
||||||
|
_, err := obj.Txn(ifs, ops, els) // TODO: do we need to look at response?
|
||||||
|
return errwrap.Wrapf(err, "could not set strings in: %s", key)
|
||||||
|
}
|
||||||
@@ -1,18 +1,18 @@
|
|||||||
// Mgmt
|
// Mgmt
|
||||||
// Copyright (C) 2013-2016+ James Shubin and the project contributors
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// GNU Affero General Public License for more details.
|
// GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package etcd
|
package etcd
|
||||||
@@ -27,10 +27,16 @@ type World struct {
|
|||||||
EmbdEtcd *EmbdEtcd
|
EmbdEtcd *EmbdEtcd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ResWatch returns a channel which spits out events on possible exported
|
||||||
|
// resource changes.
|
||||||
|
func (obj *World) ResWatch() chan error {
|
||||||
|
return WatchResources(obj.EmbdEtcd)
|
||||||
|
}
|
||||||
|
|
||||||
// ResExport exports a list of resources under our hostname namespace.
|
// ResExport exports a list of resources under our hostname namespace.
|
||||||
// Subsequent calls replace the previously set collection atomically.
|
// Subsequent calls replace the previously set collection atomically.
|
||||||
func (obj *World) ResExport(resourceList []resources.Res) error {
|
func (obj *World) ResExport(resourceList []resources.Res) error {
|
||||||
return EtcdSetResources(obj.EmbdEtcd, obj.Hostname, resourceList)
|
return SetResources(obj.EmbdEtcd, obj.Hostname, resourceList)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResCollect gets the collection of exported resources which match the filter.
|
// ResCollect gets the collection of exported resources which match the filter.
|
||||||
@@ -39,5 +45,51 @@ func (obj *World) ResCollect(hostnameFilter, kindFilter []string) ([]resources.R
|
|||||||
// XXX: should we be restricted to retrieving resources that were
|
// XXX: should we be restricted to retrieving resources that were
|
||||||
// exported with a tag that allows or restricts our hostname? We could
|
// exported with a tag that allows or restricts our hostname? We could
|
||||||
// enforce that here if the underlying API supported it... Add this?
|
// enforce that here if the underlying API supported it... Add this?
|
||||||
return EtcdGetResources(obj.EmbdEtcd, hostnameFilter, kindFilter)
|
return GetResources(obj.EmbdEtcd, hostnameFilter, kindFilter)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StrWatch returns a channel which spits out events on possible string changes.
|
||||||
|
func (obj *World) StrWatch(namespace string) chan error {
|
||||||
|
return WatchStr(obj.EmbdEtcd, namespace)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StrIsNotExist returns whether the error from StrGet is a key missing error.
|
||||||
|
func (obj *World) StrIsNotExist(err error) bool {
|
||||||
|
return err == ErrNotExist
|
||||||
|
}
|
||||||
|
|
||||||
|
// StrGet returns the value for the the given namespace.
|
||||||
|
func (obj *World) StrGet(namespace string) (string, error) {
|
||||||
|
return GetStr(obj.EmbdEtcd, namespace)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StrSet sets the namespace value to a particular string.
|
||||||
|
func (obj *World) StrSet(namespace, value string) error {
|
||||||
|
return SetStr(obj.EmbdEtcd, namespace, &value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StrDel deletes the value in a particular namespace.
|
||||||
|
func (obj *World) StrDel(namespace string) error {
|
||||||
|
return SetStr(obj.EmbdEtcd, namespace, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StrMapWatch returns a channel which spits out events on possible string changes.
|
||||||
|
func (obj *World) StrMapWatch(namespace string) chan error {
|
||||||
|
return WatchStrMap(obj.EmbdEtcd, namespace)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StrMapGet returns a map of hostnames to values in the given namespace.
|
||||||
|
func (obj *World) StrMapGet(namespace string) (map[string]string, error) {
|
||||||
|
return GetStrMap(obj.EmbdEtcd, []string{}, namespace)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StrMapSet sets the namespace value to a particular string under the identity
|
||||||
|
// of its own hostname.
|
||||||
|
func (obj *World) StrMapSet(namespace, value string) error {
|
||||||
|
return SetStrMap(obj.EmbdEtcd, obj.Hostname, namespace, &value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StrMapDel deletes the value in a particular namespace.
|
||||||
|
func (obj *World) StrMapDel(namespace string) error {
|
||||||
|
return SetStrMap(obj.EmbdEtcd, obj.Hostname, namespace, nil)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
// Mgmt
|
// Mgmt
|
||||||
// Copyright (C) 2013-2016+ James Shubin and the project contributors
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// GNU Affero General Public License for more details.
|
// GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
// Package event provides some primitives that are used for message passing.
|
// Package event provides some primitives that are used for message passing.
|
||||||
@@ -22,14 +22,14 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:generate stringer -type=EventName -output=eventname_stringer.go
|
//go:generate stringer -type=Kind -output=kind_stringer.go
|
||||||
|
|
||||||
// EventName represents the type of event being passed.
|
// Kind represents the type of event being passed.
|
||||||
type EventName int
|
type Kind int
|
||||||
|
|
||||||
// The different event names are used in different contexts.
|
// The different event kinds are used in different contexts.
|
||||||
const (
|
const (
|
||||||
EventNil EventName = iota
|
EventNil Kind = iota
|
||||||
EventExit
|
EventExit
|
||||||
EventStart
|
EventStart
|
||||||
EventPause
|
EventPause
|
||||||
@@ -43,11 +43,10 @@ type Resp chan error
|
|||||||
|
|
||||||
// Event is the main struct that stores event information and responses.
|
// Event is the main struct that stores event information and responses.
|
||||||
type Event struct {
|
type Event struct {
|
||||||
Name EventName
|
Kind Kind
|
||||||
Resp Resp // channel to send an ack response on, nil to skip
|
Resp Resp // channel to send an ack response on, nil to skip
|
||||||
//Wg *sync.WaitGroup // receiver barrier to Wait() for everyone else on
|
//Wg *sync.WaitGroup // receiver barrier to Wait() for everyone else on
|
||||||
Msg string // some words for fun
|
Err error // store an error in our event
|
||||||
Activity bool // did something interesting happen?
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ACK sends a single acknowledgement on the channel if one was requested.
|
// ACK sends a single acknowledgement on the channel if one was requested.
|
||||||
@@ -80,7 +79,7 @@ func NewResp() Resp {
|
|||||||
// ACK sends a true value to resp.
|
// ACK sends a true value to resp.
|
||||||
func (resp Resp) ACK() {
|
func (resp Resp) ACK() {
|
||||||
if resp != nil {
|
if resp != nil {
|
||||||
resp <- nil
|
resp <- nil // TODO: close instead?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,7 +113,7 @@ func (resp Resp) ACKWait() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetActivity returns the activity value.
|
// Error returns the stored error value.
|
||||||
func (event *Event) GetActivity() bool {
|
func (event *Event) Error() error {
|
||||||
return event.Activity
|
return event.Err
|
||||||
}
|
}
|
||||||
|
|||||||
11
examples/augeas1.yaml
Normal file
11
examples/augeas1.yaml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
augeas:
|
||||||
|
- name: sshd_config
|
||||||
|
lens: Sshd.lns
|
||||||
|
file: "/etc/ssh/sshd_config"
|
||||||
|
sets:
|
||||||
|
- path: X11Forwarding
|
||||||
|
value: false
|
||||||
|
edges:
|
||||||
@@ -5,11 +5,13 @@ resources:
|
|||||||
- name: drbd-utils
|
- name: drbd-utils
|
||||||
meta:
|
meta:
|
||||||
autoedge: true
|
autoedge: true
|
||||||
|
noop: true
|
||||||
state: installed
|
state: installed
|
||||||
file:
|
file:
|
||||||
- name: file1
|
- name: file1
|
||||||
meta:
|
meta:
|
||||||
autoedge: true
|
autoedge: true
|
||||||
|
noop: true
|
||||||
path: "/etc/drbd.conf"
|
path: "/etc/drbd.conf"
|
||||||
content: |
|
content: |
|
||||||
# this is an mgmt test
|
# this is an mgmt test
|
||||||
@@ -17,13 +19,14 @@ resources:
|
|||||||
- name: file2
|
- name: file2
|
||||||
meta:
|
meta:
|
||||||
autoedge: true
|
autoedge: true
|
||||||
|
noop: true
|
||||||
path: "/etc/drbd.d/"
|
path: "/etc/drbd.d/"
|
||||||
content: |
|
source: /dev/null
|
||||||
i am a directory
|
|
||||||
state: exists
|
state: exists
|
||||||
svc:
|
svc:
|
||||||
- name: drbd
|
- name: drbd
|
||||||
meta:
|
meta:
|
||||||
autoedge: true
|
autoedge: true
|
||||||
|
noop: true
|
||||||
state: stopped
|
state: stopped
|
||||||
edges: []
|
edges: []
|
||||||
|
|||||||
19
examples/autoedges4.yaml
Normal file
19
examples/autoedges4.yaml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
user:
|
||||||
|
- name: edgeuser
|
||||||
|
state: absent
|
||||||
|
gid: 10000
|
||||||
|
- name: edgeuser2
|
||||||
|
state: exists
|
||||||
|
group: edgegroup
|
||||||
|
groups: [edgegroup2, edgegroup3]
|
||||||
|
group:
|
||||||
|
- name: edgegroup
|
||||||
|
state: exists
|
||||||
|
gid: 10000
|
||||||
|
- name: edgegroup2
|
||||||
|
state: exists
|
||||||
|
- name: edgegroup3
|
||||||
|
state: exists
|
||||||
10
examples/aws_ec2_1.yaml
Normal file
10
examples/aws_ec2_1.yaml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
aws:ec2:
|
||||||
|
- name: ec2example
|
||||||
|
region: ca-central-1
|
||||||
|
type: t2.micro
|
||||||
|
imageid: ami-5ac17f3e
|
||||||
|
state: running
|
||||||
|
edges: []
|
||||||
9
examples/deep-dirs.yaml
Normal file
9
examples/deep-dirs.yaml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
file:
|
||||||
|
- name: file1
|
||||||
|
path: "/tmp/mgmt/a/b/c/f1"
|
||||||
|
content: |
|
||||||
|
i am f1
|
||||||
|
state: exists
|
||||||
@@ -2,15 +2,10 @@
|
|||||||
graph: mygraph
|
graph: mygraph
|
||||||
resources:
|
resources:
|
||||||
file:
|
file:
|
||||||
- name: file1a
|
- name: "@@filea"
|
||||||
path: "/tmp/mgmtA/f1a"
|
path: "/tmp/mgmtA/fA"
|
||||||
content: |
|
content: |
|
||||||
i am f1
|
i am fA, exported from host A
|
||||||
state: exists
|
|
||||||
- name: "@@file2a"
|
|
||||||
path: "/tmp/mgmtA/f2a"
|
|
||||||
content: |
|
|
||||||
i am f2, exported from host A
|
|
||||||
state: exists
|
state: exists
|
||||||
collect:
|
collect:
|
||||||
- kind: file
|
- kind: file
|
||||||
|
|||||||
@@ -2,15 +2,10 @@
|
|||||||
graph: mygraph
|
graph: mygraph
|
||||||
resources:
|
resources:
|
||||||
file:
|
file:
|
||||||
- name: file1b
|
- name: "@@fileb"
|
||||||
path: "/tmp/mgmtB/f1b"
|
path: "/tmp/mgmtB/fB"
|
||||||
content: |
|
content: |
|
||||||
i am f1
|
i am fB, exported from host B
|
||||||
state: exists
|
|
||||||
- name: "@@file2b"
|
|
||||||
path: "/tmp/mgmtB/f2b"
|
|
||||||
content: |
|
|
||||||
i am f2, exported from host B
|
|
||||||
state: exists
|
state: exists
|
||||||
collect:
|
collect:
|
||||||
- kind: file
|
- kind: file
|
||||||
|
|||||||
@@ -2,15 +2,10 @@
|
|||||||
graph: mygraph
|
graph: mygraph
|
||||||
resources:
|
resources:
|
||||||
file:
|
file:
|
||||||
- name: file1c
|
- name: "@@filec"
|
||||||
path: "/tmp/mgmtC/f1c"
|
path: "/tmp/mgmtC/fC"
|
||||||
content: |
|
content: |
|
||||||
i am f1
|
i am fC, exported from host C
|
||||||
state: exists
|
|
||||||
- name: "@@file2c"
|
|
||||||
path: "/tmp/mgmtC/f2c"
|
|
||||||
content: |
|
|
||||||
i am f2, exported from host C
|
|
||||||
state: exists
|
state: exists
|
||||||
collect:
|
collect:
|
||||||
- kind: file
|
- kind: file
|
||||||
|
|||||||
@@ -2,15 +2,10 @@
|
|||||||
graph: mygraph
|
graph: mygraph
|
||||||
resources:
|
resources:
|
||||||
file:
|
file:
|
||||||
- name: file1d
|
- name: "@@filed"
|
||||||
path: "/tmp/mgmtD/f1d"
|
path: "/tmp/mgmtD/fD"
|
||||||
content: |
|
content: |
|
||||||
i am f1
|
i am fD, exported from host D
|
||||||
state: exists
|
|
||||||
- name: "@@file2d"
|
|
||||||
path: "/tmp/mgmtD/f2d"
|
|
||||||
content: |
|
|
||||||
i am f2, exported from host D
|
|
||||||
state: exists
|
state: exists
|
||||||
collect:
|
collect:
|
||||||
- kind: file
|
- kind: file
|
||||||
|
|||||||
13
examples/etcd1e.yaml
Normal file
13
examples/etcd1e.yaml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
file:
|
||||||
|
- name: "@@filee"
|
||||||
|
path: "/tmp/mgmtE/fE"
|
||||||
|
content: |
|
||||||
|
i am fE, exported from host E
|
||||||
|
state: exists
|
||||||
|
collect:
|
||||||
|
- kind: file
|
||||||
|
pattern: "/tmp/mgmtE/"
|
||||||
|
edges: []
|
||||||
67
examples/exec3-sema.yaml
Normal file
67
examples/exec3-sema.yaml
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
---
|
||||||
|
graph: parallel
|
||||||
|
resources:
|
||||||
|
exec:
|
||||||
|
- name: pkg10
|
||||||
|
meta:
|
||||||
|
sema: ['mylock:1', 'otherlock:42']
|
||||||
|
cmd: sleep 10s
|
||||||
|
shell: ''
|
||||||
|
timeout: 0
|
||||||
|
watchcmd: ''
|
||||||
|
watchshell: ''
|
||||||
|
ifcmd: ''
|
||||||
|
ifshell: ''
|
||||||
|
pollint: 0
|
||||||
|
state: present
|
||||||
|
- name: svc10
|
||||||
|
meta:
|
||||||
|
sema: ['mylock:1']
|
||||||
|
cmd: sleep 10s
|
||||||
|
shell: ''
|
||||||
|
timeout: 0
|
||||||
|
watchcmd: ''
|
||||||
|
watchshell: ''
|
||||||
|
ifcmd: ''
|
||||||
|
ifshell: ''
|
||||||
|
pollint: 0
|
||||||
|
state: present
|
||||||
|
- name: exec10
|
||||||
|
meta:
|
||||||
|
sema: ['mylock:1']
|
||||||
|
cmd: sleep 10s
|
||||||
|
shell: ''
|
||||||
|
timeout: 0
|
||||||
|
watchcmd: ''
|
||||||
|
watchshell: ''
|
||||||
|
ifcmd: ''
|
||||||
|
ifshell: ''
|
||||||
|
pollint: 0
|
||||||
|
state: present
|
||||||
|
- name: pkg15
|
||||||
|
meta:
|
||||||
|
sema: ['mylock:1', 'otherlock:42']
|
||||||
|
cmd: sleep 15s
|
||||||
|
shell: ''
|
||||||
|
timeout: 0
|
||||||
|
watchcmd: ''
|
||||||
|
watchshell: ''
|
||||||
|
ifcmd: ''
|
||||||
|
ifshell: ''
|
||||||
|
pollint: 0
|
||||||
|
state: present
|
||||||
|
edges:
|
||||||
|
- name: e1
|
||||||
|
from:
|
||||||
|
kind: exec
|
||||||
|
name: pkg10
|
||||||
|
to:
|
||||||
|
kind: exec
|
||||||
|
name: svc10
|
||||||
|
- name: e2
|
||||||
|
from:
|
||||||
|
kind: exec
|
||||||
|
name: svc10
|
||||||
|
to:
|
||||||
|
kind: exec
|
||||||
|
name: exec10
|
||||||
10
examples/file0.yaml
Normal file
10
examples/file0.yaml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
file:
|
||||||
|
- name: file0
|
||||||
|
path: "/tmp/mgmt/f1"
|
||||||
|
content: |
|
||||||
|
i am f0
|
||||||
|
state: exists
|
||||||
|
edges: []
|
||||||
@@ -1,14 +1,13 @@
|
|||||||
---
|
---
|
||||||
graph: mygraph
|
graph: mygraph
|
||||||
comment: You can test Watch and CheckApply failures with chmod ugo-r and chmod ugo-w.
|
|
||||||
resources:
|
resources:
|
||||||
file:
|
file:
|
||||||
- name: file1
|
- name: file1
|
||||||
path: "/tmp/mgmt/f1"
|
|
||||||
meta:
|
meta:
|
||||||
retry: 3
|
limit: .inf
|
||||||
delay: 5000
|
burst: 0
|
||||||
|
path: "/tmp/mgmt/hello"
|
||||||
content: |
|
content: |
|
||||||
i am f1
|
i am a file
|
||||||
state: exists
|
state: exists
|
||||||
edges: []
|
edges: []
|
||||||
|
|||||||
10
examples/file4.yaml
Normal file
10
examples/file4.yaml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
file:
|
||||||
|
- name: file1
|
||||||
|
path: "/tmp/mgmt/hello"
|
||||||
|
content: |
|
||||||
|
i am a file
|
||||||
|
state: exists
|
||||||
|
edges: []
|
||||||
14
examples/graph0.hcl
Normal file
14
examples/graph0.hcl
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
resource "file" "file1" {
|
||||||
|
path = "/tmp/mgmt-hello-world"
|
||||||
|
content = "hello, world"
|
||||||
|
state = "exists"
|
||||||
|
depends_on = ["noop.noop1", "exec.sleep"]
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "noop" "noop1" {
|
||||||
|
test = "nil"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "exec" "sleep" {
|
||||||
|
cmd = "sleep 10s"
|
||||||
|
}
|
||||||
4
examples/graph1.hcl
Normal file
4
examples/graph1.hcl
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
resource "exec" "exec1" {
|
||||||
|
cmd = "cat /tmp/mgmt-hello-world"
|
||||||
|
state = "present"
|
||||||
|
}
|
||||||
8
examples/group1.yaml
Normal file
8
examples/group1.yaml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
group:
|
||||||
|
- name: testgroup
|
||||||
|
state: exists
|
||||||
|
gid: 10000
|
||||||
|
edges: []
|
||||||
9
examples/hil.hcl
Normal file
9
examples/hil.hcl
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
resource "file" "file1" {
|
||||||
|
path = "/tmp/mgmt-hello-world"
|
||||||
|
content = "${exec.sleep.Output}"
|
||||||
|
state = "exists"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "exec" "sleep" {
|
||||||
|
cmd = "echo hello"
|
||||||
|
}
|
||||||
8
examples/kv1.yaml
Normal file
8
examples/kv1.yaml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
kv:
|
||||||
|
- name: kv1
|
||||||
|
key: "hello"
|
||||||
|
value: "world"
|
||||||
|
edges: []
|
||||||
7
examples/kv2.yaml
Normal file
7
examples/kv2.yaml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
kv:
|
||||||
|
- name: kv1
|
||||||
|
key: "iamdeleted"
|
||||||
|
edges: []
|
||||||
9
examples/kv3.yaml
Normal file
9
examples/kv3.yaml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
kv:
|
||||||
|
- name: kv1
|
||||||
|
key: "stage"
|
||||||
|
value: "3"
|
||||||
|
skiplessthan: true
|
||||||
|
edges: []
|
||||||
31
examples/kv4.yaml
Normal file
31
examples/kv4.yaml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
kv:
|
||||||
|
- name: kv1
|
||||||
|
key: "stage"
|
||||||
|
value: "1"
|
||||||
|
skiplessthan: true
|
||||||
|
- name: kv2
|
||||||
|
key: "stage"
|
||||||
|
value: "2"
|
||||||
|
skiplessthan: true
|
||||||
|
- name: kv3
|
||||||
|
key: "stage"
|
||||||
|
value: "3"
|
||||||
|
skiplessthan: true
|
||||||
|
edges:
|
||||||
|
- name: e1
|
||||||
|
from:
|
||||||
|
kind: kv
|
||||||
|
name: kv1
|
||||||
|
to:
|
||||||
|
kind: kv
|
||||||
|
name: kv2
|
||||||
|
- name: e2
|
||||||
|
from:
|
||||||
|
kind: kv
|
||||||
|
name: kv2
|
||||||
|
to:
|
||||||
|
kind: kv
|
||||||
|
name: kv3
|
||||||
246
examples/lib/exec-send-recv.go
Normal file
246
examples/lib/exec-send-recv.go
Normal file
@@ -0,0 +1,246 @@
|
|||||||
|
// libmgmt example of send->recv
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/purpleidea/mgmt/gapi"
|
||||||
|
mgmt "github.com/purpleidea/mgmt/lib"
|
||||||
|
"github.com/purpleidea/mgmt/pgraph"
|
||||||
|
"github.com/purpleidea/mgmt/resources"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MyGAPI implements the main GAPI interface.
|
||||||
|
type MyGAPI struct {
|
||||||
|
Name string // graph name
|
||||||
|
Interval uint // refresh interval, 0 to never refresh
|
||||||
|
|
||||||
|
data gapi.Data
|
||||||
|
initialized bool
|
||||||
|
closeChan chan struct{}
|
||||||
|
wg sync.WaitGroup // sync group for tunnel go routines
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMyGAPI creates a new MyGAPI struct and calls Init().
|
||||||
|
func NewMyGAPI(data gapi.Data, name string, interval uint) (*MyGAPI, error) {
|
||||||
|
obj := &MyGAPI{
|
||||||
|
Name: name,
|
||||||
|
Interval: interval,
|
||||||
|
}
|
||||||
|
return obj, obj.Init(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init initializes the MyGAPI struct.
|
||||||
|
func (obj *MyGAPI) Init(data gapi.Data) error {
|
||||||
|
if obj.initialized {
|
||||||
|
return fmt.Errorf("already initialized")
|
||||||
|
}
|
||||||
|
if obj.Name == "" {
|
||||||
|
return fmt.Errorf("the graph name must be specified")
|
||||||
|
}
|
||||||
|
obj.data = data // store for later
|
||||||
|
obj.closeChan = make(chan struct{})
|
||||||
|
obj.initialized = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Graph returns a current Graph.
|
||||||
|
func (obj *MyGAPI) Graph() (*pgraph.Graph, error) {
|
||||||
|
if !obj.initialized {
|
||||||
|
return nil, fmt.Errorf("libmgmt: MyGAPI is not initialized")
|
||||||
|
}
|
||||||
|
|
||||||
|
g, err := pgraph.NewGraph(obj.Name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
metaparams := resources.DefaultMetaParams
|
||||||
|
|
||||||
|
exec1 := &resources.ExecRes{
|
||||||
|
BaseRes: resources.BaseRes{
|
||||||
|
Name: "exec1",
|
||||||
|
Kind: "exec",
|
||||||
|
MetaParams: metaparams,
|
||||||
|
},
|
||||||
|
Cmd: "echo hello world && echo goodbye world 1>&2", // to stdout && stderr
|
||||||
|
Shell: "/bin/bash",
|
||||||
|
}
|
||||||
|
g.AddVertex(exec1)
|
||||||
|
|
||||||
|
output := &resources.FileRes{
|
||||||
|
BaseRes: resources.BaseRes{
|
||||||
|
Name: "output",
|
||||||
|
Kind: "file",
|
||||||
|
MetaParams: metaparams,
|
||||||
|
// send->recv!
|
||||||
|
Recv: map[string]*resources.Send{
|
||||||
|
"Content": {Res: exec1, Key: "Output"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Path: "/tmp/mgmt/output",
|
||||||
|
State: "present",
|
||||||
|
}
|
||||||
|
g.AddVertex(output)
|
||||||
|
g.AddEdge(exec1, output, &resources.Edge{Name: "e0"})
|
||||||
|
|
||||||
|
stdout := &resources.FileRes{
|
||||||
|
BaseRes: resources.BaseRes{
|
||||||
|
Name: "stdout",
|
||||||
|
Kind: "file",
|
||||||
|
MetaParams: metaparams,
|
||||||
|
// send->recv!
|
||||||
|
Recv: map[string]*resources.Send{
|
||||||
|
"Content": {Res: exec1, Key: "Stdout"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Path: "/tmp/mgmt/stdout",
|
||||||
|
State: "present",
|
||||||
|
}
|
||||||
|
g.AddVertex(stdout)
|
||||||
|
g.AddEdge(exec1, stdout, &resources.Edge{Name: "e1"})
|
||||||
|
|
||||||
|
stderr := &resources.FileRes{
|
||||||
|
BaseRes: resources.BaseRes{
|
||||||
|
Name: "stderr",
|
||||||
|
Kind: "file",
|
||||||
|
MetaParams: metaparams,
|
||||||
|
// send->recv!
|
||||||
|
Recv: map[string]*resources.Send{
|
||||||
|
"Content": {Res: exec1, Key: "Stderr"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Path: "/tmp/mgmt/stderr",
|
||||||
|
State: "present",
|
||||||
|
}
|
||||||
|
g.AddVertex(stderr)
|
||||||
|
g.AddEdge(exec1, stderr, &resources.Edge{Name: "e2"})
|
||||||
|
|
||||||
|
//g, err := config.NewGraphFromConfig(obj.data.Hostname, obj.data.World, obj.data.Noop)
|
||||||
|
return g, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next returns nil errors every time there could be a new graph.
|
||||||
|
func (obj *MyGAPI) Next() chan gapi.Next {
|
||||||
|
ch := make(chan gapi.Next)
|
||||||
|
obj.wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer obj.wg.Done()
|
||||||
|
defer close(ch) // this will run before the obj.wg.Done()
|
||||||
|
if !obj.initialized {
|
||||||
|
next := gapi.Next{
|
||||||
|
Err: fmt.Errorf("libmgmt: MyGAPI is not initialized"),
|
||||||
|
Exit: true, // exit, b/c programming error?
|
||||||
|
}
|
||||||
|
ch <- next
|
||||||
|
return
|
||||||
|
}
|
||||||
|
startChan := make(chan struct{}) // start signal
|
||||||
|
close(startChan) // kick it off!
|
||||||
|
|
||||||
|
ticker := make(<-chan time.Time)
|
||||||
|
if obj.data.NoStreamWatch || obj.Interval <= 0 {
|
||||||
|
ticker = nil
|
||||||
|
} else {
|
||||||
|
// arbitrarily change graph every interval seconds
|
||||||
|
t := time.NewTicker(time.Duration(obj.Interval) * time.Second)
|
||||||
|
defer t.Stop()
|
||||||
|
ticker = t.C
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-startChan: // kick the loop once at start
|
||||||
|
startChan = nil // disable
|
||||||
|
// pass
|
||||||
|
case <-ticker:
|
||||||
|
// pass
|
||||||
|
case <-obj.closeChan:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("libmgmt: Generating new graph...")
|
||||||
|
select {
|
||||||
|
case ch <- gapi.Next{}: // trigger a run
|
||||||
|
case <-obj.closeChan:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return ch
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close shuts down the MyGAPI.
|
||||||
|
func (obj *MyGAPI) Close() error {
|
||||||
|
if !obj.initialized {
|
||||||
|
return fmt.Errorf("libmgmt: MyGAPI is not initialized")
|
||||||
|
}
|
||||||
|
close(obj.closeChan)
|
||||||
|
obj.wg.Wait()
|
||||||
|
obj.initialized = false // closed = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run runs an embedded mgmt server.
|
||||||
|
func Run() error {
|
||||||
|
|
||||||
|
obj := &mgmt.Main{}
|
||||||
|
obj.Program = "libmgmt" // TODO: set on compilation
|
||||||
|
obj.Version = "0.0.1" // TODO: set on compilation
|
||||||
|
obj.TmpPrefix = true // disable for easy debugging
|
||||||
|
//prefix := "/tmp/testprefix/"
|
||||||
|
//obj.Prefix = &p // enable for easy debugging
|
||||||
|
obj.IdealClusterSize = -1
|
||||||
|
obj.ConvergedTimeout = -1
|
||||||
|
obj.Noop = false // FIXME: careful!
|
||||||
|
|
||||||
|
obj.GAPI = &MyGAPI{ // graph API
|
||||||
|
Name: "libmgmt", // TODO: set on compilation
|
||||||
|
Interval: 60 * 10, // arbitrarily change graph every 15 seconds
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := obj.Init(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// install the exit signal handler
|
||||||
|
exit := make(chan struct{})
|
||||||
|
defer close(exit)
|
||||||
|
go func() {
|
||||||
|
signals := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(signals, os.Interrupt) // catch ^C
|
||||||
|
//signal.Notify(signals, os.Kill) // catch signals
|
||||||
|
signal.Notify(signals, syscall.SIGTERM)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case sig := <-signals: // any signal will do
|
||||||
|
if sig == os.Interrupt {
|
||||||
|
log.Println("Interrupted by ^C")
|
||||||
|
obj.Exit(nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Println("Interrupted by signal")
|
||||||
|
obj.Exit(fmt.Errorf("killed by %v", sig))
|
||||||
|
return
|
||||||
|
case <-exit:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return obj.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
log.Printf("Hello!")
|
||||||
|
if err := Run(); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("Goodbye!")
|
||||||
|
}
|
||||||
253
examples/lib/libmgmt-subgraph0.go
Normal file
253
examples/lib/libmgmt-subgraph0.go
Normal file
@@ -0,0 +1,253 @@
|
|||||||
|
// libmgmt example of flattened subgraph
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/purpleidea/mgmt/gapi"
|
||||||
|
mgmt "github.com/purpleidea/mgmt/lib"
|
||||||
|
"github.com/purpleidea/mgmt/pgraph"
|
||||||
|
"github.com/purpleidea/mgmt/resources"
|
||||||
|
|
||||||
|
errwrap "github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MyGAPI implements the main GAPI interface.
|
||||||
|
type MyGAPI struct {
|
||||||
|
Name string // graph name
|
||||||
|
Interval uint // refresh interval, 0 to never refresh
|
||||||
|
|
||||||
|
data gapi.Data
|
||||||
|
initialized bool
|
||||||
|
closeChan chan struct{}
|
||||||
|
wg sync.WaitGroup // sync group for tunnel go routines
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMyGAPI creates a new MyGAPI struct and calls Init().
|
||||||
|
func NewMyGAPI(data gapi.Data, name string, interval uint) (*MyGAPI, error) {
|
||||||
|
obj := &MyGAPI{
|
||||||
|
Name: name,
|
||||||
|
Interval: interval,
|
||||||
|
}
|
||||||
|
return obj, obj.Init(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init initializes the MyGAPI struct.
|
||||||
|
func (obj *MyGAPI) Init(data gapi.Data) error {
|
||||||
|
if obj.initialized {
|
||||||
|
return fmt.Errorf("already initialized")
|
||||||
|
}
|
||||||
|
if obj.Name == "" {
|
||||||
|
return fmt.Errorf("the graph name must be specified")
|
||||||
|
}
|
||||||
|
obj.data = data // store for later
|
||||||
|
obj.closeChan = make(chan struct{})
|
||||||
|
obj.initialized = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (obj *MyGAPI) subGraph() (*pgraph.Graph, error) {
|
||||||
|
g, err := pgraph.NewGraph(obj.Name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
metaparams := resources.DefaultMetaParams
|
||||||
|
|
||||||
|
f1 := &resources.FileRes{
|
||||||
|
BaseRes: resources.BaseRes{
|
||||||
|
Name: "file1",
|
||||||
|
Kind: "file",
|
||||||
|
MetaParams: metaparams,
|
||||||
|
},
|
||||||
|
Path: "/tmp/mgmt/sub1",
|
||||||
|
|
||||||
|
State: "present",
|
||||||
|
}
|
||||||
|
g.AddVertex(f1)
|
||||||
|
|
||||||
|
n1 := &resources.NoopRes{
|
||||||
|
BaseRes: resources.BaseRes{
|
||||||
|
Name: "noop1",
|
||||||
|
Kind: "noop",
|
||||||
|
MetaParams: metaparams,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
g.AddVertex(n1)
|
||||||
|
|
||||||
|
return g, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Graph returns a current Graph.
|
||||||
|
func (obj *MyGAPI) Graph() (*pgraph.Graph, error) {
|
||||||
|
if !obj.initialized {
|
||||||
|
return nil, fmt.Errorf("libmgmt: MyGAPI is not initialized")
|
||||||
|
}
|
||||||
|
|
||||||
|
g, err := pgraph.NewGraph(obj.Name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
metaparams := resources.DefaultMetaParams
|
||||||
|
|
||||||
|
content := "I created a subgraph!\n"
|
||||||
|
f0 := &resources.FileRes{
|
||||||
|
BaseRes: resources.BaseRes{
|
||||||
|
Name: "README",
|
||||||
|
MetaParams: metaparams,
|
||||||
|
},
|
||||||
|
Path: "/tmp/mgmt/README",
|
||||||
|
Content: &content,
|
||||||
|
State: "present",
|
||||||
|
}
|
||||||
|
g.AddVertex(f0)
|
||||||
|
|
||||||
|
subGraph, err := obj.subGraph()
|
||||||
|
if err != nil {
|
||||||
|
return nil, errwrap.Wrapf(err, "running subGraph() failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
edgeGenFn := func(v1, v2 pgraph.Vertex) pgraph.Edge {
|
||||||
|
edge := &resources.Edge{
|
||||||
|
Name: fmt.Sprintf("edge: %s->%s", v1, v2),
|
||||||
|
}
|
||||||
|
|
||||||
|
// if we want to do something specific based on input
|
||||||
|
_, v2IsFile := v2.(*resources.FileRes)
|
||||||
|
if v1 == f0 && v2IsFile {
|
||||||
|
edge.Notify = true
|
||||||
|
}
|
||||||
|
|
||||||
|
return edge
|
||||||
|
}
|
||||||
|
g.AddEdgeVertexGraph(f0, subGraph, edgeGenFn)
|
||||||
|
|
||||||
|
//g, err := config.NewGraphFromConfig(obj.data.Hostname, obj.data.World, obj.data.Noop)
|
||||||
|
return g, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next returns nil errors every time there could be a new graph.
|
||||||
|
func (obj *MyGAPI) Next() chan gapi.Next {
|
||||||
|
ch := make(chan gapi.Next)
|
||||||
|
obj.wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer obj.wg.Done()
|
||||||
|
defer close(ch) // this will run before the obj.wg.Done()
|
||||||
|
if !obj.initialized {
|
||||||
|
next := gapi.Next{
|
||||||
|
Err: fmt.Errorf("libmgmt: MyGAPI is not initialized"),
|
||||||
|
Exit: true, // exit, b/c programming error?
|
||||||
|
}
|
||||||
|
ch <- next
|
||||||
|
return
|
||||||
|
}
|
||||||
|
startChan := make(chan struct{}) // start signal
|
||||||
|
close(startChan) // kick it off!
|
||||||
|
|
||||||
|
ticker := make(<-chan time.Time)
|
||||||
|
if obj.data.NoStreamWatch || obj.Interval <= 0 {
|
||||||
|
ticker = nil
|
||||||
|
} else {
|
||||||
|
// arbitrarily change graph every interval seconds
|
||||||
|
t := time.NewTicker(time.Duration(obj.Interval) * time.Second)
|
||||||
|
defer t.Stop()
|
||||||
|
ticker = t.C
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-startChan: // kick the loop once at start
|
||||||
|
startChan = nil // disable
|
||||||
|
// pass
|
||||||
|
case <-ticker:
|
||||||
|
// pass
|
||||||
|
case <-obj.closeChan:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("libmgmt: Generating new graph...")
|
||||||
|
select {
|
||||||
|
case ch <- gapi.Next{}: // trigger a run
|
||||||
|
case <-obj.closeChan:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return ch
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close shuts down the MyGAPI.
|
||||||
|
func (obj *MyGAPI) Close() error {
|
||||||
|
if !obj.initialized {
|
||||||
|
return fmt.Errorf("libmgmt: MyGAPI is not initialized")
|
||||||
|
}
|
||||||
|
close(obj.closeChan)
|
||||||
|
obj.wg.Wait()
|
||||||
|
obj.initialized = false // closed = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run runs an embedded mgmt server.
|
||||||
|
func Run() error {
|
||||||
|
|
||||||
|
obj := &mgmt.Main{}
|
||||||
|
obj.Program = "libmgmt" // TODO: set on compilation
|
||||||
|
obj.Version = "0.0.1" // TODO: set on compilation
|
||||||
|
obj.TmpPrefix = true // disable for easy debugging
|
||||||
|
//prefix := "/tmp/testprefix/"
|
||||||
|
//obj.Prefix = &p // enable for easy debugging
|
||||||
|
obj.IdealClusterSize = -1
|
||||||
|
obj.ConvergedTimeout = -1
|
||||||
|
obj.Noop = false // FIXME: careful!
|
||||||
|
|
||||||
|
obj.GAPI = &MyGAPI{ // graph API
|
||||||
|
Name: "libmgmt", // TODO: set on compilation
|
||||||
|
Interval: 60 * 10, // arbitrarily change graph every 15 seconds
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := obj.Init(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// install the exit signal handler
|
||||||
|
exit := make(chan struct{})
|
||||||
|
defer close(exit)
|
||||||
|
go func() {
|
||||||
|
signals := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(signals, os.Interrupt) // catch ^C
|
||||||
|
//signal.Notify(signals, os.Kill) // catch signals
|
||||||
|
signal.Notify(signals, syscall.SIGTERM)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case sig := <-signals: // any signal will do
|
||||||
|
if sig == os.Interrupt {
|
||||||
|
log.Println("Interrupted by ^C")
|
||||||
|
obj.Exit(nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Println("Interrupted by signal")
|
||||||
|
obj.Exit(fmt.Errorf("killed by %v", sig))
|
||||||
|
return
|
||||||
|
case <-exit:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return obj.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
log.Printf("Hello!")
|
||||||
|
if err := Run(); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("Goodbye!")
|
||||||
|
}
|
||||||
243
examples/lib/libmgmt-subgraph1.go
Normal file
243
examples/lib/libmgmt-subgraph1.go
Normal file
@@ -0,0 +1,243 @@
|
|||||||
|
// libmgmt example of graph resource
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/purpleidea/mgmt/gapi"
|
||||||
|
mgmt "github.com/purpleidea/mgmt/lib"
|
||||||
|
"github.com/purpleidea/mgmt/pgraph"
|
||||||
|
"github.com/purpleidea/mgmt/resources"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MyGAPI implements the main GAPI interface.
|
||||||
|
type MyGAPI struct {
|
||||||
|
Name string // graph name
|
||||||
|
Interval uint // refresh interval, 0 to never refresh
|
||||||
|
|
||||||
|
data gapi.Data
|
||||||
|
initialized bool
|
||||||
|
closeChan chan struct{}
|
||||||
|
wg sync.WaitGroup // sync group for tunnel go routines
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMyGAPI creates a new MyGAPI struct and calls Init().
|
||||||
|
func NewMyGAPI(data gapi.Data, name string, interval uint) (*MyGAPI, error) {
|
||||||
|
obj := &MyGAPI{
|
||||||
|
Name: name,
|
||||||
|
Interval: interval,
|
||||||
|
}
|
||||||
|
return obj, obj.Init(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init initializes the MyGAPI struct.
|
||||||
|
func (obj *MyGAPI) Init(data gapi.Data) error {
|
||||||
|
if obj.initialized {
|
||||||
|
return fmt.Errorf("already initialized")
|
||||||
|
}
|
||||||
|
if obj.Name == "" {
|
||||||
|
return fmt.Errorf("the graph name must be specified")
|
||||||
|
}
|
||||||
|
obj.data = data // store for later
|
||||||
|
obj.closeChan = make(chan struct{})
|
||||||
|
obj.initialized = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Graph returns a current Graph.
|
||||||
|
func (obj *MyGAPI) Graph() (*pgraph.Graph, error) {
|
||||||
|
if !obj.initialized {
|
||||||
|
return nil, fmt.Errorf("libmgmt: MyGAPI is not initialized")
|
||||||
|
}
|
||||||
|
|
||||||
|
g, err := pgraph.NewGraph(obj.Name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
metaparams := resources.DefaultMetaParams
|
||||||
|
|
||||||
|
content := "I created a subgraph!\n"
|
||||||
|
f0 := &resources.FileRes{
|
||||||
|
BaseRes: resources.BaseRes{
|
||||||
|
Name: "README",
|
||||||
|
Kind: "file",
|
||||||
|
MetaParams: metaparams,
|
||||||
|
},
|
||||||
|
Path: "/tmp/mgmt/README",
|
||||||
|
Content: &content,
|
||||||
|
State: "present",
|
||||||
|
}
|
||||||
|
g.AddVertex(f0)
|
||||||
|
|
||||||
|
// create a subgraph to add *into* a graph resource
|
||||||
|
subGraph, err := pgraph.NewGraph(fmt.Sprintf("%s->subgraph", obj.Name))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// add elements into the sub graph
|
||||||
|
f1 := &resources.FileRes{
|
||||||
|
BaseRes: resources.BaseRes{
|
||||||
|
Name: "file1",
|
||||||
|
Kind: "file",
|
||||||
|
MetaParams: metaparams,
|
||||||
|
},
|
||||||
|
Path: "/tmp/mgmt/sub1",
|
||||||
|
|
||||||
|
State: "present",
|
||||||
|
}
|
||||||
|
subGraph.AddVertex(f1)
|
||||||
|
|
||||||
|
n1 := &resources.NoopRes{
|
||||||
|
BaseRes: resources.BaseRes{
|
||||||
|
Name: "noop1",
|
||||||
|
Kind: "noop",
|
||||||
|
MetaParams: metaparams,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
subGraph.AddVertex(n1)
|
||||||
|
|
||||||
|
e0 := &resources.Edge{Name: "e0"}
|
||||||
|
e0.Notify = true // send a notification from v0 to v1
|
||||||
|
subGraph.AddEdge(f1, n1, e0)
|
||||||
|
|
||||||
|
// create the actual resource to hold the sub graph
|
||||||
|
subGraphRes0 := &resources.GraphRes{ // TODO: should we name this SubGraphRes ?
|
||||||
|
BaseRes: resources.BaseRes{
|
||||||
|
Name: "subgraph1",
|
||||||
|
Kind: "graph",
|
||||||
|
MetaParams: metaparams,
|
||||||
|
},
|
||||||
|
Graph: subGraph,
|
||||||
|
}
|
||||||
|
g.AddVertex(subGraphRes0) // add it to the main graph
|
||||||
|
|
||||||
|
//g, err := config.NewGraphFromConfig(obj.data.Hostname, obj.data.World, obj.data.Noop)
|
||||||
|
return g, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next returns nil errors every time there could be a new graph.
|
||||||
|
func (obj *MyGAPI) Next() chan gapi.Next {
|
||||||
|
ch := make(chan gapi.Next)
|
||||||
|
obj.wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer obj.wg.Done()
|
||||||
|
defer close(ch) // this will run before the obj.wg.Done()
|
||||||
|
if !obj.initialized {
|
||||||
|
next := gapi.Next{
|
||||||
|
Err: fmt.Errorf("libmgmt: MyGAPI is not initialized"),
|
||||||
|
Exit: true, // exit, b/c programming error?
|
||||||
|
}
|
||||||
|
ch <- next
|
||||||
|
return
|
||||||
|
}
|
||||||
|
startChan := make(chan struct{}) // start signal
|
||||||
|
close(startChan) // kick it off!
|
||||||
|
|
||||||
|
ticker := make(<-chan time.Time)
|
||||||
|
if obj.data.NoStreamWatch || obj.Interval <= 0 {
|
||||||
|
ticker = nil
|
||||||
|
} else {
|
||||||
|
// arbitrarily change graph every interval seconds
|
||||||
|
t := time.NewTicker(time.Duration(obj.Interval) * time.Second)
|
||||||
|
defer t.Stop()
|
||||||
|
ticker = t.C
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-startChan: // kick the loop once at start
|
||||||
|
startChan = nil // disable
|
||||||
|
// pass
|
||||||
|
case <-ticker:
|
||||||
|
// pass
|
||||||
|
case <-obj.closeChan:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("libmgmt: Generating new graph...")
|
||||||
|
select {
|
||||||
|
case ch <- gapi.Next{}: // trigger a run
|
||||||
|
case <-obj.closeChan:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return ch
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close shuts down the MyGAPI.
|
||||||
|
func (obj *MyGAPI) Close() error {
|
||||||
|
if !obj.initialized {
|
||||||
|
return fmt.Errorf("libmgmt: MyGAPI is not initialized")
|
||||||
|
}
|
||||||
|
close(obj.closeChan)
|
||||||
|
obj.wg.Wait()
|
||||||
|
obj.initialized = false // closed = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run runs an embedded mgmt server.
|
||||||
|
func Run() error {
|
||||||
|
|
||||||
|
obj := &mgmt.Main{}
|
||||||
|
obj.Program = "libmgmt" // TODO: set on compilation
|
||||||
|
obj.Version = "0.0.1" // TODO: set on compilation
|
||||||
|
obj.TmpPrefix = true // disable for easy debugging
|
||||||
|
//prefix := "/tmp/testprefix/"
|
||||||
|
//obj.Prefix = &p // enable for easy debugging
|
||||||
|
obj.IdealClusterSize = -1
|
||||||
|
obj.ConvergedTimeout = -1
|
||||||
|
obj.Noop = false // FIXME: careful!
|
||||||
|
|
||||||
|
obj.GAPI = &MyGAPI{ // graph API
|
||||||
|
Name: "libmgmt", // TODO: set on compilation
|
||||||
|
Interval: 60 * 10, // arbitrarily change graph every 15 seconds
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := obj.Init(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// install the exit signal handler
|
||||||
|
exit := make(chan struct{})
|
||||||
|
defer close(exit)
|
||||||
|
go func() {
|
||||||
|
signals := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(signals, os.Interrupt) // catch ^C
|
||||||
|
//signal.Notify(signals, os.Kill) // catch signals
|
||||||
|
signal.Notify(signals, syscall.SIGTERM)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case sig := <-signals: // any signal will do
|
||||||
|
if sig == os.Interrupt {
|
||||||
|
log.Println("Interrupted by ^C")
|
||||||
|
obj.Exit(nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Println("Interrupted by signal")
|
||||||
|
obj.Exit(fmt.Errorf("killed by %v", sig))
|
||||||
|
return
|
||||||
|
case <-exit:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return obj.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
log.Printf("Hello!")
|
||||||
|
if err := Run(); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("Goodbye!")
|
||||||
|
}
|
||||||
@@ -40,10 +40,10 @@ func NewMyGAPI(data gapi.Data, name string, interval uint) (*MyGAPI, error) {
|
|||||||
// Init initializes the MyGAPI struct.
|
// Init initializes the MyGAPI struct.
|
||||||
func (obj *MyGAPI) Init(data gapi.Data) error {
|
func (obj *MyGAPI) Init(data gapi.Data) error {
|
||||||
if obj.initialized {
|
if obj.initialized {
|
||||||
return fmt.Errorf("Already initialized!")
|
return fmt.Errorf("already initialized")
|
||||||
}
|
}
|
||||||
if obj.Name == "" {
|
if obj.Name == "" {
|
||||||
return fmt.Errorf("The graph name must be specified!")
|
return fmt.Errorf("the graph name must be specified")
|
||||||
}
|
}
|
||||||
obj.data = data // store for later
|
obj.data = data // store for later
|
||||||
obj.closeChan = make(chan struct{})
|
obj.closeChan = make(chan struct{})
|
||||||
@@ -57,11 +57,13 @@ func (obj *MyGAPI) Graph() (*pgraph.Graph, error) {
|
|||||||
return nil, fmt.Errorf("libmgmt: MyGAPI is not initialized")
|
return nil, fmt.Errorf("libmgmt: MyGAPI is not initialized")
|
||||||
}
|
}
|
||||||
|
|
||||||
n1, err := resources.NewNoopRes("noop1")
|
n1, err := resources.NewNamedResource("noop", "noop1")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Can't create resource: %v", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE: This is considered the legacy method to build graphs. Avoid
|
||||||
|
// importing the legacy `yamlgraph` lib if possible for custom graphs.
|
||||||
// we can still build a graph via the yaml method
|
// we can still build a graph via the yaml method
|
||||||
gc := &yamlgraph.GraphConfig{
|
gc := &yamlgraph.GraphConfig{
|
||||||
Graph: obj.Name,
|
Graph: obj.Name,
|
||||||
@@ -70,7 +72,7 @@ func (obj *MyGAPI) Graph() (*pgraph.Graph, error) {
|
|||||||
Exec: []*resources.ExecRes{},
|
Exec: []*resources.ExecRes{},
|
||||||
File: []*resources.FileRes{},
|
File: []*resources.FileRes{},
|
||||||
Msg: []*resources.MsgRes{},
|
Msg: []*resources.MsgRes{},
|
||||||
Noop: []*resources.NoopRes{n1},
|
Noop: []*resources.NoopRes{n1.(*resources.NoopRes)},
|
||||||
Pkg: []*resources.PkgRes{},
|
Pkg: []*resources.PkgRes{},
|
||||||
Svc: []*resources.SvcRes{},
|
Svc: []*resources.SvcRes{},
|
||||||
Timer: []*resources.TimerRes{},
|
Timer: []*resources.TimerRes{},
|
||||||
@@ -86,28 +88,45 @@ func (obj *MyGAPI) Graph() (*pgraph.Graph, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Next returns nil errors every time there could be a new graph.
|
// Next returns nil errors every time there could be a new graph.
|
||||||
func (obj *MyGAPI) Next() chan error {
|
func (obj *MyGAPI) Next() chan gapi.Next {
|
||||||
if obj.data.NoWatch || obj.Interval <= 0 {
|
ch := make(chan gapi.Next)
|
||||||
return nil
|
|
||||||
}
|
|
||||||
ch := make(chan error)
|
|
||||||
obj.wg.Add(1)
|
obj.wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer obj.wg.Done()
|
defer obj.wg.Done()
|
||||||
defer close(ch) // this will run before the obj.wg.Done()
|
defer close(ch) // this will run before the obj.wg.Done()
|
||||||
if !obj.initialized {
|
if !obj.initialized {
|
||||||
ch <- fmt.Errorf("libmgmt: MyGAPI is not initialized")
|
next := gapi.Next{
|
||||||
|
Err: fmt.Errorf("libmgmt: MyGAPI is not initialized"),
|
||||||
|
Exit: true, // exit, b/c programming error?
|
||||||
|
}
|
||||||
|
ch <- next
|
||||||
|
}
|
||||||
|
startChan := make(chan struct{}) // start signal
|
||||||
|
close(startChan) // kick it off!
|
||||||
|
|
||||||
|
ticker := make(<-chan time.Time)
|
||||||
|
if obj.data.NoStreamWatch || obj.Interval <= 0 {
|
||||||
|
ticker = nil
|
||||||
|
} else {
|
||||||
|
// arbitrarily change graph every interval seconds
|
||||||
|
t := time.NewTicker(time.Duration(obj.Interval) * time.Second)
|
||||||
|
defer t.Stop()
|
||||||
|
ticker = t.C
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-startChan: // kick the loop once at start
|
||||||
|
startChan = nil // disable
|
||||||
|
// pass
|
||||||
|
case <-ticker:
|
||||||
|
// pass
|
||||||
|
case <-obj.closeChan:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// arbitrarily change graph every interval seconds
|
|
||||||
ticker := time.NewTicker(time.Duration(obj.Interval) * time.Second)
|
|
||||||
defer ticker.Stop()
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ticker.C:
|
|
||||||
log.Printf("libmgmt: Generating new graph...")
|
log.Printf("libmgmt: Generating new graph...")
|
||||||
ch <- nil // trigger a run
|
select {
|
||||||
|
case ch <- gapi.Next{}: // trigger a run
|
||||||
case <-obj.closeChan:
|
case <-obj.closeChan:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -164,17 +183,14 @@ func Run() error {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Println("Interrupted by signal")
|
log.Println("Interrupted by signal")
|
||||||
obj.Exit(fmt.Errorf("Killed by %v", sig))
|
obj.Exit(fmt.Errorf("killed by %v", sig))
|
||||||
return
|
return
|
||||||
case <-exit:
|
case <-exit:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if err := obj.Run(); err != nil {
|
return obj.Run()
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -42,10 +42,10 @@ func NewMyGAPI(data gapi.Data, name string, interval uint, count uint) (*MyGAPI,
|
|||||||
// Init initializes the MyGAPI struct.
|
// Init initializes the MyGAPI struct.
|
||||||
func (obj *MyGAPI) Init(data gapi.Data) error {
|
func (obj *MyGAPI) Init(data gapi.Data) error {
|
||||||
if obj.initialized {
|
if obj.initialized {
|
||||||
return fmt.Errorf("Already initialized!")
|
return fmt.Errorf("already initialized")
|
||||||
}
|
}
|
||||||
if obj.Name == "" {
|
if obj.Name == "" {
|
||||||
return fmt.Errorf("The graph name must be specified!")
|
return fmt.Errorf("the graph name must be specified")
|
||||||
}
|
}
|
||||||
obj.data = data // store for later
|
obj.data = data // store for later
|
||||||
obj.closeChan = make(chan struct{})
|
obj.closeChan = make(chan struct{})
|
||||||
@@ -59,19 +59,21 @@ func (obj *MyGAPI) Graph() (*pgraph.Graph, error) {
|
|||||||
return nil, fmt.Errorf("libmgmt: MyGAPI is not initialized")
|
return nil, fmt.Errorf("libmgmt: MyGAPI is not initialized")
|
||||||
}
|
}
|
||||||
|
|
||||||
g := pgraph.NewGraph(obj.Name)
|
g, err := pgraph.NewGraph(obj.Name)
|
||||||
var vertex *pgraph.Vertex
|
|
||||||
for i := uint(0); i < obj.Count; i++ {
|
|
||||||
n, err := resources.NewNoopRes(fmt.Sprintf("noop%d", i))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Can't create resource: %v", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
v := pgraph.NewVertex(n)
|
var vertex pgraph.Vertex
|
||||||
g.AddVertex(v)
|
for i := uint(0); i < obj.Count; i++ {
|
||||||
|
n, err := resources.NewNamedResource("noop", fmt.Sprintf("noop%d", i))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
g.AddVertex(n)
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
g.AddEdge(vertex, v, pgraph.NewEdge(fmt.Sprintf("e%d", i)))
|
g.AddEdge(vertex, n, &resources.Edge{Name: fmt.Sprintf("e%d", i)})
|
||||||
}
|
}
|
||||||
vertex = v // save
|
vertex = n // save
|
||||||
}
|
}
|
||||||
|
|
||||||
//g, err := config.NewGraphFromConfig(obj.data.Hostname, obj.data.World, obj.data.Noop)
|
//g, err := config.NewGraphFromConfig(obj.data.Hostname, obj.data.World, obj.data.Noop)
|
||||||
@@ -79,28 +81,45 @@ func (obj *MyGAPI) Graph() (*pgraph.Graph, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Next returns nil errors every time there could be a new graph.
|
// Next returns nil errors every time there could be a new graph.
|
||||||
func (obj *MyGAPI) Next() chan error {
|
func (obj *MyGAPI) Next() chan gapi.Next {
|
||||||
if obj.data.NoWatch || obj.Interval <= 0 {
|
ch := make(chan gapi.Next)
|
||||||
return nil
|
|
||||||
}
|
|
||||||
ch := make(chan error)
|
|
||||||
obj.wg.Add(1)
|
obj.wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer obj.wg.Done()
|
defer obj.wg.Done()
|
||||||
defer close(ch) // this will run before the obj.wg.Done()
|
defer close(ch) // this will run before the obj.wg.Done()
|
||||||
if !obj.initialized {
|
if !obj.initialized {
|
||||||
ch <- fmt.Errorf("libmgmt: MyGAPI is not initialized")
|
next := gapi.Next{
|
||||||
|
Err: fmt.Errorf("libmgmt: MyGAPI is not initialized"),
|
||||||
|
Exit: true, // exit, b/c programming error?
|
||||||
|
}
|
||||||
|
ch <- next
|
||||||
|
}
|
||||||
|
startChan := make(chan struct{}) // start signal
|
||||||
|
close(startChan) // kick it off!
|
||||||
|
|
||||||
|
ticker := make(<-chan time.Time)
|
||||||
|
if obj.data.NoStreamWatch || obj.Interval <= 0 {
|
||||||
|
ticker = nil
|
||||||
|
} else {
|
||||||
|
// arbitrarily change graph every interval seconds
|
||||||
|
t := time.NewTicker(time.Duration(obj.Interval) * time.Second)
|
||||||
|
defer t.Stop()
|
||||||
|
ticker = t.C
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-startChan: // kick the loop once at start
|
||||||
|
startChan = nil // disable
|
||||||
|
// pass
|
||||||
|
case <-ticker:
|
||||||
|
// pass
|
||||||
|
case <-obj.closeChan:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// arbitrarily change graph every interval seconds
|
|
||||||
ticker := time.NewTicker(time.Duration(obj.Interval) * time.Second)
|
|
||||||
defer ticker.Stop()
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ticker.C:
|
|
||||||
log.Printf("libmgmt: Generating new graph...")
|
log.Printf("libmgmt: Generating new graph...")
|
||||||
ch <- nil // trigger a run
|
select {
|
||||||
|
case ch <- gapi.Next{}: // trigger a run
|
||||||
case <-obj.closeChan:
|
case <-obj.closeChan:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -158,17 +177,14 @@ func Run(count uint) error {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Println("Interrupted by signal")
|
log.Println("Interrupted by signal")
|
||||||
obj.Exit(fmt.Errorf("Killed by %v", sig))
|
obj.Exit(fmt.Errorf("killed by %v", sig))
|
||||||
return
|
return
|
||||||
case <-exit:
|
case <-exit:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if err := obj.Run(); err != nil {
|
return obj.Run()
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -39,10 +39,10 @@ func NewMyGAPI(data gapi.Data, name string, interval uint) (*MyGAPI, error) {
|
|||||||
// Init initializes the MyGAPI struct.
|
// Init initializes the MyGAPI struct.
|
||||||
func (obj *MyGAPI) Init(data gapi.Data) error {
|
func (obj *MyGAPI) Init(data gapi.Data) error {
|
||||||
if obj.initialized {
|
if obj.initialized {
|
||||||
return fmt.Errorf("Already initialized!")
|
return fmt.Errorf("already initialized")
|
||||||
}
|
}
|
||||||
if obj.Name == "" {
|
if obj.Name == "" {
|
||||||
return fmt.Errorf("The graph name must be specified!")
|
return fmt.Errorf("the graph name must be specified")
|
||||||
}
|
}
|
||||||
obj.data = data // store for later
|
obj.data = data // store for later
|
||||||
obj.closeChan = make(chan struct{})
|
obj.closeChan = make(chan struct{})
|
||||||
@@ -56,34 +56,43 @@ func (obj *MyGAPI) Graph() (*pgraph.Graph, error) {
|
|||||||
return nil, fmt.Errorf("libmgmt: MyGAPI is not initialized")
|
return nil, fmt.Errorf("libmgmt: MyGAPI is not initialized")
|
||||||
}
|
}
|
||||||
|
|
||||||
g := pgraph.NewGraph(obj.Name)
|
g, err := pgraph.NewGraph(obj.Name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
metaparams := resources.DefaultMetaParams
|
||||||
|
|
||||||
content := "Delete me to trigger a notification!\n"
|
content := "Delete me to trigger a notification!\n"
|
||||||
f0 := &resources.FileRes{
|
f0 := &resources.FileRes{
|
||||||
BaseRes: resources.BaseRes{
|
BaseRes: resources.BaseRes{
|
||||||
Name: "README",
|
Name: "README",
|
||||||
|
Kind: "file",
|
||||||
|
MetaParams: metaparams,
|
||||||
},
|
},
|
||||||
Path: "/tmp/mgmt/README",
|
Path: "/tmp/mgmt/README",
|
||||||
Content: &content,
|
Content: &content,
|
||||||
State: "present",
|
State: "present",
|
||||||
}
|
}
|
||||||
|
|
||||||
v0 := pgraph.NewVertex(f0)
|
g.AddVertex(f0)
|
||||||
g.AddVertex(v0)
|
|
||||||
|
|
||||||
p1 := &resources.PasswordRes{
|
p1 := &resources.PasswordRes{
|
||||||
BaseRes: resources.BaseRes{
|
BaseRes: resources.BaseRes{
|
||||||
Name: "password1",
|
Name: "password1",
|
||||||
|
Kind: "password",
|
||||||
|
MetaParams: metaparams,
|
||||||
},
|
},
|
||||||
Length: 8, // generated string will have this many characters
|
Length: 8, // generated string will have this many characters
|
||||||
Saved: true, // this causes passwords to be stored in plain text!
|
Saved: true, // this causes passwords to be stored in plain text!
|
||||||
}
|
}
|
||||||
v1 := pgraph.NewVertex(p1)
|
g.AddVertex(p1)
|
||||||
g.AddVertex(v1)
|
|
||||||
|
|
||||||
f1 := &resources.FileRes{
|
f1 := &resources.FileRes{
|
||||||
BaseRes: resources.BaseRes{
|
BaseRes: resources.BaseRes{
|
||||||
Name: "file1",
|
Name: "file1",
|
||||||
|
Kind: "file",
|
||||||
|
MetaParams: metaparams,
|
||||||
// send->recv!
|
// send->recv!
|
||||||
Recv: map[string]*resources.Send{
|
Recv: map[string]*resources.Send{
|
||||||
"Content": {Res: p1, Key: "Password"},
|
"Content": {Res: p1, Key: "Password"},
|
||||||
@@ -94,55 +103,72 @@ func (obj *MyGAPI) Graph() (*pgraph.Graph, error) {
|
|||||||
State: "present",
|
State: "present",
|
||||||
}
|
}
|
||||||
|
|
||||||
v2 := pgraph.NewVertex(f1)
|
g.AddVertex(f1)
|
||||||
g.AddVertex(v2)
|
|
||||||
|
|
||||||
n1 := &resources.NoopRes{
|
n1 := &resources.NoopRes{
|
||||||
BaseRes: resources.BaseRes{
|
BaseRes: resources.BaseRes{
|
||||||
Name: "noop1",
|
Name: "noop1",
|
||||||
|
Kind: "noop",
|
||||||
|
MetaParams: metaparams,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
v3 := pgraph.NewVertex(n1)
|
g.AddVertex(n1)
|
||||||
g.AddVertex(v3)
|
|
||||||
|
|
||||||
e0 := pgraph.NewEdge("e0")
|
e0 := &resources.Edge{Name: "e0"}
|
||||||
e0.Notify = true // send a notification from v0 to v1
|
e0.Notify = true // send a notification from f0 to p1
|
||||||
g.AddEdge(v0, v1, e0)
|
g.AddEdge(f0, p1, e0)
|
||||||
|
|
||||||
g.AddEdge(v1, v2, pgraph.NewEdge("e1"))
|
g.AddEdge(p1, f1, &resources.Edge{Name: "e1"})
|
||||||
|
|
||||||
e2 := pgraph.NewEdge("e2")
|
e2 := &resources.Edge{Name: "e2"}
|
||||||
e2.Notify = true // send a notification from v2 to v3
|
e2.Notify = true // send a notification from f1 to n1
|
||||||
g.AddEdge(v2, v3, e2)
|
g.AddEdge(f1, n1, e2)
|
||||||
|
|
||||||
//g, err := config.NewGraphFromConfig(obj.data.Hostname, obj.data.World, obj.data.Noop)
|
//g, err := config.NewGraphFromConfig(obj.data.Hostname, obj.data.World, obj.data.Noop)
|
||||||
return g, nil
|
return g, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next returns nil errors every time there could be a new graph.
|
// Next returns nil errors every time there could be a new graph.
|
||||||
func (obj *MyGAPI) Next() chan error {
|
func (obj *MyGAPI) Next() chan gapi.Next {
|
||||||
if obj.data.NoWatch || obj.Interval <= 0 {
|
ch := make(chan gapi.Next)
|
||||||
return nil
|
|
||||||
}
|
|
||||||
ch := make(chan error)
|
|
||||||
obj.wg.Add(1)
|
obj.wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer obj.wg.Done()
|
defer obj.wg.Done()
|
||||||
defer close(ch) // this will run before the obj.wg.Done()
|
defer close(ch) // this will run before the obj.wg.Done()
|
||||||
if !obj.initialized {
|
if !obj.initialized {
|
||||||
ch <- fmt.Errorf("libmgmt: MyGAPI is not initialized")
|
next := gapi.Next{
|
||||||
|
Err: fmt.Errorf("libmgmt: MyGAPI is not initialized"),
|
||||||
|
Exit: true, // exit, b/c programming error?
|
||||||
|
}
|
||||||
|
ch <- next
|
||||||
|
}
|
||||||
|
startChan := make(chan struct{}) // start signal
|
||||||
|
close(startChan) // kick it off!
|
||||||
|
|
||||||
|
ticker := make(<-chan time.Time)
|
||||||
|
if obj.data.NoStreamWatch || obj.Interval <= 0 {
|
||||||
|
ticker = nil
|
||||||
|
} else {
|
||||||
|
// arbitrarily change graph every interval seconds
|
||||||
|
t := time.NewTicker(time.Duration(obj.Interval) * time.Second)
|
||||||
|
defer t.Stop()
|
||||||
|
ticker = t.C
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-startChan: // kick the loop once at start
|
||||||
|
startChan = nil // disable
|
||||||
|
// pass
|
||||||
|
case <-ticker:
|
||||||
|
// pass
|
||||||
|
case <-obj.closeChan:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// arbitrarily change graph every interval seconds
|
|
||||||
ticker := time.NewTicker(time.Duration(obj.Interval) * time.Second)
|
|
||||||
defer ticker.Stop()
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ticker.C:
|
|
||||||
log.Printf("libmgmt: Generating new graph...")
|
log.Printf("libmgmt: Generating new graph...")
|
||||||
ch <- nil // trigger a run
|
select {
|
||||||
|
case ch <- gapi.Next{}: // trigger a run
|
||||||
case <-obj.closeChan:
|
case <-obj.closeChan:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -201,17 +227,14 @@ func Run() error {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Println("Interrupted by signal")
|
log.Println("Interrupted by signal")
|
||||||
obj.Exit(fmt.Errorf("Killed by %v", sig))
|
obj.Exit(fmt.Errorf("killed by %v", sig))
|
||||||
return
|
return
|
||||||
case <-exit:
|
case <-exit:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if err := obj.Run(); err != nil {
|
return obj.Run()
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
13
examples/limit1.yaml
Normal file
13
examples/limit1.yaml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
file:
|
||||||
|
- name: file1
|
||||||
|
meta:
|
||||||
|
limit: 0.2
|
||||||
|
burst: 5
|
||||||
|
path: "/tmp/mgmt/limit"
|
||||||
|
content: |
|
||||||
|
i am a normal file
|
||||||
|
state: exists
|
||||||
|
edges: []
|
||||||
7
examples/noop0.yaml
Normal file
7
examples/noop0.yaml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
comment: simple noop example
|
||||||
|
resources:
|
||||||
|
noop:
|
||||||
|
- name: noop0
|
||||||
|
edges: []
|
||||||
30
examples/noop2.yaml
Normal file
30
examples/noop2.yaml
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
comment: dangerous noop example
|
||||||
|
resources:
|
||||||
|
noop:
|
||||||
|
- name: noop1
|
||||||
|
meta:
|
||||||
|
noop: true
|
||||||
|
file:
|
||||||
|
- name: file1
|
||||||
|
path: "/tmp/mgmt/hello-noop"
|
||||||
|
content: |
|
||||||
|
hello world from @purpleidea
|
||||||
|
state: exists
|
||||||
|
meta:
|
||||||
|
noop: true
|
||||||
|
exec:
|
||||||
|
- name: exec1
|
||||||
|
meta:
|
||||||
|
noop: true
|
||||||
|
cmd: 'rm -rf /'
|
||||||
|
shell: '/bin/bash'
|
||||||
|
timeout: 0
|
||||||
|
watchcmd: ''
|
||||||
|
watchshell: ''
|
||||||
|
ifcmd: ''
|
||||||
|
ifshell: ''
|
||||||
|
pollint: 0
|
||||||
|
state: present
|
||||||
|
edges: []
|
||||||
@@ -2,6 +2,6 @@
|
|||||||
graph: mygraph
|
graph: mygraph
|
||||||
resources:
|
resources:
|
||||||
nspawn:
|
nspawn:
|
||||||
- name: mgmt-nspawn1
|
- name: nspawn1
|
||||||
state: running
|
state: running
|
||||||
edges: []
|
edges: []
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
---
|
|
||||||
graph: mygraph
|
|
||||||
resources:
|
|
||||||
nspawn:
|
|
||||||
- name: mgmt-nspawn2
|
|
||||||
state: stopped
|
|
||||||
edges: []
|
|
||||||
24
examples/poll1.yaml
Normal file
24
examples/poll1.yaml
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
file:
|
||||||
|
- name: file1
|
||||||
|
meta:
|
||||||
|
poll: 5
|
||||||
|
path: "/tmp/mgmt/f1"
|
||||||
|
content: |
|
||||||
|
i poll every 5 seconds
|
||||||
|
state: exists
|
||||||
|
- name: file2
|
||||||
|
path: "/tmp/mgmt/f2"
|
||||||
|
content: |
|
||||||
|
i use the event based watcher
|
||||||
|
state: exists
|
||||||
|
- name: file3
|
||||||
|
meta:
|
||||||
|
poll: 1
|
||||||
|
path: "/tmp/mgmt/f3"
|
||||||
|
content: |
|
||||||
|
i poll every second
|
||||||
|
state: exists
|
||||||
|
edges: []
|
||||||
57
examples/retry1.yaml
Normal file
57
examples/retry1.yaml
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
comment: You can test Watch and CheckApply failures with chmod ugo-r and chmod ugo-w.
|
||||||
|
resources:
|
||||||
|
exec:
|
||||||
|
- name: exec1
|
||||||
|
cmd: 'touch /tmp/mgmt/no-read && chmod ugo-r /tmp/mgmt/no-read'
|
||||||
|
shell: '/bin/bash'
|
||||||
|
timeout: 0
|
||||||
|
watchcmd: ''
|
||||||
|
watchshell: ''
|
||||||
|
ifcmd: ''
|
||||||
|
ifshell: ''
|
||||||
|
pollint: 0
|
||||||
|
state: present
|
||||||
|
- name: exec2
|
||||||
|
cmd: 'touch /tmp/mgmt/no-write && chmod ugo-w /tmp/mgmt/no-write'
|
||||||
|
shell: '/bin/bash'
|
||||||
|
timeout: 0
|
||||||
|
watchcmd: ''
|
||||||
|
watchshell: ''
|
||||||
|
ifcmd: ''
|
||||||
|
ifshell: ''
|
||||||
|
pollint: 0
|
||||||
|
state: present
|
||||||
|
file:
|
||||||
|
- name: noread
|
||||||
|
path: "/tmp/mgmt/no-read"
|
||||||
|
meta:
|
||||||
|
retry: 3
|
||||||
|
delay: 5000
|
||||||
|
content: |
|
||||||
|
i am f1
|
||||||
|
state: exists
|
||||||
|
- name: nowrite
|
||||||
|
path: "/tmp/mgmt/no-write"
|
||||||
|
meta:
|
||||||
|
retry: 3
|
||||||
|
delay: 5000
|
||||||
|
content: |
|
||||||
|
i am f1
|
||||||
|
state: exists
|
||||||
|
edges:
|
||||||
|
- name: e1
|
||||||
|
from:
|
||||||
|
kind: exec
|
||||||
|
name: exec1
|
||||||
|
to:
|
||||||
|
kind: file
|
||||||
|
name: noread
|
||||||
|
- name: e2
|
||||||
|
from:
|
||||||
|
kind: exec
|
||||||
|
name: exec2
|
||||||
|
to:
|
||||||
|
kind: file
|
||||||
|
name: nowrite
|
||||||
8
examples/svc2.yaml
Normal file
8
examples/svc2.yaml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
svc:
|
||||||
|
- name: purpleidea
|
||||||
|
state: running
|
||||||
|
session: true
|
||||||
|
edges: []
|
||||||
@@ -4,7 +4,7 @@ comment: timer example
|
|||||||
resources:
|
resources:
|
||||||
timer:
|
timer:
|
||||||
- name: timer1
|
- name: timer1
|
||||||
interval: 30
|
interval: 3
|
||||||
exec:
|
exec:
|
||||||
- name: exec1
|
- name: exec1
|
||||||
cmd: echo hello world
|
cmd: echo hello world
|
||||||
|
|||||||
9
examples/user1.yaml
Normal file
9
examples/user1.yaml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
user:
|
||||||
|
- name: testuser
|
||||||
|
uid: 1002
|
||||||
|
gid: 100
|
||||||
|
state: exists
|
||||||
|
edges: []
|
||||||
21
examples/virt4.yaml
Normal file
21
examples/virt4.yaml
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
virt:
|
||||||
|
- name: mgmt4
|
||||||
|
meta:
|
||||||
|
limit: .inf
|
||||||
|
burst: 0
|
||||||
|
uri: 'qemu:///session'
|
||||||
|
cpus: 1
|
||||||
|
maxcpus: 4
|
||||||
|
memory: 524288
|
||||||
|
boot:
|
||||||
|
- hd
|
||||||
|
disk:
|
||||||
|
- type: qcow2
|
||||||
|
source: "~/.local/share/libvirt/images/fedora-23-scratch.qcow2"
|
||||||
|
state: running
|
||||||
|
transient: false
|
||||||
|
edges: []
|
||||||
|
comment: "qemu-img create -b fedora-23.qcow2 -f qcow2 fedora-23-scratch.qcow2"
|
||||||
37
gapi/gapi.go
37
gapi/gapi.go
@@ -1,18 +1,18 @@
|
|||||||
// Mgmt
|
// Mgmt
|
||||||
// Copyright (C) 2013-2016+ James Shubin and the project contributors
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// GNU Affero General Public License for more details.
|
// GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
// Package gapi defines the interface that graph API generators must meet.
|
// Package gapi defines the interface that graph API generators must meet.
|
||||||
@@ -23,28 +23,33 @@ import (
|
|||||||
"github.com/purpleidea/mgmt/resources"
|
"github.com/purpleidea/mgmt/resources"
|
||||||
)
|
)
|
||||||
|
|
||||||
// World is an interface to the rest of the different graph state. It allows
|
|
||||||
// the GAPI to store state and exchange information throughout the cluster. It
|
|
||||||
// is the interface each machine uses to communicate with the rest of the world.
|
|
||||||
type World interface { // TODO: is there a better name for this interface?
|
|
||||||
ResExport([]resources.Res) error
|
|
||||||
// FIXME: should this method take a "filter" data struct instead of many args?
|
|
||||||
ResCollect(hostnameFilter, kindFilter []string) ([]resources.Res, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Data is the set of input values passed into the GAPI structs via Init.
|
// Data is the set of input values passed into the GAPI structs via Init.
|
||||||
type Data struct {
|
type Data struct {
|
||||||
Hostname string // uuid for the host, required for GAPI
|
Hostname string // uuid for the host, required for GAPI
|
||||||
World World
|
World resources.World
|
||||||
Noop bool
|
Noop bool
|
||||||
NoWatch bool
|
NoConfigWatch bool
|
||||||
|
NoStreamWatch bool
|
||||||
// NOTE: we can add more fields here if needed by GAPI endpoints
|
// NOTE: we can add more fields here if needed by GAPI endpoints
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Next describes the particular response the GAPI implementer wishes to emit.
|
||||||
|
type Next struct {
|
||||||
|
// FIXME: the Fast pause parameter should eventually get replaced with a
|
||||||
|
// "SwitchMethod" parameter or similar that instead lets the implementer
|
||||||
|
// choose between fast pause, slow pause, and interrupt. Interrupt could
|
||||||
|
// be a future extension to the Resource API that lets an Interrupt() be
|
||||||
|
// called if we want to exit immediately from the CheckApply part of the
|
||||||
|
// resource for some reason. For now we'll keep this simple with a bool.
|
||||||
|
Fast bool // run a fast pause to switch?
|
||||||
|
Exit bool // should we cause the program to exit? (specify err or not)
|
||||||
|
Err error // if something goes wrong (use with or without exit!)
|
||||||
|
}
|
||||||
|
|
||||||
// GAPI is a Graph API that represents incoming graphs and change streams.
|
// GAPI is a Graph API that represents incoming graphs and change streams.
|
||||||
type GAPI interface {
|
type GAPI interface {
|
||||||
Init(Data) error // initializes the GAPI and passes in useful data
|
Init(Data) error // initializes the GAPI and passes in useful data
|
||||||
Graph() (*pgraph.Graph, error) // returns the most recent pgraph
|
Graph() (*pgraph.Graph, error) // returns the most recent pgraph
|
||||||
Next() chan error // returns a stream of switch events
|
Next() chan Next // returns a stream of switch events
|
||||||
Close() error // shutdown the GAPI
|
Close() error // shutdown the GAPI
|
||||||
}
|
}
|
||||||
|
|||||||
155
hcl/gapi.go
Normal file
155
hcl/gapi.go
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
// Mgmt
|
||||||
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
|
//
|
||||||
|
// 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package hcl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/purpleidea/mgmt/gapi"
|
||||||
|
"github.com/purpleidea/mgmt/pgraph"
|
||||||
|
"github.com/purpleidea/mgmt/recwatch"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GAPI ...
|
||||||
|
type GAPI struct {
|
||||||
|
File *string
|
||||||
|
|
||||||
|
initialized bool
|
||||||
|
data gapi.Data
|
||||||
|
wg sync.WaitGroup
|
||||||
|
closeChan chan struct{}
|
||||||
|
configWatcher *recwatch.ConfigWatcher
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGAPI ...
|
||||||
|
func NewGAPI(data gapi.Data, file *string) (*GAPI, error) {
|
||||||
|
if file == nil {
|
||||||
|
return nil, fmt.Errorf("empty file given")
|
||||||
|
}
|
||||||
|
|
||||||
|
obj := &GAPI{
|
||||||
|
File: file,
|
||||||
|
}
|
||||||
|
return obj, obj.Init(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init ...
|
||||||
|
func (obj *GAPI) Init(d gapi.Data) error {
|
||||||
|
if obj.initialized {
|
||||||
|
return fmt.Errorf("already initialized")
|
||||||
|
}
|
||||||
|
|
||||||
|
if obj.File == nil {
|
||||||
|
return fmt.Errorf("file cannot be nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
obj.data = d
|
||||||
|
obj.closeChan = make(chan struct{})
|
||||||
|
obj.initialized = true
|
||||||
|
obj.configWatcher = recwatch.NewConfigWatcher()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Graph ...
|
||||||
|
func (obj *GAPI) Graph() (*pgraph.Graph, error) {
|
||||||
|
config, err := loadHcl(obj.File)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to parse graph: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return graphFromConfig(config, obj.data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next ...
|
||||||
|
func (obj *GAPI) Next() chan gapi.Next {
|
||||||
|
ch := make(chan gapi.Next)
|
||||||
|
obj.wg.Add(1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer obj.wg.Done()
|
||||||
|
defer close(ch)
|
||||||
|
if !obj.initialized {
|
||||||
|
next := gapi.Next{
|
||||||
|
Err: fmt.Errorf("hcl: GAPI is not initialized"),
|
||||||
|
Exit: true,
|
||||||
|
}
|
||||||
|
ch <- next
|
||||||
|
return
|
||||||
|
}
|
||||||
|
startChan := make(chan struct{}) // start signal
|
||||||
|
close(startChan) // kick it off!
|
||||||
|
|
||||||
|
watchChan, configChan := make(chan error), make(chan error)
|
||||||
|
if obj.data.NoConfigWatch {
|
||||||
|
configChan = nil
|
||||||
|
} else {
|
||||||
|
configChan = obj.configWatcher.ConfigWatch(*obj.File) // simple
|
||||||
|
}
|
||||||
|
if obj.data.NoStreamWatch {
|
||||||
|
watchChan = nil
|
||||||
|
} else {
|
||||||
|
watchChan = obj.data.World.ResWatch()
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
var err error
|
||||||
|
var ok bool
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-startChan:
|
||||||
|
startChan = nil
|
||||||
|
case err, ok = <-watchChan:
|
||||||
|
case err, ok = <-configChan:
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case <-obj.closeChan:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("hcl: generating new graph")
|
||||||
|
next := gapi.Next{
|
||||||
|
Err: err,
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case ch <- next:
|
||||||
|
case <-obj.closeChan:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return ch
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close ...
|
||||||
|
func (obj *GAPI) Close() error {
|
||||||
|
if !obj.initialized {
|
||||||
|
return fmt.Errorf("hcl: GAPI is not initialized")
|
||||||
|
}
|
||||||
|
|
||||||
|
obj.configWatcher.Close()
|
||||||
|
close(obj.closeChan)
|
||||||
|
obj.wg.Wait()
|
||||||
|
obj.initialized = false
|
||||||
|
return nil
|
||||||
|
}
|
||||||
387
hcl/parse.go
Normal file
387
hcl/parse.go
Normal file
@@ -0,0 +1,387 @@
|
|||||||
|
// Mgmt
|
||||||
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
|
//
|
||||||
|
// 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package hcl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/hashicorp/hcl"
|
||||||
|
"github.com/hashicorp/hcl/hcl/ast"
|
||||||
|
"github.com/hashicorp/hil"
|
||||||
|
"github.com/purpleidea/mgmt/gapi"
|
||||||
|
hv "github.com/purpleidea/mgmt/hil"
|
||||||
|
"github.com/purpleidea/mgmt/pgraph"
|
||||||
|
"github.com/purpleidea/mgmt/resources"
|
||||||
|
)
|
||||||
|
|
||||||
|
type collectorResConfig struct {
|
||||||
|
Kind string
|
||||||
|
Pattern string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Config defines the structure of the hcl config.
|
||||||
|
type Config struct {
|
||||||
|
Resources []*Resource
|
||||||
|
Edges []*Edge
|
||||||
|
Collector []collectorResConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// vertex is the data structure of a vertex.
|
||||||
|
type vertex struct {
|
||||||
|
Kind string `hcl:"kind"`
|
||||||
|
Name string `hcl:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Edge defines an edge in hcl.
|
||||||
|
type Edge struct {
|
||||||
|
Name string
|
||||||
|
From vertex
|
||||||
|
To vertex
|
||||||
|
Notify bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resources define the state for resources.
|
||||||
|
type Resources struct {
|
||||||
|
Resources []resources.Res
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resource ...
|
||||||
|
type Resource struct {
|
||||||
|
Name string
|
||||||
|
Kind string
|
||||||
|
resource resources.Res
|
||||||
|
Meta resources.MetaParams
|
||||||
|
deps []*Edge
|
||||||
|
rcv map[string]*hv.ResourceVariable
|
||||||
|
}
|
||||||
|
|
||||||
|
type key struct {
|
||||||
|
kind, name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func graphFromConfig(c *Config, data gapi.Data) (*pgraph.Graph, error) {
|
||||||
|
var graph *pgraph.Graph
|
||||||
|
var err error
|
||||||
|
|
||||||
|
graph, err = pgraph.NewGraph("Graph")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to create graph from config: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
lookup := make(map[key]pgraph.Vertex)
|
||||||
|
|
||||||
|
var keep []pgraph.Vertex
|
||||||
|
var resourceList []resources.Res
|
||||||
|
|
||||||
|
log.Printf("hcl: parsing %d resources", len(c.Resources))
|
||||||
|
for _, r := range c.Resources {
|
||||||
|
res := r.resource
|
||||||
|
kind := r.resource.GetKind()
|
||||||
|
|
||||||
|
log.Printf("hcl: resource \"%s\" \"%s\"", kind, r.Name)
|
||||||
|
if !strings.HasPrefix(res.GetName(), "@@") {
|
||||||
|
fn := func(v pgraph.Vertex) (bool, error) {
|
||||||
|
return resources.VtoR(v).Compare(res), nil
|
||||||
|
}
|
||||||
|
v, err := graph.VertexMatchFn(fn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("could not match vertex: %s", err)
|
||||||
|
}
|
||||||
|
if v == nil {
|
||||||
|
v = res
|
||||||
|
graph.AddVertex(v)
|
||||||
|
}
|
||||||
|
lookup[key{kind, res.GetName()}] = v
|
||||||
|
keep = append(keep, v)
|
||||||
|
} else if !data.Noop {
|
||||||
|
res.SetName(res.GetName()[2:])
|
||||||
|
res.SetKind(kind)
|
||||||
|
resourceList = append(resourceList, res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// store in backend (usually etcd)
|
||||||
|
if err := data.World.ResExport(resourceList); err != nil {
|
||||||
|
return nil, fmt.Errorf("Config: Could not export resources: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// lookup from backend (usually etcd)
|
||||||
|
var hostnameFilter []string // empty to get from everyone
|
||||||
|
kindFilter := []string{}
|
||||||
|
for _, t := range c.Collector {
|
||||||
|
kind := strings.ToLower(t.Kind)
|
||||||
|
kindFilter = append(kindFilter, kind)
|
||||||
|
}
|
||||||
|
// do all the graph look ups in one single step, so that if the backend
|
||||||
|
// database changes, we don't have a partial state of affairs...
|
||||||
|
if len(kindFilter) > 0 { // if kindFilter is empty, don't need to do lookups!
|
||||||
|
var err error
|
||||||
|
resourceList, err = data.World.ResCollect(hostnameFilter, kindFilter)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Config: Could not collect resources: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, res := range resourceList {
|
||||||
|
matched := false
|
||||||
|
// see if we find a collect pattern that matches
|
||||||
|
for _, t := range c.Collector {
|
||||||
|
kind := strings.ToLower(t.Kind)
|
||||||
|
// use t.Kind and optionally t.Pattern to collect from storage
|
||||||
|
log.Printf("Collect: %v; Pattern: %v", kind, t.Pattern)
|
||||||
|
|
||||||
|
// XXX: expand to more complex pattern matching here...
|
||||||
|
if res.GetKind() != kind {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if matched {
|
||||||
|
// we've already matched this resource, should we match again?
|
||||||
|
log.Printf("Config: Warning: Matching %s again!", res)
|
||||||
|
}
|
||||||
|
matched = true
|
||||||
|
|
||||||
|
// collect resources but add the noop metaparam
|
||||||
|
//if noop { // now done in mgmtmain
|
||||||
|
// res.Meta().Noop = noop
|
||||||
|
//}
|
||||||
|
|
||||||
|
if t.Pattern != "" { // XXX: simplistic for now
|
||||||
|
res.CollectPattern(t.Pattern) // res.Dirname = t.Pattern
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Collect: %s: collected!", res)
|
||||||
|
|
||||||
|
// XXX: similar to other resource add code:
|
||||||
|
// if _, exists := lookup[kind]; !exists {
|
||||||
|
// lookup[kind] = make(map[string]pgraph.Vertex)
|
||||||
|
// }
|
||||||
|
|
||||||
|
fn := func(v pgraph.Vertex) (bool, error) {
|
||||||
|
return resources.VtoR(v).Compare(res), nil
|
||||||
|
}
|
||||||
|
v, err := graph.VertexMatchFn(fn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("could not VertexMatchFn() resource: %s", err)
|
||||||
|
}
|
||||||
|
if v == nil { // no match found
|
||||||
|
v = res // a standalone res can be a vertex
|
||||||
|
graph.AddVertex(v) // call standalone in case not part of an edge
|
||||||
|
}
|
||||||
|
lookup[key{kind, res.GetName()}] = v // used for constructing edges
|
||||||
|
keep = append(keep, v) // append
|
||||||
|
|
||||||
|
//break // let's see if another resource even matches
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, r := range c.Resources {
|
||||||
|
for _, e := range r.deps {
|
||||||
|
if _, ok := lookup[key{strings.ToLower(e.From.Kind), e.From.Name}]; !ok {
|
||||||
|
return nil, fmt.Errorf("can't find 'from' name")
|
||||||
|
}
|
||||||
|
if _, ok := lookup[key{strings.ToLower(e.To.Kind), e.To.Name}]; !ok {
|
||||||
|
return nil, fmt.Errorf("can't find 'to' name")
|
||||||
|
}
|
||||||
|
from := lookup[key{strings.ToLower(e.From.Kind), e.From.Name}]
|
||||||
|
to := lookup[key{strings.ToLower(e.To.Kind), e.To.Name}]
|
||||||
|
edge := &resources.Edge{
|
||||||
|
Name: e.Name,
|
||||||
|
Notify: e.Notify,
|
||||||
|
}
|
||||||
|
graph.AddEdge(from, to, edge)
|
||||||
|
}
|
||||||
|
|
||||||
|
recv := make(map[string]*resources.Send)
|
||||||
|
// build Rcv's from resource variables
|
||||||
|
for k, v := range r.rcv {
|
||||||
|
send, ok := lookup[key{strings.ToLower(v.Kind), v.Name}]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("resource not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
recv[strings.ToUpper(string(k[0]))+k[1:]] = &resources.Send{
|
||||||
|
Res: resources.VtoR(send),
|
||||||
|
Key: v.Field,
|
||||||
|
}
|
||||||
|
|
||||||
|
to := lookup[key{strings.ToLower(r.Kind), r.Name}]
|
||||||
|
edge := &resources.Edge{
|
||||||
|
Name: v.Name,
|
||||||
|
Notify: true,
|
||||||
|
}
|
||||||
|
graph.AddEdge(send, to, edge)
|
||||||
|
}
|
||||||
|
|
||||||
|
r.resource.SetRecv(recv)
|
||||||
|
}
|
||||||
|
|
||||||
|
return graph, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadHcl(f *string) (*Config, error) {
|
||||||
|
if f == nil {
|
||||||
|
return nil, fmt.Errorf("empty file given")
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := ioutil.ReadFile(*f)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to read file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := hcl.ParseBytes(data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to parse file: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
config := new(Config)
|
||||||
|
|
||||||
|
list, ok := file.Node.(*ast.ObjectList)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("unable to parse file: file does not contain root node object")
|
||||||
|
}
|
||||||
|
|
||||||
|
if resources := list.Filter("resource"); len(resources.Items) > 0 {
|
||||||
|
var err error
|
||||||
|
config.Resources, err = loadResourcesHcl(resources)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to parse: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return config, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadResourcesHcl(list *ast.ObjectList) ([]*Resource, error) {
|
||||||
|
list = list.Children()
|
||||||
|
if len(list.Items) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var result []*Resource
|
||||||
|
|
||||||
|
for _, item := range list.Items {
|
||||||
|
kind := item.Keys[0].Token.Value().(string)
|
||||||
|
name := item.Keys[1].Token.Value().(string)
|
||||||
|
|
||||||
|
var listVal *ast.ObjectList
|
||||||
|
if ot, ok := item.Val.(*ast.ObjectType); ok {
|
||||||
|
listVal = ot.List
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("module '%s': should be an object", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
var params = resources.DefaultMetaParams
|
||||||
|
if o := listVal.Filter("meta"); len(o.Items) > 0 {
|
||||||
|
err := hcl.DecodeObject(¶ms, o)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"Error parsing meta for %s: %s",
|
||||||
|
name,
|
||||||
|
err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var deps []string
|
||||||
|
if edges := listVal.Filter("depends_on"); len(edges.Items) > 0 {
|
||||||
|
err := hcl.DecodeObject(&deps, edges.Items[0].Val)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to parse: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var edges []*Edge
|
||||||
|
for _, dep := range deps {
|
||||||
|
vertices := strings.Split(dep, ".")
|
||||||
|
edges = append(edges, &Edge{
|
||||||
|
To: vertex{
|
||||||
|
Kind: kind,
|
||||||
|
Name: name,
|
||||||
|
},
|
||||||
|
From: vertex{
|
||||||
|
Kind: vertices[0],
|
||||||
|
Name: vertices[1],
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var config map[string]interface{}
|
||||||
|
if err := hcl.DecodeObject(&config, item.Val); err != nil {
|
||||||
|
log.Printf("hcl: unable to decode body: %v", err)
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"Error reading config for %s: %s",
|
||||||
|
name,
|
||||||
|
err)
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(config, "meta")
|
||||||
|
delete(config, "depends_on")
|
||||||
|
|
||||||
|
rcv := make(map[string]*hv.ResourceVariable)
|
||||||
|
// parse strings for hil
|
||||||
|
for k, v := range config {
|
||||||
|
n, err := hil.Parse(v.(string))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to parse fields: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
variables, err := hv.ParseVariables(n)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to parse variables: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range variables {
|
||||||
|
val, ok := v.(*hv.ResourceVariable)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
rcv[k] = val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := resources.NewNamedResource(kind, name)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("hcl: unable to parse resource: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := hcl.DecodeObject(res, item.Val); err != nil {
|
||||||
|
log.Printf("hcl: unable to decode body: %v", err)
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"Error reading config for %s: %s",
|
||||||
|
name,
|
||||||
|
err)
|
||||||
|
}
|
||||||
|
|
||||||
|
meta := res.Meta()
|
||||||
|
*meta = params
|
||||||
|
|
||||||
|
result = append(result, &Resource{
|
||||||
|
Name: name,
|
||||||
|
Kind: kind,
|
||||||
|
resource: res,
|
||||||
|
deps: edges,
|
||||||
|
rcv: rcv,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
89
hil/interpolate.go
Normal file
89
hil/interpolate.go
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
// Mgmt
|
||||||
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
|
//
|
||||||
|
// 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package hil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/hashicorp/hil/ast"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Variable defines an interpolated variable.
|
||||||
|
type Variable interface {
|
||||||
|
Key() string
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResourceVariable defines a variable type used to reference fields of a resource
|
||||||
|
// e.g. ${file.file1.Content}
|
||||||
|
type ResourceVariable struct {
|
||||||
|
Kind, Name, Field string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Key returns a string representation of the variable key.
|
||||||
|
func (r *ResourceVariable) Key() string {
|
||||||
|
return fmt.Sprintf("%s.%s.%s", r.Kind, r.Name, r.Field)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewInterpolatedVariable takes a variable key and return the interpolated variable
|
||||||
|
// of the required type.
|
||||||
|
func NewInterpolatedVariable(k string) (Variable, error) {
|
||||||
|
// for now resource variables are the only thing.
|
||||||
|
parts := strings.SplitN(k, ".", 3)
|
||||||
|
|
||||||
|
return &ResourceVariable{
|
||||||
|
Kind: parts[0],
|
||||||
|
Name: parts[1],
|
||||||
|
Field: parts[2],
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseVariables will traverse a HIL tree looking for variables and returns a
|
||||||
|
// list of them.
|
||||||
|
func ParseVariables(tree ast.Node) ([]Variable, error) {
|
||||||
|
var result []Variable
|
||||||
|
var finalErr error
|
||||||
|
|
||||||
|
visitor := func(n ast.Node) ast.Node {
|
||||||
|
if finalErr != nil {
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
switch nt := n.(type) {
|
||||||
|
case *ast.VariableAccess:
|
||||||
|
v, err := NewInterpolatedVariable(nt.Name)
|
||||||
|
if err != nil {
|
||||||
|
finalErr = err
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
result = append(result, v)
|
||||||
|
default:
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
tree.Accept(visitor)
|
||||||
|
|
||||||
|
if finalErr != nil {
|
||||||
|
return nil, finalErr
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
80
lib/cli.go
80
lib/cli.go
@@ -1,18 +1,18 @@
|
|||||||
// Mgmt
|
// Mgmt
|
||||||
// Copyright (C) 2013-2016+ James Shubin and the project contributors
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// GNU Affero General Public License for more details.
|
// GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package lib
|
package lib
|
||||||
@@ -24,8 +24,10 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/purpleidea/mgmt/hcl"
|
||||||
"github.com/purpleidea/mgmt/puppet"
|
"github.com/purpleidea/mgmt/puppet"
|
||||||
"github.com/purpleidea/mgmt/yamlgraph"
|
"github.com/purpleidea/mgmt/yamlgraph"
|
||||||
|
"github.com/purpleidea/mgmt/yamlgraph2"
|
||||||
|
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
@@ -55,35 +57,55 @@ func run(c *cli.Context) error {
|
|||||||
|
|
||||||
if _ = c.String("code"); c.IsSet("code") {
|
if _ = c.String("code"); c.IsSet("code") {
|
||||||
if obj.GAPI != nil {
|
if obj.GAPI != nil {
|
||||||
return fmt.Errorf("Can't combine code GAPI with existing GAPI.")
|
return fmt.Errorf("can't combine code GAPI with existing GAPI")
|
||||||
}
|
}
|
||||||
// TODO: implement DSL GAPI
|
// TODO: implement DSL GAPI
|
||||||
//obj.GAPI = &dsl.GAPI{
|
//obj.GAPI = &dsl.GAPI{
|
||||||
// Code: &s,
|
// Code: &s,
|
||||||
//}
|
//}
|
||||||
return fmt.Errorf("The Code GAPI is not implemented yet!") // TODO: DSL
|
return fmt.Errorf("the Code GAPI is not implemented yet") // TODO: DSL
|
||||||
}
|
}
|
||||||
if y := c.String("yaml"); c.IsSet("yaml") {
|
if y := c.String("yaml"); c.IsSet("yaml") {
|
||||||
if obj.GAPI != nil {
|
if obj.GAPI != nil {
|
||||||
return fmt.Errorf("Can't combine YAML GAPI with existing GAPI.")
|
return fmt.Errorf("can't combine YAML GAPI with existing GAPI")
|
||||||
}
|
}
|
||||||
obj.GAPI = &yamlgraph.GAPI{
|
obj.GAPI = &yamlgraph.GAPI{
|
||||||
File: &y,
|
File: &y,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if y := c.String("yaml2"); c.IsSet("yaml2") {
|
||||||
|
if obj.GAPI != nil {
|
||||||
|
return fmt.Errorf("can't combine YAMLv2 GAPI with existing GAPI")
|
||||||
|
}
|
||||||
|
obj.GAPI = &yamlgraph2.GAPI{
|
||||||
|
File: &y,
|
||||||
|
}
|
||||||
|
}
|
||||||
if p := c.String("puppet"); c.IsSet("puppet") {
|
if p := c.String("puppet"); c.IsSet("puppet") {
|
||||||
if obj.GAPI != nil {
|
if obj.GAPI != nil {
|
||||||
return fmt.Errorf("Can't combine puppet GAPI with existing GAPI.")
|
return fmt.Errorf("can't combine puppet GAPI with existing GAPI")
|
||||||
}
|
}
|
||||||
obj.GAPI = &puppet.GAPI{
|
obj.GAPI = &puppet.GAPI{
|
||||||
PuppetParam: &p,
|
PuppetParam: &p,
|
||||||
PuppetConf: c.String("puppet-conf"),
|
PuppetConf: c.String("puppet-conf"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if h := c.String("hcl"); c.IsSet("hcl") {
|
||||||
|
if obj.GAPI != nil {
|
||||||
|
return fmt.Errorf("can't combine hcl GAPI with existing GAPI")
|
||||||
|
}
|
||||||
|
obj.GAPI = &hcl.GAPI{
|
||||||
|
File: &h,
|
||||||
|
}
|
||||||
|
}
|
||||||
obj.Remotes = c.StringSlice("remote") // FIXME: GAPI-ify somehow?
|
obj.Remotes = c.StringSlice("remote") // FIXME: GAPI-ify somehow?
|
||||||
|
|
||||||
obj.NoWatch = c.Bool("no-watch")
|
obj.NoWatch = c.Bool("no-watch")
|
||||||
|
obj.NoConfigWatch = c.Bool("no-config-watch")
|
||||||
|
obj.NoStreamWatch = c.Bool("no-stream-watch")
|
||||||
|
|
||||||
obj.Noop = c.Bool("noop")
|
obj.Noop = c.Bool("noop")
|
||||||
|
obj.Sema = c.Int("sema")
|
||||||
obj.Graphviz = c.String("graphviz")
|
obj.Graphviz = c.String("graphviz")
|
||||||
obj.GraphvizFilter = c.String("graphviz-filter")
|
obj.GraphvizFilter = c.String("graphviz-filter")
|
||||||
obj.ConvergedTimeout = c.Int("converged-timeout")
|
obj.ConvergedTimeout = c.Int("converged-timeout")
|
||||||
@@ -115,6 +137,9 @@ func run(c *cli.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
obj.Prometheus = c.Bool("prometheus")
|
||||||
|
obj.PrometheusListen = c.String("prometheus-listen")
|
||||||
|
|
||||||
// install the exit signal handler
|
// install the exit signal handler
|
||||||
exit := make(chan struct{})
|
exit := make(chan struct{})
|
||||||
defer close(exit)
|
defer close(exit)
|
||||||
@@ -132,7 +157,7 @@ func run(c *cli.Context) error {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Println("Interrupted by signal")
|
log.Println("Interrupted by signal")
|
||||||
obj.Exit(fmt.Errorf("Killed by %v", sig))
|
obj.Exit(fmt.Errorf("killed by %v", sig))
|
||||||
return
|
return
|
||||||
case <-exit:
|
case <-exit:
|
||||||
return
|
return
|
||||||
@@ -152,7 +177,7 @@ func CLI(program, version string, flags Flags) error {
|
|||||||
|
|
||||||
// test for sanity
|
// test for sanity
|
||||||
if program == "" || version == "" {
|
if program == "" || version == "" {
|
||||||
return fmt.Errorf("Program was not compiled correctly. Please see Makefile.")
|
return fmt.Errorf("program was not compiled correctly, see Makefile")
|
||||||
}
|
}
|
||||||
app := cli.NewApp()
|
app := cli.NewApp()
|
||||||
app.Name = program // App.name and App.version pass these values through
|
app.Name = program // App.name and App.version pass these values through
|
||||||
@@ -201,6 +226,16 @@ func CLI(program, version string, flags Flags) error {
|
|||||||
Value: "",
|
Value: "",
|
||||||
Usage: "yaml graph definition to run",
|
Usage: "yaml graph definition to run",
|
||||||
},
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "yaml2",
|
||||||
|
Value: "",
|
||||||
|
Usage: "yaml graph definition to run (parser v2)",
|
||||||
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "hcl",
|
||||||
|
Value: "",
|
||||||
|
Usage: "hcl graph definition to run",
|
||||||
|
},
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "puppet, p",
|
Name: "puppet, p",
|
||||||
Value: "",
|
Value: "",
|
||||||
@@ -219,12 +254,26 @@ func CLI(program, version string, flags Flags) error {
|
|||||||
|
|
||||||
cli.BoolFlag{
|
cli.BoolFlag{
|
||||||
Name: "no-watch",
|
Name: "no-watch",
|
||||||
|
Usage: "do not update graph under any switch events",
|
||||||
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "no-config-watch",
|
||||||
|
Usage: "do not update graph on config switch events",
|
||||||
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "no-stream-watch",
|
||||||
Usage: "do not update graph on stream switch events",
|
Usage: "do not update graph on stream switch events",
|
||||||
},
|
},
|
||||||
|
|
||||||
cli.BoolFlag{
|
cli.BoolFlag{
|
||||||
Name: "noop",
|
Name: "noop",
|
||||||
Usage: "globally force all resources into no-op mode",
|
Usage: "globally force all resources into no-op mode",
|
||||||
},
|
},
|
||||||
|
cli.IntFlag{
|
||||||
|
Name: "sema",
|
||||||
|
Value: -1,
|
||||||
|
Usage: "globally add a semaphore to all resources with this lock count",
|
||||||
|
},
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "graphviz, g",
|
Name: "graphviz, g",
|
||||||
Value: "",
|
Value: "",
|
||||||
@@ -232,7 +281,7 @@ func CLI(program, version string, flags Flags) error {
|
|||||||
},
|
},
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "graphviz-filter, gf",
|
Name: "graphviz-filter, gf",
|
||||||
Value: "dot", // directed graph default
|
Value: "",
|
||||||
Usage: "graphviz filter to use",
|
Usage: "graphviz filter to use",
|
||||||
},
|
},
|
||||||
cli.IntFlag{
|
cli.IntFlag{
|
||||||
@@ -320,6 +369,15 @@ func CLI(program, version string, flags Flags) error {
|
|||||||
Value: "",
|
Value: "",
|
||||||
Usage: "default identity used for generation",
|
Usage: "default identity used for generation",
|
||||||
},
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "prometheus",
|
||||||
|
Usage: "start a prometheus instance",
|
||||||
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "prometheus-listen",
|
||||||
|
Value: "",
|
||||||
|
Usage: "specify prometheus instance binding",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
336
lib/main.go
336
lib/main.go
@@ -1,18 +1,18 @@
|
|||||||
// Mgmt
|
// Mgmt
|
||||||
// Copyright (C) 2013-2016+ James Shubin and the project contributors
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// GNU Affero General Public License for more details.
|
// GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package lib
|
package lib
|
||||||
@@ -30,6 +30,7 @@ import (
|
|||||||
"github.com/purpleidea/mgmt/gapi"
|
"github.com/purpleidea/mgmt/gapi"
|
||||||
"github.com/purpleidea/mgmt/pgp"
|
"github.com/purpleidea/mgmt/pgp"
|
||||||
"github.com/purpleidea/mgmt/pgraph"
|
"github.com/purpleidea/mgmt/pgraph"
|
||||||
|
"github.com/purpleidea/mgmt/prometheus"
|
||||||
"github.com/purpleidea/mgmt/recwatch"
|
"github.com/purpleidea/mgmt/recwatch"
|
||||||
"github.com/purpleidea/mgmt/remote"
|
"github.com/purpleidea/mgmt/remote"
|
||||||
"github.com/purpleidea/mgmt/resources"
|
"github.com/purpleidea/mgmt/resources"
|
||||||
@@ -64,8 +65,12 @@ type Main struct {
|
|||||||
GAPI gapi.GAPI // graph API interface struct
|
GAPI gapi.GAPI // graph API interface struct
|
||||||
Remotes []string // list of remote graph definitions to run
|
Remotes []string // list of remote graph definitions to run
|
||||||
|
|
||||||
NoWatch bool // do not update graph on watched graph definition file changes
|
NoWatch bool // do not change graph under any circumstances
|
||||||
|
NoConfigWatch bool // do not update graph due to config changes
|
||||||
|
NoStreamWatch bool // do not update graph due to stream changes
|
||||||
|
|
||||||
Noop bool // globally force all resources into no-op mode
|
Noop bool // globally force all resources into no-op mode
|
||||||
|
Sema int // add a semaphore with this lock count to each resource
|
||||||
Graphviz string // output file for graphviz data
|
Graphviz string // output file for graphviz data
|
||||||
GraphvizFilter string // graphviz filter to use
|
GraphvizFilter string // graphviz filter to use
|
||||||
ConvergedTimeout int // exit after approximately this many seconds in a converged state; -1 to disable
|
ConvergedTimeout int // exit after approximately this many seconds in a converged state; -1 to disable
|
||||||
@@ -93,6 +98,9 @@ type Main struct {
|
|||||||
PgpIdentity *string
|
PgpIdentity *string
|
||||||
pgpKeys *pgp.PGP // agent key pair
|
pgpKeys *pgp.PGP // agent key pair
|
||||||
|
|
||||||
|
Prometheus bool // enable prometheus metrics
|
||||||
|
PrometheusListen string // prometheus instance bind specification
|
||||||
|
|
||||||
exit chan error // exit signal
|
exit chan error // exit signal
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,11 +108,20 @@ type Main struct {
|
|||||||
func (obj *Main) Init() error {
|
func (obj *Main) Init() error {
|
||||||
|
|
||||||
if obj.Program == "" || obj.Version == "" {
|
if obj.Program == "" || obj.Version == "" {
|
||||||
return fmt.Errorf("You must set the Program and Version strings!")
|
return fmt.Errorf("you must set the Program and Version strings")
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj.Prefix != nil && obj.TmpPrefix {
|
if obj.Prefix != nil && obj.TmpPrefix {
|
||||||
return fmt.Errorf("Choosing a prefix and the request for a tmp prefix is illogical!")
|
return fmt.Errorf("choosing a prefix and the request for a tmp prefix is illogical")
|
||||||
|
}
|
||||||
|
|
||||||
|
// if we've turned off watching, then be explicit and disable them all!
|
||||||
|
// if all the watches are disabled, then it's equivalent to no watching
|
||||||
|
if obj.NoWatch {
|
||||||
|
obj.NoConfigWatch = true
|
||||||
|
obj.NoStreamWatch = true
|
||||||
|
} else if obj.NoConfigWatch && obj.NoStreamWatch {
|
||||||
|
obj.NoWatch = true
|
||||||
}
|
}
|
||||||
|
|
||||||
obj.idealClusterSize = uint16(obj.IdealClusterSize)
|
obj.idealClusterSize = uint16(obj.IdealClusterSize)
|
||||||
@@ -113,7 +130,7 @@ func (obj *Main) Init() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if obj.idealClusterSize < 1 {
|
if obj.idealClusterSize < 1 {
|
||||||
return fmt.Errorf("IdealClusterSize should be at least one!")
|
return fmt.Errorf("the IdealClusterSize should be at least one")
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj.NoServer && len(obj.Remotes) > 0 {
|
if obj.NoServer && len(obj.Remotes) > 0 {
|
||||||
@@ -121,19 +138,19 @@ func (obj *Main) Init() error {
|
|||||||
// here, so if we're okay with every remote graph running in an
|
// here, so if we're okay with every remote graph running in an
|
||||||
// isolated mode, then this is okay. Improve on this if there's
|
// isolated mode, then this is okay. Improve on this if there's
|
||||||
// someone who really wants to be able to do this.
|
// someone who really wants to be able to do this.
|
||||||
return fmt.Errorf("The Server is required when using Remotes!")
|
return fmt.Errorf("the Server is required when using Remotes")
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj.CConns < 0 {
|
if obj.CConns < 0 {
|
||||||
return fmt.Errorf("The CConns value should be at least zero!")
|
return fmt.Errorf("the CConns value should be at least zero")
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj.ConvergedTimeout >= 0 && obj.CConns > 0 && len(obj.Remotes) > int(obj.CConns) {
|
if obj.ConvergedTimeout >= 0 && obj.CConns > 0 && len(obj.Remotes) > int(obj.CConns) {
|
||||||
return fmt.Errorf("You can't converge if you have more remotes than available connections!")
|
return fmt.Errorf("you can't converge if you have more remotes than available connections")
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj.Depth < 0 { // user should not be using this argument manually
|
if obj.Depth < 0 { // user should not be using this argument manually
|
||||||
return fmt.Errorf("Negative values for Depth are not permitted!")
|
return fmt.Errorf("negative values for Depth are not permitted")
|
||||||
}
|
}
|
||||||
|
|
||||||
// transform the url list inputs into etcd typed lists
|
// transform the url list inputs into etcd typed lists
|
||||||
@@ -142,19 +159,19 @@ func (obj *Main) Init() error {
|
|||||||
util.FlattenListWithSplit(obj.Seeds, []string{",", ";", " "}),
|
util.FlattenListWithSplit(obj.Seeds, []string{",", ";", " "}),
|
||||||
)
|
)
|
||||||
if err != nil && len(obj.Seeds) > 0 {
|
if err != nil && len(obj.Seeds) > 0 {
|
||||||
return fmt.Errorf("Seeds didn't parse correctly!")
|
return fmt.Errorf("the Seeds didn't parse correctly")
|
||||||
}
|
}
|
||||||
obj.clientURLs, err = etcdtypes.NewURLs(
|
obj.clientURLs, err = etcdtypes.NewURLs(
|
||||||
util.FlattenListWithSplit(obj.ClientURLs, []string{",", ";", " "}),
|
util.FlattenListWithSplit(obj.ClientURLs, []string{",", ";", " "}),
|
||||||
)
|
)
|
||||||
if err != nil && len(obj.ClientURLs) > 0 {
|
if err != nil && len(obj.ClientURLs) > 0 {
|
||||||
return fmt.Errorf("ClientURLs didn't parse correctly!")
|
return fmt.Errorf("the ClientURLs didn't parse correctly")
|
||||||
}
|
}
|
||||||
obj.serverURLs, err = etcdtypes.NewURLs(
|
obj.serverURLs, err = etcdtypes.NewURLs(
|
||||||
util.FlattenListWithSplit(obj.ServerURLs, []string{",", ";", " "}),
|
util.FlattenListWithSplit(obj.ServerURLs, []string{",", ";", " "}),
|
||||||
)
|
)
|
||||||
if err != nil && len(obj.ServerURLs) > 0 {
|
if err != nil && len(obj.ServerURLs) > 0 {
|
||||||
return fmt.Errorf("ServerURLs didn't parse correctly!")
|
return fmt.Errorf("the ServerURLs didn't parse correctly")
|
||||||
}
|
}
|
||||||
|
|
||||||
obj.exit = make(chan error)
|
obj.exit = make(chan error)
|
||||||
@@ -194,10 +211,10 @@ func (obj *Main) Run() error {
|
|||||||
if h := obj.Hostname; h != nil && *h != "" { // override by cli
|
if h := obj.Hostname; h != nil && *h != "" { // override by cli
|
||||||
hostname = *h
|
hostname = *h
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return errwrap.Wrapf(err, "Can't get default hostname!")
|
return errwrap.Wrapf(err, "can't get default hostname")
|
||||||
}
|
}
|
||||||
if hostname == "" { // safety check
|
if hostname == "" { // safety check
|
||||||
return fmt.Errorf("Hostname cannot be empty!")
|
return fmt.Errorf("hostname cannot be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
var prefix = fmt.Sprintf("/var/lib/%s/", obj.Program) // default prefix
|
var prefix = fmt.Sprintf("/var/lib/%s/", obj.Program) // default prefix
|
||||||
@@ -209,24 +226,39 @@ func (obj *Main) Run() error {
|
|||||||
if obj.TmpPrefix || obj.AllowTmpPrefix {
|
if obj.TmpPrefix || obj.AllowTmpPrefix {
|
||||||
var err error
|
var err error
|
||||||
if prefix, err = ioutil.TempDir("", obj.Program+"-"+hostname+"-"); err != nil {
|
if prefix, err = ioutil.TempDir("", obj.Program+"-"+hostname+"-"); err != nil {
|
||||||
return fmt.Errorf("Main: Error: Can't create temporary prefix!")
|
return fmt.Errorf("can't create temporary prefix")
|
||||||
}
|
}
|
||||||
log.Println("Main: Warning: Working prefix directory is temporary!")
|
log.Println("Main: Warning: Working prefix directory is temporary!")
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("Main: Error: Can't create prefix!")
|
return fmt.Errorf("can't create prefix")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Printf("Main: Working prefix is: %s", prefix)
|
log.Printf("Main: Working prefix is: %s", prefix)
|
||||||
pgraphPrefix := fmt.Sprintf("%s/", path.Join(prefix, "pgraph")) // pgraph namespace
|
pgraphPrefix := fmt.Sprintf("%s/", path.Join(prefix, "pgraph")) // pgraph namespace
|
||||||
if err := os.MkdirAll(pgraphPrefix, 0770); err != nil {
|
if err := os.MkdirAll(pgraphPrefix, 0770); err != nil {
|
||||||
return errwrap.Wrapf(err, "Can't create pgraph prefix")
|
return errwrap.Wrapf(err, "can't create pgraph prefix")
|
||||||
|
}
|
||||||
|
|
||||||
|
var prom *prometheus.Prometheus
|
||||||
|
if obj.Prometheus {
|
||||||
|
prom = &prometheus.Prometheus{
|
||||||
|
Listen: obj.PrometheusListen,
|
||||||
|
}
|
||||||
|
if err := prom.Init(); err != nil {
|
||||||
|
return errwrap.Wrapf(err, "can't create initiate Prometheus instance")
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Main: Prometheus: Starting instance on %s", prom.Listen)
|
||||||
|
if err := prom.Start(); err != nil {
|
||||||
|
return errwrap.Wrapf(err, "can't start initiate Prometheus instance")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !obj.NoPgp {
|
if !obj.NoPgp {
|
||||||
pgpPrefix := fmt.Sprintf("%s/", path.Join(prefix, "pgp"))
|
pgpPrefix := fmt.Sprintf("%s/", path.Join(prefix, "pgp"))
|
||||||
if err := os.MkdirAll(pgpPrefix, 0770); err != nil {
|
if err := os.MkdirAll(pgpPrefix, 0770); err != nil {
|
||||||
return errwrap.Wrapf(err, "Can't create pgp prefix")
|
return errwrap.Wrapf(err, "can't create pgp prefix")
|
||||||
}
|
}
|
||||||
|
|
||||||
pgpKeyringPath := path.Join(pgpPrefix, pgp.DefaultKeyringFile) // default path
|
pgpKeyringPath := path.Join(pgpPrefix, pgp.DefaultKeyringFile) // default path
|
||||||
@@ -237,7 +269,7 @@ func (obj *Main) Run() error {
|
|||||||
|
|
||||||
var err error
|
var err error
|
||||||
if obj.pgpKeys, err = pgp.Import(pgpKeyringPath); err != nil && !os.IsNotExist(err) {
|
if obj.pgpKeys, err = pgp.Import(pgpKeyringPath); err != nil && !os.IsNotExist(err) {
|
||||||
return errwrap.Wrapf(err, "Can't import pgp key")
|
return errwrap.Wrapf(err, "can't import pgp key")
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj.pgpKeys == nil {
|
if obj.pgpKeys == nil {
|
||||||
@@ -249,24 +281,28 @@ func (obj *Main) Run() error {
|
|||||||
|
|
||||||
name, comment, email, err := pgp.ParseIdentity(identity)
|
name, comment, email, err := pgp.ParseIdentity(identity)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errwrap.Wrapf(err, "Can't parse user string")
|
return errwrap.Wrapf(err, "can't parse user string")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Make hash configurable
|
// TODO: Make hash configurable
|
||||||
if obj.pgpKeys, err = pgp.Generate(name, comment, email, nil); err != nil {
|
if obj.pgpKeys, err = pgp.Generate(name, comment, email, nil); err != nil {
|
||||||
return errwrap.Wrapf(err, "Can't creating pgp key")
|
return errwrap.Wrapf(err, "can't create pgp key")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := obj.pgpKeys.SaveKey(pgpKeyringPath); err != nil {
|
if err := obj.pgpKeys.SaveKey(pgpKeyringPath); err != nil {
|
||||||
return errwrap.Wrapf(err, "Can't save pgp key")
|
return errwrap.Wrapf(err, "can't save pgp key")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Import admin key
|
// TODO: Import admin key
|
||||||
}
|
}
|
||||||
|
|
||||||
var G, oldGraph *pgraph.Graph
|
oldGraph := &pgraph.Graph{}
|
||||||
|
graph := &resources.MGraph{}
|
||||||
|
// pass in the information we need
|
||||||
|
graph.Debug = obj.Flags.Debug
|
||||||
|
graph.Init()
|
||||||
|
|
||||||
// exit after `max-runtime` seconds for no reason at all...
|
// exit after `max-runtime` seconds for no reason at all...
|
||||||
if i := obj.MaxRuntime; i > 0 {
|
if i := obj.MaxRuntime; i > 0 {
|
||||||
@@ -306,10 +342,20 @@ func (obj *Main) Run() error {
|
|||||||
)
|
)
|
||||||
if EmbdEtcd == nil {
|
if EmbdEtcd == nil {
|
||||||
// TODO: verify EmbdEtcd is not nil below...
|
// TODO: verify EmbdEtcd is not nil below...
|
||||||
obj.Exit(fmt.Errorf("Main: Etcd: Creation failed!"))
|
obj.Exit(fmt.Errorf("Main: Etcd: Creation failed"))
|
||||||
} else if err := EmbdEtcd.Startup(); err != nil { // startup (returns when etcd main loop is running)
|
} else if err := EmbdEtcd.Startup(); err != nil { // startup (returns when etcd main loop is running)
|
||||||
obj.Exit(fmt.Errorf("Main: Etcd: Startup failed: %v", err))
|
obj.Exit(fmt.Errorf("Main: Etcd: Startup failed: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// wait for etcd server to be ready before continuing...
|
||||||
|
select {
|
||||||
|
case <-EmbdEtcd.ServerReady():
|
||||||
|
log.Printf("Main: Etcd: Server: Ready!")
|
||||||
|
// pass
|
||||||
|
case <-time.After(((etcd.MaxStartServerTimeout * etcd.MaxStartServerRetries) + 1) * time.Second):
|
||||||
|
obj.Exit(fmt.Errorf("Main: Etcd: Startup timeout"))
|
||||||
|
}
|
||||||
|
|
||||||
convergerStateFn := func(b bool) error {
|
convergerStateFn := func(b bool) error {
|
||||||
// exit if we are using the converged timeout and we are the
|
// exit if we are using the converged timeout and we are the
|
||||||
// root node. otherwise, if we are a child node in a remote
|
// root node. otherwise, if we are a child node in a remote
|
||||||
@@ -317,58 +363,61 @@ func (obj *Main) Run() error {
|
|||||||
// state and wait for the parent to trigger the exit.
|
// state and wait for the parent to trigger the exit.
|
||||||
if t := obj.ConvergedTimeout; obj.Depth == 0 && t >= 0 {
|
if t := obj.ConvergedTimeout; obj.Depth == 0 && t >= 0 {
|
||||||
if b {
|
if b {
|
||||||
log.Printf("Converged for %d seconds, exiting!", t)
|
log.Printf("Main: Converged for %d seconds, exiting!", t)
|
||||||
obj.Exit(nil) // trigger an exit!
|
obj.Exit(nil) // trigger an exit!
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// send our individual state into etcd for others to see
|
// send our individual state into etcd for others to see
|
||||||
return etcd.EtcdSetHostnameConverged(EmbdEtcd, hostname, b) // TODO: what should happen on error?
|
return etcd.SetHostnameConverged(EmbdEtcd, hostname, b) // TODO: what should happen on error?
|
||||||
}
|
}
|
||||||
if EmbdEtcd != nil {
|
if EmbdEtcd != nil {
|
||||||
converger.SetStateFn(convergerStateFn)
|
converger.SetStateFn(convergerStateFn)
|
||||||
}
|
}
|
||||||
|
|
||||||
var gapiChan chan error // stream events are nil errors
|
// implementation of the World API (alternates can be substituted in)
|
||||||
|
world := &etcd.World{
|
||||||
|
Hostname: hostname,
|
||||||
|
EmbdEtcd: EmbdEtcd,
|
||||||
|
}
|
||||||
|
|
||||||
|
graph.Data = &resources.ResData{
|
||||||
|
Hostname: hostname,
|
||||||
|
Converger: converger,
|
||||||
|
Prometheus: prom,
|
||||||
|
World: world,
|
||||||
|
Prefix: pgraphPrefix,
|
||||||
|
Debug: obj.Flags.Debug,
|
||||||
|
}
|
||||||
|
|
||||||
|
var gapiChan chan gapi.Next // stream events contain some instructions!
|
||||||
if obj.GAPI != nil {
|
if obj.GAPI != nil {
|
||||||
data := gapi.Data{
|
data := gapi.Data{
|
||||||
Hostname: hostname,
|
Hostname: hostname,
|
||||||
// NOTE: alternate implementations can be substituted in
|
World: world,
|
||||||
World: &etcd.World{
|
|
||||||
Hostname: hostname,
|
|
||||||
EmbdEtcd: EmbdEtcd,
|
|
||||||
},
|
|
||||||
Noop: obj.Noop,
|
Noop: obj.Noop,
|
||||||
NoWatch: obj.NoWatch,
|
//NoWatch: obj.NoWatch,
|
||||||
|
NoConfigWatch: obj.NoConfigWatch,
|
||||||
|
NoStreamWatch: obj.NoStreamWatch,
|
||||||
}
|
}
|
||||||
if err := obj.GAPI.Init(data); err != nil {
|
if err := obj.GAPI.Init(data); err != nil {
|
||||||
obj.Exit(fmt.Errorf("Main: GAPI: Init failed: %v", err))
|
obj.Exit(fmt.Errorf("Main: GAPI: Init failed: %v", err))
|
||||||
} else if !obj.NoWatch {
|
} else {
|
||||||
|
// this must generate at least one event for it to work
|
||||||
gapiChan = obj.GAPI.Next() // stream of graph switch events!
|
gapiChan = obj.GAPI.Next() // stream of graph switch events!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exitchan := make(chan struct{}) // exit on close
|
exitchan := make(chan struct{}) // exit on close
|
||||||
go func() {
|
go func() {
|
||||||
startChan := make(chan struct{}) // start signal
|
|
||||||
go func() { startChan <- struct{}{} }()
|
|
||||||
|
|
||||||
log.Println("Etcd: Starting...")
|
|
||||||
etcdChan := etcd.EtcdWatch(EmbdEtcd)
|
|
||||||
first := true // first loop or not
|
first := true // first loop or not
|
||||||
for {
|
for {
|
||||||
log.Println("Main: Waiting...")
|
log.Println("Main: Waiting...")
|
||||||
|
// The GAPI should always kick off an event on Next() at
|
||||||
|
// startup when (and if) it indeed has a graph to share!
|
||||||
|
fastPause := false
|
||||||
select {
|
select {
|
||||||
case <-startChan: // kick the loop once at start
|
case next, ok := <-gapiChan:
|
||||||
// pass
|
|
||||||
|
|
||||||
case b := <-etcdChan:
|
|
||||||
if !b { // ignore the message
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// everything else passes through to cause a compile!
|
|
||||||
|
|
||||||
case err, ok := <-gapiChan:
|
|
||||||
if !ok { // channel closed
|
if !ok { // channel closed
|
||||||
if obj.Flags.Debug {
|
if obj.Flags.Debug {
|
||||||
log.Printf("Main: GAPI exited")
|
log.Printf("Main: GAPI exited")
|
||||||
@@ -376,22 +425,29 @@ func (obj *Main) Run() error {
|
|||||||
gapiChan = nil // disable it
|
gapiChan = nil // disable it
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
obj.Exit(err) // trigger exit
|
// if we've been asked to exit...
|
||||||
continue
|
if next.Exit {
|
||||||
//return // TODO: return or wait for exitchan?
|
obj.Exit(next.Err) // trigger exit
|
||||||
|
continue // wait for exitchan
|
||||||
}
|
}
|
||||||
if obj.NoWatch { // extra safety for bad GAPI's
|
|
||||||
log.Printf("Main: GAPI stream should be quiet with NoWatch!") // fix the GAPI!
|
// the gapi lets us send an error to the channel
|
||||||
continue // no stream events should be sent
|
// this means there was a failure, but not fatal
|
||||||
|
if err := next.Err; err != nil {
|
||||||
|
log.Printf("Main: Error with graph stream: %v", err)
|
||||||
|
continue // wait for another event
|
||||||
}
|
}
|
||||||
|
// everything else passes through to cause a compile!
|
||||||
|
|
||||||
|
fastPause = next.Fast // should we pause fast?
|
||||||
|
|
||||||
case <-exitchan:
|
case <-exitchan:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj.GAPI == nil {
|
if obj.GAPI == nil {
|
||||||
log.Printf("Config: GAPI is empty!")
|
log.Printf("Main: GAPI is empty!")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -399,73 +455,129 @@ func (obj *Main) Run() error {
|
|||||||
// run graph vertex LOCK...
|
// run graph vertex LOCK...
|
||||||
if !first { // TODO: we can flatten this check out I think
|
if !first { // TODO: we can flatten this check out I think
|
||||||
converger.Pause() // FIXME: add sync wait?
|
converger.Pause() // FIXME: add sync wait?
|
||||||
G.Pause() // sync
|
graph.Pause(fastPause) // sync
|
||||||
|
|
||||||
//G.UnGroup() // FIXME: implement me if needed!
|
//graph.UnGroup() // FIXME: implement me if needed!
|
||||||
}
|
}
|
||||||
|
|
||||||
// make the graph from yaml, lib, puppet->yaml, or dsl!
|
// make the graph from yaml, lib, puppet->yaml, or dsl!
|
||||||
newGraph, err := obj.GAPI.Graph() // generate graph!
|
newGraph, err := obj.GAPI.Graph() // generate graph!
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Config: Error creating new graph: %v", err)
|
log.Printf("Main: Error creating new graph: %v", err)
|
||||||
// unpause!
|
// unpause!
|
||||||
if !first {
|
if !first {
|
||||||
G.Start(first) // sync
|
graph.Start(first) // sync
|
||||||
converger.Start() // after G.Start()
|
converger.Start() // after Start()
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
newGraph.Flags = pgraph.Flags{Debug: obj.Flags.Debug}
|
if obj.Flags.Debug {
|
||||||
// pass in the information we need
|
log.Printf("Main: New Graph: %v", newGraph)
|
||||||
newGraph.AssociateData(&resources.Data{
|
}
|
||||||
Converger: converger,
|
|
||||||
Prefix: pgraphPrefix,
|
|
||||||
Debug: obj.Flags.Debug,
|
|
||||||
})
|
|
||||||
|
|
||||||
|
// this edits the paused vertices, but it is safe to do
|
||||||
|
// so even if we don't use this new graph, since those
|
||||||
|
// value should be the same for existing vertices...
|
||||||
|
for _, v := range newGraph.Vertices() {
|
||||||
|
m := resources.VtoR(v).Meta()
|
||||||
// apply the global noop parameter if requested
|
// apply the global noop parameter if requested
|
||||||
if obj.Noop {
|
if obj.Noop {
|
||||||
for _, m := range newGraph.GraphMetas() {
|
|
||||||
m.Noop = obj.Noop
|
m.Noop = obj.Noop
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// append the semaphore to each resource
|
||||||
|
if obj.Sema > 0 { // NOTE: size == 0 would block
|
||||||
|
// a semaphore with an empty id is valid
|
||||||
|
m.Sema = append(m.Sema, fmt.Sprintf(":%d", obj.Sema))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: make sure we "UnGroup()" any semi-destructive
|
// We don't have to "UnGroup()" to compare, since we
|
||||||
// changes to the resources so our efficient GraphSync
|
// save the old graph to use when we compare.
|
||||||
// will be able to re-use and cmp to the old graph.
|
// TODO: Does this hurt performance or graph changes ?
|
||||||
newFullGraph, err := newGraph.GraphSync(oldGraph)
|
log.Printf("Main: GraphSync...")
|
||||||
if err != nil {
|
vertexCmpFn := func(v1, v2 pgraph.Vertex) (bool, error) {
|
||||||
log.Printf("Config: Error running graph sync: %v", err)
|
return resources.VtoR(v1).Compare(resources.VtoR(v2)), nil
|
||||||
|
}
|
||||||
|
vertexAddFn := func(v pgraph.Vertex) error {
|
||||||
|
err := resources.VtoR(v).Validate()
|
||||||
|
return errwrap.Wrapf(err, "could not Validate() resource")
|
||||||
|
}
|
||||||
|
vertexRemoveFn := func(v pgraph.Vertex) error {
|
||||||
|
// wait for exit before starting new graph!
|
||||||
|
resources.VtoR(v).Exit() // sync
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
edgeCmpFn := func(e1, e2 pgraph.Edge) (bool, error) {
|
||||||
|
edge1 := e1.(*resources.Edge) // panic if wrong
|
||||||
|
edge2 := e2.(*resources.Edge) // panic if wrong
|
||||||
|
return edge1.Compare(edge2), nil
|
||||||
|
}
|
||||||
|
// on success, this updates the receiver graph...
|
||||||
|
if err := oldGraph.GraphSync(newGraph, vertexCmpFn, vertexAddFn, vertexRemoveFn, edgeCmpFn); err != nil {
|
||||||
|
log.Printf("Main: Error running graph sync: %v", err)
|
||||||
// unpause!
|
// unpause!
|
||||||
if !first {
|
if !first {
|
||||||
G.Start(first) // sync
|
graph.Start(first) // sync
|
||||||
converger.Start() // after G.Start()
|
converger.Start() // after Start()
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
oldGraph = newFullGraph // save old graph
|
|
||||||
G = oldGraph.Copy() // copy to active graph
|
|
||||||
|
|
||||||
G.AutoEdges() // add autoedges; modifies the graph
|
//savedGraph := oldGraph.Copy() // save a copy for errors
|
||||||
G.AutoGroup() // run autogroup; modifies the graph
|
|
||||||
|
// TODO: should we call each Res.Setup() here instead?
|
||||||
|
|
||||||
|
// add autoedges; modifies the graph only if no error
|
||||||
|
if err := resources.AutoEdges(oldGraph); err != nil {
|
||||||
|
log.Printf("Main: Error running auto edges: %v", err)
|
||||||
|
// unpause!
|
||||||
|
if !first {
|
||||||
|
graph.Start(first) // sync
|
||||||
|
converger.Start() // after Start()
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// at this point, any time we error after a destructive
|
||||||
|
// modification of the graph we need to restore the old
|
||||||
|
// graph that was previously running, eg:
|
||||||
|
//
|
||||||
|
// oldGraph = savedGraph.Copy()
|
||||||
|
//
|
||||||
|
// which we are (luckily) able to avoid testing for now
|
||||||
|
|
||||||
|
resources.AutoGroup(oldGraph, &resources.NonReachabilityGrouper{}) // run autogroup; modifies the graph
|
||||||
// TODO: do we want to do a transitive reduction?
|
// TODO: do we want to do a transitive reduction?
|
||||||
// FIXME: run a type checker that verifies all the send->recv relationships
|
// FIXME: run a type checker that verifies all the send->recv relationships
|
||||||
|
|
||||||
log.Printf("Graph: %v", G) // show graph
|
graph.Update(oldGraph) // copy in structure of new graph
|
||||||
if obj.GraphvizFilter != "" {
|
|
||||||
if err := G.ExecGraphviz(obj.GraphvizFilter, obj.Graphviz); err != nil {
|
// Call this here because at this point the graph does
|
||||||
log.Printf("Graphviz: %v", err)
|
// not know anything about the prometheus instance.
|
||||||
} else {
|
if err := prom.UpdatePgraphStartTime(); err != nil {
|
||||||
log.Printf("Graphviz: Successfully generated graph!")
|
log.Printf("Main: Prometheus.UpdatePgraphStartTime() errored: %v", err)
|
||||||
}
|
}
|
||||||
}
|
// Start() needs to be synchronous or wait,
|
||||||
// G.Start(...) needs to be synchronous or wait,
|
|
||||||
// because if half of the nodes are started and
|
// because if half of the nodes are started and
|
||||||
// some are not ready yet and the EtcdWatch
|
// some are not ready yet and the EtcdWatch
|
||||||
// loops, we'll cause G.Pause(...) before we
|
// loops, we'll cause Pause() before we
|
||||||
// even got going, thus causing nil pointer errors
|
// even got going, thus causing nil pointer errors
|
||||||
G.Start(first) // sync
|
graph.Start(first) // sync
|
||||||
converger.Start() // after G.Start()
|
converger.Start() // after Start()
|
||||||
|
|
||||||
|
log.Printf("Main: Graph: %v", graph) // show graph
|
||||||
|
if obj.Graphviz != "" {
|
||||||
|
filter := obj.GraphvizFilter
|
||||||
|
if filter == "" {
|
||||||
|
filter = "dot" // directed graph default
|
||||||
|
}
|
||||||
|
if err := graph.ExecGraphviz(filter, obj.Graphviz, hostname); err != nil {
|
||||||
|
log.Printf("Main: Graphviz: %v", err)
|
||||||
|
} else {
|
||||||
|
log.Printf("Main: Graphviz: Successfully generated graph!")
|
||||||
|
}
|
||||||
|
}
|
||||||
first = false
|
first = false
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -473,7 +585,7 @@ func (obj *Main) Run() error {
|
|||||||
configWatcher := recwatch.NewConfigWatcher()
|
configWatcher := recwatch.NewConfigWatcher()
|
||||||
configWatcher.Flags = recwatch.Flags{Debug: obj.Flags.Debug}
|
configWatcher.Flags = recwatch.Flags{Debug: obj.Flags.Debug}
|
||||||
events := configWatcher.Events()
|
events := configWatcher.Events()
|
||||||
if !obj.NoWatch {
|
if !obj.NoWatch { // FIXME: fit this into a clean GAPI?
|
||||||
configWatcher.Add(obj.Remotes...) // add all the files...
|
configWatcher.Add(obj.Remotes...) // add all the files...
|
||||||
} else {
|
} else {
|
||||||
events = nil // signal that no-watch is true
|
events = nil // signal that no-watch is true
|
||||||
@@ -490,7 +602,7 @@ func (obj *Main) Run() error {
|
|||||||
|
|
||||||
// initialize the add watcher, which calls the f callback on map changes
|
// initialize the add watcher, which calls the f callback on map changes
|
||||||
convergerCb := func(f func(map[string]bool) error) (func(), error) {
|
convergerCb := func(f func(map[string]bool) error) (func(), error) {
|
||||||
return etcd.EtcdAddHostnameConvergedWatcher(EmbdEtcd, f)
|
return etcd.AddHostnameConvergedWatcher(EmbdEtcd, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// build remotes struct for remote ssh
|
// build remotes struct for remote ssh
|
||||||
@@ -517,6 +629,14 @@ func (obj *Main) Run() error {
|
|||||||
// TODO: is there any benefit to running the remotes above in the loop?
|
// TODO: is there any benefit to running the remotes above in the loop?
|
||||||
// wait for etcd to be running before we remote in, which we do above!
|
// wait for etcd to be running before we remote in, which we do above!
|
||||||
go remotes.Run()
|
go remotes.Run()
|
||||||
|
// wait for remotes to be ready before continuing...
|
||||||
|
select {
|
||||||
|
case <-remotes.Ready():
|
||||||
|
log.Printf("Main: Remotes: Run: Ready!")
|
||||||
|
// pass
|
||||||
|
//case <-time.After( ? * time.Second):
|
||||||
|
// obj.Exit(fmt.Errorf("Main: Remotes: Run timeout"))
|
||||||
|
}
|
||||||
|
|
||||||
if obj.GAPI == nil {
|
if obj.GAPI == nil {
|
||||||
converger.Start() // better start this for empty graphs
|
converger.Start() // better start this for empty graphs
|
||||||
@@ -525,34 +645,42 @@ func (obj *Main) Run() error {
|
|||||||
|
|
||||||
reterr := <-obj.exit // wait for exit signal
|
reterr := <-obj.exit // wait for exit signal
|
||||||
|
|
||||||
log.Println("Destroy...")
|
log.Println("Main: Destroy...")
|
||||||
|
|
||||||
if obj.GAPI != nil {
|
if obj.GAPI != nil {
|
||||||
if err := obj.GAPI.Close(); err != nil {
|
if err := obj.GAPI.Close(); err != nil {
|
||||||
err = errwrap.Wrapf(err, "GAPI closed poorly!")
|
err = errwrap.Wrapf(err, "the GAPI closed poorly")
|
||||||
reterr = multierr.Append(reterr, err) // list of errors
|
reterr = multierr.Append(reterr, err) // list of errors
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
configWatcher.Close() // stop sending file changes to remotes
|
configWatcher.Close() // stop sending file changes to remotes
|
||||||
if err := remotes.Exit(); err != nil { // tell all the remote connections to shutdown; waits!
|
if err := remotes.Exit(); err != nil { // tell all the remote connections to shutdown; waits!
|
||||||
err = errwrap.Wrapf(err, "Remote exited poorly!")
|
err = errwrap.Wrapf(err, "the Remote exited poorly")
|
||||||
reterr = multierr.Append(reterr, err) // list of errors
|
reterr = multierr.Append(reterr, err) // list of errors
|
||||||
}
|
}
|
||||||
|
|
||||||
// tell inner main loop to exit
|
// tell inner main loop to exit
|
||||||
close(exitchan)
|
close(exitchan)
|
||||||
|
|
||||||
G.Exit() // tell all the children to exit, and waits for them to do so
|
graph.Exit() // tells all the children to exit, and waits for them to do so
|
||||||
|
|
||||||
// cleanup etcd main loop last so it can process everything first
|
// cleanup etcd main loop last so it can process everything first
|
||||||
if err := EmbdEtcd.Destroy(); err != nil { // shutdown and cleanup etcd
|
if err := EmbdEtcd.Destroy(); err != nil { // shutdown and cleanup etcd
|
||||||
err = errwrap.Wrapf(err, "Etcd exited poorly!")
|
err = errwrap.Wrapf(err, "embedded Etcd exited poorly")
|
||||||
reterr = multierr.Append(reterr, err) // list of errors
|
reterr = multierr.Append(reterr, err) // list of errors
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if obj.Prometheus {
|
||||||
|
log.Printf("Main: Prometheus: Stopping instance")
|
||||||
|
if err := prom.Stop(); err != nil {
|
||||||
|
err = errwrap.Wrapf(err, "the Prometheus instance exited poorly")
|
||||||
|
reterr = multierr.Append(reterr, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if obj.Flags.Debug {
|
if obj.Flags.Debug {
|
||||||
log.Printf("Main: Graph: %v", G)
|
log.Printf("Main: Graph: %v", graph)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: wait for each vertex to exit...
|
// TODO: wait for each vertex to exit...
|
||||||
|
|||||||
8
main.go
8
main.go
@@ -1,18 +1,18 @@
|
|||||||
// Mgmt
|
// Mgmt
|
||||||
// Copyright (C) 2013-2016+ James Shubin and the project contributors
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// GNU Affero General Public License for more details.
|
// GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|||||||
81
misc/delta-cpu.sh
Executable file
81
misc/delta-cpu.sh
Executable file
@@ -0,0 +1,81 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# shitty cpu count control, useful for live demos
|
||||||
|
|
||||||
|
minimum=1 # don't decrease below this number of cpus
|
||||||
|
maximum=8 # don't increase above this number of cpus
|
||||||
|
count=1 # initial count
|
||||||
|
factor=3
|
||||||
|
function output() {
|
||||||
|
count=$1 # arg!
|
||||||
|
cat << EOF > ~/code/mgmt/examples/virt4.yaml
|
||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
virt:
|
||||||
|
- name: mgmt4
|
||||||
|
meta:
|
||||||
|
limit: .inf
|
||||||
|
burst: 0
|
||||||
|
uri: 'qemu:///session'
|
||||||
|
cpus: $count
|
||||||
|
maxcpus: $maximum
|
||||||
|
memory: 524288
|
||||||
|
boot:
|
||||||
|
- hd
|
||||||
|
disk:
|
||||||
|
- type: qcow2
|
||||||
|
source: "~/.local/share/libvirt/images/fedora-23-scratch.qcow2"
|
||||||
|
state: running
|
||||||
|
transient: false
|
||||||
|
edges: []
|
||||||
|
comment: "qemu-img create -b fedora-23.qcow2 -f qcow2 fedora-23-scratch.qcow2"
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
#tput cuu 1 && tput el # remove last line
|
||||||
|
str=''
|
||||||
|
tnuoc=$((maximum-count)) # backwards count
|
||||||
|
count2=$((count * factor))
|
||||||
|
tnuoc2=$((tnuoc * factor))
|
||||||
|
left=`yes '>' | head -$count2 | paste -s -d '' -`
|
||||||
|
right=`yes ' ' | head -$tnuoc2 | paste -s -d '' -`
|
||||||
|
str="${left}${right}"
|
||||||
|
_min=$((minimum-1))
|
||||||
|
_max=$((maximum+1))
|
||||||
|
reset # clean up once...
|
||||||
|
output $count # call function
|
||||||
|
while true; do
|
||||||
|
|
||||||
|
read -n1 -r -s -p "CPUs count is: $count; ${str} Press +/- key to adjust." key
|
||||||
|
if [ "$key" = "q" ] || [ "$key" = "Q" ]; then
|
||||||
|
echo # newline
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
if [ ! "$key" = "+" ] && [ ! "$key" = "-" ] && [ ! "$key" = "=" ] && [ ! "$key" = "_" ]; then # wrong key
|
||||||
|
reset # woops, reset it all...
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
if [ "$key" == "+" ] || [ "$key" == "=" ]; then
|
||||||
|
count=$((count+1))
|
||||||
|
fi
|
||||||
|
if [ "$key" == "-" ] || [ "$key" == "_" ]; then
|
||||||
|
count=$((count-1))
|
||||||
|
fi
|
||||||
|
if [ $count -eq $_min ]; then # min
|
||||||
|
count=$minimum
|
||||||
|
fi
|
||||||
|
if [ $count -eq $_max ]; then # max
|
||||||
|
count=$maximum
|
||||||
|
fi
|
||||||
|
|
||||||
|
tnuoc=$((maximum-count)) # backwards count
|
||||||
|
#echo "count is: $count"
|
||||||
|
#echo "tnuoc is: $tnuoc"
|
||||||
|
count2=$((count * factor))
|
||||||
|
tnuoc2=$((tnuoc * factor))
|
||||||
|
left=`yes '>' | head -$count2 | paste -s -d '' -`
|
||||||
|
right=`yes ' ' | head -$tnuoc2 | paste -s -d '' -`
|
||||||
|
str="${left}${right}"
|
||||||
|
#echo "str is: $str"
|
||||||
|
echo -ne '\r' # backup
|
||||||
|
output $count # call function
|
||||||
|
done
|
||||||
@@ -14,26 +14,38 @@ sudo_command=$(which sudo)
|
|||||||
YUM=`which yum 2>/dev/null`
|
YUM=`which yum 2>/dev/null`
|
||||||
DNF=`which dnf 2>/dev/null`
|
DNF=`which dnf 2>/dev/null`
|
||||||
APT=`which apt-get 2>/dev/null`
|
APT=`which apt-get 2>/dev/null`
|
||||||
|
BREW=`which brew 2>/dev/null`
|
||||||
|
PACMAN=`which pacman 2>/dev/null`
|
||||||
|
|
||||||
# if DNF is available use it
|
# if DNF is available use it
|
||||||
if [ -x "$DNF" ]; then
|
if [ -x "$DNF" ]; then
|
||||||
YUM=$DNF
|
YUM=$DNF
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -z "$YUM" -a -z "$APT" ]; then
|
if [ -z "$YUM" -a -z "$APT" -a -z "$BREW" -a -z "$PACMAN" ]; then
|
||||||
echo "The package managers can't be found."
|
echo "The package managers can't be found."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -z "$YUM" ]; then
|
if [ ! -z "$YUM" ]; then
|
||||||
$sudo_command $YUM install -y libvirt-devel
|
$sudo_command $YUM install -y libvirt-devel
|
||||||
|
$sudo_command $YUM install -y augeas-devel
|
||||||
|
|
||||||
fi
|
fi
|
||||||
if [ ! -z "$APT" ]; then
|
if [ ! -z "$APT" ]; then
|
||||||
$sudo_command $APT install -y libvirt-dev || true
|
$sudo_command $APT install -y libvirt-dev || true
|
||||||
|
$sudo_command $APT install -y libaugeas-dev || true
|
||||||
$sudo_command $APT install -y libpcap0.8-dev || true
|
$sudo_command $APT install -y libpcap0.8-dev || true
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [ ! -z "$BREW" ]; then
|
||||||
|
$BREW install libvirt || true
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -z "$PACMAN" ]; then
|
||||||
|
$sudo_command $PACMAN -S --noconfirm libvirt augeas libpcap
|
||||||
|
fi
|
||||||
|
|
||||||
if [ $travis -eq 0 ]; then
|
if [ $travis -eq 0 ]; then
|
||||||
if [ ! -z "$YUM" ]; then
|
if [ ! -z "$YUM" ]; then
|
||||||
# some go dependencies are stored in mercurial
|
# some go dependencies are stored in mercurial
|
||||||
@@ -47,11 +59,14 @@ if [ $travis -eq 0 ]; then
|
|||||||
$sudo_command $APT install -y golang-golang-x-tools || true
|
$sudo_command $APT install -y golang-golang-x-tools || true
|
||||||
$sudo_command $APT install -y golang-go.tools || true
|
$sudo_command $APT install -y golang-go.tools || true
|
||||||
fi
|
fi
|
||||||
|
if [ ! -z "$PACMAN" ]; then
|
||||||
|
$sudo_command $PACMAN -S --noconfirm go
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# if golang is too old, we don't want to fail with an obscure error later
|
# if golang is too old, we don't want to fail with an obscure error later
|
||||||
if go version | grep 'go1\.[0123]\.'; then
|
if go version | grep 'go1\.[012345]\.'; then
|
||||||
echo "mgmt requires go1.4 or higher."
|
echo "mgmt requires go1.6 or higher."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -65,4 +80,5 @@ if [[ $ret != 0 ]]; then
|
|||||||
fi
|
fi
|
||||||
go get golang.org/x/tools/cmd/stringer # for automatic stringer-ing
|
go get golang.org/x/tools/cmd/stringer # for automatic stringer-ing
|
||||||
go get github.com/golang/lint/golint # for `golint`-ing
|
go get github.com/golang/lint/golint # for `golint`-ing
|
||||||
|
go get -u gopkg.in/alecthomas/gometalinter.v1 && mv "$(dirname $(which gometalinter.v1))/gometalinter.v1" "$(dirname $(which gometalinter.v1))/gometalinter" && gometalinter --install # bonus
|
||||||
cd "$XPWD" >/dev/null
|
cd "$XPWD" >/dev/null
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ After=systemd-networkd.service
|
|||||||
Requires=systemd-networkd.service
|
Requires=systemd-networkd.service
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
ExecStart=/usr/bin/mgmt run ${OPTS}
|
ExecStart=/usr/bin/mgmt run $OPTS
|
||||||
RestartSec=5s
|
RestartSec=5s
|
||||||
Restart=always
|
Restart=always
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
// Mgmt
|
// Mgmt
|
||||||
// Copyright (C) 2013-2016+ James Shubin and the project contributors
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// GNU Affero General Public License for more details.
|
// GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package pgp
|
package pgp
|
||||||
|
|||||||
@@ -1,535 +0,0 @@
|
|||||||
// Mgmt
|
|
||||||
// Copyright (C) 2013-2016+ James Shubin and the project contributors
|
|
||||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
|
||||||
//
|
|
||||||
// This program is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
package pgraph
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"math"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/purpleidea/mgmt/event"
|
|
||||||
"github.com/purpleidea/mgmt/resources"
|
|
||||||
|
|
||||||
errwrap "github.com/pkg/errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
// GetTimestamp returns the timestamp of a vertex
|
|
||||||
func (v *Vertex) GetTimestamp() int64 {
|
|
||||||
return v.timestamp
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateTimestamp updates the timestamp on a vertex and returns the new value
|
|
||||||
func (v *Vertex) UpdateTimestamp() int64 {
|
|
||||||
v.timestamp = time.Now().UnixNano() // update
|
|
||||||
return v.timestamp
|
|
||||||
}
|
|
||||||
|
|
||||||
// OKTimestamp returns true if this element can run right now?
|
|
||||||
func (g *Graph) OKTimestamp(v *Vertex) bool {
|
|
||||||
// these are all the vertices pointing TO v, eg: ??? -> v
|
|
||||||
for _, n := range g.IncomingGraphVertices(v) {
|
|
||||||
// if the vertex has a greater timestamp than any pre-req (n)
|
|
||||||
// then we can't run right now...
|
|
||||||
// if they're equal (eg: on init of 0) then we also can't run
|
|
||||||
// b/c we should let our pre-req's go first...
|
|
||||||
x, y := v.GetTimestamp(), n.GetTimestamp()
|
|
||||||
if g.Flags.Debug {
|
|
||||||
log.Printf("%s[%s]: OKTimestamp: (%v) >= %s[%s](%v): !%v", v.Kind(), v.GetName(), x, n.Kind(), n.GetName(), y, x >= y)
|
|
||||||
}
|
|
||||||
if x >= y {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Poke notifies nodes after me in the dependency graph that they need refreshing...
|
|
||||||
// NOTE: this assumes that this can never fail or need to be rescheduled
|
|
||||||
func (g *Graph) Poke(v *Vertex, activity bool) error {
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
// these are all the vertices pointing AWAY FROM v, eg: v -> ???
|
|
||||||
for _, n := range g.OutgoingGraphVertices(v) {
|
|
||||||
// XXX: if we're in state event and haven't been cancelled by
|
|
||||||
// apply, then we can cancel a poke to a child, right? XXX
|
|
||||||
// XXX: if n.Res.getState() != resources.ResStateEvent || activity { // is this correct?
|
|
||||||
if true || activity { // XXX: ???
|
|
||||||
if g.Flags.Debug {
|
|
||||||
log.Printf("%s[%s]: Poke: %s[%s]", v.Kind(), v.GetName(), n.Kind(), n.GetName())
|
|
||||||
}
|
|
||||||
wg.Add(1)
|
|
||||||
go func(nn *Vertex) error {
|
|
||||||
defer wg.Done()
|
|
||||||
edge := g.Adjacency[v][nn] // lookup
|
|
||||||
notify := edge.Notify && edge.Refresh()
|
|
||||||
|
|
||||||
// FIXME: is it okay that this is sync?
|
|
||||||
nn.SendEvent(event.EventPoke, true, notify)
|
|
||||||
// TODO: check return value?
|
|
||||||
return nil // never error for now...
|
|
||||||
}(n)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
if g.Flags.Debug {
|
|
||||||
log.Printf("%s[%s]: Poke: %s[%s]: Skipped!", v.Kind(), v.GetName(), n.Kind(), n.GetName())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
wg.Wait() // wait for all the pokes to complete
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BackPoke pokes the pre-requisites that are stale and need to run before I can run.
|
|
||||||
func (g *Graph) BackPoke(v *Vertex) {
|
|
||||||
// these are all the vertices pointing TO v, eg: ??? -> v
|
|
||||||
for _, n := range g.IncomingGraphVertices(v) {
|
|
||||||
x, y, s := v.GetTimestamp(), n.GetTimestamp(), n.Res.GetState()
|
|
||||||
// if the parent timestamp needs poking AND it's not in state
|
|
||||||
// ResStateEvent, then poke it. If the parent is in ResStateEvent it
|
|
||||||
// means that an event is pending, so we'll be expecting a poke
|
|
||||||
// back soon, so we can safely discard the extra parent poke...
|
|
||||||
// TODO: implement a stateLT (less than) to tell if something
|
|
||||||
// happens earlier in the state cycle and that doesn't wrap nil
|
|
||||||
if x >= y && (s != resources.ResStateEvent && s != resources.ResStateCheckApply) {
|
|
||||||
if g.Flags.Debug {
|
|
||||||
log.Printf("%s[%s]: BackPoke: %s[%s]", v.Kind(), v.GetName(), n.Kind(), n.GetName())
|
|
||||||
}
|
|
||||||
// FIXME: is it okay that this is sync?
|
|
||||||
n.SendEvent(event.EventBackPoke, true, false)
|
|
||||||
} else {
|
|
||||||
if g.Flags.Debug {
|
|
||||||
log.Printf("%s[%s]: BackPoke: %s[%s]: Skipped!", v.Kind(), v.GetName(), n.Kind(), n.GetName())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// RefreshPending determines if any previous nodes have a refresh pending here.
|
|
||||||
// If this is true, it means I am expected to apply a refresh when I next run.
|
|
||||||
func (g *Graph) RefreshPending(v *Vertex) bool {
|
|
||||||
var refresh bool
|
|
||||||
for _, edge := range g.IncomingGraphEdges(v) {
|
|
||||||
// if we asked for a notify *and* if one is pending!
|
|
||||||
if edge.Notify && edge.Refresh() {
|
|
||||||
refresh = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return refresh
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetUpstreamRefresh sets the refresh value to any upstream vertices.
|
|
||||||
func (g *Graph) SetUpstreamRefresh(v *Vertex, b bool) {
|
|
||||||
for _, edge := range g.IncomingGraphEdges(v) {
|
|
||||||
if edge.Notify {
|
|
||||||
edge.SetRefresh(b)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetDownstreamRefresh sets the refresh value to any downstream vertices.
|
|
||||||
func (g *Graph) SetDownstreamRefresh(v *Vertex, b bool) {
|
|
||||||
for _, edge := range g.OutgoingGraphEdges(v) {
|
|
||||||
// if we asked for a notify *and* if one is pending!
|
|
||||||
if edge.Notify {
|
|
||||||
edge.SetRefresh(b)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process is the primary function to execute for a particular vertex in the graph.
|
|
||||||
func (g *Graph) Process(v *Vertex) error {
|
|
||||||
obj := v.Res
|
|
||||||
if g.Flags.Debug {
|
|
||||||
log.Printf("%s[%s]: Process()", obj.Kind(), obj.GetName())
|
|
||||||
}
|
|
||||||
obj.SetState(resources.ResStateEvent)
|
|
||||||
var ok = true
|
|
||||||
var applied = false // did we run an apply?
|
|
||||||
// is it okay to run dependency wise right now?
|
|
||||||
// if not, that's okay because when the dependency runs, it will poke
|
|
||||||
// us back and we will run if needed then!
|
|
||||||
if g.OKTimestamp(v) {
|
|
||||||
if g.Flags.Debug {
|
|
||||||
log.Printf("%s[%s]: OKTimestamp(%v)", obj.Kind(), obj.GetName(), v.GetTimestamp())
|
|
||||||
}
|
|
||||||
|
|
||||||
obj.SetState(resources.ResStateCheckApply)
|
|
||||||
|
|
||||||
// connect any senders to receivers and detect if values changed
|
|
||||||
if updated, err := obj.SendRecv(obj); err != nil {
|
|
||||||
return errwrap.Wrapf(err, "could not SendRecv in Process")
|
|
||||||
} else if len(updated) > 0 {
|
|
||||||
for _, changed := range updated {
|
|
||||||
if changed { // at least one was updated
|
|
||||||
obj.StateOK(false) // invalidate cache, mark as dirty
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var noop = obj.Meta().Noop // lookup the noop value
|
|
||||||
var refresh bool
|
|
||||||
var checkOK bool
|
|
||||||
var err error
|
|
||||||
|
|
||||||
if g.Flags.Debug {
|
|
||||||
log.Printf("%s[%s]: CheckApply(%t)", obj.Kind(), obj.GetName(), !noop)
|
|
||||||
}
|
|
||||||
|
|
||||||
// lookup the refresh (notification) variable
|
|
||||||
refresh = g.RefreshPending(v) // do i need to perform a refresh?
|
|
||||||
obj.SetRefresh(refresh) // tell the resource
|
|
||||||
|
|
||||||
// check cached state, to skip CheckApply; can't skip if refreshing
|
|
||||||
if !refresh && obj.IsStateOK() {
|
|
||||||
checkOK, err = true, nil
|
|
||||||
|
|
||||||
// NOTE: technically this block is wrong because we don't know
|
|
||||||
// if the resource implements refresh! If it doesn't, we could
|
|
||||||
// skip this, but it doesn't make a big difference under noop!
|
|
||||||
} else if noop && refresh { // had a refresh to do w/ noop!
|
|
||||||
checkOK, err = false, nil // therefore the state is wrong
|
|
||||||
|
|
||||||
// run the CheckApply!
|
|
||||||
} else {
|
|
||||||
// if this fails, don't UpdateTimestamp()
|
|
||||||
checkOK, err = obj.CheckApply(!noop)
|
|
||||||
}
|
|
||||||
|
|
||||||
if checkOK && err != nil { // should never return this way
|
|
||||||
log.Fatalf("%s[%s]: CheckApply(): %t, %+v", obj.Kind(), obj.GetName(), checkOK, err)
|
|
||||||
}
|
|
||||||
if g.Flags.Debug {
|
|
||||||
log.Printf("%s[%s]: CheckApply(): %t, %v", obj.Kind(), obj.GetName(), checkOK, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// if CheckApply ran without noop and without error, state should be good
|
|
||||||
if !noop && err == nil { // aka !noop || checkOK
|
|
||||||
obj.StateOK(true) // reset
|
|
||||||
if refresh {
|
|
||||||
g.SetUpstreamRefresh(v, false) // refresh happened, clear the request
|
|
||||||
obj.SetRefresh(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !checkOK { // if state *was* not ok, we had to have apply'ed
|
|
||||||
if err != nil { // error during check or apply
|
|
||||||
ok = false
|
|
||||||
} else {
|
|
||||||
applied = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// when noop is true we always want to update timestamp
|
|
||||||
if noop && err == nil {
|
|
||||||
ok = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if ok {
|
|
||||||
// did we actually do work?
|
|
||||||
activity := applied
|
|
||||||
if noop {
|
|
||||||
activity = false // no we didn't do work...
|
|
||||||
}
|
|
||||||
|
|
||||||
if activity { // add refresh flag to downstream edges...
|
|
||||||
g.SetDownstreamRefresh(v, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
// update this timestamp *before* we poke or the poked
|
|
||||||
// nodes might fail due to having a too old timestamp!
|
|
||||||
v.UpdateTimestamp() // this was touched...
|
|
||||||
obj.SetState(resources.ResStatePoking) // can't cancel parent poke
|
|
||||||
if err := g.Poke(v, activity); err != nil {
|
|
||||||
return errwrap.Wrapf(err, "the Poke() failed")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// poke at our pre-req's instead since they need to refresh/run...
|
|
||||||
return errwrap.Wrapf(err, "could not Process() successfully")
|
|
||||||
}
|
|
||||||
// else... only poke at the pre-req's that need to run
|
|
||||||
go g.BackPoke(v)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SentinelErr is a sentinal as an error type that wraps an arbitrary error.
|
|
||||||
type SentinelErr struct {
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Error is the required method to fulfill the error type.
|
|
||||||
func (obj *SentinelErr) Error() string {
|
|
||||||
return obj.err.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Worker is the common run frontend of the vertex. It handles all of the retry
|
|
||||||
// and retry delay common code, and ultimately returns the final status of this
|
|
||||||
// vertex execution.
|
|
||||||
func (g *Graph) Worker(v *Vertex) error {
|
|
||||||
// listen for chan events from Watch() and run
|
|
||||||
// the Process() function when they're received
|
|
||||||
// this avoids us having to pass the data into
|
|
||||||
// the Watch() function about which graph it is
|
|
||||||
// running on, which isolates things nicely...
|
|
||||||
obj := v.Res
|
|
||||||
// TODO: is there a better system for the `Watching` flag?
|
|
||||||
obj.SetWatching(true)
|
|
||||||
defer obj.SetWatching(false)
|
|
||||||
processChan := make(chan event.Event)
|
|
||||||
go func() {
|
|
||||||
running := false
|
|
||||||
var timer = time.NewTimer(time.Duration(math.MaxInt64)) // longest duration
|
|
||||||
if !timer.Stop() {
|
|
||||||
<-timer.C // unnecessary, shouldn't happen
|
|
||||||
}
|
|
||||||
var delay = time.Duration(v.Meta().Delay) * time.Millisecond
|
|
||||||
var retry = v.Meta().Retry // number of tries left, -1 for infinite
|
|
||||||
var saved event.Event
|
|
||||||
Loop:
|
|
||||||
for {
|
|
||||||
// this has to be synchronous, because otherwise the Res
|
|
||||||
// event loop will keep running and change state,
|
|
||||||
// causing the converged timeout to fire!
|
|
||||||
select {
|
|
||||||
case event, ok := <-processChan: // must use like this
|
|
||||||
if running && ok {
|
|
||||||
// we got an event that wasn't a close,
|
|
||||||
// while we were waiting for the timer!
|
|
||||||
// if this happens, it might be a bug:(
|
|
||||||
log.Fatalf("%s[%s]: Worker: Unexpected event: %+v", v.Kind(), v.GetName(), event)
|
|
||||||
}
|
|
||||||
if !ok { // processChan closed, let's exit
|
|
||||||
break Loop // no event, so no ack!
|
|
||||||
}
|
|
||||||
|
|
||||||
// the above mentioned synchronous part, is the
|
|
||||||
// running of this function, paired with an ack.
|
|
||||||
if e := g.Process(v); e != nil {
|
|
||||||
saved = event
|
|
||||||
log.Printf("%s[%s]: CheckApply errored: %v", v.Kind(), v.GetName(), e)
|
|
||||||
if retry == 0 {
|
|
||||||
// wrap the error in the sentinel
|
|
||||||
event.ACKNACK(&SentinelErr{e}) // fail the Watch()
|
|
||||||
break Loop
|
|
||||||
}
|
|
||||||
if retry > 0 { // don't decrement the -1
|
|
||||||
retry--
|
|
||||||
}
|
|
||||||
log.Printf("%s[%s]: CheckApply: Retrying after %.4f seconds (%d left)", v.Kind(), v.GetName(), delay.Seconds(), retry)
|
|
||||||
// start the timer...
|
|
||||||
timer.Reset(delay)
|
|
||||||
running = true
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
retry = v.Meta().Retry // reset on success
|
|
||||||
event.ACK() // sync
|
|
||||||
|
|
||||||
case <-timer.C:
|
|
||||||
if !timer.Stop() {
|
|
||||||
//<-timer.C // blocks, docs are wrong!
|
|
||||||
}
|
|
||||||
running = false
|
|
||||||
log.Printf("%s[%s]: CheckApply delay expired!", v.Kind(), v.GetName())
|
|
||||||
// re-send this failed event, to trigger a CheckApply()
|
|
||||||
go func() { processChan <- saved }()
|
|
||||||
// TODO: should we send a fake event instead?
|
|
||||||
//saved = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
var err error // propagate the error up (this is a permanent BAD error!)
|
|
||||||
// the watch delay runs inside of the Watch resource loop, so that it
|
|
||||||
// can still process signals and exit if needed. It shouldn't run any
|
|
||||||
// resource specific code since this is supposed to be a retry delay.
|
|
||||||
// NOTE: we're using the same retry and delay metaparams that CheckApply
|
|
||||||
// uses. This is for practicality. We can separate them later if needed!
|
|
||||||
var watchDelay time.Duration
|
|
||||||
var watchRetry = v.Meta().Retry // number of tries left, -1 for infinite
|
|
||||||
// watch blocks until it ends, & errors to retry
|
|
||||||
for {
|
|
||||||
// TODO: do we have to stop the converged-timeout when in this block (perhaps we're in the delay block!)
|
|
||||||
// TODO: should we setup/manage some of the converged timeout stuff in here anyways?
|
|
||||||
|
|
||||||
// if a retry-delay was requested, wait, but don't block our events!
|
|
||||||
if watchDelay > 0 {
|
|
||||||
//var pendingSendEvent bool
|
|
||||||
timer := time.NewTimer(watchDelay)
|
|
||||||
Loop:
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-timer.C: // the wait is over
|
|
||||||
break Loop // critical
|
|
||||||
|
|
||||||
// TODO: resources could have a separate exit channel to avoid this complexity!?
|
|
||||||
case event := <-obj.Events():
|
|
||||||
// NOTE: this code should match the similar Res code!
|
|
||||||
//cuid.SetConverged(false) // TODO: ?
|
|
||||||
if exit, send := obj.ReadEvent(&event); exit {
|
|
||||||
return nil // exit
|
|
||||||
} else if send {
|
|
||||||
// if we dive down this rabbit hole, our
|
|
||||||
// timer.C won't get seen until we get out!
|
|
||||||
// in this situation, the Watch() is blocked
|
|
||||||
// from performing until CheckApply returns
|
|
||||||
// successfully, or errors out. This isn't
|
|
||||||
// so bad, but we should document it. Is it
|
|
||||||
// possible that some resource *needs* Watch
|
|
||||||
// to run to be able to execute a CheckApply?
|
|
||||||
// That situation shouldn't be common, and
|
|
||||||
// should probably not be allowed. Can we
|
|
||||||
// avoid it though?
|
|
||||||
//if exit, err := doSend(); exit || err != nil {
|
|
||||||
// return err // we exit or bubble up a NACK...
|
|
||||||
//}
|
|
||||||
// Instead of doing the above, we can
|
|
||||||
// add events to a pending list, and
|
|
||||||
// when we finish the delay, we can run
|
|
||||||
// them.
|
|
||||||
//pendingSendEvent = true // all events are identical for now...
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
timer.Stop() // it's nice to cleanup
|
|
||||||
log.Printf("%s[%s]: Watch delay expired!", v.Kind(), v.GetName())
|
|
||||||
// NOTE: we can avoid the send if running Watch guarantees
|
|
||||||
// one CheckApply event on startup!
|
|
||||||
//if pendingSendEvent { // TODO: should this become a list in the future?
|
|
||||||
// if exit, err := obj.DoSend(processChan, ""); exit || err != nil {
|
|
||||||
// return err // we exit or bubble up a NACK...
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: reset the watch retry count after some amount of success
|
|
||||||
v.Res.RegisterConverger()
|
|
||||||
e := v.Res.Watch(processChan)
|
|
||||||
v.Res.UnregisterConverger()
|
|
||||||
if e == nil { // exit signal
|
|
||||||
err = nil // clean exit
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if sentinelErr, ok := e.(*SentinelErr); ok { // unwrap the sentinel
|
|
||||||
err = sentinelErr.err
|
|
||||||
break // sentinel means, perma-exit
|
|
||||||
}
|
|
||||||
log.Printf("%s[%s]: Watch errored: %v", v.Kind(), v.GetName(), e)
|
|
||||||
if watchRetry == 0 {
|
|
||||||
err = fmt.Errorf("Permanent watch error: %v", e)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if watchRetry > 0 { // don't decrement the -1
|
|
||||||
watchRetry--
|
|
||||||
}
|
|
||||||
watchDelay = time.Duration(v.Meta().Delay) * time.Millisecond
|
|
||||||
log.Printf("%s[%s]: Watch: Retrying after %.4f seconds (%d left)", v.Kind(), v.GetName(), watchDelay.Seconds(), watchRetry)
|
|
||||||
// We need to trigger a CheckApply after Watch restarts, so that
|
|
||||||
// we catch any lost events that happened while down. We do this
|
|
||||||
// by getting the Watch resource to send one event once it's up!
|
|
||||||
//v.SendEvent(eventPoke, false, false)
|
|
||||||
}
|
|
||||||
close(processChan)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start is a main kick to start the graph. It goes through in reverse topological
|
|
||||||
// sort order so that events can't hit un-started vertices.
|
|
||||||
func (g *Graph) Start(first bool) { // start or continue
|
|
||||||
log.Printf("State: %v -> %v", g.setState(graphStateStarting), g.getState())
|
|
||||||
defer log.Printf("State: %v -> %v", g.setState(graphStateStarted), g.getState())
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
t, _ := g.TopologicalSort()
|
|
||||||
// TODO: only calculate indegree if `first` is true to save resources
|
|
||||||
indegree := g.InDegree() // compute all of the indegree's
|
|
||||||
for _, v := range Reverse(t) {
|
|
||||||
// selective poke: here we reduce the number of initial pokes
|
|
||||||
// to the minimum required to activate every vertex in the
|
|
||||||
// graph, either by direct action, or by getting poked by a
|
|
||||||
// vertex that was previously activated. if we poke each vertex
|
|
||||||
// that has no incoming edges, then we can be sure to reach the
|
|
||||||
// whole graph. Please note: this may mask certain optimization
|
|
||||||
// failures, such as any poke limiting code in Poke() or
|
|
||||||
// BackPoke(). You might want to disable this selective start
|
|
||||||
// when experimenting with and testing those elements.
|
|
||||||
// if we are unpausing (since it's not the first run of this
|
|
||||||
// function) we need to poke to *unpause* every graph vertex,
|
|
||||||
// and not just selectively the subset with no indegree.
|
|
||||||
if (!first) || indegree[v] == 0 {
|
|
||||||
v.Res.Starter(true) // let the startup code know to poke
|
|
||||||
}
|
|
||||||
|
|
||||||
if !v.Res.IsWatching() { // if Watch() is not running...
|
|
||||||
g.wg.Add(1)
|
|
||||||
// must pass in value to avoid races...
|
|
||||||
// see: https://ttboj.wordpress.com/2015/07/27/golang-parallelism-issues-causing-too-many-open-files-error/
|
|
||||||
go func(vv *Vertex) {
|
|
||||||
defer g.wg.Done()
|
|
||||||
// TODO: if a sufficient number of workers error,
|
|
||||||
// should something be done? Will these restart
|
|
||||||
// after perma-failure if we have a graph change?
|
|
||||||
if err := g.Worker(vv); err != nil { // contains the Watch and CheckApply loops
|
|
||||||
log.Printf("%s[%s]: Exited with failure: %v", vv.Kind(), vv.GetName(), err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Printf("%s[%s]: Exited", vv.Kind(), vv.GetName())
|
|
||||||
}(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// let the vertices run their startup code in parallel
|
|
||||||
wg.Add(1)
|
|
||||||
go func(vv *Vertex) {
|
|
||||||
defer wg.Done()
|
|
||||||
vv.Res.Started() // block until started
|
|
||||||
}(v)
|
|
||||||
|
|
||||||
if !first { // unpause!
|
|
||||||
v.Res.SendEvent(event.EventStart, true, false) // sync!
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
wg.Wait() // wait for everyone
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pause sends pause events to the graph in a topological sort order.
|
|
||||||
func (g *Graph) Pause() {
|
|
||||||
log.Printf("State: %v -> %v", g.setState(graphStatePausing), g.getState())
|
|
||||||
defer log.Printf("State: %v -> %v", g.setState(graphStatePaused), g.getState())
|
|
||||||
t, _ := g.TopologicalSort()
|
|
||||||
for _, v := range t { // squeeze out the events...
|
|
||||||
v.SendEvent(event.EventPause, true, false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Exit sends exit events to the graph in a topological sort order.
|
|
||||||
func (g *Graph) Exit() {
|
|
||||||
if g == nil {
|
|
||||||
return
|
|
||||||
} // empty graph that wasn't populated yet
|
|
||||||
t, _ := g.TopologicalSort()
|
|
||||||
for _, v := range t { // squeeze out the events...
|
|
||||||
// turn off the taps...
|
|
||||||
// XXX: consider instead doing this by closing the Res.events channel instead?
|
|
||||||
// XXX: do this by sending an exit signal, and then returning
|
|
||||||
// when we hit the 'default' in the select statement!
|
|
||||||
// XXX: we can do this to quiesce, but it's not necessary now
|
|
||||||
|
|
||||||
v.SendEvent(event.EventExit, true, false)
|
|
||||||
}
|
|
||||||
g.wg.Wait() // for now, this doesn't need to be a separate Wait() method
|
|
||||||
}
|
|
||||||
@@ -1,103 +0,0 @@
|
|||||||
// Mgmt
|
|
||||||
// Copyright (C) 2013-2016+ James Shubin and the project contributors
|
|
||||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
|
||||||
//
|
|
||||||
// This program is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
// Package pgraph represents the internal "pointer graph" that we use.
|
|
||||||
package pgraph
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/purpleidea/mgmt/resources"
|
|
||||||
)
|
|
||||||
|
|
||||||
// add edges to the vertex in a graph based on if it matches a uid list
|
|
||||||
func (g *Graph) addEdgesByMatchingUIDS(v *Vertex, uids []resources.ResUID) []bool {
|
|
||||||
// search for edges and see what matches!
|
|
||||||
var result []bool
|
|
||||||
|
|
||||||
// loop through each uid, and see if it matches any vertex
|
|
||||||
for _, uid := range uids {
|
|
||||||
var found = false
|
|
||||||
// uid is a ResUID object
|
|
||||||
for _, vv := range g.GetVertices() { // search
|
|
||||||
if v == vv { // skip self
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if g.Flags.Debug {
|
|
||||||
log.Printf("Compile: AutoEdge: Match: %v[%v] with UID: %v[%v]", vv.Kind(), vv.GetName(), uid.Kind(), uid.GetName())
|
|
||||||
}
|
|
||||||
// we must match to an effective UID for the resource,
|
|
||||||
// that is to say, the name value of a res is a helpful
|
|
||||||
// handle, but it is not necessarily a unique identity!
|
|
||||||
// remember, resources can return multiple UID's each!
|
|
||||||
if resources.UIDExistsInUIDs(uid, vv.GetUIDs()) {
|
|
||||||
// add edge from: vv -> v
|
|
||||||
if uid.Reversed() {
|
|
||||||
txt := fmt.Sprintf("AutoEdge: %v[%v] -> %v[%v]", vv.Kind(), vv.GetName(), v.Kind(), v.GetName())
|
|
||||||
log.Printf("Compile: Adding %v", txt)
|
|
||||||
g.AddEdge(vv, v, NewEdge(txt))
|
|
||||||
} else { // edges go the "normal" way, eg: pkg resource
|
|
||||||
txt := fmt.Sprintf("AutoEdge: %v[%v] -> %v[%v]", v.Kind(), v.GetName(), vv.Kind(), vv.GetName())
|
|
||||||
log.Printf("Compile: Adding %v", txt)
|
|
||||||
g.AddEdge(v, vv, NewEdge(txt))
|
|
||||||
}
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result = append(result, found)
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
// AutoEdges adds the automatic edges to the graph.
|
|
||||||
func (g *Graph) AutoEdges() {
|
|
||||||
log.Println("Compile: Adding AutoEdges...")
|
|
||||||
for _, v := range g.GetVertices() { // for each vertexes autoedges
|
|
||||||
if !v.Meta().AutoEdge { // is the metaparam true?
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
autoEdgeObj := v.AutoEdges()
|
|
||||||
if autoEdgeObj == nil {
|
|
||||||
log.Printf("%v[%v]: Config: No auto edges were found!", v.Kind(), v.GetName())
|
|
||||||
continue // next vertex
|
|
||||||
}
|
|
||||||
|
|
||||||
for { // while the autoEdgeObj has more uids to add...
|
|
||||||
uids := autoEdgeObj.Next() // get some!
|
|
||||||
if uids == nil {
|
|
||||||
log.Printf("%v[%v]: Config: The auto edge list is empty!", v.Kind(), v.GetName())
|
|
||||||
break // inner loop
|
|
||||||
}
|
|
||||||
if g.Flags.Debug {
|
|
||||||
log.Println("Compile: AutoEdge: UIDS:")
|
|
||||||
for i, u := range uids {
|
|
||||||
log.Printf("Compile: AutoEdge: UID%d: %v", i, u)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// match and add edges
|
|
||||||
result := g.addEdgesByMatchingUIDS(v, uids)
|
|
||||||
|
|
||||||
// report back, and find out if we should continue
|
|
||||||
if !autoEdgeObj.Test(result) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
155
pgraph/graphsync.go
Normal file
155
pgraph/graphsync.go
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
// Mgmt
|
||||||
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
|
//
|
||||||
|
// 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package pgraph
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
errwrap "github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
func strVertexCmpFn(v1, v2 Vertex) (bool, error) {
|
||||||
|
if v1.String() == "" || v2.String() == "" {
|
||||||
|
return false, fmt.Errorf("empty vertex")
|
||||||
|
}
|
||||||
|
return v1.String() == v2.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func strEdgeCmpFn(e1, e2 Edge) (bool, error) {
|
||||||
|
if e1.String() == "" || e2.String() == "" {
|
||||||
|
return false, fmt.Errorf("empty edge")
|
||||||
|
}
|
||||||
|
return e1.String() == e2.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GraphSync updates the Graph so that it matches the newGraph. It leaves
|
||||||
|
// identical elements alone so that they don't need to be refreshed.
|
||||||
|
// It tries to mutate existing elements into new ones, if they support this.
|
||||||
|
// This updates the Graph on success only.
|
||||||
|
// FIXME: should we do this with copies of the vertex resources?
|
||||||
|
func (obj *Graph) GraphSync(newGraph *Graph, vertexCmpFn func(Vertex, Vertex) (bool, error), vertexAddFn func(Vertex) error, vertexRemoveFn func(Vertex) error, edgeCmpFn func(Edge, Edge) (bool, error)) error {
|
||||||
|
|
||||||
|
oldGraph := obj.Copy() // work on a copy of the old graph
|
||||||
|
if oldGraph == nil {
|
||||||
|
var err error
|
||||||
|
oldGraph, err = NewGraph(newGraph.GetName()) // copy over the name
|
||||||
|
if err != nil {
|
||||||
|
return errwrap.Wrapf(err, "GraphSync failed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
oldGraph.SetName(newGraph.GetName()) // overwrite the name
|
||||||
|
|
||||||
|
if vertexCmpFn == nil {
|
||||||
|
vertexCmpFn = strVertexCmpFn // use simple string cmp version
|
||||||
|
}
|
||||||
|
if vertexAddFn == nil {
|
||||||
|
vertexAddFn = func(Vertex) error { return nil } // noop
|
||||||
|
}
|
||||||
|
if vertexRemoveFn == nil {
|
||||||
|
vertexRemoveFn = func(Vertex) error { return nil } // noop
|
||||||
|
}
|
||||||
|
if edgeCmpFn == nil {
|
||||||
|
edgeCmpFn = strEdgeCmpFn // use simple string cmp version
|
||||||
|
}
|
||||||
|
|
||||||
|
var lookup = make(map[Vertex]Vertex)
|
||||||
|
var vertexKeep []Vertex // list of vertices which are the same in new graph
|
||||||
|
var edgeKeep []Edge // list of vertices which are the same in new graph
|
||||||
|
|
||||||
|
for v := range newGraph.Adjacency() { // loop through the vertices (resources)
|
||||||
|
var vertex Vertex
|
||||||
|
// step one, direct compare with res.Compare
|
||||||
|
if vertex == nil { // redundant guard for consistency
|
||||||
|
fn := func(vv Vertex) (bool, error) {
|
||||||
|
b, err := vertexCmpFn(vv, v)
|
||||||
|
return b, errwrap.Wrapf(err, "vertexCmpFn failed")
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
vertex, err = oldGraph.VertexMatchFn(fn)
|
||||||
|
if err != nil {
|
||||||
|
return errwrap.Wrapf(err, "VertexMatchFn failed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: consider adding a mutate API.
|
||||||
|
// step two, try and mutate with res.Mutate
|
||||||
|
//if vertex == nil { // not found yet...
|
||||||
|
// vertex = oldGraph.MutateMatch(res)
|
||||||
|
//}
|
||||||
|
|
||||||
|
if vertex == nil { // no match found yet
|
||||||
|
if err := vertexAddFn(v); err != nil {
|
||||||
|
return errwrap.Wrapf(err, "vertexAddFn failed")
|
||||||
|
}
|
||||||
|
vertex = v
|
||||||
|
oldGraph.AddVertex(vertex) // call standalone in case not part of an edge
|
||||||
|
}
|
||||||
|
lookup[v] = vertex // used for constructing edges
|
||||||
|
vertexKeep = append(vertexKeep, vertex) // append
|
||||||
|
}
|
||||||
|
|
||||||
|
// get rid of any vertices we shouldn't keep (that aren't in new graph)
|
||||||
|
for v := range oldGraph.Adjacency() {
|
||||||
|
if !VertexContains(v, vertexKeep) {
|
||||||
|
if err := vertexRemoveFn(v); err != nil {
|
||||||
|
return errwrap.Wrapf(err, "vertexRemoveFn failed")
|
||||||
|
}
|
||||||
|
oldGraph.DeleteVertex(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// compare edges
|
||||||
|
for v1 := range newGraph.Adjacency() { // loop through the vertices (resources)
|
||||||
|
for v2, e := range newGraph.Adjacency()[v1] {
|
||||||
|
// we have an edge!
|
||||||
|
// lookup vertices (these should exist now)
|
||||||
|
vertex1, exists1 := lookup[v1]
|
||||||
|
vertex2, exists2 := lookup[v2]
|
||||||
|
if !exists1 || !exists2 { // no match found, bug?
|
||||||
|
//if vertex1 == nil || vertex2 == nil { // no match found
|
||||||
|
return fmt.Errorf("new vertices weren't found") // programming error
|
||||||
|
}
|
||||||
|
|
||||||
|
edge, exists := oldGraph.Adjacency()[vertex1][vertex2]
|
||||||
|
if !exists {
|
||||||
|
edge = e // use edge
|
||||||
|
} else if b, err := edgeCmpFn(edge, e); err != nil {
|
||||||
|
return errwrap.Wrapf(err, "edgeCmpFn failed")
|
||||||
|
} else if !b {
|
||||||
|
edge = e // overwrite edge
|
||||||
|
}
|
||||||
|
|
||||||
|
oldGraph.Adjacency()[vertex1][vertex2] = edge // store it (AddEdge)
|
||||||
|
edgeKeep = append(edgeKeep, edge) // mark as saved
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete unused edges
|
||||||
|
for v1 := range oldGraph.Adjacency() {
|
||||||
|
for _, e := range oldGraph.Adjacency()[v1] {
|
||||||
|
// we have an edge!
|
||||||
|
if !EdgeContains(e, edgeKeep) {
|
||||||
|
oldGraph.DeleteEdge(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// success
|
||||||
|
*obj = *oldGraph // save old graph
|
||||||
|
return nil
|
||||||
|
}
|
||||||
92
pgraph/graphsync_test.go
Normal file
92
pgraph/graphsync_test.go
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
// Mgmt
|
||||||
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
|
//
|
||||||
|
// 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package pgraph
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGraphSync1(t *testing.T) {
|
||||||
|
g := &Graph{}
|
||||||
|
v1 := NV("v1")
|
||||||
|
v2 := NV("v2")
|
||||||
|
v3 := NV("v3")
|
||||||
|
|
||||||
|
e1 := NE("e1")
|
||||||
|
e2 := NE("e2")
|
||||||
|
g.AddEdge(v1, v3, e1)
|
||||||
|
g.AddEdge(v2, v3, e2)
|
||||||
|
|
||||||
|
// new graph
|
||||||
|
newGraph := &Graph{}
|
||||||
|
v4 := NV("v4")
|
||||||
|
v5 := NV("v5")
|
||||||
|
e3 := NE("e3")
|
||||||
|
newGraph.AddEdge(v4, v5, e3)
|
||||||
|
|
||||||
|
err := g.GraphSync(newGraph, nil, nil, nil, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("GraphSync failed: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// g should change and become the same
|
||||||
|
if s := runGraphCmp(t, g, newGraph); s != "" {
|
||||||
|
t.Errorf("%s", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGraphSync2(t *testing.T) {
|
||||||
|
v1 := NV("v1")
|
||||||
|
v2 := NV("v2")
|
||||||
|
v3 := NV("v3")
|
||||||
|
v4 := NV("v4")
|
||||||
|
v5 := NV("v5")
|
||||||
|
e1 := NE("e1")
|
||||||
|
e2 := NE("e2")
|
||||||
|
e3 := NE("e3")
|
||||||
|
|
||||||
|
g := &Graph{}
|
||||||
|
g.AddEdge(v1, v3, e1)
|
||||||
|
g.AddEdge(v2, v3, e2)
|
||||||
|
|
||||||
|
// new graph
|
||||||
|
newGraph := &Graph{}
|
||||||
|
newGraph.AddEdge(v1, v3, e1)
|
||||||
|
newGraph.AddEdge(v2, v3, e2)
|
||||||
|
newGraph.AddEdge(v4, v5, e3)
|
||||||
|
//newGraph.AddEdge(v3, v4, NE("v3,v4"))
|
||||||
|
//newGraph.AddEdge(v3, v5, NE("v3,v5"))
|
||||||
|
|
||||||
|
// graphs should differ!
|
||||||
|
if runGraphCmp(t, g, newGraph) == "" {
|
||||||
|
t.Errorf("graphs should differ initially")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err := g.GraphSync(newGraph, strVertexCmpFn, vertexAddFn, vertexRemoveFn, strEdgeCmpFn)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("GraphSync failed: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// g should change and become the same
|
||||||
|
if s := runGraphCmp(t, g, newGraph); s != "" {
|
||||||
|
t.Errorf("%s", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,21 +1,21 @@
|
|||||||
// Mgmt
|
// Mgmt
|
||||||
// Copyright (C) 2013-2016+ James Shubin and the project contributors
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// GNU Affero General Public License for more details.
|
// GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package pgraph
|
package pgraph // TODO: this should be a subpackage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -45,12 +45,16 @@ func (g *Graph) Graphviz() (out string) {
|
|||||||
out += fmt.Sprintf("\tlabel=\"%s\";\n", g.GetName())
|
out += fmt.Sprintf("\tlabel=\"%s\";\n", g.GetName())
|
||||||
//out += "\tnode [shape=box];\n"
|
//out += "\tnode [shape=box];\n"
|
||||||
str := ""
|
str := ""
|
||||||
for i := range g.Adjacency { // reverse paths
|
for i := range g.Adjacency() { // reverse paths
|
||||||
out += fmt.Sprintf("\t%s [label=\"%s[%s]\"];\n", i.GetName(), i.Kind(), i.GetName())
|
out += fmt.Sprintf("\t\"%s\" [label=\"%s\"];\n", i, i)
|
||||||
for j := range g.Adjacency[i] {
|
for j := range g.Adjacency()[i] {
|
||||||
k := g.Adjacency[i][j]
|
k := g.Adjacency()[i][j]
|
||||||
// use str for clearer output ordering
|
// use str for clearer output ordering
|
||||||
str += fmt.Sprintf("\t%s -> %s [label=%s];\n", i.GetName(), j.GetName(), k.Name)
|
//if fmtBoldFn(k) { // TODO: add this sort of formatting
|
||||||
|
// str += fmt.Sprintf("\t\"%s\" -> \"%s\" [label=\"%s\",style=bold];\n", i, j, k)
|
||||||
|
//} else {
|
||||||
|
str += fmt.Sprintf("\t\"%s\" -> \"%s\" [label=\"%s\"];\n", i, j, k)
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out += str
|
out += str
|
||||||
@@ -60,16 +64,20 @@ func (g *Graph) Graphviz() (out string) {
|
|||||||
|
|
||||||
// ExecGraphviz writes out the graphviz data and runs the correct graphviz
|
// ExecGraphviz writes out the graphviz data and runs the correct graphviz
|
||||||
// filter command.
|
// filter command.
|
||||||
func (g *Graph) ExecGraphviz(program, filename string) error {
|
func (g *Graph) ExecGraphviz(program, filename, hostname string) error {
|
||||||
|
|
||||||
switch program {
|
switch program {
|
||||||
case "dot", "neato", "twopi", "circo", "fdp":
|
case "dot", "neato", "twopi", "circo", "fdp":
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("Invalid graphviz program selected!")
|
return fmt.Errorf("invalid graphviz program selected")
|
||||||
}
|
}
|
||||||
|
|
||||||
if filename == "" {
|
if filename == "" {
|
||||||
return fmt.Errorf("No filename given!")
|
return fmt.Errorf("no filename given")
|
||||||
|
}
|
||||||
|
|
||||||
|
if hostname != "" {
|
||||||
|
filename = fmt.Sprintf("%s@%s", filename, hostname)
|
||||||
}
|
}
|
||||||
|
|
||||||
// run as a normal user if possible when run with sudo
|
// run as a normal user if possible when run with sudo
|
||||||
@@ -78,18 +86,18 @@ func (g *Graph) ExecGraphviz(program, filename string) error {
|
|||||||
|
|
||||||
err := ioutil.WriteFile(filename, []byte(g.Graphviz()), 0644)
|
err := ioutil.WriteFile(filename, []byte(g.Graphviz()), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error writing to filename!")
|
return fmt.Errorf("error writing to filename")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err1 == nil && err2 == nil {
|
if err1 == nil && err2 == nil {
|
||||||
if err := os.Chown(filename, uid, gid); err != nil {
|
if err := os.Chown(filename, uid, gid); err != nil {
|
||||||
return fmt.Errorf("Error changing file owner!")
|
return fmt.Errorf("error changing file owner")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
path, err := exec.LookPath(program)
|
path, err := exec.LookPath(program)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Graphviz is missing!")
|
return fmt.Errorf("the Graphviz program is missing")
|
||||||
}
|
}
|
||||||
|
|
||||||
out := fmt.Sprintf("%s.png", filename)
|
out := fmt.Sprintf("%s.png", filename)
|
||||||
@@ -104,7 +112,7 @@ func (g *Graph) ExecGraphviz(program, filename string) error {
|
|||||||
}
|
}
|
||||||
_, err = cmd.Output()
|
_, err = cmd.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error writing to image!")
|
return fmt.Errorf("error writing to image")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
536
pgraph/pgraph.go
536
pgraph/pgraph.go
@@ -1,18 +1,18 @@
|
|||||||
// Mgmt
|
// Mgmt
|
||||||
// Copyright (C) 2013-2016+ James Shubin and the project contributors
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// GNU Affero General Public License for more details.
|
// GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
// Package pgraph represents the internal "pointer graph" that we use.
|
// Package pgraph represents the internal "pointer graph" that we use.
|
||||||
@@ -21,30 +21,10 @@ package pgraph
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/purpleidea/mgmt/event"
|
|
||||||
"github.com/purpleidea/mgmt/resources"
|
|
||||||
|
|
||||||
errwrap "github.com/pkg/errors"
|
errwrap "github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:generate stringer -type=graphState -output=graphstate_stringer.go
|
|
||||||
type graphState int
|
|
||||||
|
|
||||||
const (
|
|
||||||
graphStateNil graphState = iota
|
|
||||||
graphStateStarting
|
|
||||||
graphStateStarted
|
|
||||||
graphStatePausing
|
|
||||||
graphStatePaused
|
|
||||||
)
|
|
||||||
|
|
||||||
// Flags contains specific constants used by the graph.
|
|
||||||
type Flags struct {
|
|
||||||
Debug bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// Graph is the graph structure in this library.
|
// Graph is the graph structure in this library.
|
||||||
// The graph abstract data type (ADT) is defined as follows:
|
// The graph abstract data type (ADT) is defined as follows:
|
||||||
// * the directed graph arrows point from left to right ( -> )
|
// * the directed graph arrows point from left to right ( -> )
|
||||||
@@ -53,75 +33,71 @@ type Flags struct {
|
|||||||
// * This is also the direction that the notify should happen in...
|
// * This is also the direction that the notify should happen in...
|
||||||
type Graph struct {
|
type Graph struct {
|
||||||
Name string
|
Name string
|
||||||
Adjacency map[*Vertex]map[*Vertex]*Edge // *Vertex -> *Vertex (edge)
|
|
||||||
Flags Flags
|
adjacency map[Vertex]map[Vertex]Edge // Vertex -> Vertex (edge)
|
||||||
state graphState
|
kv map[string]interface{} // some values associated with the graph
|
||||||
mutex *sync.Mutex // used when modifying graph State variable
|
|
||||||
wg *sync.WaitGroup
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Vertex is the primary vertex struct in this library.
|
// Vertex is the primary vertex struct in this library. It can be anything that
|
||||||
type Vertex struct {
|
// implements Stringer. The string output must be stable and unique in a graph.
|
||||||
resources.Res // anonymous field
|
type Vertex interface {
|
||||||
timestamp int64 // last updated timestamp ?
|
fmt.Stringer // String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Edge is the primary edge struct in this library.
|
// Edge is the primary edge struct in this library. It can be anything that
|
||||||
type Edge struct {
|
// implements Stringer. The string output must be stable and unique in a graph.
|
||||||
Name string
|
type Edge interface {
|
||||||
Notify bool // should we send a refresh notification along this edge?
|
fmt.Stringer // String() string
|
||||||
|
}
|
||||||
|
|
||||||
refresh bool // is there a notify pending for the dest vertex ?
|
// Init initializes the graph which populates all the internal structures.
|
||||||
|
func (g *Graph) Init() error {
|
||||||
|
if g.Name == "" { // FIXME: is this really a good requirement?
|
||||||
|
return fmt.Errorf("can't initialize graph with empty name")
|
||||||
|
}
|
||||||
|
|
||||||
|
//g.adjacency = make(map[Vertex]map[Vertex]Edge) // not required
|
||||||
|
//g.kv = make(map[string]interface{}) // not required
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGraph builds a new graph.
|
// NewGraph builds a new graph.
|
||||||
func NewGraph(name string) *Graph {
|
func NewGraph(name string) (*Graph, error) {
|
||||||
return &Graph{
|
g := &Graph{
|
||||||
Name: name,
|
|
||||||
Adjacency: make(map[*Vertex]map[*Vertex]*Edge),
|
|
||||||
state: graphStateNil,
|
|
||||||
// ptr b/c: Mutex/WaitGroup must not be copied after first use
|
|
||||||
mutex: &sync.Mutex{},
|
|
||||||
wg: &sync.WaitGroup{},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewVertex returns a new graph vertex struct with a contained resource.
|
|
||||||
func NewVertex(r resources.Res) *Vertex {
|
|
||||||
return &Vertex{
|
|
||||||
Res: r,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewEdge returns a new graph edge struct.
|
|
||||||
func NewEdge(name string) *Edge {
|
|
||||||
return &Edge{
|
|
||||||
Name: name,
|
Name: name,
|
||||||
}
|
}
|
||||||
|
if err := g.Init(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return g, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Refresh returns the pending refresh status of this edge.
|
// Value returns a value stored alongside the graph in a particular key.
|
||||||
func (obj *Edge) Refresh() bool {
|
func (g *Graph) Value(key string) (interface{}, bool) {
|
||||||
return obj.refresh
|
val, exists := g.kv[key]
|
||||||
|
return val, exists
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetRefresh sets the pending refresh status of this edge.
|
// SetValue sets a value to be stored alongside the graph in a particular key.
|
||||||
func (obj *Edge) SetRefresh(b bool) {
|
func (g *Graph) SetValue(key string, val interface{}) {
|
||||||
obj.refresh = b
|
if g.kv == nil { // initialize on first use
|
||||||
|
g.kv = make(map[string]interface{})
|
||||||
|
}
|
||||||
|
g.kv[key] = val
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy makes a copy of the graph struct
|
// Copy makes a copy of the graph struct.
|
||||||
func (g *Graph) Copy() *Graph {
|
func (g *Graph) Copy() *Graph {
|
||||||
|
if g == nil { // allow nil graphs through
|
||||||
|
return g
|
||||||
|
}
|
||||||
newGraph := &Graph{
|
newGraph := &Graph{
|
||||||
Name: g.Name,
|
Name: g.Name,
|
||||||
Adjacency: make(map[*Vertex]map[*Vertex]*Edge, len(g.Adjacency)),
|
adjacency: make(map[Vertex]map[Vertex]Edge, len(g.adjacency)),
|
||||||
Flags: g.Flags,
|
kv: g.kv,
|
||||||
state: g.state,
|
|
||||||
mutex: g.mutex,
|
|
||||||
wg: g.wg,
|
|
||||||
}
|
}
|
||||||
for k, v := range g.Adjacency {
|
for k, v := range g.adjacency {
|
||||||
newGraph.Adjacency[k] = v // copy
|
newGraph.adjacency[k] = v // copy
|
||||||
}
|
}
|
||||||
return newGraph
|
return newGraph
|
||||||
}
|
}
|
||||||
@@ -136,76 +112,49 @@ func (g *Graph) SetName(name string) {
|
|||||||
g.Name = name
|
g.Name = name
|
||||||
}
|
}
|
||||||
|
|
||||||
// getState returns the state of the graph. This state is used for optimizing
|
// AddVertex uses variadic input to add all listed vertices to the graph.
|
||||||
// certain algorithms by knowing what part of processing the graph is currently
|
func (g *Graph) AddVertex(xv ...Vertex) {
|
||||||
// undergoing.
|
if g.adjacency == nil { // initialize on first use
|
||||||
func (g *Graph) getState() graphState {
|
g.adjacency = make(map[Vertex]map[Vertex]Edge)
|
||||||
//g.mutex.Lock()
|
|
||||||
//defer g.mutex.Unlock()
|
|
||||||
return g.state
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// setState sets the graph state and returns the previous state.
|
|
||||||
func (g *Graph) setState(state graphState) graphState {
|
|
||||||
g.mutex.Lock()
|
|
||||||
defer g.mutex.Unlock()
|
|
||||||
prev := g.getState()
|
|
||||||
g.state = state
|
|
||||||
return prev
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddVertex uses variadic input to add all listed vertices to the graph
|
|
||||||
func (g *Graph) AddVertex(xv ...*Vertex) {
|
|
||||||
for _, v := range xv {
|
for _, v := range xv {
|
||||||
if _, exists := g.Adjacency[v]; !exists {
|
if _, exists := g.adjacency[v]; !exists {
|
||||||
g.Adjacency[v] = make(map[*Vertex]*Edge)
|
g.adjacency[v] = make(map[Vertex]Edge)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteVertex deletes a particular vertex from the graph.
|
// DeleteVertex deletes a particular vertex from the graph.
|
||||||
func (g *Graph) DeleteVertex(v *Vertex) {
|
func (g *Graph) DeleteVertex(v Vertex) {
|
||||||
delete(g.Adjacency, v)
|
delete(g.adjacency, v)
|
||||||
for k := range g.Adjacency {
|
for k := range g.adjacency {
|
||||||
delete(g.Adjacency[k], v)
|
delete(g.adjacency[k], v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddEdge adds a directed edge to the graph from v1 to v2.
|
// AddEdge adds a directed edge to the graph from v1 to v2.
|
||||||
func (g *Graph) AddEdge(v1, v2 *Vertex, e *Edge) {
|
func (g *Graph) AddEdge(v1, v2 Vertex, e Edge) {
|
||||||
// NOTE: this doesn't allow more than one edge between two vertexes...
|
// NOTE: this doesn't allow more than one edge between two vertexes...
|
||||||
g.AddVertex(v1, v2) // supports adding N vertices now
|
g.AddVertex(v1, v2) // supports adding N vertices now
|
||||||
// TODO: check if an edge exists to avoid overwriting it!
|
// TODO: check if an edge exists to avoid overwriting it!
|
||||||
// NOTE: VertexMerge() depends on overwriting it at the moment...
|
// NOTE: VertexMerge() depends on overwriting it at the moment...
|
||||||
g.Adjacency[v1][v2] = e
|
g.adjacency[v1][v2] = e
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteEdge deletes a particular edge from the graph.
|
// DeleteEdge deletes a particular edge from the graph.
|
||||||
// FIXME: add test cases
|
func (g *Graph) DeleteEdge(e Edge) {
|
||||||
func (g *Graph) DeleteEdge(e *Edge) {
|
for v1 := range g.adjacency {
|
||||||
for v1 := range g.Adjacency {
|
for v2, edge := range g.adjacency[v1] {
|
||||||
for v2, edge := range g.Adjacency[v1] {
|
|
||||||
if e == edge {
|
if e == edge {
|
||||||
delete(g.Adjacency[v1], v2)
|
delete(g.adjacency[v1], v2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetVertexMatch searches for an equivalent resource in the graph and returns
|
|
||||||
// the vertex it is found in, or nil if not found.
|
|
||||||
func (g *Graph) GetVertexMatch(obj resources.Res) *Vertex {
|
|
||||||
for k := range g.Adjacency {
|
|
||||||
if k.Res.Compare(obj) {
|
|
||||||
return k
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// HasVertex returns if the input vertex exists in the graph.
|
// HasVertex returns if the input vertex exists in the graph.
|
||||||
func (g *Graph) HasVertex(v *Vertex) bool {
|
func (g *Graph) HasVertex(v Vertex) bool {
|
||||||
if _, exists := g.Adjacency[v]; exists {
|
if _, exists := g.adjacency[v]; exists {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
@@ -213,33 +162,40 @@ func (g *Graph) HasVertex(v *Vertex) bool {
|
|||||||
|
|
||||||
// NumVertices returns the number of vertices in the graph.
|
// NumVertices returns the number of vertices in the graph.
|
||||||
func (g *Graph) NumVertices() int {
|
func (g *Graph) NumVertices() int {
|
||||||
return len(g.Adjacency)
|
return len(g.adjacency)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NumEdges returns the number of edges in the graph.
|
// NumEdges returns the number of edges in the graph.
|
||||||
func (g *Graph) NumEdges() int {
|
func (g *Graph) NumEdges() int {
|
||||||
count := 0
|
count := 0
|
||||||
for k := range g.Adjacency {
|
for k := range g.adjacency {
|
||||||
count += len(g.Adjacency[k])
|
count += len(g.adjacency[k])
|
||||||
}
|
}
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetVertices returns a randomly sorted slice of all vertices in the graph
|
// Adjacency returns the adjacency map representing this graph. This is useful
|
||||||
|
// for users who which to operate on the raw data structure more efficiently.
|
||||||
|
// This works because maps are reference types so we can edit this at will.
|
||||||
|
func (g *Graph) Adjacency() map[Vertex]map[Vertex]Edge {
|
||||||
|
return g.adjacency
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vertices returns a randomly sorted slice of all vertices in the graph.
|
||||||
// The order is random, because the map implementation is intentionally so!
|
// The order is random, because the map implementation is intentionally so!
|
||||||
func (g *Graph) GetVertices() []*Vertex {
|
func (g *Graph) Vertices() []Vertex {
|
||||||
var vertices []*Vertex
|
var vertices []Vertex
|
||||||
for k := range g.Adjacency {
|
for k := range g.adjacency {
|
||||||
vertices = append(vertices, k)
|
vertices = append(vertices, k)
|
||||||
}
|
}
|
||||||
return vertices
|
return vertices
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetVerticesChan returns a channel of all vertices in the graph.
|
// VerticesChan returns a channel of all vertices in the graph.
|
||||||
func (g *Graph) GetVerticesChan() chan *Vertex {
|
func (g *Graph) VerticesChan() chan Vertex {
|
||||||
ch := make(chan *Vertex)
|
ch := make(chan Vertex)
|
||||||
go func(ch chan *Vertex) {
|
go func(ch chan Vertex) {
|
||||||
for k := range g.Adjacency {
|
for k := range g.adjacency {
|
||||||
ch <- k
|
ch <- k
|
||||||
}
|
}
|
||||||
close(ch)
|
close(ch)
|
||||||
@@ -248,17 +204,17 @@ func (g *Graph) GetVerticesChan() chan *Vertex {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// VertexSlice is a linear list of vertices. It can be sorted.
|
// VertexSlice is a linear list of vertices. It can be sorted.
|
||||||
type VertexSlice []*Vertex
|
type VertexSlice []Vertex
|
||||||
|
|
||||||
func (vs VertexSlice) Len() int { return len(vs) }
|
func (vs VertexSlice) Len() int { return len(vs) }
|
||||||
func (vs VertexSlice) Swap(i, j int) { vs[i], vs[j] = vs[j], vs[i] }
|
func (vs VertexSlice) Swap(i, j int) { vs[i], vs[j] = vs[j], vs[i] }
|
||||||
func (vs VertexSlice) Less(i, j int) bool { return vs[i].String() < vs[j].String() }
|
func (vs VertexSlice) Less(i, j int) bool { return vs[i].String() < vs[j].String() }
|
||||||
|
|
||||||
// GetVerticesSorted returns a sorted slice of all vertices in the graph
|
// VerticesSorted returns a sorted slice of all vertices in the graph.
|
||||||
// The order is sorted by String() to avoid the non-determinism in the map type
|
// The order is sorted by String() to avoid the non-determinism in the map type.
|
||||||
func (g *Graph) GetVerticesSorted() []*Vertex {
|
func (g *Graph) VerticesSorted() []Vertex {
|
||||||
var vertices []*Vertex
|
var vertices []Vertex
|
||||||
for k := range g.Adjacency {
|
for k := range g.adjacency {
|
||||||
vertices = append(vertices, k)
|
vertices = append(vertices, k)
|
||||||
}
|
}
|
||||||
sort.Sort(VertexSlice(vertices)) // add determinism
|
sort.Sort(VertexSlice(vertices)) // add determinism
|
||||||
@@ -270,19 +226,14 @@ func (g *Graph) String() string {
|
|||||||
return fmt.Sprintf("Vertices(%d), Edges(%d)", g.NumVertices(), g.NumEdges())
|
return fmt.Sprintf("Vertices(%d), Edges(%d)", g.NumVertices(), g.NumEdges())
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns the canonical form for a vertex
|
|
||||||
func (v *Vertex) String() string {
|
|
||||||
return fmt.Sprintf("%s[%s]", v.Res.Kind(), v.Res.GetName())
|
|
||||||
}
|
|
||||||
|
|
||||||
// IncomingGraphVertices returns an array (slice) of all directed vertices to
|
// IncomingGraphVertices returns an array (slice) of all directed vertices to
|
||||||
// vertex v (??? -> v). OKTimestamp should probably use this.
|
// vertex v (??? -> v). OKTimestamp should probably use this.
|
||||||
func (g *Graph) IncomingGraphVertices(v *Vertex) []*Vertex {
|
func (g *Graph) IncomingGraphVertices(v Vertex) []Vertex {
|
||||||
// TODO: we might be able to implement this differently by reversing
|
// TODO: we might be able to implement this differently by reversing
|
||||||
// the Adjacency graph and then looping through it again...
|
// the Adjacency graph and then looping through it again...
|
||||||
var s []*Vertex
|
var s []Vertex
|
||||||
for k := range g.Adjacency { // reverse paths
|
for k := range g.adjacency { // reverse paths
|
||||||
for w := range g.Adjacency[k] {
|
for w := range g.adjacency[k] {
|
||||||
if w == v {
|
if w == v {
|
||||||
s = append(s, k)
|
s = append(s, k)
|
||||||
}
|
}
|
||||||
@@ -293,9 +244,9 @@ func (g *Graph) IncomingGraphVertices(v *Vertex) []*Vertex {
|
|||||||
|
|
||||||
// OutgoingGraphVertices returns an array (slice) of all vertices that vertex v
|
// OutgoingGraphVertices returns an array (slice) of all vertices that vertex v
|
||||||
// points to (v -> ???). Poke should probably use this.
|
// points to (v -> ???). Poke should probably use this.
|
||||||
func (g *Graph) OutgoingGraphVertices(v *Vertex) []*Vertex {
|
func (g *Graph) OutgoingGraphVertices(v Vertex) []Vertex {
|
||||||
var s []*Vertex
|
var s []Vertex
|
||||||
for k := range g.Adjacency[v] { // forward paths
|
for k := range g.adjacency[v] { // forward paths
|
||||||
s = append(s, k)
|
s = append(s, k)
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
@@ -303,18 +254,18 @@ func (g *Graph) OutgoingGraphVertices(v *Vertex) []*Vertex {
|
|||||||
|
|
||||||
// GraphVertices returns an array (slice) of all vertices that connect to vertex v.
|
// GraphVertices returns an array (slice) of all vertices that connect to vertex v.
|
||||||
// This is the union of IncomingGraphVertices and OutgoingGraphVertices.
|
// This is the union of IncomingGraphVertices and OutgoingGraphVertices.
|
||||||
func (g *Graph) GraphVertices(v *Vertex) []*Vertex {
|
func (g *Graph) GraphVertices(v Vertex) []Vertex {
|
||||||
var s []*Vertex
|
var s []Vertex
|
||||||
s = append(s, g.IncomingGraphVertices(v)...)
|
s = append(s, g.IncomingGraphVertices(v)...)
|
||||||
s = append(s, g.OutgoingGraphVertices(v)...)
|
s = append(s, g.OutgoingGraphVertices(v)...)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// IncomingGraphEdges returns all of the edges that point to vertex v (??? -> v).
|
// IncomingGraphEdges returns all of the edges that point to vertex v (??? -> v).
|
||||||
func (g *Graph) IncomingGraphEdges(v *Vertex) []*Edge {
|
func (g *Graph) IncomingGraphEdges(v Vertex) []Edge {
|
||||||
var edges []*Edge
|
var edges []Edge
|
||||||
for v1 := range g.Adjacency { // reverse paths
|
for v1 := range g.adjacency { // reverse paths
|
||||||
for v2, e := range g.Adjacency[v1] {
|
for v2, e := range g.adjacency[v1] {
|
||||||
if v2 == v {
|
if v2 == v {
|
||||||
edges = append(edges, e)
|
edges = append(edges, e)
|
||||||
}
|
}
|
||||||
@@ -324,9 +275,9 @@ func (g *Graph) IncomingGraphEdges(v *Vertex) []*Edge {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// OutgoingGraphEdges returns all of the edges that point from vertex v (v -> ???).
|
// OutgoingGraphEdges returns all of the edges that point from vertex v (v -> ???).
|
||||||
func (g *Graph) OutgoingGraphEdges(v *Vertex) []*Edge {
|
func (g *Graph) OutgoingGraphEdges(v Vertex) []Edge {
|
||||||
var edges []*Edge
|
var edges []Edge
|
||||||
for _, e := range g.Adjacency[v] { // forward paths
|
for _, e := range g.adjacency[v] { // forward paths
|
||||||
edges = append(edges, e)
|
edges = append(edges, e)
|
||||||
}
|
}
|
||||||
return edges
|
return edges
|
||||||
@@ -334,18 +285,18 @@ func (g *Graph) OutgoingGraphEdges(v *Vertex) []*Edge {
|
|||||||
|
|
||||||
// GraphEdges returns an array (slice) of all edges that connect to vertex v.
|
// GraphEdges returns an array (slice) of all edges that connect to vertex v.
|
||||||
// This is the union of IncomingGraphEdges and OutgoingGraphEdges.
|
// This is the union of IncomingGraphEdges and OutgoingGraphEdges.
|
||||||
func (g *Graph) GraphEdges(v *Vertex) []*Edge {
|
func (g *Graph) GraphEdges(v Vertex) []Edge {
|
||||||
var edges []*Edge
|
var edges []Edge
|
||||||
edges = append(edges, g.IncomingGraphEdges(v)...)
|
edges = append(edges, g.IncomingGraphEdges(v)...)
|
||||||
edges = append(edges, g.OutgoingGraphEdges(v)...)
|
edges = append(edges, g.OutgoingGraphEdges(v)...)
|
||||||
return edges
|
return edges
|
||||||
}
|
}
|
||||||
|
|
||||||
// DFS returns a depth first search for the graph, starting at the input vertex.
|
// DFS returns a depth first search for the graph, starting at the input vertex.
|
||||||
func (g *Graph) DFS(start *Vertex) []*Vertex {
|
func (g *Graph) DFS(start Vertex) []Vertex {
|
||||||
var d []*Vertex // discovered
|
var d []Vertex // discovered
|
||||||
var s []*Vertex // stack
|
var s []Vertex // stack
|
||||||
if _, exists := g.Adjacency[start]; !exists {
|
if _, exists := g.adjacency[start]; !exists {
|
||||||
return nil // TODO: error
|
return nil // TODO: error
|
||||||
}
|
}
|
||||||
v := start
|
v := start
|
||||||
@@ -365,31 +316,32 @@ func (g *Graph) DFS(start *Vertex) []*Vertex {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FilterGraph builds a new graph containing only vertices from the list.
|
// FilterGraph builds a new graph containing only vertices from the list.
|
||||||
func (g *Graph) FilterGraph(name string, vertices []*Vertex) *Graph {
|
func (g *Graph) FilterGraph(name string, vertices []Vertex) (*Graph, error) {
|
||||||
newgraph := NewGraph(name)
|
newGraph := &Graph{Name: name}
|
||||||
for k1, x := range g.Adjacency {
|
if err := newGraph.Init(); err != nil {
|
||||||
|
return nil, errwrap.Wrapf(err, "could not run FilterGraph() properly")
|
||||||
|
}
|
||||||
|
for k1, x := range g.adjacency {
|
||||||
for k2, e := range x {
|
for k2, e := range x {
|
||||||
//log.Printf("Filter: %s -> %s # %s", k1.Name, k2.Name, e.Name)
|
//log.Printf("Filter: %s -> %s # %s", k1.Name, k2.Name, e.Name)
|
||||||
if VertexContains(k1, vertices) || VertexContains(k2, vertices) {
|
if VertexContains(k1, vertices) || VertexContains(k2, vertices) {
|
||||||
newgraph.AddEdge(k1, k2, e)
|
newGraph.AddEdge(k1, k2, e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return newgraph
|
return newGraph, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDisconnectedGraphs returns a channel containing the N disconnected graphs
|
// DisconnectedGraphs returns a list containing the N disconnected graphs.
|
||||||
// in our main graph. We can then process each of these in parallel.
|
func (g *Graph) DisconnectedGraphs() ([]*Graph, error) {
|
||||||
func (g *Graph) GetDisconnectedGraphs() chan *Graph {
|
graphs := []*Graph{}
|
||||||
ch := make(chan *Graph)
|
var start Vertex
|
||||||
go func() {
|
var d []Vertex // discovered
|
||||||
var start *Vertex
|
|
||||||
var d []*Vertex // discovered
|
|
||||||
c := g.NumVertices()
|
c := g.NumVertices()
|
||||||
for len(d) < c {
|
for len(d) < c {
|
||||||
|
|
||||||
// get an undiscovered vertex to start from
|
// get an undiscovered vertex to start from
|
||||||
for _, s := range g.GetVertices() {
|
for _, s := range g.Vertices() {
|
||||||
if !VertexContains(s, d) {
|
if !VertexContains(s, d) {
|
||||||
start = s
|
start = s
|
||||||
}
|
}
|
||||||
@@ -398,31 +350,31 @@ func (g *Graph) GetDisconnectedGraphs() chan *Graph {
|
|||||||
// dfs through the graph
|
// dfs through the graph
|
||||||
dfs := g.DFS(start)
|
dfs := g.DFS(start)
|
||||||
// filter all the collected elements into a new graph
|
// filter all the collected elements into a new graph
|
||||||
newgraph := g.FilterGraph(g.Name, dfs)
|
newgraph, err := g.FilterGraph(g.Name, dfs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errwrap.Wrapf(err, "could not run DisconnectedGraphs() properly")
|
||||||
|
}
|
||||||
// add number of elements found to found variable
|
// add number of elements found to found variable
|
||||||
d = append(d, dfs...) // extend
|
d = append(d, dfs...) // extend
|
||||||
|
|
||||||
// return this new graph to the channel
|
// append this new graph to the list
|
||||||
ch <- newgraph
|
graphs = append(graphs, newgraph)
|
||||||
|
|
||||||
// if we've found all the elements, then we're done
|
// if we've found all the elements, then we're done
|
||||||
// otherwise loop through to continue...
|
// otherwise loop through to continue...
|
||||||
}
|
}
|
||||||
close(ch)
|
return graphs, nil
|
||||||
}()
|
|
||||||
return ch
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// InDegree returns the count of vertices that point to me in one big lookup map.
|
// InDegree returns the count of vertices that point to me in one big lookup map.
|
||||||
func (g *Graph) InDegree() map[*Vertex]int {
|
func (g *Graph) InDegree() map[Vertex]int {
|
||||||
result := make(map[*Vertex]int)
|
result := make(map[Vertex]int)
|
||||||
for k := range g.Adjacency {
|
for k := range g.adjacency {
|
||||||
result[k] = 0 // initialize
|
result[k] = 0 // initialize
|
||||||
}
|
}
|
||||||
|
|
||||||
for k := range g.Adjacency {
|
for k := range g.adjacency {
|
||||||
for z := range g.Adjacency[k] {
|
for z := range g.adjacency[k] {
|
||||||
result[z]++
|
result[z]++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -430,12 +382,12 @@ func (g *Graph) InDegree() map[*Vertex]int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// OutDegree returns the count of vertices that point away in one big lookup map.
|
// OutDegree returns the count of vertices that point away in one big lookup map.
|
||||||
func (g *Graph) OutDegree() map[*Vertex]int {
|
func (g *Graph) OutDegree() map[Vertex]int {
|
||||||
result := make(map[*Vertex]int)
|
result := make(map[Vertex]int)
|
||||||
|
|
||||||
for k := range g.Adjacency {
|
for k := range g.adjacency {
|
||||||
result[k] = 0 // initialize
|
result[k] = 0 // initialize
|
||||||
for range g.Adjacency[k] {
|
for range g.adjacency[k] {
|
||||||
result[k]++
|
result[k]++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -443,12 +395,12 @@ func (g *Graph) OutDegree() map[*Vertex]int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TopologicalSort returns the sort of graph vertices in that order.
|
// TopologicalSort returns the sort of graph vertices in that order.
|
||||||
// based on descriptions and code from wikipedia and rosetta code
|
// It is based on descriptions and code from wikipedia and rosetta code.
|
||||||
// TODO: add memoization, and cache invalidation to speed this up :)
|
// TODO: add memoization, and cache invalidation to speed this up :)
|
||||||
func (g *Graph) TopologicalSort() ([]*Vertex, error) { // kahn's algorithm
|
func (g *Graph) TopologicalSort() ([]Vertex, error) { // kahn's algorithm
|
||||||
var L []*Vertex // empty list that will contain the sorted elements
|
var L []Vertex // empty list that will contain the sorted elements
|
||||||
var S []*Vertex // set of all nodes with no incoming edges
|
var S []Vertex // set of all nodes with no incoming edges
|
||||||
remaining := make(map[*Vertex]int) // amount of edges remaining
|
remaining := make(map[Vertex]int) // amount of edges remaining
|
||||||
|
|
||||||
for v, d := range g.InDegree() {
|
for v, d := range g.InDegree() {
|
||||||
if d == 0 {
|
if d == 0 {
|
||||||
@@ -465,7 +417,7 @@ func (g *Graph) TopologicalSort() ([]*Vertex, error) { // kahn's algorithm
|
|||||||
v := S[last]
|
v := S[last]
|
||||||
S = S[:last]
|
S = S[:last]
|
||||||
L = append(L, v) // add v to tail of L
|
L = append(L, v) // add v to tail of L
|
||||||
for n := range g.Adjacency[v] {
|
for n := range g.adjacency[v] {
|
||||||
// for each node n remaining in the graph, consume from
|
// for each node n remaining in the graph, consume from
|
||||||
// remaining, so for remaining[n] > 0
|
// remaining, so for remaining[n] > 0
|
||||||
if remaining[n] > 0 {
|
if remaining[n] > 0 {
|
||||||
@@ -480,9 +432,9 @@ func (g *Graph) TopologicalSort() ([]*Vertex, error) { // kahn's algorithm
|
|||||||
// if graph has edges, eg if any value in rem is > 0
|
// if graph has edges, eg if any value in rem is > 0
|
||||||
for c, in := range remaining {
|
for c, in := range remaining {
|
||||||
if in > 0 {
|
if in > 0 {
|
||||||
for n := range g.Adjacency[c] {
|
for n := range g.adjacency[c] {
|
||||||
if remaining[n] > 0 {
|
if remaining[n] > 0 {
|
||||||
return nil, fmt.Errorf("Not a dag!")
|
return nil, fmt.Errorf("not a dag")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -499,19 +451,19 @@ func (g *Graph) TopologicalSort() ([]*Vertex, error) { // kahn's algorithm
|
|||||||
// actually return a tree if we cared about correctness.
|
// actually return a tree if we cared about correctness.
|
||||||
// This operates by a recursive algorithm; a more efficient version is likely.
|
// This operates by a recursive algorithm; a more efficient version is likely.
|
||||||
// If you don't give this function a DAG, you might cause infinite recursion!
|
// If you don't give this function a DAG, you might cause infinite recursion!
|
||||||
func (g *Graph) Reachability(a, b *Vertex) []*Vertex {
|
func (g *Graph) Reachability(a, b Vertex) []Vertex {
|
||||||
if a == nil || b == nil {
|
if a == nil || b == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
vertices := g.OutgoingGraphVertices(a) // what points away from a ?
|
vertices := g.OutgoingGraphVertices(a) // what points away from a ?
|
||||||
if len(vertices) == 0 {
|
if len(vertices) == 0 {
|
||||||
return []*Vertex{} // nope
|
return []Vertex{} // nope
|
||||||
}
|
}
|
||||||
if VertexContains(b, vertices) {
|
if VertexContains(b, vertices) {
|
||||||
return []*Vertex{a, b} // found
|
return []Vertex{a, b} // found
|
||||||
}
|
}
|
||||||
// TODO: parallelize this with go routines?
|
// TODO: parallelize this with go routines?
|
||||||
var collected = make([][]*Vertex, len(vertices))
|
var collected = make([][]Vertex, len(vertices))
|
||||||
pick := -1
|
pick := -1
|
||||||
for i, v := range vertices {
|
for i, v := range vertices {
|
||||||
collected[i] = g.Reachability(v, b) // find b by recursion
|
collected[i] = g.Reachability(v, b) // find b by recursion
|
||||||
@@ -524,110 +476,111 @@ func (g *Graph) Reachability(a, b *Vertex) []*Vertex {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if pick < 0 {
|
if pick < 0 {
|
||||||
return []*Vertex{} // nope
|
return []Vertex{} // nope
|
||||||
}
|
}
|
||||||
result := []*Vertex{a} // tack on a
|
result := []Vertex{a} // tack on a
|
||||||
result = append(result, collected[pick]...)
|
result = append(result, collected[pick]...)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// GraphSync updates the oldGraph so that it matches the newGraph receiver. It
|
// VertexMatchFn searches for a vertex in the graph and returns the vertex if
|
||||||
// leaves identical elements alone so that they don't need to be refreshed.
|
// one matches. It uses a user defined function to match. That function must
|
||||||
// FIXME: add test cases
|
// return true on match, and an error if anything goes wrong.
|
||||||
func (g *Graph) GraphSync(oldGraph *Graph) (*Graph, error) {
|
func (g *Graph) VertexMatchFn(fn func(Vertex) (bool, error)) (Vertex, error) {
|
||||||
|
for v := range g.adjacency {
|
||||||
if oldGraph == nil {
|
if b, err := fn(v); err != nil {
|
||||||
oldGraph = NewGraph(g.GetName()) // copy over the name
|
return nil, errwrap.Wrapf(err, "fn in VertexMatchFn() errored")
|
||||||
|
} else if b {
|
||||||
|
return v, nil
|
||||||
}
|
}
|
||||||
oldGraph.SetName(g.GetName()) // overwrite the name
|
|
||||||
|
|
||||||
var lookup = make(map[*Vertex]*Vertex)
|
|
||||||
var vertexKeep []*Vertex // list of vertices which are the same in new graph
|
|
||||||
var edgeKeep []*Edge // list of vertices which are the same in new graph
|
|
||||||
|
|
||||||
for v := range g.Adjacency { // loop through the vertices (resources)
|
|
||||||
res := v.Res // resource
|
|
||||||
|
|
||||||
vertex := oldGraph.GetVertexMatch(res)
|
|
||||||
if vertex == nil { // no match found
|
|
||||||
if err := res.Init(); err != nil {
|
|
||||||
return nil, errwrap.Wrapf(err, "could not Init() resource")
|
|
||||||
}
|
}
|
||||||
vertex = NewVertex(res)
|
return nil, nil // nothing found
|
||||||
oldGraph.AddVertex(vertex) // call standalone in case not part of an edge
|
|
||||||
}
|
|
||||||
lookup[v] = vertex // used for constructing edges
|
|
||||||
vertexKeep = append(vertexKeep, vertex) // append
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// get rid of any vertices we shouldn't keep (that aren't in new graph)
|
// GraphCmp compares the topology of this graph to another and returns nil if
|
||||||
for v := range oldGraph.Adjacency {
|
// they're equal. It uses a user defined function to compare topologically
|
||||||
if !VertexContains(v, vertexKeep) {
|
// equivalent vertices, and edges.
|
||||||
// wait for exit before starting new graph!
|
// FIXME: add more test cases
|
||||||
v.SendEvent(event.EventExit, true, false)
|
func (g *Graph) GraphCmp(graph *Graph, vertexCmpFn func(Vertex, Vertex) (bool, error), edgeCmpFn func(Edge, Edge) (bool, error)) error {
|
||||||
oldGraph.DeleteVertex(v)
|
n1, n2 := g.NumVertices(), graph.NumVertices()
|
||||||
|
if n1 != n2 {
|
||||||
|
return fmt.Errorf("base graph has %d vertices, while input graph has %d", n1, n2)
|
||||||
}
|
}
|
||||||
|
if e1, e2 := g.NumEdges(), graph.NumEdges(); e1 != e2 {
|
||||||
|
return fmt.Errorf("base graph has %d edges, while input graph has %d", e1, e2)
|
||||||
}
|
}
|
||||||
|
|
||||||
// compare edges
|
var m = make(map[Vertex]Vertex) // g to graph vertex correspondence
|
||||||
for v1 := range g.Adjacency { // loop through the vertices (resources)
|
Loop:
|
||||||
for v2, e := range g.Adjacency[v1] {
|
// check vertices
|
||||||
// we have an edge!
|
for v1 := range g.Adjacency() { // for each vertex in g
|
||||||
|
for v2 := range graph.Adjacency() { // does it match in graph ?
|
||||||
|
b, err := vertexCmpFn(v1, v2)
|
||||||
|
if err != nil {
|
||||||
|
return errwrap.Wrapf(err, "could not run vertexCmpFn() properly")
|
||||||
|
}
|
||||||
|
// does it match ?
|
||||||
|
if b {
|
||||||
|
m[v1] = v2 // store the mapping
|
||||||
|
continue Loop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Errorf("base graph, has no match in input graph for: %s", v1)
|
||||||
|
}
|
||||||
|
// vertices match :)
|
||||||
|
|
||||||
// lookup vertices (these should exist now)
|
// is the mapping the right length?
|
||||||
//res1 := v1.Res // resource
|
if n1 := len(m); n1 != n2 {
|
||||||
//res2 := v2.Res
|
return fmt.Errorf("mapping only has correspondence of %d, when it should have %d", n1, n2)
|
||||||
//vertex1 := oldGraph.GetVertexMatch(res1)
|
|
||||||
//vertex2 := oldGraph.GetVertexMatch(res2)
|
|
||||||
vertex1, exists1 := lookup[v1]
|
|
||||||
vertex2, exists2 := lookup[v2]
|
|
||||||
if !exists1 || !exists2 { // no match found, bug?
|
|
||||||
//if vertex1 == nil || vertex2 == nil { // no match found
|
|
||||||
return nil, fmt.Errorf("New vertices weren't found!") // programming error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
edge, exists := oldGraph.Adjacency[vertex1][vertex2]
|
// check if mapping is unique (are there duplicates?)
|
||||||
if !exists || edge.Name != e.Name { // TODO: edgeCmp
|
m1 := []Vertex{}
|
||||||
edge = e // use or overwrite edge
|
m2 := []Vertex{}
|
||||||
|
for k, v := range m {
|
||||||
|
if VertexContains(k, m1) {
|
||||||
|
return fmt.Errorf("mapping from %s is used more than once to: %s", k, m1)
|
||||||
}
|
}
|
||||||
oldGraph.Adjacency[vertex1][vertex2] = edge // store it (AddEdge)
|
if VertexContains(v, m2) {
|
||||||
edgeKeep = append(edgeKeep, edge) // mark as saved
|
return fmt.Errorf("mapping to %s is used more than once from: %s", v, m2)
|
||||||
}
|
}
|
||||||
|
m1 = append(m1, k)
|
||||||
|
m2 = append(m2, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete unused edges
|
// check edges
|
||||||
for v1 := range oldGraph.Adjacency {
|
for v1 := range g.Adjacency() { // for each vertex in g
|
||||||
for _, e := range oldGraph.Adjacency[v1] {
|
v2 := m[v1] // lookup in map to get correspondance
|
||||||
// we have an edge!
|
// g.Adjacency()[v1] corresponds to graph.Adjacency()[v2]
|
||||||
if !EdgeContains(e, edgeKeep) {
|
if e1, e2 := len(g.Adjacency()[v1]), len(graph.Adjacency()[v2]); e1 != e2 {
|
||||||
oldGraph.DeleteEdge(e)
|
return fmt.Errorf("base graph, vertex(%s) has %d edges, while input graph, vertex(%s) has %d", v1, e1, v2, e2)
|
||||||
|
}
|
||||||
|
|
||||||
|
for vv1, ee1 := range g.Adjacency()[v1] {
|
||||||
|
vv2 := m[vv1]
|
||||||
|
ee2 := graph.Adjacency()[v2][vv2]
|
||||||
|
|
||||||
|
// these are edges from v1 -> vv1 via ee1 (graph 1)
|
||||||
|
// to cmp to edges from v2 -> vv2 via ee2 (graph 2)
|
||||||
|
|
||||||
|
// check: (1) vv1 == vv2 ? (we've already checked this!)
|
||||||
|
|
||||||
|
// check: (2) ee1 == ee2
|
||||||
|
b, err := edgeCmpFn(ee1, ee2)
|
||||||
|
if err != nil {
|
||||||
|
return errwrap.Wrapf(err, "could not run edgeCmpFn() properly")
|
||||||
|
}
|
||||||
|
if !b {
|
||||||
|
return fmt.Errorf("base graph edge(%s) doesn't match input graph edge(%s)", ee1, ee2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return oldGraph, nil
|
return nil // success!
|
||||||
}
|
|
||||||
|
|
||||||
// GraphMetas returns a list of pointers to each of the resource MetaParams.
|
|
||||||
func (g *Graph) GraphMetas() []*resources.MetaParams {
|
|
||||||
metas := []*resources.MetaParams{}
|
|
||||||
for v := range g.Adjacency { // loop through the vertices (resources))
|
|
||||||
res := v.Res // resource
|
|
||||||
meta := res.Meta()
|
|
||||||
metas = append(metas, meta)
|
|
||||||
}
|
|
||||||
return metas
|
|
||||||
}
|
|
||||||
|
|
||||||
// AssociateData associates some data with the object in the graph in question.
|
|
||||||
func (g *Graph) AssociateData(data *resources.Data) {
|
|
||||||
for k := range g.Adjacency {
|
|
||||||
k.Res.AssociateData(data)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// VertexContains is an "in array" function to test for a vertex in a slice of vertices.
|
// VertexContains is an "in array" function to test for a vertex in a slice of vertices.
|
||||||
func VertexContains(needle *Vertex, haystack []*Vertex) bool {
|
func VertexContains(needle Vertex, haystack []Vertex) bool {
|
||||||
for _, v := range haystack {
|
for _, v := range haystack {
|
||||||
if needle == v {
|
if needle == v {
|
||||||
return true
|
return true
|
||||||
@@ -637,7 +590,7 @@ func VertexContains(needle *Vertex, haystack []*Vertex) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// EdgeContains is an "in array" function to test for an edge in a slice of edges.
|
// EdgeContains is an "in array" function to test for an edge in a slice of edges.
|
||||||
func EdgeContains(needle *Edge, haystack []*Edge) bool {
|
func EdgeContains(needle Edge, haystack []Edge) bool {
|
||||||
for _, v := range haystack {
|
for _, v := range haystack {
|
||||||
if needle == v {
|
if needle == v {
|
||||||
return true
|
return true
|
||||||
@@ -647,12 +600,23 @@ func EdgeContains(needle *Edge, haystack []*Edge) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Reverse reverses a list of vertices.
|
// Reverse reverses a list of vertices.
|
||||||
func Reverse(vs []*Vertex) []*Vertex {
|
func Reverse(vs []Vertex) []Vertex {
|
||||||
//var out []*Vertex // XXX: golint suggests, but it fails testing
|
out := []Vertex{}
|
||||||
out := make([]*Vertex, 0) // empty list
|
|
||||||
l := len(vs)
|
l := len(vs)
|
||||||
for i := range vs {
|
for i := range vs {
|
||||||
out = append(out, vs[l-i-1])
|
out = append(out, vs[l-i-1])
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sort the list of vertices and return a copy without modifying the input.
|
||||||
|
func Sort(vs []Vertex) []Vertex {
|
||||||
|
vertices := []Vertex{}
|
||||||
|
for _, v := range vs { // copy
|
||||||
|
vertices = append(vertices, v)
|
||||||
|
}
|
||||||
|
sort.Sort(VertexSlice(vertices))
|
||||||
|
return vertices
|
||||||
|
// sort.Sort(VertexSlice(vs)) // this is wrong, it would modify input!
|
||||||
|
//return vs
|
||||||
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
106
pgraph/subgraph.go
Normal file
106
pgraph/subgraph.go
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
// Mgmt
|
||||||
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
|
//
|
||||||
|
// 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package pgraph
|
||||||
|
|
||||||
|
// AddGraph adds the set of edges and vertices of a graph to the existing graph.
|
||||||
|
func (g *Graph) AddGraph(graph *Graph) {
|
||||||
|
g.addEdgeVertexGraphHelper(nil, graph, nil, false, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddEdgeVertexGraph adds a directed edge to the graph from a vertex.
|
||||||
|
// This is useful for flattening the relationship between a subgraph and an
|
||||||
|
// existing graph, without having to run the subgraph recursively. It adds the
|
||||||
|
// maximum number of edges, creating a relationship to every vertex.
|
||||||
|
func (g *Graph) AddEdgeVertexGraph(vertex Vertex, graph *Graph, edgeGenFn func(v1, v2 Vertex) Edge) {
|
||||||
|
g.addEdgeVertexGraphHelper(vertex, graph, edgeGenFn, false, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddEdgeVertexGraphLight adds a directed edge to the graph from a vertex.
|
||||||
|
// This is useful for flattening the relationship between a subgraph and an
|
||||||
|
// existing graph, without having to run the subgraph recursively. It adds the
|
||||||
|
// minimum number of edges, creating a relationship to the vertices with
|
||||||
|
// indegree equal to zero.
|
||||||
|
func (g *Graph) AddEdgeVertexGraphLight(vertex Vertex, graph *Graph, edgeGenFn func(v1, v2 Vertex) Edge) {
|
||||||
|
g.addEdgeVertexGraphHelper(vertex, graph, edgeGenFn, false, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddEdgeGraphVertex adds a directed edge to the vertex from a graph.
|
||||||
|
// This is useful for flattening the relationship between a subgraph and an
|
||||||
|
// existing graph, without having to run the subgraph recursively. It adds the
|
||||||
|
// maximum number of edges, creating a relationship from every vertex.
|
||||||
|
func (g *Graph) AddEdgeGraphVertex(graph *Graph, vertex Vertex, edgeGenFn func(v1, v2 Vertex) Edge) {
|
||||||
|
g.addEdgeVertexGraphHelper(vertex, graph, edgeGenFn, true, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddEdgeGraphVertexLight adds a directed edge to the vertex from a graph.
|
||||||
|
// This is useful for flattening the relationship between a subgraph and an
|
||||||
|
// existing graph, without having to run the subgraph recursively. It adds the
|
||||||
|
// minimum number of edges, creating a relationship from the vertices with
|
||||||
|
// outdegree equal to zero.
|
||||||
|
func (g *Graph) AddEdgeGraphVertexLight(graph *Graph, vertex Vertex, edgeGenFn func(v1, v2 Vertex) Edge) {
|
||||||
|
g.addEdgeVertexGraphHelper(vertex, graph, edgeGenFn, true, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// addEdgeVertexGraphHelper is a helper function to add a directed edges to the
|
||||||
|
// graph from a vertex, or vice-versa. It operates in this reverse direction by
|
||||||
|
// specifying the reverse argument as true. It is useful for flattening the
|
||||||
|
// relationship between a subgraph and an existing graph, without having to run
|
||||||
|
// the subgraph recursively. It adds the maximum number of edges, creating a
|
||||||
|
// relationship to or from every vertex if the light argument is false, and if
|
||||||
|
// it is true, it adds the minimum number of edges, creating a relationship to
|
||||||
|
// or from the vertices with an indegree or outdegree equal to zero depending on
|
||||||
|
// if we specified reverse or not.
|
||||||
|
func (g *Graph) addEdgeVertexGraphHelper(vertex Vertex, graph *Graph, edgeGenFn func(v1, v2 Vertex) Edge, reverse, light bool) {
|
||||||
|
|
||||||
|
var degree map[Vertex]int // compute all of the in/outdegree's if needed
|
||||||
|
if light && reverse {
|
||||||
|
degree = graph.OutDegree()
|
||||||
|
} else if light { // && !reverse
|
||||||
|
degree = graph.InDegree()
|
||||||
|
}
|
||||||
|
for _, v := range graph.VerticesSorted() { // sort to help out edgeGenFn
|
||||||
|
|
||||||
|
// forward:
|
||||||
|
// we only want to add edges to indegree == 0, because every
|
||||||
|
// other vertex is a dependency of at least one of those
|
||||||
|
|
||||||
|
// reverse:
|
||||||
|
// we only want to add edges to outdegree == 0, because every
|
||||||
|
// other vertex is a pre-requisite to at least one of these
|
||||||
|
if light && degree[v] != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
g.AddVertex(v) // ensure vertex is part of the graph
|
||||||
|
|
||||||
|
if vertex != nil && reverse {
|
||||||
|
edge := edgeGenFn(v, vertex) // generate a new unique edge
|
||||||
|
g.AddEdge(v, vertex, edge)
|
||||||
|
} else if vertex != nil { // && !reverse
|
||||||
|
edge := edgeGenFn(vertex, v)
|
||||||
|
g.AddEdge(vertex, v, edge)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// also remember to suck in all of the graph's edges too!
|
||||||
|
for v1 := range graph.Adjacency() {
|
||||||
|
for v2, e := range graph.Adjacency()[v1] {
|
||||||
|
g.AddEdge(v1, v2, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
187
pgraph/subgraph_test.go
Normal file
187
pgraph/subgraph_test.go
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
// Mgmt
|
||||||
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
|
//
|
||||||
|
// 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package pgraph
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAddEdgeGraph1(t *testing.T) {
|
||||||
|
v1 := NV("v1")
|
||||||
|
v2 := NV("v2")
|
||||||
|
v3 := NV("v3")
|
||||||
|
v4 := NV("v4")
|
||||||
|
v5 := NV("v5")
|
||||||
|
e1 := NE("e1")
|
||||||
|
e2 := NE("e2")
|
||||||
|
e3 := NE("e3")
|
||||||
|
|
||||||
|
g := &Graph{}
|
||||||
|
g.AddEdge(v1, v3, e1)
|
||||||
|
g.AddEdge(v2, v3, e2)
|
||||||
|
|
||||||
|
sub := &Graph{}
|
||||||
|
sub.AddEdge(v4, v5, e3)
|
||||||
|
|
||||||
|
g.AddGraph(sub)
|
||||||
|
|
||||||
|
// expected (can re-use the same vertices)
|
||||||
|
expected := &Graph{}
|
||||||
|
expected.AddEdge(v1, v3, e1)
|
||||||
|
expected.AddEdge(v2, v3, e2)
|
||||||
|
expected.AddEdge(v4, v5, e3)
|
||||||
|
|
||||||
|
//expected.AddEdge(v3, v4, NE("v3,v4"))
|
||||||
|
//expected.AddEdge(v3, v5, NE("v3,v5"))
|
||||||
|
|
||||||
|
if s := runGraphCmp(t, g, expected); s != "" {
|
||||||
|
t.Errorf("%s", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddEdgeVertexGraph1(t *testing.T) {
|
||||||
|
v1 := NV("v1")
|
||||||
|
v2 := NV("v2")
|
||||||
|
v3 := NV("v3")
|
||||||
|
v4 := NV("v4")
|
||||||
|
v5 := NV("v5")
|
||||||
|
e1 := NE("e1")
|
||||||
|
e2 := NE("e2")
|
||||||
|
e3 := NE("e3")
|
||||||
|
|
||||||
|
g := &Graph{}
|
||||||
|
g.AddEdge(v1, v3, e1)
|
||||||
|
g.AddEdge(v2, v3, e2)
|
||||||
|
|
||||||
|
sub := &Graph{}
|
||||||
|
sub.AddEdge(v4, v5, e3)
|
||||||
|
|
||||||
|
g.AddEdgeVertexGraph(v3, sub, edgeGenFn)
|
||||||
|
|
||||||
|
// expected (can re-use the same vertices)
|
||||||
|
expected := &Graph{}
|
||||||
|
expected.AddEdge(v1, v3, e1)
|
||||||
|
expected.AddEdge(v2, v3, e2)
|
||||||
|
expected.AddEdge(v4, v5, e3)
|
||||||
|
|
||||||
|
expected.AddEdge(v3, v4, NE("v3,v4"))
|
||||||
|
expected.AddEdge(v3, v5, NE("v3,v5"))
|
||||||
|
|
||||||
|
if s := runGraphCmp(t, g, expected); s != "" {
|
||||||
|
t.Errorf("%s", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddEdgeGraphVertex1(t *testing.T) {
|
||||||
|
v1 := NV("v1")
|
||||||
|
v2 := NV("v2")
|
||||||
|
v3 := NV("v3")
|
||||||
|
v4 := NV("v4")
|
||||||
|
v5 := NV("v5")
|
||||||
|
e1 := NE("e1")
|
||||||
|
e2 := NE("e2")
|
||||||
|
e3 := NE("e3")
|
||||||
|
|
||||||
|
g := &Graph{}
|
||||||
|
g.AddEdge(v1, v3, e1)
|
||||||
|
g.AddEdge(v2, v3, e2)
|
||||||
|
|
||||||
|
sub := &Graph{}
|
||||||
|
sub.AddEdge(v4, v5, e3)
|
||||||
|
|
||||||
|
g.AddEdgeGraphVertex(sub, v3, edgeGenFn)
|
||||||
|
|
||||||
|
// expected (can re-use the same vertices)
|
||||||
|
expected := &Graph{}
|
||||||
|
expected.AddEdge(v1, v3, e1)
|
||||||
|
expected.AddEdge(v2, v3, e2)
|
||||||
|
expected.AddEdge(v4, v5, e3)
|
||||||
|
|
||||||
|
expected.AddEdge(v4, v3, NE("v4,v3"))
|
||||||
|
expected.AddEdge(v5, v3, NE("v5,v3"))
|
||||||
|
|
||||||
|
if s := runGraphCmp(t, g, expected); s != "" {
|
||||||
|
t.Errorf("%s", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddEdgeVertexGraphLight1(t *testing.T) {
|
||||||
|
v1 := NV("v1")
|
||||||
|
v2 := NV("v2")
|
||||||
|
v3 := NV("v3")
|
||||||
|
v4 := NV("v4")
|
||||||
|
v5 := NV("v5")
|
||||||
|
e1 := NE("e1")
|
||||||
|
e2 := NE("e2")
|
||||||
|
e3 := NE("e3")
|
||||||
|
|
||||||
|
g := &Graph{}
|
||||||
|
g.AddEdge(v1, v3, e1)
|
||||||
|
g.AddEdge(v2, v3, e2)
|
||||||
|
|
||||||
|
sub := &Graph{}
|
||||||
|
sub.AddEdge(v4, v5, e3)
|
||||||
|
|
||||||
|
g.AddEdgeVertexGraphLight(v3, sub, edgeGenFn)
|
||||||
|
|
||||||
|
// expected (can re-use the same vertices)
|
||||||
|
expected := &Graph{}
|
||||||
|
expected.AddEdge(v1, v3, e1)
|
||||||
|
expected.AddEdge(v2, v3, e2)
|
||||||
|
expected.AddEdge(v4, v5, e3)
|
||||||
|
|
||||||
|
expected.AddEdge(v3, v4, NE("v3,v4"))
|
||||||
|
//expected.AddEdge(v3, v5, NE("v3,v5")) // not needed with light
|
||||||
|
|
||||||
|
if s := runGraphCmp(t, g, expected); s != "" {
|
||||||
|
t.Errorf("%s", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddEdgeGraphVertexLight1(t *testing.T) {
|
||||||
|
v1 := NV("v1")
|
||||||
|
v2 := NV("v2")
|
||||||
|
v3 := NV("v3")
|
||||||
|
v4 := NV("v4")
|
||||||
|
v5 := NV("v5")
|
||||||
|
e1 := NE("e1")
|
||||||
|
e2 := NE("e2")
|
||||||
|
e3 := NE("e3")
|
||||||
|
|
||||||
|
g := &Graph{}
|
||||||
|
g.AddEdge(v1, v3, e1)
|
||||||
|
g.AddEdge(v2, v3, e2)
|
||||||
|
|
||||||
|
sub := &Graph{}
|
||||||
|
sub.AddEdge(v4, v5, e3)
|
||||||
|
|
||||||
|
g.AddEdgeGraphVertexLight(sub, v3, edgeGenFn)
|
||||||
|
|
||||||
|
// expected (can re-use the same vertices)
|
||||||
|
expected := &Graph{}
|
||||||
|
expected.AddEdge(v1, v3, e1)
|
||||||
|
expected.AddEdge(v2, v3, e2)
|
||||||
|
expected.AddEdge(v4, v5, e3)
|
||||||
|
|
||||||
|
//expected.AddEdge(v4, v3, NE("v4,v3")) // not needed with light
|
||||||
|
expected.AddEdge(v5, v3, NE("v5,v3"))
|
||||||
|
|
||||||
|
if s := runGraphCmp(t, g, expected); s != "" {
|
||||||
|
t.Errorf("%s", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
93
pgraph/util_test.go
Normal file
93
pgraph/util_test.go
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
// Mgmt
|
||||||
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
|
//
|
||||||
|
// 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package pgraph
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// vertex is a test struct to test the library.
|
||||||
|
type vertex struct {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
// String is a required method of the Vertex interface that we must fulfill.
|
||||||
|
func (v *vertex) String() string {
|
||||||
|
return v.name
|
||||||
|
}
|
||||||
|
|
||||||
|
// NV is a helper function to make testing easier. It creates a new noop vertex.
|
||||||
|
func NV(s string) Vertex {
|
||||||
|
return &vertex{s}
|
||||||
|
}
|
||||||
|
|
||||||
|
// edge is a test struct to test the library.
|
||||||
|
type edge struct {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
// String is a required method of the Edge interface that we must fulfill.
|
||||||
|
func (e *edge) String() string {
|
||||||
|
return e.name
|
||||||
|
}
|
||||||
|
|
||||||
|
// NE is a helper function to make testing easier. It creates a new noop edge.
|
||||||
|
func NE(s string) Edge {
|
||||||
|
return &edge{s}
|
||||||
|
}
|
||||||
|
|
||||||
|
// edgeGenFn generates unique edges for each vertex pair, assuming unique
|
||||||
|
// vertices.
|
||||||
|
func edgeGenFn(v1, v2 Vertex) Edge {
|
||||||
|
return NE(fmt.Sprintf("%s,%s", v1, v2))
|
||||||
|
}
|
||||||
|
|
||||||
|
func vertexAddFn(v Vertex) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func vertexRemoveFn(v Vertex) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func runGraphCmp(t *testing.T, g1, g2 *Graph) string {
|
||||||
|
err := g1.GraphCmp(g2, strVertexCmpFn, strEdgeCmpFn)
|
||||||
|
if err != nil {
|
||||||
|
str := ""
|
||||||
|
str += fmt.Sprintf(" actual (g1): %v%s", g1, fullPrint(g1))
|
||||||
|
str += fmt.Sprintf("expected (g2): %v%s", g2, fullPrint(g2))
|
||||||
|
str += fmt.Sprintf("cmp error:")
|
||||||
|
str += fmt.Sprintf("%v", err)
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func fullPrint(g *Graph) (str string) {
|
||||||
|
str += "\n"
|
||||||
|
for v := range g.Adjacency() {
|
||||||
|
str += fmt.Sprintf("* v: %s\n", v)
|
||||||
|
}
|
||||||
|
for v1 := range g.Adjacency() {
|
||||||
|
for v2, e := range g.Adjacency()[v1] {
|
||||||
|
str += fmt.Sprintf("* e: %s -> %s # %s\n", v1, v2, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
263
prometheus/prometheus.go
Normal file
263
prometheus/prometheus.go
Normal file
@@ -0,0 +1,263 @@
|
|||||||
|
// Mgmt
|
||||||
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
|
//
|
||||||
|
// 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
// Package prometheus provides functions that are useful to control and manage
|
||||||
|
// the build-in prometheus instance.
|
||||||
|
package prometheus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
|
|
||||||
|
errwrap "github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DefaultPrometheusListen is registered in
|
||||||
|
// https://github.com/prometheus/prometheus/wiki/Default-port-allocations
|
||||||
|
const DefaultPrometheusListen = "127.0.0.1:9233"
|
||||||
|
|
||||||
|
// ResState represents the status of a resource.
|
||||||
|
type ResState int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// ResStateOK represents a working resource
|
||||||
|
ResStateOK ResState = iota
|
||||||
|
// ResStateSoftFail represents a resource in soft fail (will be retried)
|
||||||
|
ResStateSoftFail
|
||||||
|
// ResStateHardFail represents a resource in hard fail (will NOT be retried)
|
||||||
|
ResStateHardFail
|
||||||
|
)
|
||||||
|
|
||||||
|
// Prometheus is the struct that contains information about the
|
||||||
|
// prometheus instance. Run Init() on it.
|
||||||
|
type Prometheus struct {
|
||||||
|
Listen string // the listen specification for the net/http server
|
||||||
|
|
||||||
|
checkApplyTotal *prometheus.CounterVec // total of CheckApplies that have been triggered
|
||||||
|
pgraphStartTimeSeconds prometheus.Gauge // process start time in seconds since unix epoch
|
||||||
|
managedResources *prometheus.GaugeVec // Resources we manage now
|
||||||
|
failedResourcesTotal *prometheus.CounterVec // Total of failures since mgmt has started
|
||||||
|
failedResources *prometheus.GaugeVec // Number of current resources
|
||||||
|
|
||||||
|
resourcesState map[string]resStateWithKind // Maps the resources with their current kind/state
|
||||||
|
mutex *sync.Mutex // Mutex used to update resourcesState
|
||||||
|
}
|
||||||
|
|
||||||
|
// resStateWithKind is used to count the failures by kind
|
||||||
|
type resStateWithKind struct {
|
||||||
|
state ResState
|
||||||
|
kind string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init some parameters - currently the Listen address.
|
||||||
|
func (obj *Prometheus) Init() error {
|
||||||
|
if len(obj.Listen) == 0 {
|
||||||
|
obj.Listen = DefaultPrometheusListen
|
||||||
|
}
|
||||||
|
|
||||||
|
obj.mutex = &sync.Mutex{}
|
||||||
|
obj.resourcesState = make(map[string]resStateWithKind)
|
||||||
|
|
||||||
|
obj.checkApplyTotal = prometheus.NewCounterVec(
|
||||||
|
prometheus.CounterOpts{
|
||||||
|
Name: "mgmt_checkapply_total",
|
||||||
|
Help: "Number of CheckApply that have run.",
|
||||||
|
},
|
||||||
|
// Labels for this metric.
|
||||||
|
// kind: resource type: Svc, File, ...
|
||||||
|
// apply: if the CheckApply happened in "apply" mode
|
||||||
|
// eventful: did the CheckApply generate an event
|
||||||
|
// errorful: did the CheckApply generate an error
|
||||||
|
[]string{"kind", "apply", "eventful", "errorful"},
|
||||||
|
)
|
||||||
|
prometheus.MustRegister(obj.checkApplyTotal)
|
||||||
|
|
||||||
|
obj.pgraphStartTimeSeconds = prometheus.NewGauge(
|
||||||
|
prometheus.GaugeOpts{
|
||||||
|
Name: "mgmt_graph_start_time_seconds",
|
||||||
|
Help: "Start time of the current graph since unix epoch in seconds.",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
prometheus.MustRegister(obj.pgraphStartTimeSeconds)
|
||||||
|
|
||||||
|
obj.managedResources = prometheus.NewGaugeVec(
|
||||||
|
prometheus.GaugeOpts{
|
||||||
|
Name: "mgmt_resources",
|
||||||
|
Help: "Number of managed resources.",
|
||||||
|
},
|
||||||
|
// kind: resource type: Svc, File, ...
|
||||||
|
[]string{"kind"},
|
||||||
|
)
|
||||||
|
prometheus.MustRegister(obj.managedResources)
|
||||||
|
|
||||||
|
obj.failedResourcesTotal = prometheus.NewCounterVec(
|
||||||
|
prometheus.CounterOpts{
|
||||||
|
Name: "mgmt_failures_total",
|
||||||
|
Help: "Total of failed resources.",
|
||||||
|
},
|
||||||
|
// kind: resource type: Svc, File, ...
|
||||||
|
// failure: soft or hard
|
||||||
|
[]string{"kind", "failure"},
|
||||||
|
)
|
||||||
|
prometheus.MustRegister(obj.failedResourcesTotal)
|
||||||
|
|
||||||
|
obj.failedResources = prometheus.NewGaugeVec(
|
||||||
|
prometheus.GaugeOpts{
|
||||||
|
Name: "mgmt_failures",
|
||||||
|
Help: "Number of failing resources.",
|
||||||
|
},
|
||||||
|
// kind: resource type: Svc, File, ...
|
||||||
|
// failure: soft or hard
|
||||||
|
[]string{"kind", "failure"},
|
||||||
|
)
|
||||||
|
prometheus.MustRegister(obj.failedResources)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start runs a http server in a go routine, that responds to /metrics
|
||||||
|
// as prometheus would expect.
|
||||||
|
func (obj *Prometheus) Start() error {
|
||||||
|
http.Handle("/metrics", promhttp.Handler())
|
||||||
|
go http.ListenAndServe(obj.Listen, nil)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop the http server.
|
||||||
|
func (obj *Prometheus) Stop() error {
|
||||||
|
// TODO: There is no way in go < 1.8 to stop a http server.
|
||||||
|
// https://stackoverflow.com/questions/39320025/go-how-to-stop-http-listenandserve/41433555#41433555
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateCheckApplyTotal refreshes the failing gauge by parsing the internal
|
||||||
|
// state map.
|
||||||
|
func (obj *Prometheus) UpdateCheckApplyTotal(kind string, apply, eventful, errorful bool) error {
|
||||||
|
if obj == nil {
|
||||||
|
return nil // happens when mgmt is launched without --prometheus
|
||||||
|
}
|
||||||
|
labels := prometheus.Labels{"kind": kind, "apply": strconv.FormatBool(apply), "eventful": strconv.FormatBool(eventful), "errorful": strconv.FormatBool(errorful)}
|
||||||
|
metric := obj.checkApplyTotal.With(labels)
|
||||||
|
metric.Inc()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdatePgraphStartTime updates the mgmt_graph_start_time_seconds metric
|
||||||
|
// to the current timestamp.
|
||||||
|
func (obj *Prometheus) UpdatePgraphStartTime() error {
|
||||||
|
if obj == nil {
|
||||||
|
return nil // happens when mgmt is launched without --prometheus
|
||||||
|
}
|
||||||
|
obj.pgraphStartTimeSeconds.SetToCurrentTime()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddManagedResource increments the Managed Resource counter and updates the resource status.
|
||||||
|
func (obj *Prometheus) AddManagedResource(resUUID string, rtype string) error {
|
||||||
|
if obj == nil {
|
||||||
|
return nil // happens when mgmt is launched without --prometheus
|
||||||
|
}
|
||||||
|
obj.managedResources.With(prometheus.Labels{"kind": rtype}).Inc()
|
||||||
|
if err := obj.UpdateState(resUUID, rtype, ResStateOK); err != nil {
|
||||||
|
return errwrap.Wrapf(err, "can't update the resource status in the map")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveManagedResource decrements the Managed Resource counter and updates the resource status.
|
||||||
|
func (obj *Prometheus) RemoveManagedResource(resUUID string, rtype string) error {
|
||||||
|
if obj == nil {
|
||||||
|
return nil // happens when mgmt is launched without --prometheus
|
||||||
|
}
|
||||||
|
obj.managedResources.With(prometheus.Labels{"kind": rtype}).Dec()
|
||||||
|
if err := obj.deleteState(resUUID); err != nil {
|
||||||
|
return errwrap.Wrapf(err, "can't remove the resource status from the map")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// deleteState removes the resources for the state map and re-populates the failing gauge.
|
||||||
|
func (obj *Prometheus) deleteState(resUUID string) error {
|
||||||
|
if obj == nil {
|
||||||
|
return nil // happens when mgmt is launched without --prometheus
|
||||||
|
}
|
||||||
|
obj.mutex.Lock()
|
||||||
|
delete(obj.resourcesState, resUUID)
|
||||||
|
obj.mutex.Unlock()
|
||||||
|
if err := obj.updateFailingGauge(); err != nil {
|
||||||
|
return errwrap.Wrapf(err, "can't update the failing gauge")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateState updates the state of the resources in our internal state map
|
||||||
|
// then triggers a refresh of the failing gauge.
|
||||||
|
func (obj *Prometheus) UpdateState(resUUID string, rtype string, newState ResState) error {
|
||||||
|
defer obj.updateFailingGauge()
|
||||||
|
if obj == nil {
|
||||||
|
return nil // happens when mgmt is launched without --prometheus
|
||||||
|
}
|
||||||
|
obj.mutex.Lock()
|
||||||
|
obj.resourcesState[resUUID] = resStateWithKind{state: newState, kind: rtype}
|
||||||
|
obj.mutex.Unlock()
|
||||||
|
if newState != ResStateOK {
|
||||||
|
var strState string
|
||||||
|
if newState == ResStateSoftFail {
|
||||||
|
strState = "soft"
|
||||||
|
} else if newState == ResStateHardFail {
|
||||||
|
strState = "hard"
|
||||||
|
} else {
|
||||||
|
return errors.New("state should be soft or hard failure")
|
||||||
|
}
|
||||||
|
obj.failedResourcesTotal.With(prometheus.Labels{"kind": rtype, "failure": strState}).Inc()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// updateFailingGauge refreshes the failing gauge by parsking the internal
|
||||||
|
// state map.
|
||||||
|
func (obj *Prometheus) updateFailingGauge() error {
|
||||||
|
if obj == nil {
|
||||||
|
return nil // happens when mgmt is launched without --prometheus
|
||||||
|
}
|
||||||
|
var softFails, hardFails map[string]float64
|
||||||
|
softFails = make(map[string]float64)
|
||||||
|
hardFails = make(map[string]float64)
|
||||||
|
for _, v := range obj.resourcesState {
|
||||||
|
if v.state == ResStateSoftFail {
|
||||||
|
softFails[v.kind]++
|
||||||
|
} else if v.state == ResStateHardFail {
|
||||||
|
hardFails[v.kind]++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: we might want to Zero the metrics we are not using
|
||||||
|
// because in prometheus design the metrics keep living for some time
|
||||||
|
// even after they are removed.
|
||||||
|
obj.failedResources.Reset()
|
||||||
|
for k, v := range softFails {
|
||||||
|
obj.failedResources.With(prometheus.Labels{"kind": k, "failure": "soft"}).Set(v)
|
||||||
|
}
|
||||||
|
for k, v := range hardFails {
|
||||||
|
obj.failedResources.With(prometheus.Labels{"kind": k, "failure": "hard"}).Set(v)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,18 +1,18 @@
|
|||||||
// Mgmt
|
// Mgmt
|
||||||
// Copyright (C) 2013-2016+ James Shubin and the project contributors
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// GNU Affero General Public License for more details.
|
// GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package puppet
|
package puppet
|
||||||
@@ -50,10 +50,10 @@ func NewGAPI(data gapi.Data, puppetParam *string, puppetConf string) (*GAPI, err
|
|||||||
// Init initializes the puppet GAPI struct.
|
// Init initializes the puppet GAPI struct.
|
||||||
func (obj *GAPI) Init(data gapi.Data) error {
|
func (obj *GAPI) Init(data gapi.Data) error {
|
||||||
if obj.initialized {
|
if obj.initialized {
|
||||||
return fmt.Errorf("Already initialized!")
|
return fmt.Errorf("already initialized")
|
||||||
}
|
}
|
||||||
if obj.PuppetParam == nil {
|
if obj.PuppetParam == nil {
|
||||||
return fmt.Errorf("The PuppetParam param must be specified!")
|
return fmt.Errorf("the PuppetParam param must be specified")
|
||||||
}
|
}
|
||||||
obj.data = data // store for later
|
obj.data = data // store for later
|
||||||
obj.closeChan = make(chan struct{})
|
obj.closeChan = make(chan struct{})
|
||||||
@@ -64,43 +64,72 @@ func (obj *GAPI) Init(data gapi.Data) error {
|
|||||||
// Graph returns a current Graph.
|
// Graph returns a current Graph.
|
||||||
func (obj *GAPI) Graph() (*pgraph.Graph, error) {
|
func (obj *GAPI) Graph() (*pgraph.Graph, error) {
|
||||||
if !obj.initialized {
|
if !obj.initialized {
|
||||||
return nil, fmt.Errorf("Puppet: GAPI is not initialized!")
|
return nil, fmt.Errorf("the puppet GAPI is not initialized")
|
||||||
}
|
}
|
||||||
config := ParseConfigFromPuppet(*obj.PuppetParam, obj.PuppetConf)
|
config := ParseConfigFromPuppet(*obj.PuppetParam, obj.PuppetConf)
|
||||||
if config == nil {
|
if config == nil {
|
||||||
return nil, fmt.Errorf("Puppet: ParseConfigFromPuppet returned nil!")
|
return nil, fmt.Errorf("function ParseConfigFromPuppet returned nil")
|
||||||
}
|
}
|
||||||
g, err := config.NewGraphFromConfig(obj.data.Hostname, obj.data.World, obj.data.Noop)
|
g, err := config.NewGraphFromConfig(obj.data.Hostname, obj.data.World, obj.data.Noop)
|
||||||
return g, err
|
return g, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next returns nil errors every time there could be a new graph.
|
// Next returns nil errors every time there could be a new graph.
|
||||||
func (obj *GAPI) Next() chan error {
|
func (obj *GAPI) Next() chan gapi.Next {
|
||||||
if obj.data.NoWatch {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
puppetChan := func() <-chan time.Time { // helper function
|
puppetChan := func() <-chan time.Time { // helper function
|
||||||
return time.Tick(time.Duration(PuppetInterval(obj.PuppetConf)) * time.Second)
|
return time.Tick(time.Duration(RefreshInterval(obj.PuppetConf)) * time.Second)
|
||||||
}
|
}
|
||||||
ch := make(chan error)
|
ch := make(chan gapi.Next)
|
||||||
obj.wg.Add(1)
|
obj.wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer obj.wg.Done()
|
defer obj.wg.Done()
|
||||||
defer close(ch) // this will run before the obj.wg.Done()
|
defer close(ch) // this will run before the obj.wg.Done()
|
||||||
if !obj.initialized {
|
if !obj.initialized {
|
||||||
ch <- fmt.Errorf("Puppet: GAPI is not initialized!")
|
next := gapi.Next{
|
||||||
|
Err: fmt.Errorf("the puppet GAPI is not initialized"),
|
||||||
|
Exit: true, // exit, b/c programming error?
|
||||||
|
}
|
||||||
|
ch <- next
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
pChan := puppetChan()
|
startChan := make(chan struct{}) // start signal
|
||||||
|
close(startChan) // kick it off!
|
||||||
|
|
||||||
|
pChan := make(<-chan time.Time)
|
||||||
|
// NOTE: we don't look at obj.data.NoConfigWatch since emulating
|
||||||
|
// puppet means we do not switch graphs on code changes anyways.
|
||||||
|
if obj.data.NoStreamWatch {
|
||||||
|
pChan = nil
|
||||||
|
} else {
|
||||||
|
pChan = puppetChan()
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
case <-startChan: // kick the loop once at start
|
||||||
|
startChan = nil // disable
|
||||||
|
// pass
|
||||||
case _, ok := <-pChan:
|
case _, ok := <-pChan:
|
||||||
if !ok { // the channel closed!
|
if !ok { // the channel closed!
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
case <-obj.closeChan:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("Puppet: Generating new graph...")
|
log.Printf("Puppet: Generating new graph...")
|
||||||
|
if obj.data.NoStreamWatch {
|
||||||
|
pChan = nil
|
||||||
|
} else {
|
||||||
pChan = puppetChan() // TODO: okay to update interval in case it changed?
|
pChan = puppetChan() // TODO: okay to update interval in case it changed?
|
||||||
ch <- nil // trigger a run
|
}
|
||||||
|
next := gapi.Next{
|
||||||
|
//Exit: true, // TODO: for permanent shutdown!
|
||||||
|
Err: nil,
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case ch <- next: // trigger a run (send a msg)
|
||||||
|
// unblock if we exit while waiting to send!
|
||||||
case <-obj.closeChan:
|
case <-obj.closeChan:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -112,7 +141,7 @@ func (obj *GAPI) Next() chan error {
|
|||||||
// Close shuts down the Puppet GAPI.
|
// Close shuts down the Puppet GAPI.
|
||||||
func (obj *GAPI) Close() error {
|
func (obj *GAPI) Close() error {
|
||||||
if !obj.initialized {
|
if !obj.initialized {
|
||||||
return fmt.Errorf("Puppet: GAPI is not initialized!")
|
return fmt.Errorf("the puppet GAPI is not initialized")
|
||||||
}
|
}
|
||||||
close(obj.closeChan)
|
close(obj.closeChan)
|
||||||
obj.wg.Wait()
|
obj.wg.Wait()
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
// Mgmt
|
// Mgmt
|
||||||
// Copyright (C) 2013-2016+ James Shubin and the project contributors
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// GNU Affero General Public License for more details.
|
// GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
// Package puppet provides the integration entrypoint for the puppet language.
|
// Package puppet provides the integration entrypoint for the puppet language.
|
||||||
@@ -116,8 +116,8 @@ func ParseConfigFromPuppet(puppetParam, puppetConf string) *yamlgraph.GraphConfi
|
|||||||
return &config
|
return &config
|
||||||
}
|
}
|
||||||
|
|
||||||
// PuppetInterval returns the graph refresh interval from the puppet configuration.
|
// RefreshInterval returns the graph refresh interval from the puppet configuration.
|
||||||
func PuppetInterval(puppetConf string) int {
|
func RefreshInterval(puppetConf string) int {
|
||||||
if Debug {
|
if Debug {
|
||||||
log.Printf("Puppet: determining graph refresh interval")
|
log.Printf("Puppet: determining graph refresh interval")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
// Mgmt
|
// Mgmt
|
||||||
// Copyright (C) 2013-2016+ James Shubin and the project contributors
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// GNU Affero General Public License for more details.
|
// GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package recwatch
|
package recwatch
|
||||||
@@ -59,15 +59,23 @@ func (obj *ConfigWatcher) Add(file ...string) {
|
|||||||
ch := obj.ConfigWatch(file[0])
|
ch := obj.ConfigWatch(file[0])
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case e := <-ch:
|
case e, ok := <-ch:
|
||||||
|
if !ok { // channel closed
|
||||||
|
return
|
||||||
|
}
|
||||||
if e != nil {
|
if e != nil {
|
||||||
obj.errorchan <- e
|
obj.errorchan <- e
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
obj.ch <- file[0]
|
select {
|
||||||
continue
|
case obj.ch <- file[0]: // send on channel
|
||||||
case <-obj.closechan:
|
case <-obj.closechan:
|
||||||
return
|
return // never mind, close early!
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
// not needed, closes via ConfigWatch() chan close
|
||||||
|
//case <-obj.closechan:
|
||||||
|
// return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -126,7 +134,12 @@ func (obj *ConfigWatcher) ConfigWatch(file string) chan error {
|
|||||||
close(ch)
|
close(ch)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ch <- nil // send event!
|
select {
|
||||||
|
case ch <- nil: // send event!
|
||||||
|
case <-obj.closechan:
|
||||||
|
close(ch)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//close(ch)
|
//close(ch)
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
// Mgmt
|
// Mgmt
|
||||||
// Copyright (C) 2013-2016+ James Shubin and the project contributors
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// GNU Affero General Public License for more details.
|
// GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package recwatch
|
package recwatch
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
// Mgmt
|
// Mgmt
|
||||||
// Copyright (C) 2013-2016+ James Shubin and the project contributors
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// GNU Affero General Public License for more details.
|
// GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
// Package recwatch provides recursive file watching events via fsnotify.
|
// Package recwatch provides recursive file watching events via fsnotify.
|
||||||
@@ -51,10 +51,10 @@ type RecWatcher struct {
|
|||||||
watcher *fsnotify.Watcher
|
watcher *fsnotify.Watcher
|
||||||
watches map[string]struct{}
|
watches map[string]struct{}
|
||||||
events chan Event // one channel for events and err...
|
events chan Event // one channel for events and err...
|
||||||
once sync.Once
|
closed bool // is the events channel closed?
|
||||||
|
mutex sync.Mutex // lock guarding the channel closing
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
exit chan struct{}
|
exit chan struct{}
|
||||||
closeErr error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRecWatcher creates an initializes a new recursive watcher.
|
// NewRecWatcher creates an initializes a new recursive watcher.
|
||||||
@@ -87,11 +87,22 @@ func (obj *RecWatcher) Init() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
obj.wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
|
defer obj.wg.Done()
|
||||||
if err := obj.Watch(); err != nil {
|
if err := obj.Watch(); err != nil {
|
||||||
obj.events <- Event{Error: err}
|
// we need this mutex, because if we Init and then Close
|
||||||
|
// immediately, this can send after closed which panics!
|
||||||
|
obj.mutex.Lock()
|
||||||
|
if !obj.closed {
|
||||||
|
select {
|
||||||
|
case obj.events <- Event{Error: err}:
|
||||||
|
case <-obj.exit:
|
||||||
|
// pass
|
||||||
|
}
|
||||||
|
}
|
||||||
|
obj.mutex.Unlock()
|
||||||
}
|
}
|
||||||
obj.Close()
|
|
||||||
}()
|
}()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -106,26 +117,18 @@ func (obj *RecWatcher) Init() error {
|
|||||||
|
|
||||||
// Close shuts down the watcher.
|
// Close shuts down the watcher.
|
||||||
func (obj *RecWatcher) Close() error {
|
func (obj *RecWatcher) Close() error {
|
||||||
obj.once.Do(obj.close) // don't cause the channel to close twice
|
|
||||||
return obj.closeErr
|
|
||||||
}
|
|
||||||
|
|
||||||
// This close function is the function that actually does the close work. Don't
|
|
||||||
// call it more than once!
|
|
||||||
func (obj *RecWatcher) close() {
|
|
||||||
var err error
|
var err error
|
||||||
close(obj.exit) // send exit signal
|
close(obj.exit) // send exit signal
|
||||||
obj.wg.Wait()
|
obj.wg.Wait()
|
||||||
if obj.watcher != nil {
|
if obj.watcher != nil {
|
||||||
err = obj.watcher.Close()
|
err = obj.watcher.Close()
|
||||||
obj.watcher = nil
|
obj.watcher = nil
|
||||||
// TODO: should we send the close error?
|
|
||||||
//if err != nil {
|
|
||||||
// obj.events <- Event{Error: err}
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
obj.mutex.Lock() // FIXME: I don't think this mutex is needed anymore...
|
||||||
|
obj.closed = true
|
||||||
close(obj.events)
|
close(obj.events)
|
||||||
obj.closeErr = err // set the error
|
obj.mutex.Unlock()
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Events returns a channel of events. These include events for errors.
|
// Events returns a channel of events. These include events for errors.
|
||||||
@@ -134,10 +137,8 @@ func (obj *RecWatcher) Events() chan Event { return obj.events }
|
|||||||
// Watch is the primary listener for this resource and it outputs events.
|
// Watch is the primary listener for this resource and it outputs events.
|
||||||
func (obj *RecWatcher) Watch() error {
|
func (obj *RecWatcher) Watch() error {
|
||||||
if obj.watcher == nil {
|
if obj.watcher == nil {
|
||||||
return fmt.Errorf("Watcher is not initialized!")
|
return fmt.Errorf("the watcher is not initialized")
|
||||||
}
|
}
|
||||||
obj.wg.Add(1)
|
|
||||||
defer obj.wg.Done()
|
|
||||||
|
|
||||||
patharray := util.PathSplit(obj.safename) // tokenize the path
|
patharray := util.PathSplit(obj.safename) // tokenize the path
|
||||||
var index = len(patharray) // starting index
|
var index = len(patharray) // starting index
|
||||||
@@ -169,11 +170,11 @@ func (obj *RecWatcher) Watch() error {
|
|||||||
// no space left on device, out of inotify watches
|
// no space left on device, out of inotify watches
|
||||||
// TODO: consider letting the user fall back to
|
// TODO: consider letting the user fall back to
|
||||||
// polling if they hit this error very often...
|
// polling if they hit this error very often...
|
||||||
return fmt.Errorf("Out of inotify watches: %v", err)
|
return fmt.Errorf("out of inotify watches: %v", err)
|
||||||
} else if os.IsPermission(err) {
|
} else if os.IsPermission(err) {
|
||||||
return fmt.Errorf("Permission denied adding a watch: %v", err)
|
return fmt.Errorf("permission denied adding a watch: %v", err)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("Unknown error: %v", err)
|
return fmt.Errorf("unknown error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
@@ -236,6 +237,13 @@ func (obj *RecWatcher) Watch() error {
|
|||||||
index--
|
index--
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// when the file is moved, remove the watcher and add a new one,
|
||||||
|
// so we stop tracking the old inode.
|
||||||
|
if deltaDepth >= 0 && (event.Op&fsnotify.Rename == fsnotify.Rename) {
|
||||||
|
obj.watcher.Remove(current)
|
||||||
|
obj.watcher.Add(current)
|
||||||
|
}
|
||||||
|
|
||||||
// we must be a parent watcher, so descend in
|
// we must be a parent watcher, so descend in
|
||||||
if deltaDepth < 0 {
|
if deltaDepth < 0 {
|
||||||
// XXX: we can block here due to: https://github.com/fsnotify/fsnotify/issues/123
|
// XXX: we can block here due to: https://github.com/fsnotify/fsnotify/issues/123
|
||||||
@@ -272,11 +280,16 @@ func (obj *RecWatcher) Watch() error {
|
|||||||
if send {
|
if send {
|
||||||
send = false
|
send = false
|
||||||
// only invalid state on certain types of events
|
// only invalid state on certain types of events
|
||||||
obj.events <- Event{Error: nil, Body: &event}
|
select {
|
||||||
|
// exit even when we're blocked on event sending
|
||||||
|
case obj.events <- Event{Error: nil, Body: &event}:
|
||||||
|
case <-obj.exit:
|
||||||
|
return fmt.Errorf("pending event not sent")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case err := <-obj.watcher.Errors:
|
case err := <-obj.watcher.Errors:
|
||||||
return fmt.Errorf("Unknown watcher error: %v", err)
|
return fmt.Errorf("unknown watcher error: %v", err)
|
||||||
|
|
||||||
case <-obj.exit:
|
case <-obj.exit:
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
// Mgmt
|
// Mgmt
|
||||||
// Copyright (C) 2013-2016+ James Shubin and the project contributors
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
// Written by James Shubin <james@shubin.ca> and the project contributors
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as published by
|
// it under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// GNU Affero General Public License for more details.
|
// GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
// Package remote provides the remoting facilities for agentless execution.
|
// Package remote provides the remoting facilities for agentless execution.
|
||||||
@@ -63,6 +63,7 @@ import (
|
|||||||
|
|
||||||
cv "github.com/purpleidea/mgmt/converger"
|
cv "github.com/purpleidea/mgmt/converger"
|
||||||
"github.com/purpleidea/mgmt/util"
|
"github.com/purpleidea/mgmt/util"
|
||||||
|
"github.com/purpleidea/mgmt/util/semaphore"
|
||||||
"github.com/purpleidea/mgmt/yamlgraph"
|
"github.com/purpleidea/mgmt/yamlgraph"
|
||||||
|
|
||||||
multierr "github.com/hashicorp/go-multierror"
|
multierr "github.com/hashicorp/go-multierror"
|
||||||
@@ -691,7 +692,7 @@ type Remotes struct {
|
|||||||
fileWatch chan string
|
fileWatch chan string
|
||||||
cConns uint16 // number of concurrent ssh connections, zero means unlimited
|
cConns uint16 // number of concurrent ssh connections, zero means unlimited
|
||||||
interactive bool // allow interactive prompting
|
interactive bool // allow interactive prompting
|
||||||
sshPrivIdRsa string // path to ~/.ssh/id_rsa
|
sshPrivIDRsa string // path to ~/.ssh/id_rsa
|
||||||
caching bool // whether to try and cache the copy of the binary
|
caching bool // whether to try and cache the copy of the binary
|
||||||
depth uint16 // depth of this node in the remote execution hierarchy
|
depth uint16 // depth of this node in the remote execution hierarchy
|
||||||
prefix string // folder prefix to use for misc storage
|
prefix string // folder prefix to use for misc storage
|
||||||
@@ -701,19 +702,20 @@ type Remotes struct {
|
|||||||
wg sync.WaitGroup // keep track of each running SSH connection
|
wg sync.WaitGroup // keep track of each running SSH connection
|
||||||
lock sync.Mutex // mutex for access to sshmap
|
lock sync.Mutex // mutex for access to sshmap
|
||||||
sshmap map[string]*SSH // map to each SSH struct with the remote as the key
|
sshmap map[string]*SSH // map to each SSH struct with the remote as the key
|
||||||
|
running chan struct{} // closes when main loop is running
|
||||||
exiting bool // flag to let us know if we're exiting
|
exiting bool // flag to let us know if we're exiting
|
||||||
exitChan chan struct{} // closes when we should exit
|
exitChan chan struct{} // closes when we should exit
|
||||||
semaphore Semaphore // counting semaphore to limit concurrent connections
|
semaphore *semaphore.Semaphore // counting semaphore to limit concurrent connections
|
||||||
hostnames []string // list of hostnames we've seen so far
|
hostnames []string // list of hostnames we've seen so far
|
||||||
cuid cv.ConvergerUID // convergerUID for the remote itself
|
cuid cv.UID // convergerUID for the remote itself
|
||||||
cuids map[string]cv.ConvergerUID // map to each SSH struct with the remote as the key
|
cuids map[string]cv.UID // map to each SSH struct with the remote as the key
|
||||||
callbackCancelFunc func() // stored callback function cancel function
|
callbackCancelFunc func() // stored callback function cancel function
|
||||||
|
|
||||||
flags Flags // constant runtime values
|
flags Flags // constant runtime values
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRemotes builds a Remotes struct.
|
// NewRemotes builds a Remotes struct.
|
||||||
func NewRemotes(clientURLs, remoteURLs []string, noop bool, remotes []string, fileWatch chan string, cConns uint16, interactive bool, sshPrivIdRsa string, caching bool, depth uint16, prefix string, converger cv.Converger, convergerCb func(func(map[string]bool) error) (func(), error), flags Flags) *Remotes {
|
func NewRemotes(clientURLs, remoteURLs []string, noop bool, remotes []string, fileWatch chan string, cConns uint16, interactive bool, sshPrivIDRsa string, caching bool, depth uint16, prefix string, converger cv.Converger, convergerCb func(func(map[string]bool) error) (func(), error), flags Flags) *Remotes {
|
||||||
return &Remotes{
|
return &Remotes{
|
||||||
clientURLs: clientURLs,
|
clientURLs: clientURLs,
|
||||||
remoteURLs: remoteURLs,
|
remoteURLs: remoteURLs,
|
||||||
@@ -722,17 +724,18 @@ func NewRemotes(clientURLs, remoteURLs []string, noop bool, remotes []string, fi
|
|||||||
fileWatch: fileWatch,
|
fileWatch: fileWatch,
|
||||||
cConns: cConns,
|
cConns: cConns,
|
||||||
interactive: interactive,
|
interactive: interactive,
|
||||||
sshPrivIdRsa: sshPrivIdRsa,
|
sshPrivIDRsa: sshPrivIDRsa,
|
||||||
caching: caching,
|
caching: caching,
|
||||||
depth: depth,
|
depth: depth,
|
||||||
prefix: prefix,
|
prefix: prefix,
|
||||||
converger: converger,
|
converger: converger,
|
||||||
convergerCb: convergerCb,
|
convergerCb: convergerCb,
|
||||||
sshmap: make(map[string]*SSH),
|
sshmap: make(map[string]*SSH),
|
||||||
|
running: make(chan struct{}),
|
||||||
exitChan: make(chan struct{}),
|
exitChan: make(chan struct{}),
|
||||||
semaphore: NewSemaphore(int(cConns)),
|
semaphore: semaphore.NewSemaphore(int(cConns)),
|
||||||
hostnames: make([]string, len(remotes)),
|
hostnames: make([]string, len(remotes)),
|
||||||
cuids: make(map[string]cv.ConvergerUID),
|
cuids: make(map[string]cv.UID),
|
||||||
flags: flags,
|
flags: flags,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -829,18 +832,18 @@ func (obj *Remotes) NewSSH(file string) (*SSH, error) {
|
|||||||
|
|
||||||
// sshKeyAuth is a helper function to get the ssh key auth struct needed
|
// sshKeyAuth is a helper function to get the ssh key auth struct needed
|
||||||
func (obj *Remotes) sshKeyAuth() (ssh.AuthMethod, error) {
|
func (obj *Remotes) sshKeyAuth() (ssh.AuthMethod, error) {
|
||||||
if obj.sshPrivIdRsa == "" {
|
if obj.sshPrivIDRsa == "" {
|
||||||
return nil, fmt.Errorf("empty path specified")
|
return nil, fmt.Errorf("empty path specified")
|
||||||
}
|
}
|
||||||
p := ""
|
p := ""
|
||||||
// TODO: this doesn't match strings of the form: ~james/.ssh/id_rsa
|
// TODO: this doesn't match strings of the form: ~james/.ssh/id_rsa
|
||||||
if strings.HasPrefix(obj.sshPrivIdRsa, "~/") {
|
if strings.HasPrefix(obj.sshPrivIDRsa, "~/") {
|
||||||
usr, err := user.Current()
|
usr, err := user.Current()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Remote: Can't find home directory automatically.")
|
log.Printf("Remote: Can't find home directory automatically.")
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
p = path.Join(usr.HomeDir, obj.sshPrivIdRsa[len("~/"):])
|
p = path.Join(usr.HomeDir, obj.sshPrivIDRsa[len("~/"):])
|
||||||
}
|
}
|
||||||
if p == "" {
|
if p == "" {
|
||||||
return nil, fmt.Errorf("empty path specified")
|
return nil, fmt.Errorf("empty path specified")
|
||||||
@@ -1021,11 +1024,17 @@ func (obj *Remotes) Run() {
|
|||||||
}(sshobj, f)
|
}(sshobj, f)
|
||||||
obj.lock.Unlock()
|
obj.lock.Unlock()
|
||||||
}
|
}
|
||||||
|
close(obj.running) // notify
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ready closes its returned channel when the Run method is up and ready. It is
|
||||||
|
// useful to know when ready, since we often execute Run in a go routine.
|
||||||
|
func (obj *Remotes) Ready() <-chan struct{} { return obj.running }
|
||||||
|
|
||||||
// Exit causes as much of the Remotes struct to shutdown as quickly and as
|
// Exit causes as much of the Remotes struct to shutdown as quickly and as
|
||||||
// cleanly as possible. It only returns once everything is shutdown.
|
// cleanly as possible. It only returns once everything is shutdown.
|
||||||
func (obj *Remotes) Exit() error {
|
func (obj *Remotes) Exit() error {
|
||||||
|
<-obj.running // wait for Run to be finished before we exit!
|
||||||
obj.lock.Lock()
|
obj.lock.Lock()
|
||||||
obj.exiting = true // don't spawn new ones once this flag is set!
|
obj.exiting = true // don't spawn new ones once this flag is set!
|
||||||
obj.lock.Unlock()
|
obj.lock.Unlock()
|
||||||
@@ -1078,29 +1087,6 @@ func cleanURL(s string) string {
|
|||||||
return u.Host
|
return u.Host
|
||||||
}
|
}
|
||||||
|
|
||||||
// Semaphore is a counting semaphore.
|
|
||||||
type Semaphore chan struct{}
|
|
||||||
|
|
||||||
// NewSemaphore creates a new semaphore.
|
|
||||||
func NewSemaphore(size int) Semaphore {
|
|
||||||
return make(Semaphore, size)
|
|
||||||
}
|
|
||||||
|
|
||||||
// P acquires n resources.
|
|
||||||
func (s Semaphore) P(n int) {
|
|
||||||
e := struct{}{}
|
|
||||||
for i := 0; i < n; i++ {
|
|
||||||
s <- e // acquire one
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// V releases n resources.
|
|
||||||
func (s Semaphore) V(n int) {
|
|
||||||
for i := 0; i < n; i++ {
|
|
||||||
<-s // release one
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// combinedWriter mimics what the ssh.CombinedOutput command does.
|
// combinedWriter mimics what the ssh.CombinedOutput command does.
|
||||||
type combinedWriter struct {
|
type combinedWriter struct {
|
||||||
b bytes.Buffer
|
b bytes.Buffer
|
||||||
|
|||||||
649
resources/actions.go
Normal file
649
resources/actions.go
Normal file
@@ -0,0 +1,649 @@
|
|||||||
|
// Mgmt
|
||||||
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
|
//
|
||||||
|
// 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package resources
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"math"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/purpleidea/mgmt/event"
|
||||||
|
"github.com/purpleidea/mgmt/pgraph"
|
||||||
|
"github.com/purpleidea/mgmt/prometheus"
|
||||||
|
"github.com/purpleidea/mgmt/util"
|
||||||
|
|
||||||
|
multierr "github.com/hashicorp/go-multierror"
|
||||||
|
errwrap "github.com/pkg/errors"
|
||||||
|
"golang.org/x/time/rate"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SentinelErr is a sentinal as an error type that wraps an arbitrary error.
|
||||||
|
type SentinelErr struct {
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error is the required method to fulfill the error type.
|
||||||
|
func (obj *SentinelErr) Error() string {
|
||||||
|
return obj.err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
// OKTimestamp returns true if this element can run right now?
|
||||||
|
func (obj *BaseRes) OKTimestamp() bool {
|
||||||
|
// these are all the vertices pointing TO v, eg: ??? -> v
|
||||||
|
for _, n := range obj.Graph.IncomingGraphVertices(obj.Vertex) {
|
||||||
|
// if the vertex has a greater timestamp than any pre-req (n)
|
||||||
|
// then we can't run right now...
|
||||||
|
// if they're equal (eg: on init of 0) then we also can't run
|
||||||
|
// b/c we should let our pre-req's go first...
|
||||||
|
x, y := obj.Timestamp(), VtoR(n).Timestamp()
|
||||||
|
if b, ok := obj.Graph.Value("debug"); ok && util.Bool(b) {
|
||||||
|
log.Printf("%s: OKTimestamp: (%v) >= %s(%v): !%v", obj, x, n, y, x >= y)
|
||||||
|
}
|
||||||
|
if x >= y {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Poke tells nodes after me in the dependency graph that they need to refresh.
|
||||||
|
func (obj *BaseRes) Poke() error {
|
||||||
|
// if we're pausing (or exiting) then we should suspend poke's so that
|
||||||
|
// the graph doesn't go on running forever until it's completely done!
|
||||||
|
// this is an optional feature which we can do by default on user exit
|
||||||
|
if obj.Graph.FastPause {
|
||||||
|
return nil // TODO: should this be an error instead?
|
||||||
|
}
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
// these are all the vertices pointing AWAY FROM v, eg: v -> ???
|
||||||
|
for _, n := range obj.Graph.OutgoingGraphVertices(obj.Vertex) {
|
||||||
|
// we can skip this poke if resource hasn't done work yet... it
|
||||||
|
// needs to be poked if already running, or not running though!
|
||||||
|
// TODO: does this need an || activity flag?
|
||||||
|
if VtoR(n).GetState() != ResStateProcess {
|
||||||
|
if b, ok := obj.Graph.Value("debug"); ok && util.Bool(b) {
|
||||||
|
log.Printf("%s: Poke: %s", obj, n)
|
||||||
|
}
|
||||||
|
wg.Add(1)
|
||||||
|
go func(nn pgraph.Vertex) error {
|
||||||
|
defer wg.Done()
|
||||||
|
//edge := obj.Graph.adjacency[v][nn] // lookup
|
||||||
|
//notify := edge.Notify && edge.Refresh()
|
||||||
|
return VtoR(nn).SendEvent(event.EventPoke, nil)
|
||||||
|
}(n)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if b, ok := obj.Graph.Value("debug"); ok && util.Bool(b) {
|
||||||
|
log.Printf("%s: Poke: %s: Skipped!", obj, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: do something with return values?
|
||||||
|
wg.Wait() // wait for all the pokes to complete
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// BackPoke pokes the pre-requisites that are stale and need to run before I can run.
|
||||||
|
func (obj *BaseRes) BackPoke() {
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
// these are all the vertices pointing TO v, eg: ??? -> v
|
||||||
|
for _, n := range obj.Graph.IncomingGraphVertices(obj.Vertex) {
|
||||||
|
x, y, s := obj.Timestamp(), VtoR(n).Timestamp(), VtoR(n).GetState()
|
||||||
|
// If the parent timestamp needs poking AND it's not running
|
||||||
|
// Process, then poke it. If the parent is in ResStateProcess it
|
||||||
|
// means that an event is pending, so we'll be expecting a poke
|
||||||
|
// back soon, so we can safely discard the extra parent poke...
|
||||||
|
// TODO: implement a stateLT (less than) to tell if something
|
||||||
|
// happens earlier in the state cycle and that doesn't wrap nil
|
||||||
|
if x >= y && (s != ResStateProcess && s != ResStateCheckApply) {
|
||||||
|
if b, ok := obj.Graph.Value("debug"); ok && util.Bool(b) {
|
||||||
|
log.Printf("%s: BackPoke: %s", obj, n)
|
||||||
|
}
|
||||||
|
wg.Add(1)
|
||||||
|
go func(nn pgraph.Vertex) error {
|
||||||
|
defer wg.Done()
|
||||||
|
return VtoR(nn).SendEvent(event.EventBackPoke, nil)
|
||||||
|
}(n)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if b, ok := obj.Graph.Value("debug"); ok && util.Bool(b) {
|
||||||
|
log.Printf("%s: BackPoke: %s: Skipped!", obj, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: do something with return values?
|
||||||
|
wg.Wait() // wait for all the pokes to complete
|
||||||
|
}
|
||||||
|
|
||||||
|
// RefreshPending determines if any previous nodes have a refresh pending here.
|
||||||
|
// If this is true, it means I am expected to apply a refresh when I next run.
|
||||||
|
func (obj *BaseRes) RefreshPending() bool {
|
||||||
|
var refresh bool
|
||||||
|
for _, edge := range obj.Graph.IncomingGraphEdges(obj.Vertex) {
|
||||||
|
// if we asked for a notify *and* if one is pending!
|
||||||
|
edge := edge.(*Edge) // panic if wrong
|
||||||
|
if edge.Notify && edge.Refresh() {
|
||||||
|
refresh = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return refresh
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetUpstreamRefresh sets the refresh value to any upstream vertices.
|
||||||
|
func (obj *BaseRes) SetUpstreamRefresh(b bool) {
|
||||||
|
for _, edge := range obj.Graph.IncomingGraphEdges(obj.Vertex) {
|
||||||
|
edge := edge.(*Edge) // panic if wrong
|
||||||
|
if edge.Notify {
|
||||||
|
edge.SetRefresh(b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDownstreamRefresh sets the refresh value to any downstream vertices.
|
||||||
|
func (obj *BaseRes) SetDownstreamRefresh(b bool) {
|
||||||
|
for _, edge := range obj.Graph.OutgoingGraphEdges(obj.Vertex) {
|
||||||
|
edge := edge.(*Edge) // panic if wrong
|
||||||
|
// if we asked for a notify *and* if one is pending!
|
||||||
|
if edge.Notify {
|
||||||
|
edge.SetRefresh(b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process is the primary function to execute for a particular vertex in the graph.
|
||||||
|
func (obj *BaseRes) Process() error {
|
||||||
|
if obj.debug {
|
||||||
|
log.Printf("%s: Process()", obj)
|
||||||
|
}
|
||||||
|
// FIXME: should these SetState methods be here or after the sema code?
|
||||||
|
defer obj.SetState(ResStateNil) // reset state when finished
|
||||||
|
obj.SetState(ResStateProcess)
|
||||||
|
|
||||||
|
// is it okay to run dependency wise right now?
|
||||||
|
// if not, that's okay because when the dependency runs, it will poke
|
||||||
|
// us back and we will run if needed then!
|
||||||
|
if !obj.OKTimestamp() {
|
||||||
|
go obj.BackPoke()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// timestamp must be okay...
|
||||||
|
if b, ok := obj.Graph.Value("debug"); ok && util.Bool(b) {
|
||||||
|
log.Printf("%s: OKTimestamp(%v)", obj, obj.Timestamp())
|
||||||
|
}
|
||||||
|
|
||||||
|
// semaphores!
|
||||||
|
// These shouldn't ever block an exit, since the graph should eventually
|
||||||
|
// converge causing their them to unlock. More interestingly, since they
|
||||||
|
// run in a DAG alphabetically, there is no way to permanently deadlock,
|
||||||
|
// assuming that resources individually don't ever block from finishing!
|
||||||
|
// The exception is that semaphores with a zero count will always block!
|
||||||
|
// TODO: Add a close mechanism to close/unblock zero count semaphores...
|
||||||
|
semas := obj.Meta().Sema
|
||||||
|
if obj.debug && len(semas) > 0 {
|
||||||
|
log.Printf("%s: Sema: P(%s)", obj, strings.Join(semas, ", "))
|
||||||
|
}
|
||||||
|
if err := obj.Graph.SemaLock(semas); err != nil { // lock
|
||||||
|
// NOTE: in practice, this might not ever be truly necessary...
|
||||||
|
return fmt.Errorf("shutdown of semaphores")
|
||||||
|
}
|
||||||
|
defer obj.Graph.SemaUnlock(semas) // unlock
|
||||||
|
if obj.debug && len(semas) > 0 {
|
||||||
|
defer log.Printf("%s: Sema: V(%s)", obj, strings.Join(semas, ", "))
|
||||||
|
}
|
||||||
|
|
||||||
|
var ok = true
|
||||||
|
var applied = false // did we run an apply?
|
||||||
|
|
||||||
|
// connect any senders to receivers and detect if values changed
|
||||||
|
if updated, err := obj.SendRecv(obj.Res); err != nil {
|
||||||
|
return errwrap.Wrapf(err, "could not SendRecv in Process")
|
||||||
|
} else if len(updated) > 0 {
|
||||||
|
for _, changed := range updated {
|
||||||
|
if changed { // at least one was updated
|
||||||
|
obj.StateOK(false) // invalidate cache, mark as dirty
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var noop = obj.Meta().Noop // lookup the noop value
|
||||||
|
var refresh bool
|
||||||
|
var checkOK bool
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if b, ok := obj.Graph.Value("debug"); ok && util.Bool(b) {
|
||||||
|
log.Printf("%s: CheckApply(%t)", obj, !noop)
|
||||||
|
}
|
||||||
|
|
||||||
|
// lookup the refresh (notification) variable
|
||||||
|
refresh = obj.RefreshPending() // do i need to perform a refresh?
|
||||||
|
obj.SetRefresh(refresh) // tell the resource
|
||||||
|
|
||||||
|
// changes can occur after this...
|
||||||
|
obj.SetState(ResStateCheckApply)
|
||||||
|
|
||||||
|
// check cached state, to skip CheckApply; can't skip if refreshing
|
||||||
|
if !refresh && obj.IsStateOK() {
|
||||||
|
checkOK, err = true, nil
|
||||||
|
|
||||||
|
// NOTE: technically this block is wrong because we don't know
|
||||||
|
// if the resource implements refresh! If it doesn't, we could
|
||||||
|
// skip this, but it doesn't make a big difference under noop!
|
||||||
|
} else if noop && refresh { // had a refresh to do w/ noop!
|
||||||
|
checkOK, err = false, nil // therefore the state is wrong
|
||||||
|
|
||||||
|
// run the CheckApply!
|
||||||
|
} else {
|
||||||
|
// if this fails, don't UpdateTimestamp()
|
||||||
|
checkOK, err = obj.Res.CheckApply(!noop)
|
||||||
|
|
||||||
|
if promErr := obj.Data().Prometheus.UpdateCheckApplyTotal(obj.GetKind(), !noop, !checkOK, err != nil); promErr != nil {
|
||||||
|
// TODO: how to error correctly
|
||||||
|
log.Printf("%s: Prometheus.UpdateCheckApplyTotal() errored: %v", obj, err)
|
||||||
|
}
|
||||||
|
// TODO: Can the `Poll` converged timeout tracking be a
|
||||||
|
// more general method for all converged timeouts? this
|
||||||
|
// would simplify the resources by removing boilerplate
|
||||||
|
if obj.Meta().Poll > 0 {
|
||||||
|
if !checkOK { // something changed, restart timer
|
||||||
|
obj.cuid.ResetTimer() // activity!
|
||||||
|
if b, ok := obj.Graph.Value("debug"); ok && util.Bool(b) {
|
||||||
|
log.Printf("%s: Converger: ResetTimer", obj)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if checkOK && err != nil { // should never return this way
|
||||||
|
log.Fatalf("%s: CheckApply(): %t, %+v", obj, checkOK, err)
|
||||||
|
}
|
||||||
|
if b, ok := obj.Graph.Value("debug"); ok && util.Bool(b) {
|
||||||
|
log.Printf("%s: CheckApply(): %t, %v", obj, checkOK, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// if CheckApply ran without noop and without error, state should be good
|
||||||
|
if !noop && err == nil { // aka !noop || checkOK
|
||||||
|
obj.StateOK(true) // reset
|
||||||
|
if refresh {
|
||||||
|
obj.SetUpstreamRefresh(false) // refresh happened, clear the request
|
||||||
|
obj.SetRefresh(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !checkOK { // if state *was* not ok, we had to have apply'ed
|
||||||
|
if err != nil { // error during check or apply
|
||||||
|
ok = false
|
||||||
|
} else {
|
||||||
|
applied = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// when noop is true we always want to update timestamp
|
||||||
|
if noop && err == nil {
|
||||||
|
ok = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if ok {
|
||||||
|
// did we actually do work?
|
||||||
|
activity := applied
|
||||||
|
if noop {
|
||||||
|
activity = false // no we didn't do work...
|
||||||
|
}
|
||||||
|
|
||||||
|
if activity { // add refresh flag to downstream edges...
|
||||||
|
obj.SetDownstreamRefresh(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// update this timestamp *before* we poke or the poked
|
||||||
|
// nodes might fail due to having a too old timestamp!
|
||||||
|
obj.UpdateTimestamp() // this was touched...
|
||||||
|
obj.SetState(ResStatePoking) // can't cancel parent poke
|
||||||
|
if err := obj.Poke(); err != nil {
|
||||||
|
return errwrap.Wrapf(err, "the Poke() failed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// poke at our pre-req's instead since they need to refresh/run...
|
||||||
|
return errwrap.Wrapf(err, "could not Process() successfully")
|
||||||
|
}
|
||||||
|
|
||||||
|
// innerWorker is the CheckApply runner that reads from processChan.
|
||||||
|
func (obj *BaseRes) innerWorker() {
|
||||||
|
running := false
|
||||||
|
done := make(chan struct{})
|
||||||
|
playback := false // do we need to run another one?
|
||||||
|
|
||||||
|
waiting := false
|
||||||
|
var timer = time.NewTimer(time.Duration(math.MaxInt64)) // longest duration
|
||||||
|
if !timer.Stop() {
|
||||||
|
<-timer.C // unnecessary, shouldn't happen
|
||||||
|
}
|
||||||
|
|
||||||
|
var delay = time.Duration(obj.Meta().Delay) * time.Millisecond
|
||||||
|
var retry = obj.Meta().Retry // number of tries left, -1 for infinite
|
||||||
|
var limiter = rate.NewLimiter(obj.Meta().Limit, obj.Meta().Burst)
|
||||||
|
limited := false
|
||||||
|
|
||||||
|
wg := &sync.WaitGroup{} // wait for Process routine to exit
|
||||||
|
|
||||||
|
Loop:
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case ev, ok := <-obj.processChan: // must use like this
|
||||||
|
if !ok { // processChan closed, let's exit
|
||||||
|
break Loop // no event, so no ack!
|
||||||
|
}
|
||||||
|
if obj.Meta().Poll == 0 { // skip for polling
|
||||||
|
obj.wcuid.SetConverged(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// if process started, but no action yet, skip!
|
||||||
|
if obj.GetState() == ResStateProcess {
|
||||||
|
if b, ok := obj.Graph.Value("debug"); ok && util.Bool(b) {
|
||||||
|
log.Printf("%s: Skipped event!", obj)
|
||||||
|
}
|
||||||
|
ev.ACK() // ready for next message
|
||||||
|
obj.quiesceGroup.Done()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// if running, we skip running a new execution!
|
||||||
|
// if waiting, we skip running a new execution!
|
||||||
|
if running || waiting {
|
||||||
|
if b, ok := obj.Graph.Value("debug"); ok && util.Bool(b) {
|
||||||
|
log.Printf("%s: Playback added!", obj)
|
||||||
|
}
|
||||||
|
playback = true
|
||||||
|
ev.ACK() // ready for next message
|
||||||
|
obj.quiesceGroup.Done()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// catch invalid rates
|
||||||
|
if obj.Meta().Burst == 0 && !(obj.Meta().Limit == rate.Inf) { // blocked
|
||||||
|
e := fmt.Errorf("%s: Permanently limited (rate != Inf, burst: 0)", obj)
|
||||||
|
ev.ACK() // ready for next message
|
||||||
|
obj.quiesceGroup.Done()
|
||||||
|
obj.SendEvent(event.EventExit, &SentinelErr{e})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// rate limit
|
||||||
|
// FIXME: consider skipping rate limit check if
|
||||||
|
// the event is a poke instead of a watch event
|
||||||
|
if !limited && !(obj.Meta().Limit == rate.Inf) { // skip over the playback event...
|
||||||
|
now := time.Now()
|
||||||
|
r := limiter.ReserveN(now, 1) // one event
|
||||||
|
// r.OK() seems to always be true here!
|
||||||
|
d := r.DelayFrom(now)
|
||||||
|
if d > 0 { // delay
|
||||||
|
limited = true
|
||||||
|
playback = true
|
||||||
|
log.Printf("%s: Limited (rate: %v/sec, burst: %d, next: %v)", obj, obj.Meta().Limit, obj.Meta().Burst, d)
|
||||||
|
// start the timer...
|
||||||
|
timer.Reset(d)
|
||||||
|
waiting = true // waiting for retry timer
|
||||||
|
ev.ACK()
|
||||||
|
obj.quiesceGroup.Done()
|
||||||
|
continue
|
||||||
|
} // otherwise, we run directly!
|
||||||
|
}
|
||||||
|
limited = false // let one through
|
||||||
|
|
||||||
|
wg.Add(1)
|
||||||
|
running = true
|
||||||
|
go func(ev *event.Event) {
|
||||||
|
obj.pcuid.SetConverged(false) // "block" Process
|
||||||
|
defer wg.Done()
|
||||||
|
if e := obj.Process(); e != nil {
|
||||||
|
playback = true
|
||||||
|
log.Printf("%s: CheckApply errored: %v", obj, e)
|
||||||
|
if retry == 0 {
|
||||||
|
if err := obj.Data().Prometheus.UpdateState(obj.String(), obj.GetKind(), prometheus.ResStateHardFail); err != nil {
|
||||||
|
// TODO: how to error this?
|
||||||
|
log.Printf("%s: Prometheus.UpdateState() errored: %v", obj, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// wrap the error in the sentinel
|
||||||
|
obj.quiesceGroup.Done() // before the Wait that happens in SendEvent!
|
||||||
|
obj.SendEvent(event.EventExit, &SentinelErr{e})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if retry > 0 { // don't decrement the -1
|
||||||
|
retry--
|
||||||
|
}
|
||||||
|
if err := obj.Data().Prometheus.UpdateState(obj.String(), obj.GetKind(), prometheus.ResStateSoftFail); err != nil {
|
||||||
|
// TODO: how to error this?
|
||||||
|
log.Printf("%s: Prometheus.UpdateState() errored: %v", obj, err)
|
||||||
|
}
|
||||||
|
log.Printf("%s: CheckApply: Retrying after %.4f seconds (%d left)", obj, delay.Seconds(), retry)
|
||||||
|
// start the timer...
|
||||||
|
timer.Reset(delay)
|
||||||
|
waiting = true // waiting for retry timer
|
||||||
|
// don't obj.quiesceGroup.Done() b/c
|
||||||
|
// the timer is running and it can exit!
|
||||||
|
return
|
||||||
|
}
|
||||||
|
retry = obj.Meta().Retry // reset on success
|
||||||
|
close(done) // trigger
|
||||||
|
}(ev)
|
||||||
|
ev.ACK() // sync (now mostly useless)
|
||||||
|
|
||||||
|
case <-timer.C:
|
||||||
|
if obj.Meta().Poll == 0 { // skip for polling
|
||||||
|
obj.wcuid.SetConverged(false)
|
||||||
|
}
|
||||||
|
waiting = false
|
||||||
|
if !timer.Stop() {
|
||||||
|
//<-timer.C // blocks, docs are wrong!
|
||||||
|
}
|
||||||
|
log.Printf("%s: CheckApply delay expired!", obj)
|
||||||
|
close(done)
|
||||||
|
|
||||||
|
// a CheckApply run (with possibly retry pause) finished
|
||||||
|
case <-done:
|
||||||
|
if obj.Meta().Poll == 0 { // skip for polling
|
||||||
|
obj.wcuid.SetConverged(false)
|
||||||
|
}
|
||||||
|
if b, ok := obj.Graph.Value("debug"); ok && util.Bool(b) {
|
||||||
|
log.Printf("%s: CheckApply finished!", obj)
|
||||||
|
}
|
||||||
|
done = make(chan struct{}) // reset
|
||||||
|
// re-send this event, to trigger a CheckApply()
|
||||||
|
if playback {
|
||||||
|
// this lock avoids us sending to
|
||||||
|
// channel after we've closed it!
|
||||||
|
// TODO: can this experience indefinite postponement ?
|
||||||
|
// see: https://github.com/golang/go/issues/11506
|
||||||
|
// pause or exit is in process if not quiescing!
|
||||||
|
if !obj.quiescing {
|
||||||
|
playback = false
|
||||||
|
obj.quiesceGroup.Add(1) // lock around it, b/c still running...
|
||||||
|
go func() {
|
||||||
|
obj.Event() // replay a new event
|
||||||
|
obj.quiesceGroup.Done()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
running = false
|
||||||
|
obj.pcuid.SetConverged(true) // "unblock" Process
|
||||||
|
obj.quiesceGroup.Done()
|
||||||
|
|
||||||
|
case <-obj.wcuid.ConvergedTimer():
|
||||||
|
obj.wcuid.SetConverged(true) // converged!
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Worker is the common run frontend of the vertex. It handles all of the retry
|
||||||
|
// and retry delay common code, and ultimately returns the final status of this
|
||||||
|
// vertex execution.
|
||||||
|
func (obj *BaseRes) Worker() error {
|
||||||
|
// listen for chan events from Watch() and run
|
||||||
|
// the Process() function when they're received
|
||||||
|
// this avoids us having to pass the data into
|
||||||
|
// the Watch() function about which graph it is
|
||||||
|
// running on, which isolates things nicely...
|
||||||
|
if obj.debug {
|
||||||
|
log.Printf("%s: Worker: Running", obj)
|
||||||
|
defer log.Printf("%s: Worker: Stopped", obj)
|
||||||
|
}
|
||||||
|
// run the init (should match 1-1 with Close function)
|
||||||
|
if err := obj.Res.Init(); err != nil {
|
||||||
|
obj.ProcessExit()
|
||||||
|
// always exit the worker function by finishing with Close()
|
||||||
|
if e := obj.Res.Close(); e != nil {
|
||||||
|
err = multierr.Append(err, e) // list of errors
|
||||||
|
}
|
||||||
|
return errwrap.Wrapf(err, "could not Init() resource")
|
||||||
|
}
|
||||||
|
|
||||||
|
// if the CheckApply run takes longer than the converged
|
||||||
|
// timeout, we could inappropriately converge mid-apply!
|
||||||
|
// avoid this by blocking convergence with a fake report
|
||||||
|
// we also add a similar blocker around the worker loop!
|
||||||
|
// XXX: put these in Init() ?
|
||||||
|
// get extra cuids (worker, process)
|
||||||
|
obj.wcuid.SetConverged(true) // starts off false, and waits for loop timeout
|
||||||
|
obj.pcuid.SetConverged(true) // starts off true, because it's not running...
|
||||||
|
|
||||||
|
obj.processSync.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer obj.processSync.Done()
|
||||||
|
obj.innerWorker()
|
||||||
|
}()
|
||||||
|
|
||||||
|
var err error // propagate the error up (this is a permanent BAD error!)
|
||||||
|
// the watch delay runs inside of the Watch resource loop, so that it
|
||||||
|
// can still process signals and exit if needed. It shouldn't run any
|
||||||
|
// resource specific code since this is supposed to be a retry delay.
|
||||||
|
// NOTE: we're using the same retry and delay metaparams that CheckApply
|
||||||
|
// uses. This is for practicality. We can separate them later if needed!
|
||||||
|
var watchDelay time.Duration
|
||||||
|
var watchRetry = obj.Meta().Retry // number of tries left, -1 for infinite
|
||||||
|
// watch blocks until it ends, & errors to retry
|
||||||
|
for {
|
||||||
|
// TODO: do we have to stop the converged-timeout when in this block (perhaps we're in the delay block!)
|
||||||
|
// TODO: should we setup/manage some of the converged timeout stuff in here anyways?
|
||||||
|
|
||||||
|
// if a retry-delay was requested, wait, but don't block our events!
|
||||||
|
if watchDelay > 0 {
|
||||||
|
//var pendingSendEvent bool
|
||||||
|
timer := time.NewTimer(watchDelay)
|
||||||
|
Loop:
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-timer.C: // the wait is over
|
||||||
|
break Loop // critical
|
||||||
|
|
||||||
|
// TODO: resources could have a separate exit channel to avoid this complexity!?
|
||||||
|
case event := <-obj.Events():
|
||||||
|
// NOTE: this code should match the similar Res code!
|
||||||
|
//cuid.SetConverged(false) // TODO: ?
|
||||||
|
if exit, send := obj.ReadEvent(event); exit != nil {
|
||||||
|
obj.ProcessExit()
|
||||||
|
err := *exit // exit err
|
||||||
|
if e := obj.Res.Close(); err == nil {
|
||||||
|
err = e
|
||||||
|
} else if e != nil {
|
||||||
|
err = multierr.Append(err, e) // list of errors
|
||||||
|
}
|
||||||
|
return err // exit
|
||||||
|
} else if send {
|
||||||
|
// if we dive down this rabbit hole, our
|
||||||
|
// timer.C won't get seen until we get out!
|
||||||
|
// in this situation, the Watch() is blocked
|
||||||
|
// from performing until CheckApply returns
|
||||||
|
// successfully, or errors out. This isn't
|
||||||
|
// so bad, but we should document it. Is it
|
||||||
|
// possible that some resource *needs* Watch
|
||||||
|
// to run to be able to execute a CheckApply?
|
||||||
|
// That situation shouldn't be common, and
|
||||||
|
// should probably not be allowed. Can we
|
||||||
|
// avoid it though?
|
||||||
|
//if exit, err := doSend(); exit || err != nil {
|
||||||
|
// return err // we exit or bubble up a NACK...
|
||||||
|
//}
|
||||||
|
// Instead of doing the above, we can
|
||||||
|
// add events to a pending list, and
|
||||||
|
// when we finish the delay, we can run
|
||||||
|
// them.
|
||||||
|
//pendingSendEvent = true // all events are identical for now...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
timer.Stop() // it's nice to cleanup
|
||||||
|
log.Printf("%s: Watch delay expired!", obj)
|
||||||
|
// NOTE: we can avoid the send if running Watch guarantees
|
||||||
|
// one CheckApply event on startup!
|
||||||
|
//if pendingSendEvent { // TODO: should this become a list in the future?
|
||||||
|
// if err := obj.Event() err != nil {
|
||||||
|
// return err // we exit or bubble up a NACK...
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: reset the watch retry count after some amount of success
|
||||||
|
var e error
|
||||||
|
if obj.Meta().Poll > 0 { // poll instead of watching :(
|
||||||
|
obj.cuid.StartTimer()
|
||||||
|
e = obj.Poll()
|
||||||
|
obj.cuid.StopTimer() // clean up nicely
|
||||||
|
} else {
|
||||||
|
e = obj.Res.Watch() // run the watch normally
|
||||||
|
}
|
||||||
|
if e == nil { // exit signal
|
||||||
|
err = nil // clean exit
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if sentinelErr, ok := e.(*SentinelErr); ok { // unwrap the sentinel
|
||||||
|
err = sentinelErr.err
|
||||||
|
break // sentinel means, perma-exit
|
||||||
|
}
|
||||||
|
log.Printf("%s: Watch errored: %v", obj, e)
|
||||||
|
if watchRetry == 0 {
|
||||||
|
err = fmt.Errorf("Permanent watch error: %v", e)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if watchRetry > 0 { // don't decrement the -1
|
||||||
|
watchRetry--
|
||||||
|
}
|
||||||
|
watchDelay = time.Duration(obj.Meta().Delay) * time.Millisecond
|
||||||
|
log.Printf("%s: Watch: Retrying after %.4f seconds (%d left)", obj, watchDelay.Seconds(), watchRetry)
|
||||||
|
// We need to trigger a CheckApply after Watch restarts, so that
|
||||||
|
// we catch any lost events that happened while down. We do this
|
||||||
|
// by getting the Watch resource to send one event once it's up!
|
||||||
|
//v.SendEvent(eventPoke, false, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
obj.ProcessExit()
|
||||||
|
// close resource and return possible errors if any
|
||||||
|
if e := obj.Res.Close(); err == nil {
|
||||||
|
err = e
|
||||||
|
} else if e != nil {
|
||||||
|
err = multierr.Append(err, e) // list of errors
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
298
resources/augeas.go
Normal file
298
resources/augeas.go
Normal file
@@ -0,0 +1,298 @@
|
|||||||
|
// Mgmt
|
||||||
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
|
//
|
||||||
|
// 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
// +build !noaugeas
|
||||||
|
|
||||||
|
package resources
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/purpleidea/mgmt/recwatch"
|
||||||
|
|
||||||
|
errwrap "github.com/pkg/errors"
|
||||||
|
// FIXME: we vendor go/augeas because master requires augeas 1.6.0
|
||||||
|
// and libaugeas-dev-1.6.0 is not yet available in a PPA.
|
||||||
|
"honnef.co/go/augeas"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// NS is a namespace for augeas operations
|
||||||
|
NS = "Xmgmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterResource("augeas", func() Res { return &AugeasRes{} })
|
||||||
|
}
|
||||||
|
|
||||||
|
// AugeasRes is a resource that enables you to use the augeas resource.
|
||||||
|
// Currently only allows you to change simple files (e.g sshd_config).
|
||||||
|
type AugeasRes struct {
|
||||||
|
BaseRes `yaml:",inline"`
|
||||||
|
|
||||||
|
// File is the path to the file targeted by this resource.
|
||||||
|
File string `yaml:"file"`
|
||||||
|
|
||||||
|
// Lens is the lens used by this resource. If specified, mgmt
|
||||||
|
// will lower the augeas overhead by only loading that lens.
|
||||||
|
Lens string `yaml:"lens"`
|
||||||
|
|
||||||
|
// Sets is a list of changes that will be applied to the file, in the form of
|
||||||
|
// ["path", "value"]. mgmt will run augeas.Get() before augeas.Set(), to
|
||||||
|
// prevent changing the file when it is not needed.
|
||||||
|
Sets []AugeasSet `yaml:"sets"`
|
||||||
|
|
||||||
|
recWatcher *recwatch.RecWatcher // used to watch the changed files
|
||||||
|
}
|
||||||
|
|
||||||
|
// AugeasSet represents a key/value pair of settings to be applied.
|
||||||
|
type AugeasSet struct {
|
||||||
|
Path string `yaml:"path"` // The relative path to the value to be changed.
|
||||||
|
Value string `yaml:"value"` // The value to be set on the given Path.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default returns some sensible defaults for this resource.
|
||||||
|
func (obj *AugeasRes) Default() Res {
|
||||||
|
return &AugeasRes{
|
||||||
|
BaseRes: BaseRes{
|
||||||
|
MetaParams: DefaultMetaParams, // force a default
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate if the params passed in are valid data.
|
||||||
|
func (obj *AugeasRes) Validate() error {
|
||||||
|
if !strings.HasPrefix(obj.File, "/") {
|
||||||
|
return fmt.Errorf("the File param should start with a slash")
|
||||||
|
}
|
||||||
|
if obj.Lens != "" && !strings.HasSuffix(obj.Lens, ".lns") {
|
||||||
|
return fmt.Errorf("the Lens param should have a .lns suffix")
|
||||||
|
}
|
||||||
|
if (obj.Lens == "") != (obj.File == "") {
|
||||||
|
return fmt.Errorf("the File and Lens params must be specified together")
|
||||||
|
}
|
||||||
|
return obj.BaseRes.Validate()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init initiates the resource.
|
||||||
|
func (obj *AugeasRes) Init() error {
|
||||||
|
return obj.BaseRes.Init() // call base init, b/c we're overriding
|
||||||
|
}
|
||||||
|
|
||||||
|
// Watch is the primary listener for this resource and it outputs events.
|
||||||
|
// Taken from the File resource.
|
||||||
|
// FIXME: DRY - This is taken from the file resource
|
||||||
|
func (obj *AugeasRes) Watch() error {
|
||||||
|
var err error
|
||||||
|
obj.recWatcher, err = recwatch.NewRecWatcher(obj.File, false)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer obj.recWatcher.Close()
|
||||||
|
|
||||||
|
// notify engine that we're running
|
||||||
|
if err := obj.Running(); err != nil {
|
||||||
|
return err // bubble up a NACK...
|
||||||
|
}
|
||||||
|
|
||||||
|
var send = false // send event?
|
||||||
|
var exit *error
|
||||||
|
|
||||||
|
for {
|
||||||
|
if obj.debug {
|
||||||
|
log.Printf("%s: Watching: %s", obj, obj.File) // attempting to watch...
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case event, ok := <-obj.recWatcher.Events():
|
||||||
|
if !ok { // channel shutdown
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err := event.Error; err != nil {
|
||||||
|
return errwrap.Wrapf(err, "Unknown %s watcher error", obj)
|
||||||
|
}
|
||||||
|
if obj.debug { // don't access event.Body if event.Error isn't nil
|
||||||
|
log.Printf("%s: Event(%s): %v", obj, event.Body.Name, event.Body.Op)
|
||||||
|
}
|
||||||
|
send = true
|
||||||
|
obj.StateOK(false) // dirty
|
||||||
|
|
||||||
|
case event := <-obj.Events():
|
||||||
|
if exit, send = obj.ReadEvent(event); exit != nil {
|
||||||
|
return *exit // exit
|
||||||
|
}
|
||||||
|
//obj.StateOK(false) // dirty // these events don't invalidate state
|
||||||
|
}
|
||||||
|
|
||||||
|
// do all our event sending all together to avoid duplicate msgs
|
||||||
|
if send {
|
||||||
|
send = false
|
||||||
|
obj.Event()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkApplySet runs CheckApply for one element of the AugeasRes.Set
|
||||||
|
func (obj *AugeasRes) checkApplySet(apply bool, ag *augeas.Augeas, set AugeasSet) (bool, error) {
|
||||||
|
fullpath := fmt.Sprintf("/files/%v/%v", obj.File, set.Path)
|
||||||
|
|
||||||
|
// We do not check for errors because errors are also thrown when
|
||||||
|
// the path does not exist.
|
||||||
|
if getValue, _ := ag.Get(fullpath); set.Value == getValue {
|
||||||
|
// The value is what we expect, return directly
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if !apply {
|
||||||
|
// If noop, we can return here directly. We return with
|
||||||
|
// nil even if err is not nil because it does not mean
|
||||||
|
// there is an error.
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := ag.Set(fullpath, set.Value); err != nil {
|
||||||
|
return false, errwrap.Wrapf(err, "augeas: error while setting value")
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CheckApply method for Augeas resource.
|
||||||
|
func (obj *AugeasRes) CheckApply(apply bool) (bool, error) {
|
||||||
|
log.Printf("%s: CheckApply: %s", obj, obj.File)
|
||||||
|
// By default we do not set any option to augeas, we use the defaults.
|
||||||
|
opts := augeas.None
|
||||||
|
if obj.Lens != "" {
|
||||||
|
// if the lens is specified, we can speed up augeas by not
|
||||||
|
// loading everything. Without this option, augeas will try to
|
||||||
|
// read all the files it knows in the complete filesystem.
|
||||||
|
// e.g. to change /etc/ssh/sshd_config, it would load /etc/hosts, /etc/ntpd.conf, etc...
|
||||||
|
opts = augeas.NoModlAutoload
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initiate augeas
|
||||||
|
ag, err := augeas.New("/", "", opts)
|
||||||
|
if err != nil {
|
||||||
|
return false, errwrap.Wrapf(err, "augeas: error while initializing")
|
||||||
|
}
|
||||||
|
defer ag.Close()
|
||||||
|
|
||||||
|
if obj.Lens != "" {
|
||||||
|
// If the lens is set, load the lens for the file we want to edit.
|
||||||
|
// We pick Xmgmt, as this name will not collide with any other lens name.
|
||||||
|
// We do not pick Mgmt as in the future there might be an Mgmt lens.
|
||||||
|
// https://github.com/hercules-team/augeas/wiki/Loading-specific-files
|
||||||
|
if err = ag.Set(fmt.Sprintf("/augeas/load/%s/lens", NS), obj.Lens); err != nil {
|
||||||
|
return false, errwrap.Wrapf(err, "augeas: error while initializing lens")
|
||||||
|
}
|
||||||
|
if err = ag.Set(fmt.Sprintf("/augeas/load/%s/incl", NS), obj.File); err != nil {
|
||||||
|
return false, errwrap.Wrapf(err, "augeas: error while initializing incl")
|
||||||
|
}
|
||||||
|
if err = ag.Load(); err != nil {
|
||||||
|
return false, errwrap.Wrapf(err, "augeas: error while loading")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
checkOK := true
|
||||||
|
for _, set := range obj.Sets {
|
||||||
|
if setCheckOK, err := obj.checkApplySet(apply, &ag, set); err != nil {
|
||||||
|
return false, errwrap.Wrapf(err, "augeas: error during CheckApply of one Set")
|
||||||
|
} else if !setCheckOK {
|
||||||
|
checkOK = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the state is correct or we can't apply, return early.
|
||||||
|
if checkOK || !apply {
|
||||||
|
return checkOK, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("%s: changes needed, saving", obj)
|
||||||
|
if err = ag.Save(); err != nil {
|
||||||
|
return false, errwrap.Wrapf(err, "augeas: error while saving augeas values")
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: Workaround for https://github.com/dominikh/go-augeas/issues/13
|
||||||
|
// To be fixed upstream.
|
||||||
|
if obj.File != "" {
|
||||||
|
if _, err := os.Stat(obj.File); os.IsNotExist(err) {
|
||||||
|
return false, errwrap.Wrapf(err, "augeas: error: file does not exist")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AugeasUID is the UID struct for AugeasRes.
|
||||||
|
type AugeasUID struct {
|
||||||
|
BaseUID
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
// UIDs includes all params to make a unique identification of this object.
|
||||||
|
func (obj *AugeasRes) UIDs() []ResUID {
|
||||||
|
x := &AugeasUID{
|
||||||
|
BaseUID: BaseUID{Name: obj.GetName(), Kind: obj.GetKind()},
|
||||||
|
name: obj.Name,
|
||||||
|
}
|
||||||
|
return []ResUID{x}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GroupCmp returns whether two resources can be grouped together or not.
|
||||||
|
func (obj *AugeasRes) GroupCmp(r Res) bool {
|
||||||
|
return false // Augeas commands can not be grouped together.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compare two resources and return if they are equivalent.
|
||||||
|
func (obj *AugeasRes) Compare(r Res) bool {
|
||||||
|
// we can only compare AugeasRes to others of the same resource kind
|
||||||
|
res, ok := r.(*AugeasRes)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !obj.BaseRes.Compare(res) { // call base Compare
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if obj.Name != res.Name {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalYAML is the custom unmarshal handler for this struct.
|
||||||
|
// It is primarily useful for setting the defaults.
|
||||||
|
func (obj *AugeasRes) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||||
|
type rawRes AugeasRes // indirection to avoid infinite recursion
|
||||||
|
|
||||||
|
def := obj.Default() // get the default
|
||||||
|
res, ok := def.(*AugeasRes) // put in the right format
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("could not convert to AugeasRes")
|
||||||
|
}
|
||||||
|
raw := rawRes(*res) // convert; the defaults go here
|
||||||
|
|
||||||
|
if err := unmarshal(&raw); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
*obj = AugeasRes(raw) // restore from indirection with type conversion!
|
||||||
|
return nil
|
||||||
|
}
|
||||||
25
resources/augeas_disabled.go
Normal file
25
resources/augeas_disabled.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
// Mgmt
|
||||||
|
// Copyright (C) 2013-2017+ James Shubin and the project contributors
|
||||||
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
||||||
|
//
|
||||||
|
// 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
// +build noaugeas
|
||||||
|
|
||||||
|
package resources
|
||||||
|
|
||||||
|
// AugeasRes represents the fields of the Augeas resource. Since this file is
|
||||||
|
// only invoked with the tag "noaugeas", we do not need any fields here.
|
||||||
|
type AugeasRes struct {
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user