Skip to content

Commit 1ae59ee

Browse files
author
olevole
committed
re-import
1 parent a3444b6 commit 1ae59ee

10 files changed

+1287
-2
lines changed

.github/FUNDING.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# These are supported funding model platforms
2+
3+
patreon: clonos
4+
custom: https://www.bsdstore.ru/en/donate.html

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.idea/
2+
src
3+
./cbsd-mq-api
4+
cbsd-mq-api
5+

README.md

+95-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,95 @@
1-
# cbsd-mq-api
2-
Simplified API for creating and destroying CBSD virtual environments
1+
# CBSD RESTFull API
2+
3+
Copyright (c) 2013-2021, The CBSD Development Team
4+
5+
Homepage: https://bsdstore.ru
6+
7+
## Description
8+
9+
Provides a simplified API for creating and destroying CBSD virtual environments.
10+
11+
#### Table of Contents
12+
13+
1. [Project Description - What does the project do?](#project-description)
14+
2. [Usage - Configuration options and additional functionality](#usage)
15+
3. [Contributing - Contribute to the project](#contributing)
16+
4. [Support - Mailing List, Talks, Contacts](#support)
17+
18+
## Usage
19+
20+
21+
Init:
22+
23+
set GOPATH
24+
25+
go get
26+
go run ./cbsd-mq-api [ -l listen]
27+
28+
29+
# Install
30+
31+
mkdir -p /var/db/cbsd-api /usr/jails/var/db/api/map
32+
chown -R cbsd:cbsd /var/db/cbsd-api /usr/jails/var/db/api/map
33+
34+
Install api.d module + enable in modules.conf, cbsd initenv.
35+
36+
Setup api.d module: make sure
37+
38+
"recomendation": "/usr/local/cbsd/modules/api.d/misc/recomendation.sh",
39+
"freejname": "/usr/local/cbsd/modules/api.d/misc/freejname.sh",
40+
41+
script works (from cbsd user): chown cbsd:cbsd ~cbsd/etc/api.conf
42+
43+
# On host
44+
45+
1) pkg install -y sysutils/cbsd-mq-router
46+
47+
2) setup cbsd-mq-router.json, e.g:
48+
49+
```
50+
{
51+
"cbsdenv": "/usr/jails",
52+
"cbsdcolor": false,
53+
"broker": "beanstalkd",
54+
"logfile": "/dev/stdout",
55+
"beanstalkd": {
56+
"uri": "127.0.0.1:11300",
57+
"tube": "cbsd_host1_example_com",
58+
"reply_tube_prefix": "cbsd_host1_example_com_result_id",
59+
"reconnect_timeout": 5,
60+
"reserve_timeout": 5,
61+
"publish_timeout": 5,
62+
"logdir": "/var/log/cloudmq"
63+
}
64+
}
65+
```
66+
67+
3) service cbsd-mq-router enable
68+
4) service cbsd-mq-router start
69+
70+
## Usage
71+
72+
Valid endpoints:
73+
74+
```
75+
curl -H "cid:<cid>" http://127.0.0.1:65531/api/v1/cluster
76+
curl -H "cid:<cid>" http://127.0.0.1:65531/api/v1/status/<env>
77+
curl -H "cid:<cid>" http://127.0.0.1:65531/api/v1/start/<env>
78+
curl -H "cid:<cid>" http://127.0.0.1:65531/api/v1/stop/<env>
79+
curl -H "cid:<cid>" http://127.0.0.1:65531/api/v1/destroy/<env>
80+
```
81+
82+
## Contributing
83+
84+
* Fork me on GitHub: [https://github.com/cbsd/cbsd-mq-api.git](https://github.com/cbsd/cbsd-mq-api.git)
85+
* Switch to 'develop' branch
86+
* Commit your changes (`git commit -am 'Added some feature'`)
87+
* Push to the branch (`git push`)
88+
* Create new Pull Request
89+
90+
## Support
91+
92+
* For CBSD-related support, discussion and talks, please join to Telegram CBSD usergroup channel: @cbsdofficial
93+
* Web link: https://t.me/cbsdofficial
94+
* Or subscribe to mailing list by sending email to: [email protected]
95+
* Other contact: https://www.bsdstore.ru/en/feedback.html

beanstalk.go

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"log"
7+
"strings"
8+
"time"
9+
10+
"github.com/beanstalkd/go-beanstalk"
11+
)
12+
13+
// beanstalk config struct
14+
type BeanstalkConfig struct {
15+
Uri string `json:"uri"`
16+
Tube string `json:"tube"`
17+
ReplyTubePrefix string `json:"reply_tube_prefix"`
18+
ReconnectTimeout int `json:"reconnect_timeout"`
19+
ReserveTimeout int `json:"reserve_timeout"`
20+
PublishTimeout int `json:"publish_timeout"`
21+
}
22+
23+
func beanstalkSend(config BeanstalkConfig, body string) (string, error) {
24+
25+
amqpURI := config.Uri
26+
tube := config.Tube
27+
28+
fmt.Printf("Calling beanstalkd: %s\n", amqpURI)
29+
fmt.Printf("Tube selected: %s\n", tube)
30+
fmt.Printf("Reply Tube prefix: %s\n", config.ReplyTubePrefix)
31+
32+
c, err := beanstalk.Dial("tcp", amqpURI)
33+
34+
if err != nil {
35+
log.Printf("Unable connect to beanstalkd broker:%s", err)
36+
return "", err
37+
}
38+
39+
mytube := &beanstalk.Tube{Conn: c, Name: tube}
40+
id, err := mytube.Put([]byte(body), 1, 0, time.Duration(config.PublishTimeout)*time.Second)
41+
42+
if err != nil {
43+
fmt.Printf("\nerr: %d\n", err)
44+
return "", err
45+
}
46+
47+
callbackQueueName := fmt.Sprintf("%s%d", config.ReplyTubePrefix, id)
48+
fmt.Printf("got id: %d,callback queue name: %s\n", id, callbackQueueName)
49+
50+
c1 := make(chan string)
51+
52+
go func() {
53+
54+
// todo: global timeout
55+
for {
56+
c.TubeSet = *beanstalk.NewTubeSet(c, callbackQueueName)
57+
id, body, err := c.Reserve(time.Duration(config.ReserveTimeout) * time.Second)
58+
59+
if err != nil {
60+
fmt.Printf("\nid: %d, res: %s\n", id, err.Error())
61+
}
62+
63+
if id == 0 {
64+
return // timeout
65+
// continue
66+
}
67+
68+
cbsdTask := CbsdTask{}
69+
err = json.Unmarshal(body, &cbsdTask)
70+
if err != nil {
71+
log.Printf("json decode error %s", err.Error())
72+
c.Delete(id)
73+
return
74+
}
75+
76+
if cbsdTask.Progress == 100 {
77+
c1 <- cbsdTask.Message
78+
}
79+
c.Delete(id)
80+
}
81+
}()
82+
83+
select {
84+
case msg1 := <-c1:
85+
if strings.Compare(msg1, "EOF") == 0 {
86+
fmt.Printf("EXIT\n")
87+
c.Close()
88+
return "", err
89+
} else {
90+
fmt.Println("received:", msg1)
91+
fmt.Printf("EXIT\n")
92+
c.Close()
93+
return msg1, err
94+
}
95+
}
96+
97+
c.Close()
98+
return "", err
99+
}

