Torrent.go
package main
import (
"fmt"
"github.com/anacrolix/torrent"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
)
func getFileList(path string, prefix string) []string{
var files []string
err := filepath.Walk(path, func(path string, f fs.FileInfo, err error) error {
if f == nil{
return err
}
if f.IsDir() {
return nil
}
comma := strings.LastIndex(path,"_") +1
name := path[comma:]
if strings.HasPrefix(name, prefix) && !strings.HasSuffix(name, "@SynoEAStream"){
fmt.Println(name)
files = append(files, path)
}
return nil
})
if err != nil{
fmt.Printf("filepath.Walk() returned %v\n", err)
}
return files
}
func main() {
downPath := os.Getenv("DOWNLOAD_PATH")
if len(downPath) == 0 {
downPath = "D:\\download"
}
prefix := os.Getenv("PREFIX")
if len(prefix) == 0 {
prefix = "03"
}
torrentPath := os.Getenv("TORRENT_PATH")
if len(torrentPath) == 0 {
torrentPath = downPath + "/torrent"
}
log.Printf("prefix:%s,torrentPath:%s,downPath:%s\n", prefix, torrentPath, downPath)
_, err := os.Stat(downPath)
if err != nil && os.IsNotExist(err) {
os.MkdirAll(downPath, 0666)
}
files := getFileList(torrentPath, prefix)
config := torrent.NewDefaultClientConfig()
config.DataDir = downPath
c, _ := torrent.NewClient(config)
defer c.Close()
for _, path := range files{
fmt.Println(path)
t, _ := c.AddTorrentFromFile(path)
<-t.GotInfo()
t.DownloadAll()
c.WaitAll()
}
}
Dockfile
FROM golang:stretch
MAINTAINER panxi "7066450@qq.com"
ENV GO111MODULE=on \
GOPROXY=https://goproxy.cn,direct \
GIN_MODE=release
RUN apt install gcc
WORKDIR $GOPATH/src/torrent
ADD . ./
ARG GO_MAIN=Torrent.go
RUN go build -o torrent Torrent.go
ENTRYPOINT ["./torrent"]
K8S Job
apiVersion: batch/v1
kind: Job
metadata:
#{{}}模板變量括號(hào)中間不能有空格
name: torrent-{{prefix}}
namespace: dev
spec:
ttlSecondsAfterFinished: 10 #1.21版本以下需要啟用api-server feature-gates:TTLAfterFinished=true
template:
spec:
containers:
- name: torrent-{{prefix}}
image: harbor.lab/apps/torrent
volumeMounts:
- mountPath: "/storage"
name: storage
imagePullPolicy: Always
env:
- name: DOWNLOAD_PATH
value: "/storage/{{prefix}}"
- name: PREFIX
value: "{{prefix}}"
- name: TORRENT_PATH
value: "{{torrentPath}}"
imagePullSecrets:
- name: docker-secret
restartPolicy: Never
volumes:
- name: storage
persistentVolumeClaim:
claimName: docs-storageclass
Java調(diào)用k8s API發(fā)布job
@Override
public void createDocTorrentResolve(String prefix, String torrentPath) throws IOException {
List<V1Job> jobs = clusterService.listJob("job-name=torrent-" + prefix);
if(!ObjectUtils.isEmpty(jobs)){
for (V1Job job : jobs) {
V1JobStatus status = job.getStatus();
if(status != null
&& status.getSucceeded() != null
&& 1 == status.getSucceeded()){
V1ObjectMeta v1ObjectMeta = job.getMetadata();
if(v1ObjectMeta != null){
clusterService.deleteJob(v1ObjectMeta.getName());
}
}else{
throw new ArgumentException("存在未完成任務(wù),無(wú)法創(chuàng)建文獻(xiàn)種子文件下載任務(wù)!");
}
}
}
clusterService.applyYaml(
"torrent.yaml",
new HashedMap<String, String>() {
{
put("prefix", prefix);
put("torrentPath", torrentPath);
}
});
}