Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernized Hello-World APB #3

Merged
merged 18 commits into from Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions .travis.yml
@@ -0,0 +1,26 @@
---
services: docker
sudo: required
language: python
python:
- '2.7'

env:
- OPENSHIFT_VERSION=latest
- OPENSHIFT_VERSION=v3.9
- KUBERNETES_VERSION=latest
- KUBERNETES_VERSION=v1.9.0

script:
# Configure test values
- export apb_name='hello-world-apb'

# Download test shim.
- wget -O ${PWD}/apb-test.sh https://raw.githubusercontent.com/ansibleplaybookbundle/apb-test-shim/master/apb-test.sh
- chmod +x ${PWD}/apb-test.sh

# Run tests.
- ${PWD}/apb-test.sh

# notifications:
# webhooks: https://galaxy.ansible.com/api/v1/notifications/
30 changes: 17 additions & 13 deletions Dockerfile
@@ -1,20 +1,24 @@
FROM ansibleplaybookbundle/apb-base
FROM ansibleplaybookbundle/apb-base:canary

LABEL "com.redhat.apb.spec"=\
"dmVyc2lvbjogMS4wCm5hbWU6IGhlbGxvLXdvcmxkLWFwYgpkZXNjcmlwdGlvbjogZGVwbG95cyBo\
ZWxsby13b3JsZCB3ZWIgYXBwbGljYXRpb24KYmluZGFibGU6IEZhbHNlCmFzeW5jOiBvcHRpb25h\
bAptZXRhZGF0YToKICBkaXNwbGF5TmFtZTogSGVsbG8gV29ybGQgKEFQQikKICBsb25nRGVzY3Jp\
cHRpb246IEEgc2FtcGxlIEFQQiB3aGljaCBkZXBsb3lzIGEgY29udGFpbmVyaXplZCBIZWxsbyBX\
b3JsZCB3ZWIgYXBwbGljYXRpb24KICBkZXBlbmRlbmNpZXM6IFsnZG9ja2VyLmlvL2Fuc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29ybGQKICAgIGZy\
ZWU6ICJUcnVlIgogICAgbWV0YWRhdGE6CiAgICAgIGRpc3BsYXlOYW1lOiBEZWZhdWx0CiAgICAg\
IGxvbmdEZXNjcmlwdGlvbjoKICAgICAgICBUaGlzIHBsYW4gZGVwbG95cyBhIFB5dGhvbiB3ZWIg\
YXBwbGljYXRpb24gZGlzcGxheWluZyBIZWxsbyBXb3JsZAogICAgICBjb3N0OiAkMC4wMAogICAg\
cGFyYW1ldGVyczogW10K"

ADD playbooks /opt/apb/actions
ADD roles /opt/ansible/roles

# Add our role into the ansible roles dir
ADD . /opt/ansible/roles/hello-world-apb

RUN chmod -R g=u /opt/{ansible,apb}

USER apb
26 changes: 18 additions & 8 deletions README.md
@@ -1,20 +1,30 @@
hello-world-apb
======================

