Init
This commit is contained in:
parent
2454c5984e
commit
541222e2d0
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
gocage
|
||||
go.sum
|
||||
|
102
cmd/list.go
Normal file
102
cmd/list.go
Normal file
@ -0,0 +1,102 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
"log"
|
||||
"io/ioutil"
|
||||
"gocage/jail"
|
||||
"encoding/json"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
|
||||
var gJails []Jail
|
||||
|
||||
|
||||
func listJails(args []string) {
|
||||
for _, d := range viper.GetStringSlice("datastore") {
|
||||
listJailsFromDatastore(d)
|
||||
}
|
||||
|
||||
for _, j := range (gJails) {
|
||||
if j.Running {
|
||||
fmt.Printf("%d ; %s ; %s ; %s\n", j.JID, j.Name, j.Config.Ip4_addr, j.Config.Release)
|
||||
} else {
|
||||
fmt.Printf(" ; %s ; %s ; %s\n", j.Name, j.Config.Ip4_addr, j.Config.Release)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func listJailsFromDatastore(datastore string) {
|
||||
fileInfo, err := os.Stat(datastore)
|
||||
if err != nil { log.Fatalln(fmt.Sprintf("Unable to access %s, check path and/or rights", datastore)) }
|
||||
if fileInfo.IsDir() == false { log.Fatalln(fmt.Sprintf("%s is not a directory", datastore)) }
|
||||
|
||||
// A datastore have to contain a "jails" directory
|
||||
jailsDir := fmt.Sprintf("%s/jails", datastore)
|
||||
fileInfo, err = os.Stat(jailsDir)
|
||||
if err != nil { log.Fatalln(fmt.Sprintf("Unable to access %s, check path and/or rights", jailsDir)) }
|
||||
if fileInfo.IsDir() == false { log.Fatalln(fmt.Sprintf("%s is not a directory", jailsDir)) }
|
||||
|
||||
listJailsFromDirectory(jailsDir)
|
||||
}
|
||||
|
||||
|
||||
func listJailsFromDirectory(dir string) ([]Jail) {
|
||||
files, err := ioutil.ReadDir(dir)
|
||||
if err != nil { log.Fatalln(fmt.Sprintf("Unable to browse %s, check path and/or rights", dir)) }
|
||||
|
||||
for _, fi := range files {
|
||||
if fi.IsDir() == true {
|
||||
|
||||
// 1. Get conf from config.json
|
||||
jailConfPath := fmt.Sprintf("%s/%s/%s", dir, fi.Name(), "config.json")
|
||||
jailConf, err := getJailConfig(jailConfPath)
|
||||
if err != nil { log.Println("ERROR reading jail config for %s", jailConfPath) }
|
||||
|
||||
// 2. Build jail object from config
|
||||
jailRootPath := fmt.Sprintf("%s/%s/%s", dir, fi.Name(), "root")
|
||||
j := Jail{
|
||||
Name: jailConf.Host_hostname,
|
||||
Config: jailConf,
|
||||
ConfigPath: jailConfPath,
|
||||
RootPath: jailRootPath,
|
||||
Running: false,
|
||||
}
|
||||
|
||||
// 3. Add current running informations
|
||||
// FIXME : use rj.host.hostname
|
||||
//j.Internal_name = fmt.Sprintf("ioc-%s", j.Name)
|
||||
|
||||
rjails, err := jail.GetJails()
|
||||
if err != nil { log.Fatalln("Unable to list running jails") }
|
||||
for _, rj := range rjails {
|
||||
if rj.Path == j.RootPath {
|
||||
j.JID = rj.Jid
|
||||
j.Running = true
|
||||
j.InternalName = rj.Name
|
||||
}
|
||||
}
|
||||
|
||||
gJails = append(gJails, j)
|
||||
}
|
||||
}
|
||||
|
||||
return gJails
|
||||
}
|
||||
|
||||
|
||||
func getJailConfig(jailConfigPath string) (JailConfig, error) {
|
||||
content, err := ioutil.ReadFile(jailConfigPath)
|
||||
if err != nil { log.Fatalln(fmt.Sprintf("Unable to read %s, check path and/or rights", jailConfigPath)) }
|
||||
|
||||
var jc JailConfig
|
||||
err = json.Unmarshal([]byte(content), &jc)
|
||||
if err != nil { log.Fatalln(fmt.Sprintf("Error occured during unmarshaling %s: %s", jailConfigPath, err.Error())) }
|
||||
|
||||
//fmt.Printf("Jail Config: %#v\n", jc)
|
||||
|
||||
return jc, err
|
||||
}
|
95
cmd/root.go
Normal file
95
cmd/root.go
Normal file
@ -0,0 +1,95 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
// "strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
const (
|
||||
version = "0.001"
|
||||
)
|
||||
|
||||
var (
|
||||
gConfigFile string
|
||||
|
||||
rootCmd = & cobra.Command{
|
||||
Use: "gocage",
|
||||
Short: "GoCage is a FreeBSD Jail management tool",
|
||||
Long: `GoCage is a jail management tool. It support VNET, host-only, NAT networks. Provides snapshots and cloning.
|
||||
It support iocage jails and can coexist with iocage.`,
|
||||
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("Here we are in the Run")
|
||||
},
|
||||
}
|
||||
|
||||
versionCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Print the version number of GoCage",
|
||||
Long: `Let this show you how much fail I had to get this *cough* perfect`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Printf("GoCage v.%s\n", version)
|
||||
},
|
||||
}
|
||||
|
||||
listCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "Print jails",
|
||||
Long: `Display jails, their IP and OS`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
listJails(args)
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
// Global switches
|
||||
rootCmd.PersistentFlags().StringVarP(&gConfigFile, "config", "c", "/etc/gocage/gocage.conf", "GoCage configuration file")
|
||||
|
||||
// Command dependant switches
|
||||
/* listComputerCmd.PersistentFlags().BoolVarP(&gDisplayAsCSV, "csv-format", "v", false, "Show results in CSV (separator = ';')")
|
||||
searchComputerCmd.PersistentFlags().BoolVarP(&gShowAccount, "show-account", "c", false, "Show account when listing computer")
|
||||
searchComputerCmd.PersistentFlags().BoolVarP(&gDisplayAsCSV, "csv-format", "v", false, "Show results in CSV (separator = ';')")
|
||||
*/
|
||||
|
||||
// Now declare commands
|
||||
rootCmd.AddCommand(versionCmd)
|
||||
rootCmd.AddCommand(listCmd)
|
||||
}
|
||||
|
||||
func initConfig() {
|
||||
if gConfigFile == "" {
|
||||
fmt.Println("No config file!")
|
||||
return
|
||||
}
|
||||
|
||||
// fmt.Printf("We are in initConfig(), with config file %s\n", gConfigFile)
|
||||
|
||||
viper.SetConfigFile(gConfigFile)
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
fmt.Printf("ERROR reading config file %s : %s\n", gConfigFile, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// fmt.Println("Using config file:", viper.ConfigFileUsed())
|
||||
// fmt.Printf("datastore in config : %s\n", viper.GetStringSlice("datastore"))
|
||||
// fmt.Printf("datastore.0 in config : %s\n", viper.GetStringSlice("datastore.0"))
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
|
154
cmd/struct.go
Normal file
154
cmd/struct.go
Normal file
@ -0,0 +1,154 @@
|
||||
package cmd
|
||||
|
||||
type Jail struct {
|
||||
Name string
|
||||
InternalName string
|
||||
JID int
|
||||
Config JailConfig
|
||||
RootPath string
|
||||
ConfigPath string
|
||||
Running bool
|
||||
}
|
||||
|
||||
// iocage struct as stored in config.json
|
||||
// CONFIG_VERSION = 27
|
||||
type JailConfig struct {
|
||||
Config_version string `json:"CONFIG_VERSION"`
|
||||
Allow_chflags int `json:"allow_chflags"`
|
||||
Allow_mlock int `json:"allow_mlock"`
|
||||
Allow_mount int `json:"allow_mount"`
|
||||
Allow_mount_devfs int `json:"allow_mount_devfs"`
|
||||
Allow_mount_fusefs int `json:"allow_mount_fusefs"`
|
||||
Allow_mount_nullfs int `json:"allow_mount_nullfs"`
|
||||
Allow_mount_procfs int `json:"allow_mount_procfs"`
|
||||
Allow_mount_tmpfs int `json:"allow_mount_tmpfs"`
|
||||
Allow_mount_zfs int `json:"allow_mount_zfs"`
|
||||
Allow_quotas int `json:"allow_quotas"`
|
||||
Allow_raw_sockets int `json:"allow_raw_sockets"`
|
||||
Allow_set_hostname int `json:"allow_set_hostname"`
|
||||
Allow_socket_af int `json:"allow_socket_af"`
|
||||
Allow_sysvipc int `json:"allow_sysvipc"`
|
||||
Allow_tun int `json:"allow_tun"`
|
||||
Allow_vmm int `json:"allow_vmm"`
|
||||
Assign_localhost int `json:"assign_localhost"`
|
||||
Available string `json:"available"`
|
||||
Basejail int `json:"basejail"`
|
||||
Boot int `json:"boot"`
|
||||
Bpf int `json:"bpf"`
|
||||
Children_max string `json:"children_max"`
|
||||
Cloned_release string `json:"cloned_release"`
|
||||
Comment string `json:"comment"`
|
||||
Compression string `json:"compression"`
|
||||
Compressratio string `json:"compressratio"`
|
||||
Coredumpsize string `json:"coredumpsize"`
|
||||
Count string `json:"count"`
|
||||
Cpuset string `json:"cpuset"`
|
||||
Cputime string `json:"cputime"`
|
||||
Datasize string `json:"datasize"`
|
||||
Dedup string `json:"dedup"`
|
||||
Defaultrouter string `json:"defaultrouter"`
|
||||
Defaultrouter6 string `json:"defaultrouter6"`
|
||||
Depends string `json:"depends"`
|
||||
Devfs_ruleset string `json:"devfs_ruleset"`
|
||||
Dhcp int `json:"dhcp"`
|
||||
Enforce_statfs string `json:"enforce_statfs"`
|
||||
Exec_clean int `json:"exec_clean"`
|
||||
Exec_created string `json:"exec_created"`
|
||||
Exec_fib string `json:"exec_fib"`
|
||||
Exec_jail_user string `json:"exec_jail_user"`
|
||||
Exec_poststart string `json:"exec_poststart"`
|
||||
Exec_poststop string `json:"exec_poststop"`
|
||||
Exec_prestart string `json:"exec_prestart"`
|
||||
Exec_prestop string `json:"exec_prestop"`
|
||||
Exec_start string `json:"exec_start"`
|
||||
Exec_stop string `json:"exec_stop"`
|
||||
Exec_system_jail_user string `json:"exec_system_jail_user"`
|
||||
Exec_system_user string `json:"exec_system_user"`
|
||||
Exec_timeout string `json:"exec_timeout"`
|
||||
Host_domainname string `json:"host_domainname"`
|
||||
Host_hostname string `json:"host_hostname"`
|
||||
Host_hostuuid string `json:"host_hostuuid"`
|
||||
Host_time int `json:"host_time"`
|
||||
Hostid string `json:"hostid"`
|
||||
Hostid_strict_check int `json:"hostid_strict_check"`
|
||||
Interfaces string `json:"interfaces"`
|
||||
Ip4 string `json:"ip4"`
|
||||
Ip4_addr string `json:"ip4_addr"`
|
||||
Ip4_saddrsel string `json:"ip4_saddrsel"`
|
||||
Ip6 string `json:"ip6"`
|
||||
Ip6_addr string `json:"ip6_addr"`
|
||||
Ip6_saddrsel string `json:"ip4_saddrsel"`
|
||||
Ip_hostname int `json:"ip_hostname"`
|
||||
Jail_zfs int `json:"jail_zfs"`
|
||||
Jail_zfs_dataset string `json:"jail_zfs_dataset"`
|
||||
Jail_zfs_mountpoint string `json:"jail_zfs_mountpoint"`
|
||||
Last_started string `json:"last_started"`
|
||||
Localhost_ip string `json:"localhost_ip"`
|
||||
Login_flags string `json:"login_flags"`
|
||||
Mac_prefix string `json:"mac_prefix"`
|
||||
Maxproc string `json:"maxproc"`
|
||||
Memorylocked string `json:"memorylocked"`
|
||||
Memoryuse string `json:"memoryuse"`
|
||||
Min_dyn_devfs_ruleset string `json:"min_dyn_devfs_ruleset"`
|
||||
Mount_devfs int `json:"mount_devfs"`
|
||||
Mount_fdescfs int `json:"mount_fdescfs"`
|
||||
Mount_linprocfs int `json:"mount_linprocfs"`
|
||||
Mount_procfs int `json:"mount_procfs"`
|
||||
Mountpoint string `json:"mountpoint"`
|
||||
Msgqqueued string `json:"msgqqueued"`
|
||||
Msgqsize string `json:"msgqsize"`
|
||||
Nat int `json:"nat"`
|
||||
Nat_backend string `json:"nat_backend"`
|
||||
Nat_forwards string `json:"nat_forwards"`
|
||||
Nat_interface string `json:"nat_interface"`
|
||||
Nat_prefix string `json:"nat_prefix"`
|
||||
Nmsgq string `json:"nmsgq"`
|
||||
Notes string `json:"notes"`
|
||||
Nsem string `json:"nsem"`
|
||||
Nsemop string `json:"nsemop"`
|
||||
Nshm string `json:"nshm"`
|
||||
Nthr string `json:"nthr"`
|
||||
Openfiles string `json:"openfiles"`
|
||||
Origin string `json:"origin"`
|
||||
Owner string `json:"owner"`
|
||||
Pcpu string `json:"pcpu"`
|
||||
Plugin_name string `json:"plugin_name"`
|
||||
Plugin_repository string `json:"plugin_repository"`
|
||||
Priority string `json:"priority"`
|
||||
Pseudoterminals string `json:"pseudoterminals"`
|
||||
Quota string `json:"quota"`
|
||||
Readbps string `json:"readbps"`
|
||||
Readiops string `json:"readiops"`
|
||||
Release string `json:"release"`
|
||||
Reservation string `json:"reservation"`
|
||||
Resolver string `json:"resolver"`
|
||||
Rlimits string `json:"rlimits"`
|
||||
Rtsold int `json:"rtsold"`
|
||||
Securelevel string `json:"securelevel"`
|
||||
Shmsize string `json:"shmsize"`
|
||||
Stacksize string `json:"stacksize"`
|
||||
Stop_timeout string `json:"stop_timeout"`
|
||||
Swapuse string `json:"swapuse"`
|
||||
Sync_state string `json:"sync_state"`
|
||||
Sync_target string `json:"sync_target"`
|
||||
Sync_tgt_zpool string `json:"sync_tgt_zpool"`
|
||||
Sysvmsg string `json:"sysvmsg"`
|
||||
Sysvsem string `json:"sysvsem"`
|
||||
Sysvshm string `json:"sysvshm"`
|
||||
Template int `json:"template"`
|
||||
// Go don't like a variable named "type" (And i dont care about finding a cleaner way)
|
||||
Jailtype string `json:"type"`
|
||||
Used string `json:"used"`
|
||||
Vmemoryuse string `json:"vmemoryuse"`
|
||||
Vnet int `json:"vnet"`
|
||||
Vnet0_mac string `json:"vnet0_mac"`
|
||||
Vnet1_mac string `json:"vnet1_mac"`
|
||||
Vnet2_mac string `json:"vnet2_mac"`
|
||||
Vnet3_mac string `json:"vnet3_mac"`
|
||||
Vnet_default_interface string `json:"vnet_default_interface"`
|
||||
Vnet_interfaces string `json:"vnet_interfaces"`
|
||||
Wallclock string `json:"wallclock"`
|
||||
Writebps string `json:"writebps"`
|
||||
Writeiops string `json:"writeiops"`
|
||||
}
|
||||
|
26
go.mod
Normal file
26
go.mod
Normal file
@ -0,0 +1,26 @@
|
||||
module gocage
|
||||
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/spf13/cobra v1.2.1
|
||||
github.com/spf13/viper v1.9.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/fsnotify/fsnotify v1.5.1 // indirect
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/inconshreveable/mousetrap v1.0.0 // indirect
|
||||
github.com/magiconair/properties v1.8.5 // indirect
|
||||
github.com/mitchellh/mapstructure v1.4.2 // indirect
|
||||
github.com/pelletier/go-toml v1.9.4 // indirect
|
||||
github.com/spf13/afero v1.6.0 // indirect
|
||||
github.com/spf13/cast v1.4.1 // indirect
|
||||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/subosito/gotenv v1.2.0 // indirect
|
||||
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
|
||||
golang.org/x/text v0.3.6 // indirect
|
||||
gopkg.in/ini.v1 v1.63.2 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
)
|
98
jail/main.go
Normal file
98
jail/main.go
Normal file
@ -0,0 +1,98 @@
|
||||
// Copyright 2021, johan@nosd.in
|
||||
// +build freebsd
|
||||
//
|
||||
// Use libjail.so to get/set jail params
|
||||
package jail
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -I /usr/lib
|
||||
#cgo LDFLAGS: -L. -ljail -lc
|
||||
#include <stdlib.h>
|
||||
#include <jail.h>
|
||||
#include <utmpx.h>
|
||||
#include <pwd.h>
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"strconv"
|
||||
// "syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
|
||||
type Jail struct {
|
||||
Name string
|
||||
Jid int
|
||||
Path string
|
||||
}
|
||||
|
||||
|
||||
// We can not use jail_getv ou jail_setv because they are variadic C functions (would need a C wrapper)
|
||||
func GetJails() ([]Jail, error) {
|
||||
var jls []Jail
|
||||
var jl Jail
|
||||
var err error
|
||||
|
||||
// Make "params" a list of 4 jails parameters
|
||||
params := make([]C.struct_jailparam, 4)
|
||||
|
||||
// initialize parameter names
|
||||
csname := C.CString("name")
|
||||
defer C.free(unsafe.Pointer(csname))
|
||||
csjid := C.CString("jid")
|
||||
defer C.free(unsafe.Pointer(csjid))
|
||||
cspath := C.CString("path")
|
||||
defer C.free(unsafe.Pointer(cspath))
|
||||
cslastjid := C.CString("lastjid")
|
||||
defer C.free(unsafe.Pointer(cslastjid))
|
||||
|
||||
// initialize params struct with parameter names
|
||||
C.jailparam_init(¶ms[0], csname)
|
||||
C.jailparam_init(¶ms[1], csjid)
|
||||
C.jailparam_init(¶ms[2], cspath)
|
||||
|
||||
// The key to retrieve jail. lastjid = 0 returns first jail and its jid as jailparam_get return value
|
||||
C.jailparam_init(¶ms[3], cslastjid)
|
||||
|
||||
lastjailid := 0
|
||||
cslastjidval := C.CString(strconv.Itoa(lastjailid))
|
||||
defer C.free(unsafe.Pointer(cslastjidval))
|
||||
|
||||
C.jailparam_import(¶ms[3], cslastjidval)
|
||||
|
||||
// loop on existing jails
|
||||
for lastjailid >= 0 {
|
||||
// get parameter values
|
||||
lastjailid = int(C.jailparam_get(¶ms[0], 4, 0))
|
||||
if lastjailid > 0 {
|
||||
nametmp := C.jailparam_export(¶ms[0])
|
||||
jl.Name = C.GoString(nametmp)
|
||||
// Memory mgmt : Non gere par Go
|
||||
C.free(unsafe.Pointer(nametmp))
|
||||
|
||||
jidtmp := C.jailparam_export(¶ms[1])
|
||||
jl.Jid, _ = strconv.Atoi(C.GoString(jidtmp))
|
||||
// Memory mgmt : Non gere par Go
|
||||
C.free(unsafe.Pointer(jidtmp))
|
||||
|
||||
pathtmp := C.jailparam_export(¶ms[2])
|
||||
jl.Path = C.GoString(pathtmp)
|
||||
// Memory mgmt : Non gere par Go
|
||||
C.free(unsafe.Pointer(pathtmp))
|
||||
|
||||
jls = append(jls, jl)
|
||||
//log.Debug("Got jid " + strconv.Itoa(jl.jid) + " with name " + jl.name)
|
||||
|
||||
// Prepare next loop iteration
|
||||
cslastjidval := C.CString(strconv.Itoa(lastjailid))
|
||||
defer C.free(unsafe.Pointer(cslastjidval))
|
||||
C.jailparam_import(¶ms[3], cslastjidval)
|
||||
}
|
||||
}
|
||||
|
||||
// Free 4 items of params list
|
||||
C.jailparam_free(¶ms[0], 4)
|
||||
|
||||
return jls, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user