Ejemplo n.º 1
0
        private async Task SetupEmptyVolumeAsync(IMachineInteractionProvider sshClient, int size, CancellationToken?cancellationToken = null)
        {
            CancellationToken token = cancellationToken.HasValue ? cancellationToken.Value : new CancellationToken();

            try
            {
                this.VolumeId = await this.CreateVolumeAsync(size : size, cancellationToken : token);

                token.ThrowIfCancellationRequested();
            }
            catch (Exception e)
            {
                this.Logger.Log("Error creating new volume:\n{0}", e.Format());
                throw;
            }

            Exception exception = null;

            try
            {
                this.Logger.Log("Waiting for volume to reach the 'attached' state");
                this.Device = await this.Instance.AttachVolumeAsync(this);

                await this.UntilVolumeAttachedStateAsync("attached", cancellationToken : token);

                token.ThrowIfCancellationRequested();

                this.Logger.Log("Creating filesystem");
                await sshClient.SetupFilesystemAsync(this.Device, this.Logger);

                token.ThrowIfCancellationRequested();

                this.Logger.Log("Mounting device");
                await sshClient.MountDeviceAsync(this.Device, this.MountPoint, this.Logger, cancellationToken);

                token.ThrowIfCancellationRequested();
            }
            catch (Exception e)
            {
                exception = e;
            }
            if (exception != null)
            {
                await this.DeleteAsync();

                throw exception;
            }

            this.IsSetup = true;

            this.Logger.Log("Volume successfully created");
        }
Ejemplo n.º 2
0
        public async Task SetupAsync(IMachineInteractionProvider sshClient, CancellationToken?cancellationToken = null)
        {
            if (this.IsSetup)
            {
                this.Logger.Log("Already set up. Nothing to do");

                // TODO: More to be done here?
                return;
            }

            if (string.IsNullOrEmpty(this.source) && this.sizeGb.HasValue)
            {
                await this.SetupEmptyVolumeAsync(sshClient, sizeGb.Value, cancellationToken);
            }
            else if (!string.IsNullOrEmpty(this.source))
            {
                await this.SetupVolumeFromSourceAsync(sshClient, cancellationToken);
            }
            else
            {
                throw new Exception("Neither source not size specified. Don't know how to create volume");
            }
        }
Ejemplo n.º 3
0
        public async Task SetupVolumeFromSourceAsync(IMachineInteractionProvider sshClient, CancellationToken?cancellationToken = null)
        {
            CancellationToken token = cancellationToken.HasValue ? cancellationToken.Value : new CancellationToken();
            bool weCreatedVolume    = false;

            if (this.source.StartsWith("snap-"))
            {
                weCreatedVolume = true;
                try
                {
                    this.VolumeId = await this.CreateVolumeAsync(snapshotId : this.source, cancellationToken : token);

                    token.ThrowIfCancellationRequested();
                }
                catch (Exception e)
                {
                    this.Logger.Log("Error creating volume from snapshot:\n{0}", e.Format());
                    throw;
                }
            }
            else if (this.source.StartsWith("vol-"))
            {
                this.VolumeId = this.source;
            }
            else
            {
                throw new Exception("Volume ID must start with vol- or snap-");
            }

            Exception exception = null;

            try
            {
                this.Logger.Log("Waiting for volume to reach the 'attached' state");
                this.Device = await this.Instance.AttachVolumeAsync(this);

                await this.UntilVolumeAttachedStateAsync("attached", cancellationToken : token);

                token.ThrowIfCancellationRequested();

                this.Logger.Log("Mounting and setting up device");
                await sshClient.MountDeviceAsync(this.Device, this.MountPoint, this.Logger, cancellationToken);

                await sshClient.SetupDeviceAsync(this.Device, this.MountPoint, this.Logger);

                token.ThrowIfCancellationRequested();

                this.Logger.Log("Retriving port settings");
                var portSettings = (await sshClient.GetPortDescriptionsAsync(this.MountPoint, this.Logger, cancellationToken)).ToArray();
                token.ThrowIfCancellationRequested();

                await this.Instance.AuthorizeIngressAsync(portSettings);

                token.ThrowIfCancellationRequested();
            }
            catch (Exception e)
            {
                exception = e;
            }
            if (exception != null)
            {
                this.Logger.Log("Error performing the last operation: {0}. Rolling back", exception.Message);
                if (weCreatedVolume)
                {
                    await this.DeleteAsync();
                }
                throw exception;
            }

            this.IsSetup = true;

            this.Logger.Log("Volume successfully mounted");
        }
