/// <summary>
        /// Creates a new cloud service
        /// </summary>
        /// <param name="location">the data centre location of the cloud service</param>
        /// <param name="description">The description of the cloud service</param>
        public void CreateNewCloudService(string location, string description = "Fluent Management created cloud service")
        {
            var hostedServiceCreate = new CreateCloudServiceCommand(Name, description, location)
            {
                Certificate    = ManagementCertificate,
                SubscriptionId = SubscriptionId
            };

            hostedServiceCreate.Execute();
        }
Example #2
0
        /// <summary>
        /// Creates a new virtual machine from a gallery template
        /// </summary>
        /// <param name="properties">Can be any gallery template</param>
        public IVirtualMachineClient CreateNewVirtualMachineFromTemplateGallery(WindowsVirtualMachineProperties properties)
        {
            // for the time being we're going to adopt the default powershell cmdlet behaviour and always create a new cloud services
            EnsureVirtualMachineProperties(properties);
            if (!properties.UseExistingCloudService)
            {
                var cloudServiceCommand = new CreateCloudServiceCommand(properties.CloudServiceName, "Created by Fluent Management", properties.Location)
                {
                    SubscriptionId = properties.SubscriptionId,
                    Certificate    = properties.Certificate
                };
                cloudServiceCommand.Execute();
            }
            // continue to the create the virtual machine in the cloud service
            var command = new CreateWindowsVirtualMachineDeploymentCommand(properties)
            {
                SubscriptionId = properties.SubscriptionId,
                Certificate    = properties.Certificate
            };

            command.Execute();
            // start the role up -- this could take a while the previous two operations are fairly lightweight
            // and the provisioning doesn't occur until the role starts not when it is created
            var startCommand = new StartVirtualMachineCommand(properties)
            {
                SubscriptionId = properties.SubscriptionId,
                Certificate    = properties.Certificate
            };

            startCommand.Execute();
            // important here to force a refresh - just in case someone to conduct an operation on the VM in a single step
            Properties = properties;
            var vm = VirtualMachine;

            // create a new client and return this so that properties can be populated automatically
            return(new WindowsVirtualMachineClient(properties));
        }
Example #3
0
        /// <summary>
        /// Creates a deplopment based on the specific criteria specified in the DeploymentManager
        /// </summary>
        /// <param name="packageLocation">Where the package is left</param>
        private void CreateDeployment(string packageLocation)
        {
            bool deleted = false;

            if (!_blobClient.CheckStorageAccountHasResolved())
            {
                throw new ApplicationException("unable to proceed storage account cannot be resolved after default 5 minute timeout");
            }

            if (!_manager.UseExistingHostedService)
            {
                var hostedServiceCreate = new CreateCloudServiceCommand(_manager.HostedServiceName, _manager.Description ?? "Deployed by Fluent Management", _manager.Location)
                {
                    Certificate    = _manager.ManagementCertificate,
                    SubscriptionId = _manager.SubscriptionId
                };
                hostedServiceCreate.Execute();
                _manager.WriteComplete(EventPoint.HostedServiceCreated, "Hosted service with name " + _manager.HostedServiceName + " created");
            }

            // send up service certificate - whatever happens we want the certificate up there - sometimes we may get a request but not need to alter the config of the SSL
            if (_manager.EnableSsl)
            {
                byte[] export = _manager.ServiceCertificate.Certificate.Export(X509ContentType.Pkcs12, _manager.ServiceCertificate.PvkPassword);

                var addCertificate =
                    new AddServiceCertificateCommand(export, _manager.ServiceCertificate.PvkPassword, _manager.HostedServiceName)
                {
                    SubscriptionId = _manager.SubscriptionId,
                    Certificate    = _manager.ManagementCertificate
                };
                addCertificate.Execute();
            }

            // read in the enum value for the additional params
            bool startImmediately = true, treatErrorsAsWarnings = false;

            if (_manager.DeploymentParams.HasValue)
            {
                startImmediately      = (_manager.DeploymentParams.Value & DeploymentParams.StartImmediately) == DeploymentParams.StartImmediately;
                treatErrorsAsWarnings = (_manager.DeploymentParams.Value & DeploymentParams.WarningsAsErrors) == DeploymentParams.WarningsAsErrors;
            }
            // read in the config and convert Base64
            var deployment = new CreateDeploymentCommand(_manager.HostedServiceName, _manager.DeploymentName, packageLocation,
                                                         _manager.Base64CsfgFile, _manager.DeploymentSlot, startImmediately, treatErrorsAsWarnings)
            {
                Certificate    = _manager.ManagementCertificate,
                SubscriptionId = _manager.SubscriptionId
            };

            try
            {
                deployment.Execute();
                _manager.WriteComplete(EventPoint.DeploymentCreated, "Deployment " + _manager.DeploymentName + " created");
            }
            catch (Exception)
            {
                DeleteDeployment();
                deleted = true;
            }
            finally
            {
                if (deleted)
                {
                    deployment.Execute();
                    _manager.WriteComplete(EventPoint.DeploymentCreated, "Deployment " + _manager.DeploymentName + " created");
                }
                // check here and execute on a timer to see if the role are ready and running
                if (_manager.WaitUntilAllRoleInstancesAreRunning)
                {
                    var command = new GetAggregateDeploymentStatusCommand(_manager.HostedServiceName, _manager.DeploymentSlot)
                    {
                        Certificate    = _manager.ManagementCertificate,
                        SubscriptionId = _manager.SubscriptionId
                    };
                    while (!command.AllDeploymentNodesRunning)
                    {
                        command.Execute();
                        // TODO: put a 5 second timer in here for now but replace with a timeout and exception method if over a certain value
                        Thread.Sleep(5000);
                    }
                }
            }
        }
