Skip to content

Commit

Permalink
setup CI/CD (#2)
Browse files Browse the repository at this point in the history
- fix command initialization
- add test
- add release and test github actions workflow
- fix README install guide
  • Loading branch information
Blue-Pix committed May 15, 2020
1 parent 57e898c commit caddf84
Show file tree
Hide file tree
Showing 12 changed files with 608 additions and 185 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: goreleaser

on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+"

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.14
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
25 changes: 25 additions & 0 deletions .github/workflows/run_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: test

on:
push:

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v2
- name: setup Go
uses: actions/setup-go@v2
with:
go-version: 1.14
- name: get dependencies
run: go get -v -t -d ./...
- name: setup aws credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-1
- name: run test
run: go test -v ./lib/...
35 changes: 35 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
project_name: abc
env:
- GO111MODULE=on
before:
hooks:
- go mod tidy
builds:
- main: .
binary: abc
goos:
- windows
- darwin
- linux
goarch:
- amd64
- 386
ldflags:
- -s -w
- -X main.Version={{.Version}}
env:
- CGO_ENABLED=0
archives:
- replacements:
darwin: darwin
linux: linux
windows: windows
386: i386
amd64: x86_64
format_overrides:
- goos: windows
format: zip
changelog:
skip: true
release:
draft: true
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
It wraps, pipes and extends AWS API.

# Install
Install source code with go, build the binaries yourself, and run as `abc` command.
You can install binaries from [releases](https://github.com/Blue-Pix/abc/releases),
or pull [repository](https://github.com/Blue-Pix/abc) and build the binaries yourself.

If you build yourself:
**Prerequisition**
- Git
- Go
Expand Down
178 changes: 3 additions & 175 deletions cmd/ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,185 +16,13 @@ limitations under the License.
package cmd

import (
"encoding/json"
"fmt"
"regexp"
"strconv"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/spf13/cobra"
"github.com/Blue-Pix/abc/lib/ami"
)

// amiCmd represents the ami command
var amiCmd = &cobra.Command{
Use: "ami",
Short: "return latest amazon linux ami",
Long: `[abc ami]
This command returns latest amazon linux ami as json format.
Internally it uses ssm get-parameters-by-path api. (https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/parameter-store-public-parameters.html)
Please configure your aws credential which has required policy.
By default, this returns serveral type of amis.
You can query it with options below. `,
Run: func(cmd *cobra.Command, args []string) {
amis := getAMIList()
if version, err := cmd.Flags().GetString("version"); version != "" && err == nil {
amis = filterByVersion(version, amis)
}
if virtualizationType, err := cmd.Flags().GetString("virtualization-type"); virtualizationType != "" && err == nil {
amis = filterByVirtualizationType(virtualizationType, amis)
}
if arch, err := cmd.Flags().GetString("arch"); arch != "" && err == nil {
amis = filterByArch(arch, amis)
}
if storage, err := cmd.Flags().GetString("storage"); storage != "" && err == nil {
amis = filterByStorage(storage, amis)
}
if minimal, err := cmd.Flags().GetString("minimal"); minimal != "" && err == nil {
amis = filterByMinimal(minimal, amis)
}
str := toJSON(amis)
fmt.Println(str)
},
}
var amiCmd = ami.NewCmd()

func init() {
amiCmd.SetOut(rootCmd.OutOrStdout())
rootCmd.AddCommand(amiCmd)
amiCmd.Flags().StringP("version", "v", "", "os version(1 or 2)")
amiCmd.Flags().StringP("virtualization-type", "V", "", "virtualization type(hvm or pv)")
amiCmd.Flags().StringP("arch", "a", "", "cpu architecture(x86_64 or arm64)")
amiCmd.Flags().StringP("storage", "s", "", "storage type(gp2, ebs or s3)")
amiCmd.Flags().StringP("minimal", "m", "", "if minimal image or not(true or false)")
}

const PATH = "/aws/service/ami-amazon-linux-latest"

type AMI struct {
Os string `json:"os"`
Version string `json:"version"`
VirtualizationType string `json:"virtualization_type"`
Arch string `json:"arch"`
Storage string `json:"storage"`
Minimal bool `json:"minimal"`
Id string `json:"id"`
Arn string `json:"arn"`
}

func getParametersByPath(sess *session.Session, token *string, path string) (*ssm.GetParametersByPathOutput, error) {
service := ssm.New(sess)
params := &ssm.GetParametersByPathInput{
NextToken: token,
Path: aws.String(path),
Recursive: aws.Bool(true),
}
return service.GetParametersByPath(params)
}

func toAMI(parameter *ssm.Parameter) AMI {
r := regexp.MustCompile(`^` + PATH + `\/([^\d]+)(\d)?-ami-(minimal\-)?(.+)-(.+)-(.+)$`)
list := r.FindAllStringSubmatch(aws.StringValue(parameter.Name), -1)
ami := AMI{
Os: list[0][1],
Version: "1",
VirtualizationType: list[0][4],
Arch: list[0][5],
Storage: list[0][6],
Id: aws.StringValue(parameter.Value),
Arn: aws.StringValue(parameter.ARN),
}
if list[0][2] != "" {
ami.Version = list[0][2]
}
if list[0][3] != "" {
ami.Minimal = true
}
return ami
}

func getAMIList() []AMI {
sess := session.Must(session.NewSession())
var parameters []*ssm.Parameter
var token *string = nil

for {
resp, err := getParametersByPath(sess, token, PATH)
if err != nil {
panic(err)
}
parameters = append(parameters, resp.Parameters...)

if resp.NextToken == nil {
break
}
token = resp.NextToken
}

var amis []AMI
for _, parameter := range parameters {
amis = append(amis, toAMI(parameter))
}
return amis
}

func filterByVersion(version string, amis []AMI) []AMI {
var newAmis []AMI
for _, ami := range amis {
if version == ami.Version {
newAmis = append(newAmis, ami)
}
}
return newAmis
}

func filterByVirtualizationType(virtualizationType string, amis []AMI) []AMI {
var newAmis []AMI
for _, ami := range amis {
if virtualizationType == ami.VirtualizationType {
newAmis = append(newAmis, ami)
}
}
return newAmis
}

func filterByArch(arch string, amis []AMI) []AMI {
var newAmis []AMI
for _, ami := range amis {
if arch == ami.Arch {
newAmis = append(newAmis, ami)
}
}
return newAmis
}

func filterByStorage(storage string, amis []AMI) []AMI {
var newAmis []AMI
for _, ami := range amis {
if storage == ami.Storage {
newAmis = append(newAmis, ami)
}
}
return newAmis
}

func filterByMinimal(minimal string, amis []AMI) []AMI {
var newAmis []AMI
for _, ami := range amis {
m, _ := strconv.ParseBool(minimal)
if m == ami.Minimal {
newAmis = append(newAmis, ami)
}
}
return newAmis
}

func toJSON(amis []AMI) string {
jsonBytes, err := json.Marshal(amis)
if err != nil {
panic(err)
}
jsonStr := string(jsonBytes)
return jsonStr
}
13 changes: 4 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,19 @@ package cmd

import (
"fmt"
"github.com/spf13/cobra"
"os"

"github.com/Blue-Pix/abc/lib/root"
"github.com/spf13/cobra"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)

var cfgFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "abc",
Short: "helper command to become friends with AWS🤝",
Long: `A usage, please read each sub commands`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
var rootCmd = root.NewCmd()

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.4.0
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
golang.org/x/text v0.3.2 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
)

0 comments on commit caddf84

Please sign in to comment.