Skip to content

Commit

Permalink
Change config variable expansion to the form ${instance.<key>} and ${…
Browse files Browse the repository at this point in the history
…env.<key>}

to be forwards compatible with a possible future enhancement using hashicorp/hil
Also switches os.Expand() to a private variant which only expands ${name} and not
$name to minimise impact to any existing configs that may contain $
  • Loading branch information
Travers Carter committed Mar 21, 2017
1 parent b8cbe70 commit 30c4a57
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 19 deletions.
33 changes: 18 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,24 @@ The following configuration settings are supported:
this setting provides a maximum batch size to use when clearing a large backlog of events, e.g.
from system boot when the program starts for the first time.

Additionally values in the configuration file can contain variable expansions of the form ${name} or $name
which will be expanded from either the AWS Instance Identity Document or the operating system environment
variables. At the time of writing the supported InstanceIdentityDocument variables are:

* `${AvailabilityZone}`: The name of the availability zone the instance is running, eg `ap-southeast-2b`
* `${PrivateIP}`: The AWS internal private IP address of the instance, eg `172.1.2.3`
* `${Version}`: The version of the InstanceIdentityDocument definition?, eg `2010-08-31`
* `${Region}`: The name of the region the instance is running in, eg `ap-southeast-2`
* `${InstanceID}`: The instance identifier, eg `i-0123456789abcdef0`
* `${InstanceType}`: The type of the instance, eg `x1.32xlarge`
* `${AccountID}`: The amazon web services account the instance is running under, eg `098765432123`
* `${ImageID}`: The AMI (image) id the instance was launched from, eg `ami-a1b2c3d4`
* `${KernelID}`: The kernel ID used to launch the instance (PV instances only)
* `${RamdiskID}`: The ramdisk ID used to launch the instance (PV instances only)
* `${Architecture}`: The CPU architecture of the instance, eg `x86_64`
Additionally values in the configuration file can contain variable expansions of the form
${instance.<key>} which will be exapnded from the AWS Instance Identity Document or ${env.<name>}
which will be expanded from the operating system environment variables, if a key does not exist
it expands to the empty string.

At the time of writing, in early 2017, the supported InstanceIdentityDocument variables are:

* `${instance.AvailabilityZone}`: The name of the availability zone the instance is running, eg `ap-southeast-2b`
* `${instance.PrivateIP}`: The AWS internal private IP address of the instance, eg `172.1.2.3`
* `${instance.Version}`: The version of the InstanceIdentityDocument definition?, eg `2010-08-31`
* `${instance.Region}`: The name of the region the instance is running in, eg `ap-southeast-2`
* `${instance.InstanceID}`: The instance identifier, eg `i-0123456789abcdef0`
* `${instance.InstanceType}`: The type of the instance, eg `x1.32xlarge`
* `${instance.AccountID}`: The amazon web services account the instance is running under, eg `098765432123`
* `${instance.ImageID}`: The AMI (image) id the instance was launched from, eg `ami-a1b2c3d4`
* `${instance.KernelID}`: The kernel ID used to launch the instance (PV instances only)
* `${instance.RamdiskID}`: The ramdisk ID used to launch the instance (PV instances only)
* `${instance.Architecture}`: The CPU architecture of the instance, eg `x86_64`

### AWS API access

Expand Down
44 changes: 40 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"reflect"
"strings"

"github.com/aws/aws-sdk-go/aws"
awsCredentials "github.com/aws/aws-sdk-go/aws/credentials"
Expand Down Expand Up @@ -190,16 +191,51 @@ func expandFileConfig(config *fileConfig, metaClient *ec2metadata.EC2Metadata) {
val := field.Interface().(string)
if val != "" {
field.SetString(
os.Expand(
expandBraceVars(
val,
func(varname string) string {
if val, exists := vars[varname]; exists {
return val
if strings.HasPrefix(varname, "instance.") {
if val, exists := vars[strings.TrimPrefix(varname, "instance.")]; exists {
return val
}
// Unknown key => empty string
return ""
} else if (strings.HasPrefix(varname, "env.")) {
return os.Getenv(strings.TrimPrefix(varname, "env."))
} else {
// Unknown prefix => empty string
return ""
}
return os.Getenv(varname)
},
),
)
}
}
}


// Modified version of os.Expand() that only expands ${name} and not $name
func expandBraceVars(s string, mapping func(string) string) string {
buf := make([]byte, 0, 2*len(s))
// ${} is all ASCII, so bytes are fine for this operation.
i := 0
for j := 0; j < len(s); j++ {
if s[j] == '$' && j+3 < len(s) && s[j+1] == '{' {
buf = append(buf, s[i:j]...)
idx := strings.Index(s[j+2:], "}")
if (idx >= 0) {
// We have a full ${name} string
buf = append(buf, mapping(s[j+2:j+2+idx])...)
j += 2+idx
} else {
// We ran out of string (unclosed ${)
return string(buf)
}
i = j + 1
}
}
return string(buf) + s[i:]
}



0 comments on commit 30c4a57

Please sign in to comment.