Exemplo n.º 1
0
/** @author yama */
public abstract class RequestWorker {
  private static Logger logger = LoggerFactory.get(RequestWorker.class);
  //
  public static final String HTTP_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
  public static final String HTTP_DATE_GMT_TIMEZONE = "GMT";
  public static final int HTTP_CACHE_SECONDS = 60;
  protected ChannelHandlerContext ctx;
  protected DefaultHttpRequest request;
  protected CdnServer cdnServer;
  protected File requestFile;
  protected FilterContext filterCtx;
  //
  RequestWorker(CdnServer cdnServer, ChannelHandlerContext ctx, DefaultHttpRequest request) {
    this.cdnServer = cdnServer;
    this.ctx = ctx;
    this.request = request;
  }
  //
  protected boolean filter() throws Exception {
    if (!request.decoderResult().isSuccess()) {
      if (logger.isDebugEnabled()) {
        logger.debug("bad request");
      }
      sendError(ctx, BAD_REQUEST);
      return false;
    }
    requestFile = new File(cdnServer.getHomeDir(), request.uri());
    if (cdnServer.requestFilter != null) {
      filterCtx = new FilterContext(requestFile);
      filterCtx.request = request;
      cdnServer.requestFilter.filter(filterCtx);
      if (filterCtx.errorCode != FilterContext.CODE_OK) {
        if (logger.isDebugEnabled()) {
          logger.debug("requestFilter {} reject ", cdnServer.requestFilter);
        }
        sendError(ctx, filterCtx.errorCode, filterCtx.responseMap);
        return false;
      }
    }
    return true;
  }
  //
  public abstract void processRequest();

  public abstract void handleHttpContent(DefaultHttpContent content);

  public abstract void channelClosed();
  //
  public static void sendRedirect(ChannelHandlerContext ctx, String newUri) {
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, FOUND);
    response.headers().set(LOCATION, newUri);

    // Close the connection as soon as the error message is sent.
    ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
  }

  public static void sendError(
      ChannelHandlerContext ctx, int statusCode, Map<String, String> rspHeaders) {
    HttpResponseStatus status = HttpResponseStatus.valueOf(statusCode);
    logger.warn("send status {}", ctx.channel(), statusCode);
    FullHttpResponse response =
        new DefaultFullHttpResponse(
            HTTP_1_1, status, Unpooled.copiedBuffer(status + "\r\n", CharsetUtil.UTF_8));
    rspHeaders.forEach(
        (k, v) -> {
          response.headers().set(k, v);
        });
    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
    // Close the connection as soon as the error message is sent.
    ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
  }
  //
  public static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
    logger.warn("send status {}", ctx.channel(), status);
    FullHttpResponse response =
        new DefaultFullHttpResponse(
            HTTP_1_1, status, Unpooled.copiedBuffer(status + "\r\n", CharsetUtil.UTF_8));
    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
    // Close the connection as soon as the error message is sent.
    ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
  }

  /**
   * When file timestamp is the same as what the browser is sending up, send a "304 Not Modified"
   *
   * @param ctx Context
   */
  public static void sendNotModified(ChannelHandlerContext ctx) {
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_MODIFIED);
    setDateHeader(response);

    // Close the connection as soon as the error message is sent.
    ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
  }

  /**
   * Sets the Date header for the HTTP response
   *
   * @param response HTTP response
   */
  private static void setDateHeader(FullHttpResponse response) {
    SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
    dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));

    Calendar time = new GregorianCalendar();
    response.headers().set(DATE, dateFormatter.format(time.getTime()));
  }

  /**
   * Sets the Date and Cache headers for the HTTP Response
   *
   * @param response HTTP response
   * @param fileToCache file to extract content type
   */
  public static void setDateAndCacheHeaders(HttpResponse response, File fileToCache) {
    SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
    dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));

    // Date header
    Calendar time = new GregorianCalendar();
    response.headers().set(DATE, dateFormatter.format(time.getTime()));

    // Add cache headers
    time.add(Calendar.SECOND, HTTP_CACHE_SECONDS);
    response.headers().set(EXPIRES, dateFormatter.format(time.getTime()));
    response.headers().set(CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS);
    response
        .headers()
        .set(LAST_MODIFIED, dateFormatter.format(new Date(fileToCache.lastModified())));
  }

  /**
   * Sets the content type header for the HTTP Response
   *
   * @param response HTTP response
   * @param file file to extract content type
   */
  public static void setContentTypeHeader(HttpResponse response, File file) {
    MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
    response.headers().set(CONTENT_TYPE, mimeTypesMap.getContentType(file.getPath()));
  }
}
Exemplo n.º 2
0
/** @author yama 6 Jan, 2015 */
public class DeployManager {
  private static Logger logger = LoggerFactory.get(DeployManager.class);
  //
  private static Map<String, Instance> instanceMap;
  private static Map<String, User> userMap;
  private static Map<String, Machine> machineMap;
  private static Map<String, AppPackage> packageMap;
  private static Map<String, Application> applicationMap;
  private static List<PackageDownloadInfo> downloadInfos;
  private static GraphVizRenderer graphVizRenderer;
  private static StringBuffer errorMessage;
  //
  static {
    instanceMap = new ConcurrentHashMap<String, Instance>();
    machineMap = new ConcurrentHashMap<String, Machine>();
    packageMap = new ConcurrentHashMap<String, AppPackage>();
    applicationMap = new ConcurrentHashMap<String, Application>();
    userMap = new ConcurrentHashMap<String, User>();
    downloadInfos = Collections.synchronizedList(new LinkedList<PackageDownloadInfo>());
    graphVizRenderer = new GraphVizRenderer();
  }
  //
  public static String workSpaceDir = "";
  public static String deployHostname = "";
  public static String repoPath = "";
  public static String antPath = "";
  public static String antCommonLibPath = "";

