Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-at-startupmedia committed Sep 24, 2024
2 parents 8202212 + b0fa608 commit bcb9d35
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 28 deletions.
102 changes: 87 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ make systemd_install
### Using Release Installer

```bash
wget -O - https://raw.githubusercontent.com/joe-at-startupmedia/pmon3/master/release-installer.bash | bash -s 1.16.1
wget -O - https://raw.githubusercontent.com/joe-at-startupmedia/pmon3/master/release-installer.bash | bash -s 1.16.2
```

:exclamation::exclamation: Note :exclamation::exclamation:
Expand All @@ -76,7 +76,7 @@ Usage:
pmon3 [command]
Available Commands:
appconfig Output Application Configuration JSON
appconfig Export Application Configuration
completion Generate completion script
del Delete process by id or name
desc Show process information by id or name
Expand Down Expand Up @@ -157,15 +157,6 @@ Parameter arguments need to use the absolute path.
pmon3 ls
```

### Top Native [ topn ]

This will output the resource utilization of all processes using the native `top` command that is pre-installed on most unix-based operating systems. It will only show those processes managed by (and including) the `pmond` process. The output is updated every few seconds until the process is terminated using Ctrl+C.

```bash
pmon3 topn
```
<img width="559" alt="pmon3_topn" src="https://github.com/joe-at-startupmedia/pmon3/assets/13522698/a77cce0f-55b0-479f-8489-d6aaf9fcdd6b">

### (re)start the process [ restart/start ]

```bash
Expand Down Expand Up @@ -257,12 +248,27 @@ pmon3 reset -p [id_or_name]
<a name="pmon3_appconfig"></a>
### Output Application Configuration JSON [ appconfig ]

