lang: Add module imports and more

This enables imports in mcl code, and is one of last remaining blockers
to using mgmt. Now we can start writing standalone modules, and adding
standard library functions as needed. There's still lots to do, but this
was a big missing piece. It was much harder to get right than I had
expected, but I think it's solid!

This unfortunately large commit is the result of some wild hacking I've
been doing for the past little while. It's the result of a rebase that
broke many "wip" commits that tracked my private progress, into
something that's not gratuitously messy for our git logs. Since this was
a learning and discovery process for me, I've "erased" the confusing git
history that wouldn't have helped. I'm happy to discuss the dead-ends,
and a small portion of that code was even left in for possible future
use.

This patch includes:

* A change to the cli interface:
You now specify the front-end explicitly, instead of leaving it up to
the front-end to decide when to "activate". For example, instead of:

mgmt run --lang code.mcl

we now do:

mgmt run lang --lang code.mcl

We might rename the --lang flag in the future to avoid the awkward word
repetition. Suggestions welcome, but I'm considering "input". One
side-effect of this change, is that flags which are "engine" specific
now must be specified with "run" before the front-end name. Eg:

mgmt run --tmp-prefix lang --lang code.mcl

instead of putting --tmp-prefix at the end. We also changed the GAPI
slightly, but I've patched all code that used it. This also makes things
consistent with the "deploy" command.

* The deploys are more robust and let you deploy after a run
This has been vastly improved and let's mgmt really run as a smart
engine that can handle different workloads. If you don't want to deploy
when you've started with `run` or if one comes in, you can use the
--no-watch-deploy option to block new deploys.

* The import statement exists and works!
We now have a working `import` statement. Read the docs, and try it out.
I think it's quite elegant how it fits in with `SetScope`. Have a look.
As a result, we now have some built-in functions available in modules.
This also adds the metadata.yaml entry-point for all modules. Have a
look at the examples or the tests. The bulk of the patch is to support
this.

* Improved lang input parsing code:
I re-wrote the parsing that determined what ran when we passed different
things to --lang. Deciding between running an mcl file or raw code is
now handled in a more intelligent, and re-usable way. See the inputs.go
file if you want to have a look. One casualty is that you can't stream
code from stdin *directly* to the front-end, it's encapsulated into a
deploy first. You can still use stdin though! I doubt anyone will notice
this change.

* The scope was extended to include functions and classes:
Go forth and import lovely code. All these exist in scopes now, and can
be re-used!

* Function calls actually use the scope now. Glad I got this sorted out.

* There is import cycle detection for modules!
Yes, this is another dag. I think that's #4. I guess they're useful.

* A ton of tests and new test infra was added!
This should make it much easier to add new tests that run mcl code. Have
a look at TestAstFunc1 to see how to add more of these.

As usual, I'll try to keep these commits smaller in the future!
This commit is contained in:
James Shubin
2018-11-22 16:48:10 -05:00
parent 948a3c6d08
commit 96dccca475
146 changed files with 5301 additions and 1112 deletions

View File

@@ -32,7 +32,7 @@
- iptables -F
- cd /vagrant/mgmt/ && make path
- cd /vagrant/mgmt/ && make deps && make build && cp mgmt ~/bin/
- cd && mgmt run --yaml /vagrant/mgmt/examples/pkg1.yaml --converged-timeout=5
- cd && mgmt run --converged-timeout=5 yaml --yaml /vagrant/mgmt/examples/pkg1.yaml
:namespace: omv
:count: 0
:username: ''

View File

@@ -33,7 +33,7 @@
- iptables -F
- cd /vagrant/mgmt/ && make path
- cd /vagrant/mgmt/ && make deps && make build && cp mgmt ~/bin/
- cd && mgmt run --yaml /vagrant/mgmt/examples/pkg1.yaml --converged-timeout=5
- cd && mgmt run --converged-timeout=5 yaml --yaml /vagrant/mgmt/examples/pkg1.yaml
:namespace: omv
:count: 0
:username: ''

