diff --git a/nginx-php-fpm-apb/Dockerfile b/nginx-php-fpm-apb/Dockerfile new file mode 100644 index 0000000..a4baea4 --- /dev/null +++ b/nginx-php-fpm-apb/Dockerfile @@ -0,0 +1,16 @@ +FROM ansibleplaybookbundle/apb-base + +LABEL "com.redhat.apb.version"="0.1.0" +LABEL "com.redhat.apb.spec"=\ +"bmFtZTogbmdpbngtcGhwLWZwbS1hcGIKaW1hZ2U6IGFuc2libGVwbGF5Ym9va2J1bmRsZS9uZ2lu\ +eC1waHAtZnBtLWFwYgpkZXNjcmlwdGlvbjogVGhpcyBpcyBhbiBhcHBsaWNhdGlvbiB3aXRoIHR3\ +byBjb250YWluZXJzIG5naW54IGFuZCBwaHAtZnBtLgpiaW5kYWJsZTogRmFsc2UKYXN5bmM6IG9w\ +dGlvbmFsCm1ldGFkYXRhOgogIGRpc3BsYXlOYW1lOiBuZ2lueC1waHAtZnBtCnBsYW5zOgogIC0g\ +bmFtZTogZGVmYXVsdAogICAgZGVzY3JpcHRpb246IFRoaXMgZGVmYXVsdCBwbGFuIGRlcGxveXMg\ +bmdpbngtcGhwLWZwbS1hcGIKICAgIGZyZWU6IFRydWUKICAgIG1ldGFkYXRhOiB7fQogICAgcGFy\ +YW1ldGVyczogW10=" + +COPY playbooks /opt/apb/actions +COPY roles /opt/ansible/roles +RUN chmod -R g=u /opt/{ansible,apb} +USER apb diff --git a/nginx-php-fpm-apb/README.md b/nginx-php-fpm-apb/README.md new file mode 100644 index 0000000..109f8c5 --- /dev/null +++ b/nginx-php-fpm-apb/README.md @@ -0,0 +1,19 @@ +nginx-php-fpm-apb +====================== + +An apb for deploying nginx and php-fpm containers. + +## What it does +* Deploys 1 nginx container and 1 php-fpm container and links them. + +## Requirements +* Existing project + +## Parameters +* namespace: name of an existing namespace / project + +## Running the application +`docker run -e "OPENSHIFT_TARGET=" -e "OPENSHIFT_TOKEN"=" ansibleplaybookbundle/nginx-php-fpm-apb provision --extra-vars "namespace="` + +## Tearing down the application +`docker run -e "OPENSHIFT_TARGET=" -e "OPENSHIFT_TOKEN"=" ansibleplaybookbundle/nginx-php-fpm-apb deprovision --extra-vars "namespace="` diff --git a/nginx-php-fpm-apb/apb.yml b/nginx-php-fpm-apb/apb.yml new file mode 100644 index 0000000..d6de496 --- /dev/null +++ b/nginx-php-fpm-apb/apb.yml @@ -0,0 +1,13 @@ +name: nginx-php-fpm-apb +image: ansibleplaybookbundle/nginx-php-fpm-apb +description: This is an application with two containers nginx and php-fpm. +bindable: False +async: optional +metadata: + displayName: nginx-php-fpm +plans: + - name: default + description: This default plan deploys nginx-php-fpm-apb + free: True + metadata: {} + parameters: [] \ No newline at end of file diff --git a/nginx-php-fpm-apb/playbooks/deprovision.yml b/nginx-php-fpm-apb/playbooks/deprovision.yml new file mode 100644 index 0000000..0dbb910 --- /dev/null +++ b/nginx-php-fpm-apb/playbooks/deprovision.yml @@ -0,0 +1,10 @@ +- name: nginx-php-fpm-apb playbook to deprovision the application + hosts: localhost + gather_facts: false + connection: local + roles: + - role: ansible.kubernetes-modules + install_python_requirements: no + - role: ansibleplaybookbundle.asb-modules + - role: deprovision-nginx-php-fpm-apb + playbook_debug: false \ No newline at end of file diff --git a/nginx-php-fpm-apb/playbooks/provision.yml b/nginx-php-fpm-apb/playbooks/provision.yml new file mode 100644 index 0000000..40523b8 --- /dev/null +++ b/nginx-php-fpm-apb/playbooks/provision.yml @@ -0,0 +1,10 @@ +- name: nginx-php-fpm-apb playbook to provision the application + hosts: localhost + gather_facts: false + connection: local + roles: + - role: ansible.kubernetes-modules + install_python_requirements: no + - role: ansibleplaybookbundle.asb-modules + - role: provision-nginx-php-fpm-apb + playbook_debug: false \ No newline at end of file diff --git a/nginx-php-fpm-apb/roles/deprovision-nginx-php-fpm-apb/tasks/main.yml b/nginx-php-fpm-apb/roles/deprovision-nginx-php-fpm-apb/tasks/main.yml new file mode 100644 index 0000000..c28dd0b --- /dev/null +++ b/nginx-php-fpm-apb/roles/deprovision-nginx-php-fpm-apb/tasks/main.yml @@ -0,0 +1,42 @@ +############################################################################## +## Deprovision nginx-php-fpm +## This role executes much of the needed functionality to deprovision an +## application using an Ansible Playbook Bundle and delete resources created +## in the sample apb generated by the provision role. +############################################################################## + + +############################################################################## +## Deprovision a route +############################################################################## +- openshift_v1_route: + name: nginx-php-fpm + namespace: '{{ namespace }}' + state: absent + + +############################################################################## +## Deprovision a service +############################################################################## +- k8s_v1_service: + name: nginx-php-fpm + namespace: '{{ namespace }}' + state: absent + + +############################################################################## +## Deprovision a deployment config +## When removing a Deployment Config, OpenShift will automatically clean up +## its associated resources like replication controllers and pods +############################################################################## +- openshift_v1_deployment_config: + name: nginx-php-fpm + namespace: '{{ namespace }}' + state: absent + +- name: Delete NGINX configmap + shell: oc delete configmap {{ item }} -n {{ namespace }} + with_items: + - nginx-conf + - site-files + diff --git a/nginx-php-fpm-apb/roles/provision-nginx-php-fpm-apb/files/default.conf b/nginx-php-fpm-apb/roles/provision-nginx-php-fpm-apb/files/default.conf new file mode 100644 index 0000000..62b2357 --- /dev/null +++ b/nginx-php-fpm-apb/roles/provision-nginx-php-fpm-apb/files/default.conf @@ -0,0 +1,23 @@ +server { + listen 8080; + server_name localhost; + + location / { + root /var/www/html; + index index.php index.html; + } + + # redirect server error pages to the static page /50x.html + # + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /var/www/html; + } + + location ~ \.php$ { + fastcgi_pass nginx-php-fpm:9000; + fastcgi_index index.php; + fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; + include fastcgi_params; + } +} diff --git a/nginx-php-fpm-apb/roles/provision-nginx-php-fpm-apb/files/index.php b/nginx-php-fpm-apb/roles/provision-nginx-php-fpm-apb/files/index.php new file mode 100644 index 0000000..dcd1cee --- /dev/null +++ b/nginx-php-fpm-apb/roles/provision-nginx-php-fpm-apb/files/index.php @@ -0,0 +1,8 @@ + + + PHP Test + + + + + \ No newline at end of file diff --git a/nginx-php-fpm-apb/roles/provision-nginx-php-fpm-apb/tasks/main.yml b/nginx-php-fpm-apb/roles/provision-nginx-php-fpm-apb/tasks/main.yml new file mode 100644 index 0000000..fdf7af0 --- /dev/null +++ b/nginx-php-fpm-apb/roles/provision-nginx-php-fpm-apb/tasks/main.yml @@ -0,0 +1,121 @@ +############################################################################## +## Provision nginx-php-fpm +## This role executes much of the needed functionality to provision an +## application using an Ansible Playbook Bundle. Included in the comments +## below are some sample resources for getting started deploying an application +## to OpenShift. +############################################################################## + + +############################################################################## +## An OpenShift Origin deployment configuration provides a replication +## controller, spins up pods, and also provides the ability to transition from +## one deployment of an image to a new one. +## https://docs.openshift.org/latest/architecture/core_concepts/deployments.html#deployments-and-deployment-configurations +############################################################################## +- name: create deployment config + openshift_v1_deployment_config: + name: nginx-php-fpm + namespace: '{{ namespace }}' + labels: + app: '{{ namespace }}' + service: nginx-php-fpm + replicas: 1 + selector: + app: '{{ namespace }}' + service: nginx-php-fpm + spec_template_metadata_labels: + app: '{{ namespace }}' + service: nginx-php-fpm + containers: + - env: + image: php:7.1-fpm-alpine + imagePullPolicy: IfNotPresent + name: php-fpm + ports: + - container_port: 9000 + protocol: TCP + volumeMounts: + - mountPath: /var/www/html + name: site + - env: + image: toccoag/openshift-nginx + imagePullPolicy: IfNotPresent + name: nginx + ports: + - container_port: 80 + protocol: TCP + volumeMounts: + - mountPath: /etc/nginx/conf.d + name: configuration + - mountPath: /var/www/html + name: site + restart_policy: Always + volumes: + - name: site + configMap: + name: site-files + items: + - key: index-php + path: index.php + - name: configuration + configMap: + name: nginx-conf + items: + - key: nginx-conf + path: default.conf + +############################################################################## +## A Kubernetes service serves as an internal load balancer. It identifies a +## set of replicated pods in order to proxy the connections it receives to them. +## https://docs.openshift.org/latest/architecture/core_concepts/pods_and_services.html#services +############################################################################## +- name: create nginx-php-fpm service + k8s_v1_service: + name: nginx-php-fpm + namespace: '{{ namespace }}' + labels: + app: '{{ namespace }}' + service: nginx-php-fpm + selector: + app: '{{ namespace }}' + service: nginx-php-fpm + ports: + - name: nginx + port: 80 + target_port: 8080 + - name: php-fpm + port: 9000 + target_port: 9000 + +############################################################################## +## An OpenShift Origin route exposes a service at a host name, so that external +## clients can reach it by name. Each route consists of a name, a service +## selector, and an optional security configuration. +## https://docs.openshift.org/latest/architecture/core_concepts/routes.html +############################################################################## +- name: create nginx-php-fpm route + openshift_v1_route: + name: nginx-php-fpm + namespace: '{{ namespace }}' + labels: + app: '{{ namespace }}' + service: nginx-php-fpm + to_name: nginx-php-fpm + spec_port_target_port: nginx + +- name: Copy default.conf + copy: + src: default.conf + dest: /tmp/default.conf + +- name: Create NGINX configmap for nginx.conf + shell: oc create configmap nginx-conf --from-file=nginx-conf=/tmp/default.conf -n {{ namespace }} + +- name: Copy index.php + copy: + src: index.php + dest: /tmp/index.php + +- name: Create NGINX configmap for index.php + shell: oc create configmap site-files --from-file=index-php=/tmp/index.php -n {{ namespace }}