Exemplo n.º 1
0
        internal override void Execute(SkytapClient client, SkytapConfiguration configuration)
        {
            var runstate = client.GetConfigurationRunstate(configuration.Id);

            if (runstate == "stopped")
            {
                this.LogInformation("Environment {0} is already stopped.", configuration.Name);
                return;
            }

            if (runstate == "suspended" && this.Runstate == StopConfigurationMode.Suspend)
            {
                this.LogInformation("Environment {0} is already suspended.", configuration.Name);
                return;
            }

            this.LogInformation(
                "{0} environment...",
                InedoLib.Util.Switch <StopConfigurationMode, string>(this.Runstate)
                .Case(StopConfigurationMode.Suspend, "Suspending")
                .Case(StopConfigurationMode.ShutDown, "Shutting down")
                .Case(StopConfigurationMode.PowerOff, "Powering off")
                .End()
                );

            var targetRunstate = InedoLib.Util.Switch <StopConfigurationMode, string>(this.Runstate)
                                 .Case(StopConfigurationMode.Suspend, "suspended")
                                 .Case(StopConfigurationMode.ShutDown, "stopped")
                                 .Case(StopConfigurationMode.PowerOff, "halted")
                                 .End();

            client.SetConfigurationRunstate(configuration.Id, targetRunstate);

            if (this.WaitForStop)
            {
                this.LogInformation("Stop command issued; waiting for environment to enter {0} state...", this.Runstate == StopConfigurationMode.Suspend ? "suspended" : "stopped");

                do
                {
                    this.Context.CancellationToken.WaitHandle.WaitOne(5000);
                    this.ThrowIfCanceledOrTimeoutExpired();
                    runstate = client.GetConfigurationRunstate(configuration.Id);
                }while (runstate == "busy");

                if (runstate == "stopped" || runstate == "suspended")
                {
                    this.LogInformation("Environment is {0}.", runstate);
                }
                else
                {
                    this.LogError("Environment is {0}.", runstate);
                }
            }
            else
            {
                this.LogInformation("Stop command issued.");
            }
        }
Exemplo n.º 2
0
        internal sealed override void Execute(SkytapClient client)
        {
            if (string.IsNullOrWhiteSpace(this.ConfigurationId) && string.IsNullOrWhiteSpace(this.ConfigurationName))
            {
                this.LogError("Environment ID or environment name must be specified.");
                return;
            }

            this.LogDebug("Using best match for environment Id={0}, environment name={1}", this.ConfigurationId, this.ConfigurationName);

            var notFoundLogLevel = this.allowConfigNotFound ? MessageLevel.Debug : MessageLevel.Error;

            this.LogDebug("Looking up environment on Skytap...");

            SkytapConfiguration configuration = null;

            if (!string.IsNullOrWhiteSpace(this.ConfigurationId))
            {
                configuration = client.GetConfiguration(this.ConfigurationId);
                if (configuration == null)
                {
                    if (string.IsNullOrWhiteSpace(this.ConfigurationName))
                    {
                        var message = "Could not find environment with ID=" + this.ConfigurationId;
                        this.Log(notFoundLogLevel, message);
                        if (this.allowConfigNotFound)
                        {
                            this.Execute(client, null);
                        }
                        return;
                    }
                    else
                    {
                        this.LogDebug("Could not find environment with ID=" + this.ConfigurationId + "; looking up environment with name=" + this.ConfigurationName);
                    }
                }
            }

            if (configuration == null && !string.IsNullOrWhiteSpace(this.ConfigurationName))
            {
                configuration = client.GetConfigurationFromName(this.ConfigurationName);
                if (configuration == null)
                {
                    var message = "Could not find environment with name=" + this.ConfigurationName;
                    this.Log(notFoundLogLevel, message);
                    if (this.allowConfigNotFound)
                    {
                        this.Execute(client, null);
                    }
                    return;
                }
            }

            this.LogDebug("Found environment ID={0}, name={1}", configuration.Id, configuration.Name);
            this.Execute(client, configuration);
        }
