Skip to content

Commit 087a299

Browse files
committed
Add service logs command to faasd ce from faasd-pro
A more limited version is added, for continuity. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
1 parent 6dcdab8 commit 087a299

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func init() {
1717
rootCommand.AddCommand(installCmd)
1818
rootCommand.AddCommand(makeProviderCmd())
1919
rootCommand.AddCommand(collectCmd)
20+
rootCommand.AddCommand(makeServiceCmd())
2021
}
2122

2223
func RootCommand() *cobra.Command {

cmd/service.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cmd
2+
3+
import "github.com/spf13/cobra"
4+
5+
func makeServiceCmd() *cobra.Command {
6+
var command = &cobra.Command{
7+
Use: "service",
8+
Short: "Manage services",
9+
Long: `Manage services created by faasd from the docker-compose.yml file`,
10+
}
11+
12+
command.RunE = runServiceE
13+
14+
command.AddCommand(makeServiceLogsCmd())
15+
return command
16+
}
17+
18+
func runServiceE(cmd *cobra.Command, args []string) error {
19+
20+
return cmd.Help()
21+
22+
}

cmd/service_logs.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"os"
8+
"time"
9+
10+
goexecute "github.com/alexellis/go-execute/v2"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
func makeServiceLogsCmd() *cobra.Command {
15+
var command = &cobra.Command{
16+
Use: "logs",
17+
Short: "View logs for a service",
18+
Long: `View logs for a service created by faasd from the docker-compose.yml file.`,
19+
Example: ` ## View logs for the gateway for the last hour
20+
faasd service logs gateway --since 1h
21+
22+
## View logs for the cron-connector, and tail them
23+
faasd service logs cron-connector -f
24+
`,
25+
}
26+
27+
command.Flags().Duration("since", 10*time.Minute, "How far back in time to include logs")
28+
command.Flags().BoolP("follow", "f", false, "Follow the logs")
29+
30+
command.RunE = runServiceLogsE
31+
command.PreRunE = preRunServiceLogsE
32+
33+
return command
34+
}
35+
36+
func runServiceLogsE(cmd *cobra.Command, args []string) error {
37+
name := args[0]
38+
39+
namespace, _ := cmd.Flags().GetString("namespace")
40+
follow, _ := cmd.Flags().GetBool("follow")
41+
since, _ := cmd.Flags().GetDuration("since")
42+
43+
journalTask := goexecute.ExecTask{
44+
Command: "journalctl",
45+
Args: []string{"-o", "cat", "-t", fmt.Sprintf("%s:%s", namespace, name)},
46+
StreamStdio: true,
47+
}
48+
49+
if follow {
50+
journalTask.Args = append(journalTask.Args, "-f")
51+
}
52+
53+
if since != 0 {
54+
// Calculate the timestamp that is 'age' duration ago
55+
sinceTime := time.Now().Add(-since)
56+
// Format according to journalctl's expected format: "2012-10-30 18:17:16"
57+
formattedTime := sinceTime.Format("2006-01-02 15:04:05")
58+
journalTask.Args = append(journalTask.Args, fmt.Sprintf("--since=%s", formattedTime))
59+
}
60+
61+
res, err := journalTask.Execute(context.Background())
62+
if err != nil {
63+
return err
64+
}
65+
66+
if res.ExitCode != 0 {
67+
return fmt.Errorf("failed to get logs for service %s: %s", name, res.Stderr)
68+
}
69+
70+
return nil
71+
}
72+
73+
func preRunServiceLogsE(cmd *cobra.Command, args []string) error {
74+
75+
if os.Geteuid() != 0 {
76+
return errors.New("this command must be run as root")
77+
}
78+
79+
if len(args) == 0 {
80+
return errors.New("service name is required as an argument")
81+
}
82+
83+
namespace, _ := cmd.Flags().GetString("namespace")
84+
if namespace == "" {
85+
return errors.New("namespace is required")
86+
}
87+
88+
return nil
89+
}

0 commit comments

Comments
 (0)