Commits · ampify

tav  ·  c3d683a4e823aee76906c1126197683854c0795d  ·  ampify  ·  github 1311930076

Restructured the nodule code into individual files.

Changes

src/amp/nodule/Makefile
......@@ -5,6 +5,10 @@ include $(GOROOT)/src/Make.inc
55
 
66
 TARG=amp/nodule
77
 GOFILES=\
8
+		app.go\
9
+		config.go\
10
+		host.go\
11
+		log.go\
812
         nodule.go\
913
 
1014
 include $(GOROOT)/src/Make.pkg
src/amp/nodule/app.go
......@@ -0,0 +1,5 @@
1
+// Public Domain (-) 2011 The Ampify Authors.
2
+// See the Ampify UNLICENSE file for details.
3
+
4
+package nodule
5
+
src/amp/nodule/config.go
......@@ -0,0 +1,73 @@
1
+// Public Domain (-) 2011 The Ampify Authors.
2
+// See the Ampify UNLICENSE file for details.
3
+
4
+package nodule
5
+
6
+import (
7
+	"amp/runtime"
8
+	"bytes"
9
+	"exp/template"
10
+	"os"
11
+	"strings"
12
+)
13
+
14
+type Config struct {
15
+	Type    string
16
+	Build   []string
17
+	Run     []string
18
+	Test    []string
19
+	Depends []string
20
+	Ignore  []string
21
+}
22
+
23
+type ConfigEnv struct {
24
+	Profile  string
25
+	Platform string
26
+	Darwin   bool
27
+	Linux    bool
28
+	FreeBSD  bool
29
+}
30
+
31
+var defaultEnv *ConfigEnv
32
+
33
+func GetConfigEnv() *ConfigEnv {
34
+	if defaultEnv != nil {
35
+		return defaultEnv
36
+	}
37
+	env := &ConfigEnv{
38
+		Profile:  runtime.Profile,
39
+		Platform: runtime.Platform,
40
+	}
41
+	switch runtime.Platform {
42
+	case "linux":
43
+		env.Linux = true
44
+	case "freebsd":
45
+		env.FreeBSD = true
46
+	case "darwin":
47
+		env.Darwin = true
48
+	}
49
+	defaultEnv = env
50
+	return env
51
+}
52
+
53
+func EvalStrings(name string, list []string, data interface{}) ([]string, os.Error) {
54
+	result := make([]string, len(list))
55
+	for idx, value := range list {
56
+		if strings.IndexRune(value, '{') == -1 {
57
+			result[idx] = value
58
+		} else {
59
+			tpl := template.New(name)
60
+			buf := &bytes.Buffer{}
61
+			err := tpl.Parse(value)
62
+			if err != nil {
63
+				return nil, err
64
+			}
65
+			err = tpl.Execute(buf, data)
66
+			if err != nil {
67
+				return nil, err
68
+			}
69
+			result[idx] = buf.String()
70
+		}
71
+	}
72
+	return result, nil
73
+}
src/amp/nodule/host.go
......@@ -0,0 +1,78 @@
1
+// Public Domain (-) 2011 The Ampify Authors.
2
+// See the Ampify UNLICENSE file for details.
3
+
4
+package nodule
5
+
6
+import (
7
+	"amp/logging"
8
+	"amp/master"
9
+	"fmt"
10
+	"net"
11
+	"os"
12
+	"path/filepath"
13
+)
14
+
15
+type Host struct {
16
+	CachePath    string
17
+	ControlTCP   net.Listener
18
+	ControlUnix  net.Listener
19
+	LastRef      uint64
20
+	Listener     net.Listener
21
+	Nodules      map[uint64]*Nodule
22
+	ReadTimeout  int64
23
+	WriteTimeout int64
24
+}
25
+
26
+func (host *Host) Run(debug bool) (err os.Error) {
27
+	logging.InfoData("node", "foo")
28
+	for {
29
+
30
+	}
31
+	return
32
+}
33
+
34
+func NewHost(runPath, hostAddress string, hostPort int, ctrlAddress string, ctrlPort int, initNodules string, nodulePaths []string, master *master.Client) (host *Host, err os.Error) {
35
+
36
+	// Create the cache directory if it doesn't exist.
37
+	cachePath := filepath.Join(runPath, "cache")
38
+	err = os.MkdirAll(cachePath, 0755)
39
+	if err != nil {
40
+		return
41
+	}
42
+
43
+	ctrlTCP, err := net.Listen("tcp", fmt.Sprintf("%s:%d", ctrlAddress, ctrlPort))
44
+	if err != nil {
45
+		return
46
+	}
47
+
48
+	socket := filepath.Join(runPath, "node.sock")
49
+	_, err = os.Stat(socket)
50
+	if err == nil {
51
+		err = os.Remove(socket)
52
+		if err != nil {
53
+			return
54
+		}
55
+	}
56
+
57
+	ctrlUnix, err := net.Listen("unix", socket)
58
+	if err != nil {
59
+		return
60
+	}
61
+
62
+	listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", hostAddress, hostPort))
63
+	if err != nil {
64
+		return
65
+	}
66
+
67
+	host = &Host{
68
+		CachePath:    cachePath,
69
+		ControlTCP:   ctrlTCP,
70
+		ControlUnix:  ctrlUnix,
71
+		Listener:     listener,
72
+		ReadTimeout:  60 * 1e9,
73
+		WriteTimeout: 60 * 1e9,
74
+	}
75
+
76
+	return
77
+
78
+}
src/amp/nodule/log.go
......@@ -0,0 +1,30 @@
1
+// Public Domain (-) 2011 The Ampify Authors.
2
+// See the Ampify UNLICENSE file for details.
3
+
4
+package nodule
5
+
6
+import (
7
+	"amp/logging"
8
+	"fmt"
9
+)
10
+
11
+func Log(msg string, args ...interface{}) {
12
+	if len(args) > 0 {
13
+		logging.InfoData("node", fmt.Sprintf(msg, args...))
14
+	} else {
15
+		logging.InfoData("node", msg)
16
+	}
17
+}
18
+
19
+func FilterConsoleLog(record *logging.Record) (write bool, data []interface{}) {
20
+	if len(record.Items) > 0 {
21
+		meta := record.Items[0]
22
+		switch meta.(type) {
23
+		case string:
24
+			if meta.(string) == "node" {
25
+				return true, record.Items[1:]
26
+			}
27
+		}
28
+	}
29
+	return true, nil
30
+}
src/amp/nodule/nodule.go
......@@ -5,143 +5,13 @@ package nodule
55
 
