233 lines
6.1 KiB
Go
233 lines
6.1 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"sort"
|
|
"time"
|
|
|
|
"gitlab.com/infektcommon/settings"
|
|
)
|
|
|
|
const (
|
|
jsonContentType = "application/json"
|
|
)
|
|
|
|
var (
|
|
basicAuthUsername string
|
|
basicAuthPassword string
|
|
deploymentStateFile string
|
|
)
|
|
|
|
func init() {
|
|
basicAuthUsername = settings.GetSetting("BASIC_AUTH_USERNAME", "default")
|
|
basicAuthPassword = settings.GetSetting("BASIC_AUTH_PASSWORD", "default")
|
|
deploymentStateFile = settings.GetSetting("DEPLOYMENT_STATE_FILE", "./guidelines.json")
|
|
}
|
|
|
|
type DeploymentAgentServer struct {
|
|
http.Handler
|
|
}
|
|
|
|
type DeploymentSpec []struct {
|
|
App string `json:"app"`
|
|
Version string `json:"version"`
|
|
}
|
|
|
|
type QueuedDeploymentSpec struct {
|
|
Id int64
|
|
Status string // one of: queued (input queue), deploying, failed, deployed (process queue)
|
|
StatusMessage string // status or error message
|
|
DeploymentSpec DeploymentSpec
|
|
DeploymentState map[string]string
|
|
}
|
|
|
|
type DeploymentQueue struct {
|
|
items []QueuedDeploymentSpec
|
|
}
|
|
|
|
func (q *DeploymentQueue) Enqueue(id int64, status, statusMessage string, deploymentSpec DeploymentSpec) {
|
|
q.items = append(q.items, QueuedDeploymentSpec{
|
|
Id: id,
|
|
Status: status,
|
|
StatusMessage: statusMessage,
|
|
DeploymentSpec: deploymentSpec,
|
|
})
|
|
|
|
}
|
|
|
|
func (q *DeploymentQueue) Dequeue() QueuedDeploymentSpec {
|
|
item := q.items[0]
|
|
q.items = q.items[1:len(q.items)]
|
|
return item
|
|
}
|
|
|
|
func (q *DeploymentQueue) GetAll() []QueuedDeploymentSpec {
|
|
return q.items
|
|
}
|
|
|
|
func (q *DeploymentQueue) updateStatus(id int64, status, statusMessage string, deploymentState map[string]string) {
|
|
for i := range q.items {
|
|
if q.items[i].Id == id {
|
|
p := &q.items[i]
|
|
p.Status = status
|
|
p.StatusMessage = statusMessage
|
|
p.DeploymentState = deploymentState
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
func (q *DeploymentQueue) String() string {
|
|
return fmt.Sprint(q.items)
|
|
}
|
|
|
|
func newDeploymentAgentServer() *DeploymentAgentServer {
|
|
server := new(DeploymentAgentServer)
|
|
router := http.NewServeMux()
|
|
|
|
router.Handle("/deploy", http.HandlerFunc(server.deploymentHandler))
|
|
router.Handle("/status", http.HandlerFunc(server.statusHandler))
|
|
|
|
server.Handler = router
|
|
|
|
return server
|
|
}
|
|
|
|
func isAuthenticated(w http.ResponseWriter, r http.Request) (http.ResponseWriter, bool) {
|
|
user, pass, ok := r.BasicAuth()
|
|
if !ok {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
w.Header().Add("WWW-Authenticate", `Basic realm=""`)
|
|
w.Write([]byte(`{"Error": "No credentials present"}`))
|
|
return w, false
|
|
}
|
|
|
|
if (user != basicAuthUsername && user != "default") || (pass != basicAuthPassword && pass != "default") {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
w.Header().Add("WWW-Authenticate", `Basic realm=""`)
|
|
w.Write([]byte(`{"Error": "Invalid credentials"}`))
|
|
return w, false
|
|
}
|
|
|
|
return w, true
|
|
}
|
|
|
|
func (s *DeploymentAgentServer) deploymentHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", jsonContentType)
|
|
if authenticatedResponse, ok := isAuthenticated(w, *r); ok {
|
|
w = authenticatedResponse
|
|
} else {
|
|
return
|
|
}
|
|
|
|
switch r.Method {
|
|
case http.MethodPost:
|
|
queueDeploymentSpec(w, r)
|
|
default:
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
w.Write([]byte(`{"Error": "Method not allowed"}`))
|
|
}
|
|
}
|
|
|
|
func (s *DeploymentAgentServer) statusHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", jsonContentType)
|
|
if authenticatedResponse, ok := isAuthenticated(w, *r); ok {
|
|
w = authenticatedResponse
|
|
} else {
|
|
return
|
|
}
|
|
|
|
switch r.Method {
|
|
case http.MethodGet:
|
|
var status []QueuedDeploymentSpec
|
|
status = append(status, inputQueue.GetAll()...)
|
|
status = append(status, processQueue.GetAll()...)
|
|
sort.Slice(status, func(i, j int) bool {
|
|
return status[i].Id < status[j].Id
|
|
})
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
|
json.NewEncoder(w).Encode(status)
|
|
default:
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
w.Write([]byte(`{"Error": "Method not allowed"}`))
|
|
}
|
|
}
|
|
|
|
func queueDeploymentSpec(w http.ResponseWriter, r *http.Request) {
|
|
var error struct{ Error string }
|
|
var deploymentSpec DeploymentSpec
|
|
|
|
body, _ := ioutil.ReadAll(r.Body)
|
|
err := json.Unmarshal(body, &deploymentSpec)
|
|
if err != nil {
|
|
error.Error = err.Error()
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
json.NewEncoder(w).Encode(&error)
|
|
} else {
|
|
message := "Queued for deployment"
|
|
deploymentId := time.Now().UnixNano() / 1000
|
|
inputQueue.Enqueue(deploymentId, "queued", message, deploymentSpec)
|
|
w.WriteHeader(http.StatusAccepted)
|
|
w.Write([]byte(fmt.Sprintf(`{"Message": %q, "DeploymentId": "%d"}`, message, deploymentId)))
|
|
}
|
|
}
|
|
|
|
func processDeploymentSpecs(pc *podmanClient, hc *http.Client, url string) {
|
|
fmt.Println("Server ready to schedule deployments")
|
|
for {
|
|
time.Sleep(5 * time.Second)
|
|
if len(inputQueue.GetAll()) > 0 {
|
|
dequequedDeploymentSpec := inputQueue.Dequeue()
|
|
processQueue.Enqueue(dequequedDeploymentSpec.Id, "deploying", "Deployment in progress", dequequedDeploymentSpec.DeploymentSpec)
|
|
err := pc.pullImages(dequequedDeploymentSpec.DeploymentSpec, hc, url)
|
|
if err != nil {
|
|
processQueue.updateStatus(dequequedDeploymentSpec.Id, "failed", err.Error(), nil)
|
|
} else {
|
|
err := mergeDeploymentState(dequequedDeploymentSpec.DeploymentSpec)
|
|
if err != nil {
|
|
processQueue.updateStatus(dequequedDeploymentSpec.Id, "failed", err.Error(), getDeploymentState())
|
|
} else {
|
|
processQueue.updateStatus(dequequedDeploymentSpec.Id, "deployed", "Deployment successful", getDeploymentState())
|
|
}
|
|
}
|
|
fmt.Println(processQueue.GetAll())
|
|
}
|
|
}
|
|
}
|
|
|
|
func getDeploymentState() map[string]string {
|
|
state := make(map[string]string)
|
|
|
|
file, _ := ioutil.ReadFile(deploymentStateFile)
|
|
err := json.Unmarshal([]byte(file), &state)
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
}
|
|
return state
|
|
}
|
|
|
|
func mergeDeploymentState(deploymentSpec DeploymentSpec) error {
|
|
var deployedApps = []string{}
|
|
deploymentState := getDeploymentState()
|
|
|
|
for k := range deploymentState {
|
|
deployedApps = append(deployedApps, k)
|
|
}
|
|
|
|
for _, app := range deploymentSpec {
|
|
deploymentState[app.App] = app.Version
|
|
}
|
|
|
|
file, _ := json.MarshalIndent(deploymentState, "", " ")
|
|
err := ioutil.WriteFile(deploymentStateFile, file, 0644)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|