  public static int deployHostport = 80;
  //
  public static void setup() throws Exception {
    workSpaceDir = Jazmin.environment.getString("deploy.workspace", "./workspace/");
    deployHostname = Jazmin.environment.getString("deploy.hostname", "localhost");
    repoPath = Jazmin.environment.getString("deploy.repo.dir", "./repo");
    antPath = Jazmin.environment.getString("deploy.ant", "ant");
    antCommonLibPath = Jazmin.environment.getString("deploy.ant.lib", "./lib");

    WebServer ws = Jazmin.getServer(WebServer.class);
    if (ws != null) {
      deployHostport = ws.getPort();
    }
    checkWorkspace();
    Velocity.init();
  }
  //
  public static String getErrorMessage() {
    if (errorMessage == null) {
      return "";
    }
    return errorMessage.toString();
  }
  //
  public static void reload() {
    String configDir = workSpaceDir;
    configDir += "config";
    try {
      errorMessage = new StringBuffer();
      reloadApplicationConfig(configDir);
      checkApplicationConfig();
      reloadMachineConfig(configDir);
      reloadInstanceConfig(configDir);
      reloadUserConfig(configDir);
      setInstancePrioriy();
      reloadPackage();
    } catch (Exception e) {
      logErrorMessage(e.getMessage());
      logger.error(e.getMessage(), e);
    }
  }
  //
  private static void logErrorMessage(String msg) {
    errorMessage.append(msg + "\n");
  }
  //
  public static String getConfigFile(String file) {
    String configDir = workSpaceDir + "config";
    try {
      return FileUtil.getContent(new File(configDir, file));
    } catch (IOException e) {
      logger.catching(e);
      return null;
    }
  }
  //
  public static List<Script> getScripts() {
    String configDir = workSpaceDir + "script";
    File dir = new File(configDir);
    List<Script> result = new ArrayList<Script>();
    for (File f : dir.listFiles()) {
      if (f.isFile()) {
        Script s = new Script();
        s.name = f.getName();
        s.lastModifiedTime = new Date(f.lastModified());
        result.add(s);
      }
    }
    return result;
  }
  //
  public static void deleteScript(String name) {
    File scriptFile = new File(workSpaceDir + "script/" + name);
    scriptFile.delete();
  }
  //
  public static boolean existsScript(String name) {
    File scriptFile = new File(workSpaceDir + "script/" + name);
    return scriptFile.exists();
  }
  //
  public static String getScript(String name) throws IOException {
    File scriptFile = new File(workSpaceDir + "script/" + name);
    return FileUtil.getContent(scriptFile);
  }
  //
  public static void saveScript(String name, String content) throws IOException {
    File scriptFile = new File(workSpaceDir + "script/" + name);
    if (!scriptFile.exists()) {
      scriptFile.createNewFile();
    }
    FileUtil.saveContent(content, scriptFile);
  }
  //
  public static void saveConfigFile(String file, String value) {
    String configDir = workSpaceDir + "config";
    try {
      FileUtil.saveContent(value, new File(configDir, file));
      reload();
    } catch (IOException e) {
      logger.catching(e);
    }
  }

