public object ExecuteTool(string uid, [FromBody] IDictionary parameters = null) { if (uid == null) { throw new ArgumentNullException(nameof(uid)); } if (parameters == null) { parameters = new PythonDictionary(); } RepositoryEntity repositoryEntity; try { repositoryEntity = _repositoryService.LoadEntity(RepositoryEntityUid.Parse(uid)); } catch (WirehomeRepositoryEntityNotFoundException) { HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound; return(null); } try { var scriptHost = _pythonEngineService.CreateScriptHost(); scriptHost.Initialize(repositoryEntity.Script); return(scriptHost.InvokeFunction("main", parameters)); } catch (Exception exception) { return(new ExceptionPythonModel(exception).ConvertToPythonDictionary()); } }
public void DeleteEntity(string uid) { if (uid == null) { throw new ArgumentNullException(nameof(uid)); } _repositoryService.DeleteEntity(RepositoryEntityUid.Parse(uid)); }
public string get_file_uri(string uid, string filename) { if (uid == null) { throw new ArgumentNullException(nameof(uid)); } var entityUid = RepositoryEntityUid.Parse(uid); return($"/repository/{entityUid.Id}/{entityUid.Version}/{filename}"); }
public void Initialize(RepositoryEntityUid repositoryEntityUid) { RepositoryEntityUid = repositoryEntityUid ?? throw new ArgumentNullException(nameof(repositoryEntityUid)); var repositoryEntitySource = _repositoryService.LoadEntity(RepositoryEntityUid); lock (_syncRoot) { _pythonScriptHost = _pythonEngineService.CreateScriptHost(_logger); _pythonScriptHost.Initialize(repositoryEntitySource.Script); } }
public async Task DownloadAsync(RepositoryEntityUid uid, string targetPath) { if (uid == null) { throw new ArgumentNullException(nameof(uid)); } if (targetPath == null) { throw new ArgumentNullException(nameof(targetPath)); } var tempPath = targetPath + "_downloading"; if (Directory.Exists(tempPath)) { Directory.Delete(tempPath, true); } Directory.CreateDirectory(tempPath); using (var httpClient = new HttpClient()) { // The User-Agent is mandatory for using the GitHub API. // https://developer.github.com/v3/?#user-agent-required httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Wirehome.Core"); var uri = $"{_settings.OfficialRepositoryBaseUri}/{uid.Id}/{uid.Version}"; _logger.Log(LogLevel.Information, $"Downloading file list from '{uri}'."); var fileListContent = await httpClient.GetStringAsync(uri).ConfigureAwait(false); var fileList = JsonConvert.DeserializeObject <List <GitHubFileEntry> >(fileListContent); foreach (var fileEntry in fileList) { uri = fileEntry.DownloadUrl; _logger.Log(LogLevel.Information, $"Downloading file '{uri}' (Size = {fileEntry.Size} bytes)."); var fileContent = await httpClient.GetByteArrayAsync(uri).ConfigureAwait(false); var filename = Path.Combine(tempPath, fileEntry.Name); File.WriteAllBytes(filename, fileContent); } if (Directory.Exists(targetPath)) { Directory.Delete(targetPath, true); } Directory.Move(tempPath, targetPath); } }
public void TryInitializeService(string id, ServiceConfiguration configuration) { if (!configuration.IsEnabled) { _logger.Log(LogLevel.Information, $"Service '{id}' not initialized because it is disabled."); return; } try { var repositoryEntityUid = new RepositoryEntityUid { Id = id, Version = configuration.Version }; var serviceInstance = new ServiceInstance(_repositoryService, _pythonEngineService, _loggerFactory); serviceInstance.Initialize(repositoryEntityUid); if (configuration.Variables != null) { foreach (var variable in configuration.Variables) { serviceInstance.SetVariable(variable.Key, variable.Value); } } _logger.Log(LogLevel.Information, "Initializing service '{0}'.", id); serviceInstance.ExecuteFunction("initialize"); lock (_serviceInstances) { if (_serviceInstances.TryGetValue(id, out var existingServiceInstance)) { _logger.Log(LogLevel.Information, "Stopping service '{0}'.", id); existingServiceInstance.ExecuteFunction("stop"); } _serviceInstances[id] = serviceInstance; _logger.Log(LogLevel.Information, "Starting service '{0}'.", id); serviceInstance.ExecuteFunction("start"); } _logger.Log(LogLevel.Information, $"Service '{id}' started."); } catch (Exception exception) { _logger.Log(LogLevel.Error, exception, $"Error while initializing service '{id}'."); } }
public async Task DownloadEntity(string uid) { if (uid == null) { throw new ArgumentNullException(nameof(uid)); } var entityUid = RepositoryEntityUid.Parse(uid); if (string.IsNullOrEmpty(entityUid.Version)) { HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; return; } await _repositoryService.DownloadEntityAsync(entityUid); }
public string GetReleaseNotes(string uid) { if (uid == null) { throw new ArgumentNullException(nameof(uid)); } var entityUid = RepositoryEntityUid.Parse(uid); if (string.IsNullOrEmpty(entityUid.Version)) { HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; return(null); } return(_repositoryService.GetReleaseNotes(entityUid)); }
private ServiceInstance CreateServiceInstance(string id, ServiceConfiguration configuration) { var repositoryEntityUid = new RepositoryEntityUid(id, configuration.Version); var repositoryEntitySource = _repositoryService.LoadEntity(repositoryEntityUid); var scriptHost = _pythonScriptHostFactoryService.CreateScriptHost(_logger); scriptHost.Initialize(repositoryEntitySource.Script); var serviceInstance = new ServiceInstance(id, configuration, scriptHost); if (configuration.Variables != null) { foreach (var variable in configuration.Variables) { serviceInstance.SetVariable(variable.Key, variable.Value); } } return(serviceInstance); }
public WirehomeRepositoryEntityNotFoundException(RepositoryEntityUid entityUid) : base($"Repository entity '{entityUid}' not found.", null) { }