Skip to content

Commit

Permalink
Merge pull request #126 from francescotimperi/content-action
Browse files Browse the repository at this point in the history
Implements system action for nuvolaris/nuvolaris#316
  • Loading branch information
giusdp committed Oct 14, 2023
2 parents fcaa41d + 26a9e10 commit 307d222
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 68 deletions.
52 changes: 28 additions & 24 deletions actions/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,26 @@ tasks:
- >
{{.WSKSYS}} action get nuv/login --url
upload:prepare:
content:prepare:
- |-
mkdir -p upload/common
cp ./common/minio_util.py upload/common
mkdir -p content/common
cp ./common/minio_util.py content/common
- |-
cd upload
rm -f ../{{.DEPLOY}}/upload.zip
zip -r ../{{.DEPLOY}}/upload.zip *
cd content
rm -f ../{{.DEPLOY}}/content.zip
zip -r ../{{.DEPLOY}}/content.zip *
upload:deploy:
- task: upload:prepare
content:deploy:
- task: content:prepare
- >
{{.WSKSYS}} package update nuv
-p minio_host {{.MINIO_HOST}}
-p minio_port {{.MINIO_PORT}}
- >
{{.WSKSYS}} action update nuv/upload
{{.DEPLOY}}/upload.zip --kind python:3 --web true
{{.WSKSYS}} action update nuv/content
{{.DEPLOY}}/content.zip --kind python:3 --web true
- >
{{.WSKSYS}} action get nuv/upload --url
{{.WSKSYS}} action get nuv/content --url
devel:redis:prepare:
- |-
Expand Down Expand Up @@ -259,21 +259,25 @@ tasks:
- >
curl -X POST https://gptuser.api.k3s.nuvtest.net/api/v1/web/whisk-system/nuv/login -H "Content-Type: application/json" -d '{"login": "{{.USERNAME}}", "password": "{{.PASSWORD}}"}'
upload:webtest:
content:webtest:
- >
curl -X PUT -T ../nuvolaris/templates/index.html -H "minioauth: {{.NUV_MINIO_AUTH}}" {{.APIHOST}}/api/v1/web/whisk-system/nuv/upload/nuvolaris/index2.html
curl -X PUT -T ../nuvolaris/templates/index.html -H "minioauth: {{.NUV_MINIO_AUTH}}" {{.APIHOST}}/api/v1/web/whisk-system/nuv/content/nuvolaris/index2.html
upload:webtest2:
content:webtest2:
- >
curl -X PUT -T ../nuvolaris/templates/content.html -H "minioauth: {{.NUV_MINIO_AUTH}}" {{.APIHOST}}/api/v1/web/whisk-system/nuv/upload/nuvolaris/tidy/content.html
curl -X PUT -T ../nuvolaris/templates/content.html -H "minioauth: {{.NUV_MINIO_AUTH}}" {{.APIHOST}}/api/v1/web/whisk-system/nuv/content/nuvolaris/tidy/content.html
upload:yaml:
content:yaml:
- >
curl -X PUT -T ../nuvolaris/templates/couchdb-init.yaml -H "minioauth: {{.NUV_MINIO_AUTH}}" {{.APIHOST}}/api/v1/web/whisk-system/nuv/upload/nuvolaris/yaml/couchdb-init.yaml
curl -X PUT -T ../nuvolaris/templates/couchdb-init.yaml -H "minioauth: {{.NUV_MINIO_AUTH}}" {{.APIHOST}}/api/v1/web/whisk-system/nuv/content/nuvolaris/yaml/couchdb-init.yaml
upload:chess:
content-delete:yaml:
- >
curl -X PUT -T ../nuvolaris/templates/content.html -H "minioauth: {{.NUV_MINIO_AUTH}}" {{.APIHOST}}/api/v1/web/whisk-system/nuv/upload/nuvolaris/chess/index.html
curl -X DELETE -H "minioauth: {{.NUV_MINIO_AUTH}}" {{.APIHOST}}/api/v1/web/whisk-system/nuv/content/nuvolaris/yaml/couchdb-init.yaml
content:chess:
- >
curl -X PUT -T ../nuvolaris/templates/content.html -H "minioauth: {{.NUV_MINIO_AUTH}}" {{.APIHOST}}/api/v1/web/whisk-system/nuv/content/nuvolaris/chess/index.html
devel:redis:set:
- >
Expand Down Expand Up @@ -342,18 +346,18 @@ tasks:
- task: login:deploy
- task: login:webtest