  //
  public static void setPackageVersion(List<Instance> instances, String version) {
    if (version == null || version.trim().isEmpty()) {
      return;
    }
    if (!Pattern.matches("\\d\\d*\\.\\d\\d*\\.\\d\\d*", version)) {
      throw new IllegalArgumentException("version format just like :1.0.0");
    }
    instances.forEach(i -> i.packageVersion = (version));
  }
  //
  public static void saveInstanceConfig() throws Exception {
    String configDir = workSpaceDir;
    configDir += "config";
    File configFile = new File(configDir, "instance.json");
    List<Instance> list = getInstances();
    Collections.sort(list, (o1, o2) -> o1.priority - o2.priority);
    if (configFile.exists()) {
      String result =
          JSONUtil.toJson(
              list,
              new JSONPropertyFilter() {
                @Override
                public boolean apply(Object arg0, String name, Object arg2) {
                  if (name.equals("alive") || name.equals("machine") || name.equals("priority")) {
                    return false;
                  }
                  return true;
                }
              },
              true);
      FileUtil.saveContent(result, configFile);
    }
  }
  //
  private static void reloadPackage() {
    packageMap.clear();
    String packageDir = workSpaceDir;
    packageDir += "package";

    File packageFolder = new File(packageDir);
    if (packageFolder.exists() && packageFolder.isDirectory()) {
      for (File ff : packageFolder.listFiles()) {
        if (ff.isFile() && !ff.isHidden()) {
          AppPackage pkg = new AppPackage();
          String fileName = ff.getName();
          pkg.id = (fileName);
          pkg.file = (ff.getAbsolutePath());
          pkg.lastModifiedTime = new Date(ff.lastModified());
          packageMap.put(pkg.id, pkg);
        }
      }
    }
  }
  //
  public static User validate(String u, String p) {
    User ui = userMap.get(u);
    if (ui == null) {
      return null;
    }
    if (ui.password.equals(p)) {
      return ui;
    }
    return null;
  }
  //
  public static User getUser(String uid) {
    return userMap.get(uid);
  }
  //
  public static List<PackageDownloadInfo> getPackageDownloadInfos() {
    return downloadInfos;
  }
  //
  public static void addPackageDownloadInfo(PackageDownloadInfo info) {
    downloadInfos.add(info);
  }
  //
  public static void removePackageDownloadInfo(PackageDownloadInfo info) {
    downloadInfos.remove(info);
  }
  //
  public static List<AppPackage> getPackages() {
    return new ArrayList<AppPackage>(packageMap.values());
  }
  //
  public static List<Application> getApplications(String uid, String search) {
    if (search == null || search.trim().isEmpty()) {
      return new ArrayList<Application>();
    }

    String queryBegin = "select * from " + Application.class.getName() + " where";
    User user = getUser(uid);
    String sql = queryBegin;
    if (!uid.equals(User.ADMIN)) {
      StringBuilder cc = new StringBuilder();
      for (String q : user.applicationSystems) {
        cc.append("'" + q + "',");
      }
      if (cc.length() > 0) {
        cc.deleteCharAt(cc.length() - 1);
      }
      sql += " 1=1 and system in(" + cc + ") and ";
    } else {
      sql += " 1=1 and ";
    }
    sql += search;
    return BeanUtil.query(getApplications(), sql);
  }
  //
  public static List<Application> getApplications() {
    return new ArrayList<Application>(applicationMap.values());
  }
  //
  public static List<Application> getApplicationBySystem(String system) {
    List<Application> result = new ArrayList<Application>();
    for (Application a : applicationMap.values()) {
      if (a.system.equals(system)) {
        result.add(a);
      }
    }
    return result;
  }
  //
  public static List<AppPackage> getPackages(String search) throws Exception {
    if (search == null || search.trim().isEmpty()) {
      return new ArrayList<AppPackage>();
    }
    String queryBegin = "select * from " + AppPackage.class.getName() + " where 1=1 and ";
    return BeanUtil.query(getPackages(), queryBegin + search);
  }
  //
  //
  public static List<RepoItem> getRepoItems(String search) throws Exception {
    if (search == null || search.trim().isEmpty()) {
      return new ArrayList<RepoItem>();
    }
    String queryBegin = "select * from " + RepoItem.class.getName() + " where 1=1 and ";
    return BeanUtil.query(getRepoItems(), queryBegin + search);
  }
  //
  public static List<RepoItem> getRepoItems() {
    List<RepoItem> items = new ArrayList<RepoItem>();
    String repoDir = workSpaceDir + "repo";
    File ff = new File(repoDir);
    if (ff.isDirectory()) {
      for (File f : ff.listFiles()) {
        if (f.isFile()) {
          RepoItem i = new RepoItem();
          i.file = f.getAbsolutePath();
          i.id = f.getName();
          i.lastModifiedTime = new Date(f.lastModified());
          items.add(i);
        }
      }
    }
    return items;
  }
  //
  public static List<Instance> getInstances(String uid, String search) throws Exception {
    if (search == null || search.trim().isEmpty()) {
      return new ArrayList<Instance>();
    }
    String queryBegin = "select * from " + Instance.class.getName() + " where";
    User user = getUser(uid);
    String sql = queryBegin;
    if (!uid.equals(User.ADMIN)) {
      StringBuilder cc = new StringBuilder();
      for (String q : user.instanceClusters) {
        cc.append("'" + q + "',");
      }
      if (cc.length() > 0) {
        cc.deleteCharAt(cc.length() - 1);
      }
      sql += " 1=1 and cluster in(" + cc + ") and ";
    } else {
      sql += " 1=1 and ";
    }
    sql += search;
    return BeanUtil.query(getInstances(), sql);
  }
  //
  public static Instance getInstance(String id) {
    return instanceMap.get(id);
  }
  //
  public static List<Machine> getMachines(String uid, String search) {
    if (search == null || search.trim().isEmpty()) {
      return new ArrayList<Machine>();
    }
    String queryBegin = "select * from " + Machine.class.getName() + " where";
    User user = getUser(uid);
    String sql = queryBegin;
    if (!uid.equals(User.ADMIN)) {
      StringBuilder cc = new StringBuilder();
      for (String q : user.machines) {
        cc.append("'" + q + "',");
      }
      if (cc.length() > 0) {
        cc.deleteCharAt(cc.length() - 1);
      }
      sql += " 1=1 and id in(" + cc + ") and ";
    } else {
      sql += " 1=1 and ";
    }
    sql += search;
    return BeanUtil.query(getMachines(), sql);
  }
  //
  public static List<Machine> getMachines() {
    return new ArrayList<Machine>(machineMap.values());
  }

  public static Machine getMachine(String id) {
    return machineMap.get(id);
  }
  //
  public static List<Instance> getInstances() {
    return new ArrayList<Instance>(instanceMap.values());
  }
  //
  private static void reloadMachineConfig(String configDir) throws Exception {
    File configFile = new File(configDir, "machine.json");
    if (configFile.exists()) {
      machineMap.clear();
      logger.info("load config from:" + configFile.getAbsolutePath());
      String ss = FileUtil.getContent(configFile);
      List<Machine> machines = JSONUtil.fromJsonList(ss, Machine.class);
      machines.forEach(in -> machineMap.put(in.id, in));
    } else {
      logErrorMessage("can not find :" + configFile);
    }
  }

  private static void reloadApplicationConfig(String configDir) throws Exception {
    File configFile = new File(configDir, "application.json");
    if (configFile.exists()) {
      applicationMap.clear();
      logger.info("load application from:" + configFile.getAbsolutePath());
      String ss = FileUtil.getContent(configFile);
      List<Application> apps = JSONUtil.fromJsonList(ss, Application.class);
      apps.forEach(
          in -> {
            applicationMap.put(in.id, in);
          });
    } else {
      logErrorMessage("can not find :" + configFile);
    }
  }