View File

@@ -10,7 +10,7 @@ mkdir -p "${MGMT_TMPDIR}"
> "${MGMT_TMPDIR}"sshd_config
# run empty graph, with prometheus support
$timeout --kill-after=60s 55s "$MGMT" run --tmp-prefix --yaml=augeas-1.yaml &
$timeout --kill-after=60s 55s "$MGMT" run --tmp-prefix yaml --yaml=augeas-1.yaml &
pid=$!
# kill server on error

View File

@@ -14,7 +14,7 @@ if [[ ! "$tmpdir" =~ "/tmp" ]]; then
exit 99
fi
env TMPDIR="${tmpdir}" TEST=123 EMPTY="" $timeout -sKILL 60s "$MGMT" run --tmp-prefix --converged-timeout=5 --lang env0.mcl
env TMPDIR="${tmpdir}" TEST=123 EMPTY="" $timeout -sKILL 60s "$MGMT" run --tmp-prefix --converged-timeout=5 lang --lang env0.mcl
e=$?
egrep "$regex" "$tmpdir/environ" || fail_test "Could not match '$(cat "$tmpdir/environ")' in '$tmpdir/environ' to '$regex'."

View File

@@ -3,7 +3,7 @@
# should take a few seconds plus converged timeout, and test we don't hang!
# TODO: should we return a different exit code if the resources fail?
# TODO: should we be converged if one of the resources has permanently failed?
$timeout --kill-after=120s 75s "$MGMT" run --yaml exec-fail.yaml --converged-timeout=15 --no-watch --no-pgp --tmp-prefix &
$timeout --kill-after=120s 75s "$MGMT" run --converged-timeout=15 --no-watch --no-pgp --tmp-prefix yaml --yaml exec-fail.yaml &
pid=$!
wait $pid # get exit status
exit $?

View File