upload:all:
- task: upload:prepare
- task: upload:deploy
content:all:
- task: content:prepare
- task: content:deploy

# prepares all the system related actions
prepare:
- task: login:prepare
- task: upload:prepare
- task: content:prepare

deploy:
- task: login:deploy
- task: upload:deploy
- task: content:deploy

all:
- task: prepare
Expand Down
66 changes: 44 additions & 22 deletions actions/upload/__main__.py → actions/content/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ def build_error(message: str):
"body": message
}

def build_response(user, filename,bucket, upload_result,upload_message):
def build_response(user, filename, bucket, upload_result, operation):
body = {
"user":user,
"filename":filename,
"bucket":bucket,
"uploaded": upload_result
"operation": operation
}

if upload_message:
body['message']=upload_message

return {
"statusCode": upload_result and 200 or 400,
"body": body
Expand All @@ -49,40 +46,65 @@ def process_path_param(ow_path: str):

return upload_data

def _put(mo_client, upload_data, content_as_b64):
"""
Store the given payload into a folder
:param mo_client
:param upload_data
:param content_as_b64 file to be stored in a base64 endoded format
"""
print(f"processing request to upload file {upload_data['filename']} under {upload_data['user']} web-bucket")
tmp_file = mutil.prepare_file_upload(upload_data['user'],upload_data['filename'],content_as_b64)

if tmp_file:
upload_result, upload_message = mutil.upload_file(mo_client,tmp_file,f"{upload_data['user']}-web",upload_data['filename'])
return build_response(upload_data['user'],upload_data['filename'],f"{upload_data['user']}-web",upload_result,"PUT")
else:
return build_error("Unexptected error content action. Check activation log")

def _delete(mo_client, upload_data):
print(f"processing request to remove file {upload_data['filename']} under {upload_data['user']} web-bucket")
removed = mutil.rm_file(mo_client,f"{upload_data['user']}-web", upload_data['filename'])

if removed:
return build_response(upload_data['user'],upload_data['filename'],f"{upload_data['user']}-web",removed,"DELETE")
else:
return build_error("Unexptected error content action. Check activation log")

def main(args):
"""
Simple actions to upload a files into a minio bucket.
The upload action it is supposed to receive a path param similar
/<user>/<path>?<auth>
and will store the given path under the <user>-web bucket using mnio
Simple actions to upload/remove a files into a minio bucket.
The action it is supposed to receive a path param similar to /<user>/<path>?<auth>
and will store/remove the given path under the <user>-web bucket using minio client
"""
print(args)

method = args['__ow_method']
headers = args['__ow_headers']

if(method.lower() not in ['put','delete'] ):
return build_error(f"invalid request, HTTP verb {method} is not supported")

if('minioauth' not in headers):
return build_error("invalid request, missing mandatory header: minioauth")

if(len(args['__ow_body']) == 0):
return build_error("invalid request, no file content has been received")

upload_data = process_path_param(args['__ow_path'])

if 'user' not in upload_data and 'filename' not in upload_data:
return build_error("invalid request, username and/or filename path error")

minio_host = args['minio_host']
minio_port = args['minio_port']
content_as_b64 = args['__ow_body']
minio_port = args['minio_port']
auth = headers['minioauth']

print(f"processing request to upload file {upload_data['filename']} under {upload_data['user']} web-bucket")

mo_client = mutil.build_mo_client(minio_host, minio_port,upload_data['user'], auth)
tmp_file = mutil.prepare_file_upload(upload_data['user'],upload_data['filename'],content_as_b64)

if tmp_file:
upload_result, upload_message = mutil.upload_file(mo_client,tmp_file,f"{upload_data['user']}-web",upload_data['filename'])
return build_response(upload_data['user'],upload_data['filename'],f"{upload_data['user']}-web",upload_result, upload_message)
else:
return build_error("Unexptected error upload action. Check activation log")
if method.lower() in 'put':
if(len(args['__ow_body']) == 0):
return build_error("invalid request, no file content has been received")

content_as_b64 = args['__ow_body']
return _put(mo_client, upload_data, content_as_b64)

if method.lower() in 'delete':
return _delete(mo_client, upload_data)
File renamed without changes.
14 changes: 7 additions & 7 deletions actions/system.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ tasks:
rm -f ../{{.DEPLOY}}/login.zip
zip -r ../{{.DEPLOY}}/login.zip *
upload:prepare:
content:prepare:
- |-
mkdir -p upload/common
cp ./common/minio_util.py upload/common
mkdir -p content/common
cp ./common/minio_util.py content/common
- |-
cd upload
rm -f ../{{.DEPLOY}}/upload.zip
zip -r ../{{.DEPLOY}}/upload.zip *
cd content
rm -f ../{{.DEPLOY}}/content.zip
zip -r ../{{.DEPLOY}}/content.zip *
redis:prepare:
- |-
Expand Down Expand Up @@ -116,7 +116,7 @@ tasks:
# prepares all the system related actions
prepare:
- task: login:prepare
- task: upload:prepare
- task: content:prepare
- task: redis:prepare
- task: psql:prepare
- task: minio:prepare
Expand Down
20 changes: 10 additions & 10 deletions nuvolaris/whisk_actions_deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,24 @@ def prepare_login_action():

return login

def prepare_upload_action():
def prepare_content_action():
minio_host= cfg.get("minio.host") or "minio"
minio_port= cfg.get("minio.port") or "9000"
minio_full_host = f"{minio_host}.nuvolaris.svc.cluster.local"

upload_inputs=[]
upload_inputs.append({"key":"minio_host", "value":minio_full_host})
upload_inputs.append({"key":"minio_port", "value":minio_port})
content_inputs=[]
content_inputs.append({"key":"minio_host", "value":minio_full_host})
content_inputs.append({"key":"minio_port", "value":minio_port})

upload = {
"name":"upload",
"function":"upload.zip",
content = {
"name":"content",
"function":"content.zip",
"runtime":"python:3",
"web":"true",
"inputs":upload_inputs
"inputs":content_inputs
}

return upload
return content

def prepare_redis_action():

Expand Down Expand Up @@ -145,7 +145,7 @@ def prepare_system_actions():
"""
actions = []
actions.append(prepare_login_action())
actions.append(prepare_upload_action())
actions.append(prepare_content_action())
actions.append(prepare_redis_action())
actions.append(prepare_psql_action())
actions.append(prepare_minio_action())
Expand Down
10 changes: 5 additions & 5 deletions whisk-system.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ cd ${HOME}/actions/login
rm -f ${HOME}/deploy/whisk-system/login.zip
zip -r ${HOME}/deploy/whisk-system/login.zip *

cd ${HOME}/actions/upload
mkdir -p ${HOME}/actions/upload/common
cp ${HOME}/actions/common/minio_util.py ${HOME}/actions/upload/common
rm -f ${HOME}/deploy/whisk-system/upload.zip
zip -r ${HOME}/deploy/whisk-system/upload.zip *
cd ${HOME}/actions/content
mkdir -p ${HOME}/actions/content/common
cp ${HOME}/actions/common/minio_util.py ${HOME}/actions/content/common
rm -f ${HOME}/deploy/whisk-system/content.zip
zip -r ${HOME}/deploy/whisk-system/content.zip *

mkdir -p ${HOME}/actions/devel/redis/nuvolaris
mkdir -p ${HOME}/actions/devel/redis/common
Expand Down

0 comments on commit 307d222

Please sign in to comment.