  private static void reloadUserConfig(String configDir) throws Exception {
    File configFile = new File(configDir, "user.json");
    if (configFile.exists()) {
      userMap.clear();
      logger.info("load user from:" + configFile.getAbsolutePath());
      String ss = FileUtil.getContent(configFile);
      List<User> apps = JSONUtil.fromJsonList(ss, User.class);
      apps.forEach(
          in -> {
            userMap.put(in.id, in);
          });
    } else {
      logErrorMessage("can not find :" + configFile);
    }
  }
  //
  private static void checkApplicationConfig() {
    for (Application a : applicationMap.values()) {
      for (String depend : a.depends) {
        if (!applicationMap.containsKey(depend)) {
          logErrorMessage("can not find depend application " + depend + " for " + a.id);
        }
        //
        if (depend.equals(a)) {
          logErrorMessage("can not depend self " + depend);
        }
      }
    }
    // do topsearch and cal priority
    int idx = 0;
    for (Application a : TopSearch.topSearch(getApplications())) {
      a.priority = idx++;
    }
  }

  //
  private static void reloadInstanceConfig(String configDir) throws Exception {
    File configFile = new File(configDir, "instance.json");
    if (configFile.exists()) {
      instanceMap.clear();
      logger.info("load config from:" + configFile.getAbsolutePath());
      String ss = FileUtil.getContent(configFile);
      List<Instance> instances = JSONUtil.fromJsonList(ss, Instance.class);
      AtomicInteger ai = new AtomicInteger();
      instances.forEach(
          in -> {
            if (in.user == null) {
              in.user = ("");
            }
            if (in.password == null) {
              in.password = ("");
            }
            if (in.packageVersion == null) {
              in.packageVersion = ("1.0.0");
            }
            in.priority = (ai.incrementAndGet());
            Machine m = machineMap.get(in.machineId);
            if (m == null) {
              logErrorMessage(
                  "can not find machine " + in.machineId + " for instance " + in.id + "");
            } else {
              in.machine = (m);
            }
            Application app = applicationMap.get(in.appId);
            if (app == null) {
              logErrorMessage(
                  "can not find application " + in.appId + " for instance " + in.id + "");
            } else {
              in.application = (app);
            }
            instanceMap.put(in.id, in);
          });
    } else {
      logErrorMessage("can not find :" + configFile);
    }
  }
  //
  private static void setInstancePrioriy() {
    for (Instance i : getInstances()) {
      i.priority = i.application.priority;
    }
  }
  //
  public static String renderTemplate(String instanceName) {
    Instance instance = instanceMap.get(instanceName);
    if (instance == null) {
      return null;
    }
    return renderTemplate(instance);
  }
  //
  //
  public static void saveTemplate(String appId, String value) {
    String templateDir = workSpaceDir;
    templateDir += "template";
    File file = new File(templateDir + "/" + appId + ".vm");
    try {
      if (!file.exists()) {
        file.createNewFile();
      }
      FileUtil.saveContent(value, file);
    } catch (Exception e) {
      logger.catching(e);
    }
  }
  //
  public static String getTemplate(String appId) {
    String templateDir = workSpaceDir;
    templateDir += "template";
    File file = new File(templateDir + "/" + appId + ".vm");
    if (!file.exists()) {
      return null;
    }
    try {
      return FileUtil.getContent(file);
    } catch (IOException e) {
      logger.catching(e);
      return null;
    }
  }
  //
  private static String renderTemplate(Instance instance) {
    VelocityContext ctx = new VelocityContext();
    WebServer ws = Jazmin.getServer(WebServer.class);
    if (ws != null) {
      ctx.put("deployServerPort", ws.getPort());
    }
    ctx.put("env", Jazmin.environment.envs());
    ctx.put("instances", getInstances());
    ctx.put("instanceMap", instanceMap);
    ctx.put("machines", getMachines());
    ctx.put("machineMap", machineMap);
    ctx.put("applications", getApplications());
    ctx.put("applicationMap", applicationMap);
    ctx.put("instance", instance);
    //
    Map<String, String> properties = new HashMap<String, String>();
    properties.putAll(instance.properties);
    properties.putAll(instance.application.properties);
    properties.putAll(instance.machine.properties);
    //
    ctx.put("properties", properties);

    StringWriter sw = new StringWriter();
    String templateDir = workSpaceDir;
    templateDir += "template";

    File file = new File(templateDir + "/" + instance.appId + ".vm");
    if (!file.exists()) {
      logger.info("can not find {} use Default.vm to render", file);
      file = new File(templateDir + "/Default.vm");
    }
    if (!file.exists()) {
      logger.warn("can not find template {}", file);
      return null;
    }
    Velocity.mergeTemplate(file.getPath(), "UTF-8", ctx, sw);
    return sw.toString();
  }
  //
  private static boolean testPort(String host, int port) {
    try {
      Socket socket = new Socket();
      socket.connect(new InetSocketAddress(host, port), 1000);
      socket.close();
      return true;
    } catch (Exception ex) {
      return false;
    }
  }
  //
  public static void testMachine(Machine machine) {
    machine.isAlive = (pingHost(machine.publicHost));
  }
  //
  public static String runCmdOnMachine(Machine m, boolean root, String cmd) {
    StringBuilder sb = new StringBuilder();
    try {
      SshUtil.execute(
          m.publicHost,
          m.sshPort,
          root ? "root" : m.sshUser,
          root ? m.rootSshPassword : m.sshPassword,
          cmd,
          m.getSshTimeout(),
          (out, err) -> {
            sb.append(out + "\n");
            if (err != null && !err.isEmpty()) {
              sb.append(err + "\n");
            }
          });
    } catch (Exception e) {
      e.printStackTrace();
      sb.append(e.getMessage() + "\n");
    }
    return sb.toString();
  }
  //
  public static String runScriptOnMachine(Machine m, boolean root, String script) {
    StringBuilder sb = new StringBuilder();
    try {
      String shellFile = script;
      shellFile = shellFile.replaceAll("'", "\\'").replaceAll("\"", "\\\\\"");
      String uuid = UUID.randomUUID().toString().replaceAll("-", "");
      SshUtil.execute(
          m.publicHost,
          m.sshPort,
          root ? "root" : m.sshUser,
          root ? m.rootSshPassword : m.sshPassword,
          "echo \""
              + shellFile
              + "\" > "
              + "/tmp/"
              + uuid
              + ";chmod +x /tmp/"
              + uuid
              + ";/tmp/"
              + uuid,
          m.getSshTimeout(),
          (out, err) -> {
            sb.append(out + "\n");
            if (err != null && !err.isEmpty()) {
              sb.append(err + "\n");
            }
          });
    } catch (Exception e) {
      e.printStackTrace();
      sb.append(e.getMessage() + "\n");
    }
    return sb.toString();
  }
  //
  private static boolean pingHost(String host) {
    try {
      return InetAddress.getByName(host).isReachable(3000);
    } catch (Exception e) {
      return false;
    }
  }
  //
  //
  public static void testInstance(Instance instance) {
    instance.isAlive = (testPort(instance.machine.publicHost, instance.port));
  }
  //
  private static String exec(Instance instance, boolean root, String cmd) throws Exception {
    StringBuilder sb = new StringBuilder();
    try {
      Machine m = instance.machine;
      SshUtil.execute(
          m.publicHost,
          m.sshPort,
          root ? "root" : m.sshUser,
          root ? m.rootSshPassword : m.sshPassword,
          cmd,
          m.getSshTimeout(),
          (out, err) -> {
            sb.append(out + "\n");
            if (err != null && !err.isEmpty()) {
              sb.append(err + "\n");
            }
          });
    } catch (Exception e) {
      e.printStackTrace();
      sb.append(e.getMessage() + "\n");
      throw e;
    }
    return sb.toString();
  }
  //
  public static String createInstance(Instance instance) throws Exception {
    StringBuilder sb = new StringBuilder();
    if (instance.application.type.startsWith("jazmin")) {
      String instanceDir = instance.machine.jazminHome + "/instance/" + instance.id;
      sb.append(exec(instance, false, "mkdir " + instanceDir) + "");
      //
      String hostname = deployHostname + ":" + deployHostport;
      String jsFile =
          "jazmin.include('http://" + hostname + "/srv/deploy/boot/'+jazmin.getServerName());";
      jsFile = jsFile.replaceAll("'", "\\'").replaceAll("\"", "\\\\\"");
      //
      sb.append(
          exec(instance, false, "echo \"" + jsFile + "\" > " + instanceDir + "/jazmin.js") + "\n");
    }
    //
    if (instance.application.type.equals(Application.TYPE_HAPROXY)) {
      String haproxyHome = instance.machine.haproxyHome;
      String instanceDir = haproxyHome + "" + instance.id;
      sb.append(exec(instance, false, "mkdir -p " + instanceDir) + "");
    }
    //
    if (instance.application.type.equals(Application.TYPE_MEMCACHED)) {
      String memcachedHome = instance.machine.memcachedHome;
      String instanceDir = memcachedHome + "" + instance.id;
      sb.append(exec(instance, false, "mkdir -p " + instanceDir) + "");
    }
    return sb.toString();
  }
  //
  public static String startInstance(Instance instance) throws Exception {
    StringBuilder sb = new StringBuilder();
    if (instance.application.type.startsWith("jazmin")) {
      sb.append(
          exec(instance, false, instance.machine.jazminHome + "/jazmin startbg " + instance.id));
    }
    if (instance.application.type.equals(Application.TYPE_MEMCACHED)) {
      String size = instance.getProperties().getOrDefault(Instance.P_MEMCACHED_SIZE, "64m");
      String memcachedHome = instance.machine.memcachedHome;
      String instanceDir = memcachedHome + "/" + instance.id;
      String pidFile = instanceDir + "/memcached_pid";
      String memcachedCmd =
          "memcached -d -m " + size + " -l 0.0.0.0 -p " + instance.port + " -P " + pidFile;
      sb.append(exec(instance, false, memcachedCmd));
    }
    if (instance.application.type.equals(Application.TYPE_HAPROXY)) {
      String configFile = renderTemplate(instance);
      configFile = configFile.replaceAll("'", "\\'").replaceAll("\"", "\\\\\"");
      //
      String haproxyHome = instance.machine.haproxyHome;
      String instanceDir = haproxyHome + "/" + instance.id;
      String configPath = instanceDir + "/haproxy_cfg";
      sb.append(exec(instance, false, "echo \"" + configFile + "\" > " + configPath) + "\n");
      String pidFile = instanceDir + "/haproxy_pid";
      sb.append(exec(instance, true, "haproxy -p " + pidFile + " -f " + configPath));
    }

    return sb.toString();
  }
  //
  public static String stopInstance(Instance instance) throws Exception {
    StringBuilder sb = new StringBuilder();
    if (instance.application.type.startsWith("jazmin")) {
      sb.append(exec(instance, false, instance.machine.jazminHome + "/jazmin stop " + instance.id));
    }
    //
    if (instance.application.type.equals(Application.TYPE_MEMCACHED)) {
      String memcachedHome = instance.machine.memcachedHome;
      String instanceDir = memcachedHome + "/" + instance.id;
      String pidFile = instanceDir + "/memcached_pid";
      sb.append(exec(instance, false, "kill -9 `cat " + pidFile + "`"));
    }
    //
    if (instance.application.type.equals(Application.TYPE_HAPROXY)) {
      String haproxyHome = instance.machine.haproxyHome;
      String instanceDir = haproxyHome + "/" + instance.id;
      String pidFile = instanceDir + "/haproxy_pid";
      sb.append(exec(instance, true, "kill -9 `cat " + pidFile + "`"));
    }
    return sb.toString();
  }
  /** */
  public static AppPackage getInstancePackage(String instanceId) {
    Instance ins = instanceMap.get(instanceId);
    if (ins == null) {
      logger.warn("can not find instance {}", instanceId);
      return null;
    }
    String suffex = "";
    suffex = ".jaz";
    if (ins.application.type.equals(Application.TYPE_JAZMIN_WEB)) {
      suffex = ".war";
    }
    String packageName = ins.appId + "-" + ins.packageVersion + suffex;
    AppPackage p = packageMap.get(packageName);
    logger.info("return package {} - {}", p, packageName);
    return p;
  }
  //
  public static AppPackage getPackage(String name) {
    return packageMap.get(name);
  }
  //
  public static String renderApplicationGraph(String system) {
    return graphVizRenderer.renderInstanceGraph(system, "");
  }
  //
  public static String renderInstanceGraph(String system, String cluster) {
    return graphVizRenderer.renderInstanceGraph(system, cluster);
  }
  //
  private static void createDirs(String path) {
    File workspace = new File(workSpaceDir, path);
    if (!workspace.exists()) {
      logger.info("create workspace dir {}", workspace);
      if (!workspace.mkdirs()) {
        logger.warn("can not create workspace dir {}", workspace);
        return;
      }
    }
  }
  //
  public static void checkWorkspace() {
    createDirs("config");
    createDirs("template");
    createDirs("repo");
    createDirs("package");
    createDirs("script");
    //
    createFile("config/application.json");
    createFile("config/instance.json");
    createFile("config/machine.json");
    createFile("config/user.json");
    createFile("config/iptables.rule");
    createFile("template/Default.vm");
  }
  //
  private static void createFile(String path) {
    try {
      logger.info("create new file {}", path);
      String s =
          IOUtil.getContent(DeployStartServlet.class.getResourceAsStream("workspace/" + path));
      File configFile = new File(workSpaceDir, path);
      if (!configFile.exists()) {
        configFile.createNewFile();
        FileUtil.saveContent(s, configFile);
      }
    } catch (IOException e) {
      logger.catching(e);
    }
  }
  //

