Skip to content

Commit

Permalink
agent 설치 및 실행을 위한 스크립트 api
Browse files Browse the repository at this point in the history
  • Loading branch information
JaehoonKim committed Jul 13, 2021
1 parent 514f2ae commit 51e5106
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 8 deletions.
44 changes: 36 additions & 8 deletions pkg/manager/api_install.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package manager

import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"

"github.com/Klevry/klevr/pkg/common"
"github.com/NexClipper/logger"
"github.com/gorilla/mux"
)

// InitInstall initialize install API
// InitInstall initialize install API

func (api *API) InitInstall(install *mux.Router) {
logger.Debug("API InitInstall - init URI")

Expand All @@ -19,19 +22,44 @@ func (api *API) InitInstall(install *mux.Router) {
registURI(install, GET, "/agents/download", api.downloadAgent)
}

// agent setup script 생성
type BootstrapCommand struct {
APIKey string `json:"apiKey"`
Platform string `json:"platform"`
ManagerURL string `json:"managerUrl"`
ZoneID uint64 `json:"zoneId"`
}

//
// generateBootstrapCommand godoc
// @Summary agent setup script 생성
// @Description 에이전트가 설치되기 위한 최소 정보를 이용해 설치를 할 수 있는 스크립트를 생성한다
// @Tags Install
// @Accept json
// @Produce plain
// @Router /install/agents/bootstrap [post]
// @Param b body manager.BootstrapCommand true "Bootstrap Info"
// @Success 200 {string} string
func (api *API) generateBootstrapCommand(w http.ResponseWriter, r *http.Request) {
var cmd = "curl -sL gg.gg/klevr |bash && ./klevr -apiKey=\"{apiKey}\" -platform={platform} -manager=\"{managerUrl}\" -zoneId={zoneId}"

var cr = &common.Request{Request: r}
var bc BootstrapCommand
err := json.NewDecoder(r.Body).Decode(&bc)
if err != nil {
common.WriteHTTPError(500, w, err, "JSON parsing error")
return
}

zone := strconv.FormatUint(bc.ZoneID, 10)

cmd = strings.Replace(cmd, "{apiKey}", cr.Param("apiKey"), 1)
cmd = strings.Replace(cmd, "{platform}", cr.Param("platform"), 1)
cmd = strings.Replace(cmd, "{managerUrl}", cr.Param("managerUrl"), 1)
cmd = strings.Replace(cmd, "{zoneId}", cr.Param("zoneId"), 1)
cmd = strings.Replace(cmd, "{apiKey}", bc.APIKey, 1)
cmd = strings.Replace(cmd, "{platform}", bc.Platform, 1)
cmd = strings.Replace(cmd, "{managerUrl}", bc.ManagerURL, 1)
cmd = strings.Replace(cmd, "{zoneId}", zone, 1)

w.WriteHeader(http.StatusUnauthorized)
//w.WriteHeader(http.StatusUnauthorized)
// w.Write([]byte(fmt.Sprintf("%s", cmd)))
w.WriteHeader(200)
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "%s", cmd)
}

Expand Down
51 changes: 51 additions & 0 deletions pkg/manager/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,40 @@ var doc = `{
}
}
}
},
"/install/agents/bootstrap": {
"post": {
"description": "에이전트가 설치되기 위한 최소 정보를 이용해 설치를 할 수 있는 스크립트를 생성한다",
"consumes": [
"application/json"
],
"produces": [
"text/plain"
],
"tags": [
"Install"
],
"summary": "agent setup script 생성",
"parameters": [
{
"description": "Bootstrap Info",
"name": "b",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/manager.BootstrapCommand"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
}
},
"definitions": {
Expand Down Expand Up @@ -1549,6 +1583,23 @@ var doc = `{
}
}
},
"manager.BootstrapCommand": {
"type": "object",
"properties": {
"apiKey": {
"type": "string"
},
"managerUrl": {
"type": "string"
},
"platform": {
"type": "string"
},
"zoneId": {
"type": "integer"
}
}
},
"manager.KlevrVariable": {
"type": "object",
"properties": {
Expand Down
51 changes: 51 additions & 0 deletions pkg/manager/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,40 @@
}
}
}
},
"/install/agents/bootstrap": {
"post": {
"description": "에이전트가 설치되기 위한 최소 정보를 이용해 설치를 할 수 있는 스크립트를 생성한다",
"consumes": [
"application/json"
],
"produces": [
"text/plain"
],
"tags": [
"Install"
],
"summary": "agent setup script 생성",
"parameters": [
{
"description": "Bootstrap Info",
"name": "b",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/manager.BootstrapCommand"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
}
},
"definitions": {
Expand Down Expand Up @@ -1530,6 +1564,23 @@
}
}
},
"manager.BootstrapCommand": {
"type": "object",
"properties": {
"apiKey": {
"type": "string"
},
"managerUrl": {
"type": "string"
},
"platform": {
"type": "string"
},
"zoneId": {
"type": "integer"
}
}
},
"manager.KlevrVariable": {
"type": "object",
"properties": {
Expand Down
33 changes: 33 additions & 0 deletions pkg/manager/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ definitions:
userId:
type: integer
type: object
manager.BootstrapCommand:
properties:
apiKey:
type: string
managerUrl:
type: string
platform:
type: string
zoneId:
type: integer
type: object
manager.KlevrVariable:
properties:
description:
Expand Down Expand Up @@ -1052,4 +1063,26 @@ paths:
summary: Klevr에서 제공하는 시스템 변수 목록을 조회한다.
tags:
- servers
/install/agents/bootstrap:
post:
consumes:
- application/json
description: 에이전트가 설치되기 위한 최소 정보를 이용해 설치를 할 수 있는 스크립트를 생성한다
parameters:
- description: Bootstrap Info
in: body
name: b
required: true
schema:
$ref: '#/definitions/manager.BootstrapCommand'
produces:
- text/plain
responses:
"200":
description: OK
schema:
type: string
summary: agent setup script 생성
tags:
- Install
swagger: "2.0"

0 comments on commit 51e5106

Please sign in to comment.