Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 1 | package flags |
| 2 | |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 3 | import ( |
| 4 | "flag" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "strings" |
Asim Shankar | 95910b6 | 2014-10-31 22:02:29 -0700 | [diff] [blame] | 8 | |
| 9 | "veyron.io/veyron/veyron/lib/flags/consts" |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 10 | ) |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 11 | |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 12 | // FlagGroup is the type for identifying groups of related flags. |
| 13 | type FlagGroup int |
| 14 | |
| 15 | const ( |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 16 | // Runtime identifies the flags and associated environment variables |
| 17 | // used by the Vanadium process runtime. Namely: |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 18 | // --veyron.namespace.root (which may be repeated to supply multiple values) |
| 19 | // --veyron.credentials |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 20 | Runtime FlagGroup = iota |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 21 | // Listen identifies the flags typically required to configure |
| 22 | // ipc.ListenSpec. Namely: |
| 23 | // --veyron.tcp.protocol |
| 24 | // --veyron.tcp.address |
| 25 | // --veyron.proxy |
| 26 | Listen |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 27 | // --veyron.acl (which may be repeated to supply multiple values) |
Cosmos Nicolaou | c7ddcf0 | 2014-11-05 16:26:56 -0800 | [diff] [blame] | 28 | // ACL files are named - i.e. --veyron.acl=<name>:<file> with the |
| 29 | // name <runtime> reserved for use by the runtime. |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 30 | ACL |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 31 | ) |
| 32 | |
Cosmos Nicolaou | e497a4b | 2014-10-31 09:50:01 -0700 | [diff] [blame] | 33 | const defaultNamespaceRoot = "/proxy.envyor.com:8101" |
| 34 | |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 35 | // Flags represents the set of flag groups created by a call to |
| 36 | // CreateAndRegister. |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 37 | type Flags struct { |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 38 | FlagSet *flag.FlagSet |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 39 | groups map[FlagGroup]interface{} |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 42 | type namespaceRootFlagVar struct { |
Cosmos Nicolaou | e497a4b | 2014-10-31 09:50:01 -0700 | [diff] [blame] | 43 | isSet bool // is true when a flag have has been explicitly set. |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 44 | roots []string |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 47 | func (nsr *namespaceRootFlagVar) String() string { |
| 48 | return fmt.Sprintf("%v", nsr.roots) |
| 49 | } |
| 50 | |
| 51 | func (nsr *namespaceRootFlagVar) Set(v string) error { |
Cosmos Nicolaou | e497a4b | 2014-10-31 09:50:01 -0700 | [diff] [blame] | 52 | if !nsr.isSet { |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 53 | // override the default value |
Cosmos Nicolaou | e497a4b | 2014-10-31 09:50:01 -0700 | [diff] [blame] | 54 | nsr.isSet = true |
| 55 | nsr.roots = []string{} |
| 56 | } |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 57 | nsr.roots = append(nsr.roots, v) |
| 58 | return nil |
| 59 | } |
| 60 | |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 61 | type aclFlagVar struct { |
| 62 | isSet bool |
| 63 | files map[string]string |
| 64 | } |
| 65 | |
| 66 | func (aclf *aclFlagVar) String() string { |
| 67 | return fmt.Sprintf("%v", aclf.files) |
| 68 | } |
| 69 | |
| 70 | func (aclf *aclFlagVar) Set(v string) error { |
| 71 | if !aclf.isSet { |
| 72 | // override the default value |
| 73 | aclf.isSet = true |
| 74 | aclf.files = make(map[string]string) |
| 75 | } |
| 76 | parts := strings.SplitN(v, ":", 2) |
| 77 | if len(parts) != 2 { |
| 78 | return fmt.Errorf("%q is not in 'name:file' format", v) |
| 79 | } |
| 80 | name, file := parts[0], parts[1] |
| 81 | aclf.files[name] = file |
| 82 | return nil |
| 83 | } |
| 84 | |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 85 | // RuntimeFlags contains the values of the Runtime flag group. |
| 86 | type RuntimeFlags struct { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 87 | // NamespaceRoots may be initialized by NAMESPACE_ROOT* enivornment |
| 88 | // variables as well as --veyron.namespace.root. The command line |
| 89 | // will override the environment. |
| 90 | NamespaceRoots []string // TODO(cnicolaou): provide flag.Value impl |
| 91 | |
| 92 | // Credentials may be initialized by the the VEYRON_CREDENTIALS |
| 93 | // environment variable. The command line will override the environment. |
| 94 | Credentials string // TODO(cnicolaou): provide flag.Value impl |
| 95 | |
Matt Rosencrantz | 3e76f28 | 2014-11-10 09:38:57 -0800 | [diff] [blame] | 96 | // Vtrace flags control various aspects of Vtrace. |
| 97 | Vtrace VtraceFlags |
| 98 | |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 99 | namespaceRootsFlag namespaceRootFlagVar |
| 100 | } |
| 101 | |
Matt Rosencrantz | 3e76f28 | 2014-11-10 09:38:57 -0800 | [diff] [blame] | 102 | type VtraceFlags struct { |
| 103 | // VtraceSampleRate is the rate (from 0.0 - 1.0) at which |
| 104 | // vtrace traces started by this process are sampled for collection. |
| 105 | SampleRate float64 |
| 106 | |
| 107 | // VtraceDumpOnShutdown tells the runtime to dump all stored traces |
| 108 | // to Stderr at shutdown if true. |
| 109 | DumpOnShutdown bool |
| 110 | |
| 111 | // VtraceCacheSize the number of traces to cache in memory. |
| 112 | CacheSize int |
| 113 | } |
| 114 | |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 115 | // ACLFlags contains the values of the ACLFlags flag group. |
| 116 | type ACLFlags struct { |
| 117 | flag aclFlagVar |
| 118 | } |
| 119 | |
| 120 | // ACLFile returns the file which is presumed to contain ACL information |
| 121 | // associated with the supplied name parameter. |
| 122 | func (af ACLFlags) ACLFile(name string) string { |
| 123 | return af.flag.files[name] |
| 124 | } |
| 125 | |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 126 | // ListenFlags contains the values of the Listen flag group. |
| 127 | type ListenFlags struct { |
| 128 | ListenProtocol TCPProtocolFlag |
| 129 | ListenAddress IPHostPortFlag |
| 130 | ListenProxy string |
| 131 | } |
| 132 | |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 133 | // createAndRegisterRuntimeFlags creates and registers the RuntimeFlags |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 134 | // group with the supplied flag.FlagSet. |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 135 | func createAndRegisterRuntimeFlags(fs *flag.FlagSet) *RuntimeFlags { |
| 136 | f := &RuntimeFlags{} |
Cosmos Nicolaou | e497a4b | 2014-10-31 09:50:01 -0700 | [diff] [blame] | 137 | roots, creds := readEnv() |
| 138 | if len(roots) == 0 { |
| 139 | f.namespaceRootsFlag.roots = []string{defaultNamespaceRoot} |
| 140 | } else { |
| 141 | f.namespaceRootsFlag.roots = roots |
| 142 | } |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 143 | |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 144 | fs.Var(&f.namespaceRootsFlag, "veyron.namespace.root", "local namespace root; can be repeated to provided multiple roots") |
Cosmos Nicolaou | e497a4b | 2014-10-31 09:50:01 -0700 | [diff] [blame] | 145 | fs.StringVar(&f.Credentials, "veyron.credentials", creds, "directory to use for storing security credentials") |
Matt Rosencrantz | 3e76f28 | 2014-11-10 09:38:57 -0800 | [diff] [blame] | 146 | |
| 147 | fs.Float64Var(&f.Vtrace.SampleRate, "veyron.vtrace.sample_rate", 0.0, "Rate (from 0.0 to 1.0) to sample vtrace traces.") |
| 148 | fs.BoolVar(&f.Vtrace.DumpOnShutdown, "veyron.vtrace.dump_on_shutdown", false, "If true, dump all stored traces on runtime shutdown.") |
| 149 | fs.IntVar(&f.Vtrace.CacheSize, "veyron.vtrace.cache_size", 1024, "The number of vtrace traces to store in memory.") |
| 150 | |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 151 | return f |
| 152 | } |
| 153 | |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 154 | func createAndRegisterACLFlags(fs *flag.FlagSet) *ACLFlags { |
| 155 | f := &ACLFlags{} |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 156 | fs.Var(&f.flag, "veyron.acl", "specify an acl file as <name>:<aclfile>") |
| 157 | return f |
| 158 | } |
| 159 | |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 160 | // createAndRegisterListenFlags creates and registers the ListenFlags |
| 161 | // group with the supplied flag.FlagSet. |
| 162 | func createAndRegisterListenFlags(fs *flag.FlagSet) *ListenFlags { |
| 163 | f := &ListenFlags{} |
| 164 | f.ListenProtocol = TCPProtocolFlag{"tcp"} |
| 165 | f.ListenAddress = IPHostPortFlag{Port: "0"} |
| 166 | fs.Var(&f.ListenProtocol, "veyron.tcp.protocol", "protocol to listen with") |
| 167 | fs.Var(&f.ListenAddress, "veyron.tcp.address", "address to listen on") |
| 168 | fs.StringVar(&f.ListenProxy, "veyron.proxy", "", "object name of proxy service to use to export services across network boundaries") |
| 169 | return f |
| 170 | } |
| 171 | |
| 172 | // CreateAndRegister creates a new set of flag groups as specified by the |
| 173 | // supplied flag group parameters and registers them with the supplied |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 174 | // flag.Flagset. |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 175 | func CreateAndRegister(fs *flag.FlagSet, groups ...FlagGroup) *Flags { |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 176 | if len(groups) == 0 { |
| 177 | return nil |
| 178 | } |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 179 | f := &Flags{FlagSet: fs, groups: make(map[FlagGroup]interface{})} |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 180 | for _, g := range groups { |
| 181 | switch g { |
| 182 | case Runtime: |
| 183 | f.groups[Runtime] = createAndRegisterRuntimeFlags(fs) |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 184 | case Listen: |
| 185 | f.groups[Listen] = createAndRegisterListenFlags(fs) |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 186 | case ACL: |
| 187 | f.groups[ACL] = createAndRegisterACLFlags(fs) |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | return f |
| 191 | } |
| 192 | |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 193 | // RuntimeFlags returns the Runtime flag subset stored in its Flags |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 194 | // instance. |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 195 | func (f *Flags) RuntimeFlags() RuntimeFlags { |
| 196 | if p := f.groups[Runtime]; p == nil { |
| 197 | return RuntimeFlags{} |
| 198 | } |
| 199 | from := f.groups[Runtime].(*RuntimeFlags) |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 200 | to := *from |
| 201 | to.NamespaceRoots = make([]string, len(from.NamespaceRoots)) |
| 202 | copy(to.NamespaceRoots, from.NamespaceRoots) |
| 203 | return to |
| 204 | } |
| 205 | |
| 206 | // ListenFlags returns a copy of the Listen flag group stored in Flags. |
| 207 | // This copy will contain default values if the Listen flag group |
| 208 | // was not specified when CreateAndRegister was called. The HasGroup |
| 209 | // method can be used for testing to see if any given group was configured. |
| 210 | func (f *Flags) ListenFlags() ListenFlags { |
| 211 | if p := f.groups[Listen]; p != nil { |
| 212 | return *(p.(*ListenFlags)) |
| 213 | } |
| 214 | return ListenFlags{} |
| 215 | } |
| 216 | |
Cosmos Nicolaou | 7823737 | 2014-11-04 18:19:09 -0800 | [diff] [blame] | 217 | // ACLFlags returns a copy of the ACL flag group stored in Flags. |
| 218 | // This copy will contain default values if the ACL flag group |
| 219 | // was not specified when CreateAndRegister was called. The HasGroup |
| 220 | // method can be used for testing to see if any given group was configured. |
| 221 | func (f *Flags) ACLFlags() ACLFlags { |
| 222 | if p := f.groups[ACL]; p != nil { |
| 223 | return *(p.(*ACLFlags)) |
| 224 | } |
| 225 | return ACLFlags{} |
| 226 | } |
| 227 | |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 228 | // HasGroup returns group if the supplied FlagGroup has been created |
| 229 | // for these Flags. |
| 230 | func (f *Flags) HasGroup(group FlagGroup) bool { |
| 231 | _, present := f.groups[group] |
| 232 | return present |
| 233 | } |
| 234 | |
| 235 | // Args returns the unparsed args, as per flag.Args. |
| 236 | func (f *Flags) Args() []string { |
| 237 | return f.FlagSet.Args() |
| 238 | } |
| 239 | |
Cosmos Nicolaou | e497a4b | 2014-10-31 09:50:01 -0700 | [diff] [blame] | 240 | // readEnv reads the legacy NAMESPACE_ROOT? and VEYRON_CREDENTIALS env vars. |
| 241 | func readEnv() ([]string, string) { |
| 242 | roots := []string{} |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 243 | for _, ev := range os.Environ() { |
| 244 | p := strings.SplitN(ev, "=", 2) |
| 245 | if len(p) != 2 { |
| 246 | continue |
| 247 | } |
| 248 | k, v := p[0], p[1] |
Asim Shankar | 95910b6 | 2014-10-31 22:02:29 -0700 | [diff] [blame] | 249 | if strings.HasPrefix(k, consts.NamespaceRootPrefix) && len(v) > 0 { |
Cosmos Nicolaou | e497a4b | 2014-10-31 09:50:01 -0700 | [diff] [blame] | 250 | roots = append(roots, v) |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
Asim Shankar | 95910b6 | 2014-10-31 22:02:29 -0700 | [diff] [blame] | 253 | return roots, os.Getenv(consts.VeyronCredentials) |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Cosmos Nicolaou | a025109 | 2014-11-09 22:04:37 -0800 | [diff] [blame] | 256 | // Parse parses the supplied args, as per flag.Parse. |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 257 | func (f *Flags) Parse(args []string) error { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 258 | // TODO(cnicolaou): implement a single env var 'VANADIUM_OPTS' |
| 259 | // that can be used to specify any command line. |
| 260 | if err := f.FlagSet.Parse(args); err != nil { |
| 261 | return err |
| 262 | } |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 263 | |
Cosmos Nicolaou | e497a4b | 2014-10-31 09:50:01 -0700 | [diff] [blame] | 264 | hasrt := f.groups[Runtime] != nil |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 265 | if hasrt { |
| 266 | runtime := f.groups[Runtime].(*RuntimeFlags) |
Cosmos Nicolaou | e497a4b | 2014-10-31 09:50:01 -0700 | [diff] [blame] | 267 | if runtime.namespaceRootsFlag.isSet { |
| 268 | // command line overrides the environment. |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 269 | runtime.NamespaceRoots = runtime.namespaceRootsFlag.roots |
Cosmos Nicolaou | e497a4b | 2014-10-31 09:50:01 -0700 | [diff] [blame] | 270 | } else { |
| 271 | // we have a default value for the command line, which |
| 272 | // is only used if the environment variables have not been |
| 273 | // supplied. |
| 274 | if len(runtime.NamespaceRoots) == 0 { |
| 275 | runtime.NamespaceRoots = runtime.namespaceRootsFlag.roots |
| 276 | } |
Cosmos Nicolaou | 9896004 | 2014-10-31 00:05:51 -0700 | [diff] [blame] | 277 | } |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 278 | } |
| 279 | return nil |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 280 | } |