Beispiel #1
0
        private static string UploadSnapshot(SnapshotServiceClient client, Options.Create options)
        {
            if (!File.Exists(options.SnapshotPath))
            {
                Ipc.WriteError(Ipc.ErrorCode.NotFound, $"Could not find snapshot file at: {options.SnapshotPath}");
                return(null);
            }

            // Read snapshot.
            var bytes = File.ReadAllBytes(options.SnapshotPath);

            if (bytes.Length == 0)
            {
                Ipc.WriteError(Ipc.ErrorCode.Unknown, $"Snapshot file at {options.SnapshotPath} has zero bytes.");
                return(null);
            }

            // Create HTTP endpoint to upload to.
            var snapshotToUpload = new Snapshot
            {
                ProjectName    = options.ProjectName,
                DeploymentName = options.DeploymentName
            };

            using (var md5 = MD5.Create())
            {
                snapshotToUpload.Checksum = Convert.ToBase64String(md5.ComputeHash(bytes));
                snapshotToUpload.Size     = bytes.Length;
            }

            var uploadSnapshotResponse =
                client.UploadSnapshot(new UploadSnapshotRequest {
                Snapshot = snapshotToUpload
            });

            snapshotToUpload = uploadSnapshotResponse.Snapshot;

            using (var httpClient = new HttpClient())
            {
                try
                {
                    var content = new ByteArrayContent(bytes);
                    content.Headers.Add("Content-MD5", snapshotToUpload.Checksum);

                    if (options.Environment == "cn-production")
                    {
                        content.Headers.Add("x-amz-server-side-encryption", "AES256");
                    }

                    using (var response = httpClient.PutAsync(uploadSnapshotResponse.UploadUrl, content).Result)
                    {
                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            Ipc.WriteError(Ipc.ErrorCode.SnapshotUploadFailed, $"Snapshot upload returned non-OK error code: {response.StatusCode}");
                            return(null);
                        }
                    }
                }
                catch (HttpRequestException e)
                {
                    Ipc.WriteError(Ipc.ErrorCode.SnapshotUploadFailed, $"Failed to upload snapshot with following exception: {e.Message}");
                    return(null);
                }
            }

            // Confirm that the snapshot was uploaded successfully.
            var confirmUploadResponse = client.ConfirmUpload(new ConfirmUploadRequest
            {
                DeploymentName = snapshotToUpload.DeploymentName,
                Id             = snapshotToUpload.Id,
                ProjectName    = snapshotToUpload.ProjectName
            });

            return(confirmUploadResponse.Snapshot.Id);
        }
Beispiel #2
0
 public static int CreateDeployment(Options.Create options)
 {
     return(CreateDeploymentInternal(options, opts => File.ReadAllText(opts.LaunchJsonPath)));
 }