private async Task <DaemonHostViewModel> GetDaemons(DaemonHost daemonHost) { try { var api = _restApiProvider.Get <IDaemonApi>(daemonHost.Url); var daemons = await api.GetDaemons(daemonHost.SubscribedDaemons.ToArray()); // we pass this url to the Lightbringer.Service. // when one of its daemons changes, it will notify us by calling this url. var url = $"{Request.Scheme}://{Request.Host}{Request.PathBase}/api/notify/changed?id={daemonHost.Id}&type={NotifyParameter.DaemonType}&daemon={NotifyParameter.DaemonName}&state={NotifyParameter.State}"; await api.Notify(url); var daemonVms = daemons .Select(d => _daemonDtoConverter.ToDaemonVm(d, daemonHost)) .ToArray(); return(new DaemonHostViewModel { DaemonHost = daemonHost, Daemons = daemonVms }); } catch (Exception ex) { return(new DaemonHostViewModel { DaemonHost = daemonHost, Error = ex.Message }); } }
public async Task <IActionResult> Start(int id, string type, string daemon) { var daemonHost = _store.Get(id); // TODO: OK as long as we only have Win32 services if (!daemonHost.SubscribedDaemons.Contains(daemon)) { return(BadRequest()); } var api = _restApiProvider.Get <IDaemonApi>(daemonHost.Url); _log.LogInformation("Sending Start to {0}-{1}...", type, daemon); await api.Start(type, daemon); return(Ok()); }
public async Task <IActionResult> AddDaemonHostUrl(AddDaemonHostViewModel viewModel) { try { var url = viewModel.DaemonHostUrl; if (string.IsNullOrEmpty(url)) { return(RedirectToAction(nameof(Index))); } if (!url.Contains(':') && !url.Contains('/')) { url = _configuration.WebApiUrlTemplate.Replace("{hostName}", url); } var uri = new Uri(url); // try to parse url, if not valid, just fail. url = uri.ToString(); var daemonHost = _store().Find(url); if (daemonHost == null) { var isAliveApi = _restApiProvider.Get <IIsAliveApi>(url); await isAliveApi.Get(); daemonHost = new DaemonHost { Name = viewModel.DaemonHostUrl, Url = url }; _store().Upsert(daemonHost); } return(RedirectToAction(nameof(ListDaemons), new { hostId = daemonHost.Id })); } catch (Exception ex) { viewModel.Error = ex.Message; return(View(nameof(Index), viewModel)); } }