public async Task UpdateTxtRecord(DnsDTO data)
 {
     using (var dns = new DnsAdmin())
     {
         await dns.SetTxtRecord(data.TxtRecord, data.DomainName);
     }
 }
예제 #2
0
        public async Task <LabSettingsDTO> ResetTxtAssignment(TeamDTO team)
        {
            var res = new LabSettingsDTO();

            try
            {
                var data = await LabRepo.GetDomAssignment(team.Lab.LabCode, team.TeamAssignment.TeamAuth);

                //remove zone record from zone
                using (var dns = new DnsAdmin())
                {
                    var domGroup = await _repo.GetGroup(data.Lab.AzureSubscriptionId, data.Lab.DnsZoneRG);

                    await dns.InitAsync();

                    dns.SetClient(domGroup);
                    await dns.ClearTxtRecord(team.TeamAssignment.DomainName);
                }
                //update record in Cosmos
                var assignment = await LabRepo.GetDomAssignment(team.Lab.LabCode, team.TeamAssignment.TeamAuth);

                assignment.TeamAssignment.DnsTxtRecord = null;
                await LabRepo.UpdateTeamAssignment(assignment.TeamAssignment);

                res.ResponseMessage = "TXT record reset";
                res.Settings        = await LabRepo.GetLabAndSettings(team.Lab.Id);
            }
            catch (Exception ex)
            {
                res.ResponseMessage = "ERROR: " + ex.Message;
            }

            return(res);
        }
        /// <summary>
        /// called from the web job, removes assignments then deletes the lab
        /// </summary>
        /// <returns></returns>
        public static async Task RemoveLabAssignments(LabSettings lab, int endCount = 0)
        {
            var assignments = await GetDomAssignments(lab.Id);

            var counter = assignments.Count();

            try
            {
                var domGroup = (await DocDBRepo.DB <DomainResourceGroup> .GetItemsAsync(g => g.AzureSubscriptionId == lab.AzureSubscriptionId && g.DnsZoneRG == lab.DnsZoneRG)).SingleOrDefault();

                using (var dns = new DnsAdmin())
                {
                    await dns.InitAsync();

                    try
                    {
                        dns.SetClient(domGroup);
                    }
                    catch (Exception ex)
                    {
                        //we may have lost our auth
                        await Logging.WriteDebugInfoToErrorLog(string.Format("Error creating DNS client while deleting lab {0} - continuing.", lab.LabName), ex);
                    }
                    foreach (var item in assignments)
                    {
                        if (counter == endCount)
                        {
                            return;
                        }
                        try
                        {
                            await dns.RemoveChildZone(item.ParentZone, item.TeamName, item.DomainName);
                        }
                        catch (Exception ex)
                        {
                            //we may have lost our auth
                            await Logging.WriteDebugInfoToErrorLog(string.Format("Error deleting child zone {0} - continuing.", item.DomainName), ex);
                        }
                        await DocDBRepo.DB <DomAssignment> .DeleteItemAsync(item);

                        counter--;
                    }
                }

                //update lab
                lab.State = LabState.Ready;
                await DocDBRepo.DB <LabSettings> .UpdateItemAsync(lab);
            }
            catch (Exception ex)
            {
                await Logging.WriteDebugInfoToErrorLog(string.Format("Unable to remove domain assignments for labid {0}", lab.Id), ex);

                throw ex;
            }
        }
        public static async Task AddLabAssignments(LabSettings lab, int counter = 0)
        {
            var domGroup = (await DocDBRepo.DB <DomainResourceGroup> .GetItemsAsync(g => g.AzureSubscriptionId == lab.AzureSubscriptionId && g.DnsZoneRG == lab.DnsZoneRG)).SingleOrDefault();

            var    domains = domGroup.DomainList;
            string auth    = null;

            using (var dns = new DnsAdmin())
            {
                await dns.InitAsync();

                dns.SetClient(domGroup);
                var itemsPerDomain = (lab.AttendeeCount / domains.Count()) + 1;
                foreach (var dom in domains)
                {
                    if (counter == lab.AttendeeCount)
                    {
                        continue;
                    }

                    for (var x = 0; x < itemsPerDomain; x++)
                    {
                        if (counter == lab.AttendeeCount)
                        {
                            continue;
                        }

                        //create [itemsPerDomain] teams/child domains per parent domain name
                        var team = string.Format("{0}{1}", lab.LabName, (counter + 1));
                        auth = DomAssignment.GenAuthCode(team);
                        var newTeamItem = new DomAssignment
                        {
                            ParentZone    = dom,
                            TeamName      = team,
                            DomainName    = string.Format("{0}.{1}", team, dom),
                            TeamAuth      = auth,
                            LabCode       = lab.LabCode,
                            LabSettingsId = lab.Id
                        };
                        await DocDBRepo.DB <DomAssignment> .CreateItemAsync(newTeamItem);

                        await dns.CreateNewChildZone(newTeamItem.ParentZone, newTeamItem.TeamName, newTeamItem.DomainName);

                        counter++;
                    }
                }
            }

            //update lab
            lab.State = LabState.Ready;
            await DocDBRepo.DB <LabSettings> .UpdateItemAsync(lab);
        }