build.sh

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/sh
2+
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
3+
pgm="${0##*/}" # Program basename
4+
progdir="${0%/*}" # Program directory
5+
workdir=$( realpath ${progdir} ) # realpath dir
6+
cd ${workdir}
7+
8+
# Check go install
9+
if [ -z "$( which go )" ]; then
10+
echo "error: Go is not installed. Please install go: pkg install -y lang/go"
11+
exit 1
12+
fi
13+
14+
# Check go version
15+
GOVERS="$( go version | cut -d " " -f 3 )"
16+
if [ -z "${GOVERS}" ]; then
17+
echo "unable to determine: go version"
18+
exit 1
19+
fi
20+
21+
export GOPATH="${workdir}"
22+
export GOBIN="${workdir}"
23+
export GO111MODULE=off
24+
25+
set -e
26+
go get
27+
go build -ldflags "${LDFLAGS} -extldflags '-static'" -o "${workdir}/cbsd-mq-api"

cbsd-mq-api.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"cbsdenv": "/usr/jails",
3+
"cbsdcolor": false,
4+
"broker": "beanstalkd",
5+
"logfile": "/dev/stdout",
6+
"recomendation": "/usr/local/cbsd/modules/api.d/misc/recomendation.sh",
7+
"freejname": "/usr/local/cbsd/modules/api.d/misc/freejname.sh",
8+
"server_url": "https://127.0.0.1",
9+
"beanstalkd": {
10+
"uri": "127.0.0.1:11300",
11+
"tube": "cbsd_zpool1",
12+
"reply_tube_prefix": "cbsd_zpool1_result_id",
13+
"reconnect_timeout": 5,
14+
"reserve_timeout": 5,
15+
"publish_timeout": 5,
16+
"logdir": "/var/log/cbsdmq"
17+
}
18+
}
19+