  public static void compileApp(Application app, OutputListener listener) {
    if (app.scmUser == null) {
      return;
    }
    File localPath = new File(DeployManager.repoPath, app.id);
    if (!localPath.exists()) {
      logger.info("create local path:{}", localPath.getAbsolutePath());
      localPath.mkdirs();
    }
    WorkingCopy wc =
        new WorkingCopy(app.scmUser, app.scmPassword, app.scmPath, localPath.getAbsolutePath());
    wc.setOutputListener(listener);
    try {
      wc.cleanup();
      wc.checkout();
      wc.update();
    } catch (SVNException e) {
      logger.catching(e);
      listener.onOutput(e.getMessage());
      return;
    }
    //
    if (app.antTarget != null) {
      AntManager antManager = new AntManager(DeployManager.antPath);
      antManager.setOutputListener(listener);
      antManager.setCommonLib(DeployManager.antCommonLibPath);
      File buildFile = new File(localPath, "build.xml");
      try {
        antManager.antCall(app.antTarget, buildFile.getAbsolutePath());
      } catch (Exception e) {
        logger.catching(e);
        listener.onOutput(e.getMessage());
        return;
      }
    }
  }
}
Exemplo n.º 3
0
/** @author yama 25 Dec, 2014 */
public class Session {
  private static Logger logger = LoggerFactory.get(Session.class);
  //
  String connectionType;
  int id;
  String principal;
  String userAgent;
  Object userObject;
  io.netty.channel.Channel channel;
  long lastAccessTime;
  String remoteHostAddress;
  int remotePort;
  int requestId;
  long sentMessageCount;
  long receiveMessageCount;
  Set<String> channels;
  Date createTime;
  //
  RateLimiter rateLimiter;
  private AtomicBoolean processSyncServiceState;
  //
  Session(io.netty.channel.Channel channel) {
    setChannel(channel);
    lastAccess();
    sentMessageCount = 0;
    receiveMessageCount = 0;
    rateLimiter = new RateLimiter();
    processSyncServiceState = new AtomicBoolean();
    processSyncService(false);
    channels = Collections.synchronizedSet(new TreeSet<String>());
    createTime = new Date();
    connectionType = "tcp";
  }
  // --------------------------------------------------------------------------
  // public interface