예제 #5
0
        public async Task <ActionResult> UpdateAssignment(DnsDTO item)
        {
            string labCode  = Session["labCode"].ToString();
            string teamCode = Session["teamCode"].ToString();

            var data = await LabRepo.GetDomAssignment(labCode, teamCode);

            var test = ContinueEditingAssignment(data);

            if (test != null)
            {
                ViewBag.ErrorHeader = test.ErrorHeader;
                ViewBag.Error       = test.Error;
                ViewBag.IsLive      = true;
                return(View("Index"));
            }
            try
            {
                //updating DNS record
                using (var dns = new DnsAdmin())
                {
                    var domGroup = await _repo.GetGroup(data.Lab.AzureSubscriptionId, data.Lab.DnsZoneRG);

                    await dns.InitAsync();

                    dns.SetClient(domGroup);
                    await dns.SetTxtRecord(item.TxtRecord, data.TeamAssignment.DomainName);
                };
                //updating
                data.TeamAssignment.DnsTxtRecord = item.TxtRecord;
                await LabRepo.UpdateDnsRecord(data);
            }
            catch (Exception ex)
            {
                ViewBag.ErrorHeader = "DNS Update Failed";
                ViewBag.Error       = ex.Message;
                item.TxtRecord      = "";
            }
            item.DomainName = data.TeamAssignment.DomainName;
            item.LabId      = data.Lab.Id;
            return(View("Index", item));
        }
예제 #6
0
        // Please set the following connection strings in app.config for this WebJob to run:
        // AzureWebJobsDashboard and AzureWebJobsStorage
        public static void Main()
        {
            try
            {
                _dns = new DnsAdmin();

                //Check for debug flag - this gives time to attach a remote debugger
                int iWait = int.Parse(ConfigurationManager.AppSettings["WebJobDebugWait"]);
                if (iWait > 0)
                {
                    while (!Debugger.IsAttached)
                    {
                        Thread.Sleep(100);
                    }
                }

                var dir = AppContext.BaseDirectory;

                // Settings.Init(ConfigurationManager.AppSettings, dir).GetAwaiter().GetResult();

                Init(ConfigurationManager.AppSettings, dir).GetAwaiter().GetResult();

                var config = new JobHostConfiguration
                {
                    DashboardConnectionString = Settings.StorageConnectionString,
                    StorageConnectionString   = Settings.StorageConnectionString
                };

                config.NameResolver = new CustomNameResolver();

                var host = new JobHost(config);
                // The following code ensures that the WebJob will be running continuously
                host.RunAndBlock();
            }
            catch (Exception ex)
            {
                Trace.Write(ex.Message);
                Logging.WriteToAppLog("Error initializing WebJob", EventLogEntryType.Error, ex);
                throw ex;
            }
        }