Compare commits

..

2 Commits

Author SHA1 Message Date
Lulzette
76282e9e28 Update .gitlab-ci.yml file 2021-10-19 01:23:32 +00:00
Lulzette
0b7d20dcaa Update .gitlab-ci.yml file 2021-10-19 01:22:44 +00:00
6 changed files with 54 additions and 93 deletions

View File

@ -1,25 +0,0 @@
kind: pipeline
type: exec
name: build
steps:
- name: build
commands:
- docker-compose -p pycms build
- docker-compose -p pycms push
---
name: test
type: docker
kind: pipeline
steps:
- name: run db
image: mongo
detach: true
- name: run_tests
image: pycms_server
commands:
- bash ./test.sh
depends_on:
- build

39
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,39 @@
# This file is a template, and might need editing before it works on your project.
# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Getting-Started.gitlab-ci.yml
# This is a sample GitLab CI/CD configuration file that should run without any modifications.
# It demonstrates a basic 3 stage CI/CD pipeline. Instead of real tests or scripts,
# it uses echo commands to simulate the pipeline execution.
#
# A pipeline is composed of independent jobs that run scripts, grouped into stages.
# Stages run in sequential order, but jobs within stages run in parallel.
#
# For more information, see: https://docs.gitlab.com/ee/ci/yaml/index.html#stages
stages: # List of stages for jobs, and their order of execution
- build
- test
- deploy
build-job: # This job runs in the build stage, which runs first.
stage: build
script:
- echo "Compiling the code..."
- docker build .
- echo "Compile complete."
test-job: # This job also runs in the test stage.
stage: test # It can run at the same time as unit-test-job (in parallel).
script:
- echo "Linting code... This will take about 10 seconds."
- docker-compose up --build --abort-on-container-exit
- echo "No lint issues found."
deploy-job: # This job runs in the deploy stage.
stage: deploy # It only runs when *both* jobs in the test stage complete successfully.
script:
- echo "Deploying application..."
- echo "Application successfully deployed."

View File

@ -2,7 +2,4 @@
host = db
port = 27017
name = pycms
[App]
host = 0.0.0.0
port = 8080
debug = True

View File

@ -4,15 +4,9 @@ services:
build: .
ports:
- 8080:8080
depends_on:
- db
volumes:
- ./config.ini:/app/config.ini
db:
image: mongo
tests:
build: .
command: bash ./test.sh
depends_on:
- db
- server
db:
image: mongo

55
main.py
View File

@ -16,10 +16,6 @@ class Config:
- host
- port
- dbname
App:
- host
- port
- debug
"""
def __init__(self):
"""
@ -32,8 +28,9 @@ class Config:
print('DB not found, creating')
database = mongoclient[self.dbname]
# TODO: Create table if not exists
if 'posts' not in database.list_collection_names():
print('Table not found, creating')
print('Table not fount, creating')
posts = database['posts']
self.posts = posts
@ -47,15 +44,11 @@ class Config:
self.createConfig(config)
db = config['DB']
self.host = db['host']
self.port = int(db['port'])
self.dbname = db['name']
app = config['App']
self.apphost = app['host']
self.appport = int(app['port'])
self.appdebug = bool(app['debug'])
def createConfig(self, config):
"""
Create config file
@ -66,12 +59,6 @@ class Config:
db['port'] = '27017'
db['name'] = 'pycms'
config['App'] = {}
app = config['App']
app['port'] = '8080'
app['debug'] = 'True'
app['host'] = '0.0.0.0'
with open('config.ini', 'w') as cfgfile:
config.write(cfgfile)
@ -101,38 +88,20 @@ class Back():
return str(dict_posts)
def updatePost(self, name, body):
# TODO: return RESTful error/success result
# If post exists, update it
if posts.find_one({'name': name}):
newPostJson = {'$set': {'text': body}}
newPost = posts.update_one({'name': name}, newPostJson)
result = dict(
status=200, state='updated', count=newPost.matched_count
)
newPost = {'$set': {'text': body}}
return str(posts.update_one({'name': name}, newPost))
# Else - create new
else:
newPostJson = {'name': name, 'text': body,
'create_timestamp': str(time.time())}
newPost = posts.insert_one(newPostJson).inserted_id
result = dict(status=200, state='new')
return str(result)
newPost = {'name': name, 'text': body,
'create_timestamp': str(time.time())}
return str(posts.insert_one(newPost).inserted_id)
def deletePost(self, name):
delete = posts.delete_one({'name': name}).deleted_count
if not delete:
result = dict(status=500, count=delete)
else:
result = dict(status=200, count=delete)
return str(result)
class Metrics:
def alive():
return str("alive 1")
@route('/metrics')
def metrics():
return Metrics.alive()
# TODO: return RESTful error/success result
return bool(posts.delete_one({'name': name}).deleted_count)
@route('/post/<name>')
@ -179,4 +148,4 @@ if __name__ == '__main__':
print("Configured")
back = Back()
posts = cfg.posts
run(host=cfg.apphost, port=cfg.appport, reloader=cfg.appdebug, debug=cfg.appdebug)
run(host='0.0.0.0', port=8080, reloader=True, debug=True)

13
test.sh
View File

@ -1,13 +0,0 @@
#!/bin/bash
curl_cmd='curl -s -w :%{http_code}'
#curl_cmd='curl -s -w "%{http_code}\t%{stdout}" -o /dev/null'
url='server:8080'
$curl_cmd -X POST $url/admin/post/test -F 'body=testpage' && echo ": Create success" || echo ': create fail'
$curl_cmd $url/post/test && echo ": get Success" || echo ': get fail'
$curl_cmd -X POST $url/admin/post/test -F 'body=testpage2' && echo ": Update success" || echo ': update fail'
$curl_cmd -X DELETE $url/admin/post/test && echo ": delete success" || echo ': delete fail'
$curl_cmd $url/metrics && echo ": metrics available" || echo ': metrics unavailable'