Exemplo n.º 3
0
        protected override sealed void Execute()
        {
            var configurer = (SkytapExtensionConfigurer)this.GetExtensionConfigurer();

            if (configurer == null)
            {
                this.LogError("A configuration profile is required for this action.");
                return;
            }

            if (string.IsNullOrWhiteSpace(configurer.UserName))
            {
                this.LogError("A user name must be specified in the Skytap extension configuration profile.");
                return;
            }

            if (string.IsNullOrWhiteSpace(configurer.Password))
            {
                this.LogError("An API key must be specified in the Skytap extension configuration profile.");
                return;
            }

            try
            {
                var client = new SkytapClient(configurer.UserName, configurer.Password);
                this.Execute(client);
            }
            catch (WebException ex)
            {
                var response = ex.Response as HttpWebResponse;
                if (response == null)
                {
                    throw;
                }

                try
                {
                    using (var responseStream = response.GetResponseStream())
                    {
                        var xdoc  = XDocument.Load(responseStream);
                        var error = (string)xdoc.Descendants("error").FirstOrDefault();
                        if (!string.IsNullOrWhiteSpace(error))
                        {
                            this.LogError("Error returned from Skytap: ({0}) {1}", (int)response.StatusCode, error);
                            return;
                        }
                    }
                }
                catch
                {
                }

                this.LogError("Error returned from Skytap: ({0}) {1}", (int)response.StatusCode, response.StatusDescription);
            }
        }
Exemplo n.º 4
0
        internal override void Execute(SkytapClient client, SkytapConfiguration configuration)
        {
            if (configuration == null)
            {
                this.LogWarning("Environment {0} not found.", this.ConfigurationName);
                return;
            }

            this.LogInformation("Deleting {0} environment...", configuration.Name);
            try
            {
                client.DeleteConfiguration(configuration.Id);
                this.LogInformation("Environment deleted.");
            }
            catch (Exception ex)
            {
                this.LogError("The environment could not be deleted: " + ex.Message);
            }
        }
Exemplo n.º 5
0
        public static object GetTemplates(string token)
        {
            var rawToken = Encoding.UTF8.GetString(ProtectedData.Unprotect(Convert.FromBase64String(token), null, DataProtectionScope.LocalMachine));
            var parts    = rawToken.Split(new[] { ':' }, 3);

            var cache        = HttpContext.Current.Cache;
            var cachedValues = cache.Get(parts[0]);

            if (cachedValues == null)
            {
                var client = new SkytapClient(parts[1], parts[2]);
                cachedValues = client
                               .ListTemplates()
                               .Select(t => new { id = Uri.EscapeDataString(t.Id) + "&" + Uri.EscapeDataString(t.Name), text = t.Name })
                               .ToList();

                cache.Add(parts[0], cachedValues, null, DateTime.UtcNow.AddMinutes(15), Cache.NoSlidingExpiration, CacheItemPriority.Low, null);
            }

            return(cachedValues);
        }
