Skip to content

Commit 1849822

Browse files
committed
first commit
0 parents  commit 1849822

21 files changed

+1374
-0
lines changed

.dockerignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Dockerfile
2+
*.sh
3+
.git
4+
*.goex

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
aws-sdk-go-docker-image/aws-lambda-go/
2+
aws-sdk-go-docker-image/aws-sdk-go/

Dockerfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This dockerfile creates a docker image having the aws-utils-go lib copied to /go/src/stash.b2w/asp/aws-utils-go.git
2+
3+
FROM golang:1.13.4
4+
5+
RUN mkdir -p /go/src/stash.b2w/asp/aws-utils-go.git
6+
7+
COPY ./ /go/src/stash.b2w/asp/aws-utils-go.git/

Readme.md

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# aws-utils-go
2+
## Utilitários com interface simples para acessar serviços da AWS
3+
4+
A aws disponibiliza para a linguagem go a sdk aws-sdk-go, porém sua interface pode ser bem complicada.
5+
6+
Esta lib oferece funções com interfaces simples para executar tarefas comuns.
7+
8+
## Pacotes
9+
10+
* dynamodbutils: oferece interfaces simplificadas para as ações PutItem, GetItem, UpdateItem, PutItemWithConditional, FindOneFromIndex
11+
* s3utils: oferece GetObject, GetObjectAsString, ListObjects, PutObject.
12+
* snsutils: oferece SendMessage, SendMessageWithAttributes.
13+
* sqsutils: oferece SendMessage, ReadMessage, DeleteMessage, GetMessageAttribute
14+
* sessionutils: permite configurar a Session (aws-sdk-go/aws/session) que será utilizada pelos utils para se comunicarem com a AWS.
15+
* localstack (**experimental**): utilitários para iniciar/parar o localstack e seus serviços na máquina local. Está *experimental* ainda e sua interface deve mudar.
16+
17+
## Como importar e utilizar o código
18+
19+
#### Instalar o aws-utils-go no seu ambiente de desenvolvimento
20+
21+
Para ser utilizada, a lib aws-utils-go precisa estar deployada em seu diretorio $GOPATH/src.
22+
23+
Quem faz este deploy é o `go get stash.b2w/asp/aws-utils-go.git`. Acontece que este comando irá tentar fazer o download via https, e o nosso stash não suporta https, ele suporta ssh.
24+
25+
O `go get` utiliza o git client para fazer o download, portanto precisamos configurar o git client para usar ssh ao falar com o host *stash.b2w*.
26+
27+
Para configurar o git client, rode `vi ~/.gitconfig` e acrescente no final do arquivo as linhas abaixo:
28+
29+
```
30+
31+
insteadOf = https://stash.b2w/
32+
```
33+
34+
Feito isto, faça a instalação da lib rodando o `go get stash.b2w/asp/aws-utils-go.git`
35+
36+
#### Importar o aws-utils-go no seu código
37+
38+
Declare o import da lib como no exemplo abaixo:
39+
40+
```golang
41+
package main
42+
43+
import (
44+
"stash.b2w/asp/aws-utils-go.git/dynamodbutils"
45+
)
46+
...
47+
// save to the "Cities" table an instance of the "City" struct
48+
err := dynamodbutils.PutItem("Cities", city)
49+
...
50+
```
51+
52+
## Como extender o aws-utils-go
53+
54+
Se quiser extender o aws-utils-go o clone do projeto obrigatoriamente tem que ser feito no diretorio `$GOPATH/src/stash.b2w/asp/aws-utils-go.git`.
55+
56+
Isto é porque o próprio codigo do aws-utils-go, quando faz import de um pacote do mesmo projeto, utiliza no importe do pacote o prefixo `stash.b2w/asp/aws-utils-go.git`.
57+
58+
Para fazer o clone, use os comandos:
59+
60+
```shell
61+
mkdir -p $GOPATH/src/stash.b2w/asp/
62+
cd $GOPATH/src/stash.b2w/asp/
63+
git clone ssh://[email protected]/asp/aws-utils-go.git aws-utils-go.git
64+
```
65+
66+
## Gerar uma imagem docker com a lib 'aws-utils-go' embedada e publicar no Nexus B2W
67+
68+
Para que seu código que utilizou 'aws-utils-go' possa ser buildado no bamboo é preciso criar uma imagem docker para golang
69+
contendo esta lib deployada na GOPATH.
70+
Este projeto vem com um Dockerfile que cria esta imagem.
71+
72+
O script *build-docker-image.sh* builda a imagem com o nome de *b2wbuild/golang-aws-utils-go:TAG* e publica a mesma no repositório da B2W, *registry.b2w.io*.
73+
74+
Para buildar e publicar uma nova imagem, rode o script como no exemplo:
75+
76+
```shell
77+
./build-docker-image.sh "1.0"
78+
```
79+
80+
Onde "1.0" é uma tag name que irá identificar a versão da lib aws-utils-go que foi empacotada. A medida que esta lib for atualizada, novas tags deverão ser publicadas no repósitório.
81+
82+
O script *build-docker-image.sh* não permite sobrescrever uma tag existente e irá dar erro se já houver uma imagem com a mesma tag no repositório.
83+
84+
## Como buildar o seu código pelo bamboo usando uma imagem docker
85+
86+
#### Criar o 'build plan' usando a imagem gerada
87+
88+
No bamboo, quando for buildar o seu projeto, utilize a imagem docker de GO com a aws-utils-go embedada (a que foi criada no passo acima).
89+
90+
Veja o exemplo abaixo, o comando usado no "build plan" do bamboo usa a imagem *registry.b2w.io/b2wbuild/golang-aws-utils-go:1.0* para buildar a app:
91+
92+
```shell
93+
run --volume ${bamboo.build.working.directory}/ame-iot-auth:/go/src/ame-iot-auth --workdir /go/src/ame-iot-auth --rm registry.b2w.io/b2wbuild/golang-aws-utils-go:1.0 /bin/bash -c ./device-api/build.sh
94+
```
95+
96+

