Exemplo n.º 1
0
        /// <summary>
        /// 获取某课程所有虚拟机列表 by zzw
        /// </summary>
        /// <param name="courseId"></param>
        /// <returns></returns>
        public static List <VMConfig> GetCourseVMs(int courseId)
        {
            List <Experiment> experiment = ExperimentDao.GetExperimentByCourseId(courseId);
            List <VMConfig>   configs    = GetAllVm();
            List <VMConfig>   configs1   = new List <VMConfig>();
            List <string>     apply_s    = new List <string>();

            foreach (Experiment experiment1 in experiment)
            {
                apply_s.Add(experiment1.vm_name);
            }
            foreach (VMConfig config in configs)
            {
                String name = config.Name;
                if (name == null)
                {
                    continue;
                }
                foreach (string apply in apply_s)
                {
                    if (name.StartsWith(apply + "_"))
                    {
                        configs1.Add(config);
                        break;
                    }
                }
            }
            return(configs1);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取可使用的虚拟机列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static List <VMConfig> GetUsingVMs(string userId)
        {
            List <VMConfig> configs  = GetAllVm();
            List <VMConfig> configs1 = new List <VMConfig>();

            configs1 = configs.Where(c => c.is_exps.Equals("True")).ToList();

            User user = UserDao.GetUserById(userId);

            if (user == null)
            {
                return(null);
            }
            if (user.role == 1)
            {
                configs  = configs1;
                configs1 = new List <VMConfig>();
                foreach (VMConfig fig in configs)
                {
                    if (fig.student_id != null && fig.student_id.Equals(user.id))
                    {
                        configs1.Add(fig);
                    }
                }
                var expList = ExperimentDao.GetExperimentByStuId(user.id).Where(e => e.vm_passwd != null).ToList();
                foreach (var e in expList)
                {
                    var expVM = configs1.Where(c => c.Name.StartsWith(e.vm_name)).ToList();
                    expVM.ForEach(v => v.AdvancedConfig = e.vm_passwd);
                }
                return(configs1);
            }
            else if (user.role == 4)
            {
                return(configs1);
            }
            else if (user.role == 2)
            {
                configs  = configs1;
                configs1 = new List <VMConfig>();
                foreach (VMConfig config in configs)
                {
                    User user1 = UserDao.GetUserById(config.student_id);
                    if (user1.department_id.Equals(user.department_id))
                    {
                        configs1.Add(config);
                    }
                }
                return(configs1);
            }
            return(null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 为实验创建虚拟机
        /// </summary>
        /// <param name="vmConfig">虚拟机配置</param>
        /// <param name="vmName">虚拟机名</param>
        /// <param name="expId">实验id</param>
        /// <returns></returns>
        public static string CreateVMsForExp(VMConfig vmConfig, int applyId, string stulist)
        {
            Experiment exp = ExperimentDao.GetExperimentByApplyId(applyId);

            Course course = CourseDao.GetCourseInfoById((int)exp.course_id);

            vmConfig.Name = exp.vm_name;
            string teacherId = course.teacher_id;

            string[]      ids    = stulist.Split(' ');
            List <string> stuIds = new List <string>();

            foreach (string id in ids)
            {
                if (id != null && id.Length != 0)
                {
                    stuIds.Add(id);
                }
            }
            string[] names = new string[stuIds.Count()];
            string   res   = "success";


            for (int i = 0; i < stuIds.Count(); ++i)
            {
                names[i] = vmConfig.Name + "_" + stuIds[i];
            }
            if (ManagerType.Equals("Sangfor"))
            {
                List <SangforInfo> infoList = new List <SangforInfo>();
                for (int i = 0; i < stuIds.Count(); ++i)
                {
                    SangforInfo info = new SangforInfo();
                    info.student_id   = stuIds[i];
                    info.teacher_id   = teacherId;
                    info.IsTemplate   = false;
                    info.is_exp       = true;
                    info.is_exps      = "True";
                    info.Name         = names[i];
                    info.TemplateName = vmConfig.TemplateName;
                    infoList.Add(info);
                }
                //vmConfig.Disk = vmConfig.Disk / 1024;

                foreach (SangforInfo info in infoList)
                {
                    string result = restful.Create(vmConfig, info.Name, null, null);
                    if (!result.StartsWith("error"))
                    {
                        info.id = result;
                        SangforDao.Add(info);
                    }
                }
                // 使用新逻辑创建,优化创建时间
                //restful.CreateNewLogic(vmConfig,infoList);
            }
            else
            {
                res = manager.Create(vmConfig, names.ToList(), stuIds.ToList(), teacherId);
            }

            return(res);
        }