private Site CreateNewSite(string suffix)
        {
            var webspaceList = WebsitesClient.ListWebSpaces();

            if (Git && webspaceList.Count == 0)
            {
                string error = string.Format(Resources.PortalInstructions, Name);
                throw new Exception(string.Format("{0}\n{1}", error, Resources.PortalInstructionsGit));
            }

            WebSpace webspace = FindWebSpace(webspaceList);

            var website = new SiteWithWebSpace
            {
                Name             = Name,
                WebSpace         = webspace.Name,
                WebSpaceToCreate = webspace
            };
            Site result;

            try
            {
                result = CreateSite(webspace, website);
            }
            catch (EndpointNotFoundException)
            {
                // Create webspace with VirtualPlan failed, try with subscription id
                // This supports Windows Azure Pack
                webspace.Plan = CurrentContext.Subscription.Id.ToString();
                result        = CreateSite(webspace, website);
            }
            return(result);
        }
Exemple #2
0
        private void CreateSite(WebSpace webspace, SiteWithWebSpace website)
        {
            try
            {
                InvokeInOperationContext(() => RetryCall(s => Channel.CreateSite(s, webspace.Name, website)));

                Cache.AddSite(CurrentSubscription.SubscriptionId, website);
                SiteConfig websiteConfiguration = null;
                InvokeInOperationContext(() =>
                {
                    websiteConfiguration = RetryCall(s => Channel.GetSiteConfig(s, website.WebSpace, website.Name));
                    WaitForOperation(CommandRuntime.ToString());
                });
                WriteObject(new SiteWithConfig(website, websiteConfiguration));
            }
            catch (ProtocolException ex)
            {
                // Handle site creating indepently so that cmdlet is idempotent.
                string message = ProcessException(ex, false);
                if (message.Equals(string.Format(Resources.WebsiteAlreadyExistsReplacement,
                                                 Name)) && (Git || GitHub))
                {
                    WriteWarning(message);
                }
                else if (message.Equals(Resources.DefaultHostnamesValidation))
                {
                    WriteExceptionError(new Exception(Resources.InvalidHostnameValidation));
                }
                else
                {
                    WriteExceptionError(new Exception(message));
                }
            }
        }
Exemple #3
0
 private void CreateSite(WebSpace webspace, SiteWithWebSpace website)
 {
     try
     {
         WebsitesClient.CreateWebsite(webspace.Name, website);
         Cache.AddSite(CurrentSubscription.SubscriptionId, website);
         SiteConfig websiteConfiguration = WebsitesClient.GetWebsiteConfiguration(Name);
         WriteObject(new SiteWithConfig(website, websiteConfiguration));
     }
     catch (CloudException ex)
     {
         if (SiteAlreadyExists(ex) && (Git || GitHub))
         {
             // Handle conflict - it's ok to attempt to use cmdlet on an
             // existing website if you're updating the source control stuff.
             WriteWarning(ex.Message);
         }
         else if (HostNameValidationFailed(ex))
         {
             WriteExceptionError(new Exception(Resources.InvalidHostnameValidation));
         }
         else if (BadPlan(ex))
         {
             throw new EndpointNotFoundException();
         }
         else
         {
             WriteExceptionError(new Exception(ex.Message));
         }
     }
 }
        private Site CreateSite(WebSpace webspace, SiteWithWebSpace website)
        {
            Site createdWebsite = null;

            try
            {
                if (WebsitesClient.WebsiteExists(website.Name) && !string.IsNullOrEmpty(Slot))
                {
                    createdWebsite = WebsitesClient.GetWebsite(website.Name);

                    // Make sure that the website is in Standard mode
                    if (createdWebsite.ComputeMode == WebSiteComputeMode.Dedicated)
                    {
                        WebsitesClient.CreateWebsite(webspace.Name, website, Slot);
                    }
                    else
                    {
                        throw new Exception("Can not create slot in a website not in Standard mode");
                    }
                }
                else
                {
                    WebsitesClient.CreateWebsite(webspace.Name, website, null);
                }

                createdWebsite = WebsitesClient.GetWebsite(website.Name);

                Cache.AddSite(CurrentContext.Subscription.Id.ToString(), createdWebsite);
                SiteConfig websiteConfiguration = WebsitesClient.GetWebsiteConfiguration(createdWebsite.Name, Slot);
                WriteObject(new SiteWithConfig(createdWebsite, websiteConfiguration));
            }
            catch (CloudException ex)
            {
                if (SiteAlreadyExists(ex) && (Git || GitHub))
                {
                    // Handle conflict - it's ok to attempt to use cmdlet on an
                    // existing website if you're updating the source control stuff.
                    WriteWarning(ex.Message);
                    createdWebsite = WebsitesClient.GetWebsite(website.Name, null);
                }
                else if (HostNameValidationFailed(ex))
                {
                    WriteExceptionError(new Exception(Resources.InvalidHostnameValidation));
                }
                else if (BadPlan(ex))
                {
                    throw new EndpointNotFoundException();
                }
                else
                {
                    WriteExceptionError(new Exception(ex.Message));
                }
            }

            return(createdWebsite);
        }
