summaryrefslogtreecommitdiff
path: root/cmd/weles-server/main.go
blob: afc534369f1eb203da74dccbcc5f2ef12e652d3c (plain)
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
// Copyright (c) 2017-2018 Samsung Electronics Co., Ltd All Rights Reserved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
//

package main

import (
	"fmt"
	"log"
	"os"
	"time"

	loads "github.com/go-openapi/loads"
	flag "github.com/spf13/pflag"

	"github.com/SamsungSLAV/boruta/http/client"
	"github.com/SamsungSLAV/weles"
	"github.com/SamsungSLAV/weles/artifacts"
	"github.com/SamsungSLAV/weles/controller"
	"github.com/SamsungSLAV/weles/manager"
	"github.com/SamsungSLAV/weles/parser"
	"github.com/SamsungSLAV/weles/server"
	"github.com/SamsungSLAV/weles/server/operations"
)

var (
	borutaAddress            string
	borutaRefreshPeriod      time.Duration
	artifactDBName           string
	artifactDBLocation       string
	artifactDownloadQueueCap int
	activeWorkersCap         int
	notifierChannelCap       int
	version                  bool
)

// This file was generated by the swagger tool.
// Make sure to regenerate server only with the Makefile which has --exclude-main option set.

func exitOnErr(ctx string, err error) {
	if err != nil {
		log.Fatal(ctx, err)
	}
}

func main() {

	swaggerSpec, err := loads.Embedded(server.SwaggerJSON, server.FlatSwaggerJSON)
	exitOnErr("failed to load embedded swagger spec", err)

	var srv *server.Server // make sure init is called
	var apiDefaults server.APIDefaults

	flag.Int32Var(&apiDefaults.PageLimit, "page-limit", 0, "Default limit of page size returned "+
		"by Weles API. If set to 0 pagination will be turned off")

	flag.StringVar(&borutaAddress, "boruta-address", "http://127.0.0.1:8487",
		"Boruta address. Must contain protocol.")

	flag.DurationVar(&borutaRefreshPeriod, "boruta-refresh-period", 2*time.Second,
		"Boruta refresh period")

	flag.StringVar(&artifactDBName, "db-file", "weles.db",
		"name of *.db file. Should be located in --db-location")

	flag.StringVar(&artifactDBLocation, "db-location", "/tmp/weles/",
		"location of *.db file and place where Weles will store artifacts.")

	//TODO: when cyberdryads or testlab instance will be present, performance tests should be done
	// to set default values of below:
	flag.IntVar(&artifactDownloadQueueCap, "artifact-download-queue-cap", 100,
		"Capacity of artifact download queue")

	flag.IntVar(&activeWorkersCap, "active-workers-cap", 16, "Maximum number of active workers.")

	flag.IntVar(&notifierChannelCap, "notifier-channel-cap", 100, "Notifier channel capacity.")

	flag.BoolVar(&version, "version", false, "Print Weles server version and exit.")

	//TODO: input validation

	flag.Usage = func() {
		fmt.Fprint(os.Stderr, "Usage:\n")
		fmt.Fprint(os.Stderr, "  weles-server [OPTIONS]\n\n")

		title := "Weles"
		fmt.Fprint(os.Stderr, title+"\n\n")
		desc := "This is a Weles server. " +
			"You can find out more about Weles at [http://tbd.tbd](http://tbd.tbd)."
		if desc != "" {
			fmt.Fprintf(os.Stderr, desc+"\n\n")
		}
		fmt.Fprintln(os.Stderr, flag.CommandLine.FlagUsages())
	}
	// parse the CLI flags
	flag.Parse()

	if version {
		fmt.Println("weles version", weles.SrvVersion)
		os.Exit(0)
	}

	var yap parser.Parser
	am, err := artifacts.NewArtifactManager(
		artifactDBName,
		artifactDBLocation,
		notifierChannelCap,
		activeWorkersCap,
		artifactDownloadQueueCap)
	exitOnErr("failed to initialize ArtifactManager ", err)
	bor := client.NewBorutaClient(borutaAddress)
	djm := manager.NewDryadJobManager(artifactDBLocation)
	jm := controller.NewJobManager(am, &yap, bor, borutaRefreshPeriod, djm)

	api := operations.NewWelesAPI(swaggerSpec)
	// get server with flag values filled out
	srv = server.NewServer(api)

	defer func() {
		if err = srv.Shutdown(); err != nil {
			log.Println("Failed to shut down server: " + err.Error())
		}
	}()

	apiDefaults.Managers = server.NewManagers(jm, am)

	srv.WelesConfigureAPI(&apiDefaults)
	err = srv.Serve()
	exitOnErr("failed to serve the API", err)
}