Skip to content

Commit

Permalink
Added account ID as a flag
Browse files Browse the repository at this point in the history
  • Loading branch information
okankoAMZ committed Sep 20, 2023
1 parent e9ca4e5 commit f66182a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 33 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/test-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ env:
CWA_GITHUB_TEST_REPO_NAME: "aws/amazon-cloudwatch-agent-test"

on:
push:
branches:
- uniform-build-env
workflow_dispatch:
inputs:
ContainerRepositoryNameAndTag:
Expand Down Expand Up @@ -78,16 +81,12 @@ jobs:
with:
key: "cached_binaries_${{ github.sha }}"
path: go.mod
- name: vartest
env:
Test: "oh no"
run: echo "${{secrets.AWS_PRIVATE_KEY}} ${{vars.IAMNOTASECRETUNLESS}} ${{env.Test}}"
- name: Build Uniform Build Env
if: contains(inputs.BucketKey, 'test') == false || steps.cached_binaries.outputs.cache-hit == false
run: |
cd packaging/uniformBuild
go build .
./uniformBuild -r "${{github.repositoryUrl}}" -b "main" -c "${{github.sha}}"
./uniformBuild -r "${{github.repositoryUrl}}" -b "main" -c "${{github.sha}} -a ${{secrets.AWS_TEST_ACCOUNT_ID}}"
# - name: Cache go ^^^ make it not main
# # Only skip for integration builds not release builds.
Expand Down
19 changes: 7 additions & 12 deletions packaging/uniformBuild/aws_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"time"
)

const ACCOUNT_ID = "956457624121"