  /** @return the createTime */
  public Date getCreateTime() {
    return createTime;
  }
  /** @return the connectionType */
  public String getConnectionType() {
    return connectionType;
  }

  /** @return the maxRequestCountPerSecond */
  public int getMaxRequestCountPerSecond() {
    return rateLimiter.getMaxRequestCountPerSecond();
  }

  /** @param maxRequestCountPerSecond the maxRequestCountPerSecond to set */
  public void setMaxRequestCountPerSecond(int maxRequestCountPerSecond) {
    this.rateLimiter.setMaxRequestCountPerSecond(maxRequestCountPerSecond);
  }

  /** @return the id */
  public int getId() {
    return id;
  }

  /** @return the sentMessageCount */
  public long getSentMessageCount() {
    return sentMessageCount;
  }

  /** @return the receiveMessageCount */
  public long getReceiveMessageCount() {
    return receiveMessageCount;
  }

  /** @return the principal */
  public String getPrincipal() {
    return principal;
  }
  /** @return the userObject */
  public Object getUserObject() {
    return userObject;
  }
  /** @param userObject the userObject to set */
  public void setUserObject(Object userObject) {
    this.userObject = userObject;
  }
  /** @return the remoteHostAddress */
  public String getRemoteHostAddress() {
    return remoteHostAddress;
  }
  /** @return the remotePort */
  public int getRemotePort() {
    return remotePort;
  }
  /** @return last access time */
  public long getLastAccessTime() {
    return lastAccessTime;
  }
  /**
   * push message to client
   *
   * @param serviceId the message id
   * @param payload message payload
   */
  public void push(String serviceId, Object payload) {
    if (serviceId == null) {
      throw new IllegalArgumentException("serviceId can not be null.");
    }
    if (payload == null) {
      throw new IllegalArgumentException("payload can not be null.");
    }
    ResponseMessage rsm = new ResponseMessage();
    rsm.requestId = 0;
    rsm.serviceId = serviceId;
    rsm.responseMessages.put("payload", payload);
    sendMessage(rsm);
  }
  //
  /**
   * push message to client
   *
   * @param serviceId the message id
   * @param payload message payload
   */
  public void pushRaw(String serviceId, byte[] payload) {
    if (serviceId == null) {
      throw new IllegalArgumentException("serviceId can not be null.");
    }
    if (payload == null) {
      throw new IllegalArgumentException("payload can not be null.");
    }
    ResponseMessage rsp = new ResponseMessage();
    rsp.requestId = 0;
    rsp.serviceId = serviceId;
    rsp.rawData = payload;
    sendMessage(rsp);
  }
  /**
   * send kick message and close session
   *
   * @param message the message before session kicked
   */
  public void kick(String message) {
    if (logger.isDebugEnabled()) {
      logger.debug("session kicked:{}", message);
    }
    sendError(null, ResponseMessage.SC_KICKED, message);
    if (channel != null) {
      try {
        channel.close().sync();
      } catch (InterruptedException e) {
        logger.catching(e);
      }
    }
  }