aws-sdk-go-docker-image/Dockerfile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This dockerfile creates a docker image having the aws-utils-go lib copied to /go/src/stash.b2w/asp/aws-utils-go.git
2+
# it also packages aws-sdk-go and aws-lambda-go
3+
4+
FROM golang:1.12.9
5+
6+
RUN mkdir -p /go/src/github.com/aws/aws-lambda-go
7+
COPY ./aws-lambda-go/ /go/src/github.com/aws/aws-lambda-go/
8+
9+
RUN mkdir -p /go/src/github.com/aws/aws-sdk-go
10+
COPY ./aws-sdk-go/ /go/src/github.com/aws/aws-sdk-go/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/bin/bash
2+
# This script builds a docker image and publishes it to "registry.b2w.io/b2wbuild"
3+
4+
function die {
5+
declare MSG="$@"
6+
echo -e "$0: Error: $MSG">&2
7+
exit 1
8+
}
9+
10+
# check for script dependencies
11+
dependencies=(jq curl git docker)
12+
for dep in ${dependencies[@]}; do
13+
which $dep > /dev/null || die "please install '$dep'"
14+
done
15+
16+
function existing_tags {
17+
curl http://registry.b2w.io/repository/docker-private/v2/b2wbuild/aws-sdk-go/tags/list 2>/dev/null| jq -r ".tags"
18+
}
19+
20+
function tag_exists {
21+
declare tag="$1"
22+
existing_tags | grep -q $tag
23+
}
24+
25+
function download_lib {
26+
declare libname="$1"
27+
if [ -d "$libname" ]; then
28+
cd $libname || return 1
29+
git pull || return 1
30+
cd ..
31+
else
32+
git clone https://github.com/aws/$libname.git
33+
fi
34+
}
35+
36+
TAG="$1"
37+
38+
[ -z "$TAG" ] && die "Parameter 'TAGNAME' cannot be empty.
39+
You must specify a tag name to be associated to the docker image that will be created.\n
40+
Example:\n$0 '1.0.0'.\n
41+
Existing tags are:\n
42+
$(existing_tags)"
43+
44+
tag_exists $TAG && die "TAG $TAG already exists in the repository"
45+
46+
47+
IMAGE_NAME="registry.b2w.io/b2wbuild/aws-sdk-go:${TAG}"
48+
49+
SEPARATOR="#######################################################################################"
50+
51+
echo "Downloading aws-sdk-go from github..."
52+
download_lib aws-sdk-go || die "failed downloading lib aws-sdk-go"
53+
54+
# make sure the chosen TAG maches the aws-sdk-go version
55+
version_file="aws-sdk-go/aws/version.go"
56+
[ -f $version_file ] || die "where is the file $version_file?"
57+
grep "\"$TAG\"" "$version_file" > /dev/null || \
58+
die "Tag '$TAG' does not match the version from the file $version_file.\nCurrent version is:$(cat $version_file | grep 'const SDKVersion' | cut -d'=' -f2)"
59+
60+
echo "Download aws-lambda-go from github"
61+
download_lib aws-lambda-go || die "failed downloading lib aws-lambda-go"
62+
63+
echo $SEPARATOR
64+
echo "creating image $IMAGE_NAME"
65+
66+
docker build -t $IMAGE_NAME . || die "failed to build docker image $IMAGE_NAME"
67+
68+
echo $SEPARATOR
69+
echo "build completed successfully."
70+
71+
echo $SEPARATOR
72+
echo "publishing image $IMAGE_NAME to the repository..."
73+
74+
docker push $IMAGE_NAME || die "failed to publish image to the repository."
75+
76+
echo $SEPARATOR
77+
echo "Image $IMAGE_NAME was published successfully."