var (
INVALID_INSTANCE = errors.New("Invalid Instance")
INVALID_OS = errors.New("That OS is not in supported AMIs")
Expand Down Expand Up @@ -48,12 +46,12 @@ func CreateNewInstanceManager(cfg aws.Config) *InstanceManager {
amis: make(map[OS]*types.Image),
}
}
func (imng *InstanceManager) GetAllAMIVersions() []types.Image {
func (imng *InstanceManager) GetAllAMIVersions(accountID string) []types.Image {
//returns a sorted list by creation date
filters := []types.Filter{
{
Name: aws.String("owner-id"),
Values: []string{ACCOUNT_ID},
Values: []string{accountID},
},
{
Name: aws.String("tag-key"),
Expand All @@ -73,12 +71,8 @@ func (imng *InstanceManager) GetAllAMIVersions() []types.Image {
})
return resp.Images
}
func (imng *InstanceManager) GetLatestAMIVersion() *types.Image {
amiList := imng.GetAllAMIVersions()
//// Print the latest AMI ID
//for _, i := range resp.Images {
// fmt.Printf("%s,%s\n", *i.ImageId, *i.CreationDate)
//}
func (imng *InstanceManager) GetLatestAMIVersion(accountID string) *types.Image {
amiList := imng.GetAllAMIVersions(accountID)
if len(amiList) > 0 {
fmt.Println("Latest AMI ID:", *amiList[0].ImageId)
return &amiList[0]
Expand All @@ -95,9 +89,10 @@ func parseTime(value string) *time.Time {
return &t
}

func (imng *InstanceManager) GetSupportedAMIs() {
func (imng *InstanceManager) GetSupportedAMIs(accountID string) {
//this populates the amis map
latestAmis := imng.GetAllAMIVersions()
latestAmis := imng.GetAllAMIVersions(accountID)
fmt.Printf("Found %d possible AMIs", len(latestAmis))
for _, ami := range latestAmis {
for _, os := range SUPPORTED_OS {
if strings.Contains(strings.ToLower(*ami.PlatformDetails), string(os)) {
Expand Down
27 changes: 20 additions & 7 deletions packaging/uniformBuild/uniformBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
Expand Down Expand Up @@ -32,7 +33,7 @@ var DEFAULT_INSTANCE_GUIDE = map[string]OS{
/*
This function will create EC2 instances as a side effect
*/
func CreateRemoteBuildManager(instanceGuide map[string]OS) *RemoteBuildManager {
func CreateRemoteBuildManager(instanceGuide map[string]OS, accountID string) *RemoteBuildManager {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return nil
Expand All @@ -41,12 +42,18 @@ func CreateRemoteBuildManager(instanceGuide map[string]OS) *RemoteBuildManager {
rbm := RemoteBuildManager{}

rbm.instanceManager = CreateNewInstanceManager(cfg)

rbm.instanceManager.GetSupportedAMIs()
fmt.Println("New Instance Manager Created")
rbm.instanceManager.GetSupportedAMIs(accountID)
b, err := json.MarshalIndent(rbm.instanceManager.amis, "", " ")
fmt.Printf("Got Supported Amis: %s %s\n ", b, err)
//linuxImage := rbm.instanceManager.GetLatestAMIVersion()
//rbm.instanceManager.amis["linux"] = linuxImage
fmt.Println("About to create ec2 instances")
err = rbm.instanceManager.CreateEC2InstancesBlocking(instanceGuide)

rbm.instanceManager.CreateEC2InstancesBlocking(instanceGuide)
if err != nil {
panic(err)
}
fmt.Println("Starting SSM Client")
rbm.ssmClient = ssm.NewFromConfig(cfg)
//RunCmdRemotely(rbm.ssmClient, rbm.instances["linux"], "export PATH=$PATH:/usr/local/go/bin")
Expand All @@ -67,6 +74,7 @@ func (rbm *RemoteBuildManager) BuildCWAAgent(gitUrl string, branch string, commi
if err != nil {
return err
}
fmt.Println("Starting CWA Build")
buildMasterCommand := mergeCommands(
CloneGitRepo(gitUrl, branch),
MakeBuild(),
Expand Down Expand Up @@ -123,7 +131,9 @@ func main() {
var repo string
var branch string
var comment string

var accountID string
flag.StringVar(&accountID, "a", "", "accountID")
flag.StringVar(&accountID, "account_id", "", "accountID")
flag.StringVar(&repo, "r", "", "repository")
flag.StringVar(&repo, "repo", "", "repository")
flag.StringVar(&branch, "b", "", "branch")
Expand All @@ -132,8 +142,11 @@ func main() {
flag.StringVar(&comment, "comment", "", "comment")

flag.Parse()
rbm := CreateRemoteBuildManager(DEFAULT_INSTANCE_GUIDE)
rbm := CreateRemoteBuildManager(DEFAULT_INSTANCE_GUIDE, accountID)
defer rbm.Close()
rbm.BuildCWAAgent(repo, branch, comment, "MainBuildEnv")
err := rbm.BuildCWAAgent(repo, branch, comment, "MainBuildEnv")
if err != nil {
panic(err)
}

}
28 changes: 19 additions & 9 deletions packaging/uniformBuild/uniformBuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,43 @@ package main
import (
//"github.com/stretchr/testify/require"
"context"
"flag"
"fmt"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/stretchr/testify/require"
"os"
"testing"
"time"

"github.com/aws/aws-sdk-go-v2/config"
)

var accountID string

func TestMain(m *testing.M) {
flag.StringVar(&accountID, "a", "", "accountID")
flag.StringVar(&accountID, "account_id", "", "accountID")
code := m.Run()
os.Exit(code)
}
func TestAmiLatest(t *testing.T) {
cfg, _ := config.LoadDefaultConfig(context.TODO())

imng := CreateNewInstanceManager(cfg)
// is it consistent
previous := *imng.GetLatestAMIVersion().ImageId
previous := *imng.GetLatestAMIVersion(accountID).ImageId
for i := 0; i < 5; i++ {
current := *imng.GetLatestAMIVersion().ImageId
current := *imng.GetLatestAMIVersion(accountID).ImageId
require.Equalf(t, current, previous, "AMI is inconsistent %s | %s", current, previous)
}
fmt.Println(imng.GetLatestAMIVersion().ImageId)
fmt.Println(imng.GetLatestAMIVersion(accountID).ImageId)

}
func TestSupportedAmis(t *testing.T) {
cfg, _ := config.LoadDefaultConfig(context.TODO())

imng := CreateNewInstanceManager(cfg)
imng.GetSupportedAMIs()
imng.GetSupportedAMIs(accountID)
for _, os := range SUPPORTED_OS {
_, ok := imng.amis[os]
require.Truef(t, ok, "It does not contain", os)
Expand All @@ -38,7 +48,7 @@ func TestSupportedAmis(t *testing.T) {
}

func TestEc2Generation(t *testing.T) {
rbm := CreateRemoteBuildManager(DEFAULT_INSTANCE_GUIDE)
rbm := CreateRemoteBuildManager(DEFAULT_INSTANCE_GUIDE, accountID)
fmt.Println(rbm.ssmClient)
rbm.Close()
}
Expand All @@ -56,7 +66,7 @@ func TestEnviorment(t *testing.T) {
guide := map[string]OS{
"MainBuildEnv": LINUX,
}
rbm := CreateRemoteBuildManager(guide)
rbm := CreateRemoteBuildManager(guide, accountID)
defer rbm.Close()
func() {
require.NoError(t,
Expand Down Expand Up @@ -89,7 +99,7 @@ func TestOSMixUp(t *testing.T) {
"linux": LINUX,
"win": WINDOWS,
}
rbm := CreateRemoteBuildManager(guide)
rbm := CreateRemoteBuildManager(guide, accountID)
defer rbm.Close()
require.NoErrorf(t, rbm.instanceManager.insertOSRequirement("linux", LINUX), "")
require.Errorf(t, rbm.instanceManager.insertOSRequirement("linux", WINDOWS),
Expand All @@ -99,7 +109,7 @@ func TestOSMixUp(t *testing.T) {
func TestPublicRepoBuild(t *testing.T) {
REPO_NAME := "https://github.com/aws/amazon-cloudwatch-agent.git"
BRANCH_NAME := "main"
rbm := CreateRemoteBuildManager(DEFAULT_INSTANCE_GUIDE)
rbm := CreateRemoteBuildManager(DEFAULT_INSTANCE_GUIDE, accountID)
defer rbm.Close()
err := rbm.BuildCWAAgent(REPO_NAME, BRANCH_NAME, fmt.Sprintf("PUBLIC_REPO_TEST-%d", time.Now().Unix()), "MainBuildEnv")
require.NoError(t, err)
Expand All @@ -117,7 +127,7 @@ func TestMakeMsiZip(t *testing.T) {
guide := map[string]OS{
"WindowsMSIPacker": LINUX,
}
rbm := CreateRemoteBuildManager(guide)
rbm := CreateRemoteBuildManager(guide, accountID)
defer rbm.Close()
require.NoError(t, rbm.MakeMsiZip("WindowsMSIPacker", "PUBLIC_REPO_TEST-1695063642"))
}
Expand Down

0 comments on commit f66182a

Please sign in to comment.