Exemplo n.º 6
0
        internal override void Execute(SkytapClient client)
        {
            if (string.IsNullOrWhiteSpace(this.TemplateId) && string.IsNullOrWhiteSpace(this.TemplateName))
            {
                this.LogError("Template ID or template name must be specified.");
                return;
            }

            SkytapResource template = null;

            if (!string.IsNullOrWhiteSpace(this.TemplateId))
            {
                template = client.GetTemplate(this.TemplateId);
                if (template == null)
                {
                    if (string.IsNullOrWhiteSpace(this.TemplateName))
                    {
                        this.LogError("Could not find template with ID=" + this.TemplateId);
                        return;
                    }
                    else
                    {
                        this.LogDebug("Could not find template with ID=" + this.TemplateId + "; looking up template with name=" + this.TemplateName);
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(this.TemplateName))
            {
                template = client.GetTemplateFromName(this.TemplateName);
                if (template == null)
                {
                    this.LogError("Could not find template with name=" + this.TemplateName);
                    return;
                }
            }

            this.LogDebug("Found template ID={0}, name={1}", template.Id, template.Name);
            this.Execute(client, template);
        }
Exemplo n.º 7
0
        internal override void Execute(SkytapClient client, SkytapConfiguration configuration)
        {
            if (string.IsNullOrWhiteSpace(this.NewConfigurationName))
            {
                this.LogInformation("Creating environment from {1} environment...", configuration.Name);
            }
            else
            {
                this.LogInformation("Creating {0} environment from {1} template...", this.NewConfigurationName, configuration.Name);
            }

            string configurationId;

            try
            {
                configurationId = client.CopyConfiguration(configuration.Id);
            }
            catch (WebException ex)
            {
                this.LogError("The environment could not be copied: " + ex.Message);
                return;
            }

            this.LogDebug("Environment copied (ID={0})", configurationId);

            if (!string.IsNullOrWhiteSpace(this.NewConfigurationName))
            {
                this.LogDebug("Setting environment name to {0}...", this.NewConfigurationName);
                client.RenameConfiguration(configurationId, this.NewConfigurationName);
                this.LogDebug("Environment renamed.");
            }

            if (this.ExportVariables)
            {
                this.SetSkytapVariableValue("EnvironmentId", configurationId);
            }

            this.LogInformation("Environment copied.");
        }
Exemplo n.º 8
0
        internal override void Execute(SkytapClient client, SkytapConfiguration configuration)
        {
            var runstate = client.GetConfigurationRunstate(configuration.Id);

            if (runstate == "running")
            {
                this.LogInformation("Environment {0} is already running.", configuration.Name);
                return;
            }

            this.LogInformation("Starting environment...");
            client.SetConfigurationRunstate(configuration.Id, "running");

            if (this.WaitForStart)
            {
                this.LogInformation("Start command issued; waiting for environment to enter running state...");

                do
                {
                    this.Context.CancellationToken.WaitHandle.WaitOne(5000);
                    this.ThrowIfCanceledOrTimeoutExpired();
                    runstate = client.GetConfigurationRunstate(configuration.Id);
                }while (runstate == "busy");

                if (runstate == "running")
                {
                    this.LogInformation("Environment is running.");
                }
                else
                {
                    this.LogError("Environment is {0}.", runstate);
                }
            }
            else
            {
                this.LogInformation("Start command issued.");
            }
        }
Exemplo n.º 9
0
        internal override void Execute(SkytapClient client, SkytapConfiguration configuration)
        {
            var serverIds = this.CreateServers(configuration);

            if (serverIds.Count == 0)
            {
                this.LogWarning("There were no BuildMaster servers created for this environment.");
                return;
            }

            this.RunUpdater();

            this.LogInformation("Waiting for servers to enter ready state...");

            do
            {
                var statuses = StoredProcs
                               .Environments_GetServers(Domains.YN.No)
                               .Execute()
                               .Where(s => serverIds.Contains(s.Server_Id));

                foreach (var server in statuses)
                {
                    if (server.ServerStatus_Code == Domains.ServerStatus.Ready)
                    {
                        this.LogDebug("Server {0} is ready.", server.Server_Name);
                        serverIds.Remove(server.Server_Id);
                    }
                }

                this.Context.CancellationToken.WaitHandle.WaitOne(5000);
                this.ThrowIfCanceledOrTimeoutExpired();
            }while (serverIds.Count > 0);

            this.LogInformation("Servers are ready.");
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            var client = new SkytapClient("yourUser", "yourKey");

            // Scratch pad for trying out the client
        }
Exemplo n.º 11
0
 internal abstract void Execute(SkytapClient client, SkytapConfiguration configuration);
Exemplo n.º 12
0
 internal abstract void Execute(SkytapClient client);
Exemplo n.º 13
0
        internal override void Execute(SkytapClient client, SkytapConfiguration configuration)
        {
            if (string.IsNullOrWhiteSpace(this.DefaultAccess) && (this.VirtualMachines == null || this.VirtualMachines.Length == 0))
            {
                this.LogError("Virtual machine access level is not specified.");
                return;
            }

            this.LogInformation("Getting list of virtual machines for {0} environment...", configuration.Name);
            var configVms = client.ListVms(configuration.Id).ToList();

            this.LogDebug("Received {0} virtual machines.", configVms.Count);

            var publishedVmRefs = new List <SkytapPublishedVmRef>();

            if (this.VirtualMachines != null && this.VirtualMachines.Length > 0)
            {
                var vms = this.VirtualMachines
                          .GroupJoin(
                    configVms,
                    p => p.Name,
                    v => v.Name,
                    (p, v) => new { p.Name, p.Access, Matches = v.ToList() },
                    StringComparer.OrdinalIgnoreCase);

                bool errors = false;
                foreach (var vm in vms)
                {
                    if (vm.Matches.Count == 1)
                    {
                        this.LogDebug("{0} with {1} access", vm.Name, vm.Access);
                        publishedVmRefs.Add(new SkytapPublishedVmRef(vm.Matches[0].Id, vm.Access));
                    }
                    else if (vm.Matches.Count == 0)
                    {
                        this.LogError("Could not resolve virtual machine named {0} in {1} environment.", vm.Name, configuration.Name);
                        errors = true;
                    }
                    else
                    {
                        this.LogError("Ambiguous virtual machine names ({0}) in {1} environment.", vm.Name, configuration.Name);
                        errors = true;
                    }
                }

                if (errors)
                {
                    return;
                }
            }
            else
            {
                foreach (var vm in configVms)
                {
                    this.LogDebug("{0} with {1} access", vm.Name, this.DefaultAccess);
                    publishedVmRefs.Add(new SkytapPublishedVmRef(vm.Id, this.DefaultAccess));
                }
            }

            TimeSpan?runtimeLimit = null;

            if (!string.IsNullOrEmpty(this.RuntimeLimitHours))
            {
                runtimeLimit = TimeSpan.FromHours(double.Parse(this.RuntimeLimitHours));
                this.LogDebug("Runtime limit: " + runtimeLimit);
            }

            DateTime?expirationDate = null;

            if (!string.IsNullOrEmpty(this.ExpirationHours))
            {
                var timeUntilExipiration = TimeSpan.FromHours(double.Parse(this.ExpirationHours));
                expirationDate = DateTime.UtcNow + timeUntilExipiration;
                this.LogDebug("Expiration date: {0} (UTC)", expirationDate);
            }

            TimeSpan?dailyStart = null;
            TimeSpan?dailyEnd   = null;

            if (!string.IsNullOrEmpty(this.DailyAccessStart) && !string.IsNullOrEmpty(this.DailyAccessEnd))
            {
                var utcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
                var start     = TimeSpan.Parse(this.DailyAccessStart) - utcOffset;
                var end       = TimeSpan.Parse(this.DailyAccessEnd) - utcOffset;

                if (start < TimeSpan.Zero)
                {
                    start += new TimeSpan(24, 0, 0);
                }
                else if (start >= new TimeSpan(24, 0, 0))
                {
                    start -= new TimeSpan(24, 0, 0);
                }

                if (end < TimeSpan.Zero)
                {
                    end += new TimeSpan(24, 0, 0);
                }
                else if (end >= new TimeSpan(24, 0, 0))
                {
                    end -= new TimeSpan(24, 0, 0);
                }

                dailyStart = start;
                dailyEnd   = end;

                this.LogDebug("Daily access window: {0} to {1} (UTC)", start, end);
            }

            this.LogInformation("Creating publish set...");
            var publishedSet = client.CreatePublishSet(
                configuration.Id,
                this.PublishedSetName,
                this.OneUrlPerVm,
                dailyStart,
                dailyEnd,
                this.Password,
                publishedVmRefs,
                runtimeLimit,
                expirationDate
                );

            this.LogInformation("Publish set created.");

            if (this.ExportVariables)
            {
                string desktopsUrl;

                if (this.OneUrlPerVm && publishedSet.Vms.Count > 1)
                {
                    desktopsUrl = string.Join(
                        Environment.NewLine,
                        publishedSet.Vms
                        .Join(
                            configVms,
                            p => p.Id,
                            v => v.Id,
                            (p, v) => new { v.Name, p.DesktopUrl })
                        .Select(v => v.Name + ": " + v.DesktopUrl)
                        );
                }
                else
                {
                    desktopsUrl = publishedSet.DesktopsUrl;
                }

                this.SetSkytapVariableValue("DesktopsUrl", desktopsUrl);
            }
        }