Skip to content

Commit

Permalink
Feature/script documentation (open-eats#28)
Browse files Browse the repository at this point in the history
* cleaning up the quick start script

* updating docs for new quick-start script version

* updating the release script to pull commits from github since the last release

* adding new release

* removing the example and updating the script
  • Loading branch information
RyanNoelk authored Mar 30, 2018
1 parent 568c5de commit 60d4e79
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 40 deletions.
37 changes: 37 additions & 0 deletions commits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python
# encoding: utf-8

import requests
from datetime import datetime, timedelta


def get_commits(repo):
"""
Give a repo, this function will return a list of commits since the last release.
:param repo: a github repo.
:return: array of commit messages.
"""

# Get the last commit from the last release
res = requests.get('https://api.github.com/repos/open-eats/%s/tags' % repo)
commit = res.json()[0].get('commit').get('sha')

# Get the date of the last commit from the last release
res = requests.get('https://api.github.com/repos/open-eats/%s/commits/%s' % (repo, commit))
date = res.json().get('commit').get('author').get('date')

# Convert the date to a datetime and add 1 second to it,
# So we don't get the last commit of the previous release.
date = datetime.strptime(date, "%Y-%m-%dT%H:%M:%SZ") + timedelta(seconds=1)

# Get all the commits messages since the last release
res = requests.get('https://api.github.com/repos/open-eats/%s/commits?since=%s' % (repo, date))
return [re.get('commit').get('message') for re in res.json()]


if __name__ == '__main__':
messages = []
repos = ['openeats-nginx', 'openeats-api', 'openeats-web', 'OpenEats']
for r in repos:
messages += get_commits(r)
print('\n'.join([m.split('\r', 1)[0] for m in messages]))
6 changes: 5 additions & 1 deletion docs/Running_the_App.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ You will also need to edit your `docker-prod.yml` file to remove the database fr
Once the files have been created run the command below and replace the version with version of OpenEats you want to run. You can also leave this blank (this will pull the latest code)

```bash
./quick-start.py 1.0.3
./quick-start.py -t 1.0.3
```
OR
```bash
./quick-start.py
```
OR
```bash
./quick-start.py --help
```

The quick start script will do a few things.
1. Creates a `docker-prod.version.yml` file with the required image tags.
Expand Down
6 changes: 5 additions & 1 deletion docs/Updating_the_App.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ There should only be changes to these files in major releases (IE. 2.0.0, 3.0.0)
Once you know your env and docker compose files are up to date, Run:

```bash
./quick-start.py 1.0.3
./quick-start.py -t 1.0.3
```
OR
```bash
./quick-start.py
```
OR
```bash
./quick-start.py --help
```

The quick start script will do a few things.
1. Creates a `docker-prod.version.yml` file with the required image tags.
Expand Down
48 changes: 40 additions & 8 deletions quick-start.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@


def update_image_tags(version=None):
"""
A simple function to configure the OpenEats version.
:param version: The version of OpenEats the users wants to run.
This is a git Tag.
:return: A file called `docker-prod.version.yml`.
With the version of each image to pull.
"""
version = version if version is not None else 'latest'
version = '''version: '2.3'
services:
Expand All @@ -22,6 +30,7 @@ def update_image_tags(version=None):


def download_images(version=None):
""" Download the required images """
version = version if version is not None else 'latest'
print("==================")
print("Downloading Images")
Expand All @@ -32,10 +41,18 @@ def download_images(version=None):


def start_containers():
"""
Takes a back up of the Recipe images and DB.
Restarts OpenEats with a new (or the same) version.
"""
print("==================")
print("Starting OpenEats")
print("==================")

# Check if the DB is up and running locally.
# If it is then take a backup.
# If the user is using a remote DB, do nothing.
# If no DB is found, Start the docker DB and wait 45s to start.
p = Popen(
['docker', 'ps', '-q', '-f', 'name=openeats_db_1'],
stdin=PIPE,
Expand All @@ -59,6 +76,9 @@ def start_containers():
call(['docker-compose', '-f', 'docker-prod.yml', 'up', '-d', 'db'])
sleep(45)

# Check if the API is up.
# If it is then take a backup of the Recipe images folder.
# The backup folder is called `site-media`.
p = Popen(
['docker', 'ps', '-q', '-f', 'name=openeats_api_1'],
stdin=PIPE,
Expand All @@ -73,6 +93,8 @@ def start_containers():
shell=True
)

# Stop each container that needs to be updated.
# Don't stop the DB! There is no reason to.
call([
'docker-compose',
'-f', 'docker-prod.yml',
Expand All @@ -94,6 +116,8 @@ def start_containers():
'-f', 'docker-prod.override.yml',
'stop', 'web'
])

# Start all the containers
call([
'docker-compose',
'-f', 'docker-prod.yml',
Expand All @@ -106,13 +130,21 @@ def start_containers():


if __name__ == '__main__':
from sys import argv
print("OpenEats quick setup script")
try:
update_image_tags(argv[1])
download_images(argv[1])
except IndexError:
update_image_tags(None)
download_images(None)
import argparse
parser = argparse.ArgumentParser(
description='OpenEats quick setup script. '
'This script will restart your OpenEats server and '
'take a database and recipe image backup.'
)
parser.add_argument(
'-t',
'--tag',
type=str,
help='The git tag of OpenEats you want to run. '
'If not included, then the master branch will be used.'
)
args = parser.parse_args()

update_image_tags(args.tag)
download_images(args.tag)
start_containers()
101 changes: 71 additions & 30 deletions release.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#!/usr/bin/env python
# encoding: utf-8

import json
import requests
from requests.auth import HTTPBasicAuth
from secrets.secrets import username, password


def release(_repo, _tag, _name, _target, _body, _draft, _prerelease):
"""
Send a post request to github to log a release.
Then print out the response status and message.
"""
response = requests.post(
'https://api.github.com/repos/open-eats/%s/releases' % _repo,
json={
Expand All @@ -19,39 +24,75 @@ def release(_repo, _tag, _name, _target, _body, _draft, _prerelease):
},
auth=HTTPBasicAuth(username, password)
)
print(response)
print('Status: %s' % response)
print('Response: %s' % response.text)


# Define some help test that the json file should follow.
help_text = '''
A JSON file with release info in it. See below for an example.
{
"tag": "1.1.1",
"name": "Release Test",
"body": "the is a test release!",
"target": "master",
"draft": false,
"prerelease": false
}
'''


if __name__ == '__main__':
from sys import argv
print("Starting Release")
import argparse
parser = argparse.ArgumentParser(
description='OpenEats release script.'
)
# Require a json file as part of the script
parser.add_argument(
'release',
type=str,
help=help_text
)

try:
# required
tag = argv[1]
name = argv[2]
# Open the json file and try and parse it into a json file
with open(parser.parse_args().release, 'r') as fp:
args = json.loads(fp.read())

# optional
try:
body = argv[3]
except IndexError:
body = argv[2]
try:
target = argv[4]
except IndexError:
target = 'master'
try:
draft = argv[5].lower() == 'true'
except IndexError:
draft = False
# All json files should have this format.
json_format = [
'tag',
'name',
'target',
'body',
'draft',
'prerelease',
]

# Iterate through the `json_format` array and
# make sure the json file supplied has all the required fields.
for key in json_format:
try:
prerelease = argv[6].lower() == 'true'
except IndexError:
prerelease = False

release('openeats-api', tag, name, target, body, draft, prerelease)
release('openeats-web', tag, name, target, body, draft, prerelease)
release('openeats-nginx', tag, name, target, body, draft, prerelease)
release('OpenEats', tag, name, target, body, draft, prerelease)
except IndexError:
exit('Please add a tag name and release name')
tmp = args[key]
except (IndexError, KeyError):
print("$s missing! Please add it." % key)
exit(1)

# The list of repos we want to push a release to.
repos = [
'openeats-nginx',
'openeats-api',
'openeats-web',
'OpenEats'
]

# Run a release for all the repos with the data from json file.
for r in repos:
release(
r,
args.get('tag'),
args.get('name'),
args.get('target'),
args.get('body'),
args.get('draft'),
args.get('prerelease')
)
8 changes: 8 additions & 0 deletions releases/1.2.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"tag": "1.2.0",
"name": "Build process improvement",
"body": "Adding JWT support\nAdding support for using slug values in the recipe\nFixing issue with tests always passing\nAdding docs and settings for remote DB config\nNew ui build process\nAdding coverage reports\nUse nginx to serve static\nNginx config to also pass port info to django",
"target": "master",
"draft": false,
"prerelease": false
}

0 comments on commit 60d4e79

Please sign in to comment.