common.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import "time"
4+
5+
type Comment struct {
6+
Command string
7+
JobID uint64
8+
Date time.Time
9+
CommandArgs map[string]string
10+
}
11+
12+
type CommentProtocol interface {
13+
Decode(encodedComment []byte) (*Comment, error)
14+
Encode(comment *Comment) ([]byte, error)
15+
}
16+
17+
type CommentProcessor interface {
18+
DoProcess(comment *Comment) error
19+
}
20+
21+
type CbsdTask struct {
22+
Progress int
23+
ErrCode int
24+
Message string
25+
}

config.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
)
8+
9+
type Config struct {
10+
ServerUrl string `json:"server_url"`
11+
CbsdEnv string `json:"cbsdenv"`
12+
Broker string `json:"broker"`
13+
ImageList string `json:"imagelist"`
14+
Recomendation string `json:"recomendation"`
15+
Freejname string `json:"freejname"`
16+
BeanstalkConfig `json:"beanstalkd"`
17+
}
18+
19+
func LoadConfiguration(file string) (Config, error) {
20+
var config Config
21+
configFile, err := os.Open(file)
22+
defer configFile.Close()
23+
24+
if err != nil {
25+
fmt.Println(err.Error())
26+
return config, err
27+
}
28+
29+
jsonParser := json.NewDecoder(configFile)
30+
err = jsonParser.Decode(&config)
31+
32+
if err != nil {
33+
fmt.Printf("config error: %s: %s\n", file, err.Error())
34+
return config, err
35+
}
36+
37+
fmt.Printf("Using config file: %s\n", file)
38+
return config, err
39+
}

etc/cbsd-mq-api.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"cbsdenv": "/usr/jails",
3+
"cbsdcolor": false,
4+
"broker": "beanstalkd",
5+
"logfile": "/dev/stdout",
6+
"recomendation": "/usr/local/cbsd/modules/api.d/misc/recomendation.sh",
7+
"freejname": "/usr/local/cbsd/modules/api.d/misc/freejname.sh",
8+
"server_url": "http://127.0.0.1:65531",
9+
"beanstalkd": {
10+
"uri": "127.0.0.1:11300",
11+
"tube": "cbsd_zpool1",
12+
"reply_tube_prefix": "cbsd_zpool1_result_id",
13+
"reconnect_timeout": 5,
14+
"reserve_timeout": 5,
15+
"publish_timeout": 5,
16+
"logdir": "/var/log/cbsdmq"
17+
}
18+
}

0 commit comments

Comments
 (0)