Exemplo n.º 1
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);
            }
        }