Ejemplo n.º 1
0
        public void SaveTrafficSettings(TrafficSettings ts)
        {
            using (var db = new DefaultContext())
            {
                TrafficSettings currentTrafficSettings = db.TrafficSettings.SingleOrDefault(x => x.Pk_TrafficId == ts.Pk_TrafficId);
                currentTrafficSettings.Enabled            = ts.Enabled;
                currentTrafficSettings.Address            = ts.Address;
                currentTrafficSettings.WorkAddress        = ts.WorkAddress;
                currentTrafficSettings.Driving            = ts.Driving;
                currentTrafficSettings.AddressPlaceId     = ts.AddressPlaceId;
                currentTrafficSettings.WorkAddressPlaceId = ts.WorkAddressPlaceId;
                currentTrafficSettings.LatLng             = ts.LatLng;


                db.TrafficSettings.Attach(currentTrafficSettings);
                db.Entry(currentTrafficSettings).State = EntityState.Modified;
                db.SaveChanges();
            }
        }
Ejemplo n.º 2
0
        public override void OnInspectorGUI()
        {
            TrafficSettings ts = editingSettings.GetComponentInParent <TrafficSettings>();

            GUILayout.BeginVertical();
            EditorGUILayout.LabelField("Vehicles Allowed On Child Roads:");

            if (ts == null)
            {
                EditorGUILayout.LabelField("No Traffic Settings found In Parent Hierarchy!");
            }
            else
            {
                if (ts.vehiclePrefabs.Count == 0)
                {
                    EditorGUILayout.LabelField("Parent Traffic Settings should contain one of each vehicle in its vehicle pool!");
                }
                else
                {
                    uint   output  = 0;
                    bool[] toggles = new bool[ts.vehiclePrefabs.Count];
                    for (int a = 0; a < ts.vehiclePrefabs.Count; a++)
                    {
                        bool inputVal = (editingSettings.vehicleSelection & (1 << a)) != 0;
                        GUILayout.BeginHorizontal();
                        GUILayout.Space(20.0f);
                        bool outputVal = EditorGUILayout.ToggleLeft("" + ts.vehiclePrefabs[a].name, inputVal);
                        GUILayout.EndHorizontal();

                        output |= (outputVal == false) ? 0 : (uint)(1 << a);
                    }

                    editingSettings.vehicleSelection = output;
                }
            }
            if (GUILayout.Button("Add A New Road"))
            {
                AddNewPath();
            }

            GUILayout.EndVertical();
        }
Ejemplo n.º 3
0
 public void SaveTrafficSettings(TrafficSettings ts)
 {
     utility.SaveTrafficSettings(ts);
 }
Ejemplo n.º 4
0
 public static void ResetSettings()
 {
     sSettings = null;
 }
Ejemplo n.º 5
0
 public static void ResetSettings()
 {
     sSettings = null;
 }