An apb for deploying a simple [hello world](https://hub.docker.com/r/ansibleplaybookbundle/hello-world/) app that can bind to Postgres for testing purposes.
[![Build Status](https://travis-ci.org/ansibleplaybookbundle/hello-world-apb.svg?branch=master)](https://travis-ci.org/ansibleplaybookbundle/hello-world-apb)

## What it does
* Deploys a bindable web app.
An apb for deploying a simple [hello world](https://hub.docker.com/r/ansibleplaybookbundle/hello-world/) app that can be bound to PostgreSQL for testing purposes.

## Requirements
## What it does
* Deploys a web app that can be bound to a database.

## Parameters
* NAMESPACE: Optional, default 'hello-world', Namespace to deploy the cluster in.
* OPENSHIFT_TARGET: Required, target openshift deployment
* OPENSHIFT_TOKEN: Required, openshift token to authenticate as

## Running the application
`docker run -e "OPENSHIFT_TARGET=<openshift_target>" -e "OPENSHIFT_TOKEN=<token>" ansibleplaybookbundle/hello-world-apb provision`
```
docker run --rm --net=host \
-v $HOME/.kube:/opt/apb/.kube:z \
-u $UID \
docker.io/ansibleplaybookbundle/hello-world-apb \
provision --extra-vars 'namespace=hello-world'
```

## Tearing down the application
`docker run -e "OPENSHIFT_TARGET=<openshift_target>" -e "OPENSHIFT_TOKEN=<token>" ansibleplaybookbundle/hello-world-apb deprovision`
```
docker run --rm --net=host \
-v $HOME/.kube:/opt/apb/.kube:z \
-u $UID \
docker.io/ansibleplaybookbundle/hello-world-apb \
deprovision --extra-vars 'namespace=hello-world'
```
11 changes: 7 additions & 4 deletions apb.yml
@@ -1,19 +1,22 @@
---
version: 1.0
name: hello-world-apb
description: deploys hello-world web application
bindable: False
bindable: "False"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason these need to be strings and not bools?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://travis-ci.org/djzager/Hello-World-APB/jobs/339544060

apb.yml
  1:1       warning  missing document start "---"  (document-start)
  4:11      warning  truthy value is not quoted  (truthy)
  8:81      error    line too long (89 > 80 characters)  (line-length)
  14:11     warning  truthy value is not quoted  (truthy)
  17:81     error    line too long (88 > 80 characters)  (line-length)

async: optional
metadata:
displayName: Hello World (APB)
longDescription: A sample APB which deploys a containerized Hello World web application
longDescription:
A sample APB which deploys a containerized Hello World web application
dependencies: ['docker.io/ansibleplaybookbundle/hello-world:latest']
providerDisplayName: "Red Hat, Inc."
plans:
- name: default
description: A sample APB which deploys Hello World
free: True
free: "True"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

metadata:
displayName: Default
longDescription: This plan deploys a Python web application displaying Hello World
longDescription:
This plan deploys a Python web application displaying Hello World
cost: $0.00
parameters: []
18 changes: 18 additions & 0 deletions defaults/main.yml
@@ -0,0 +1,18 @@
---
apb_name: hello-world

# Defaults for values we could expect from a broker
_apb_plan_id: default
_apb_service_class_id: 0
_apb_service_instance_id: 0

state_map:
provision: present
deprovision: absent
state: "{{ state_map[apb_action] }}"

cluster: kubernetes

name: "{{ apb_name }}-{{ _apb_service_instance_id }}"
namespace: "{{ lookup('env','NAMESPACE') | default('hello-world', true) }}"
image: docker.io/ansibleplaybookbundle/hello-world:latest
57 changes: 57 additions & 0 deletions meta/main.yml
@@ -0,0 +1,57 @@
galaxy_info:
author: Automation Broker Team
description: Deploys hello-world web application in OpenShift/Kubernetes
company: Red Hat

# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker

# Some suggested licenses:
# - BSD (default)
# - MIT
# - GPLv2
# - GPLv3
# - Apache
# - CC-BY
license: license (GPLv2, CC-BY, etc)

min_ansible_version: 1.5

# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:

# Optionally specify the branch Galaxy will use when accessing the GitHub
# repo for this role. During role install, if no tags are available,
# Galaxy will use this branch. During import Galaxy will access files on
# this branch. If Travis integration is configured, only notifications for this
# branch will be accepted. Otherwise, in all cases, the repo's default branch
# (usually master) will be used.
#github_branch:

#
# platforms is a list of platforms, and each platform has a name and a list of versions.
#
# platforms:
# - name: Fedora
# versions:
# - all
# - 25
# - name: SomePlatform
# versions:
# - all
# - 1.0
# - 7
# - 99.99

galaxy_tags: []
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.

dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.
8 changes: 4 additions & 4 deletions playbooks/deprovision.yml
Expand Up @@ -3,7 +3,7 @@
gather_facts: false
connection: local
roles:
- role: ansible.kubernetes-modules
install_python_requirements: no
- role: deprovision-hello-world-apb
playbook_debug: false
- role: ansibleplaybookbundle.asb-modules
- role: hello-world-apb
vars:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spacing here and in provision.yml looks weird to me, does this work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does work. Not sure what I would do to improve it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, seeing it in the email cleared it up. Thought the var was attached to the role include and not the play.

apb_action: deprovision
8 changes: 4 additions & 4 deletions playbooks/provision.yml
Expand Up @@ -3,7 +3,7 @@
gather_facts: false
connection: local
roles:
- role: ansible.kubernetes-modules
install_python_requirements: no
- role: provision-hello-world-apb
playbook_debug: false
- role: ansibleplaybookbundle.asb-modules
- role: hello-world-apb
vars:
apb_action: provision
18 changes: 18 additions & 0 deletions playbooks/test.yml
@@ -0,0 +1,18 @@
- name: hello-world-apb provision
hosts: localhost
gather_facts: false
connection: local
roles:
- role: ansibleplaybookbundle.asb-modules
tasks:
- name: Provision test
include_role:
name: hello-world-apb
vars:
apb_action: provision

- name: Deprovision test
include_role:
name: hello-world-apb
vars:
apb_action: deprovision
2 changes: 0 additions & 2 deletions roles/deprovision-hello-world-apb/defaults/main.yml

This file was deleted.

36 changes: 0 additions & 36 deletions roles/deprovision-hello-world-apb/tasks/main.yml

This file was deleted.

2 changes: 0 additions & 2 deletions roles/provision-hello-world-apb/defaults/main.yml

This file was deleted.

77 changes: 32 additions & 45 deletions .../provision-hello-world-apb/tasks/main.yml → tasks/main.yml
Expand Up @@ -6,69 +6,56 @@
# to OpenShift.
#############################################################################

- name: "Update last operation"
asb_last_operation:
description: "0%: Starting"

#############################################################################
# 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: hello-world
namespace: '{{ namespace }}'
labels:
app: hello-world
service: hello-world
replicas: 1
selector:
app: hello-world
service: hello-world
spec_template_metadata_labels:
app: hello-world
service: hello-world
containers:
- env:
image: docker.io/ansibleplaybookbundle/hello-world:latest
name: hello-world
ports:
- container_port: 8080
protocol: TCP
- name: "deployment config state={{ state }}"
openshift_raw:
state: '{{ state }}'
definition: "{{ lookup('template', 'deployment_config.yml') | from_yaml }}"
when: cluster == "openshift"

- name: "deployment state={{ state }}"
k8s_raw:
state: '{{ state }}'
definition: "{{ lookup('template', 'deployment.yml') | from_yaml }}"
when: cluster == "kubernetes"

#############################################################################
# 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 hello-world service
k8s_v1_service:
name: hello-world
namespace: '{{ namespace }}'
labels:
app: hello-world
service: hello-world
selector:
app: hello-world
service: hello-world
ports:
- name: web
port: 8080
target_port: 8080

- name: "service state={{ state }}"
k8s_raw:
state: '{{ state }}'
definition: "{{ lookup('template', 'service.yml') | from_yaml }}"

#############################################################################
# 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 hello-world route
openshift_v1_route:
name: hello-world
namespace: '{{ namespace }}'
spec_port_target_port: web
labels:
app: hello-world
service: hello-world
to_name: hello-world
- name: "route state={{ state }}"
openshift_raw:
state: '{{ state }}'
definition: "{{ lookup('template', 'route.yml') | from_yaml }}"
when: cluster == "openshift"

- name: "Update last operation"
asb_last_operation:
description: "50%: Cluster objects state={{ state }}"

- include_tasks: "verify_{{ apb_action }}.yml"

- name: "Update last operation"
asb_last_operation:
description: "100%: Done"