Exemple #5
0
        /// <summary>
        /// Create a new website.
        /// </summary>
        /// <param name="webspaceName">Web space to create site in.</param>
        /// <param name="siteToCreate">Details about the site to create.</param>
        /// <returns></returns>
        public Site CreateWebsite(string webspaceName, SiteWithWebSpace siteToCreate)
        {
            var options = new WebSiteCreateParameters
            {
                Name         = siteToCreate.Name,
                WebSpaceName = siteToCreate.WebSpaceToCreate.Name,
                WebSpace     = new WebSiteCreateParameters.WebSpaceDetails
                {
                    GeoRegion = siteToCreate.WebSpaceToCreate.GeoRegion,
                    Name      = siteToCreate.WebSpaceToCreate.Name,
                    Plan      = siteToCreate.WebSpaceToCreate.Plan,
                }
            };

            siteToCreate.HostNames.ForEach(s => options.HostNames.Add(s));

            var response = WebsiteManagementClient.WebSites.Create(webspaceName, options);

            return(response.WebSite.ToSite());
        }
        public override void ExecuteCmdlet()
        {
            if (Git && GitHub)
            {
                throw new Exception("Please run the command with either -Git or -GitHub options. Not both.");
            }

            if (Git)
            {
                PublishingUsername = GetPublishingUser();
            }

            WebSpaces webspaceList = null;
            InvokeInOperationContext(() => { webspaceList = RetryCall(s => Channel.GetWebSpacesWithCache(s)); });
            if (Git && webspaceList.Count == 0)
            {
                // If location is still empty or null, give portal instructions.
                string error = string.Format(Resources.PortalInstructions, Name);
                throw new Exception(!Git
                    ? error
                    : string.Format("{0}\n{1}", error, Resources.PortalInstructionsGit));
            }

            WebSpace webspace = null;
            if (string.IsNullOrEmpty(Location))
            {
                // If no location was provided as a parameter, try to default it
                webspace = webspaceList.FirstOrDefault();
                if (webspace == null)
                {
                    // Use east us
                    webspace = new WebSpace
                    {
                        Name = "eastuswebspace",
                        GeoRegion = "East US",
                        Subscription = CurrentSubscription.SubscriptionId,
                        Plan = "VirtualDedicatedPlan"
                    };
                }
            }
            else
            {
                // Find the webspace that corresponds to the georegion
                webspace = webspaceList.FirstOrDefault(w => w.GeoRegion.Equals(Location, StringComparison.OrdinalIgnoreCase));
                if (webspace == null)
                {
                    // If no webspace corresponding to the georegion was found, attempt to create it
                    webspace = new WebSpace
                    {
                        Name = Regex.Replace(Location.ToLower(), " ", "") + "webspace",
                        GeoRegion = Location,
                        Subscription = CurrentSubscription.SubscriptionId,
                        Plan = "VirtualDedicatedPlan"
                    };
                }
            }

            SiteWithWebSpace website = new SiteWithWebSpace
            {
                Name = Name,
                HostNames = new[] { Name + General.AzureWebsiteHostNameSuffix },
                WebSpace = webspace.Name,
                WebSpaceToCreate = webspace
            };

            if (!string.IsNullOrEmpty(Hostname))
            {
                List<string> newHostNames = new List<string>(website.HostNames);
                newHostNames.Add(Hostname);
                website.HostNames = newHostNames.ToArray();
            }

            try
            {
                InvokeInOperationContext(() => RetryCall(s => Channel.CreateSite(s, webspace.Name, website)));

                // If operation succeeded try to update cache with new webspace if that's the case
                if (webspaceList.FirstOrDefault(ws => ws.Name.Equals(webspace.Name)) == null)
                {
                    Cache.AddWebSpace(CurrentSubscription.SubscriptionId, webspace);
                }

                Cache.AddSite(CurrentSubscription.SubscriptionId, website);
                SiteConfig websiteConfiguration = null;
                InvokeInOperationContext(() =>
                {
                    websiteConfiguration = RetryCall(s => Channel.GetSiteConfig(s, website.WebSpace, website.Name));
                    WaitForOperation(CommandRuntime.ToString());
                });
                WriteObject(new SiteWithConfig(website, websiteConfiguration));
            }
            catch (ProtocolException ex)
            {
                // Handle site creating indepently so that cmdlet is idempotent.
                string message = ProcessException(ex, false);
                if (message.Equals(string.Format(Resources.WebsiteAlreadyExistsReplacement,
                                                 Name)) && (Git || GitHub))
                {
                    WriteWarning(message);
                }
                else if (message.Equals(Resources.DefaultHostnamesValidation))
                {
                    WriteExceptionError(new Exception(Resources.InvalidHostnameValidation));
                }
                else
                {
                    WriteExceptionError(new Exception(message));
                }
            }

            if (Git || GitHub)
            {
                try
                {
                    Directory.SetCurrentDirectory(SessionState.Path.CurrentFileSystemLocation.Path);
                }
                catch (Exception)
                {
                    // Do nothing if session state is not present
                }

                LinkedRevisionControl linkedRevisionControl = null;
                if (Git)
                {
                    linkedRevisionControl = new GitClient(this);
                }
                else if (GitHub)
                {
                    linkedRevisionControl = new GithubClient(this, GithubCredentials, GithubRepository);
                }

                linkedRevisionControl.Init();

                CopyIisNodeWhenServerJsPresent();
                UpdateLocalConfigWithSiteName(Name, webspace.Name);

                InitializeRemoteRepo(webspace.Name, Name);

                Site updatedWebsite = RetryCall(s => Channel.GetSite(s, webspace.Name, Name, "repositoryuri,publishingpassword,publishingusername"));
                if (Git)
                {
                    AddRemoteToLocalGitRepo(updatedWebsite);
                }

                linkedRevisionControl.Deploy(updatedWebsite);
                linkedRevisionControl.Dispose();
            }
        }
 private void CreateSite(WebSpace webspace, SiteWithWebSpace website)
 {
     try
     {
         WebsitesClient.CreateWebsite(webspace.Name, website);
         Cache.AddSite(CurrentSubscription.SubscriptionId, website);
         SiteConfig websiteConfiguration = WebsitesClient.GetWebsiteConfiguration(Name);
         WriteObject(new SiteWithConfig(website, websiteConfiguration));
     }
     catch (CloudException ex)
     {
         if (SiteAlreadyExists(ex) && (Git || GitHub))
         {
             // Handle conflict - it's ok to attempt to use cmdlet on an
             // existing website if you're updating the source control stuff.
             WriteWarning(ex.Message);
         }
         else if (HostNameValidationFailed(ex))
         {
             WriteExceptionError(new Exception(Resources.InvalidHostnameValidation));
         }
         else if (BadPlan(ex))
         {
             throw new EndpointNotFoundException();
         }
         else
         {
             WriteExceptionError(new Exception(ex.Message));
         }
     }
 }
        private WebSpace CreateNewSite(string suffix)
        {
            var webspaceList = WebsitesClient.ListWebSpaces();
            if (Git && webspaceList.Count == 0)
            {
                string error = string.Format(Resources.PortalInstructions, Name);
                throw new Exception(string.Format("{0}\n{1}", error, Resources.PortalInstructionsGit));
            }

            WebSpace webspace = FindWebSpace(webspaceList);

            var website = new SiteWithWebSpace
            {
                Name = Name,
                HostNames = BuildHostNames(suffix),
                WebSpace = webspace.Name,
                WebSpaceToCreate = webspace
            };

            try
            {
                CreateSite(webspace, website);
            }
            catch (EndpointNotFoundException)
            {
                // Create webspace with VirtualPlan failed, try with subscription id
                // This supports Windows Azure Pack
                webspace.Plan = CurrentSubscription.SubscriptionId;
                CreateSite(webspace, website);
            }
            return webspace;
        }
        /// <summary>
        /// Create a new website in production.
        /// </summary>
        /// <param name="webspaceName">Web space to create site in.</param>
        /// <param name="siteToCreate">Details about the site to create.</param>
        /// <returns>The created site object</returns>
        private Site CreateWebsite(string webspaceName, SiteWithWebSpace siteToCreate)
        {
            var options = new WebSiteCreateParameters
            {
                Name = siteToCreate.Name,
                WebSpaceName = siteToCreate.WebSpaceToCreate.Name,
                WebSpace = new WebSiteCreateParameters.WebSpaceDetails
                {
                    GeoRegion = siteToCreate.WebSpaceToCreate.GeoRegion,
                    Name = siteToCreate.WebSpaceToCreate.Name,
                    Plan = siteToCreate.WebSpaceToCreate.Plan
                }
            };

            siteToCreate.HostNames.ForEach(s => options.HostNames.Add(s));

            var response = WebsiteManagementClient.WebSites.Create(webspaceName, options);
            return response.WebSite.ToSite();
        }
 /// <summary>
 /// Create a new website.
 /// </summary>
 /// <param name="webspaceName">Web space to create site in.</param>
 /// <param name="siteToCreate">Details about the site to create.</param>
 /// <param name="slot">The slot name.</param>
 /// <returns>The created site object</returns>
 public Site CreateWebsite(string webspaceName, SiteWithWebSpace siteToCreate, string slot)
 {
     siteToCreate.Name = SetWebsiteName(siteToCreate.Name, slot);
     string[] hostNames = { GetHostName(siteToCreate.Name, slot) };
     siteToCreate.HostNames = hostNames;
     return CreateWebsite(webspaceName, siteToCreate);
 }
        private Site CreateSite(WebSpace webspace, SiteWithWebSpace website)
        {
            Site createdWebsite = null;

            try
            {
                if (WebsitesClient.WebsiteExists(website.Name) && !string.IsNullOrEmpty(Slot))
                {
                    createdWebsite = WebsitesClient.GetWebsite(website.Name);

                    // API makes sure site is in Standard mode
                    WebsitesClient.CreateWebsite(createdWebsite.WebSpace, website, Slot);
                }
                else
                {
                    WebsitesClient.CreateWebsite(webspace.Name, website, null);
                }

                createdWebsite = WebsitesClient.GetWebsite(website.Name);

                Cache.AddSite(CurrentContext.Subscription.Id.ToString(), createdWebsite);
                SiteConfig websiteConfiguration = WebsitesClient.GetWebsiteConfiguration(createdWebsite.Name, Slot);
                WriteObject(new SiteWithConfig(createdWebsite, websiteConfiguration));
            }
            catch (CloudException ex)
            {
                if (SiteAlreadyExists(ex) && (Git || GitHub))
                {
                    // Handle conflict - it's ok to attempt to use cmdlet on an
                    // existing website if you're updating the source control stuff.
                    WriteWarning(ex.Message);
                    createdWebsite = WebsitesClient.GetWebsite(website.Name, null);
                }
                else if (HostNameValidationFailed(ex))
                {
                    WriteExceptionError(new Exception(Resources.InvalidHostnameValidation));
                }
                else if (BadPlan(ex))
                {
                    throw new EndpointNotFoundException();
                }
                else
                {
                    WriteExceptionError(new Exception(ex.Message));
                }
            }

            return createdWebsite;
        }