  /** @return the userAgent */
  public String getUserAgent() {
    return userAgent;
  }
  /** @return all channel id of session joined */
  public List<String> getChannels() {
    return new ArrayList<>(channels);
  }
  //
  // --------------------------------------------------------------------------
  //
  //
  boolean isProcessSyncService() {
    return processSyncServiceState.get();
  }
  //
  void enterChannel(Channel c) {
    channels.add(c.id);
  }
  //
  void leaveChannel(Channel c) {
    channels.remove(c.id);
  }
  //
  void lastAccess() {
    lastAccessTime = System.currentTimeMillis();
  }
  /** @param userAgent the userAgent to set */
  void setUserAgent(String userAgent) {
    this.userAgent = userAgent;
  }

  /** @param principal the principal to set */
  void setPrincipal(String principal) {
    this.principal = principal;
  }
  /*
   */
  void setId(int id) {
    this.id = id;
  }
  //
  void processSyncService(boolean isProcess) {
    processSyncServiceState.set(isProcess);
  }
  //
  /**/
  void setChannel(io.netty.channel.Channel channel) {
    this.channel = channel;
    remoteHostAddress = null;
    remotePort = 0;
    if (channel != null) {
      SocketAddress remoteAddr = channel.remoteAddress();
      if (remoteAddr != null) {
        InetSocketAddress addr = (InetSocketAddress) remoteAddr;
        InetAddress ad = addr.getAddress();
        remoteHostAddress = ad.getHostAddress();
        remotePort = addr.getPort();
      }
    }
  }
  //
  boolean isFrequencyReach() {
    return rateLimiter.accessAndTest();
  }

  //
  void receivedMessage(RequestMessage message) {
    receiveMessageCount++;
    requestId = message.requestId;
    lastAccess();
  }
  /*
   * @return the requestId
   */
  int getRequestId() {
    return requestId;
  }
  /*
   * @param requestId the requestId to set
   */
  void setRequestId(int requestId) {
    this.requestId = requestId;
  }
  //
  void sendError(RequestMessage message, int code, String msg) {
    ResponseMessage rsp = new ResponseMessage();
    rsp.requestId = (message == null) ? 0 : message.requestId;
    rsp.statusCode = code;
    rsp.statusMessage = msg;
    sendMessage(rsp);
  }
  //
  void sendMessage(ResponseMessage responseMessage) {
    lastAccess();
    if (channel != null) {
      sentMessageCount++;
      channel.writeAndFlush(responseMessage);
    }
  }
  //
  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + id;
    return result;
  }
  //
  @Override
  public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null) return false;
    if (getClass() != obj.getClass()) return false;
    Session other = (Session) obj;
    if (id != other.id) return false;
    return true;
  }

  @Override
  public String toString() {
    return "Session [id=" + id + ", principal=" + principal + ", channel=" + channel + "]";
  }
}
Exemplo n.º 4
0
/** @author yama 25 Dec, 2015 */
public class WorkingCopy {
  private static Logger logger = LoggerFactory.get(WorkingCopy.class);
  //
  private SVNClientManager ourClientManager;
  private ISVNEventHandler myCommitEventHandler;
  private ISVNEventHandler myUpdateEventHandler;
  private ISVNEventHandler myWCEventHandler;
  SVNURL repositoryURL;
  File destPath;
  private OutputListener outputListener;

