package com.taobao.zeus.jobs.sub;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.IOUtils;

import com.taobao.zeus.jobs.JobContext;
import com.taobao.zeus.jobs.ProcessJob;
import com.taobao.zeus.util.PropertyKeys;
/**
 * 采用Shell脚本的任务
 * @author zhoufang
 *
 */
public class ShellJob extends ProcessJob{

	protected String shell;

	public ShellJob(JobContext jobContext) {
		super(jobContext);
	}
	public ShellJob(JobContext jobContext,String shell){
		this(jobContext);
		this.shell=shell;
	}
	
	@Override
	public Integer run() throws Exception {
		return super.run();
	}

	@Override
	public List<String> getCommandList() {
		String script=null;
		if(shell!=null){
			script=shell;
		}else{
			script=getProperties().getLocalProperty(PropertyKeys.JOB_SCRIPT);
		}
		
		OutputStreamWriter writer=null;
		try {
			File f=new File(jobContext.getWorkDir()+File.separator+(new Date().getTime())+".sh");
			if(!f.exists()){
				f.createNewFile();
			}
			writer=new OutputStreamWriter(new FileOutputStream(f),Charset.forName(jobContext.getProperties().getProperty("zeus.fs.encode", "utf-8")));
			writer.write(script);
			getProperties().setProperty(PropertyKeys.RUN_SHELLPATH, f.getAbsolutePath());
		} catch (Exception e) {
			jobContext.getJobHistory().getLog().appendZeusException(e);
		} finally{
			IOUtils.closeQuietly(writer);
		}
		
		String shellFilePath=getProperty(PropertyKeys.RUN_SHELLPATH, "");
		List<String> list=new ArrayList<String>();
		//修改权限
		list.add("chmod u+x " + shellFilePath);
		//格式转换
		list.add("dos2unix " + shellFilePath);
		//执行shell
		list.add("sh "+shellFilePath);
		return list;
	}
}

@@ -426,25 +426,211 @@
 eus.
-util.PropertyKeys
+store.Super;%0Aimport com.taobao.zeus.store.mysql.persistence.ZeusUser;%0Aimport com.taobao.zeus.util.PropertyKeys;%0Aimport com.taobao.zeus.web.LoginUser;%0Aimport com.taobao.zeus.web.platform.client.util.ZUser
 ;%0A/*
@@ -1817,16 +1817,19 @@
 H, %22%22);%0A
+%09%09%0A
 %09%09List%3CS
@@ -1865,16 +1865,118 @@
 ing%3E();%0A
+%09%09%0A%09%09// get current username%0A%09%09ZeusUser u= LoginUser.getUser();%0A%0A%09%09String shellName = u.getName();%0A%09%09%0A
 %09%09//%E4%BF%AE%E6%94%B9%E6%9D%83%E9%99%90
@@ -2082,26 +2082,83 @@
 shell%0A%09%09
-list.add(%22
+// run shell as current user%0A%09%09list.add(%22sudo -u %22 + shellName + %22 
 sh %22+she

change user authorization when exec shell script