This command will output Application Configuration JSON from the current process list. This command is useful when you want to generate [Application Configuration](#section_appconfig) JSON to use for pmond initialization from the specified `apps_config_file`.
This command will export Application Configuration from the current process list. This command is useful when you want to generate [Application Configuration](#section_appconfig) to use for pmond initialization from the specified `apps_config_file`.

```bash
pmon3 appconfig

#specify toml as a format
pmon3 appconfig -f toml

#specify yaml as a format
pmon3 appconfig -f yaml
```

### Top Native [ topn ]

This will output the resource utilization of all processes using the native `top` command that is pre-installed on most unix-based operating systems. It will only show those processes managed by (and including) the `pmond` process. The output is updated every few seconds until the process is terminated using Ctrl+C.

```bash
pmon3 topn
```
<img width="559" alt="pmon3_topn" src="https://github.com/joe-at-startupmedia/pmon3/assets/13522698/a77cce0f-55b0-479f-8489-d6aaf9fcdd6b">

<a name="section_config"></a>
## Configuration
The default path of the configuration file is `/etc/pmon3/config/config.yml`. This value can be overridden with the `PMON3_CONF` environment variable.
Expand Down Expand Up @@ -306,7 +312,7 @@ shmem_dir: /dev/shm/
mq_user:
# specify a custom group to access files in posix_mq_dir or shmem_dir (must also provide a user)
mq_group:
# a JSON configuration file to specify a list of apps to start on the first initialization
# a configuration file to specify a list of apps to start on the first initialization (json, yaml or toml)
apps_config_file: /etc/pmon3/config/app.config.json
```
Expand Down Expand Up @@ -346,12 +352,13 @@ If applications are specified in the Apps Config, they will overwrite matching p

### Configuration

#### /etc/pmon3/config/config.yml
```yaml
#default value when empty or omitted
# a configuration file to specify a list of apps to start on the first initialization (json, yaml or toml)
apps_config_file: /etc/pmon3/config/apps.config.json
```

supported formats are json, toml and yaml

#### /etc/pmon3/config/app.config.json
```json
{
Expand Down Expand Up @@ -391,6 +398,71 @@ apps_config_file: /etc/pmon3/config/apps.config.json
}
```

#### /etc/pmon3/config/app.config.yaml
```yaml
apps:
- file: "/usr/local/bin/happac"
flags:
name: happac1
args: "-h startup-patroni-1.node.consul -p 5555 -r 5000"
user: vagrant
log_dir: "/var/log/custom/"
dependencies:
- happac2
groups:
- happac
- file: "/usr/local/bin/happab"
flags:
name: happac2
log: "/var/log/happac2.log"
args: "-h startup-patroni-1.node.consul -p 5556 -r 5001"
user: vagrant
no_auto_restart: true
groups:
- happac
- file: "/usr/local/bin/node"
flags:
name: metabase-api
args: "/var/www/vhosts/metabase-api/index.js"
env_vars: NODE_ENV=prod
user: dw_user
```

#### /etc/pmon3/config/app.config.toml
Unlike json and yaml, all fields are camel-cased:
```toml
[[Apps]]
File = "/usr/local/bin/happac"
[Apps.Flags]
Name = "happac1"
Args = "-h startup-patroni-1.node.consul -p 5555 -r 5000"
User = "vagrant"
LogDir = "/var/log/custom/"
Dependencies = [ "happac2" ]
Groups = [ "happac" ]
[[Apps]]
File = "/usr/local/bin/happab"
[Apps.Flags]
Name = "happac2"
Log = "/var/log/happac2.log"
Args = "-h startup-patroni-1.node.consul -p 5556 -r 5001"
User = "vagrant"
NoAutoRestart = true
Groups = [ "happac" ]
[[Apps]]
File = "/usr/local/bin/node"
[Apps.Flags]
Name = "metabase-api"
Args = "/var/www/vhosts/metabase-api/index.js"
EnvVars = "NODE_ENV=prod"
User = "dw_user"
```

### Generation Utility

Instead of configuring this file from scratch you can use the the [appconfig](#pmon3_appconfig) command to output JSON from the current process list. This will allow the administrator to dynamically build a process list using imperative commands without having to manually write the configuration file.
Expand Down
56 changes: 53 additions & 3 deletions cli/cmd/appconfig/appconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,33 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/BurntSushi/toml"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"pmon3/cli"
"pmon3/cli/cmd/base"
"pmon3/pmond/model"
)

type flags struct {
format string
}

var flag flags

var Cmd = &cobra.Command{
Use: "appconfig",
Short: "Output Application Configuration JSON",
Use: "appconfig",

Short: "Export Application Configuration",
Run: func(cmd *cobra.Command, args []string) {
cmdRun(args)
},
}

func init() {
Cmd.Flags().StringVarP(&flag.format, "format", "f", "json", "the format to export")
}

func jsonPrettyPrint(in string) string {
var out bytes.Buffer
err := json.Indent(&out, []byte(in), "", " ")
Expand All @@ -26,6 +40,24 @@ func jsonPrettyPrint(in string) string {
return out.String()
}

func tomlPrettyPrint(ac *model.AppsConfig) string {
out := new(bytes.Buffer)
encoder := toml.NewEncoder(out)
if err := encoder.Encode(ac); err != nil {
cli.Log.Fatal(err)
}
return out.String()
}

func yamlPrettyPrint(ac *model.AppsConfig) string {
out := new(bytes.Buffer)
encoder := yaml.NewEncoder(out)
if err := encoder.Encode(ac); err != nil {
cli.Log.Fatal(err)
}
return out.String()
}

func cmdRun(args []string) {
base.OpenSender()
defer base.CloseSender()
Expand All @@ -35,5 +67,23 @@ func cmdRun(args []string) {
cli.Log.Fatalf(newCmdResp.GetError())
}
jsonOutput := newCmdResp.GetValueStr()
fmt.Println(jsonPrettyPrint(jsonOutput))

var ac model.AppsConfig
if flag.format == "toml" || flag.format == "yaml" {
err := json.Unmarshal([]byte(jsonOutput), &ac)
if err != nil {
cli.Log.Fatal(err)
}
}

switch flag.format {
case "toml":
fmt.Println(tomlPrettyPrint(&ac))
case "yaml":
fmt.Println(yamlPrettyPrint(&ac))
case "json":
fmt.Println(jsonPrettyPrint(jsonOutput))
default:
cli.Log.Fatalf("The only valid formats accepted are: json, toml or yaml")
}
}
2 changes: 1 addition & 1 deletion conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// current app version
var Version = "1.16.1"
var Version = "1.16.2"

const DEFAULT_LOG_LEVEL = logrus.InfoLevel

Expand Down
2 changes: 1 addition & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@
#mq_user:
# specify a custom group to access files in posix_mq_dir or shmem_dir (must also provide a user)
#mq_group:
# a JSON configuration file to specify a list of apps to start on the first initialization
# a configuration file to specify a list of apps to start on the first initialization (json, yaml or toml)
#apps_config_file: /etc/pmon3/config/app.config.json
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22
toolchain go1.22.4

require (
github.com/BurntSushi/toml v1.2.0
github.com/charmbracelet/bubbletea v0.26.0
github.com/charmbracelet/lipgloss v0.10.0
github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203
Expand All @@ -24,12 +25,12 @@ require (
github.com/struCoder/pidusage v0.2.1
github.com/tabalt/gracehttp v1.3.0
google.golang.org/protobuf v1.34.2
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/sqlite v1.5.5
gorm.io/gorm v1.25.10
)

require (
github.com/BurntSushi/toml v1.2.0 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
Expand Down Expand Up @@ -82,7 +83,6 @@ require (
golang.org/x/sys v0.24.0 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect
modernc.org/libc v1.41.0 // indirect
modernc.org/mathutil v1.6.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions pmond/model/exec_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (

type ExecFlags struct {
User string `json:"user"`
Log string `json:"log,omitempty"`
LogDir string `json:"log_dir,omitempty"`
Log string `json:"log,omitempty" yaml:"log,omitempty" toml:"Log,omitempty"`
LogDir string `json:"log_dir,omitempty" yaml:"log_dir,omitempty" toml:"LogDir,omitempty"`
Args string `json:"args"`
EnvVars string `json:"env_vars,omitempty"`
EnvVars string `json:"env_vars,omitempty" yaml:"env_vars,omitempty" toml:"EnvVars,omitempty"`
Name string `json:"name"`
Dependencies []string `json:"dependencies,omitempty"`
Groups []string `json:"groups,omitempty"`
NoAutoRestart bool `json:"no_auto_restart"`
Dependencies []string `json:"dependencies,omitempty" yaml:"dependencies,omitempty" toml:"dependencies,omitempty"`
Groups []string `json:"groups,omitempty" yaml:"groups,omitempty" toml:"groups,omitempty" `
NoAutoRestart bool `json:"no_auto_restart" yaml:"no_auto_restart,omitempty" toml:"NoAutoRestart,omitempty"`
}

func (e *ExecFlags) Parse(jsonStr string) (*ExecFlags, error) {
Expand Down

0 comments on commit bcb9d35

Please sign in to comment.