  static {
    setupLibrary();
  }
  //

  //
  public WorkingCopy(String name, String password, String svnPath, String localPath) {
    try {
      repositoryURL = SVNURL.parseURIEncoded(svnPath);
    } catch (SVNException e) {
      //
    }
    myUpdateEventHandler = new UpdateEventHandler(this);
    myWCEventHandler = new WCEventHandler(this);
    DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
    ourClientManager = SVNClientManager.newInstance(options, name, password);
    ourClientManager.getCommitClient().setEventHandler(myCommitEventHandler);
    ourClientManager.getUpdateClient().setEventHandler(myUpdateEventHandler);
    ourClientManager.getWCClient().setEventHandler(myWCEventHandler);
    //
    destPath = new File(localPath);
  }
  /** @return the outputListener */
  public OutputListener getOutputListener() {
    return outputListener;
  }
  /** @param outputListener the outputListener to set */
  public void setOutputListener(OutputListener outputListener) {
    this.outputListener = outputListener;
  }
  //
  public void println(String s) {
    if (outputListener != null) {
      outputListener.onOutput(s + "\n");
    }
    if (logger.isDebugEnabled()) {
      logger.debug(s);
    }
  }
  //
  /*
   * Initializes the library to work with a repository via
   * different protocols.
   */
  private static void setupLibrary() {
    DAVRepositoryFactory.setup();
    SVNRepositoryFactoryImpl.setup();
    FSRepositoryFactory.setup();
  }
  /*
   *
   */
  public long checkout() throws SVNException {
    SVNUpdateClient updateClient = ourClientManager.getUpdateClient();
    updateClient.setIgnoreExternals(false);
    return updateClient.doCheckout(
        repositoryURL, destPath, SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY, true);
  }
  //
  public void cleanup() throws SVNException {
    try {
      SVNWCClient wcClient = ourClientManager.getWCClient();
      wcClient.doCleanup(destPath);
    } catch (Exception e) {
    }
  }
  /*
   *
   */
  @SuppressWarnings("deprecation")
  public long update() throws SVNException {

    SVNUpdateClient updateClient = ourClientManager.getUpdateClient();
    updateClient.setIgnoreExternals(false);
    return updateClient.doUpdate(destPath, SVNRevision.HEAD, true);
  }

  //
  public static void main(String[] args) throws Exception {
    WorkingCopy wc =
        new WorkingCopy(
            "svnuser",
            "svnuser",
            "svn://itit.io/repo/HwWebSystem",
            "/Users/yama/Desktop/svn_test/");
    wc.cleanup();
    wc.checkout();
    wc.update();
  }
}
Exemplo n.º 5
0
public class RtmpEncoder extends SimpleChannelDownstreamHandler {

  private static final Logger logger = LoggerFactory.getLogger(RtmpEncoder.class);

  private int chunkSize = 128;
  private RtmpHeader[] channelPrevHeaders = new RtmpHeader[RtmpHeader.MAX_CHANNEL_ID];

  private void clearPrevHeaders() {
    logger.debug("clearing prev stream headers");
    channelPrevHeaders = new RtmpHeader[RtmpHeader.MAX_CHANNEL_ID];
  }

  @Override
  public void writeRequested(final ChannelHandlerContext ctx, final MessageEvent e) {
    Channels.write(ctx, e.getFuture(), encode((RtmpMessage) e.getMessage()));
  }

  public ChannelBuffer encode(final RtmpMessage message) {
    final ChannelBuffer in = message.encode();
    final RtmpHeader header = message.getHeader();
    if (header.isChunkSize()) {
      final ChunkSize csMessage = (ChunkSize) message;
      logger.debug("encoder new chunk size: {}", csMessage);
      chunkSize = csMessage.getChunkSize();
    } else if (header.isControl()) {
      final Control control = (Control) message;
      if (control.getType() == Control.Type.STREAM_BEGIN) {
        clearPrevHeaders();
      }
    }
    final int channelId = header.getChannelId();
    header.setSize(in.readableBytes());
    final RtmpHeader prevHeader = channelPrevHeaders[channelId];
    if (prevHeader != null // first stream message is always large
        && header.getStreamId() > 0 // all control messages always large
        && header.getTime() > 0) { // if time is zero, always large
      if (header.getSize() == prevHeader.getSize()) {
        header.setHeaderType(RtmpHeader.Type.SMALL);
      } else {
        header.setHeaderType(RtmpHeader.Type.MEDIUM);
      }
      final int deltaTime = header.getTime() - prevHeader.getTime();
      if (deltaTime < 0) {
        logger.warn("negative time: {}", header);
        header.setDeltaTime(0);
      } else {
        header.setDeltaTime(deltaTime);
      }
    } else {
      // otherwise force to LARGE
      header.setHeaderType(RtmpHeader.Type.LARGE);
    }
    channelPrevHeaders[channelId] = header;
    if (logger.isDebugEnabled()) {
      // logger.debug(">> {}", message);
    }
    final ChannelBuffer out =
        ChannelBuffers.buffer(
            RtmpHeader.MAX_ENCODED_SIZE + header.getSize() + header.getSize() / chunkSize);
    boolean first = true;
    while (in.readable()) {
      final int size = Math.min(chunkSize, in.readableBytes());
      if (first) {
        header.encode(out);
        first = false;
      } else {
        out.writeBytes(header.getTinyHeader());
      }
      in.readBytes(out, size);
    }
    return out;
  }
}