Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from keptn/418/feature/go-utils
Browse files Browse the repository at this point in the history
#418 First helper utils
  • Loading branch information
agrimmer authored Jun 4, 2019
2 parents ea93217 + e4c346d commit 6f2f5ad
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 1 deletion.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright 2019 Dynatrace LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
20 changes: 20 additions & 0 deletions pkg/utils/cmdUtils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package utils

import (
"fmt"
"os"
"os/exec"
"strings"
)

// ExecuteCommand exectues the command using the args
func ExecuteCommand(command string, args []string) error {
cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("Error executing command %s %s: %s", command, strings.Join(args, " "), err.Error())
}
return nil
}
98 changes: 98 additions & 0 deletions pkg/utils/gitHubUtils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package utils

import (
"fmt"
"os"

"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
)

// CheckoutConfiguration clons a GitHub repo and checks out the specified branch
func CheckoutConfiguration(gitHubOrg string, project string, branch string) (*git.Repository, error) {

err := os.RemoveAll(project)
if err != nil {
return nil, err
}

var repo *git.Repository
if os.Getenv("GITHUB_USERNAME") != "" && os.Getenv("GITHUB_TOKEN") != "" {
// If credentials are available, use them
repo, err = git.PlainClone(project, false, &git.CloneOptions{
URL: "https://github.com/" + gitHubOrg + "/" + project + ".git",
Auth: &http.BasicAuth{
Username: os.Getenv("GITHUB_USERNAME"), // anything except an empty string
Password: os.Getenv("GITHUB_TOKEN"),
},
})
} else {
repo, err = git.PlainClone(project, false, &git.CloneOptions{
URL: "https://github.com/" + gitHubOrg + "/" + project + ".git",
})
}
w, err := repo.Worktree()
if err != nil {
return nil, err
}

err = repo.Fetch(&git.FetchOptions{
RefSpecs: []config.RefSpec{"refs/*:refs/*", "HEAD:refs/heads/HEAD"},
})
if err != nil {
return nil, err
}

// Checking out branch
return repo, w.Checkout(&git.CheckoutOptions{
Branch: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", branch)),
Force: true,
})
}

// CheckoutPrevCommit moves the HEAD pointer to the previous commit. It returns the original HEAD.
func CheckoutPrevCommit(repo *git.Repository) (*plumbing.Reference, error) {

ref, err := repo.Head()
if err != nil {
return nil, err
}

commitIter, err := repo.Log(&git.LogOptions{From: ref.Hash()})
if err != nil {
return nil, err
}

_, err = commitIter.Next()
if err != nil {
return nil, err
}
c, err := commitIter.Next()
if err != nil {
return nil, err
}

w, err := repo.Worktree()
if err != nil {
return nil, err
}

return ref, w.Checkout(&git.CheckoutOptions{
Hash: c.Hash,
})
}

// CheckoutReference moves the HEAD pointer to the specified reference.
func CheckoutReference(repo *git.Repository, ref *plumbing.Reference) error {

w, err := repo.Worktree()
if err != nil {
return err
}

return w.Checkout(&git.CheckoutOptions{
Hash: ref.Hash(),
})
}
24 changes: 24 additions & 0 deletions pkg/utils/kubeUtils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package utils

import (
"fmt"
)

func DoHelmUpgrade(project string, stage string) error {
helmChart := fmt.Sprintf("%s/helm-chart", project)
projectStage := fmt.Sprintf("%s-%s", project, stage)
err := ExecuteCommand("helm", []string{"init", "--client-only"})
if err != nil {
return err
}
err = ExecuteCommand("helm", []string{"dep", "update", helmChart})
if err != nil {
return err
}
return ExecuteCommand("helm", []string{"upgrade", "--install", projectStage, helmChart, "--namespace", projectStage})
}

func CheckDeploymentRolloutStatus(serviceName string, namespace string) error {

return ExecuteCommand("kubectl", []string{"rollout", "status", "deployment/" + serviceName, "--namespace", namespace})
}
41 changes: 41 additions & 0 deletions pkg/utils/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package utils

import (
"encoding/json"
"fmt"
)

type keptnLogMessage struct {
KeptnContext string `json:"keptnContext"`
Message string `json:"message"`
KeptnService string `json:"keptnService"`
LogLevel string `json:"logLevel"`
}

const serviceName = "deploy-service"

// Info logs an info message
func Info(keptnContext string, message string) {
printLogMessage(keptnLogMessage{KeptnContext: keptnContext, Message: message, KeptnService: serviceName, LogLevel: "INFO"})
}

// Error logs an error message
func Error(keptnContext string, message string) {
printLogMessage(keptnLogMessage{KeptnContext: keptnContext, Message: message, KeptnService: serviceName, LogLevel: "ERROR"})
}

// Debug logs a debug message
func Debug(keptnContext string, message string) {
printLogMessage(keptnLogMessage{KeptnContext: keptnContext, Message: message, KeptnService: serviceName, LogLevel: "DEBUG"})
}

func printLogMessage(logMessage keptnLogMessage) {
logString, err := json.Marshal(logMessage)

if err != nil {
fmt.Println("Could not log keptn log message")
return
}

fmt.Println(string(logString))
}

0 comments on commit 6f2f5ad

Please sign in to comment.