This repository has been archived by the owner on Sep 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added env substitution to the cft files..
- Loading branch information
Showing
5 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
UserData: | ||
"Fn::Base64": !Sub | | ||
#!/bin/bash | ||
yum install -y aws-cfn-bootstrap | ||
${Local::IncludeEnv TEMP_ENV_VAL} | ||
/opt/aws/bin/cfn-init -v --region ${AWS::Region} --stack ${AWS::StackName} --resource ECSLaunchConfiguration | ||
/opt/aws/bin/cfn-signal -e $? --region ${AWS::Region} --stack ${AWS::StackName} --resource ECSAutoScalingGroup | ||
UserData2-${Local::IncludeEnv TEMP_ENV_VAL}: | ||
temp: !Local::IncludeEnv TEMP_ENV_VAL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package utils | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"io" | ||
"os" | ||
"regexp" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
// Matches literal string: ${Local::IncludeEnv <key>} | ||
// example: ${Local::IncludeEnv HOME} | ||
literalIncludeEnvRe = regexp.MustCompile(`\${[ ]*Local::IncludeEnv[ ]+([[:ascii:]]+)}`) | ||
// Matches tag !Local::IncludeEnv <key> | ||
// for example: !Local::IncludeEnv HOME | ||
valueIncludeEnvTagRe = regexp.MustCompile(`!Local::IncludeEnv[ ]+([[:ascii:]]+)`) | ||
) | ||
|
||
func ApplyIncludeEnvDirective(reader io.Reader) []byte { | ||
output := bytes.NewBuffer([]byte{}) | ||
scanner := bufio.NewScanner(reader) | ||
for scanner.Scan() { | ||
line := scanner.Bytes() | ||
|
||
loc := literalIncludeEnvRe.FindIndex(line) | ||
tagLoc := valueIncludeEnvTagRe.FindIndex(line) | ||
|
||
if len(loc) == 2 { | ||
envName := string(literalIncludeEnvRe.FindSubmatch(line)[1]) | ||
log.Debugf("loading include env: %s", envName) | ||
|
||
output.Write(line[0:loc[0]]) | ||
envValue := os.Getenv(envName) | ||
output.WriteString(envValue) | ||
output.Write(line[loc[1]:]) | ||
|
||
} else if len(tagLoc) == 2 { | ||
envName := string(valueIncludeEnvTagRe.FindSubmatch(line)[1]) | ||
log.Debugf("loading include env: %s", envName) | ||
|
||
output.Write(line[0:tagLoc[0]]) | ||
envValue := os.Getenv(envName) | ||
output.WriteString(envValue) | ||
output.Write(line[tagLoc[1]:]) | ||
|
||
} else { | ||
output.Write(line) | ||
} | ||
|
||
output.WriteByte('\n') | ||
} | ||
return output.Bytes() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// Copyright 2016 Capital One Services, LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and limitations under the License. | ||
// | ||
// SPDX-Copyright: Copyright (c) Capital One Services, LLC | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
package utils | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var expectedEnvIncl = `UserData: | ||
"Fn::Base64": !Sub | | ||
#!/bin/bash | ||
yum install -y aws-cfn-bootstrap | ||
something | ||
/opt/aws/bin/cfn-init -v --region ${AWS::Region} --stack ${AWS::StackName} --resource ECSLaunchConfiguration | ||
/opt/aws/bin/cfn-signal -e $? --region ${AWS::Region} --stack ${AWS::StackName} --resource ECSAutoScalingGroup | ||
UserData2-something: | ||
temp: something | ||
` | ||
|
||
func TestApplyIncludeEnvDirective(t *testing.T) { | ||
fname := "../resources/" + "user-data-env.yaml" | ||
TemporaryChdir("../resources", func() { | ||
f, err := os.Open(fname) | ||
assert.Nil(t, err) | ||
defer f.Close() | ||
var result []byte | ||
TemporaryEnv("TEMP_ENV_VAL", "something", func() { | ||
result = ApplyIncludeEnvDirective(f) | ||
}) | ||
|
||
assert.Equal(t, expectedEnvIncl, string(result)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters