utils

package module
v0.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2022 License: MulanPSL-2.0 Imports: 19 Imported by: 4

README

utils

Go utilities

script

git clone https://codehub-y.huawei.com/dcs/container/utils.git

cd utils
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ${HOME}/script_test cmd/script_test/main.go

chmod +x ${HOME}/script_test

${HOME}/script_test -path "${HOME}/test.sh" -args "-m hello" -env "EXIT_CODE_OK=0 ERROR_CODE_INVALID=1" -out "${HOME}/test.out" -dir "${HOME}"

Documentation

Overview

Copyright (c) 2022 Yaohui Wang (yaohuiwang@outlook.com) utils is licensed under Mulan PubL v2. You can use this software according to the terms and conditions of the Mulan PubL v2. You may obtain a copy of Mulan PubL v2 at:

http://license.coscl.org.cn/MulanPubL-2.0

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PubL v2 for more details.

Copyright (c) 2022 Yaohui Wang (yaohuiwang@outlook.com) utils is licensed under Mulan PubL v2. You can use this software according to the terms and conditions of the Mulan PubL v2. You may obtain a copy of Mulan PubL v2 at:

http://license.coscl.org.cn/MulanPubL-2.0

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PubL v2 for more details.

Index

Constants

View Source
const (
	LOG_DEBUG int = iota
	LOG_INFO
	LOG_WARN
	LOG_ERROR
)
View Source
const (
	MODE_PERM_RW fs.FileMode = 0600
	MODE_PERM_RO fs.FileMode = 0400
)
View Source
const (
	STATUS_UP      string = "up"
	STATUS_DOWN    string = "down"
	STATUS_PENDING string = "pending"
)
View Source
const (
	ScriptStatusWaiting = "waiting"
	ScriptStatusRunning = "running"
	ScriptStatusExited  = "exited"
	ScriptStatusKilled  = "killed"
	ScriptStatusFailed  = "failed_start"
)
View Source
const (
	DefaultThreadQueueSize int = 64
	DefaultThreadPoolSize  int = 8
)

Variables

View Source
var (
	Logger   *log.Logger = log.Default()
	LogLevel int         = LOG_DEBUG
	LogLabel []string    = []string{"DEBUG", "INFO", "WARN", "ERROR"}
)

Functions

func AllocIPFromCidr

func AllocIPFromCidr(cidr string, exist []string) (*string, error)

allocate ip from cidr

func AllocIPFromRange

func AllocIPFromRange(start string, end string, exist []string) (*string, error)

allocate ip from range

func CreateFile

func CreateFile(target string, data ...string) error

func DelFileIfExist

func DelFileIfExist(f string) error

func ExecCommand

func ExecCommand(cmd string) (int, error)

func FileExist

func FileExist(f string) bool

func GetIPStatus

func GetIPStatus(ipaddress string, prefix *uint8, dev *string) string

func GetInterfaceOfIP

func GetInterfaceOfIP(ipaddress string) (*string, *int, error)

func GetInterfaceStatus

func GetInterfaceStatus(dev *string) string

func GetProcStatus

func GetProcStatus(pidFile string) string

func IpInNetwork

func IpInNetwork(ipstr string, cidr string) bool

cidr contai

func ListContains

func ListContains(slice []string, obj string) bool

func ListDelete

func ListDelete(slice []string, obj string) (result []string)

func LogPrintf

func LogPrintf(level int, module string, format string, v ...interface{})

func MergeFiles

func MergeFiles(target string, files ...string) error

func MkdirIfNotExist

func MkdirIfNotExist(path string) error

func RandNum

func RandNum(max int) int

func RandomString

func RandomString(len int) string

func WriteFile

func WriteFile(target string, flag int, mode os.FileMode, data ...string) error

Types

type Gossip

type Gossip struct {
	Name                    string
	BindAddr                string
	BindPort                int
	SecretKey               []byte
	ProbeInterval           int
	SyncInterval            int
	RetransmitMult          int
	Ready                   bool
	NotifyJoinHandler       func(*memberlist.Node)
	NotifyLeaveHandler      func(*memberlist.Node)
	NotifyUpdateHandler     func(*memberlist.Node)
	InvalidatesHandler      func(memberlist.Broadcast) bool
	NodeMetaHandler         func() []byte
	NotifyMsgHandler        func([]byte)
	LocalStateHandler       func() []byte
	MergeRemoteStateHandler func([]byte)
	// contains filtered or unexported fields
}

func DefaultGossip

func DefaultGossip() *Gossip

Generage a default broadcast, need to set handlers:

  • NotifyMsgHandler
  • LocalStateHandler
  • MergeRemoteStateHandler

func GossipWith

func GossipWith(name string, bindAddr string, bindPort int) *Gossip

func (*Gossip) Broadcast

func (g *Gossip) Broadcast(msg []byte)

Broadcast message

func (*Gossip) Start

func (g *Gossip) Start(members *string) error

Start gossip

type Runnable

type Runnable interface {
	Run()
}

type Script

type Script struct {
	Spec   *ScriptSpec
	Cmd    *exec.Cmd
	Status string

	Runnable
	// contains filtered or unexported fields
}

func NewScript

func NewScript(s *ScriptSpec, callback func()) (*Script, error)

func (*Script) Expect

func (s *Script) Expect(regex string, timeout time.Duration) bool

func (*Script) Input

func (s *Script) Input(line string) error

func (*Script) Run

func (s *Script) Run()

func (*Script) Wait

func (s *Script) Wait()

type ScriptSpec

type ScriptSpec struct {
	Path        string
	Dir         string
	Args        []string
	Env         []string
	SysProcAttr *syscall.SysProcAttr // &syscall.SysProcAttr{Setpgid: true}
	Out         string
}

type ThreadPool

type ThreadPool struct {
	QueueSize int
	PoolSize  int
	Queue     chan Runnable
	// contains filtered or unexported fields
}

func DefaultThreadPool

func DefaultThreadPool() *ThreadPool

func NewThreadPool

func NewThreadPool(QueueSize int, PoolSize int) *ThreadPool

func (*ThreadPool) Destroy

func (p *ThreadPool) Destroy()

func (*ThreadPool) Init

func (p *ThreadPool) Init()

func (*ThreadPool) Put

func (p *ThreadPool) Put(r Runnable) error

func (*ThreadPool) Wait

func (p *ThreadPool) Wait()

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL