Skip to content

Commit b716015

Browse files
committed
airflow, api: rest: Change time sleep task to API based task
1 parent b115cd7 commit b716015

File tree

9 files changed

+223
-8
lines changed

9 files changed

+223
-8
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "cicada_task_time_sleep",
3+
"description": "Pause with configurable time.",
4+
"api_connection_id": "cicada_api",
5+
"swagger_yaml_endpoint": "/cicada/api/doc.yaml",
6+
"endpoint": "/sleep_time",
7+
"method": "POST"
8+
}

lib/airflow/example/task_component/time_sleep.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

lib/cmd/runCmd.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"os/exec"
6+
)
7+
8+
func runCMD(name string, arg ...string) (output string, err error) {
9+
cmd := exec.Command(name, arg...)
10+
outputBytes, err := cmd.CombinedOutput()
11+
if err != nil {
12+
if len(outputBytes) == 0 {
13+
outputBytes = []byte(err.Error())
14+
}
15+
return string(outputBytes), errors.New(string(outputBytes))
16+
}
17+
18+
return string(outputBytes), nil
19+
}
20+
21+
func RunBash(commandString string) (output string, err error) {
22+
return runCMD("bash", "-c", commandString)
23+
}

pkg/api/rest/controller/script.go renamed to pkg/api/rest/controller/cicadaTaskComponent.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package controller
33
import (
44
"net/http"
55

6+
"github.com/cloud-barista/cm-cicada/lib/cmd"
67
"github.com/cloud-barista/cm-cicada/lib/ssh"
78
"github.com/cloud-barista/cm-cicada/pkg/api/rest/common"
89
"github.com/cloud-barista/cm-cicada/pkg/api/rest/model"
@@ -58,3 +59,39 @@ func RunScript(c echo.Context) error {
5859

5960
return c.JSONPretty(http.StatusOK, result, " ")
6061
}
62+
63+
// SleepTime godoc
64+
//
65+
// @ID sleep-time
66+
// @Summary Run sleep command on cicada
67+
// @Description Runs sleep command on cicada and waits for configured time.
68+
// @Tags [Cicada Task Component]
69+
// @Accept json
70+
// @Produce json
71+
// @Param request body model.SleepTimeReq true "SleepTime request"
72+
// @Success 200 {object} model.ScriptResult "Result of sleep"
73+
// @Failure 400 {object} common.ErrorResponse "Sent bad request."
74+
// @Failure 500 {object} common.ErrorResponse "Failed to run script"
75+
// @Router /sleep_time [post]
76+
func SleepTime(c echo.Context) error {
77+
sleepTimeReq := new(model.SleepTimeReq)
78+
err := c.Bind(sleepTimeReq)
79+
if err != nil {
80+
return err
81+
}
82+
83+
if sleepTimeReq.Time == "" {
84+
return common.ReturnErrorMsg(c, "Please provide the time.")
85+
}
86+
87+
var result model.SimpleMsg
88+
89+
_, err = cmd.RunBash("sleep " + sleepTimeReq.Time)
90+
if err != nil {
91+
result.Message = err.Error()
92+
} else {
93+
result.Message = "success"
94+
}
95+
96+
return c.JSONPretty(http.StatusOK, result, " ")
97+
}

pkg/api/rest/docs/docs.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,53 @@ const docTemplate = `{
132132
}
133133
}
134134
},
135+
"/sleep_time": {
136+
"post": {
137+
"description": "Runs sleep command on cicada and waits for configured time.",
138+
"consumes": [
139+
"application/json"
140+
],
141+
"produces": [
142+
"application/json"
143+
],
144+
"tags": [
145+
"[Cicada Task Component]"
146+
],
147+
"summary": "Run sleep command on cicada",
148+
"operationId": "sleep-time",
149+
"parameters": [
150+
{
151+
"description": "SleepTime request",
152+
"name": "request",
153+
"in": "body",
154+
"required": true,
155+
"schema": {
156+
"$ref": "#/definitions/github_com_cloud-barista_cm-cicada_pkg_api_rest_model.SleepTimeReq"
157+
}
158+
}
159+
],
160+
"responses": {
161+
"200": {
162+
"description": "Result of sleep",
163+
"schema": {
164+
"$ref": "#/definitions/github_com_cloud-barista_cm-cicada_pkg_api_rest_model.ScriptResult"
165+
}
166+
},
167+
"400": {
168+
"description": "Sent bad request.",
169+
"schema": {
170+
"$ref": "#/definitions/github_com_cloud-barista_cm-cicada_pkg_api_rest_common.ErrorResponse"
171+
}
172+
},
173+
"500": {
174+
"description": "Failed to run script",
175+
"schema": {
176+
"$ref": "#/definitions/github_com_cloud-barista_cm-cicada_pkg_api_rest_common.ErrorResponse"
177+
}
178+
}
179+
}
180+
}
181+
},
135182
"/task/{taskId}": {
136183
"get": {
137184
"description": "Get the task directly.",
@@ -2140,6 +2187,15 @@ const docTemplate = `{
21402187
}
21412188
}
21422189
},
2190+
"github_com_cloud-barista_cm-cicada_pkg_api_rest_model.SleepTimeReq": {
2191+
"type": "object",
2192+
"properties": {
2193+
"time": {
2194+
"description": "Ex: 300 = 300sec, 1m 30s = 1min 30seconds, 1h 10m 15s = 1hour 10minutes 15seconds, 1d 1s = 1day 1second, No Input Default: 10s",
2195+
"type": "string"
2196+
}
2197+
}
2198+
},
21432199
"github_com_cloud-barista_cm-cicada_pkg_api_rest_model.Task": {
21442200
"type": "object",
21452201
"required": [

pkg/api/rest/docs/swagger.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,53 @@
125125
}
126126
}
127127
},
128+
"/sleep_time": {
129+
"post": {
130+
"description": "Runs sleep command on cicada and waits for configured time.",
131+
"consumes": [
132+
"application/json"
133+
],
134+
"produces": [
135+
"application/json"
136+
],
137+
"tags": [
138+
"[Cicada Task Component]"
139+
],
140+
"summary": "Run sleep command on cicada",
141+
"operationId": "sleep-time",
142+
"parameters": [
143+
{
144+
"description": "SleepTime request",
145+
"name": "request",
146+
"in": "body",
147+
"required": true,
148+
"schema": {
149+
"$ref": "#/definitions/github_com_cloud-barista_cm-cicada_pkg_api_rest_model.SleepTimeReq"
150+
}
151+
}
152+
],
153+
"responses": {
154+
"200": {
155+
"description": "Result of sleep",
156+
"schema": {
157+
"$ref": "#/definitions/github_com_cloud-barista_cm-cicada_pkg_api_rest_model.ScriptResult"
158+
}
159+
},
160+
"400": {
161+
"description": "Sent bad request.",
162+
"schema": {
163+
"$ref": "#/definitions/github_com_cloud-barista_cm-cicada_pkg_api_rest_common.ErrorResponse"
164+
}
165+
},
166+
"500": {
167+
"description": "Failed to run script",
168+
"schema": {
169+
"$ref": "#/definitions/github_com_cloud-barista_cm-cicada_pkg_api_rest_common.ErrorResponse"
170+
}
171+
}
172+
}
173+
}
174+
},
128175
"/task/{taskId}": {
129176
"get": {
130177
"description": "Get the task directly.",
@@ -2133,6 +2180,15 @@
21332180
}
21342181
}
21352182
},
2183+
"github_com_cloud-barista_cm-cicada_pkg_api_rest_model.SleepTimeReq": {
2184+
"type": "object",
2185+
"properties": {
2186+
"time": {
2187+
"description": "Ex: 300 = 300sec, 1m 30s = 1min 30seconds, 1h 10m 15s = 1hour 10minutes 15seconds, 1d 1s = 1day 1second, No Input Default: 10s",
2188+
"type": "string"
2189+
}
2190+
}
2191+
},
21362192
"github_com_cloud-barista_cm-cicada_pkg_api_rest_model.Task": {
21372193
"type": "object",
21382194
"required": [

pkg/api/rest/docs/swagger.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ definitions:
215215
message:
216216
type: string
217217
type: object
218+
github_com_cloud-barista_cm-cicada_pkg_api_rest_model.SleepTimeReq:
219+
properties:
220+
time:
221+
description: 'Ex: 300 = 300sec, 1m 30s = 1min 30seconds, 1h 10m 15s = 1hour
222+
10minutes 15seconds, 1d 1s = 1day 1second, No Input Default: 10s'
223+
type: string
224+
type: object
218225
github_com_cloud-barista_cm-cicada_pkg_api_rest_model.Task:
219226
properties:
220227
dependencies:
@@ -594,6 +601,37 @@ paths:
594601
summary: Run script on target
595602
tags:
596603
- '[Cicada Task Component]'
604+
/sleep_time:
605+
post:
606+
consumes:
607+
- application/json
608+
description: Runs sleep command on cicada and waits for configured time.
609+
operationId: sleep-time
610+
parameters:
611+
- description: SleepTime request
612+
in: body
613+
name: request
614+
required: true
615+
schema:
616+
$ref: '#/definitions/github_com_cloud-barista_cm-cicada_pkg_api_rest_model.SleepTimeReq'
617+
produces:
618+
- application/json
619+
responses:
620+
"200":
621+
description: Result of sleep
622+
schema:
623+
$ref: '#/definitions/github_com_cloud-barista_cm-cicada_pkg_api_rest_model.ScriptResult'
624+
"400":
625+
description: Sent bad request.
626+
schema:
627+
$ref: '#/definitions/github_com_cloud-barista_cm-cicada_pkg_api_rest_common.ErrorResponse'
628+
"500":
629+
description: Failed to run script
630+
schema:
631+
$ref: '#/definitions/github_com_cloud-barista_cm-cicada_pkg_api_rest_common.ErrorResponse'
632+
summary: Run sleep command on cicada
633+
tags:
634+
- '[Cicada Task Component]'
597635
/task/{taskId}:
598636
get:
599637
consumes:

pkg/api/rest/model/script.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ type ScriptResult struct {
1212
Output string `json:"output"`
1313
Error string `json:"error"`
1414
}
15+
16+
type SleepTimeReq struct {
17+
Time string `json:"time"` // Ex: 300 = 300sec, 1m 30s = 1min 30seconds, 1h 10m 15s = 1hour 10minutes 15seconds, 1d 1s = 1day 1second, No Input Default: 10s
18+
}

pkg/api/rest/route/workflow.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ func Workflow(e *echo.Echo) {
4040
e.GET("/"+strings.ToLower(common.ShortModuleName)+"/workflow/:wfId/status", controller.GetWorkflowStatus)
4141

4242
e.POST("/"+strings.ToLower(common.ShortModuleName)+"/run_script", controller.RunScript)
43+
e.POST("/"+strings.ToLower(common.ShortModuleName)+"/sleep_time", controller.SleepTime)
4344
}

0 commit comments

Comments
 (0)