2016-11-16 02:42:22 +01:00
|
|
|
// Copyright 2016 Marapongo, Inc. All rights reserved.
|
|
|
|
|
|
|
|
package compiler
|
|
|
|
|
|
|
|
import (
|
2016-11-16 19:00:52 +01:00
|
|
|
"github.com/blang/semver"
|
2016-11-16 20:09:45 +01:00
|
|
|
"github.com/golang/glog"
|
2016-11-16 19:00:52 +01:00
|
|
|
|
2016-11-16 18:29:44 +01:00
|
|
|
"github.com/marapongo/mu/pkg/ast"
|
2016-11-17 17:52:54 +01:00
|
|
|
"github.com/marapongo/mu/pkg/compiler/core"
|
2016-11-16 02:42:22 +01:00
|
|
|
"github.com/marapongo/mu/pkg/diag"
|
|
|
|
"github.com/marapongo/mu/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PTAnalyzer knows how to walk and validate parse trees.
|
|
|
|
type PTAnalyzer interface {
|
2016-11-17 17:52:54 +01:00
|
|
|
core.Visitor
|
2016-11-16 02:42:22 +01:00
|
|
|
|
|
|
|
// Analyze checks the validity of an entire parse tree (starting with a top-level Stack).
|
2016-11-16 18:29:44 +01:00
|
|
|
Analyze(doc *diag.Document, stack *ast.Stack)
|
2016-11-16 02:42:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPTAnalayzer allocates a new PTAnalyzer associated with the given Compiler.
|
|
|
|
func NewPTAnalyzer(c Compiler) PTAnalyzer {
|
|
|
|
return &ptAnalyzer{c: c}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ptAnalyzer struct {
|
|
|
|
c Compiler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ptAnalyzer) Diag() diag.Sink {
|
|
|
|
return a.c.Diag()
|
|
|
|
}
|
|
|
|
|
2016-11-16 18:29:44 +01:00
|
|
|
func (a *ptAnalyzer) Analyze(doc *diag.Document, stack *ast.Stack) {
|
2016-11-16 20:09:45 +01:00
|
|
|
glog.Infof("Parsetree analyzing Mu Stack: %v", stack.Name)
|
|
|
|
if glog.V(2) {
|
|
|
|
defer func() {
|
|
|
|
glog.V(2).Infof("Parsetree analysis for Mu Stack %v completed w/ %v warnings and %v errors",
|
|
|
|
stack.Name, a.Diag().Warnings(), a.Diag().Errors())
|
|
|
|
}()
|
2016-11-16 02:42:22 +01:00
|
|
|
}
|
2016-11-16 20:45:41 +01:00
|
|
|
|
|
|
|
// Use an InOrderVisitor to walk the tree in-order; this handles determinism for us.
|
2016-11-17 17:52:54 +01:00
|
|
|
v := core.NewInOrderVisitor(a, nil)
|
2016-11-16 20:45:41 +01:00
|
|
|
v.VisitStack(doc, stack)
|
2016-11-16 20:09:45 +01:00
|
|
|
}
|
|
|
|
|
Support Workspaces
This change adds support for Workspaces, a convenient way of sharing settings
among many Stacks, like default cluster targets, configuration settings, and the
like, which are not meant to be distributed as part of the Stack itself.
The following things are included in this checkin:
* At workspace initialization time, detect and parse the .mu/workspace.yaml
file. This is pretty rudimentary right now and contains just the default
cluster targets. The results are stored in a new ast.Workspace type.
* Rename "target" to "cluster". This impacts many things, including ast.Target
being changed to ast.Cluster, and all related fields, the command line --target
being changed to --cluster, various internal helper functions, and so on. This
helps to reinforce the desired mental model.
* Eliminate the ast.Metadata type. Instead, the metadata moves directly onto
the Stack. This reflects the decision to make Stacks "the thing" that is
distributed, versioned, and is the granularity of dependency.
* During cluster targeting, add the workspace settings into the probing logic.
We still search in the same order: CLI > Stack > Workspace.
2016-11-22 19:41:07 +01:00
|
|
|
func (a *ptAnalyzer) VisitStack(doc *diag.Document, stack *ast.Stack) {
|
|
|
|
// Stack names are required.
|
|
|
|
if stack.Name == "" {
|
|
|
|
a.Diag().Errorf(errors.MissingStackName.WithDocument(doc))
|
2016-11-16 20:09:45 +01:00
|
|
|
}
|
|
|
|
|
Support Workspaces
This change adds support for Workspaces, a convenient way of sharing settings
among many Stacks, like default cluster targets, configuration settings, and the
like, which are not meant to be distributed as part of the Stack itself.
The following things are included in this checkin:
* At workspace initialization time, detect and parse the .mu/workspace.yaml
file. This is pretty rudimentary right now and contains just the default
cluster targets. The results are stored in a new ast.Workspace type.
* Rename "target" to "cluster". This impacts many things, including ast.Target
being changed to ast.Cluster, and all related fields, the command line --target
being changed to --cluster, various internal helper functions, and so on. This
helps to reinforce the desired mental model.
* Eliminate the ast.Metadata type. Instead, the metadata moves directly onto
the Stack. This reflects the decision to make Stacks "the thing" that is
distributed, versioned, and is the granularity of dependency.
* During cluster targeting, add the workspace settings into the probing logic.
We still search in the same order: CLI > Stack > Workspace.
2016-11-22 19:41:07 +01:00
|
|
|
// Stack versions must be valid semantic versions (and specifically, not ranges). In other words, we need
|
2016-11-16 20:09:45 +01:00
|
|
|
// a concrete version number like "1.3.9-beta2" and *not* a range like ">1.3.9".
|
|
|
|
// TODO: should we require a version number?
|
Support Workspaces
This change adds support for Workspaces, a convenient way of sharing settings
among many Stacks, like default cluster targets, configuration settings, and the
like, which are not meant to be distributed as part of the Stack itself.
The following things are included in this checkin:
* At workspace initialization time, detect and parse the .mu/workspace.yaml
file. This is pretty rudimentary right now and contains just the default
cluster targets. The results are stored in a new ast.Workspace type.
* Rename "target" to "cluster". This impacts many things, including ast.Target
being changed to ast.Cluster, and all related fields, the command line --target
being changed to --cluster, various internal helper functions, and so on. This
helps to reinforce the desired mental model.
* Eliminate the ast.Metadata type. Instead, the metadata moves directly onto
the Stack. This reflects the decision to make Stacks "the thing" that is
distributed, versioned, and is the granularity of dependency.
* During cluster targeting, add the workspace settings into the probing logic.
We still search in the same order: CLI > Stack > Workspace.
2016-11-22 19:41:07 +01:00
|
|
|
if stack.Version != "" {
|
|
|
|
if _, err := semver.Parse(string(stack.Version)); err != nil {
|
|
|
|
a.Diag().Errorf(errors.IllegalStackSemVer.WithDocument(doc), stack.Version)
|
2016-11-16 20:09:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Support Workspaces
This change adds support for Workspaces, a convenient way of sharing settings
among many Stacks, like default cluster targets, configuration settings, and the
like, which are not meant to be distributed as part of the Stack itself.
The following things are included in this checkin:
* At workspace initialization time, detect and parse the .mu/workspace.yaml
file. This is pretty rudimentary right now and contains just the default
cluster targets. The results are stored in a new ast.Workspace type.
* Rename "target" to "cluster". This impacts many things, including ast.Target
being changed to ast.Cluster, and all related fields, the command line --target
being changed to --cluster, various internal helper functions, and so on. This
helps to reinforce the desired mental model.
* Eliminate the ast.Metadata type. Instead, the metadata moves directly onto
the Stack. This reflects the decision to make Stacks "the thing" that is
distributed, versioned, and is the granularity of dependency.
* During cluster targeting, add the workspace settings into the probing logic.
We still search in the same order: CLI > Stack > Workspace.
2016-11-22 19:41:07 +01:00
|
|
|
func (a *ptAnalyzer) VisitCluster(doc *diag.Document, name string, cluster *ast.Cluster) {
|
|
|
|
// Decorate the AST with contextual information so subsequent passes can operate context-free.
|
|
|
|
cluster.Name = name
|
2016-11-16 20:09:45 +01:00
|
|
|
}
|
|
|
|
|
Rename parameters to properties
The more I live with the current system, the more I prefer "properties" to
"parameters" for stacks and services. Although it is true that these things
are essentially construction-time arguments, they manifest more like properties
in the way they are used; in fact, if you think of the world in terms of primary
constructors, the distinction is pretty subtle anyway.
For example, when creating a new service, we say the following:
services:
private:
some/service:
a: 0
b: true
c: foo
This looks like a, b, and c are properties of the type some/service. If, on
the other hand, we kept calling these parameters, then you'd arguably prefer to
see the following:
services:
private:
some/service:
arguments:
a: 0
b: true
c: foo
This is a more imperative than declarative view of the world, which I dislike
(especially because it is more verbose).
Time will tell whether this is the right decision or not ...
2016-11-19 19:34:51 +01:00
|
|
|
func (a *ptAnalyzer) VisitProperty(doc *diag.Document, name string, param *ast.Property) {
|
2016-11-16 20:51:50 +01:00
|
|
|
// Decorate the AST with contextual information so subsequent passes can operate context-free.
|
|
|
|
param.Name = name
|
2016-11-16 20:09:45 +01:00
|
|
|
}
|
|
|
|
|
Implement dependency resolution
This change includes logic to resolve dependencies declared by stacks. The design
is described in https://github.com/marapongo/mu/blob/master/docs/deps.md.
In summary, each stack may declare dependencies, which are name/semver pairs. A
new structure has been introduced, ast.Ref, to distinguish between ast.Names and
dependency names. An ast.Ref includes a protocol, base part, and a name part (the
latter being an ast.Name); for example, in "https://hub.mu.com/mu/container/",
"https://" is the protocol, "hub.mu.com/" is the base, and "mu/container" is the
name. This is used to resolve URL-like names to package manager-like artifacts.
The dependency resolution phase happens after parsing, but before semantic analysis.
This is because dependencies are "source-like" in that we must load and parse all
dependency metadata files. We stick the full transitive closure of dependencies
into a map attached to the compiler to avoid loading dependencies multiple times.
Note that, although dependencies prohibit cycles, this forms a DAG, meaning multiple
inbound edges to a single stack may come from multiple places.
From there, we rely on ordinary visitation to deal with dependencies further.
This includes inserting symbol entries into the symbol table, mapping names to the
loaded stacks, during the first phase of binding so that they may be found
subsequently when typechecking during the second phase and beyond.
2016-11-21 20:19:25 +01:00
|
|
|
func (a *ptAnalyzer) VisitDependency(doc *diag.Document, ref ast.Ref, dep *ast.Dependency) {
|
2016-11-16 20:09:45 +01:00
|
|
|
// Dependency versions must be valid semantic versions *or* ranges.
|
|
|
|
// TODO: should we require dependencies to have versions?
|
|
|
|
ver := *dep
|
|
|
|
if ver != "" {
|
|
|
|
if _, err := semver.ParseRange(string(ver)); err != nil {
|
Implement dependency resolution
This change includes logic to resolve dependencies declared by stacks. The design
is described in https://github.com/marapongo/mu/blob/master/docs/deps.md.
In summary, each stack may declare dependencies, which are name/semver pairs. A
new structure has been introduced, ast.Ref, to distinguish between ast.Names and
dependency names. An ast.Ref includes a protocol, base part, and a name part (the
latter being an ast.Name); for example, in "https://hub.mu.com/mu/container/",
"https://" is the protocol, "hub.mu.com/" is the base, and "mu/container" is the
name. This is used to resolve URL-like names to package manager-like artifacts.
The dependency resolution phase happens after parsing, but before semantic analysis.
This is because dependencies are "source-like" in that we must load and parse all
dependency metadata files. We stick the full transitive closure of dependencies
into a map attached to the compiler to avoid loading dependencies multiple times.
Note that, although dependencies prohibit cycles, this forms a DAG, meaning multiple
inbound edges to a single stack may come from multiple places.
From there, we rely on ordinary visitation to deal with dependencies further.
This includes inserting symbol entries into the symbol table, mapping names to the
loaded stacks, during the first phase of binding so that they may be found
subsequently when typechecking during the second phase and beyond.
2016-11-21 20:19:25 +01:00
|
|
|
a.Diag().Errorf(errors.IllegalDependencySemVer.WithDocument(doc), ref, ver)
|
2016-11-16 19:00:52 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-16 02:42:22 +01:00
|
|
|
}
|
2016-11-17 02:30:03 +01:00
|
|
|
|
Implement dependency resolution
This change includes logic to resolve dependencies declared by stacks. The design
is described in https://github.com/marapongo/mu/blob/master/docs/deps.md.
In summary, each stack may declare dependencies, which are name/semver pairs. A
new structure has been introduced, ast.Ref, to distinguish between ast.Names and
dependency names. An ast.Ref includes a protocol, base part, and a name part (the
latter being an ast.Name); for example, in "https://hub.mu.com/mu/container/",
"https://" is the protocol, "hub.mu.com/" is the base, and "mu/container" is the
name. This is used to resolve URL-like names to package manager-like artifacts.
The dependency resolution phase happens after parsing, but before semantic analysis.
This is because dependencies are "source-like" in that we must load and parse all
dependency metadata files. We stick the full transitive closure of dependencies
into a map attached to the compiler to avoid loading dependencies multiple times.
Note that, although dependencies prohibit cycles, this forms a DAG, meaning multiple
inbound edges to a single stack may come from multiple places.
From there, we rely on ordinary visitation to deal with dependencies further.
This includes inserting symbol entries into the symbol table, mapping names to the
loaded stacks, during the first phase of binding so that they may be found
subsequently when typechecking during the second phase and beyond.
2016-11-21 20:19:25 +01:00
|
|
|
func (a *ptAnalyzer) VisitBoundDependency(doc *diag.Document, ref ast.Ref, dep *ast.BoundDependency) {
|
|
|
|
}
|
|
|
|
|
2016-11-17 02:30:03 +01:00
|
|
|
func (a *ptAnalyzer) VisitServices(doc *diag.Document, svcs *ast.Services) {
|
Retain unrecognized service properties
During unmarshaling, the default behavior of the stock Golang JSON marshaler,
and consequently the YAML one we used which mimics its behavior, is to toss away
unrecognized properties. This isn't what we want for two reasons:
First, we want to issue errors/warnings on unrecognized fields to aid in diagnostics;
we will set aside some extensible section for 3rd parties to use. This is not
addressed in this change, however.
Second, and more pertinent, is that we need to retain unrecognized fields for certain
types like services, which are extensible by default.
Until golang/go#6213 is addressed -- imminent, it seems -- we will have to do a
somewhat hacky workaround to this problem. This change contains what I consider to
be the "least bad" in that we won't introduce a lot of performance overhead, and
just have to deal with the slight annoyance of the ast.Services node type containing
both Public/Private *and* PublicUntyped/PrivateUntyped fields alongside one another.
The marshaler dumps property bags into the *Untyped fields, and the parsetree analyzer
expands them out into a structured ast.Service type. Subsequent passes can then
ignore the *Untyped fields altogether.
Note that this would cause some marshaling funkiness if we ever wanted to remarshal
the mutated ASTs back into JSON/YAML. Since we don't do that right now, however, I've
not made any attempt to keep the two pairs in synch. Post-parsetree analyzer, we
literally just forget about the *Untyped guys.
2016-11-19 18:01:23 +01:00
|
|
|
// We need to expand the UntypedServiceMaps into strongly typed ServiceMaps. As part of this, we also decorate the
|
|
|
|
// AST with extra contextual information so subsequent passes can operate context-free.
|
2016-11-19 21:31:00 +01:00
|
|
|
// TODO[marapongo/mu#4]: once we harden the marshalers, we should be able to largely eliminate this.
|
Retain unrecognized service properties
During unmarshaling, the default behavior of the stock Golang JSON marshaler,
and consequently the YAML one we used which mimics its behavior, is to toss away
unrecognized properties. This isn't what we want for two reasons:
First, we want to issue errors/warnings on unrecognized fields to aid in diagnostics;
we will set aside some extensible section for 3rd parties to use. This is not
addressed in this change, however.
Second, and more pertinent, is that we need to retain unrecognized fields for certain
types like services, which are extensible by default.
Until golang/go#6213 is addressed -- imminent, it seems -- we will have to do a
somewhat hacky workaround to this problem. This change contains what I consider to
be the "least bad" in that we won't introduce a lot of performance overhead, and
just have to deal with the slight annoyance of the ast.Services node type containing
both Public/Private *and* PublicUntyped/PrivateUntyped fields alongside one another.
The marshaler dumps property bags into the *Untyped fields, and the parsetree analyzer
expands them out into a structured ast.Service type. Subsequent passes can then
ignore the *Untyped fields altogether.
Note that this would cause some marshaling funkiness if we ever wanted to remarshal
the mutated ASTs back into JSON/YAML. Since we don't do that right now, however, I've
not made any attempt to keep the two pairs in synch. Post-parsetree analyzer, we
literally just forget about the *Untyped guys.
2016-11-19 18:01:23 +01:00
|
|
|
svcs.Public = make(ast.ServiceMap)
|
|
|
|
for _, name := range ast.StableUntypedServices(svcs.PublicUntyped) {
|
|
|
|
svcs.Public[name] = a.untypedServiceToTyped(doc, name, true, svcs.PublicUntyped[name])
|
|
|
|
}
|
|
|
|
svcs.Private = make(ast.ServiceMap)
|
|
|
|
for _, name := range ast.StableUntypedServices(svcs.PrivateUntyped) {
|
|
|
|
svcs.Private[name] = a.untypedServiceToTyped(doc, name, false, svcs.PrivateUntyped[name])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ptAnalyzer) untypedServiceToTyped(doc *diag.Document,
|
|
|
|
name ast.Name, public bool, bag map[string]interface{}) ast.Service {
|
|
|
|
var typ ast.Name
|
|
|
|
t, has := bag["type"]
|
|
|
|
if has {
|
|
|
|
// If the bag contains a type, ensure that it is a string.
|
|
|
|
ts, ok := t.(string)
|
|
|
|
if ok {
|
|
|
|
typ = ast.Name(ts)
|
|
|
|
} else {
|
|
|
|
a.Diag().Errorf(errors.IllegalMufileSyntax.WithDocument(doc), "service type must be a string")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast.Service{
|
|
|
|
Name: name,
|
|
|
|
Type: typ,
|
|
|
|
Public: public,
|
|
|
|
Extra: bag,
|
|
|
|
}
|
2016-11-17 02:30:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ptAnalyzer) VisitService(doc *diag.Document, name ast.Name, public bool, svc *ast.Service) {
|
|
|
|
}
|