Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/klevr-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ func main() {
flag.Parse() // Important for parsing

// Check the null data from CLI
if len(*apikey) == 0 {
if *apikey == "" {
logger.Error("Please insert an API Key")
os.Exit(0)
}
if len(*platform) == 0 {
if *platform == "" {
logger.Error("Please make sure the platform")
os.Exit(0)
}
if len(*zone) == 0 {
if *zone == "" {
logger.Error("Please insert zoneId")
os.Exit(0)
}
if len(*klevrAddr) == 0 {
if *klevrAddr == "" {
logger.Error("Please insert manager addr")
os.Exit(0)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (agent *KlevrAgent) updateScheduler() {
interval = defaultSchedulerInterval
}
if oldSchedulerInterval != interval {
if agent.scheduler.IsRunning() == true {
if agent.scheduler.IsRunning() {
agent.scheduler.Clear()
if agent.checkPrimary(agent.Primary.IP) {
agent.scheduler.Every(int(interval)).Seconds().Do(agent.polling)
Expand Down
4 changes: 1 addition & 3 deletions pkg/agent/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ func (agent *KlevrAgent) handShake() *common.Primary {
agent.schedulerInterval = body.Me.CallCycle

if len(body.Agent.Nodes) > 0 {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for loop not needed as i can be done directly

for _, v := range body.Agent.Nodes {
agent.Agents = append(agent.Agents, v)
}
agent.Agents = append(agent.Agents, body.Agent.Nodes...)
}

return &body.Agent.Primary
Expand Down
5 changes: 2 additions & 3 deletions pkg/agent/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ func (agent *KlevrAgent) getRemoteUpdatedTasks() []common.KlevrTask {

logger.Debugf("tasks: %v", tasks)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for loop not needed as i can be done directly

for _, item := range tasks {
remoteTasks = append(remoteTasks, item)
}
remoteTasks = append(remoteTasks, tasks...)

} else {
logger.Debugf("getRemoteUpdatedTasks error: %v", resErr)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/agent/scheduler_primary.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (agent *KlevrAgent) assignmentTask(primaryAgentKey string, task []common.Kl
}

func (agent *KlevrAgent) polling() {
if agent.taskPollingPause == true {
if agent.taskPollingPause {
logger.Debug("Polling aborted because authentication failed.")
return
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func collectAgentLog() Command {

cutData := make([]byte, baseSize)

copy(cutData[:], data[sIndex:eIndex])
copy(cutData, data[sIndex:eIndex])

data = cutData
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func Decrypt(key string, crypt string) (decrypted string, err error) {
dec := make([]byte, len(b))
ecb.CryptBlocks(dec, b)
decrypt := getPKCS5Trimming(dec)
decrypt = decrypt[8:len(decrypt)]
decrypt = decrypt[8:]

b, err = encoder.DecodeString(string(decrypt))
if err != nil {
Expand Down
63 changes: 29 additions & 34 deletions pkg/common/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,43 +54,38 @@ func NewChannelQueue(chanBufSize uint32) *Queue {

// Queue 생성 시 큐에 삽입되는 데이터를 채널로 받아 처리하기 위한 listener go routine. Close()가 호출되면 종료된다.
go func() {
// alive가 false가 될 때까지 반복 처리
for q.alive {
// select로 buf 채널을 수신
select {
case newItem := <-q.buf:
// buf에 nil이 들어오면 queue가 종료된다. (Close() 를 통해 nil을 전달 받아 종료시킨다.)
if newItem != nil {
nq := &queueItem{
item: newItem,
next: nil,
}

if q.length == 0 {
// 빈 큐에 데이터가 삽입될 시 출력 채널과 데이터가 동기화 되어 go routine이 block 되므로 새로운 go routine에서 채널을 전송한다.
go func() {
q.current <- nq
}()
} else {
q.last.next = nq
}

q.last = nq
q.length++

if q.listener != nil {
q.listenerRunCount++

// 리스너 함수가 설정되고 호출 건수가 만족되면 리스너 함수를 호출한다.
if q.listenerRunCount >= q.listenerCallCount {
// 리스너 함수는 별도의 go routine으로 호출되므로 호출 시점의 누적 건수와 실행 시점의 누적 건수는 차이가 발생할 수 있다.
go q.listener(&Q)
q.listenerRunCount = 0
}
}
newItem := <-q.buf
// buf에 nil이 들어오면 queue가 종료된다. (Close() 를 통해 nil을 전달 받아 종료시킨다.)
if newItem != nil {
nq := &queueItem{
item: newItem,
next: nil,
}

if q.length == 0 {
// 빈 큐에 데이터가 삽입될 시 출력 채널과 데이터가 동기화 되어 go routine이 block 되므로 새로운 go routine에서 채널을 전송한다.
go func() {
q.current <- nq
}()
} else {
q.last.next = nq
}

q.last = nq
q.length++

if q.listener != nil {
q.listenerRunCount++

// 리스너 함수가 설정되고 호출 건수가 만족되면 리스너 함수를 호출한다.
if q.listenerRunCount >= q.listenerCallCount {
// 리스너 함수는 별도의 go routine으로 호출되므로 호출 시점의 누적 건수와 실행 시점의 누적 건수는 차이가 발생할 수 있다.
go q.listener(&Q)
q.listenerRunCount = 0
}
}
}

}()

return &Q
Expand Down
4 changes: 2 additions & 2 deletions pkg/common/task_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@ func (executor *taskExecutor) execute(tw *TaskWrapper) {
}(err)

if RESERVED == tw.recover.CommandType {
result, err = runReservedCommand(tw.Result, tw.KlevrTask, tw.recover)
result, _ = runReservedCommand(tw.Result, tw.KlevrTask, tw.recover)
} else if INLINE == tw.recover.CommandType {
result, err = runInlineCommand(tw.Result, tw.KlevrTask, tw.recover)
result, _ = runInlineCommand(tw.Result, tw.KlevrTask, tw.recover)
}

tw.Result = result
Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func Init(ctx *common.Context) *API {
api.InitAgent(api.BaseRoutes.Agent)
api.InitInstall(api.BaseRoutes.Install)
api.InitInner(api.BaseRoutes.Inner)
if api.Manager.Config.Console.Usage == true {
if api.Manager.Config.Console.Usage {
api.InitConsole(api.BaseRoutes.Console)
}

Expand Down
10 changes: 3 additions & 7 deletions pkg/manager/api_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func (api *agentAPI) receivePolling(w http.ResponseWriter, r *http.Request) {
arrAgent[i].FreeDisk = manager.encrypt(strconv.Itoa(a.FreeDisk))
arrAgent[i].IsActive = boolToByte(a.IsActive)

if a.IsActive == false {
if !a.IsActive {
inactiveAgentKeys = append(inactiveAgentKeys, a.AgentKey)
if tid, ok := CheckShutdownTask(a.AgentKey); ok {
agentKeys = append(agentKeys, a.AgentKey)
Expand Down Expand Up @@ -367,7 +367,7 @@ func (api *agentAPI) receivePolling(w http.ResponseWriter, r *http.Request) {
}

// Credential 조회
nCredentials, cnt := tx.getCredentials(ch.ZoneID)
nCredentials, _ := tx.getCredentials(ch.ZoneID)

// 신규 task 할당
nTasks, cnt := tx.getTasksWithSteps(manager, ch.ZoneID, []string{string(common.WaitPolling), string(common.HandOver)})
Expand Down Expand Up @@ -847,11 +847,7 @@ func upsertAgent(ctx *common.Context, tx *Tx, agent *Agents, ch *common.CustomHe
}

func byteToBool(b byte) bool {
if b == 0 {
return false
}

return true
return b != 0
}

func boolToByte(b bool) byte {
Expand Down
6 changes: 3 additions & 3 deletions pkg/manager/api_console.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (api *ConsoleAPI) ChangePassword(w http.ResponseWriter, r *http.Request) {
}

pm := (*pms)[0]
if pm.Activated == true {
if pm.Activated {
decPassword, err := common.Decrypt(manager.Config.Server.EncryptionKey, pm.UserPassword)
if err != nil || pw != decPassword {
w.WriteHeader(http.StatusUnauthorized)
Expand Down Expand Up @@ -203,7 +203,7 @@ func (api *ConsoleAPI) Activated(w http.ResponseWriter, r *http.Request) {

pm := (*pms)[0]
var activatedStatus string
if pm.Activated == true {
if pm.Activated {
activatedStatus = "activated"
} else {
activatedStatus = "initialized"
Expand Down Expand Up @@ -251,7 +251,7 @@ func (api *ConsoleAPI) UnActivated(w http.ResponseWriter, r *http.Request) {
}

pm := (*pms)[0]
if pm.Activated == true {
if pm.Activated {
manager := ctx.Get(CtxServer).(*KlevrManager)
encPassword, err := common.Encrypt(manager.Config.Server.EncryptionKey, "admin")
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions pkg/manager/api_inner.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ func (api *serversAPI) getTasks(w http.ResponseWriter, r *http.Request) {
logger.Debugf("%d", len(agentKeys))
logger.Debugf("%d", len(taskNames))

if groupIDs == nil || len(groupIDs) == 0 {
if len(groupIDs) == 0 {
common.WriteHTTPError(400, w, nil, "Query parameter groupID is required.")
return
}
Expand Down Expand Up @@ -1030,7 +1030,7 @@ func (api *serversAPI) deleteGroup(w http.ResponseWriter, r *http.Request) {
func (api *serversAPI) deletegroup(ctx *common.Context, tx *Tx, id uint64) error {
tx.deletePrimaryAgent(id)
_, ok := tx.getPrimaryAgent(id)
if ok == true {
if ok {
return fmt.Errorf("It cannot remove the zone(primaryagent) of the zoneid: %d", id)
}

Expand Down Expand Up @@ -1224,13 +1224,13 @@ func TaskMatchingCredential(manager *KlevrManager, task Tasks, credential *[]Cre
return task
}

if len(task.TaskDetail.Parameter) == 0 {
if task.TaskDetail.Parameter == "" {
return task
}

r := regexp.MustCompile("{{2}[a-zA-Z0-9]*}{2}")
isMatch := r.MatchString(task.TaskDetail.Parameter)
if isMatch == false {
if !isMatch {
return task
}

Expand All @@ -1239,7 +1239,7 @@ func TaskMatchingCredential(manager *KlevrManager, task Tasks, credential *[]Cre
v := manager.decrypt(c.Value)

re := regexp.MustCompile(pattern)
task.TaskDetail.Parameter = fmt.Sprintf("%s", re.ReplaceAllString(task.TaskDetail.Parameter, v))
task.TaskDetail.Parameter = re.ReplaceAllString(task.TaskDetail.Parameter, v)
logger.Debugf("Apply Credential : %s", task.TaskDetail.Parameter)
}

Expand Down
10 changes: 7 additions & 3 deletions pkg/manager/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,14 @@ func (c *Cache) GetAgentsForInactive(ctx *common.Context, before time.Time) (int

for _, member := range members {
var buf Agents
json.Unmarshal([]byte(member), &buf)
err = json.Unmarshal([]byte(member), &buf)
if err != nil {
logger.Debug(err)
return 0, nil
}

if byteToBool(buf.IsActive) == true {
if res := buf.LastAccessTime.Before(before); res == true {
if byteToBool(buf.IsActive) {
if res := buf.LastAccessTime.Before(before); res {
inactivedAgents = append(inactivedAgents, buf)
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1790,7 +1790,7 @@ type s struct{}

func (s *s) ReadDoc() string {
sInfo := SwaggerInfo
sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1)
sInfo.Description = strings.ReplaceAll(sInfo.Description, "\n", "\\n")

t, err := template.New("swagger_info").Funcs(template.FuncMap{
"marshal": func(v interface{}) string {
Expand Down
6 changes: 3 additions & 3 deletions pkg/manager/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,11 @@ func (tx *Tx) updateTask(manager *KlevrManager, t *Tasks) {
if detail.Result != "" {
detail.Result = manager.encrypt(detail.Result)

cnt, err = tx.Where("TASK_ID = ?", t.Id).
_, err = tx.Where("TASK_ID = ?", t.Id).
Cols("CURRENT_STEP", "RESULT", "FAILED_STEP", "IS_FAILED_RECOVER").
Update(detail)
} else {
cnt, err = tx.Where("TASK_ID = ?", t.Id).
_, err = tx.Where("TASK_ID = ?", t.Id).
Cols("CURRENT_STEP", "FAILED_STEP", "IS_FAILED_RECOVER").
Update(detail)
}
Expand Down Expand Up @@ -791,7 +791,7 @@ func (tx *Tx) getCredentialByName(zoneID uint64, credentialName string) *Credent
exist := common.CheckGetQuery(tx.Where("CREDENTIALS.ZONE_ID = ?", zoneID).And("CREDENTIALS.KEY = ?", credentialName).Get(&credential))
logger.Debugf("Selected Credentials : exist[%v], id[%d], key[%s]", exist, credential.Id, credential.Key)

if exist == false {
if !exist {
return nil
}

Expand Down
19 changes: 7 additions & 12 deletions pkg/manager/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (manager *KlevrManager) Run() error {
ctx.Put(CtxDbConn, db)
ctx.Put(CtxPrimary, &sync.Mutex{})

if manager.Config.DB.Cache == true {
if manager.Config.DB.Cache {
ctx.Put(CtxCacheLock, &sync.Mutex{})
}

Expand Down Expand Up @@ -634,16 +634,16 @@ func (manager *KlevrManager) updateAgentStatus(ctx *common.Context, cycle int) {
cnt, agents := txManager.GetAgentsForInactive(ctx, tx, before)

if cnt > 0 {
len := len(*agents)
inactiveIDs := make([]uint64, len)
inactiveAgentKeys := make([]string, len)
agentLen := len(*agents)
inactiveIDs := make([]uint64, agentLen)
inactiveAgentKeys := make([]string, agentLen)
forceShutdownAgentKeys := make([]string, 0)
taskIDs := make([]uint64, 0)

var events = make([]KlevrEvent, len)
var events = make([]KlevrEvent, agentLen)
var eventTime = &common.JSONTime{Time: time.Now().UTC()}

for i := 0; i < len; i++ {
for i := 0; i < agentLen; i++ {
agent := (*agents)[i]

inactiveIDs[i] = agent.Id
Expand Down Expand Up @@ -722,12 +722,7 @@ func checkLock(tx *Tx, instanceID string, d time.Duration) bool {
func expired(lockDate time.Time, d time.Duration) bool {
current := time.Now().UTC()
compare := lockDate.Add(d)

if current.After(compare) {
return true
}

return false
return current.After(compare)
}

func (manager *KlevrManager) encrypt(msg string) string {
Expand Down
3 changes: 1 addition & 2 deletions pkg/rabbitmq/rabbitmq.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,8 @@ func DialCluster(urls []string) (*Connection, error) {
if i < count-1 {
err = nil
continue
} else {
return nil, errors.Wrap(err, "all connection failed.")
}
return nil, errors.Wrap(err, "all connection failed.")
}

connection = &Connection{
Expand Down
3 changes: 1 addition & 2 deletions test/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ func TestToTasks(t *testing.T) {
TaskDetail: &manager.TaskDetail{TaskId: 2},
})

var nrts *[]manager.RetriveTask
nrts = &rts
nrts := &rts

var tasks = make([]manager.Tasks, 0)
var tasks2 *[]manager.Tasks
Expand Down