@@ -38,7 +38,7 @@ function run_usergroup_test() {
setup
# run till completion
sudo -A timeout --kill-after=45s 40s "$MGMT" run --yaml ./exec-usergroup/${graph} --converged-timeout=15 --no-watch --tmp-prefix &
sudo -A timeout --kill-after=45s 40s "$MGMT" run --converged-timeout=15 --no-watch --tmp-prefix yaml --yaml ./exec-usergroup/${graph} &
pid=$!
wait $pid # get exit status
e=$?

View File

@@ -5,7 +5,7 @@ set -x
. ../util.sh
# run till completion
$timeout --kill-after=60s 55s "$MGMT" run --yaml file-mode.yaml --converged-timeout=5 --no-watch --tmp-prefix &
$timeout --kill-after=60s 55s "$MGMT" run --converged-timeout=5 --no-watch --tmp-prefix yaml --yaml file-mode.yaml &
pid=$!
wait $pid # get exit status
e=$?

View File

@@ -7,7 +7,7 @@ exit 0
mkdir -p /tmp/mgmt/a/b/c/
# run empty graph, with prometheus support
$timeout --kill-after=60s 55s "$MGMT" run --tmp-prefix --yaml=examples/deep-dirs.yaml &
$timeout --kill-after=60s 55s "$MGMT" run --tmp-prefix yaml --yaml=examples/deep-dirs.yaml &
pid=$!
sleep 10s # let it converge

View File

@@ -4,7 +4,7 @@ mkdir -p /tmp/mgmt/
rm /tmp/mgmt/f1 || true
# run empty graph, with prometheus support
$timeout --kill-after=60s 55s "$MGMT" run --tmp-prefix --yaml=file-move.yaml 2>&1 | tee /tmp/mgmt/file-move.log &
$timeout --kill-after=60s 55s "$MGMT" run --tmp-prefix yaml --yaml=file-move.yaml 2>&1 | tee /tmp/mgmt/file-move.log &
pid=$!
sleep 5s # let it converge

View File

@@ -9,7 +9,7 @@ if ! timeout 1s sudo -A true; then
fi
# run till completion
$timeout --kill-after=30s 25s sudo -A "$MGMT" run --yaml file-owner.yaml --converged-timeout=5 --no-watch --tmp-prefix &
$timeout --kill-after=30s 25s sudo -A "$MGMT" run --converged-timeout=5 --no-watch --tmp-prefix yaml --yaml file-owner.yaml &
pid=$!
wait $pid # get exit status
e=$?

View File

@@ -9,7 +9,7 @@ if ! timeout 1s sudo -A true; then
fi
# run till completion
$timeout --kill-after=30s 25s sudo -A "$MGMT" run --lang file-source.mcl --converged-timeout=5 --no-watch --tmp-prefix &
$timeout --kill-after=30s 25s sudo -A "$MGMT" run --converged-timeout=5 --no-watch --tmp-prefix lang --lang file-source.mcl &
pid=$!
wait $pid # get exit status
e=$?

View File

@@ -3,7 +3,7 @@
# should take 15 seconds for longest resources plus startup time to shutdown
# we don't want the ^C to allow the rest of the graph to continue executing!
# this is a test of mgmt exiting quickly via a fast pause after it sees a ^C
$timeout --kill-after=60s 55s "$MGMT" run --yaml graph-exit1.yaml --no-watch --no-pgp --tmp-prefix &
$timeout --kill-after=60s 55s "$MGMT" run --no-watch --no-pgp --tmp-prefix yaml --yaml graph-exit1.yaml &
pid=$!
sleep 5s # let the initial resources start to run...
killall -SIGINT mgmt # send ^C to exit mgmt

View File

@@ -2,7 +2,7 @@
# should take 15 seconds for longest resources plus startup time to shutdown
# we don't want the ^C to allow the rest of the graph to continue executing!
$timeout --kill-after=65s 55s "$MGMT" run --yaml graph-exit2.yaml --no-watch --no-pgp --tmp-prefix &
$timeout --kill-after=65s 55s "$MGMT" run --no-watch --no-pgp --tmp-prefix yaml --yaml graph-exit2.yaml &
pid=$!
sleep 10s # let the initial resources start to run...
killall -SIGINT mgmt # send ^C to exit mgmt

View File

@@ -1,7 +1,7 @@
#!/bin/bash -e
# should take more than 25s plus overhead
$timeout --kill-after=50s 45s "$MGMT" run --yaml graph-fanin-1.yaml --converged-timeout=5 --no-watch --tmp-prefix --no-pgp &
$timeout --kill-after=50s 45s "$MGMT" run --converged-timeout=5 --no-watch --tmp-prefix --no-pgp yaml --yaml graph-fanin-1.yaml &
pid=$!
wait $pid # get exit status
exit $?

View File

@@ -40,7 +40,7 @@ file "${tmpdir}/loadavg" {
}
EOF
$timeout --kill-after=60s 55s "$MGMT" run --tmp-prefix --converged-timeout=1 --lang "$tmpdir/load0.mcl" &
$timeout --kill-after=60s 55s "$MGMT" run --tmp-prefix --converged-timeout=1 lang --lang "$tmpdir/load0.mcl" &
pid=$!
wait $pid # get exit status
e=$?

View File

@@ -44,7 +44,7 @@ sudo mkdir -p /etc/systemd/network
sudo ip link add $IFACE type dummy || true
# run mgmt net res with $IFACE and $ADDR set as above
sudo -A $timeout --kill-after=55s 50s "$MGMT" run --lang ./net0.mcl --converged-timeout=5 --tmp-prefix &
sudo -A $timeout --kill-after=55s 50s "$MGMT" run --converged-timeout=5 --tmp-prefix lang --lang ./net0.mcl &
pid1=$!
# give the engine time to start up
@@ -85,7 +85,7 @@ wait $pid1
e1=$?
# run mgmt net res with $IFACE state => "down"
sudo -A $timeout --kill-after=20 15s "$MGMT" run --lang ./net1.mcl --converged-timeout=5 --tmp-prefix &
sudo -A $timeout --kill-after=20 15s "$MGMT" run --converged-timeout=5 --tmp-prefix lang --lang ./net1.mcl &
# give the engine time to start up
sleep 5

View File

@@ -1,7 +1,7 @@
#!/bin/bash -e
# run empty graph, with prometheus support
$timeout --kill-after=60s 55s "$MGMT" run --tmp-prefix --prometheus &
$timeout --kill-after=60s 55s "$MGMT" run --tmp-prefix --prometheus empty &
pid=$!
sleep 5s # let it converge

View File

@@ -1,7 +1,7 @@
#!/bin/bash -e
# run empty graph, with prometheus support
$timeout --kill-after=60s 55s "$MGMT" run --tmp-prefix --prometheus --prometheus-listen :52637 &
$timeout --kill-after=60s 55s "$MGMT" run --tmp-prefix --prometheus --prometheus-listen :52637 empty &
pid=$!
sleep 5s # let it converge

View File

@@ -3,7 +3,7 @@
exit 0 # XXX: temporarily disabled until prometheus is added back post refactor
# run a graph, with prometheus support
$timeout --kill-after=60s 55s "$MGMT" run --tmp-prefix --no-pgp --prometheus --yaml prometheus-3.yaml &
$timeout --kill-after=60s 55s "$MGMT" run --tmp-prefix --no-pgp --prometheus yaml --yaml prometheus-3.yaml &
pid=$!
sleep 10s # let it converge

View File

@@ -3,7 +3,7 @@
exit 0 # XXX: temporarily disabled until prometheus is added back post refactor
# run a graph, with prometheus support
$timeout --kill-after=60s 55s "$MGMT" run --tmp-prefix --no-pgp --prometheus --yaml prometheus-4.yaml &
$timeout --kill-after=60s 55s "$MGMT" run --tmp-prefix --no-pgp --prometheus yaml --yaml prometheus-4.yaml &
pid=$!
sleep 15s # let it converge

View File

@@ -2,7 +2,7 @@
# should take at least 55s, but fail if we block this
# TODO: it would be nice to make sure this test doesn't exit too early!
$timeout --kill-after=120s 110s "$MGMT" run --yaml sema-1.yaml --sema 2 --converged-timeout=5 --no-watch --no-pgp --tmp-prefix &
$timeout --kill-after=120s 110s "$MGMT" run --sema 2 --converged-timeout=5 --no-watch --no-pgp --tmp-prefix yaml --yaml sema-1.yaml &
pid=$!
wait $pid # get exit status
exit $?

View File

@@ -7,7 +7,7 @@ if env | grep -q -e '^TRAVIS=true$'; then
fi
# run till completion
$timeout --kill-after=15s 10s "$MGMT" run --yaml t2.yaml --converged-timeout=5 --no-watch --tmp-prefix &
$timeout --kill-after=15s 10s "$MGMT" run --converged-timeout=5 --no-watch --tmp-prefix yaml --yaml t2.yaml &
pid=$!
wait $pid # get exit status
e=$?

View File

@@ -10,11 +10,11 @@ fi
mkdir -p "${MGMT_TMPDIR}"mgmt{A..C}
# run till completion
$timeout --kill-after=15s 10s "$MGMT" run --yaml t3-a.yaml --converged-timeout=5 --no-watch --tmp-prefix &
$timeout --kill-after=15s 10s "$MGMT" run --converged-timeout=5 --no-watch --tmp-prefix yaml --yaml t3-a.yaml &
pid1=$!
$timeout --kill-after=15s 10s "$MGMT" run --yaml t3-b.yaml --converged-timeout=5 --no-watch --tmp-prefix &
$timeout --kill-after=15s 10s "$MGMT" run --converged-timeout=5 --no-watch --tmp-prefix yaml --yaml t3-b.yaml &
pid2=$!
$timeout --kill-after=15s 10s "$MGMT" run --yaml t3-c.yaml --converged-timeout=5 --no-watch --tmp-prefix &
$timeout --kill-after=15s 10s "$MGMT" run --converged-timeout=5 --no-watch --tmp-prefix yaml --yaml t3-c.yaml &
pid3=$!
wait $pid1 # get exit status

View File

@@ -1,7 +1,7 @@
#!/bin/bash -e
# should take slightly more than 35s, but fail if we take much longer)
$timeout --kill-after=55s 50s "$MGMT" run --yaml t5.yaml --converged-timeout=5 --no-watch --no-pgp --tmp-prefix &
$timeout --kill-after=55s 50s "$MGMT" run --converged-timeout=5 --no-watch --no-pgp --tmp-prefix yaml --yaml t5.yaml &
pid=$!
wait $pid # get exit status
exit $?

View File

@@ -1,7 +1,7 @@
#!/bin/bash -e
# should take slightly more than 35s, but fail if we take much longer)
$timeout --kill-after=55s 50s "$MGMT" run --yaml t5b.yaml --converged-timeout=5 --no-watch --no-pgp --tmp-prefix &
$timeout --kill-after=55s 50s "$MGMT" run --converged-timeout=5 --no-watch --no-pgp --tmp-prefix yaml --yaml t5b.yaml &
pid=$!
wait $pid # get exit status
exit $?

View File

@@ -7,7 +7,7 @@ if env | grep -q -e '^TRAVIS=true$'; then
fi
# run till completion
$timeout --kill-after=60s 55s "$MGMT" run --yaml t6.yaml --no-watch --tmp-prefix &
$timeout --kill-after=60s 55s "$MGMT" run --no-watch --tmp-prefix yaml --yaml t6.yaml &
pid=$!
sleep 10s # let it converge
test -e /tmp/mgmt/f1

View File

@@ -1,7 +1,7 @@
#!/bin/bash -e
# run empty graph
$timeout --kill-after=45s 35s "$MGMT" run --tmp-prefix --no-pgp &
$timeout --kill-after=45s 35s "$MGMT" run --tmp-prefix --no-pgp empty &
pid=$!
sleep 10s # let it converge
$(sleep 3s && killall -SIGINT mgmt)& # send ^C to exit mgmt

View File

@@ -3,11 +3,11 @@
exit 0 # XXX: test temporarily disabled till etcd or mgmt regression is fixed.
# run empty graphs, we're just testing etcd clustering
$timeout --kill-after=180s 120s "$MGMT" run --hostname h1 --tmp-prefix &
$timeout --kill-after=180s 120s "$MGMT" run --hostname h1 --tmp-prefix empty &
pid1=$!
sleep 15s # let it startup
$timeout --kill-after=180s 120s "$MGMT" run --hostname h2 --seeds http://127.0.0.1:2379 --client-urls http://127.0.0.1:2381 --server-urls http://127.0.0.1:2382 --tmp-prefix &
$timeout --kill-after=180s 120s "$MGMT" run --hostname h2 --seeds http://127.0.0.1:2379 --client-urls http://127.0.0.1:2381 --server-urls http://127.0.0.1:2382 --tmp-prefix empty &
pid2=$!
sleep 15s

View File

@@ -14,7 +14,7 @@ fi
# set the config file
cp -a yaml-change1a.yaml /tmp/mgmt/yaml-change.yaml
$timeout --kill-after=30s 20s "$MGMT" run --yaml /tmp/mgmt/yaml-change.yaml --tmp-prefix &
$timeout --kill-after=30s 20s "$MGMT" run --tmp-prefix yaml --yaml /tmp/mgmt/yaml-change.yaml &
pid=$!
sleep 5s # let it converge
grep -q 'hello world' /tmp/mgmt/change1 # check contents are correct