|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "time" |
| 10 | + |
| 11 | + mq "github.com/cheshir/go-mq/v2" |
| 12 | + "github.com/uselagoon/machinery/api/lagoon" |
| 13 | + lclient "github.com/uselagoon/machinery/api/lagoon/client" |
| 14 | + "github.com/uselagoon/machinery/api/schema" |
| 15 | + "github.com/uselagoon/machinery/utils/jwt" |
| 16 | +) |
| 17 | + |
| 18 | +type Idled struct { |
| 19 | + Idled bool `json:"idled"` |
| 20 | +} |
| 21 | + |
| 22 | +func (m *Messenger) handleIdling(ctx context.Context, messageQueue *mq.MessageQueue, message *schema.LagoonMessage, messageID string) error { |
| 23 | + prefix := fmt.Sprintf("(messageid:%s) %s: ", messageID, message.Namespace) |
| 24 | + log.Println(fmt.Sprintf("%sreceived idling environment status update", prefix)) |
| 25 | + // generate a lagoon token with a expiry of 60 seconds from now |
| 26 | + token, err := jwt.GenerateAdminToken(m.LagoonAPI.TokenSigningKey, m.LagoonAPI.JWTAudience, m.LagoonAPI.JWTSubject, m.LagoonAPI.JWTIssuer, time.Now().Unix(), 60) |
| 27 | + if err != nil { |
| 28 | + // the token wasn't generated |
| 29 | + if m.EnableDebug { |
| 30 | + log.Println(fmt.Sprintf("ERROR: unable to generate token: %v", err)) |
| 31 | + } |
| 32 | + return nil |
| 33 | + } |
| 34 | + // set up a lagoon client for use in the following process |
| 35 | + l := lclient.New(m.LagoonAPI.Endpoint, "actions-handler", m.LagoonAPI.Version, &token, false) |
| 36 | + var environmentID uint |
| 37 | + // determine the environment id from the message |
| 38 | + if message.Meta.ProjectID == nil && message.Meta.EnvironmentID == nil { |
| 39 | + project, err := lagoon.GetMinimalProjectByName(ctx, message.Meta.Project, l) |
| 40 | + if err != nil { |
| 41 | + // send the log to the lagoon-logs exchange to be processed |
| 42 | + m.toLagoonLogs(messageQueue, map[string]interface{}{ |
| 43 | + "severity": "error", |
| 44 | + "event": fmt.Sprintf("actions-handler:%s:failed", "updateEnvironment"), |
| 45 | + "meta": project, |
| 46 | + "message": err.Error(), |
| 47 | + }) |
| 48 | + if m.EnableDebug { |
| 49 | + log.Println(fmt.Sprintf("%sERROR: unable to get project - %v", prefix, err)) |
| 50 | + } |
| 51 | + return err |
| 52 | + } |
| 53 | + environment, err := lagoon.GetEnvironmentByName(ctx, message.Meta.Environment, project.ID, l) |
| 54 | + if err != nil { |
| 55 | + // send the log to the lagoon-logs exchange to be processed |
| 56 | + m.toLagoonLogs(messageQueue, map[string]interface{}{ |
| 57 | + "severity": "error", |
| 58 | + "event": fmt.Sprintf("actions-handler:%s:failed", "updateEnvironment"), |
| 59 | + "meta": project, |
| 60 | + "message": err.Error(), |
| 61 | + }) |
| 62 | + if m.EnableDebug { |
| 63 | + log.Println(fmt.Sprintf("%sERROR: unable to get environment - %v", prefix, err)) |
| 64 | + } |
| 65 | + return err |
| 66 | + } |
| 67 | + environmentID = environment.ID |
| 68 | + } else { |
| 69 | + // pull the id from the message |
| 70 | + environmentID = *message.Meta.EnvironmentID |
| 71 | + } |
| 72 | + decodeData, _ := base64.StdEncoding.DecodeString(message.Meta.AdvancedData) |
| 73 | + idled := &Idled{} |
| 74 | + json.Unmarshal(decodeData, idled) |
| 75 | + updateEnvironmentPatch := schema.UpdateEnvironmentPatchInput{ |
| 76 | + Idled: &idled.Idled, |
| 77 | + } |
| 78 | + updateEnvironment, err := lagoon.UpdateEnvironment(ctx, environmentID, updateEnvironmentPatch, l) |
| 79 | + if err != nil { |
| 80 | + // send the log to the lagoon-logs exchange to be processed |
| 81 | + m.toLagoonLogs(messageQueue, map[string]interface{}{ |
| 82 | + "severity": "error", |
| 83 | + "event": fmt.Sprintf("actions-handler:%s:failed", "updateDeployment"), |
| 84 | + "meta": updateEnvironment, |
| 85 | + "message": err.Error(), |
| 86 | + }) |
| 87 | + if m.EnableDebug { |
| 88 | + log.Println(fmt.Sprintf("%sERROR: unable to update environment - %v", prefix, err)) |
| 89 | + } |
| 90 | + return err |
| 91 | + } |
| 92 | + log.Println(fmt.Sprintf("%supdated environment", prefix)) |
| 93 | + return nil |
| 94 | +} |
0 commit comments