Skip to content

Commit

Permalink
source discovery service version from param store
Browse files Browse the repository at this point in the history
  • Loading branch information
kahgeh-work committed Sep 28, 2020
1 parent 99cf357 commit ccbe541
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 17 deletions.
24 changes: 20 additions & 4 deletions aws/spotFleet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ Resources:
- :repository/*
Version: "2012-10-17"
PolicyName: ecs-custom-instance-policy
- PolicyName: AllowGetParam
PolicyDocument:
Version: "2012-10-17"
Statement:
- Action:
- ssm:GetParametersByPath
- ssm:GetParameters
- ssm:GetParameter
Effect: Allow
Resource:
- !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/allEnvs/${Environment}/*
- !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/allEnvs/${Environment}/
Type: AWS::IAM::Role
IamFleetRole:
Type: AWS::IAM::Role
Expand Down Expand Up @@ -137,18 +149,22 @@ Resources:
#!/bin/bash -xe
yum install -y aws-cli
yum install -y amazon-cloudwatch-agent

region=$(curl http://169.254.169.254/latest/meta-data/placement/region)
export AWS_REGION=$region
export AWS_DEFAULT_REGION=$region
export AWS_PAGER=""
WHALE_DISCO_VERSION=$(aws ssm get-parameter --name /allEnvs/${Environment}/WhaleDiscoVersion --query "Parameter.Value" --output text)
cat <<'EOF' >> /etc/ecs/ecs.config
ECS_CLUSTER=${EcsClusterName}
ECS_ENABLE_CONTAINER_METADATA=true
EOF

curl -fOL https://github.com/kahgeh/whale-disco/releases/download/v0.0.3/whale-disco_0.0.3_Linux_x86_64.tar.gz
tar -xvf whale-disco_0.0.3_Linux_x86_64.tar.gz
curl -fOL https://github.com/kahgeh/whale-disco/releases/download/v${!WHALE_DISCO_VERSION}/whale-disco_${!WHALE_DISCO_VERSION}_Linux_x86_64.tar.gz
tar -xvf whale-disco_${!WHALE_DISCO_VERSION}_Linux_x86_64.tar.gz
cp whale-disco /usr/bin/
chmod a+x /usr/bin/whale-disco

rm whale-disco_0.0.3_Linux_x86_64.tar.gz
rm whale-disco_${!WHALE_DISCO_VERSION}_Linux_x86_64.tar.gz
rm whale-disco
rm Readme.MD

Expand Down
33 changes: 26 additions & 7 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@ package cmd

import (
"fmt"
cmdTypes "github.com/kahgeh/devenv/cmd/types"
"github.com/kahgeh/devenv/fixed"
"github.com/kahgeh/devenv/logger"
"github.com/kahgeh/devenv/provider/types"
"github.com/kahgeh/devenv/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"runtime/debug"
)

const (
baseUrl string = "https://raw.githubusercontent.com/kahgeh/devenv/master"
)
const (
argDiscoveryServiceVersion cmdTypes.ArgName = "discovery-service-version"
)

var cloudProviderFiles = map[string][]string{"aws": {"vpc.yml", "ecsCluster.yml",
"spotFleet.yml", "publicIp.yml",
Expand All @@ -53,7 +58,7 @@ func initialiseConfig() {
log.Debug(err.Error())
log.Failf("fail to ensure %q exist", cloudProviderFolderPath)
}
frontProxyFolderPath :=fmt.Sprintf("%v/front-proxy", cloudProviderFolderPath)
frontProxyFolderPath := fmt.Sprintf("%v/front-proxy", cloudProviderFolderPath)
log.Infof("ensuring front proxy folder '%s' exists...", frontProxyFolderPath)
err = utils.CreateFolderIfNotExist(frontProxyFolderPath)
if err != nil {
Expand All @@ -77,6 +82,14 @@ var initCmd = &cobra.Command{
Run: initialise,
}

func extractInitParameters() (domainName string, domainEmail string, envName string, discoveryServiceVersion string) {
domainName = viper.GetString("domain-name")
domainEmail = viper.GetString("domain-email")
envName = viper.GetString("env-name")
discoveryServiceVersion = viper.GetString(string(argDiscoveryServiceVersion))
return
}

func initialise(_ *cobra.Command, args []string) {
startLog()
log := logger.New()
Expand All @@ -92,16 +105,22 @@ func initialise(_ *cobra.Command, args []string) {
initialiseConfig()
return
}
domainName := viper.GetString("domain-name")
domainEmail := viper.GetString("domain-email")
envName := viper.GetString("env-name")

domainName, domainEmail, envName, discoveryServiceVersion := extractInitParameters()
createSession().Initialise(&types.InitialisationParameters{
DomainName: domainName,
DomainEmail: domainEmail,
EnvironmentName: envName,
DomainName: domainName,
DomainEmail: domainEmail,
EnvironmentName: envName,
DiscoveryServiceVersion: discoveryServiceVersion,
})
}

func init() {
rootCmd.AddCommand(initCmd)
deployCmd.PersistentFlags().String(string(argDiscoveryServiceVersion), "0.0.1", "--discovery-service-version <relative or absolute path>")
err := viper.BindPFlags(deployCmd.PersistentFlags())
if err != nil {
fmt.Printf("fail to bind command arguments\n %s", err.Error())
os.Exit(logger.ExitFailureStatus)
}
}
4 changes: 2 additions & 2 deletions provider/aws/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,14 @@ func (session *Session) deployFrontProxy(image string, envName string) {
if stackDescription != nil {
ssmSession := session.NewSsmSession()
log.Info("getting app details...")
cluster, err := ssmSession.GetParamterValue(fmt.Sprintf("/allEnvs/%s/infra/ecs/name", envName))
cluster, err := ssmSession.GetParameterValue(fmt.Sprintf("/allEnvs/%s/infra/ecs/name", envName))
if err != nil {
log.Debug(err.Error())
log.Fail("fail to get cluster name details")
return
}

serviceArn, err := ssmSession.GetParamterValue(fmt.Sprintf("/allEnvs/%s/apps/%s/serviceArn", envName, appName))
serviceArn, err := ssmSession.GetParameterValue(fmt.Sprintf("/allEnvs/%s/apps/%s/serviceArn", envName, appName))
if err != nil {
log.Debug(err.Error())
log.Fail("fail to get service details")
Expand Down
21 changes: 21 additions & 0 deletions provider/aws/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,36 @@ func (session *Session) savePstoreKey(keyName string, path string) {
log.Succeed()
}

func (session *Session) saveWhaleDiscoveryVersion(envName, version string) {
log := logger.NewTaskLogger()
defer log.LogDone()
ssmSession := session.NewSsmSession()
parameterName := fmt.Sprintf("/allEnvs/%s/WhaleDiscoVersion", envName)
parameterVersion, err := ssmSession.SaveParameter(parameterName, version)
if err != nil {
log.Failf("cannot save discovery service version, %s", err.Error())
return
}
if parameterVersion == nil {
log.Failf("cannot save discovery service version, the parameter version returned was empty")
return
}
log.Infof("discovery service version saved in param store %q for the %v time", parameterName, *parameterVersion)
log.Succeed()

}

func (session *Session) Initialise(parameters *types.InitialisationParameters) {
log := logger.New()
defer log.LogDone()
envName := parameters.EnvironmentName
domainName := parameters.DomainName
discoveryServiceVersion := parameters.DiscoveryServiceVersion
session.createKeyPair()
session.createVpc()
session.createEcsCluster(envName)
session.createEcsSpotFleet(envName, domainName)
pstoreKeyPath := fmt.Sprintf(string(TemplateParamStoreKeyPath), envName)
session.savePstoreKey("alias/aws/ssm", pstoreKeyPath)
session.saveWhaleDiscoveryVersion(envName, discoveryServiceVersion)
}
19 changes: 18 additions & 1 deletion provider/aws/ssm.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (session *Session) NewSsmSession() *SsmSession {
}
}

func (session *SsmSession) GetParamterValue(name string) (value *string, err error) {
func (session *SsmSession) GetParameterValue(name string) (value *string, err error) {
api := session.api

request := api.GetParameterRequest(&ssm.GetParameterInput{
Expand All @@ -42,3 +42,20 @@ func (session *SsmSession) GetParamterValue(name string) (value *string, err err
value = response.GetParameterOutput.Parameter.Value
return
}

func (session *SsmSession) SaveParameter(name string, value string) (version *int64, err error) {
api := session.api

request := api.PutParameterRequest(&ssm.PutParameterInput{
Name: aws.String(name),
Value: &value,
Type: ssm.ParameterTypeString,
Overwrite: aws.Bool(true),
})
response, err := request.Send(ctx.GetContext())
if err != nil {
return
}
version = response.Version
return
}
7 changes: 4 additions & 3 deletions provider/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ type DeployParameters struct {
}

type InitialisationParameters struct {
DomainName string
DomainEmail string
EnvironmentName string
DomainName string
DomainEmail string
EnvironmentName string
DiscoveryServiceVersion string
}

type StartParameters struct {
Expand Down

0 comments on commit ccbe541

Please sign in to comment.