Exemple #12
0
        public override void ExecuteCmdlet()
        {
            WebsitesClient = WebsitesClient ?? new WebsitesClient(CurrentSubscription, WriteDebug);
            string suffix = WebsitesClient.GetWebsiteDnsSuffix();

            if (Git && GitHub)
            {
                throw new Exception("Please run the command with either -Git or -GitHub options. Not both.");
            }

            if (Git)
            {
                PublishingUsername = GetPublishingUser();
            }

            WebSpaces webspaceList = null;

            InvokeInOperationContext(() => { webspaceList = RetryCall(s => Channel.GetWebSpaces(s)); });
            if (Git && webspaceList.Count == 0)
            {
                // If location is still empty or null, give portal instructions.
                string error = string.Format(Resources.PortalInstructions, Name);
                throw new Exception(!Git
                    ? error
                    : string.Format("{0}\n{1}", error, Resources.PortalInstructionsGit));
            }

            WebSpace webspace = null;

            if (string.IsNullOrEmpty(Location))
            {
                // If no location was provided as a parameter, try to default it
                webspace = webspaceList.FirstOrDefault();
                if (webspace == null)
                {
                    string defaultLocation;

                    try
                    {
                        defaultLocation = WebsitesClient.GetDefaultLocation();
                    }
                    catch
                    {
                        throw new Exception(Resources.CreateWebsiteFailed);
                    }

                    webspace = new WebSpace
                    {
                        Name         = Regex.Replace(defaultLocation.ToLower(), " ", "") + "webspace",
                        GeoRegion    = defaultLocation,
                        Subscription = CurrentSubscription.SubscriptionId,
                        Plan         = "VirtualDedicatedPlan"
                    };
                }
            }
            else
            {
                // Find the webspace that corresponds to the georegion
                webspace = webspaceList.FirstOrDefault(w => w.GeoRegion.Equals(Location, StringComparison.OrdinalIgnoreCase));
                if (webspace == null)
                {
                    // If no webspace corresponding to the georegion was found, attempt to create it
                    webspace = new WebSpace
                    {
                        Name         = Regex.Replace(Location.ToLower(), " ", "") + "webspace",
                        GeoRegion    = Location,
                        Subscription = CurrentSubscription.SubscriptionId,
                        Plan         = "VirtualDedicatedPlan"
                    };
                }
            }

            SiteWithWebSpace website = new SiteWithWebSpace
            {
                Name             = Name,
                HostNames        = new[] { string.Format("{0}.{1}", Name, suffix) },
                WebSpace         = webspace.Name,
                WebSpaceToCreate = webspace
            };

            if (!string.IsNullOrEmpty(Hostname))
            {
                List <string> newHostNames = new List <string>(website.HostNames);
                newHostNames.Add(Hostname);
                website.HostNames = newHostNames.ToArray();
            }

            try
            {
                CreateSite(webspace, website);
            }
            catch (EndpointNotFoundException)
            {
                // Create webspace with VirtualPlan failed, try with subscription id
                webspace.Plan = CurrentSubscription.SubscriptionId;
                CreateSite(webspace, website);
            }

            if (Git || GitHub)
            {
                try
                {
                    Directory.SetCurrentDirectory(SessionState.Path.CurrentFileSystemLocation.Path);
                }
                catch (Exception)
                {
                    // Do nothing if session state is not present
                }

                LinkedRevisionControl linkedRevisionControl = null;
                if (Git)
                {
                    linkedRevisionControl = new GitClient(this);
                }
                else if (GitHub)
                {
                    linkedRevisionControl = new GithubClient(this, GithubCredentials, GithubRepository);
                }

                linkedRevisionControl.Init();

                CopyIisNodeWhenServerJsPresent();
                UpdateLocalConfigWithSiteName(Name, webspace.Name);

                InitializeRemoteRepo(webspace.Name, Name);

                Site updatedWebsite = RetryCall(s => Channel.GetSite(s, webspace.Name, Name, "repositoryuri,publishingpassword,publishingusername"));
                if (Git)
                {
                    AddRemoteToLocalGitRepo(updatedWebsite);
                }

                linkedRevisionControl.Deploy(updatedWebsite);
                linkedRevisionControl.Dispose();
            }
        }
        private void CreateSite(WebSpace webspace, SiteWithWebSpace website)
        {
            try
            {
                InvokeInOperationContext(() => RetryCall(s => Channel.CreateSite(s, webspace.Name, website)));

                Cache.AddSite(CurrentSubscription.SubscriptionId, website);
                SiteConfig websiteConfiguration = null;
                InvokeInOperationContext(() =>
                {
                    websiteConfiguration = RetryCall(s => Channel.GetSiteConfig(s, website.WebSpace, website.Name));
                    WaitForOperation(CommandRuntime.ToString());
                });
                WriteObject(new SiteWithConfig(website, websiteConfiguration));
            }
            catch (ProtocolException ex)
            {
                // Handle site creating indepently so that cmdlet is idempotent.
                string message = ProcessException(ex, false);
                if (message.Equals(string.Format(Resources.WebsiteAlreadyExistsReplacement,
                                                 Name)) && (Git || GitHub))
                {
                    WriteWarning(message);
                }
                else if (message.Equals(Resources.DefaultHostnamesValidation))
                {
                    WriteExceptionError(new Exception(Resources.InvalidHostnameValidation));
                }
                else
                {
                    WriteExceptionError(new Exception(message));
                }
            }
        }
        public override void ExecuteCmdlet()
        {
            WebsitesClient = WebsitesClient ?? new WebsitesClient(CurrentSubscription, WriteDebug);
            string suffix = WebsitesClient.GetWebsiteDnsSuffix();

            if (Git && GitHub)
            {
                throw new Exception("Please run the command with either -Git or -GitHub options. Not both.");
            }

            if (Git)
            {
                PublishingUsername = GetPublishingUser();
            }

            WebSpaces webspaceList = null;
            InvokeInOperationContext(() => { webspaceList = RetryCall(s => Channel.GetWebSpaces(s)); });
            if (Git && webspaceList.Count == 0)
            {
                // If location is still empty or null, give portal instructions.
                string error = string.Format(Resources.PortalInstructions, Name);
                throw new Exception(!Git
                    ? error
                    : string.Format("{0}\n{1}", error, Resources.PortalInstructionsGit));
            }

            WebSpace webspace = null;
            if (string.IsNullOrEmpty(Location))
            {
                // If no location was provided as a parameter, try to default it
                webspace = webspaceList.FirstOrDefault();
                if (webspace == null)
                {
                    string defaultLocation;

                    try
                    {
                        defaultLocation = WebsitesClient.GetDefaultLocation();
                    }
                    catch
                    {
                        throw new Exception(Resources.CreateWebsiteFailed);
                    }

                    webspace = new WebSpace
                    {
                        Name = Regex.Replace(defaultLocation.ToLower(), " ", "") + "webspace",
                        GeoRegion = defaultLocation,
                        Subscription = CurrentSubscription.SubscriptionId,
                        Plan = "VirtualDedicatedPlan"
                    };
                }
            }
            else
            {
                // Find the webspace that corresponds to the georegion
                webspace = webspaceList.FirstOrDefault(w => w.GeoRegion.Equals(Location, StringComparison.OrdinalIgnoreCase));
                if (webspace == null)
                {
                    // If no webspace corresponding to the georegion was found, attempt to create it
                    webspace = new WebSpace
                    {
                        Name = Regex.Replace(Location.ToLower(), " ", "") + "webspace",
                        GeoRegion = Location,
                        Subscription = CurrentSubscription.SubscriptionId,
                        Plan = "VirtualDedicatedPlan"
                    };
                }
            }

            SiteWithWebSpace website = new SiteWithWebSpace
            {
                Name = Name,
                HostNames = new[] { string.Format("{0}.{1}", Name, suffix) },
                WebSpace = webspace.Name,
                WebSpaceToCreate = webspace
            };

            if (!string.IsNullOrEmpty(Hostname))
            {
                List<string> newHostNames = new List<string>(website.HostNames);
                newHostNames.Add(Hostname);
                website.HostNames = newHostNames.ToArray();
            }

            try
            {
                CreateSite(webspace, website);
            }
            catch (EndpointNotFoundException)
            {
                // Create webspace with VirtualPlan failed, try with subscription id
                webspace.Plan = CurrentSubscription.SubscriptionId;
                CreateSite(webspace, website);
            }

            if (Git || GitHub)
            {
                try
                {
                    Directory.SetCurrentDirectory(SessionState.Path.CurrentFileSystemLocation.Path);
                }
                catch (Exception)
                {
                    // Do nothing if session state is not present
                }

                LinkedRevisionControl linkedRevisionControl = null;
                if (Git)
                {
                    linkedRevisionControl = new GitClient(this);
                }
                else if (GitHub)
                {
                    linkedRevisionControl = new GithubClient(this, GithubCredentials, GithubRepository);
                }

                linkedRevisionControl.Init();

                CopyIisNodeWhenServerJsPresent();
                UpdateLocalConfigWithSiteName(Name, webspace.Name);

                InitializeRemoteRepo(webspace.Name, Name);

                Site updatedWebsite = RetryCall(s => Channel.GetSite(s, webspace.Name, Name, "repositoryuri,publishingpassword,publishingusername"));
                if (Git)
                {
                    AddRemoteToLocalGitRepo(updatedWebsite);
                }

                linkedRevisionControl.Deploy(updatedWebsite);
                linkedRevisionControl.Dispose();
            }
        }
        private Site CreateNewSite(string suffix)
        {
            var webspaceList = WebsitesClient.ListWebSpaces();
            if (Git && webspaceList.Count == 0)
            {
                string error = string.Format(Resources.PortalInstructions, Name);
                throw new Exception(string.Format("{0}\n{1}", error, Resources.PortalInstructionsGit));
            }

            WebSpace webspace = FindWebSpace(webspaceList);

            var website = new SiteWithWebSpace
            {
                Name = Name,
                WebSpace = webspace.Name,
                WebSpaceToCreate = webspace
            };
            Site result;

            try
            {
                result = CreateSite(webspace, website);
            }
            catch (EndpointNotFoundException)
            {
                // Create webspace with VirtualPlan failed, try with subscription id
                // This supports Windows Azure Pack
                webspace.Plan = Profile.Context.Subscription.Id.ToString();
                result = CreateSite(webspace, website);
            }
            return result;
        }
