예제 #1
0
        public async Task <ActionResult> Execute([FromRoute] string appkey, [FromBody] DockerhubWebhock webhock, CancellationToken ct)
        {
            if (!string.IsNullOrEmpty(SecretHelper.GetSecret("appkey")))
            {
                if (!string.Equals(appkey, SecretHelper.GetSecret("appkey"), StringComparison.CurrentCultureIgnoreCase))
                {
                    Console.WriteLine($"HTTP 401: appkey invalid {appkey} ");
                    return(Unauthorized());
                }
            }
            else
            {
                if (!string.Equals(appkey, EnvironmentVariable.GetValueOrDefault("appkey", "topsecret"), StringComparison.CurrentCultureIgnoreCase))
                {
                    Console.WriteLine($"HTTP 401: appkey invalid {appkey} ");
                    return(Unauthorized());
                }
            }

            if (!string.Equals(webhock.PushData.Tag, EnvironmentVariable.GetValue("tag")) ||
                !string.Equals(webhock.Repository.Owner, EnvironmentVariable.GetValue("owner")) ||
                !string.Equals(webhock.Repository.Name, EnvironmentVariable.GetValue("imagename")))
            {
                return(BadRequest($"docker invalid: tag {webhock.PushData.Tag} / owner {webhock.Repository.Owner}  / imagename {webhock.Repository.Name}"));
            }

            string containerName = EnvironmentVariable.GetValue("containername");
            string portMap       = EnvironmentVariable.GetValueOrDefault("portmap", "");

            var helper = new ContainerHelper(EnvironmentVariable.GetValueOrDefault("endpointUrl", "unix:///var/run/docker.sock"));

            helper.PullImage(webhock.Repository.Owner, webhock.Repository.Name, webhock.PushData.Tag, ct);
            var containerId = await helper.GetContainerIdByName(containerName, ct);

            if (!string.IsNullOrEmpty(containerId))
            {
                await helper.StopContainer(containerId, ct);

                await helper.DeleteContainer(containerId);
            }
            await helper.StartContainer(containerName, webhock.Repository.Owner, webhock.Repository.Name, webhock.PushData.Tag, portMap, ct);

            return(Ok());
        }
        public DockerHubModule()
        {
            Post("/dockerhub/{appkey}", async(ctx, ct) =>
            {
                if (!string.Equals(ctx.appkey, EnvironmentVariable.GetValueOrDefault("appkey", "topsecret"), StringComparison.CurrentCultureIgnoreCase))
                {
                    Console.WriteLine($"HTTP 401: appkey invalid {ctx.appkey} ");
                    return(HttpStatusCode.Unauthorized);
                }

                using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
                {
                    var contentAsText = await reader.ReadToEndAsync();
                    var webhock       = JsonConvert.DeserializeObject <DockerhubWebhock>(contentAsText);
                    if (!string.Equals(webhock.PushData.Tag, EnvironmentVariable.GetValue("tag")) ||
                        !string.Equals(webhock.Repository.Owner, EnvironmentVariable.GetValue("owner")) ||
                        !string.Equals(webhock.Repository.Name, EnvironmentVariable.GetValue("imagename")))
                    {
                        Console.WriteLine($"HTTP 400: docker invalid: tag {webhock.PushData.Tag} / owner {webhock.Repository.Owner}  / imagename {webhock.Repository.Name}");
                        return(HttpStatusCode.BadRequest);
                    }

                    string containerName = EnvironmentVariable.GetValue("containername");
                    string portMap       = EnvironmentVariable.GetValueOrDefault("portmap", "");

                    var helper = new ContainerHelper(EnvironmentVariable.GetValueOrDefault("endpointUrl", "unix://var/run/docker.sock"));
                    await helper.PullImage(webhock.Repository.Owner, webhock.Repository.Name, webhock.PushData.Tag);
                    var containerId = await helper.GetContainerIdByName(containerName);
                    if (!string.IsNullOrEmpty(containerId))
                    {
                        await helper.StopContainer(containerId, ct);
                        await helper.DeleteContainer(containerId);
                    }
                    await helper.StartContainer(containerName, webhock.Repository.Owner, webhock.Repository.Name, webhock.PushData.Tag, portMap);
                    Console.WriteLine("HTTP 200");
                    return(HttpStatusCode.OK);
                }
            });
        }