summaryrefslogtreecommitdiff
path: root/types.go
blob: 2663c07fa74e8d9e9801e28e904aa82799fde2c4 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main

import (
	"bytes"
	"fmt"
	"golang.org/x/crypto/openpgp"
)

// error constants
const (
	SCORSH_ERR_NO_FILE = -(1 << iota)
	SCORSH_ERR_KEYRING
	SCORSH_ERR_NO_REPO
	SCORSH_ERR_NO_COMMIT
	SCORSH_ERR_SIGNATURE
)

// SCORSHmsg type represents messages received from the spool and
// sent to workers
type SCORSHmsg struct {
	ID     string `yaml:"m_id"`
	Repo   string `yaml:"m_repo"`
	Branch string `yaml:"m_branch"`
	OldRev string `yaml:"m_oldrev"`
	NewRev string `yaml:"m_newrev"`
	Path   string
}

// SCORSHcmd represents commands configured on the server side
type SCORSHcmd struct {
	URL  string `yaml:"c_url"`
	Hash string `yaml:"c_hash"`
}

// SCORSHtagCfg represents tags configured on the server side
type SCORSHtagCfg struct {
	Name     string      `yaml:"t_name"`
	Keyrings []string    `yaml:"t_keyrings"`
	Commands []SCORSHcmd `yaml:"t_commands"`
}

// SCORSHworkerCfg represents the static configuration of a worker
type SCORSHworkerCfg struct {
	Name    string   `yaml:"w_name"`
	Repos   []string `yaml:"w_repos"`
	Folder  string   `yaml:"w_folder"`
	Logfile string   `yaml:"w_logfile"`
	Tagfile string   `yaml:"w_tagfile"`
	//	Keyrings []string        `yaml:"w_keyrings"`
	Tags    []SCORSHtagCfg `yaml:"w_tags"`
	TagKeys map[string]map[string]bool
}

// SCORSHworkerState represents the runtime state of a worker
type SCORSHworkerState struct {
	Keys       map[string]openpgp.KeyRing
	MsgChan    chan SCORSHmsg
	StatusChan chan SCORSHmsg
}

// SCORSHworker represents the configuration and state of a worker
type SCORSHworker struct {
	SCORSHworkerCfg `yaml:",inline"`
	SCORSHworkerState
}

// SCORSHmasterCfg represents the static configuration of the master
type SCORSHmasterCfg struct {
	Spooldir  string         `yaml:"s_spooldir"`
	Logfile   string         `yaml:"s_logfile"`
	LogPrefix string         `yaml:"s_logprefix"`
	Workers   []SCORSHworker `yaml:"s_workers"`
}

// SCORSHmasterState represents the runtime state of the master
type SCORSHmasterState struct {
	Spooler    chan SCORSHmsg
	StatusChan chan SCORSHmsg
	Repos      map[string][]*SCORSHworker
	WorkingMsg map[string]int
}

// SCORSHmaster represents the configuration and state of the master
type SCORSHmaster struct {
	SCORSHmasterCfg `yaml:",inline"`
	SCORSHmasterState
}

// SCORSHtag is the type of commands sent by clients
type SCORSHtag struct {
	Tag  string   `yaml:"s_tag"`
	Args []string `yaml:"s_args"`
}

// SCORSHclientMsg is the list of commands sent by a client
type SCORSHclientMsg struct {
	Tags []SCORSHtag `yaml:"scorsh"`
}

////////////////////////

func (cfg *SCORSHmaster) String() string {

	var buff bytes.Buffer

	fmt.Fprintf(&buff, "spooldir: %s\n", cfg.Spooldir)
	fmt.Fprintf(&buff, "logfile: %s\n", cfg.Logfile)
	fmt.Fprintf(&buff, "logprefix: %s\n", cfg.LogPrefix)
	fmt.Fprintf(&buff, "Workers: \n")

	for _, w := range cfg.Workers {
		fmt.Fprintf(&buff, "%s", &w)
	}

	return buff.String()
}

func (msg *SCORSHmsg) String() string {

	var buff bytes.Buffer
	fmt.Fprintf(&buff, "Id: %s\n", msg.ID)
	fmt.Fprintf(&buff, "Repo: %s\n", msg.Repo)
	fmt.Fprintf(&buff, "Branch: %s\n", msg.Branch)
	fmt.Fprintf(&buff, "OldRev: %s\n", msg.OldRev)
	fmt.Fprintf(&buff, "Newrev: %s\n", msg.NewRev)
	fmt.Fprintf(&buff, "Path: %s\n", msg.Path)

	return buff.String()

}

func (w *SCORSHworker) String() string {

	var buff bytes.Buffer
	fmt.Fprintf(&buff, "Name: %s\n", w.Name)
	fmt.Fprintf(&buff, "Repos: %s\n", w.Repos)
	fmt.Fprintf(&buff, "Folder: %s\n", w.Folder)
	fmt.Fprintf(&buff, "Logfile: %s\n", w.Logfile)
	fmt.Fprintf(&buff, "Tagfile: %s\n", w.Tagfile)
	//	fmt.Fprintf(&buff, "Keyrings: %s\n", w.Keyrings)

	return buff.String()
}

func (msg *SCORSHclientMsg) String() string {

	var buff bytes.Buffer

	for _, t := range msg.Tags {

		fmt.Fprintf(&buff, "s_tag: %s\n", t.Tag)
		for _, a := range t.Args {
			fmt.Fprintf(&buff, "  s_args: %s\n", a)
		}
	}

	return buff.String()

}