Exemple #16
0
        public override void ExecuteCmdlet()
        {
            WebsitesClient = WebsitesClient ?? new WebsitesClient(CurrentSubscription, WriteDebug);
            string suffix = WebsitesClient.GetWebsiteDnsSuffix();

            if (Git && GitHub)
            {
                throw new Exception("Please run the command with either -Git or -GitHub options. Not both.");
            }

            if (Git)
            {
                PublishingUsername = GetPublishingUser();
            }

            WebSpaces webspaceList = null;

            InvokeInOperationContext(() => { webspaceList = RetryCall(s => Channel.GetWebSpacesWithCache(s)); });
            if (Git && webspaceList.Count == 0)
            {
                // If location is still empty or null, give portal instructions.
                string error = string.Format(Resources.PortalInstructions, Name);
                throw new Exception(!Git
                    ? error
                    : string.Format("{0}\n{1}", error, Resources.PortalInstructionsGit));
            }

            WebSpace webspace = null;

            if (string.IsNullOrEmpty(Location))
            {
                // If no location was provided as a parameter, try to default it
                webspace = webspaceList.FirstOrDefault();
                if (webspace == null)
                {
                    webspace = new WebSpace
                    {
                        GeoRegion    = WebsitesClient.GetDefaultLocation(),
                        Subscription = CurrentSubscription.SubscriptionId,
                        Plan         = "VirtualDedicatedPlan"
                    };
                }
            }
            else
            {
                // Find the webspace that corresponds to the georegion
                webspace = webspaceList.FirstOrDefault(w => w.GeoRegion.Equals(Location, StringComparison.OrdinalIgnoreCase));
                if (webspace == null)
                {
                    // If no webspace corresponding to the georegion was found, attempt to create it
                    webspace = new WebSpace
                    {
                        Name         = Regex.Replace(Location.ToLower(), " ", "") + "webspace",
                        GeoRegion    = Location,
                        Subscription = CurrentSubscription.SubscriptionId,
                        Plan         = "VirtualDedicatedPlan"
                    };
                }
            }

            SiteWithWebSpace website = new SiteWithWebSpace
            {
                Name             = Name,
                HostNames        = new[] { string.Format("{0}.{1}", Name, suffix) },
                WebSpace         = webspace.Name,
                WebSpaceToCreate = webspace
            };

            if (!string.IsNullOrEmpty(Hostname))
            {
                List <string> newHostNames = new List <string>(website.HostNames);
                newHostNames.Add(Hostname);
                website.HostNames = newHostNames.ToArray();
            }

            try
            {
                InvokeInOperationContext(() => RetryCall(s => Channel.CreateSite(s, webspace.Name, website)));

                // If operation succeeded try to update cache with new webspace if that's the case
                if (webspaceList.FirstOrDefault(ws => ws.Name.Equals(webspace.Name)) == null)
                {
                    Cache.AddWebSpace(CurrentSubscription.SubscriptionId, webspace);
                }

                Cache.AddSite(CurrentSubscription.SubscriptionId, website);
                SiteConfig websiteConfiguration = null;
                InvokeInOperationContext(() =>
                {
                    websiteConfiguration = RetryCall(s => Channel.GetSiteConfig(s, website.WebSpace, website.Name));
                    WaitForOperation(CommandRuntime.ToString());
                });
                WriteObject(new SiteWithConfig(website, websiteConfiguration));
            }
            catch (ProtocolException ex)
            {
                // Handle site creating indepently so that cmdlet is idempotent.
                string message = ProcessException(ex, false);
                if (message.Equals(string.Format(Resources.WebsiteAlreadyExistsReplacement,
                                                 Name)) && (Git || GitHub))
                {
                    WriteWarning(message);
                }
                else if (message.Equals(Resources.DefaultHostnamesValidation))
                {
                    WriteExceptionError(new Exception(Resources.InvalidHostnameValidation));
                }
                else
                {
                    WriteExceptionError(new Exception(message));
                }
            }

            if (Git || GitHub)
            {
                try
                {
                    Directory.SetCurrentDirectory(SessionState.Path.CurrentFileSystemLocation.Path);
                }
                catch (Exception)
                {
                    // Do nothing if session state is not present
                }

                LinkedRevisionControl linkedRevisionControl = null;
                if (Git)
                {
                    linkedRevisionControl = new GitClient(this);
                }
                else if (GitHub)
                {
                    linkedRevisionControl = new GithubClient(this, GithubCredentials, GithubRepository);
                }

                linkedRevisionControl.Init();

                CopyIisNodeWhenServerJsPresent();
                UpdateLocalConfigWithSiteName(Name, webspace.Name);

                InitializeRemoteRepo(webspace.Name, Name);

                Site updatedWebsite = RetryCall(s => Channel.GetSite(s, webspace.Name, Name, "repositoryuri,publishingpassword,publishingusername"));
                if (Git)
                {
                    AddRemoteToLocalGitRepo(updatedWebsite);
                }

                linkedRevisionControl.Deploy(updatedWebsite);
                linkedRevisionControl.Dispose();
            }
        }