src/amp/amp/node.go |
| ... | ... | @@ -0,0 +1,129 @@ |
| 1 | +// Public Domain (-) 2011 The Ampify Authors. |
| 2 | +// See the Ampify UNLICENSE file for details. |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "amp/nodule" |
| 8 | + "amp/optparse" |
| 9 | + "amp/repo" |
| 10 | + "amp/runtime" |
| 11 | + "os" |
| 12 | + "strings" |
| 13 | +) |
| 14 | + |
| 15 | +func ampNode(argv []string, usage string) { |
| 16 | + |
| 17 | + opts := optparse.Parser( |
| 18 | + "Usage: amp node <config.yaml> [options]\n\n " + usage + "\n") |
| 19 | + |
| 20 | + nodeHost := opts.StringConfig("node-host", "", |
| 21 | + "the host to bind this node to") |
| 22 | + |
| 23 | + nodePort := opts.IntConfig("node-port", 8050, |
| 24 | + "the port to bind this node to [8050]") |
| 25 | + |
| 26 | + ctrlHost := opts.StringConfig("control-host", "", |
| 27 | + "the host to bind the nodule control socket to") |
| 28 | + |
| 29 | + ctrlPort := opts.IntConfig("control-port", 8051, |
| 30 | + "the port to bind the nodule control socket to [8051]") |
| 31 | + |
| 32 | + nodules := opts.StringConfig("nodules", "*", |
| 33 | + "comma-separated list of nodules to initialise [*]") |
| 34 | + |
| 35 | + nodulePaths := opts.StringConfig("nodule-paths", ".", |
| 36 | + "comma-separated list of nodule container directories [nodule]") |
| 37 | + |
| 38 | + repoNodes := opts.StringConfig("repo-nodes", "localhost:8060", |
| 39 | + "comma-separated addresses of amp repo nodes [localhost:8060]") |
| 40 | + |
| 41 | + repoKeyPath := opts.StringConfig("repo-key", "cert/repo.key", |
| 42 | + "the path to the file containing the amp repo public key [cert/repo.key]") |
| 43 | + |
| 44 | + debug, _, runPath := runtime.DefaultOpts("node", opts, argv, nil) |
| 45 | + |
| 46 | + repoClient, err := repo.NewClient(*repoNodes, *repoKeyPath) |
| 47 | + |
| 48 | + if err != nil { |
| 49 | + runtime.StandardError(err) |
| 50 | + } |
| 51 | + |
| 52 | + node, err := nodule.NewHost( |
| 53 | + runPath, *nodeHost, *nodePort, *ctrlHost, *ctrlPort, *nodules, |
| 54 | + strings.SplitN(*nodulePaths, ",", -1), repoClient) |
| 55 | + |
| 56 | + if err != nil { |
| 57 | + runtime.StandardError(err) |
| 58 | + } |
| 59 | + |
| 60 | + node.Run(debug) |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +func getNodules(paths []string) (nodules [][]string) { |
| 65 | + |
| 66 | + if paths == nil { |
| 67 | + cwd, err := os.Getwd() |
| 68 | + if err != nil { |
| 69 | + runtime.StandardError(err) |
| 70 | + } |
| 71 | + paths = []string{cwd} |
| 72 | + } |
| 73 | + |
| 74 | + nodules = make([][]string, 0) |
| 75 | + seen := make(map[string]bool) |
| 76 | + |
| 77 | + for _, path := range paths { |
| 78 | + data, err := nodule.Find(path) |
| 79 | + if err != nil { |
| 80 | + runtime.StandardError(err) |
| 81 | + } |
| 82 | + for name, confpath := range data { |
| 83 | + if seen[confpath] { |
| 84 | + continue |
| 85 | + } |
| 86 | + nodules = append(nodules, []string{name, confpath}) |
| 87 | + seen[confpath] = true |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return |
| 92 | + |
| 93 | +} |
| 94 | + |
| 95 | +func ampBuild(argv []string, usage string) { |
| 96 | + |
| 97 | + opts := optparse.Parser( |
| 98 | + "Usage: amp build <path> [options]\n\n " + usage + "\n") |
| 99 | + |
| 100 | + args := opts.Parse(argv) |
| 101 | + |
| 102 | + if len(args) == 0 { |
| 103 | + opts.PrintUsage() |
| 104 | + runtime.Exit(0) |
| 105 | + } |
| 106 | + |
| 107 | + for _, info := range getNodules(args) { |
| 108 | + nodule.Build(info[0], info[1]) |
| 109 | + } |
| 110 | + |
| 111 | +} |
| 112 | + |
| 113 | +func ampTest(argv []string, usage string) { |
| 114 | + |
| 115 | + opts := optparse.Parser( |
| 116 | + "Usage: amp test <path> [options]\n\n " + usage + "\n") |
| 117 | + |
| 118 | + args := opts.Parse(argv) |
| 119 | + |
| 120 | + if len(args) == 0 { |
| 121 | + opts.PrintUsage() |
| 122 | + runtime.Exit(0) |
| 123 | + } |
| 124 | + |
| 125 | + for _, info := range getNodules(args) { |
| 126 | + nodule.Test(info[0], info[1]) |
| 127 | + } |
| 128 | + |
| 129 | +} |