Ejemplo n.º 6
0
        /// <inheritdoc/>
        public override void Run(CommandLine commandLine)
        {
            if (commandLine.HasHelpOption || commandLine.Arguments.Length == 0)
            {
                Console.WriteLine(usage);
                Program.Exit(0);
            }

            Program.ConnectHive();

            // Process the command arguments.

            var trafficManager = (TrafficManager)null;
            var yaml           = commandLine.HasOption("--yaml");
            var directorName   = commandLine.Arguments.FirstOrDefault();
            var isPublic       = false;

            switch (directorName)
            {
            case "help":

                // $hack: This isn't really a traffic manager name.

                Console.WriteLine(ruleHelp);
                Program.Exit(0);
                break;

            case "public":

                trafficManager = HiveHelper.Hive.PublicTraffic;
                isPublic       = true;
                break;

            case "private":

                trafficManager = HiveHelper.Hive.PrivateTraffic;
                isPublic       = false;
                break;

            default:

                Console.Error.WriteLine($"*** ERROR: Load balancer name must be one of [public] or [private] ([{directorName}] is not valid).");
                Program.Exit(1);
                break;
            }

            commandLine = commandLine.Shift(1);

            var command = commandLine.Arguments.FirstOrDefault();

            if (command == null)
            {
                Console.WriteLine(usage);
                Program.Exit(1);
            }

            commandLine = commandLine.Shift(1);

            string ruleName;

            switch (command)
            {
            case "get":

                ruleName = commandLine.Arguments.FirstOrDefault();

                if (string.IsNullOrEmpty(ruleName))
                {
                    Console.Error.WriteLine("*** ERROR: [RULE] argument expected.");
                    Program.Exit(1);
                }

                if (!HiveDefinition.IsValidName(ruleName))
                {
                    Console.Error.WriteLine($"*** ERROR: [{ruleName}] is not a valid rule name.");
                    Program.Exit(1);
                }

                // Fetch a specific traffic manager rule and output it.

                var rule = trafficManager.GetRule(ruleName);

                if (rule == null)
                {
                    Console.Error.WriteLine($"*** ERROR: Load balancer [{directorName}] rule [{ruleName}] does not exist.");
                    Program.Exit(1);
                }

                Console.WriteLine(yaml ? rule.ToYaml() : rule.ToJson());
                break;

            case "haproxy":
            case "haproxy-bridge":
            case "varnish":

                // We're going to download the traffic manager's ZIP archive containing the
                // [haproxy.cfg] or [varnish.vcl] file, extract and write it to the console.

                using (var consul = HiveHelper.OpenConsul())
                {
                    var proxy        = command.Equals("haproxy-bridge", StringComparison.InvariantCultureIgnoreCase) ? directorName + "-bridge" : directorName;
                    var confKey      = $"neon/service/neon-proxy-manager/proxies/{proxy}/proxy-conf";
                    var confZipBytes = consul.KV.GetBytesOrDefault(confKey).Result;

                    if (confZipBytes == null)
                    {
                        Console.Error.WriteLine($"*** ERROR: Proxy ZIP configuration was not found in Consul at [{confKey}].");
                        Program.Exit(1);
                    }

                    using (var msZipData = new MemoryStream(confZipBytes))
                    {
                        using (var zip = new ZipFile(msZipData))
                        {
                            var file  = command.Equals("varnish", StringComparison.InvariantCultureIgnoreCase) ? "varnish.vcl" : "haproxy.cfg";
                            var entry = zip.GetEntry(file);

                            if (entry == null || !entry.IsFile)
                            {
                                Console.Error.WriteLine($"*** ERROR: Proxy ZIP configuration in Consul at [{confKey}] appears to be corrupt.  Cannot locate the [{file}] entry.");
                                Program.Exit(1);
                            }

                            using (var entryStream = zip.GetInputStream(entry))
                            {
                                using (var reader = new StreamReader(entryStream))
                                {
                                    foreach (var line in reader.Lines())
                                    {
                                        Console.WriteLine(line);
                                    }
                                }
                            }
                        }
                    }
                }
                break;

            case "inspect":

                Console.WriteLine(NeonHelper.JsonSerialize(trafficManager.GetDefinition(), Formatting.Indented));
                break;

            case "list":
            case "ls":

                var showAll = commandLine.HasOption("--all");
                var showSys = commandLine.HasOption("--sys");
                var rules   = trafficManager.ListRules(
                    r =>
                {
                    if (showAll)
                    {
                        return(true);
                    }
                    else if (showSys)
                    {
                        return(r.System);
                    }
                    else
                    {
                        return(!r.System);
                    }
                });

                Console.WriteLine();
                Console.WriteLine($"[{rules.Count()}] {trafficManager.Name} rules");
                Console.WriteLine();

                foreach (var item in rules)
                {
                    Console.WriteLine(item.Name);
                }

                Console.WriteLine();
                break;

            case "purge":

                var purgeUri = commandLine.Arguments.FirstOrDefault();

                if (string.IsNullOrEmpty(purgeUri))
                {
                    Console.Error.WriteLine("*** ERROR: [URI-PATTERN] or [ALL] argument expected.");
                }

                if (purgeUri.Equals("all", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (!commandLine.HasOption("--force") && !Program.PromptYesNo($"*** Are you sure you want to purge all cached items for [{directorName.ToUpperInvariant()}]?"))
                    {
                        return;
                    }

                    trafficManager.PurgeAll();
                }
                else
                {
                    trafficManager.Purge(new string[] { purgeUri });
                }

                Console.WriteLine();
                Console.WriteLine("Purge request submitted.");
                Console.WriteLine();
                break;

            case "update":

                trafficManager.Update();
                break;

            case "remove":
            case "rm":

                ruleName = commandLine.Arguments.FirstOrDefault();

                if (string.IsNullOrEmpty(ruleName))
                {
                    Console.Error.WriteLine("*** ERROR: [RULE] argument expected.");
                    Program.Exit(1);
                }

                if (!HiveDefinition.IsValidName(ruleName))
                {
                    Console.Error.WriteLine($"*** ERROR: [{ruleName}] is not a valid rule name.");
                    Program.Exit(1);
                }

                if (trafficManager.RemoveRule(ruleName))
                {
                    Console.Error.WriteLine($"Deleted load balancer [{directorName}] rule [{ruleName}].");
                }
                else
                {
                    Console.Error.WriteLine($"*** ERROR: Load balancer [{directorName}] rule [{ruleName}] does not exist.");
                    Program.Exit(1);
                }
                break;

            case "set":

                // $todo(jeff.lill):
                //
                // It would be really nice to download the existing rules and verify that
                // adding the new rule won't cause conflicts.  Currently errors will be
                // detected only by the [neon-proxy-manager] which will log them and cease
                // updating the hive until the errors are corrected.
                //
                // An alternative would be to have some kind of service available in the
                // hive to do this for us or perhaps having [neon-proxy-manager] generate
                // a summary of all of the certificates (names, covered hostnames, and
                // expiration dates) and save this to Consul so it would be easy to
                // download.  Perhaps do the same for the rules?

                if (commandLine.Arguments.Length != 2)
                {
                    Console.Error.WriteLine("*** ERROR: FILE or [-] argument expected.");
                    Program.Exit(1);
                }

                // Load the rule.  Note that we support reading rules as JSON or
                // YAML, automatcially detecting the format.  We always persist
                // rules as JSON though.

                var ruleFile = commandLine.Arguments[1];

                string ruleText;

                if (ruleFile == "-")
                {
                    using (var input = Console.OpenStandardInput())
                    {
                        using (var reader = new StreamReader(input, detectEncodingFromByteOrderMarks: true))
                        {
                            ruleText = reader.ReadToEnd();
                        }
                    }
                }
                else
                {
                    ruleText = File.ReadAllText(ruleFile);
                }

                var trafficManagerRule = TrafficRule.Parse(ruleText, strict: true);

                ruleName = trafficManagerRule.Name;

                if (!HiveDefinition.IsValidName(ruleName))
                {
                    Console.Error.WriteLine($"*** ERROR: [{ruleName}] is not a valid rule name.");
                    Program.Exit(1);
                }

                // Validate a clone of the rule with any implicit frontends.

                var clonedRule = NeonHelper.JsonClone(trafficManagerRule);
                var context    = new TrafficValidationContext(directorName, null)
                {
                    ValidateCertificates = false        // Disable this because we didn't download the certs (see note above)
                };

                clonedRule.Validate(context);
                clonedRule.Normalize(isPublic);

                if (context.HasErrors)
                {
                    Console.Error.WriteLine("*** ERROR: One or more rule errors:");
                    Console.Error.WriteLine();

                    foreach (var error in context.Errors)
                    {
                        Console.Error.WriteLine(error);
                    }

                    Program.Exit(1);
                }

                if (trafficManager.SetRule(trafficManagerRule))
                {
                    Console.WriteLine($"Load balancer [{directorName}] rule [{ruleName}] has been updated.");
                }
                else
                {
                    Console.WriteLine($"Load balancer [{directorName}] rule [{ruleName}] has been added.");
                }
                break;

            case "settings":

                var settingsFile = commandLine.Arguments.FirstOrDefault();

                if (string.IsNullOrEmpty(settingsFile))
                {
                    Console.Error.WriteLine("*** ERROR: [-] or FILE argument expected.");
                    Program.Exit(1);
                }

                string settingsText;

                if (settingsFile == "-")
                {
                    settingsText = NeonHelper.ReadStandardInputText();
                }
                else
                {
                    settingsText = File.ReadAllText(settingsFile);
                }

                var trafficManagerSettings = TrafficSettings.Parse(settingsText, strict: true);

                trafficManager.UpdateSettings(trafficManagerSettings);
                Console.WriteLine($"Traffic manager [{directorName}] settings have been updated.");
                break;

            case "status":

                using (var consul = HiveHelper.OpenConsul())
                {
                    var statusJson = consul.KV.GetStringOrDefault($"neon/service/neon-proxy-manager/status/{directorName}").Result;

                    if (statusJson == null)
                    {
                        Console.Error.WriteLine($"*** ERROR: Status for traffic manager [{directorName}] is not currently available.");
                        Program.Exit(1);
                    }

                    var trafficManagerStatus = NeonHelper.JsonDeserialize <TrafficStatus>(statusJson);

                    Console.WriteLine();
                    Console.WriteLine($"Snapshot Time: {trafficManagerStatus.TimestampUtc} (UTC)");
                    Console.WriteLine();

                    using (var reader = new StringReader(trafficManagerStatus.Status))
                    {
                        foreach (var line in reader.Lines())
                        {
                            Console.WriteLine(line);
                        }
                    }
                }
                break;

            default:

                Console.Error.WriteLine($"*** ERROR: Unknown command: [{command}]");
                Program.Exit(1);
                break;
            }
        }