Example #4
0
        /// <summary>
        /// Creates a new virtual machine from a gallery template
        /// </summary>
        /// <param name="properties">Can be any gallery template</param>
        /// <param name="cloudServiceName">The name of the cloud service - if it doesn't exist it will be created</param>
        /// <param name="serviceCertificate">The service certificate responsible for adding the ssh keys</param>
        /// <param name="location">Where the cloud service will be created if it doesn't exist</param>
        /// <param name="affinityGroup">Affinity group that this service will live in</param>
        public IVirtualMachineClient CreateNewVirtualMachineDeploymentFromTemplateGallery(List <LinuxVirtualMachineProperties> properties, string cloudServiceName, ServiceCertificateModel serviceCertificate = null, string location = LocationConstants.NorthEurope, string affinityGroup = "")
        {
            if (String.IsNullOrEmpty(cloudServiceName))
            {
                throw new FluentManagementException("Cloud service name cannot be empty", "LinuxVirtualMachineClient");
            }

            // if the cloud service doesn't exist we'll create it
            // first check that the service is available
            var checkCloudServiceAvailabilityCommand = new CheckCloudServiceNameAvailableCommand(cloudServiceName)
            {
                SubscriptionId = SubscriptionId,
                Certificate    = ManagementCertificate
            };

            checkCloudServiceAvailabilityCommand.Execute();
            Trace.WriteLine(String.Format("Checked cloud service availability - is available: {0}", checkCloudServiceAvailabilityCommand.CloudServiceAvailable));
            // when the check is complete we'll create the cloud service in the specified or default region if it doesn't exist
            if (checkCloudServiceAvailabilityCommand.CloudServiceAvailable)
            {
                var cloudServiceCommand = new CreateCloudServiceCommand(cloudServiceName, "Created by Fluent Management", location, affinityGroup)
                {
                    SubscriptionId = SubscriptionId,
                    Certificate    = ManagementCertificate
                };
                cloudServiceCommand.Execute();
                Trace.WriteLine(String.Format("Cloud service named {0} has been created", cloudServiceName));
            }
            // adds service certificate to the deployment
            if (serviceCertificate != null)
            {
                AddServiceCertificateToRoles(serviceCertificate, cloudServiceName, ref properties);
                Trace.WriteLine("A new service certificate has been added to the cloud service");
                ServiceCertificate = serviceCertificate.ServiceCertificate.Export(X509ContentType.Pfx);
            }

            // This is really unfortunate and not documented anywhere - unable to add multiple roles to a rolelist!!!
            // continue to the create the virtual machine in the cloud service
            var command = new CreateLinuxVirtualMachineDeploymentCommand(new List <LinuxVirtualMachineProperties>(new[] { properties[0] }), cloudServiceName)
            {
                SubscriptionId = SubscriptionId,
                Certificate    = ManagementCertificate
            };

            command.Execute();
            // raise this event every time we create a VM
            if (LinuxVirtualMachineCreationEvent != null)
            {
                LinuxVirtualMachineCreationEvent(this, properties[0]);
            }
            Trace.WriteLine("Deployment created and first virtual machine added");
            // try and add the other concurrently
            // concurrency doesn't work - there is no way to build a fast deployment :-(
            for (int i = 1; i < properties.Count; i++)
            {
                // add this we'll need to ensure that it's populated for the command
                var theProperty = properties[i];
                theProperty.CloudServiceName = cloudServiceName;
                // clousrure may cause an exception here - proof is in the pudding!
                var startCommand = new AddLinuxVirtualMachineToDeploymentCommand(theProperty, cloudServiceName)
                {
                    SubscriptionId = SubscriptionId,
                    Certificate    = ManagementCertificate
                };
                startCommand.Execute();
                // raise this event every time we create a VM
                if (LinuxVirtualMachineCreationEvent != null)
                {
                    LinuxVirtualMachineCreationEvent(this, theProperty);
                }
                Trace.WriteLine(String.Format("New VM added to deployment with hostname {0}", theProperty.HostName));
            }
            if (LinuxVirtualMachineStatusEvent != null)
            {
                Task.Factory.StartNew(() => CheckVmDeploymentIsRunning(properties));
            }

            // important here to force a refresh - just in case someone to conduct an operation on the VM in a single step
            Properties = properties;
            // create a new client and return this so that properties can be populated automatically
            return(new LinuxVirtualMachineClient(properties, SubscriptionId, ManagementCertificate));
        }