aws-sdk-go-docker-image2/Dockerfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This dockerfile creates a docker image having the aws-utils-go lib copied to /go/src/stash.b2w/asp/aws-utils-go.git
2+
# it also packages aws-sdk-go and aws-lambda-go
3+
4+
FROM golang:1.12.9
5+
6+
RUN go get -v github.com/aws/aws-lambda-go/... && go get -v github.com/aws/aws-sdk-go/...
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/bin/bash
2+
# This script builds a docker image and publishes it to "registry.b2w.io/b2wbuild"
3+
4+
function die {
5+
declare MSG="$@"
6+
echo -e "$0: Error: $MSG">&2
7+
exit 1
8+
}
9+
10+
# check for script dependencies
11+
dependencies=(jq curl git docker)
12+
for dep in ${dependencies[@]}; do
13+
which $dep > /dev/null || die "please install '$dep'"
14+
done
15+
16+
function existing_tags {
17+
curl http://registry.b2w.io/repository/docker-private/v2/b2wbuild/aws-sdk-go/tags/list 2>/dev/null| jq -r ".tags"
18+
}
19+
20+
function tag_exists {
21+
declare tag="$1"
22+
existing_tags | grep -q $tag
23+
}
24+
25+
function download_lib {
26+
declare libname="$1"
27+
if [ -d "$libname" ]; then
28+
cd $libname || return 1
29+
git pull || return 1
30+
cd ..
31+
else
32+
git clone https://github.com/aws/$libname.git
33+
fi
34+
}
35+
36+
TAG="$1"
37+
38+
[ -z "$TAG" ] && die "Parameter 'TAGNAME' cannot be empty.
39+
You must specify a tag name to be associated to the docker image that will be created.\n
40+
Example:\n$0 '1.0.0'.\n
41+
Existing tags are:\n
42+
$(existing_tags)"
43+
44+
tag_exists $TAG && die "TAG $TAG already exists in the repository"
45+
46+
47+
IMAGE_NAME="registry.b2w.io/b2wbuild/aws-sdk-go:${TAG}"
48+
49+
SEPARATOR="#######################################################################################"
50+
51+
echo "Downloading aws-sdk-go from github..."
52+
download_lib aws-sdk-go || die "failed downloading lib aws-sdk-go"
53+
54+
# make sure the chosen TAG maches the aws-sdk-go version
55+
version_file="aws-sdk-go/aws/version.go"
56+
[ -f $version_file ] || die "where is the file $version_file?"
57+
grep "\"$TAG\"" "$version_file" > /dev/null || \
58+
die "Tag '$TAG' does not match the version from the file $version_file.\nCurrent version is:$(cat $version_file | grep 'const SDKVersion' | cut -d'=' -f2)"
59+
60+
echo "Download aws-lambda-go from github"
61+
download_lib aws-lambda-go || die "failed downloading lib aws-lambda-go"
62+
63+
echo $SEPARATOR
64+
echo "creating image $IMAGE_NAME"
65+
66+
docker build -t $IMAGE_NAME . || die "failed to build docker image $IMAGE_NAME"
67+
68+
echo $SEPARATOR
69+
echo "build completed successfully."
70+
71+
echo $SEPARATOR
72+
echo "publishing image $IMAGE_NAME to the repository..."
73+
74+
docker push $IMAGE_NAME || die "failed to publish image to the repository."
75+
76+
echo $SEPARATOR
77+
echo "Image $IMAGE_NAME was published successfully."

build-docker-image.sh

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/bash
2+
# This script builds a docker image and publishes it to "registry.b2w.io/b2wbuild"
3+
4+
function die {
5+
declare MSG="$@"
6+
echo -e "$0: Error: $MSG">&2
7+
exit 1
8+
}
9+
10+
# check for script dependencies
11+
dependencies=(jq curl git docker)
12+
for dep in ${dependencies[@]}; do
13+
which $dep > /dev/null || die "please install '$dep'"
14+
done
15+
16+
function existing_tags {
17+
curl http://registry.b2w.io/repository/docker-private/v2/b2wbuild/golang-aws-utils-go/tags/list 2>/dev/null| jq -r ".tags"
18+
}
19+
20+
function tag_exists {
21+
declare tag="$1"
22+
existing_tags | grep -q $tag
23+
}
24+
25+
TAG="$1"
26+
27+
[ -z "$TAG" ] && die "Parameter 'TAGNAME' cannot be empty.
28+
You must specify a tag name to be associated to the docker image that will be created.\n
29+
Example:\n$0 '1.0.0'.\n
30+
Existing tags are:\n
31+
$(existing_tags)"
32+
33+
tag_exists $TAG && die "TAG $TAG already exists in the repository"
34+
35+
36+
IMAGE_NAME="registry.b2w.io/b2wbuild/golang-aws-utils-go:${TAG}"
37+
38+
SEPARATOR="#######################################################################################"
39+
40+
echo $SEPARATOR
41+
echo "creating image $IMAGE_NAME"
42+
43+
docker build -t $IMAGE_NAME . || die "failed to build docker image $IMAGE_NAME"
44+
45+
echo $SEPARATOR
46+
echo "build completed successfully."
47+
48+
echo $SEPARATOR
49+
echo "publishing image $IMAGE_NAME to the repository..."
50+
51+
docker push $IMAGE_NAME || die "failed to publish image to the repository."
52+
53+
echo $SEPARATOR
54+
echo "Image $IMAGE_NAME was published successfully."

0 commit comments

Comments
 (0)