public RemoteAppRunHandle Handle(RemoteAppRunRequest request)
        {
            var handle = new RemoteAppRunHandle();

            try
            {
                // find the app to run
                Type appType = LighthouseFetcher.Fetch(request.What);


                // if not, also let them know
                if (appType == null)
                {
                    handle.Status = $"Can't find app with name: {request.What}";
                    return(handle);
                }

                // if you can run it!
                // inform the container the run the app dats it!
                Container.Launch(appType, request.How).GetAwaiter().GetResult();
            }
            catch (Exception e)
            {
                handle.Status = e.Message;
            }

            return(handle);
        }
Example #2
0
        static async Task Main(string[] args)
        {
            Console.WriteLine("Starting lighthouse server");

            var server = new LighthouseServer();

            server.AddLogger(Console.WriteLine);

            Parser.Default.ParseArguments <RunOptions, ViewOptions>(args)
            .MapResult(
                (RunOptions runOptions) =>
            {
                if (string.IsNullOrEmpty(runOptions.Application) && string.IsNullOrEmpty(runOptions.File))
                {
                    throw new ApplicationException("Can't find app name");
                }

                if (!runOptions.IsAppMode)
                {
                    var fileContents = File.ReadAllText(runOptions.File);

                    var config = YamlUtil.ParseYaml <LighthouseRunConfig>(fileContents);

                    //(IEnumerable<ResourceProviderConfig> Resources, IEnumerable<Type> Types) = YamlV1Decomposer.Deserialize(fileContents);

                    //var failedResourceCreations = new List<string>();

                    //foreach (var resourceConfig in Resources)
                    //{
                    //    (bool wasSuccessful, string errorReason) = ResourceFactory.TryCreate(resourceConfig, out var resource);

                    //    if (wasSuccessful)
                    //    {
                    //        server.RegisterResourceProvider(resource);
                    //    }
                    //    else
                    //    {
                    //        failedResourceCreations.Add(errorReason);
                    //    }
                    //}

                    // load resources first
                    foreach (var resource in config.Resources)
                    {
                    }

                    // then launch apps
                    foreach (var app in config.Applications)
                    {
                    }
                }
                else
                {
                    Type appType = LighthouseFetcher.Fetch(runOptions.Application);
                    if (appType == null)
                    {
                        throw new ApplicationException("Can't find app name");
                    }

                    server.Launch(appType);
                }

                return(0);
            },
                (ViewOptions viewOptions) =>
            {
                if (Uri.TryCreate(viewOptions.Server, UriKind.Absolute, out var uri))
                {
                    //var connection = server.Connect(uri);

                    // do more things here
                    return(0);
                }
                else
                {
                    throw new ApplicationException($"URI: {viewOptions.Server} could not be converted to a valid URI");
                }
            },
                errs => 1
                );

            _ = Console.ReadLine();
            await server.Stop();
        }
        public int Run(IEnumerable <string> args)
        {
            LighthouseClient GetClient(Uri target)
            {
                var networkProvider = TypeFactory.Create <INetworkProvider>();
                var client          = new LighthouseClient(target, networkProvider);

                client.AddLogger(ConsoleWrite);
                return(client);
            }

            var result = Parser.Default.ParseArguments <RunOptions, InspectOptions, StopOptions, StoreOptions, RetrieveOptions, ConfigureOptions>(args)
                         .MapResult(
                (RunOptions run) =>
            {
                if (run.IsFileMode)
                {
                    var fileContents = File.ReadAllText(run.File);

                    var config = YamlUtil.ParseYaml <LighthouseRunConfig>(fileContents);

                    // load resources first
                    foreach (var resource in config.Resources)
                    {
                    }

                    // then launch apps
                    foreach (var app in config.Applications)
                    {
                    }
                }
                else
                {
                    if (run.Where != null)
                    {
                        var client = GetClient(run.Where.ToUri());

                        // make a connection to the other server
                        var response = client.MakeRequest <RemoteAppRunRequest, RemoteAppRunHandle>(new RemoteAppRunRequest(run.What)).GetAwaiter().GetResult();
                        ConsoleWrite($"Request {response?.Status ?? "no response"} (ID: {response?.Id ?? "no ID"})");
                    }
                    else
                    {
                        Type appType = LighthouseFetcher.Fetch(run.What);
                        if (appType == null)
                        {
                            throw new ApplicationException($"Can't find app with name: {run.What}");
                        }

                        // start a lighthouse server locally, and have it run the task
                        var server = new LighthouseServer();
                        server.AddLogger(ConsoleWrite);
                        server.Launch(appType).GetAwaiter().GetResult();
                    }
                }

                return(0);
            },
                (InspectOptions inspect) =>
            {
                var client = GetClient(inspect.Where.ToUri());

                if (inspect.Where == null)
                {
                    throw new Exception("Must include Where to inspect.");
                }

                if (inspect.What == null)
                {
                    var response = client.MakeRequest <StatusRequest, StatusResponse>(new StatusRequest()).GetAwaiter().GetResult();
                    ConsoleWrite(response.ToString());
                }
                else
                {
                    var response = client.MakeRequest <InspectRequest, InspectResponse>(
                        new InspectRequest {
                        What = inspect.What
                    }
                        ).GetAwaiter().GetResult();

                    ConsoleWrite(string.Join(Environment.NewLine, response.RawResponse));
                }

                return(0);
            },
                (StopOptions stop) =>
            {
                if (stop.What == null || stop.Where == null)
                {
                    throw new Exception("Stop what and where?");
                }

                var client = GetClient(stop.Where.ToUri());

                var response = client.MakeRequest <StopRequest, bool>(
                    new StopRequest {
                    What = stop.What
                }
                    ).GetAwaiter().GetResult();

                ConsoleWrite(response ? $"{stop.What} stopped on {stop.Where}" : "failed");

                return(0);
            },
                (StoreOptions store) =>
            {
                if (store.What == null || store.Where == null)
                {
                    throw new Exception("Stop what and where?");
                }

                var client      = GetClient(store.Where.ToUri());
                var deserialize = store.What.DeserializeFromJSON <WarehouseStoreRequest>();

                var response = client.MakeRequest <WarehouseStoreRequest, bool>(
                    new WarehouseStoreRequest {
                    Key = deserialize.Key, Value = deserialize.Value
                }
                    ).GetAwaiter().GetResult();

                ConsoleWrite(response ? "stored" : "failed");

                return(0);
            },
                (RetrieveOptions retrieve) =>
            {
                if (retrieve.What == null || retrieve.Where == null)
                {
                    throw new Exception("Stop what and where?");
                }

                var client      = GetClient(retrieve.Where.ToUri());
                var deserialize = retrieve.What.DeserializeFromJSON <WarehouseRetrieveRequest>();

                var response = client.MakeRequest <WarehouseRetrieveRequest, WarehouseRetrieveResponse>(
                    new WarehouseRetrieveRequest {
                    Key = deserialize.Key
                }
                    ).GetAwaiter().GetResult();

                ConsoleWrite(response.Value ?? string.Empty);

                return(0);
            },
                (ConfigureOptions configure) =>
            {
                if (configure.What == null || configure.Where == null || configure.How == null)
                {
                    throw new Exception("Configure what,where, and how?");
                }

                var client = GetClient(configure.Where.ToUri());

                if (configure.What == "resource")
                {
                    var request = configure.How.DeserializeFromJSON <ResourceRequest>();

                    var response = client.MakeRequest <ResourceRequest, ResourceResponse>(request).GetAwaiter().GetResult();

                    foreach (var val in response.ActionsTaken)
                    {
                        ConsoleWrite(val);
                    }

                    // inspect the event stream for bound events ???
                    return(0);
                }

                ConsoleWrite($"unsupported configure target {configure.What}");
                return(-1);
            },

                errs =>
            {
                foreach (var error in errs)
                {
                    ConsoleWrite(error.ToString());
                }

                throw new Exception(string.Join(",", errs));
            });

            return(0);
        }