private async Task ConnectAsync() { try { var configuration = new RegistryClientConfiguration(Registry); AuthenticationProvider authenticationProvider; if (IsAnonymous) { authenticationProvider = new AnonymousOAuthAuthenticationProvider(); } else { authenticationProvider = new PasswordOAuthAuthenticationProvider(Username, Password); } var client = configuration.CreateClient(authenticationProvider); await client.System.PingAsync(); var connection = new RegistryConnection(client, Registry, IsAnonymous, Username, Password); Connection = connection; //Save the connections! Save(); RequestClose(true); } catch (Exception ex) { MessageBox.Show(ex.Message, "Unable to connect"); } }
protected override async void Ok() { var ex = await Executor.ExecuteAsync(async() => { var configuration = new RegistryClientConfiguration(new Uri(Endpoint)); AuthenticationProvider authenticationProvider; if (IsAnonymous) { authenticationProvider = new AnonymousOAuthAuthenticationProvider(); } else { authenticationProvider = new PasswordOAuthAuthenticationProvider(Username, Password); } var client = configuration.CreateClient(authenticationProvider); await client.System.PingAsync(); RegistryClient = client; }); if (ex == null) { base.Ok(); } }
public async Task <DependencyVersion> GetLatestMetaDataAsync(RegistryDto registry, Dependency dependency) { RegistryClientConfiguration clientConfig = new RegistryClientConfiguration(new Uri(registry.Endpoint).GetComponents(UriComponents.HostAndPort, UriFormat.SafeUnescaped)); DependencyVersion version; if (!string.IsNullOrWhiteSpace(registry.Username) && !string.IsNullOrWhiteSpace(registry.Password)) { using (IRegistryClient client = clientConfig.CreateClient(new PasswordOAuthAuthenticationProvider(registry.Username, registry.Password))) { ListImageTagsResponse imageTags = await client.Tags.ListImageTagsAsync(dependency.Name, new ListImageTagsParameters()); version = new DependencyVersion { Dependency = dependency, DependencyId = dependency.Id, Version = imageTags.Tags.Last(), IsLatest = true }; } } else { using (IRegistryClient client = clientConfig.CreateClient()) { ListImageTagsResponse imageTags = await client.Tags.ListImageTagsAsync(dependency.Name, new ListImageTagsParameters()); version = new DependencyVersion { Dependency = dependency, DependencyId = dependency.Id, Version = imageTags.Tags.Last(), IsLatest = true }; } } return(version); }
public async Task BlobUploadOperationTest() { var config = new RegistryClientConfiguration(new Uri("http://172.16.0.175:3000")); var client = config.CreateClient(); using (var fs = File.OpenRead("")) { await client.BlobUploads.UploadBlobAsync("Test", (int)fs.Length, fs, ""); } }
public NetworkClient( RegistryClientConfiguration configuration, AuthenticationProvider authenticationProvider) { this._configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); this._authenticationProvider = authenticationProvider ?? throw new ArgumentNullException( nameof(authenticationProvider)); this._client = new HttpClient(); this.DefaultTimeout = configuration.DefaultTimeout; this.JsonSerializer = new JsonSerializer(); if (this._configuration.EndpointBaseUri != null) { this._effectiveEndpointBaseUri = this._configuration.EndpointBaseUri; } }
public RegistryClient( RegistryClientConfiguration configuration, AuthenticationProvider authenticationProvider) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if (authenticationProvider == null) { throw new ArgumentNullException(nameof(authenticationProvider)); } var client = new NetworkClient(configuration, authenticationProvider); this.Manifest = new ManifestOperations(client); this.Catalog = new CatalogOperations(client); this.Blobs = new BlobOperations(client); this.BlobUploads = new BlobUploadOperations(client); this.System = new SystemOperations(client); this.Tags = new TagOperations(client); }
public void PerformUpdate() { var deployments = kubernetes.ListDeploymentForAllNamespaces(); Console.WriteLine("{0} deployments to be checked.", deployments.Items.Count); var registries = from deployment in deployments.Items from container in deployment.Spec.Template.Spec.Containers let decomposedImage = ImageReference.Parse(container.Image) where ShouldProcess(deployment, decomposedImage) let versionMatchString = deployment.Metadata.Annotations group new { Deployment = deployment, Container = container, Image = decomposedImage, VersionString = deployment.Metadata.Annotations } by decomposedImage.FullyQualifiedRegistry(); ICollection <ContainerToUpdate> containerUpdates = new List <ContainerToUpdate>(); Console.WriteLine("{0} deployments should be processed.", registries.Count()); foreach (var registry in registries) { IRegistryClient registryClient = new RegistryClientConfiguration(registry.Key.Host).CreateClient(); var images = from r in registry group r by r.Image; foreach (var image in images) { ListImageTagsParameters tagsParameters = new ListImageTagsParameters { Number = 100 }; var result = registryClient.Tags.ListImageTagsAsync(image.Key.ImageName, tagsParameters); try { result.Wait(); } catch (AggregateException e) { throw new Exception(string.Format("Failed to list image tags for '{0}' at registry '{1}'.", image.Key.ImageName, image.Key.Registry), e.InnerException); } foreach (var container in image) { var matcher = ExtractMatchString(container.Deployment, container.Container.Name); var start = matcher.Match(container.Image.Version); if (!start.Success) { continue; } var currentVersion = new Version(start.Groups[1].Value); var upgradeTarget = (from tag in result.Result.Tags let parsed = matcher.Match(tag) where parsed.Success let candidate = new Version(parsed.Groups[1].Value) where candidate > currentVersion orderby candidate descending select tag).FirstOrDefault(); if (upgradeTarget != null) { Console.WriteLine("Upgrade deployment {0} container {1} from version {2} to {3}.", container.Deployment.Metadata.Name, container.Container.Name, container.Image.Version, upgradeTarget); var toVersion = container.Image.WithVersion(upgradeTarget); container.Container.Image = toVersion.ToString(); containerUpdates.Add(new ContainerToUpdate() { ContainerName = container.Container.Name, FromVersion = container.Image, ToVersion = toVersion }); kubernetes.ReplaceNamespacedDeployment(container.Deployment, container.Deployment.Metadata.Name, container.Deployment.Metadata.NamespaceProperty); } } } } var mailConfig = EmailConfig.GetFromEnvironment(); if (containerUpdates.Count > 0 && mailConfig.ShouldSend) { MailMessage mailMessage = new MailMessage { DeliveryNotificationOptions = DeliveryNotificationOptions.Never, Body = new EmailNotificationBuilder().BuildMessage(containerUpdates), Subject = string.Format("{0} containers updated", containerUpdates.Count) }; mailMessage.To.Add(mailConfig.ToAddress); mailMessage.From = mailConfig.FromAddress; SmtpClient smtpClient = new SmtpClient(mailConfig.RelayHost, mailConfig.RelayPort); smtpClient.Send(mailMessage); } }
private static async Task TestAsync() { //string url = "https://registry-1.docker.io/"; string url = "http://10.0.4.44:5000/"; var configuration = new RegistryClientConfiguration(new Uri(url)); using (var client = configuration.CreateClient()) { var bytes = File.ReadAllBytes(@"c:\layer.txt"); string sha256Hash; using (var sha = SHA256.Create()) { var hash = sha.ComputeHash(bytes); sha256Hash = $"sha256:{string.Join("", hash.Select(b => b.ToString("x")))}"; } using (var source = File.OpenRead(@"c:\layer.txt")) { // await client.BlobUploads.UploadBlobAsync("my-repo", (int)source.Length, source, sha256Hash); } ////Console.WriteLine("Ping..."); ////var tags = await client.Tags.ListImageTagsAsync("resin", new ListImageTagsParameters()); ////foreach (var tag in tags.Tags) ////{ //// Console.WriteLine(); ////} //var catalog = await client.Catalog.GetCatalogAsync(new CatalogParameters() //{ // Number = 10 //}); //var repositories = catalog.Repositories; //foreach (var repository in repositories) //{ // Console.WriteLine("-----------------------------------------------"); // Console.WriteLine(repository); // Console.WriteLine("-----------------------------------------------"); // var tags = await client.Tags.ListImageTagsAsync(repository, new ListImageTagsParameters()); // foreach (var tag in tags.Tags) // { // Console.WriteLine($" {tag}"); // } //} //if (string.IsNullOrWhiteSpace(repository)) //{ // Console.WriteLine("No repository found."); //} //else //{ // var tagsReponse = await client.Tags.ListImageTagsAsync(repository, new ListImageTagsParameters()); // var tag = tagsReponse.Tags.FirstOrDefault(); // if (string.IsNullOrWhiteSpace(tag)) // { // Console.WriteLine("No tags found."); // } // else // { // var manifestResult = await client.Manifest.GetManifestAsync(repository, tag); // Console.WriteLine(manifestResult.Manifest.GetType().Name); // var imageManifest = manifestResult.Manifest as ImageManifest2_1; // if (imageManifest != null) // { // var layer = imageManifest.FsLayers.First(); // var getBlobResponse = await client.Blobs.GetBlobAsync(repository, layer.BlobSum); // Console.WriteLine($"\t\tDigetst: {getBlobResponse.DockerContentDigest}"); // using (getBlobResponse.Stream) // using (Stream targetStream = File.OpenWrite(@"c:\test.layer")) // { // await getBlobResponse.Stream.CopyToAsync(targetStream); // } // } // } //} } }
private async void btnLogin_Click(object sender, EventArgs e) { Cursor PreviousCursor = this.Cursor; Cursor = Cursors.WaitCursor; Button Sender = sender as Button; GroupBox grp = default; ComboBox comboEndpoint = default; CheckBox chkAnonymous = default; TextBox txtUserName = default; TextBox txtPassword = default; if (Sender == btnBaseLogin) { grp = grpBaseRegistry; comboEndpoint = comboBaseRegistry; chkAnonymous = chkBaseAnonymous; txtUserName = txtBaseUserName; txtPassword = txtBasePassword; _baseRegistryClient = null; } else { grp = grpTargetRegistry; comboEndpoint = comboTargetRegistry; chkAnonymous = chkTargetAnonymous; txtUserName = txtTargetUserName; txtPassword = txtTargetPassword; _targetRegistryClient = null; } if (Sender.Text == "Logout") { foreach (var control in grp.Controls) { if (control.GetType() == typeof(ListBox)) { ((ListBox)control).Items.Clear(); } if (control.GetType() == typeof(TextBox)) { ((TextBox)control).Text = ""; } // ... } _baseRegistryClient = null; Sender.Text = "Login"; comboEndpoint.Focus(); } else { var configuration = new RegistryClientConfiguration(comboEndpoint.SelectedItem?.ToString() ?? comboEndpoint.Text); AuthenticationProvider authenticationProvider; if (chkAnonymous.Checked) { authenticationProvider = new AnonymousOAuthAuthenticationProvider(); } else { authenticationProvider = new PasswordOAuthAuthenticationProvider(txtUserName.Text, txtPassword.Text); } var client = configuration.CreateClient(authenticationProvider); try { await client.System.PingAsync(); } catch (UnauthorizedAccessException ex) { // authentication failed MessageBox.Show($"Authentication failed with {ex.Message}", "", MessageBoxButtons.OK, MessageBoxIcon.Error); Cursor = PreviousCursor; return; } catch (RegistryConnectionException ex) { // connection failed MessageBox.Show($"Unable to connect, exception {ex.Message})", "", MessageBoxButtons.OK, MessageBoxIcon.Error); Cursor = PreviousCursor; return; } Sender.Text = "Logout"; if (Sender == btnBaseLogin) { _baseRegistryClient = client; } else { _targetRegistryClient = client; } } Cursor = PreviousCursor; }
private async void btnLogin_Click(object sender, EventArgs e) { Cursor PreviousCursor = this.Cursor; Cursor = Cursors.WaitCursor; if ((sender as Button).Text == "Logout") { Text = $"Not connected"; foreach (var control in grpControls.Controls) { if (control.GetType() == typeof(ListBox)) { ((ListBox)control).Items.Clear(); } if (control.GetType() == typeof(TextBox)) { ((TextBox)control).Text = ""; } // ... } grpControls.Enabled = false; _registryClient = null; btnLogin.Text = "Login"; comboEndpoint.Focus(); } else { var configuration = new RegistryClientConfiguration(comboEndpoint.SelectedItem?.ToString() ?? comboEndpoint.Text); AuthenticationProvider authenticationProvider; if (chkAnonymous.Checked) { authenticationProvider = new AnonymousOAuthAuthenticationProvider(); } else { authenticationProvider = new PasswordOAuthAuthenticationProvider(txtUserName.Text, txtPassword.Text); } var client = configuration.CreateClient(authenticationProvider); try { await client.System.PingAsync(); } catch (UnauthorizedAccessException ex) { // authentication failed MessageBox.Show("Authentication failed", "", MessageBoxButtons.OK, MessageBoxIcon.Error); Cursor = PreviousCursor; return; } catch (RegistryConnectionException ex) { // connection failed MessageBox.Show("Unable to connect", "", MessageBoxButtons.OK, MessageBoxIcon.Error); Cursor = PreviousCursor; return; } Text = $"Connected to endpoint {comboEndpoint.SelectedItem.ToString()}"; grpControls.Enabled = true; btnLogin.Text = "Logout"; _registryClient = client; btnCatalog.Focus(); } Cursor = PreviousCursor; }