AutoScalingCtrl.java 6.15 KB
package com.ui.ctrl;

import com.ui.cloud.CommAutoModifyScalingGroupReq;
import com.ui.cloud.DeployProjectCloud;
import com.ui.cloud.QcloudModifyScalingGroupReq;
import com.ui.contants.HttpUriContants;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import com.ui.model.domain.BuildMessage;
import com.ui.model.req.BuildRequest;
import com.ui.model.req.User;
import com.ui.project.ProjectEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 亚马逊云 腾讯云 伸缩组操作
 * Created by xh on 2017/5/9.
 */
@Controller
@RequestMapping("autoScalingTool")
public class AutoScalingCtrl {

    @Autowired
    private HttpRestClient httpClient;

    /**
     * 跳转到页面
     * @param model
     * @return
     */
    @RequestMapping("/toautoscalingpage")
    public ModelAndView toAutoScalingPage(Model model) {
        return new ModelAndView("manager/autoscaling");
    }

    /**
     * 显示配置信息
     * @param cloudType 云类型 1:AWS 2:QCLOUD
     * @return
     */
    @RequestMapping("/autoscalingconfig")
    @ResponseBody
    public BaseResponse<?> autoScalingConfig(int cloudType) {
        BaseResponse<?> response = httpClient.defaultPost(
                HttpUriContants.GET_CLOUD_AUTOSCALING_CONFIG + "?cloudType="+cloudType, null,
                BaseResponse.class);
        return response;
    }

    /**
     * 显示配置信息
     * @param qCloudReq 修改的相关参数,暂时只用了腾讯云的,日后考虑亚马逊的
     * @return
     */
    @RequestMapping("/modifyScalingGroup")
    @ResponseBody
    public BaseResponse<?> modifyScalingGroup(QcloudModifyScalingGroupReq qCloudReq, int cloudType) {
        CommAutoModifyScalingGroupReq req = new CommAutoModifyScalingGroupReq();
        req.setCloudType(cloudType);
        req.setQCloudParms(qCloudReq);
        BaseResponse<?> response = httpClient.defaultPost(
                HttpUriContants.GET_CLOUD_MODIFY_SCALING_GROUP, req, BaseResponse.class);
        return response;
    }

    /**
     * 腾讯云:修改伸缩组后的下一个流程是查询伸缩活动
     * 亚马逊云:修改伸缩组后的下一个流程待定,因为不清楚
     * @param scalingGroupId 待查询的伸缩组Id
     * @param startTime 开始时间,如果不填,则默认是当前时间前10秒之后发生的活动
     * @return
     */
    @RequestMapping("/describeScalingActivity")
    @ResponseBody
    public BaseResponse<?> describeScalingActivity(String scalingGroupId, String startTime){
        String url = String.format(HttpUriContants.GET_CLOUD_DESCRIBE_SCALING_ACTIVITY+"?scalingGroupId=%s&startTime=%s",scalingGroupId,startTime);
        BaseResponse<?> response = httpClient.defaultPost(url, null, BaseResponse.class);
        return response;
    }

    /**
     * 腾讯云:根据实例id获取内网ip
     * 亚马逊云待定:不清楚流程
     * @param ids 实例id集合
     * @return
     */
    @RequestMapping("/getQcloudDescribeInstances")
    @ResponseBody
    public BaseResponse<?> getQcloudDescribeInstances(String ids){
        String url = String.format(HttpUriContants.GET_QCLOUD_DESCRIBE_INSTANCES+"?instanceIds=%s",ids);
        BaseResponse<?> response = httpClient.defaultPost(url, null, BaseResponse.class);
        return response;
    }

    /**
     * 系统发布
     * @param session
     * @param projectName 项目名称
     * @param ips 所需发布的ip集合 ,分割
     * @param branch 分支名称
     * @param environment 发布环境 此为qcloud
     * @return
     */
    @RequestMapping("/deployCloudProject")
    @ResponseBody
    public BaseResponse<?> deployCloudProject(HttpSession session, String projectName, String ips, String branch, String environment){
        BaseResponse response = null;

        try{
            User user = (User) session.getAttribute("user");
            List<BuildMessage> list = new ArrayList<>();
            BuildRequest buildRequest = new BuildRequest();
            //分支名称
            buildRequest.setBranch(branch);
            //登陆用户
            buildRequest.setUser(user.getName());
            //设置需要发布的ip
            buildRequest.setHost(ips);
            //发布环境
            buildRequest.setEnvironment(environment);
            //不存在回滚文件
            //操作类型
            buildRequest.setOperate("Deploy");
            //关联工单
            buildRequest.setWorkid("release0");
            //项目名称
            buildRequest.setProject(projectName);

            String messageids = "";
            BuildMessage buildMessage = httpClient.post(ProjectEnvironment.getUrl(environment) + "build", buildRequest, BuildMessage.class);
            if (buildMessage != null) {
                list.add(buildMessage);
                messageids = messageids + buildMessage.getId() + ",";
            }
            DeployProjectCloud deploy = new DeployProjectCloud();
            deploy.setMessageids(messageids);
            deploy.setMessageList(list);
            response = new BaseResponse();
            response.setData(deploy);
        }catch (Exception e){
            response.setCode(300);
            response.setMessage(e.getMessage());
        }

        return response;
    }

    /**
     * 项目发布后集成测试
     * @param cloudType 云类型 1:AWS 2:QCLOUD
     * @param projectName 项目名称
     * @param ips 部署在哪台服务器上,内网ip集合, ,分割
     * @return
     */
    @RequestMapping("/integrationTest")
    @ResponseBody
    public BaseResponse<?> integrationTest(int cloudType, String projectName, String ips){
        String url = String.format(HttpUriContants.GET_CLOUD_INTEGRATION_TEST+"?cloudType=%s&projectName=%s&ips=%s",cloudType, projectName, ips);
        BaseResponse<?> response = httpClient.defaultPost(url, null, BaseResponse.class);
        return response;
    }
}