-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* logs fix * Restyled by gofmt (#424) Co-authored-by: Restyled.io <[email protected]> * DEBUG level * basicFormatter * Restyled by gofmt (#431) Co-authored-by: Restyled.io <[email protected]> * improvements Co-authored-by: restyled-io[bot] <32688539+restyled-io[bot]@users.noreply.github.com> Co-authored-by: Restyled.io <[email protected]> Co-authored-by: Helio Machado <[email protected]>
- Loading branch information
1 parent
7718196
commit 9fc440f
Showing
7 changed files
with
390 additions
and
103 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
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,105 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/sirupsen/logrus" | ||
"strings" | ||
"time" | ||
) | ||
|
||
var baseTimestamp = time.Now() | ||
var colors = make(map[string]int) | ||
|
||
type basicFormatter struct{} | ||
|
||
func (f *basicFormatter) Format(entry *logrus.Entry) ([]byte, error) { | ||
levelText := strings.ToUpper(entry.Level.String()) | ||
levelColor := colors[levelText] | ||
tpl := "[%s] 🚀\x1b[%dmTPI\x1b[0m %s\n" | ||
return []byte(fmt.Sprintf(tpl, levelText, levelColor, entry.Message)), nil | ||
} | ||
|
||
func init() { | ||
colors["DEBUG"] = 34 | ||
colors["INFO"] = 36 | ||
colors["WARN"] = 33 | ||
colors["ERROR"] = 31 | ||
colors["FATAL"] = 31 | ||
colors["foreground"] = 35 | ||
|
||
logrus.SetLevel(logrus.DebugLevel) | ||
logrus.SetFormatter(&basicFormatter{}) | ||
} | ||
|
||
type tpiFormatter struct{} | ||
|
||
func (f *tpiFormatter) Format(entry *logrus.Entry) ([]byte, error) { | ||
data := make(logrus.Fields) | ||
for k, v := range entry.Data { | ||
data[k] = v | ||
} | ||
|
||
d := data["d"].(*schema.ResourceData) | ||
message := entry.Message | ||
levelText := strings.ToUpper(entry.Level.String()) | ||
levelColor := colors[levelText] | ||
|
||
if message == "instance" { | ||
cloud := d.Get("cloud").(string) | ||
machine := d.Get("machine").(string) | ||
region := d.Get("region").(string) | ||
spot := d.Get("spot").(float64) | ||
|
||
spottext := "" | ||
if spot > 0 { | ||
spottext = fmt.Sprintf("(Spot %f/h)", spot) | ||
} | ||
message = fmt.Sprintf("%s %s%s in %s", cloud, machine, spottext, region) | ||
} | ||
|
||
if message == "status" { | ||
status := d.Get("status").(map[string]interface{}) | ||
terminatedStr := "terminated 🔵" | ||
|
||
running := "queued 🟣" | ||
if status["running"] != nil { | ||
running = terminatedStr | ||
if status["running"].(int) == 1 { | ||
running = "running 🟡" | ||
} | ||
} | ||
|
||
success := "" | ||
if running == terminatedStr { | ||
success = "without any output" | ||
if status["succeeded"] != nil && status["succeeded"].(int) == 1 { | ||
success = "succesfully 🟢" | ||
} | ||
if status["failed"] != nil && status["failed"].(int) == 1 { | ||
success = "with errors 🔴" | ||
} | ||
} | ||
|
||
message = fmt.Sprintf("Task %s %s", running, success) | ||
} | ||
|
||
if message == "logs" { | ||
logs := d.Get("logs").([]interface{}) | ||
taskLogs := "No logs" | ||
if len(logs) > 0 { | ||
taskLogs = strings.Replace(logs[0].(string), "\n", fmt.Sprintf("\n[%s] ", levelText), -1) | ||
} | ||
|
||
message = fmt.Sprintf("Task logs:\x1b[%dm%s\x1b[0m", colors["foreground"], taskLogs) | ||
} | ||
|
||
tpl := "[%s] \x1b[%dm🚀TPI %s\x1b[0m %s'\n" | ||
return []byte(fmt.Sprintf(tpl, levelText, levelColor, d.Id(), message)), nil | ||
} | ||
|
||
func TpiLogger(d *schema.ResourceData) *logrus.Entry { | ||
logrus.SetFormatter(&tpiFormatter{}) | ||
|
||
return logrus.WithFields(logrus.Fields{"d": d}) | ||
} |
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,172 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func TestState(t *testing.T) { | ||
d := generateSchemaData(t, map[string]interface{}{ | ||
"name": "mytask", | ||
"status": map[string]interface{}{ | ||
"running": 0, | ||
"failed": 0, | ||
}, | ||
}) | ||
|
||
logger := TpiLogger(d) | ||
logger.Info("status") | ||
} | ||
|
||
func TestState2(t *testing.T) { | ||
d := generateSchemaData(t, map[string]interface{}{ | ||
"name": "mytask", | ||
"status": map[string]interface{}{ | ||
"running": 0, | ||
"failed": 1, | ||
}, | ||
}) | ||
|
||
logger := TpiLogger(d) | ||
logger.Info("status") | ||
} | ||
|
||
func TestState3(t *testing.T) { | ||
d := generateSchemaData(t, map[string]interface{}{ | ||
"name": "mytask", | ||
"status": map[string]interface{}{ | ||
"running": 0, | ||
"succeeded": 1, | ||
}, | ||
}) | ||
|
||
logger := TpiLogger(d) | ||
logger.Info("status") | ||
} | ||
|
||
func TestLogs(t *testing.T) { | ||
logs := make([]interface{}, 0) | ||
logs = append(logs, "-- Logs begin at Tue 2022-03-01 12:25:09 UTC, end at Tue 2022-03-01 12:30:30 UTC. --\nMar 01 12:25:50 tpi000000 systemd[1]: Started tpi-task.service.\nMar 01 12:25:50 tpi000000 sudo[1706]: root : TTY=unknown ; PWD=/tmp/tpi-task ; USER=root ; COMMAND=/usr/bin/apt update\nMar 01 12:25:50 tpi000000 sudo[1706]: pam_unix(sudo:session): session opened for user root by (uid=0)\nMar 01 12:25:50 tpi000000 tpi-task[1711]: WARNING: apt does not have a stable CLI interface. Use with caution in scripts.\nMar 01 12:25:50 tpi000000 tpi-task[1711]: Hit:1 http://azure.archive.ubuntu.com/ubuntu focal InRelease\nMar 01 12:25:50 tpi000000 tpi-task[1711]: Get:2 http://azure.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]\nMar 01 12:25:50 tpi000000 tpi-task[1711]: Get:3 http://azure.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]\nMar 01 12:25:51 tpi000000 tpi-task[1711]: Get:4 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]\nMar 01 12:25:51 tpi000000 tpi-task[1711]: Get:5 http://azure.archive.ubuntu.com/ubuntu focal/universe amd64 Packages [8628 kB]\n") | ||
|
||
d := generateSchemaData(t, map[string]interface{}{ | ||
"name": "mytask", | ||
"logs": logs, | ||
}) | ||
|
||
logger := TpiLogger(d) | ||
logger.Info("logs") | ||
} | ||
|
||
func TestMachine(t *testing.T) { | ||
d := generateSchemaData(t, map[string]interface{}{ | ||
"name": "mytask", | ||
"cloud": "aws", | ||
"machine": "t2.micro", | ||
"spot": 0.2, | ||
"region": "us-west", | ||
}) | ||
|
||
logger := TpiLogger(d) | ||
logger.Info("instance") | ||
} | ||
|
||
func generateSchemaData(t *testing.T, raw map[string]interface{}) *schema.ResourceData { | ||
sch := map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
ForceNew: true, | ||
Optional: true, | ||
}, | ||
"cloud": { | ||
Type: schema.TypeString, | ||
ForceNew: true, | ||
Required: true, | ||
}, | ||
"region": { | ||
Type: schema.TypeString, | ||
ForceNew: true, | ||
Optional: true, | ||
Default: "us-west", | ||
}, | ||
"machine": { | ||
Type: schema.TypeString, | ||
ForceNew: true, | ||
Optional: true, | ||
Default: "m", | ||
}, | ||
"disk_size": { | ||
Type: schema.TypeInt, | ||
ForceNew: true, | ||
Optional: true, | ||
Default: 30, | ||
}, | ||
"spot": { | ||
Type: schema.TypeFloat, | ||
ForceNew: true, | ||
Optional: true, | ||
Default: -1, | ||
}, | ||
"image": { | ||
Type: schema.TypeString, | ||
ForceNew: true, | ||
Optional: true, | ||
Default: "ubuntu", | ||
}, | ||
"addresses": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"status": { | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeInt, | ||
}, | ||
}, | ||
"events": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"logs": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"script": { | ||
Type: schema.TypeString, | ||
ForceNew: true, | ||
Required: true, | ||
}, | ||
"workdir": { | ||
Optional: true, | ||
Type: schema.TypeSet, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"input": { | ||
Type: schema.TypeString, | ||
ForceNew: true, | ||
Optional: true, | ||
Default: "", | ||
}, | ||
"output": { | ||
Type: schema.TypeString, | ||
ForceNew: false, | ||
Optional: true, | ||
Default: "", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
return schema.TestResourceDataRaw(t, sch, raw) | ||
} |
Oops, something went wrong.