diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 000000000..f4f0604b4 Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore index 26f73e312..17d49fc24 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,5 @@ hs_err_pid* # IDEA files .idea + +.DS_Store \ No newline at end of file diff --git a/packs/terraform/.dockerignore b/packs/terraform/.dockerignore new file mode 100644 index 000000000..762752c83 --- /dev/null +++ b/packs/terraform/.dockerignore @@ -0,0 +1,5 @@ +draft.toml +charts/ +NOTICE +LICENSE +README.md \ No newline at end of file diff --git a/packs/terraform/.helmignore b/packs/terraform/.helmignore new file mode 100644 index 000000000..747e6e94a --- /dev/null +++ b/packs/terraform/.helmignore @@ -0,0 +1,27 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*~ +# Various IDEs +.project +.idea/ +*.tmproj +*.png + +# known compile time folders +target/ +node_modules/ +vendor/ \ No newline at end of file diff --git a/packs/terraform/Dockerfile b/packs/terraform/Dockerfile new file mode 100644 index 000000000..c4f72f070 --- /dev/null +++ b/packs/terraform/Dockerfile @@ -0,0 +1,14 @@ +#FROM hashicorp/terraform:light +FROM jenkinsxio/builder-base:latest + +# Install your tools and libraries +RUN yum -y install wget unzip && \ + wget https://releases.hashicorp.com/terraform/0.11.14/terraform_0.11.14_linux_amd64.zip && \ + unzip ./terraform_0.11.14_linux_amd64.zip -d /usr/local/bin/ + + +#RUN mkdir -p /usr/src/app +#WORKDIR /usr/src/app +#COPY . /usr/src/app + +ENTRYPOINT ["terraform", "-v"] \ No newline at end of file diff --git a/packs/terraform/charts/.helmignore b/packs/terraform/charts/.helmignore new file mode 100644 index 000000000..f0c131944 --- /dev/null +++ b/packs/terraform/charts/.helmignore @@ -0,0 +1,21 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*~ +# Various IDEs +.project +.idea/ +*.tmproj diff --git a/packs/terraform/charts/Chart.yaml b/packs/terraform/charts/Chart.yaml new file mode 100644 index 000000000..bdb8d449b --- /dev/null +++ b/packs/terraform/charts/Chart.yaml @@ -0,0 +1,5 @@ +name: terraform +description: A Helm chart for Kubernetes +apiVersion: v1 +version: v0.1.0 +icon: https://raw.githubusercontent.com/jenkins-x/jenkins-x-platform/d273e09/images/nodejs.png \ No newline at end of file diff --git a/packs/terraform/charts/Makefile b/packs/terraform/charts/Makefile new file mode 100644 index 000000000..bd29b9c44 --- /dev/null +++ b/packs/terraform/charts/Makefile @@ -0,0 +1,48 @@ +CHART_REPO := http://jenkins-x-chartmuseum:8080 +CURRENT=$(pwd) +NAME := REPLACE_ME_APP_NAME +OS := $(shell uname) +RELEASE_VERSION := $(shell cat ../../VERSION) + +build: clean + rm -rf requirements.lock + helm dependency build + helm lint + +install: clean build + helm install . --name ${NAME} + +upgrade: clean build + helm upgrade ${NAME} . + +delete: + helm delete --purge ${NAME} + +clean: + rm -rf charts + rm -rf ${NAME}*.tgz + +release: clean + helm dependency build + helm lint + helm init --client-only + helm package . + curl --fail -u $(CHARTMUSEUM_CREDS_USR):$(CHARTMUSEUM_CREDS_PSW) --data-binary "@$(NAME)-$(shell sed -n 's/^version: //p' Chart.yaml).tgz" $(CHART_REPO)/api/charts + rm -rf ${NAME}*.tgz% + +tag: +ifeq ($(OS),Darwin) + sed -i "" -e "s/version:.*/version: $(RELEASE_VERSION)/" Chart.yaml + sed -i "" -e "s/tag:.*/tag: $(RELEASE_VERSION)/" values.yaml +else ifeq ($(OS),Linux) + sed -i -e "s/version:.*/version: $(RELEASE_VERSION)/" Chart.yaml + sed -i -e "s/repository:.*/repository: $(DOCKER_REGISTRY)\/$(ORG)\/$(APP_NAME)/" values.yaml + sed -i -e "s/tag:.*/tag: $(RELEASE_VERSION)/" values.yaml +else + echo "platfrom $(OS) not supported to release from" + exit -1 +endif + git add --all + git commit -m "release $(RELEASE_VERSION)" --allow-empty # if first release then no verion update is performed + git tag -fa v$(RELEASE_VERSION) -m "Release version $(RELEASE_VERSION)" + git push origin v$(RELEASE_VERSION) diff --git a/packs/terraform/charts/templates/NOTES.txt b/packs/terraform/charts/templates/NOTES.txt new file mode 100644 index 000000000..da948fa27 --- /dev/null +++ b/packs/terraform/charts/templates/NOTES.txt @@ -0,0 +1,15 @@ + +{{- if contains "NodePort" .Values.service.type }} + Get the application URL by running these commands: + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ template "fullname" . }}) + export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") + echo http://$NODE_IP:$NODE_PORT/login +{{- else if contains "LoadBalancer" .Values.service.type }} + Get the application URL by running these commands: + NOTE: It may take a few minutes for the LoadBalancer IP to be available. + You can watch the status of by running 'kubectl get svc -w {{ template "fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "fullname" . }} -o jsonpath='{.status.loadBalancer.ingress[0].ip}') + echo http://$SERVICE_IP:{{ .Values.service.externalPort }} +{{- else }} + http://{{ .Release.Name }}.{{ .Values.basedomain }} to access your application +{{- end }} diff --git a/packs/terraform/charts/templates/_helpers.tpl b/packs/terraform/charts/templates/_helpers.tpl new file mode 100644 index 000000000..f0d83d2ed --- /dev/null +++ b/packs/terraform/charts/templates/_helpers.tpl @@ -0,0 +1,16 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Expand the name of the chart. +*/}} +{{- define "name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +*/}} +{{- define "fullname" -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} +{{- end -}} diff --git a/packs/terraform/charts/templates/deployment.yaml b/packs/terraform/charts/templates/deployment.yaml new file mode 100644 index 000000000..58af0d202 --- /dev/null +++ b/packs/terraform/charts/templates/deployment.yaml @@ -0,0 +1,51 @@ +{{- if .Values.knativeDeploy }} +{{- else }} +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: {{ template "fullname" . }} + labels: + draft: {{ default "draft-app" .Values.draft }} + chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}" +spec: + replicas: {{ .Values.replicaCount }} + template: + metadata: + labels: + draft: {{ default "draft-app" .Values.draft }} + app: {{ template "fullname" . }} +{{- if .Values.podAnnotations }} + annotations: +{{ toYaml .Values.podAnnotations | indent 8 }} +{{- end }} + spec: + containers: + - name: {{ .Chart.Name }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + env: +{{- range $pkey, $pval := .Values.env }} + - name: {{ $pkey }} + value: {{ $pval }} +{{- end }} + ports: + - containerPort: {{ .Values.service.internalPort }} + livenessProbe: + httpGet: + path: {{ .Values.probePath }} + port: {{ .Values.service.internalPort }} + initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }} + periodSeconds: {{ .Values.livenessProbe.periodSeconds }} + successThreshold: {{ .Values.livenessProbe.successThreshold }} + timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }} + readinessProbe: + httpGet: + path: {{ .Values.probePath }} + port: {{ .Values.service.internalPort }} + periodSeconds: {{ .Values.readinessProbe.periodSeconds }} + successThreshold: {{ .Values.readinessProbe.successThreshold }} + timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }} + resources: +{{ toYaml .Values.resources | indent 12 }} + terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }} +{{- end }} diff --git a/packs/terraform/charts/templates/ksvc.yaml b/packs/terraform/charts/templates/ksvc.yaml new file mode 100644 index 000000000..c2f9e6cf9 --- /dev/null +++ b/packs/terraform/charts/templates/ksvc.yaml @@ -0,0 +1,41 @@ +{{- if .Values.knativeDeploy }} +apiVersion: serving.knative.dev/v1alpha1 +kind: Service +metadata: +{{- if .Values.service.name }} + name: {{ .Values.service.name }} +{{- else }} + name: {{ template "fullname" . }} +{{- end }} + labels: + chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}" +spec: + runLatest: + configuration: + revisionTemplate: + spec: + container: + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + env: +{{- range $pkey, $pval := .Values.env }} + - name: {{ $pkey }} + value: {{ $pval }} +{{- end }} + livenessProbe: + httpGet: + path: {{ .Values.probePath }} + initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }} + periodSeconds: {{ .Values.livenessProbe.periodSeconds }} + successThreshold: {{ .Values.livenessProbe.successThreshold }} + timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }} + readinessProbe: + httpGet: + path: {{ .Values.probePath }} + periodSeconds: {{ .Values.readinessProbe.periodSeconds }} + successThreshold: {{ .Values.readinessProbe.successThreshold }} + timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }} + terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }} + resources: +{{ toYaml .Values.resources | indent 12 }} +{{- end }} diff --git a/packs/terraform/charts/templates/service.yaml b/packs/terraform/charts/templates/service.yaml new file mode 100644 index 000000000..9321361e3 --- /dev/null +++ b/packs/terraform/charts/templates/service.yaml @@ -0,0 +1,26 @@ +{{- if .Values.knativeDeploy }} +{{- else }} +apiVersion: v1 +kind: Service +metadata: +{{- if .Values.service.name }} + name: {{ .Values.service.name }} +{{- else }} + name: {{ template "fullname" . }} +{{- end }} + labels: + chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}" +{{- if .Values.service.annotations }} + annotations: +{{ toYaml .Values.service.annotations | indent 4 }} +{{- end }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.externalPort }} + targetPort: {{ .Values.service.internalPort }} + protocol: TCP + name: http + selector: + app: {{ template "fullname" . }} +{{- end }} diff --git a/packs/terraform/charts/values.yaml b/packs/terraform/charts/values.yaml new file mode 100644 index 000000000..9df6cbf50 --- /dev/null +++ b/packs/terraform/charts/values.yaml @@ -0,0 +1,41 @@ +# Default values for Ruby. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +replicaCount: 2 +image: + repository: draft + tag: dev + pullPolicy: IfNotPresent + +# define environment variables here as a map of key: value +env: + +# enable this flag to use knative serve to deploy the app +knativeDeploy: false + +service: + name: REPLACE_ME_APP_NAME + type: ClusterIP + externalPort: 80 + internalPort: 3000 + annotations: + fabric8.io/expose: "true" + fabric8.io/ingress.annotations: "kubernetes.io/ingress.class: nginx" +resources: + limits: + cpu: 100m + memory: 128Mi + requests: + cpu: 100m + memory: 128Mi +probePath: / +livenessProbe: + initialDelaySeconds: 60 + periodSeconds: 10 + successThreshold: 1 + timeoutSeconds: 1 +readinessProbe: + periodSeconds: 10 + successThreshold: 1 + timeoutSeconds: 1 +terminationGracePeriodSeconds: 10 diff --git a/packs/terraform/pipeline.yaml b/packs/terraform/pipeline.yaml new file mode 100644 index 000000000..4f3fc82b5 --- /dev/null +++ b/packs/terraform/pipeline.yaml @@ -0,0 +1,48 @@ +extends: + import: classic + file: pipeline.yaml +agent: + label: builder-terraform + container: terraform +pipelines: + pullRequest: + build: + steps: + - sh: export VERSION=$PREVIEW_VERSION && skaffold build -f skaffold.yaml + name: container-build + postBuild: + steps: + - sh: jx step post build --image $DOCKER_REGISTRY/$ORG/$APP_NAME:$PREVIEW_VERSION + name: post-build + promote: + steps: + - dir: ./charts/preview + steps: + - sh: make preview + name: make-preview + - sh: jx preview --app $APP_NAME --dir ../.. + name: jx-preview + + release: + setVersion: + steps: + - sh: jx step next-version --use-git-tag-only --tag + name: tag-with-new-version + build: + steps: + - sh: export VERSION=`cat VERSION` && skaffold build -f skaffold.yaml + name: container-build + - sh: jx step post build --image $DOCKER_REGISTRY/$ORG/$APP_NAME:\$(cat VERSION) + name: post-build + promote: + steps: + - dir: ./charts/REPLACE_ME_APP_NAME + steps: + - sh: jx step changelog --version v\$(cat ../../VERSION) + name: changelog + - comment: release the helm chart + sh: jx step helm release + name: helm-release + - comment: promote through all 'Auto' promotion Environments + sh: jx promote -b --all-auto --timeout 1h --version \$(cat ../../VERSION) + name: jx-promote diff --git a/packs/terraform/preview/Chart.yaml b/packs/terraform/preview/Chart.yaml new file mode 100644 index 000000000..0c32270fe --- /dev/null +++ b/packs/terraform/preview/Chart.yaml @@ -0,0 +1,5 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +name: preview +icon: https://raw.githubusercontent.com/jenkins-x/jenkins-x-platform/master/images/java.png +version: 0.1.0-SNAPSHOT diff --git a/packs/terraform/preview/Makefile b/packs/terraform/preview/Makefile new file mode 100755 index 000000000..ef73f4fcf --- /dev/null +++ b/packs/terraform/preview/Makefile @@ -0,0 +1,18 @@ +OS := $(shell uname) + +preview: +ifeq ($(OS),Darwin) + sed -i "" -e "s/version:.*/version: $(PREVIEW_VERSION)/" Chart.yaml + sed -i "" -e "s/version:.*/version: $(PREVIEW_VERSION)/" ../*/Chart.yaml + sed -i "" -e "s/tag:.*/tag: $(PREVIEW_VERSION)/" values.yaml +else ifeq ($(OS),Linux) + sed -i -e "s/version:.*/version: $(PREVIEW_VERSION)/" Chart.yaml + sed -i -e "s/version:.*/version: $(PREVIEW_VERSION)/" ../*/Chart.yaml + sed -i -e "s|repository:.*|repository: $(DOCKER_REGISTRY)\/REPLACE_ME_ORG\/REPLACE_ME_APP_NAME|" values.yaml + sed -i -e "s/tag:.*/tag: $(PREVIEW_VERSION)/" values.yaml +else + echo "platfrom $(OS) not supported to release from" + exit -1 +endif + echo " version: $(PREVIEW_VERSION)" >> requirements.yaml + jx step helm build diff --git a/packs/terraform/preview/requirements.yaml b/packs/terraform/preview/requirements.yaml new file mode 100644 index 000000000..c8ac6acff --- /dev/null +++ b/packs/terraform/preview/requirements.yaml @@ -0,0 +1,16 @@ +# !! File must end with empty line !! +dependencies: +- alias: expose + name: exposecontroller + repository: http://chartmuseum.jenkins-x.io + version: 2.3.92 +- alias: cleanup + name: exposecontroller + repository: http://chartmuseum.jenkins-x.io + version: 2.3.92 + + # !! "alias: preview" must be last entry in dependencies array !! + # !! Place custom dependencies above !! +- alias: preview + name: REPLACE_ME_APP_NAME + repository: file://../REPLACE_ME_APP_NAME diff --git a/packs/terraform/preview/values.yaml b/packs/terraform/preview/values.yaml new file mode 100644 index 000000000..b53ceaa57 --- /dev/null +++ b/packs/terraform/preview/values.yaml @@ -0,0 +1,22 @@ + +expose: + Annotations: + helm.sh/hook: post-install,post-upgrade + helm.sh/hook-delete-policy: hook-succeeded + config: + exposer: Ingress + http: true + tlsacme: false + +cleanup: + Args: + - --cleanup + Annotations: + helm.sh/hook: pre-delete + helm.sh/hook-delete-policy: hook-succeeded + +preview: + image: + repository: + tag: + pullPolicy: IfNotPresent \ No newline at end of file diff --git a/packs/terraform/skaffold.yaml b/packs/terraform/skaffold.yaml new file mode 100644 index 000000000..85f493487 --- /dev/null +++ b/packs/terraform/skaffold.yaml @@ -0,0 +1,30 @@ +apiVersion: skaffold/v1beta2 +kind: Config +build: + artifacts: + - image: changeme + context: . + docker: {} + tagPolicy: + envTemplate: + template: '{{.DOCKER_REGISTRY}}/REPLACE_ME_ORG/REPLACE_ME_APP_NAME:{{.VERSION}}' + local: {} +deploy: + kubectl: {} +profiles: +- name: dev + build: + artifacts: + - docker: {} + tagPolicy: + envTemplate: + template: '{{.DOCKER_REGISTRY}}/REPLACE_ME_ORG/REPLACE_ME_APP_NAME:{{.DIGEST_HEX}}' + local: {} + deploy: + helm: + releases: + - name: REPLACE_ME_APP_NAME + chartPath: charts/REPLACE_ME_APP_NAME + setValueTemplates: + image.repository: '{{.DOCKER_REGISTRY}}/REPLACE_ME_ORG/REPLACE_ME_APP_NAME' + image.tag: '{{.DIGEST_HEX}}' \ No newline at end of file