# Note: If you change the GOVER of the installer `FROM` you probably want to also change it twice (later on in this file)
#       inside the final multi-stage image
ARG GOVER=1.11.1
FROM golang:$GOVER as gobase

ARG PROTOGO=1.2.0
ARG TWIRP=5.5.0
ARG PROTO_LINT=0.2.1
ARG GOMETALINTER=2.0.11
ARG SLOGEN=1.4
ARG DEP=0.5.0
ARG ECSRUN=1.3.2
ARG CFEXECUTE=0.5.1
ARG TERRAFORM_PAGERDUTY=1.2.0
ARG TERRAFORM_GRAFANA=1.2.0-fiximportsbools
ARG SANDSTORM_CLI

# install protoc-gen-go
RUN mkdir -p "$GOPATH/src/github.com/golang"
RUN cd "$GOPATH/src/github.com/golang" && git clone https://github.com/golang/protobuf.git && cd protobuf && git reset --hard v${PROTOGO}
RUN go install github.com/golang/protobuf/protoc-gen-go

# Install twirp
RUN mkdir -p "$GOPATH/src/github.com/twitchtv"
RUN cd "$GOPATH/src/github.com/twitchtv" && git clone https://github.com/twitchtv/twirp.git && cd twirp && git reset --hard v${TWIRP}
RUN go install github.com/twitchtv/twirp/protoc-gen-twirp

# Install proto linter
RUN mkdir -p "$GOPATH/src/github.com/ckaznocha"
RUN cd "$GOPATH/src/github.com/ckaznocha" && git clone https://github.com/ckaznocha/protoc-gen-lint.git && cd protoc-gen-lint && git reset --hard v${PROTO_LINT}
RUN go install github.com/ckaznocha/protoc-gen-lint

# Install ecsrun
RUN mkdir -p "$GOPATH/src/github.com/cep21"
RUN cd "$GOPATH/src/github.com/cep21" && git clone https://github.com/cep21/ecsrun.git && cd ecsrun && git reset --hard v${ECSRUN}
RUN go install github.com/cep21/ecsrun

# Install cfexecute
RUN mkdir -p "$GOPATH/src/github.com/cep21"
RUN cd "$GOPATH/src/github.com/cep21" && git clone https://github.com/cep21/cfexecute.git && cd cfexecute && git reset --hard v${CFEXECUTE}
RUN go install github.com/cep21/cfexecute

# Install gometalinter
RUN mkdir -p "$GOPATH/src/github.com/alecthomas"
RUN cd "$GOPATH/src/github.com/alecthomas" && git clone https://github.com/alecthomas/gometalinter.git && cd gometalinter && git reset --hard v${GOMETALINTER}
RUN go install github.com/alecthomas/gometalinter
RUN gometalinter --install

# Install dep
RUN mkdir -p "$GOPATH/src/github.com/golang"
RUN cd  "$GOPATH/src/github.com/golang" && git clone https://github.com/golang/dep.git && cd dep && git reset --hard v${DEP}
RUN go install github.com/golang/dep/cmd/dep

# Install slogenerator
RUN mkdir -p "$GOPATH/src/code.justin.tv/hygienic"
COPY twirpserviceslohook "$GOPATH/src/code.justin.tv/hygienic/twirpserviceslohook"
RUN cd "$GOPATH/src/code.justin.tv/hygienic/twirpserviceslohook" && git reset --hard v${SLOGEN}
RUN go install code.justin.tv/hygienic/twirpserviceslohook/cmd/slogenerator

# Install sandstorm CLI
RUN mkdir -p "$GOPATH/src/code.justin.tv/systems"
COPY sandstorm "$GOPATH/src/code.justin.tv/systems/sandstorm"
RUN cd "$GOPATH/src/code.justin.tv/systems/sandstorm" && git reset --hard v${SANDSTORM_CLI}
RUN go install code.justin.tv/systems/sandstorm/cmd/sandstorm-cli

# Install terraform plugins
RUN mkdir -p /terraform_plugins
# -- Install pagerduty
RUN mkdir -p "$GOPATH/src/github.com/terraform-providers"
RUN cd "$GOPATH/src/github.com/terraform-providers" && git clone https://github.com/terraform-providers/terraform-provider-pagerduty.git && cd terraform-provider-pagerduty && git reset --hard v${TERRAFORM_PAGERDUTY}
RUN go install github.com/terraform-providers/terraform-provider-pagerduty
RUN mv /go/bin/terraform-provider-pagerduty /terraform_plugins/terraform-provider-pagerduty_v${TERRAFORM_PAGERDUTY}
# -- Install custom grafana
RUN mkdir -p "$GOPATH/src/code.justin.tv/twitch"
COPY terraform-provider-grafana "$GOPATH/src/github.com/terraform-providers/terraform-provider-grafana"
RUN cd "$GOPATH/src/github.com/terraform-providers/terraform-provider-grafana" && git reset --hard v${TERRAFORM_GRAFANA}
RUN go install github.com/terraform-providers/terraform-provider-grafana
RUN mv /go/bin/terraform-provider-grafana /terraform_plugins/terraform-provider-grafana_v${TERRAFORM_GRAFANA}


ARG GOVER=1.11.1
FROM golang:$GOVER
ARG PROTOC=3.6.1
# Note: Same version as jenkins
ARG DOCKER=18.03.1
ARG AWS_CLI=1.16.33
ARG HADOLINT=1.15.0
ARG TERRAFORM=0.11.10

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

RUN apt-get update && apt-get install -y --no-install-recommends \
  # For protoc install
  unzip=6.0\* \
  # For aws-cli
  python-setuptools=33.1\* python-pip=9.0\* \
  # For midway auth parsing
  jq=1.5\* \
  # For docker
  apt-transport-https=1.4.8 ca-certificates=20161130\* curl=7.52\* gnupg2=2.1.\* software-properties-common=0.96.20.\* \
   && apt-get clean && rm -rf /var/lib/apt/lists/*

# Install protoc
RUN mkdir -p /lib/protoc && cd /lib/protoc && wget "https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC}/protoc-${PROTOC}-linux-x86_64.zip" && unzip protoc-${PROTOC}-linux-x86_64.zip
RUN ln -s /lib/protoc/bin/protoc /bin/protoc

# Install docker (from https://docs.docker.com/install/linux/docker-ce/debian/#set-up-the-repository)
RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
# Note: this is the debian package.  If you switch OS, you may need a different image
RUN add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
RUN apt-get update && apt-get install -y --no-install-recommends docker-ce=${DOCKER}\* \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Install aws-cli
RUN pip install awscli==${AWS_CLI}

# Install hadolint
RUN wget -O /bin/hadolint https://github.com/hadolint/hadolint/releases/download/v${HADOLINT}/hadolint-Linux-x86_64 && chmod a+x /bin/hadolint

# Install terraform
RUN mkdir -p /lib/terraform && cd /lib/terraform && wget https://releases.hashicorp.com/terraform/${TERRAFORM}/terraform_${TERRAFORM}_linux_amd64.zip && unzip terraform_${TERRAFORM}_linux_amd64.zip && mv terraform /bin && rm terraform_${TERRAFORM}_linux_amd64.zip
# Install terraform dependencies
COPY --from=gobase /terraform_plugins/* /root/.terraform.d/plugins/
# Install left over go binaries
COPY --from=gobase /go/bin/* /bin/

ENTRYPOINT ["./make.sh"]