66
 import (
77
 	"amp/logging"
8
-	"amp/master"
9
-	"amp/runtime"
108
 	"amp/yaml"
11
-	"bytes"
129
 	"exec"
13
-	"exp/template"
1410
 	"fmt"
15
-	"net"
1611
 	"os"
1712
 	"path/filepath"
18
-	"strings"
1913
 )
2014
 
21
-// Find all nodules at or within the immediate subdirectories of the given
22
-// ``directory``.
23
-func Find(directory string) (nodules []*Nodule, err os.Error) {
24
-
25
-	directory, err = filepath.Abs(filepath.Clean(directory))
26
-	if err != nil {
27
-		return
28
-	}
29
-
30
-	root, err := os.Open(directory)
31
-	if err != nil {
32
-		return
33
-	}
34
-
35
-	defer root.Close()
36
-
37
-	stat, err := root.Stat()
38
-	if err != nil {
39
-		return
40
-	}
41
-
42
-	if !stat.IsDirectory() {
43
-		return nil, fmt.Errorf("No directory found at %q.", directory)
44
-	}
45
-
46
-	file := filepath.Join(directory, "nodule.yaml")
47
-
48
-	fileobj, err := os.Open(file)
49
-	if err == nil {
50
-		fileobj.Close()
51
-		return []*Nodule{&Nodule{Name: stat.Name, Path: directory}}, nil
52
-	}
53
-
54
-	nodules = make([]*Nodule, 0)
55
-
56
-	for {
57
-		items, err := root.Readdirnames(100)
58
-		if len(items) == 0 {
59
-			if err != nil && err != os.EOF {
60
-				return nil, err
61
-			}
62
-			break
63
-		}
64
-		for _, name := range items {
65
-			path := filepath.Join(directory, name)
66
-			stat, err := os.Stat(path)
67
-			if err != nil {
68
-				return nil, err
69
-			}
70
-			if stat.IsDirectory() {
71
-				file := filepath.Join(path, "nodule.yaml")
72
-				fileobj, err = os.Open(file)
73
-				if err == nil {
74
-					fileobj.Close()
75
-					nodules = append(nodules, &Nodule{Name: name, Path: path})
76
-				}
77
-			}
78
-		}
79
-	}
80
-
81
-	return nodules, nil
82
-
83
-}
84
-
85
-type ConfigEnv struct {
86
-	Profile  string
87
-	Platform string
88
-	Darwin   bool
89
-	Linux    bool
90
-	FreeBSD  bool
91
-}
92
-
93
-var defaultEnv *ConfigEnv
94
-
95
-func GetConfigEnv() *ConfigEnv {
96
-	if defaultEnv != nil {
97
-		return defaultEnv
98
-	}
99
-	env := &ConfigEnv{
100
-		Profile:  runtime.Profile,
101
-		Platform: runtime.Platform,
102
-	}
103
-	switch runtime.Platform {
104
-	case "linux":
105
-		env.Linux = true
106
-	case "freebsd":
107
-		env.FreeBSD = true
108
-	case "darwin":
109
-		env.Darwin = true
110
-	}
111
-	defaultEnv = env
112
-	return env
113
-}
114
-
115
-func EvalStrings(name string, list []string, data interface{}) ([]string, os.Error) {
116
-	result := make([]string, len(list))
117
-	for idx, value := range list {
118
-		if strings.IndexRune(value, '{') == -1 {
119
-			result[idx] = value
120
-		} else {
121
-			tpl := template.New(name)
122
-			buf := &bytes.Buffer{}
123
-			err := tpl.Parse(value)
124
-			if err != nil {
125
-				return nil, err
126
-			}
127
-			err = tpl.Execute(buf, data)
128
-			if err != nil {
129
-				return nil, err
130
-			}
131
-			result[idx] = buf.String()
132
-		}
133
-	}
134
-	return result, nil
135
-}
136
-
137
-type Config struct {
138
-	Type    string
139
-	Build   []string
140
-	Run     []string
141
-	Test    []string
142
-	Depends []string
143
-}
144
-
14515
 type Nodule struct {
14616
 	Name string
14717
 	Path string
......@@ -156,8 +26,7 @@ func (nodule *Nodule) Error(process string, error os.Error) os.Error {
15626
 }
15727
 
15828
 func (nodule *Nodule) ConfigError(msg string, v ...interface{}) os.Error {
159
-	err := fmt.Errorf(msg, v...)
160
-	return nodule.Error("loading config for", err)
29
+	return nodule.Error("loading config for", fmt.Errorf(msg, v...))
16130
 }
16231
 
16332
 func (nodule *Nodule) LoadConf() (err os.Error) {
......@@ -306,87 +175,66 @@ func (nodule *Nodule) Run() (err os.Error) {
306175
 	return nodule.handleCommon("running", nodule.Conf.Run)
307176
 }
308177
 
309
-type Host struct {
310
-	CachePath    string
311
-	ControlTCP   net.Listener
312
-	ControlUnix  net.Listener
313
-	LastRef      uint64
314
-	Listener     net.Listener
315
-	Nodules      map[uint64]*Nodule
316
-	ReadTimeout  int64
317
-	WriteTimeout int64
318
-}
319
-
320
-func (host *Host) Run(debug bool) (err os.Error) {
321
-	for {
322
-
323
-	}
324
-	return
325
-}
326
-
327
-func NewHost(runPath, hostAddress string, hostPort int, ctrlAddress string, ctrlPort int, initNodules string, nodulePaths []string, master *master.Client) (host *Host, err os.Error) {
178
+// Find all nodules at or within the immediate subdirectories of the given
179
+// ``directory``.
180
+func Find(directory string) (nodules []*Nodule, err os.Error) {
328181
 
329
-	// Create the cache directory if it doesn't exist.
330
-	cachePath := filepath.Join(runPath, "cache")
331
-	err = os.MkdirAll(cachePath, 0755)
182
+	directory, err = filepath.Abs(filepath.Clean(directory))
332183
 	if err != nil {
333184
 		return
334185
 	}
335186
 
336
-	ctrlTCP, err := net.Listen("tcp", fmt.Sprintf("%s:%d", ctrlAddress, ctrlPort))
187
+	root, err := os.Open(directory)
337188
 	if err != nil {
338189
 		return
339190
 	}
340191
 
341
-	socket := filepath.Join(runPath, "node.sock")
342
-	_, err = os.Stat(socket)
343
-	if err == nil {
344
-		err = os.Remove(socket)
345
-		if err != nil {
346
-			return
347
-		}
348
-	}
349
-
350
-	ctrlUnix, err := net.Listen("unix", socket)
351
-	if err != nil {
352
-		return
353
-	}
192
+	defer root.Close()
354193
 
355
-	listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", hostAddress, hostPort))
194
+	stat, err := root.Stat()
356195
 	if err != nil {
357196
 		return
358197
 	}
359198
 
360
-	host = &Host{
361
-		CachePath:    cachePath,
362
-		ControlTCP:   ctrlTCP,
363
-		ControlUnix:  ctrlUnix,
364
-		Listener:     listener,
365
-		ReadTimeout:  60 * 1e9,
366
-		WriteTimeout: 60 * 1e9,
199
+	if !stat.IsDirectory() {
200
+		return nil, fmt.Errorf("No directory found at %q.", directory)
367201
 	}
368202
 
369
-	return
370
-
371
-}
203
+	file := filepath.Join(directory, "nodule.yaml")
372204
 
373
-func Log(msg string, args ...interface{}) {
374
-	if len(args) > 0 {
375
-		logging.InfoData("node", fmt.Sprintf(msg, args...))
376
-	} else {
377
-		logging.InfoData("node", msg)
205
+	fileobj, err := os.Open(file)
206
+	if err == nil {
207
+		fileobj.Close()
208
+		return []*Nodule{&Nodule{Name: stat.Name, Path: directory}}, nil
378209
 	}
379
-}
380210
 
381
-func FilterConsoleLog(record *logging.Record) (write bool, data []interface{}) {
382
-	if len(record.Items) > 0 {
383
-		meta := record.Items[0]
384
-		switch meta.(type) {
385
-		case string:
386
-			if meta.(string) == "node" {
387
-				return true, record.Items[1:]
211
+	nodules = make([]*Nodule, 0)
212
+
213
+	for {
214
+		items, err := root.Readdirnames(100)
215
+		if len(items) == 0 {
216
+			if err != nil && err != os.EOF {
217
+				return nil, err
218
+			}
219
+			break
220
+		}
221
+		for _, name := range items {
222
+			path := filepath.Join(directory, name)
223
+			stat, err := os.Stat(path)
224
+			if err != nil {
225
+				return nil, err
226
+			}
227
+			if stat.IsDirectory() {
228
+				file := filepath.Join(path, "nodule.yaml")
229
+				fileobj, err = os.Open(file)
230
+				if err == nil {
231
+					fileobj.Close()
232
+					nodules = append(nodules, &Nodule{Name: name, Path: path})
233
+				}
388234
 			}
389235
 		}
390236
 	}
391
-	return true, nil
237
+
238
+	return nodules, nil
239
+
392240
 }