Ejemplo n.º 4
0
        private async Task SetupEmptyVolumeAsync(IMachineInteractionProvider sshClient, int size, CancellationToken? cancellationToken = null)
        {
            CancellationToken token = cancellationToken.HasValue ? cancellationToken.Value : new CancellationToken();

            try
            {
                this.VolumeId = await this.CreateVolumeAsync(size: size, cancellationToken: token);
                token.ThrowIfCancellationRequested();  
            }
            catch (Exception e)
            {
                this.Logger.Log("Error creating new volume:\n{0}", e.Format());
                throw;
            }

            Exception exception = null;
            try
            {
                this.Logger.Log("Waiting for volume to reach the 'attached' state");
                this.Device = await this.Instance.AttachVolumeAsync(this);
                await this.UntilVolumeAttachedStateAsync("attached", cancellationToken: token);
                token.ThrowIfCancellationRequested();

                this.Logger.Log("Creating filesystem");
                await sshClient.SetupFilesystemAsync(this.Device, this.Logger);
                token.ThrowIfCancellationRequested();

                this.Logger.Log("Mounting device");
                await sshClient.MountDeviceAsync(this.Device, this.MountPoint, this.Logger, cancellationToken);
                token.ThrowIfCancellationRequested();
            }
            catch (Exception e)
            {
                exception = e;
            }
            if (exception != null)
            {
                await this.DeleteAsync();
                throw exception;
            }

            this.IsSetup = true;

            this.Logger.Log("Volume successfully created");
        }
Ejemplo n.º 5
0
        public async Task SetupVolumeFromSourceAsync(IMachineInteractionProvider sshClient, CancellationToken? cancellationToken = null)
        {
            CancellationToken token = cancellationToken.HasValue ? cancellationToken.Value : new CancellationToken();
            bool weCreatedVolume = false;

            if (this.source.StartsWith("snap-"))
            {
                weCreatedVolume = true;
                try
                {
                    this.VolumeId = await this.CreateVolumeAsync(snapshotId: this.source, cancellationToken: token);
                    token.ThrowIfCancellationRequested();
                }
                catch (Exception e)
                {
                    this.Logger.Log("Error creating volume from snapshot:\n{0}", e.Format());
                    throw;
                }
            }
            else if (this.source.StartsWith("vol-"))
            {
                this.VolumeId = this.source;
            }
            else
            {
                throw new Exception("Volume ID must start with vol- or snap-");
            }

            Exception exception = null;
            try
            {
                this.Logger.Log("Waiting for volume to reach the 'attached' state");
                this.Device = await this.Instance.AttachVolumeAsync(this);
                await this.UntilVolumeAttachedStateAsync("attached", cancellationToken: token);
                token.ThrowIfCancellationRequested();

                this.Logger.Log("Mounting and setting up device");
                await sshClient.MountDeviceAsync(this.Device, this.MountPoint, this.Logger, cancellationToken);
                await sshClient.SetupDeviceAsync(this.Device, this.MountPoint, this.Logger);
                token.ThrowIfCancellationRequested();

                this.Logger.Log("Retriving port settings");
                var portSettings = (await sshClient.GetPortDescriptionsAsync(this.MountPoint, this.Logger, cancellationToken)).ToArray();
                token.ThrowIfCancellationRequested();

                await this.Instance.AuthorizeIngressAsync(portSettings);
                token.ThrowIfCancellationRequested();
            }
            catch (Exception e)
            {
                exception = e;
            }
            if (exception != null)
            {
                this.Logger.Log("Error performing the last operation: {0}. Rolling back", exception.Message);
                if (weCreatedVolume)
                    await this.DeleteAsync();
                throw exception;
            }

            this.IsSetup = true;

            this.Logger.Log("Volume successfully mounted");
        }
Ejemplo n.º 6
0
        public async Task SetupAsync(IMachineInteractionProvider sshClient, CancellationToken? cancellationToken = null)
        {
            if (this.IsSetup)
            {
                this.Logger.Log("Already set up. Nothing to do");

                // TODO: More to be done here?
                return;
            }

            if (string.IsNullOrEmpty(this.source) && this.sizeGb.HasValue)
            {
                await this.SetupEmptyVolumeAsync(sshClient, sizeGb.Value, cancellationToken);
            }
            else if (!string.IsNullOrEmpty(this.source))
            {
                await this.SetupVolumeFromSourceAsync(sshClient, cancellationToken);
            }
            else
            {
                throw new Exception("Neither source not size specified. Don't know how to create volume");
            }
        }