歡迎您光臨本站 註冊首頁

用shell和java實現自動部署

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

  公司用到了rackspace的cloudserver 做為壓力測試的伺服器,但是一旦啟動就開始收費,一個小時0.4刀.因此要求用的時候打開,不用的時候關掉.

  但是遇到一個問題就是,這樣的話在上邊部署應用程序和軟體就比較麻煩了,不能每次都重新部署吧,那樣效率太低了.因此想到寫一些腳本進行自動的部署.其他的都好辦,但是有些需要修改xml文件和properties文件的,用shell實現起來就費力了.

  既然是java的應用就用java來實現吧,用了一下午寫好了.下邊我把修改properties和xml的代碼貼出來,希望對大家有所幫助.

  1:修改properties文件

  package util.xml;

  import java.io.File;

  import java.io.FileInputStream;

  import java.io.FileOutputStream;

  import java.io.IOException;

  import java.util.List;

  import java.util.Properties;

  import java.util.Date;

  import java.text.SimpleDateFormat;

  import java.util.ArrayList;

  import java.util.List;

  public class ModifyPropertiesFile{

  public String propertyFilePath="remoting_client.properties";

  public String stagingIp="192.168.1.2";

  public String messageIp="192.168.1.3";

  public void modifyPropertiesFile()

  {

  try {

  Properties p = new Properties();

  File f = new File(propertyFilePath);

  List<String> list = new ArrayList<String>();

  list.add(stagingIp);

  list.add(messageIp);

  if(f.exists() && f.isFile())

  {

  FileInputStream fis;

  fis = new FileInputStream(f);

  p.load(fis);

  p.setProperty("service.online.url","socket://" list.get(0) ":5600");

  p.setProperty("service.message.url","socket://" list.get(1) ":5700");

  p.setProperty("service.account.url","socket://" list.get(0) ":5701");

  p.setProperty("service.order.url","socket://" list.get(0) ":5702");

  p.setProperty("service.party.url","socket://" list.get(0) ":5703");

  p.setProperty("service.product.url","socket://" list.get(0) ":5704");

  p.setProperty("service.webimgateway.url","socket://" list.get(0) ":5705");

  p.setProperty("service.dirtyword.url","socket://" list.get(0) ":5898");

  FileOutputStream oFile = new FileOutputStream(f);

  p.store(oFile,"");

  fis.close();

  oFile.close();

  }

  }

  catch (Exception e)

  {

  e.printStackTrace();

  e.getMessage();

  System.out.println("Can't modify this properties file,please check your program again!!!!");

  }

  }

  public static void main(String args[])

  {

  ModifyPropertiesFile t=new ModifyPropertiesFile();

  t.modifyPropertiesFile();

  }

  }

  2:修改xml文件

  package util.xml;

  import java.io.File;

  import java.io.FileInputStream;

  import java.io.FileOutputStream;

  import java.io.IOException;

  import java.util.List;

  import java.util.Properties;

  import java.util.Date;

  import java.text.SimpleDateFormat;

  import org.jdom.Document;

  import org.jdom.Element;

  import org.jdom.JDOMException;

  import org.jdom.input.SAXBuilder;

  import org.jdom.output.XMLOutputter;

  import org.jdom.output.Format;

  import org.jdom.xpath.XPath;

  import org.apache.log4j.Logger;

  import org.xml.sax.EntityResolver;

  import org.xml.sax.InputSource;

  import org.xml.sax.SAXException;

  import java.util.HashMap;

  import java.util.Iterator;

  public class ModifyHibernateXml{

  private String dataBaseInfo;

  private String userName;

  private String userPass;

  private String deploymentTitle;

  private String c3p0max_size;

  private String fileName;

  //defind propertis file name

  public String propertyFilePath="ModifyXml.properties";

  public void modifyXml()

  {

  try

  {

  Properties p = new Properties();

  HashMap<String,String> map=new HashMap<String,String>();

  map.put("xmlFileName",propertyFilePath);

  File f = new File(map.get("xmlFileName"));

  // load properties value from properties file

  if(f.exists() && f.isFile())

  {

  FileInputStream fis;

  fis = new FileInputStream(f);

  p.load(fis);

  dataBaseInfo =p.getProperty("hibernate.connection.url");

  userName =p.getProperty("hibernate.connection.username");

  userPass =p.getProperty("hibernate.connection.password");

  deploymentTitle=p.getProperty("deployment.title");

  c3p0max_size =p.getProperty("hibernate.c3p0.max_size");

  fileName =p.getProperty("hibernate.configrtion.path");

  fis.close();

  }

  SAXBuilder builder = new SAXBuilder();

  Document doc = builder.build(fileName);

  Element root = doc.getRootElement();

  XMLOutputter xmlOutputer = new XMLOutputter();

  builder.setValidation(false);

  builder.setEntityResolver(new EntityResolver() {

  public InputSource resolveEntity(String publicId,

  String systemId) throws SAXException, IOException {

  return new InputSource("./hibernate-configuration-3.0.dt

  d");

  }

  });

  // use XPath search your need node

  List list = XPath.selectNodes(root, "/hibernate-configuration/session-factory/property");

  for(int i = 0;i<list.size();i )

  {

  Element disk_element = (Element) list.get(i);

  String name= disk_element.getAttributue("name");

  if (name.equals("connection.url"))

  {

  System.out.println("database connection info" " " name);

  disk_element.setText(dataBaseInfo);

  }

  else if (name.equals("connection.username"))

  {

  System.out.println("database connection username" " " name);

  disk_element.setText(userName);

  }

  else if (name.equals("connection.password"))

  {

  System.out.println("database connection password" " " name);

  disk_element.setText(userPass);

  }

  else if (name.equals("c3p0.max_size"))

  {

  System.out.println("database max connnection is " c3p0max_size);

  disk_element.setText(c3p0max_size);

  }

  }

  // If you want to take out blank line you can uncomment format commect

  // Format format = Format.getPrettyFormat();

  // format.setEncoding("UTF-8");

  // xmlOutputer.setFormat(format);

  xmlOutputer.output(doc, new FileOutputStream(fileName));

  }catch (Exception e)

  {

  e.printStackTrace();

  e.getMessage();

  System.out.println("Can't modify xml file,please check your program again!!!!");

  }

  }

  public static void main(String[] args)

  {

  ModifyHibernateXml hibernate =new ModifyHibernateXml();

  hibernate.modifyXml();

  }

  }

  3:最后可以寫一個總的類,來調用以上兩個類,實現模塊話,厲害吧就幾個文件還來了一個模塊化.

  package util.xml;

  public class CloudServer

  {

  public static void main(String args[])

  {

  ModifyPropertiesFile t=new ModifyPropertiesFile();

  t.modifyPropertiesFile();

  ModifyHibernateXml hibernate =new ModifyHibernateXml();

  hibernate.modifyXml();

  }

  }


[火星人 ] 用shell和java實現自動部署已經有452次圍觀

http://coctec.com/docs/java/show-post-60827.html