-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first pass at auto deploy cloud foundry
- Loading branch information
Showing
13 changed files
with
316 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package cron | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
func deploy(apikey, org, space, resourceGroup, region string) error { | ||
githubUser := os.Getenv("GITHUB_USER") | ||
githubToken := os.Getenv("GITHUB_TOKEN") | ||
grantClusterURL := os.Getenv("GRANT_CLUSTER_REPO_URL") | ||
|
||
if err := cmd("ibmcloud", | ||
"login", "--apikey", apikey, "-a", "https://cloud.ibm.com", "-r", region); err != nil { | ||
return err | ||
} | ||
if err := cmd("ibmcloud", | ||
"target", "-o", org, "-s", space, "-g", resourceGroup); err != nil { | ||
return err | ||
} | ||
if err := cmd("git", | ||
"clone", fmt.Sprintf("https://%s:%s@%s.git", githubUser, githubToken, grantClusterURL)); err != nil { | ||
return err | ||
} | ||
|
||
defer cleanup("grant-cluster") | ||
|
||
if err := cmd("./grant-cluster/scripts/deploy-app.sh"); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Cleanup folder | ||
func cleanup(filepaths ...string) error { | ||
for _, filepath := range filepaths { | ||
fi, err := os.Stat(filepath) | ||
if err != nil { | ||
return err | ||
} | ||
mode := fi.Mode() | ||
if mode.IsDir() { | ||
if err := os.RemoveAll(filepath); err != nil { | ||
return err | ||
} | ||
} else if mode.IsRegular() { | ||
if err := os.Remove(filepath); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func cmd(name string, args ...string) error { | ||
cmd := exec.Command(name, args...) | ||
|
||
cmd.Stdin = os.Stdin | ||
cmd.Stderr = os.Stderr | ||
|
||
if output, err := cmd.Output(); err != nil { | ||
return err | ||
} else { | ||
fmt.Printf("%s\n", output) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cron | ||
|
||
import ( | ||
"bytes" | ||
"html/template" | ||
"log" | ||
) | ||
|
||
func getEmailBody(data EmailData) (string, error) { | ||
tmpl, err := template.ParseFiles("templates/email.gohtml") | ||
if err != nil { | ||
log.Println("could not parse file", err) | ||
return "", err | ||
} | ||
htmlTemplate := template.Must(tmpl, err) | ||
buf := new(bytes.Buffer) | ||
|
||
if err := htmlTemplate.Execute(buf, data); err != nil { | ||
log.Println("could not parse file", err) | ||
return "", err | ||
} | ||
|
||
return buf.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cron | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/moficodes/ibmcloud-kubernetes-admin/pkg/ibmcloud" | ||
) | ||
|
||
func setEnvs(accountID string, schedule ibmcloud.Schedule) error { | ||
|
||
if err := os.Setenv("EVENT_NAME", schedule.EventName); err != nil { | ||
return err | ||
} | ||
if err := os.Setenv("PASSWORD", schedule.Password); err != nil { | ||
return err | ||
} | ||
if err := os.Setenv("RESOURCE_GROUP_NAME", schedule.ResourceGroupName); err != nil { | ||
return err | ||
} | ||
if err := os.Setenv("ACCESS_GROUP_NAME", schedule.AccessGroupName); err != nil { | ||
return err | ||
} | ||
if err := os.Setenv("APP_HOSTNAME", schedule.EventName); err != nil { | ||
return err | ||
} | ||
if err := os.Setenv("ACCOUNT", accountID); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cron | ||
|
||
import ( | ||
"encoding/base64" | ||
"os" | ||
|
||
"github.com/moficodes/ibmcloud-kubernetes-admin/pkg/notification" | ||
) | ||
|
||
func createComment(issue, comment string) error { | ||
apiEndpoint := os.Getenv("GITHUB_API_ENDPOINT") | ||
repo := os.Getenv("REPO") | ||
owner := os.Getenv("OWNER") | ||
githubUser := os.Getenv("GITHUB_USER") | ||
githubToken := os.Getenv("GITHUB_TOKEN") | ||
token := "Basic " + base64Encode(githubUser+":"+githubToken) | ||
if err := notification.CreateComment(token, apiEndpoint, owner, repo, issue, comment); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func base64Encode(data string) string { | ||
return base64.StdEncoding.EncodeToString([]byte(data)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package notification | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
const githubBaseURL = "https://api.github.ibm.com" | ||
|
||
func CreateComment(token, apiEndpoint, owner, repo, issue, comment string) error { | ||
endpoint := fmt.Sprintf("%s/repos/%s/%s/issues/%s/comments", githubBaseURL, owner, repo, issue) | ||
|
||
header := map[string]string{ | ||
"Authorization": token, | ||
} | ||
|
||
type Comment struct { | ||
Body string `json:"body"` | ||
} | ||
|
||
c := &Comment{ | ||
Body: comment, | ||
} | ||
|
||
jsonValue, err := json.Marshal(c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var res interface{} | ||
|
||
if err := postBody(endpoint, header, nil, jsonValue, res); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// postBody makes a post request with json body | ||
func postBody(endpoint string, header, query map[string]string, jsonValue []byte, res interface{}) error { | ||
request, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(jsonValue)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return handleRequest(request, header, query, res) | ||
} | ||
|
||
func handleRequest(request *http.Request, header map[string]string, query map[string]string, res interface{}) error { | ||
for key, value := range header { | ||
request.Header.Add(key, value) | ||
} | ||
|
||
q := request.URL.Query() | ||
for key, value := range query { | ||
q.Add(key, value) | ||
} | ||
|
||
request.URL.RawQuery = q.Encode() | ||
|
||
client := &http.Client{Timeout: time.Duration(150 * time.Second)} | ||
|
||
resp, err := client.Do(request) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) { | ||
json, err := json.Marshal(resp.Body) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
return errors.New(string(json)) | ||
} | ||
|
||
// b, _ := ioutil.ReadAll(resp.Body) | ||
// log.Println(string(b)) | ||
// This was a delete request and was successful. | ||
// no need to try decode the body. | ||
if resp.StatusCode == 204 { | ||
return nil | ||
} | ||
|
||
if err = json.NewDecoder(resp.Body).Decode(&res); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.