====== Gant Tasks ====== ===== Ant Task Override ===== 기본 Ant 태스크에 공통 옵션을 적용해두고 호출할 수 있는 속임수 기법이 있다. Ant 태스크와 동일한 이름으로 함수를 만들고 거기서 ''ant.태스크이름''으로 명시적으로 Ant 태스크를 호출하면서 기본값을 지정해주면 된다. ''exec'' 참조. ===== body에 값 넣기 ===== Ant 태스크 중에는 XML Attribute가 아닌 ''body''에 값을 넣도록 처리하는 경우가 있다. 이 때 이것을 gant로 변경할 때는, 태스크 호출 맨 마지막에 바디의 내용을 문자열로 넣어주면 된다. 이런 저런 메시지들을 출력해 봅시다! 변환하면 ant.echo(level: 'error', """이런 저런 메시지들을 출력해 봅시다!""") ===== taskname ===== Ant에서 표준출력으로 내보낼 때 ''[echo] blah.. blah..'' 처럼 맨 앞에 태스크 이름이 나온다. ''taskname'' 속성을 통해 이를 조정할 수 있다. ant.echo(message: 'Hello World', taskname: 'echo:hello') 결과는.. [echo:hello] Hello World ===== TaskDef ===== * 기본이 아닌 외부 태스크 정의 [[http://ant.apache.org/manual/Tasks/taskdef.html|Ant taskdef]] * [[http://ant.apache.org/manual/develop.html|Writing Your Own Task]] * [[gant:antlib|Gant Ant Libraries]] ==== 클래스 이름 기반 taskdef ==== groovyc 태스크를 등록하는 것으로 알아본다. ''classpath'' 혹은 ''classpathref''로 해당 태스크가 속한 라이브러리의 클래스패스를 지정할 수 있다. taskdef(name: 'groovyc', classname: 'org.codehaus.groovy.ant.Groovyc') target(compile: 'Compile Groovy sources') { File dest = new File('classes') dest.mkdirs() File lib = new File('lib') lib.mkdirs() groovyc(srcdir: 'src', destdir: 'classes') { classpath { fileset(dir: 'lib') { include(name: '*.jar') } pathelement(path: 'classes') } } } ===== input ===== * [[http://ant.apache.org/manual/Tasks/input.html|input]] * 사용자로부터 문자열을 입력받아서 Ant 프라퍼티로 저장한다. * 비밀번호 등을 gant 스크립트에 직접 넣지말고 초기에 한 번 직접 입력하도록 하여, 프라퍼티에서 값을 뽑아 사용하도록 설계한다.(특히 ''sshexec'' 등에서 사용) // 비밀번호를 입력 받아서 server.password Ant 프라퍼티로 저장한다. input(message: '비밀번호를 입력해주세요.', addproperty: 'server.password') ===== exec ===== * [[http://ant.apache.org/manual/Tasks/exec.html|exec]] * 예시 exec(executable: '명령어', failonerror: true) { arg value: '명령행 인자 1' arg value: '명령행 인자 2' arg line: '명령행인자를 한 줄로 쭉..' env key: '환경변수이름', value: '환경변수값' } * ''faileonerror'' : 오류 발생시 빌드 즉시 실패 * ''dir'' : 명령을 실행한 디렉토리 명시 * ''resultproperty'' : ''faileonerror=false''일 때 리턴값을 저장할 프라퍼티 이름 ==== 명령을 받아서 파싱해서 ant.exec로 자동 넘기기 ==== 문자열 하나에 명령과 인자를 모두 넣은 상태에서 failonerror를 항상 true로 한 기본 exec를 만들어 두고 호출 할 수 있다. def exec(command, dir = '.') { def splittedCommand = command.split(' ') def executable = splittedCommand[0] ant.exec(executable: executable, failonerror: true, dir: dir) { if (splittedCommand.size() > 1) { splittedCommand[1..-1].each { arg value: it } } } } target(hello: 'world') { // ant.exec보다 로컬에 선언한 함수 exec가 우선된다. exec('cmd /c dir') } ===== sshexec ===== * [[http://ant.apache.org/manual/Tasks/sshexec.html|Ant sshexec]] * [[http://www.jcraft.com/jsch/index.html|JSch]] 0.1.42 이상 버전 필요. Ant 1.8.3 이상 권장. ''$GANT_HOME/lib''에 라이브러리를 복사해 둔다. * Ant의 sshexec는 리눅스간 뿐만 아니라 Windows에서 리눅스로의 접속도 매우 잘 지원한다. * Key 생성은 [[linux:ssh|Linux SSH]] 참조 * 실행시 ''com.jcraft.jsch.JSchException: reject HostKey:'' 오류가 발생한다면 ''trust: true'' 옵션을 지정한다. * ''ant.sshexec''로 명시적으로 ant 를 지정해야만 작동한다. * 위에 설명한 ''taskname'' 속성을 통해 접속중인 호스트명을 출력해주면 좋다. ==== 명령 사용자명/비밀번호 직접 지정방식 ==== target(remotels: 'Remote list files') { ant.sshexec(host: '호스트네임/IP주소', username: '계정명', password: '비밀번호', failonerror: true, trust: true, command: 'ls /etc') } ==== Key 기반 로그인 ==== target(remotels: 'Remote list files') { ant.sshexec(host: '호스트네임/IP주소', username: '계정명', keyfile: '${user.home}/.ssh/id_dsa', passphrase: '패스프레이즈', failonerror: true, trust: true, command: 'ls /etc') } ==== sudo ==== * ''sudo -S''는 ''stdin''으로 부터 비밀번호를 입력받도록 해주는 옵션이다. * ''inputstring'' (문자열), ''inputproperty'' (지정된 프라퍼티에 있는 값), ''input'' (파일의 내용) 등의 옵션은 ''stdin''으로 보낼 내용을 적는 방법들이다. * ''usepty''는 가상의 TTY를 생성해준다(''ssh -t''). **Ant 1.8.3 이상** 버전이 필요하다. * 위 옵션들을 적용하면 비밀번호 입력없이 ''sudo'' 사용이 가능해진다. target(remotels: 'Remote list files') { ant.sshexec(host: '호스트네임/IP주소', username: '계정명', keyfile: '${user.home}/.ssh/id_dsa', passphrase: '패스프레이즈', failonerror: true, trust: true, inputstring: 'sudo비밀번호\n', // \n로 엔터치게 해준다. usepty: true, command: 'sudo -S ls /var/log/httpd') } * ''echo''를 이용하는 방법도 있다. ''usepty: true''로 둔 상태에서 ''command: 'echo "비밀번호" | sudo -S 명령''' 형태로 사용한다. input* 계열의 경우 가끔 입력 시점과 그걸 받아들이는 시점에 약간의 시간차가 발생하여 제대로 작동 안하는 경우가 있다. ''echo''가 더 완벽하게 작동하는 듯. * ''/etc/sudoers'' 파일을 편집하여 아예 접속 계정에 비밀번호 없이 명령 실행 권한을 주는 방법도 있다. [[http://lang.snu.ac.kr/clab/?q=node/135|sudoers]] 참조. ==== 여러개의 명령 실행 ==== * ''command: 'cmd1; cmd2; cmd3; ...''' : 여러 명령어를 실행한다. * ''command: 'cmd1 & cmd2 & cmd3 & ...''' : 여러 명령어를 실행하되 앞의 명령이 성공해야만(and 조건) 다음 명령을 실행한다. * ''commandResource: '명령이 줄 단위로 들어있는 파일명''' : 파일에서 명령을 읽어서 실행한다. * ''commandResource''를 사용하면 ''inputstring''등의 ''stdin''으로 지정한 입력이 명령 실행시마다 반복 입력된다. ===== scp ===== * [[http://ant.apache.org/manual/Tasks/scp.html|Ant scp]] * 대부분의 SSH 관련 설정은 ''sshexec''와 동일하다. 단, 'username' 인자는 없다. * 사용자명은 복사 대상 혹은 목표 지점 지정시에 ''**사용자명[:비밀번호]@호스트명:/some/path**'' 형태로 지정한다. 비밀번호는 ''keyfile'' 혹은 ''password''로 대체 가능하다. ==== 로컬 파일을 리모트로 복사하기 ==== target(remoteCopy: 'Remote Copy files') { ant.scp(host: '${host}', keyfile: '../id_rsa', failonerror: true, trust: true, toDir: 'username@${host}:/some/directory') { fileset(dir: '/local/directory') { include(name: '**/*.jar') } } } ==== 복사 대상과 목표 지점 등록하기 ==== * 복사 대상지정 * ''file'' 단일 파일 지정. 로컬/원격 가능. ''@''가 들어있으면 원격으로 간주. * ''localFile'' : 단일 파일 지정. 무조건 로컬 파일. * ''remoteFile'' : 단일 파일 지정. 무조건 리모트 파일. ''사용자명@hostname:/some/path/..'' 형태. * ''fileset (dir: '로컬 경로') ... '' : 로컬 파일셋 지정. * 목표 지점 * ''toDir'' : 파일이 복사돼 들어갈 디렉토리. 로컬/원격 가능. * ''localTodir'' : 파일이 복사돼 들어갈 디렉토리. 무조건 로컬 디렉토리. * ''localToFile'' : 이하 생략... * ''remoteToDir'' * ''remoteToFile'' ===== SSH 관련 보안 ===== * ''sshexec''와 ''scp'' 사용시 빌드 파일에 사용자명과 비밀번호, ''passphrase'' 등을 직접 넣어 두는 것은 보안상 좋지 않다. * 두가지 방법으로 해결한다. * 위에서 설명한 ''input'' 태스크를 사용하여 프라퍼티로 만든 뒤에, 사용자명, 비밀번호 등은 실행시 입력 받아서 Ant 프라퍼티로 전달하는 것이 좋다. - 제일 좋다. * ''gant -D username=사용자명 -D password=비밀번호'' 형태로 명령행에서 Ant 프라퍼티를 입력받고, 이 값을 사용한다. - 다른 사용자가 ''ps''나 ''history'' 명령으로 명령행 옵션을 보는 것이 가능하다. target(inputSudoPassword: 'Input sudo password') { input(message: 'Input sudo password', addproperty: 'sudopassword') } target(remoteSudoLs: 'Remote sudo list files') { depends(inputSudoPassword) ant.sshexec(host: '${host}', username: '사용자명', keyfile: '/some/dir/id_rsa', failonerror: true, trust: true, usepty: true, command: 'echo "${sudopassword}" | sudo -S ls /var/log/httpd') }