-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
157 lines (143 loc) · 4.58 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// transfer2go - Go implementation of loosely coupled, distributed agents for data transfer
//
// Author: Valentin Kuznetsov <[email protected]>
//
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"runtime"
"time"
log "github.com/sirupsen/logrus"
"github.com/vkuznet/transfer2go/client"
"github.com/vkuznet/transfer2go/server"
"github.com/vkuznet/transfer2go/utils"
)
func main() {
// server options
var agent string
flag.StringVar(&agent, "agent", "", "Registration agent URL [SERVER|CLIENT]")
var configFile string
flag.StringVar(&configFile, "config", "", "Agent configuration file [SERVER]")
var verbose int
flag.IntVar(&verbose, "verbose", 0, "Verbosity level [SERVER|CLENT]")
var version bool
flag.BoolVar(&version, "version", false, "Show version [SERVER|CLIENT]")
// client options
var src string
flag.StringVar(&src, "src", "", "Source end-point, either local file or AgentName:LFN [CLIENT]")
var dst string
flag.StringVar(&dst, "dst", "", "Destination end-point, either AgentName or AgentName:LFN [CLIENT]")
var action string
flag.StringVar(&action, "action", "", "Specify action JSON to process [CLIENT]")
var register string
flag.StringVar(®ister, "register", "", "File with meta-data of records in JSON data format to register at remote agent [CLIENT]")
var approve int64
flag.Int64Var(&approve, "approve", 0, "Approve given request id to initiate the transfer [CLIENT]")
// var model string
// flag.StringVar(&model, "model", "pull", "Transfer model: pull (data transfer through main agent), push (data transfer from src to dst directly) [CLIENT]")
var requests string
flag.StringVar(&requests, "requests", "", "Show given type of requests (pending, transfer) [CLIENT]")
flag.BoolVar(&utils.Auth, "auth", true, "To disable the auth layer [SERVER|CLIENT]")
flag.Usage = func() {
fmt.Println(fmt.Sprintf("Usage of %s", os.Args[0]))
fmt.Println("[SERVER] refers to server options")
fmt.Println("[CLIENT] refers to client options")
flag.PrintDefaults()
}
flag.Parse()
if version {
fmt.Println(info())
os.Exit(0)
}
if utils.Auth {
utils.CheckX509()
}
utils.VERBOSE = verbose
if configFile != "" {
data, err := ioutil.ReadFile(configFile)
if err != nil {
log.WithFields(log.Fields{
"configFile": configFile,
}).Fatal("Unable to read", err)
}
var config server.Config
err = json.Unmarshal(data, &config)
if err != nil {
log.WithFields(log.Fields{
"configFile": configFile,
}).Fatal("Unable to parse", err)
}
if config.Catalog == "" {
pwd, err := os.Getwd()
if err != nil {
log.Fatal("Unable to get current directory", err)
}
config.Catalog = pwd // use current directory as catalog
}
if config.Workers == 0 {
config.Workers = 10 // default value
}
if config.QueueSize == 0 {
config.QueueSize = 100 // default value
}
if config.Protocol == "" {
config.Protocol = "http" // default value
}
if config.Port == 0 {
config.Port = 8989
}
if agent != "" {
config.Register = agent
}
if config.Type == "" {
config.Type = "pull" // default value
}
if config.Register == "" {
log.Warn("WARNING this agent is not registered with remote ones, either provide register in your config or invoke register API call")
}
server.Init()
server.Server(config)
} else {
if register != "" { // register data in agent
client.Register(agent, register)
} else if action != "" { // perform action on main agent
client.ProcessAction(agent, action)
// core.AuthzDecorator(client.ProcessAction, "admin")(agent, action)
} else if requests != "" { // show requests from the agent
client.ShowRequests(agent, requests)
} else if src == "" { // no transfer request
client.Agent(agent)
} else {
client.RegisterRequest(agent, src, dst)
/*
if model == "pull" {
client.RegisterRequest(agent, src, dst)
// core.AuthzDecorator(client.RegisterRequest, "admin")(agent, src, dst)
} else if model == "push" {
client.Transfer(agent, src, dst)
// core.AuthzDecorator(client.Transfer, "cms")(agent, src, dst)
} else {
log.Fatal("Unknown transfer model")
}
*/
}
}
}
// helper function to return current version
func info() string {
goVersion := runtime.Version()
tstamp := time.Now()
return fmt.Sprintf("Build: git={{VERSION}} go=%s date=%s", goVersion, tstamp)
}
// helper function to construct site name
func makeSiteName() string {
host, err := os.Hostname()
if err != nil {
panic(fmt.Sprintf("Unable to get hostname, error=%v", err))
}
return fmt.Sprintf("T4_%s_%v", host, os.Getuid())
}