grim/convey
Clone
Summary
Browse
Changes
Graph
Add environment support to metaplans. Fixes CONVEY-185
2021-12-23, Gary Kramlich
9d2745690a29
Add environment support to metaplans. Fixes CONVEY-185
// Convey
// Copyright 2016-2018 Gary Kramlich <grim@reaperworld.com>
//
// 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
runtime
import
(
"context"
"fmt"
"keep.imfreedom.org/grim/convey/environment"
)
// MetaPlan is a representation of a meta plan.
type
MetaPlan
struct
{
Plans
[]
string
`yaml:"plans"`
Environment
environment
.
Environment
`yaml:"environment"`
}
// UnmarshalYAML is a custom yaml unmarshaller for MetaPlan's.
func
(
m
*
MetaPlan
)
UnmarshalYAML
(
unmarshal
func
(
interface
{})
error
)
error
{
type
rawMetaPlan
MetaPlan
raw
:=
rawMetaPlan
{}
if
err
:=
unmarshal
(
&
raw
);
err
!=
nil
{
return
err
}
if
len
(
raw
.
Plans
)
==
0
{
return
fmt
.
Errorf
(
"no plans specified"
)
}
*
m
=
MetaPlan
(
raw
)
return
nil
}
// Execute runs the MetaPlan
func
(
m
*
MetaPlan
)
Execute
(
ctx
context
.
Context
,
rt
*
Runtime
)
error
{
errChan
:=
make
(
chan
error
,
1
)
cancelled
:=
false
go
func
()
{
for
_
,
name
:=
range
m
.
Plans
{
mp
,
ok
:=
rt
.
MetaPlans
[
name
]
if
ok
{
// Store the old config environment from the runtime, copy it,
// and then merge the metaplan's environment into it. The
// config environment is the toplevel environment in the
// config, and that is the only thing the metaplan should be
// overwriting.
oldenv
:=
rt
.
ConfigEnvironment
rt
.
ConfigEnvironment
=
oldenv
.
Copy
().
Merge
(
mp
.
Environment
)
// Run the metaplan with the merged config environment, and
// then reset the runtime's config environment after execution.
err
:=
mp
.
Execute
(
ctx
,
rt
)
rt
.
ConfigEnvironment
=
oldenv
if
err
!=
nil
{
errChan
<-
err
return
}
}
else
{
plan
:=
rt
.
Plans
[
name
]
if
err
:=
plan
.
Execute
(
name
,
rt
);
err
!=
nil
{
errChan
<-
err
return
}
}
if
cancelled
{
break
}
}
errChan
<-
nil
}()
for
{
select
{
case
err
:=
<-
errChan
:
return
err
case
<-
ctx
.
Done
():
cancelled
=
true
}
}
}