107 lines
2.8 KiB
Go
107 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"flag"
|
|
"io"
|
|
"io/ioutil"
|
|
"log"
|
|
"net"
|
|
"os"
|
|
"os/user"
|
|
"path/filepath"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
"google.golang.org/grpc"
|
|
"src.dualinventive.com/go/config"
|
|
"src.dualinventive.com/go/devsim"
|
|
"src.dualinventive.com/go/devsim/daemon"
|
|
"src.dualinventive.com/go/devsim/repository/git"
|
|
_ "src.dualinventive.com/go/devsim/simulator/js"
|
|
)
|
|
|
|
type cfg struct {
|
|
config.Config `yaml:",inline"`
|
|
|
|
GRPCListen string `yaml:"grpc_listen"`
|
|
HTTPListen string `yaml:"http_listen"`
|
|
BaseRepositoryDir string `yaml:"repository_dir"`
|
|
GitPrivateKey string `yaml:"git_private_key"`
|
|
GitSSHFingerprintsFile string `yaml:"git_ssh_fingerprints_file"`
|
|
SmpEndpoint string `yaml:"smp_endpoint"`
|
|
LoggerDir string `yaml:"logger_dir"`
|
|
}
|
|
|
|
var (
|
|
// ApplicationName is the name of the application
|
|
ApplicationName = "di-devsimd"
|
|
// ApplicationVersion is the version of the application
|
|
ApplicationVersion = "0.0.0-dev"
|
|
configuration = cfg{
|
|
GRPCListen: "0.0.0.0:3600",
|
|
HTTPListen: "0.0.0.0:3601",
|
|
BaseRepositoryDir: filepath.Join(os.TempDir(), "repositories"),
|
|
GitPrivateKey: "",
|
|
GitSSHFingerprintsFile: "",
|
|
SmpEndpoint: "dev02:4000",
|
|
LoggerDir: filepath.Join(os.TempDir(), "log"),
|
|
}
|
|
)
|
|
|
|
func readFingerprints(filename string) io.Reader {
|
|
if filename == "" {
|
|
return nil
|
|
}
|
|
// #nosec
|
|
fp, err := ioutil.ReadFile(filename)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return bytes.NewReader(fp)
|
|
}
|
|
|
|
func main() {
|
|
// Load the default id_rsa
|
|
if user, err := user.Current(); err != nil {
|
|
configuration.GitPrivateKey = filepath.Join(user.HomeDir, ".ssh", "id_rsa")
|
|
}
|
|
|
|
var confFile string
|
|
flag.StringVar(&confFile, "config", ApplicationName, "Configuration-file name")
|
|
flag.Parse()
|
|
err := config.Load(confFile, &configuration)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
logrus.Infof("Starting %s (version %s)", ApplicationName, ApplicationVersion)
|
|
|
|
fp := readFingerprints(configuration.GitSSHFingerprintsFile)
|
|
auth, err := git.Authorization("git", configuration.GitPrivateKey, fp)
|
|
if err != nil {
|
|
logrus.Fatalf("Cannot authenticate: %v", err)
|
|
}
|
|
|
|
repo := git.NewBucket(auth, configuration.BaseRepositoryDir)
|
|
d := daemon.New(&daemon.RAMStorage{}, repo, configuration.SmpEndpoint, configuration.LoggerDir)
|
|
|
|
go listenAndServeGRPCGateway(configuration.HTTPListen, configuration.GRPCListen)
|
|
|
|
err = serve(d, configuration.GRPCListen)
|
|
if err != nil {
|
|
log.Println("failed to serve grpc socket", err)
|
|
}
|
|
}
|
|
|
|
func serve(d *daemon.Daemon, listenURL string) error {
|
|
log.Println("listen on", listenURL)
|
|
lis, err := net.Listen("tcp", listenURL)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
g := grpc.NewServer()
|
|
devsim.RegisterRepositoryServiceServer(g, &repositoryServer{d: d})
|
|
devsim.RegisterSimulatorServiceServer(g, &simulatorServer{d: d})
|
|
return g.Serve(lis)
|
|
}
|